2 // Allows a teacher/admin to login as another user (in stealth mode)
4 require("../config.php");
7 require_variable($id); // course id
8 optional_variable($user); // login as this user
9 optional_variable($return); // return to being the real user again
11 if (! $course = get_record("course", "id", $id)) {
12 error("Course ID was incorrect");
15 require_login($course->id);
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;
22 redirect($HTTP_REFERER);
26 // $user must be defined to go on
28 if (!isteacher($course->id)) {
29 error("Only teachers can use this page!");
32 if ($course->category and !isstudent($course->id, $user)) {
33 error("This student is not in this course!");
36 // Login as this student and return to course home page.
38 $teacher_name = "$USER->firstname $USER->lastname";
39 $teacher_id = "$USER->id";
41 $USER = get_user_info_from_db("id", $user);
42 $USER->loggedin = true;
43 $USER->site = $CFG->wwwroot;
44 $USER->realuser = $teacher_id;
47 set_moodle_cookie($USER->username);
48 $student_name = "$USER->firstname $USER->lastname";
50 add_to_log($course->id, "course", "loginas", "../user/view.php?id=$course->id&user=$user", "$teacher_name -> $student_name");
53 $strloginas = get_string("loginas");
54 $strloggedinas = get_string("loggedinas", "", $student_name);
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");