| 1 |
efrain |
1 |
<?php
|
|
|
2 |
// Allows a teacher/admin to login as another user (in stealth mode).
|
|
|
3 |
|
|
|
4 |
require_once('../config.php');
|
|
|
5 |
require_once('lib.php');
|
|
|
6 |
|
|
|
7 |
$id = optional_param('id', SITEID, PARAM_INT); // course id
|
|
|
8 |
$redirect = optional_param('redirect', 0, PARAM_BOOL);
|
|
|
9 |
|
|
|
10 |
$url = new moodle_url('/course/loginas.php', array('id'=>$id));
|
|
|
11 |
$PAGE->set_url($url);
|
|
|
12 |
|
|
|
13 |
// Reset user back to their real self if needed, for security reasons you need to log out and log in again.
|
|
|
14 |
if (\core\session\manager::is_loggedinas()) {
|
|
|
15 |
require_sesskey();
|
|
|
16 |
require_logout();
|
|
|
17 |
|
|
|
18 |
// We can not set wanted URL here because the session is closed.
|
|
|
19 |
redirect(new moodle_url($url, array('redirect'=>1)));
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
if ($redirect) {
|
|
|
23 |
if ($id && $id != SITEID) {
|
|
|
24 |
$SESSION->wantsurl = "$CFG->wwwroot/course/view.php?id=".$id;
|
|
|
25 |
} else {
|
|
|
26 |
$SESSION->wantsurl = "$CFG->wwwroot/?redirect=1";
|
|
|
27 |
}
|
|
|
28 |
|
|
|
29 |
redirect(get_login_url());
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
// Try log in as this user.
|
|
|
33 |
$userid = required_param('user', PARAM_INT);
|
|
|
34 |
|
|
|
35 |
require_sesskey();
|
|
|
36 |
$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
|
|
|
37 |
|
|
|
38 |
// User must be logged in.
|
|
|
39 |
require_login();
|
|
|
40 |
|
| 1441 |
ariadna |
41 |
$user = $DB->get_record('user', ['id' => $userid]);
|
| 1 |
efrain |
42 |
|
| 1441 |
ariadna |
43 |
$context = \core\session\loginas_helper::get_context_user_can_login_as($USER, $user, $course);
|
|
|
44 |
if (empty($context)) {
|
|
|
45 |
throw new moodle_exception('nologinas');
|
| 1 |
efrain |
46 |
}
|
|
|
47 |
|
| 1441 |
ariadna |
48 |
$PAGE->set_context($context);
|
|
|
49 |
|
| 1 |
efrain |
50 |
// Login as this user and return to course home page.
|
|
|
51 |
\core\session\manager::loginas($userid, $context);
|
|
|
52 |
// Add a notification to let the logged in as user know that all content will be force cleaned
|
|
|
53 |
// while in this session.
|
|
|
54 |
\core\notification::info(get_string('sessionforceclean', 'core'));
|
|
|
55 |
$newfullname = fullname($USER, true);
|
|
|
56 |
|
|
|
57 |
$strloginas = get_string('loginas');
|
|
|
58 |
$strloggedinas = get_string('loggedinas', '', $newfullname);
|
|
|
59 |
|
|
|
60 |
$PAGE->set_title($strloggedinas);
|
|
|
61 |
$PAGE->set_heading($course->fullname);
|
|
|
62 |
$PAGE->navbar->add($strloggedinas);
|
|
|
63 |
|
|
|
64 |
if ($course->id != SITEID) {
|
|
|
65 |
$returnurl = course_get_url($course);
|
|
|
66 |
} else {
|
|
|
67 |
$returnurl = new moodle_url('/', ['redirect' => 1]);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
notice($strloggedinas, $returnurl);
|