Fixes bug MDL-8234, "New groups code & AS keyword"
[moodle-pu.git] / course / loginas.php
blob11f99b0b3033f1e3dbf430c9a7445793149d0597
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 $USER = get_complete_user_data('id', $USER->realuser);
12 load_all_capabilities(); // load all this user's normal capabilities
14 if (isset($SESSION->oldcurrentgroup)) { // Restore previous "current group" cache.
15 $SESSION->currentgroup = $SESSION->oldcurrentgroup;
16 unset($SESSION->oldcurrentgroup);
18 if (isset($SESSION->oldtimeaccess)) { // Restore previous timeaccess settings
19 $USER->timeaccess = $SESSION->oldtimeaccess;
20 unset($SESSION->oldtimeaccess);
23 if ($return and isset($_SERVER["HTTP_REFERER"])) { // That's all we wanted to do, so let's go back
24 redirect($_SERVER["HTTP_REFERER"]);
25 } else {
26 redirect($CFG->wwwroot);
31 ///-------------------------------------
32 /// We are trying to log in as this user in the first place
34 $id = required_param('id', PARAM_INT); // course id
35 $userid = required_param('user', PARAM_INT); // login as this user
37 if (!$site = get_site()) {
38 error("Site isn't defined!");
41 if (! $course = get_record("course", "id", $id)) {
42 error("Course ID was incorrect");
45 /// User must be logged in
47 if ($course->id == SITEID) {
48 require_login();
49 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
50 } else {
51 require_login($course->id);
52 $context = get_context_instance(CONTEXT_COURSE, $course->id);
53 if (!has_capability('moodle/course:view', $context, $userid, false)) {
54 error('This user is not in this course!');
58 /// User must have permissions
60 require_capability('moodle/user:loginas', $context);
63 /// Remember current timeaccess settings for later
65 if (isset($USER->timeaccess)) {
66 $SESSION->oldtimeaccess = $USER->timeaccess;
69 /// Login as this user and return to course home page.
71 $oldfullname = fullname($USER, true);
72 $olduserid = $USER->id;
74 $USER = get_complete_user_data('id', $userid); // Create the new USER object with all details
75 $USER->realuser = $olduserid;
77 load_user_capability('', $context); // load this user's capabilities for this context only
79 if (isset($SESSION->currentgroup)) { // Remember current cache setting for later
80 $SESSION->oldcurrentgroup = $SESSION->currentgroup;
81 unset($SESSION->currentgroup);
84 $newfullname = fullname($USER, true);
86 add_to_log($course->id, "course", "loginas", "../user/view.php?id=$course->id&amp;user=$userid", "$oldfullname -> $newfullname");
88 $strloginas = get_string('loginas');
89 $strloggedinas = get_string('loggedinas', '', $newfullname);
91 print_header_simple($strloggedinas, '', $strloggedinas, '', '', true, '&nbsp;', navmenu($course));
92 notice($strloggedinas, "$CFG->wwwroot/course/view.php?id=$course->id");