Add some space between the environment table and environment box.
[pfb-moodle.git] / course / loginas.php
blob7fd8e2f135aac11404373619f98ddb510c5b16a7
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!');
56 if (has_capability('moodle/site:doanything', $context, $userid, false)) {
57 print_error('nologinas');
61 /// User must have permissions
63 require_capability('moodle/user:loginas', $context);
66 /// Remember current timeaccess settings for later
68 if (isset($USER->timeaccess)) {
69 $SESSION->oldtimeaccess = $USER->timeaccess;
72 /// Login as this user and return to course home page.
74 $oldfullname = fullname($USER, true);
75 $olduserid = $USER->id;
77 $USER = get_complete_user_data('id', $userid); // Create the new USER object with all details
78 $USER->realuser = $olduserid;
80 load_user_capability('', $context); // load this user's capabilities for this context only
82 if (isset($SESSION->currentgroup)) { // Remember current cache setting for later
83 $SESSION->oldcurrentgroup = $SESSION->currentgroup;
84 unset($SESSION->currentgroup);
87 $newfullname = fullname($USER, true);
89 add_to_log($course->id, "course", "loginas", "../user/view.php?id=$course->id&amp;user=$userid", "$oldfullname -> $newfullname");
91 $strloginas = get_string('loginas');
92 $strloggedinas = get_string('loggedinas', '', $newfullname);
94 print_header_simple($strloggedinas, '', $strloggedinas, '', '', true, '&nbsp;', navmenu($course));
95 notice($strloggedinas, "$CFG->wwwroot/course/view.php?id=$course->id");