2 // Allows a teacher/admin to login as another user (in stealth mode)
4 require_once('../config.php');
5 require_once('lib.php');
7 /// Reset user back to their real self if needed
8 $return = optional_param('return', 0, PARAM_BOOL
); // return to the page we came from
10 if (!empty($USER->realuser
)) {
11 if (!confirm_sesskey()) {
12 print_error('confirmsesskeybad');
15 $USER = get_complete_user_data('id', $USER->realuser
);
16 load_all_capabilities(); // load all this user's normal capabilities
18 if (isset($SESSION->oldcurrentgroup
)) { // Restore previous "current group" cache.
19 $SESSION->currentgroup
= $SESSION->oldcurrentgroup
;
20 unset($SESSION->oldcurrentgroup
);
22 if (isset($SESSION->oldtimeaccess
)) { // Restore previous timeaccess settings
23 $USER->timeaccess
= $SESSION->oldtimeaccess
;
24 unset($SESSION->oldtimeaccess
);
27 if ($return and isset($_SERVER["HTTP_REFERER"])) { // That's all we wanted to do, so let's go back
28 redirect($_SERVER["HTTP_REFERER"]);
30 redirect($CFG->wwwroot
);
34 ///-------------------------------------
35 /// We are trying to log in as this user in the first place
37 $id = optional_param('id', SITEID
, PARAM_INT
); // course id
38 $userid = required_param('user', PARAM_INT
); // login as this user
40 if (!confirm_sesskey()) {
41 print_error('confirmsesskeybad');
44 if (! $course = get_record('course', 'id', $id)) {
45 error("Course ID was incorrect");
48 /// User must be logged in
50 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
51 $coursecontext = get_context_instance(CONTEXT_COURSE
, $course->id
);
55 if (has_capability('moodle/user:loginas', $systemcontext)) {
56 if (has_capability('moodle/site:doanything', $systemcontext, $userid, false)) {
57 print_error('nologinas');
59 $context = $systemcontext;
60 } else if (has_capability('moodle/user:loginas', $coursecontext)) {
61 require_login($course);
62 if (!has_capability('moodle/course:view', $coursecontext, $userid, false)) {
63 error('This user is not in this course!');
65 if (has_capability('moodle/site:doanything', $coursecontext, $userid, false)) {
66 print_error('nologinas');
68 $context = $coursecontext;
71 /// Remember current timeaccess settings for later
73 if (isset($USER->timeaccess
)) {
74 $SESSION->oldtimeaccess
= $USER->timeaccess
;
77 /// Login as this user and return to course home page.
79 $oldfullname = fullname($USER, true);
80 $olduserid = $USER->id
;
82 /// Create the new USER object with all details and reload needed capabilitites
83 $USER = get_complete_user_data('id', $userid);
84 $USER->realuser
= $olduserid;
85 $USER->loginascontext
= $context;
86 load_all_capabilities(); // reload capabilities
88 if (isset($SESSION->currentgroup
)) { // Remember current cache setting for later
89 $SESSION->oldcurrentgroup
= $SESSION->currentgroup
;
90 unset($SESSION->currentgroup
);
93 $newfullname = fullname($USER, true);
95 add_to_log($course->id
, "course", "loginas", "../user/view.php?id=$course->id&user=$userid", "$oldfullname -> $newfullname");
97 $strloginas = get_string('loginas');
98 $strloggedinas = get_string('loggedinas', '', $newfullname);
100 print_header_simple($strloggedinas, '', build_navigation(array(array('name'=>$strloggedinas, 'link'=>'','type'=>'misc'))),
101 '', '', true, ' ', navmenu($course));
102 notice($strloggedinas, "$CFG->wwwroot/course/view.php?id=$course->id");