Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / course / loginas.php
blob3bddfe5aaf6c9fa8e120d8acc613f474956f1750
1 <?php // $Id$
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);
26 if (isset($SESSION->grade_last_report)) { // Restore grade defaults if any
27 $USER->grade_last_report = $SESSION->grade_last_report;
28 unset($SESSION->grade_last_report);
31 if ($return and isset($_SERVER["HTTP_REFERER"])) { // That's all we wanted to do, so let's go back
32 redirect($_SERVER["HTTP_REFERER"]);
33 } else {
34 redirect($CFG->wwwroot);
38 ///-------------------------------------
39 /// We are trying to log in as this user in the first place
41 $id = optional_param('id', SITEID, PARAM_INT); // course id
42 $userid = required_param('user', PARAM_INT); // login as this user
44 if (!confirm_sesskey()) {
45 print_error('confirmsesskeybad');
48 if (! $course = get_record('course', 'id', $id)) {
49 error("Course ID was incorrect");
52 /// User must be logged in
54 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
55 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
57 require_login();
59 if (has_capability('moodle/user:loginas', $systemcontext)) {
60 if (has_capability('moodle/site:doanything', $systemcontext, $userid, false)) {
61 print_error('nologinas');
63 $context = $systemcontext;
64 } else {
65 require_login($course);
66 require_capability('moodle/user:loginas', $coursecontext);
67 if (!has_capability('moodle/course:view', $coursecontext, $userid, false)) {
68 error('This user is not in this course!');
70 if (has_capability('moodle/site:doanything', $coursecontext, $userid, false)) {
71 print_error('nologinas');
73 $context = $coursecontext;
76 /// Remember current timeaccess settings for later
78 if (isset($USER->timeaccess)) {
79 $SESSION->oldtimeaccess = $USER->timeaccess;
81 if (isset($USER->grade_last_report)) {
82 $SESSION->grade_last_report = $USER->grade_last_report;
85 /// Login as this user and return to course home page.
87 $oldfullname = fullname($USER, true);
88 $olduserid = $USER->id;
90 /// Create the new USER object with all details and reload needed capabilitites
91 $USER = get_complete_user_data('id', $userid);
92 $USER->realuser = $olduserid;
93 $USER->loginascontext = $context;
94 check_enrolment_plugins($USER);
95 load_all_capabilities(); // reload capabilities
97 if (isset($SESSION->currentgroup)) { // Remember current cache setting for later
98 $SESSION->oldcurrentgroup = $SESSION->currentgroup;
99 unset($SESSION->currentgroup);
102 $newfullname = fullname($USER, true);
104 add_to_log($course->id, "course", "loginas", "../user/view.php?id=$course->id&amp;user=$userid", "$oldfullname -> $newfullname");
106 $strloginas = get_string('loginas');
107 $strloggedinas = get_string('loggedinas', '', $newfullname);
109 print_header_simple($strloggedinas, '', build_navigation(array(array('name'=>$strloggedinas, 'link'=>'','type'=>'misc'))),
110 '', '', true, '&nbsp;', navmenu($course));
111 notice($strloggedinas, "$CFG->wwwroot/course/view.php?id=$course->id");