When using loginas to be a student, you can now return to being
[moodle.git] / course / loginas.php
CommitLineData
f9903ed0 1<?PHP // $Id$
97c270e9 2 // Allows a teacher/admin to login as another user (in stealth mode)
f9903ed0 3
7ad5e3ad 4 require("../config.php");
5 require("lib.php");
f9903ed0 6
7 require_variable($id); // course id
65ccdd8c 8 optional_variable($user); // login as this user
9 optional_variable($return); // return to being the real user again
f9903ed0 10
11 if (! $course = get_record("course", "id", $id)) {
12 error("Course ID was incorrect");
13 }
14
7ad5e3ad 15 require_login($course->id);
f9903ed0 16
65ccdd8c 17 if ($return and $USER->realuser) {
18 $USER = get_user_info_from_db("id", $USER->realuser);
19 $USER->loggedin = true;
20 $USER->site = $CFG->wwwroot;
21 save_session("USER");
22 redirect($HTTP_REFERER);
23 exit;
24 }
25
26 // $user must be defined to go on
27
f9903ed0 28 if (!isteacher($course->id)) {
29 error("Only teachers can use this page!");
30 }
31
adecd142 32 if ($course->category and !isstudent($course->id, $user)) {
7ad5e3ad 33 error("This student is not in this course!");
f9903ed0 34 }
35
36 // Login as this student and return to course home page.
37
38 $teacher_name = "$USER->firstname $USER->lastname";
14147859 39 $teacher_id = "$USER->id";
f9903ed0 40
41 $USER = get_user_info_from_db("id", $user);
42 $USER->loggedin = true;
45f21575 43 $USER->site = $CFG->wwwroot;
4b1371a7 44 $USER->realuser = $teacher_id;
45 save_session("USER");
46
f9903ed0 47 set_moodle_cookie($USER->username);
f9903ed0 48 $student_name = "$USER->firstname $USER->lastname";
14147859 49
50 add_to_log($course->id, "course", "loginas", "../user/view.php?id=$course->id&user=$user", "$teacher_name -> $student_name");
51
f9903ed0 52
4b1371a7 53 $strloginas = get_string("loginas");
54 $strloggedinas = get_string("loggedinas", "", $student_name);
55
56 print_header("$course->fullname: $strloginas $student_name", "$course->fullname",
57 "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> ->
58 $strloginas $student_name");
59 notice($strloggedinas, "$CFG->wwwroot/course/view.php?id=$course->id");
f9903ed0 60
8223d271 61
f9903ed0 62?>