b0db697111e43559f358b4bee80819e424519770
[moodle.git] / course / login.php
1 <?PHP // $Id$
3 //  Asks for a course pass key, once only
5     require("../config.php");
6     require("lib.php");
8     require_login();
9     require_variable($id);
12     if (match_referer() && isset($HTTP_POST_VARS)) {    // form submitted
14         $actual_password = get_field("course", "password", "id", $id);
16         if ($password == $actual_password) {
18             enrol_student_in_course($USER->id, $id);
19             add_to_log("Enrolled in course", $id);
21             $USER->student["$id"] = true;
22             
23             if ($SESSION->wantsurl) {
24                 $destination = $SESSION->wantsurl;
25                 unset($SESSION->wantsurl);
26             } else {
27                 $destination = "$CFG->wwwroot/course/view.php?id=$id";
28             }
30             redirect($destination);
31     
32         } else {
33             $errormsg = "That entry key was incorrect, please try again".
34                         "<BR>(Here's a hint - it starts with \"".substr($actual_password,0,1)."\")";
35         }
36     }
38     if (! $course = get_record("course", "id", $id) ) {
39         error("That's an invalid course id");
40     }
42     if (! $site = get_record("course", "category", "0") ) {
43         error("Could not find a site!");
44     }
46     if ($course->password == "") {   // no password, so enrol
47         if (! enrol_student_in_course($USER->id, $course->id)) {
48             error("An error occurred while trying to enrol you.");
49         }
51         add_to_log("Enrolled in course", $id);
53         $USER->student["$id"] = true;
54         
55         if ($SESSION->wantsurl) {
56             $destination = $SESSION->wantsurl;
57             unset($SESSION->wantsurl);
58         } else {
59             $destination = "$CFG->wwwroot/course/view.php?id=$id";
60         }
62         redirect($destination);
63     }
65     $teacher = get_teacher($course->id);
67     print_header("Login to $course->shortname", "Login to $course->shortname", "<A HREF=\".\">Courses</A> -> Login to $course->shortname", "form.password"); 
69     print_course($course); 
71     include("login.html");
73     print_footer();
76 //// FUNCTIONS /////////////////////////////////////////////
78 function enrol_student_in_course($user, $course) {
79     
80     global $db;
82         $timenow = time();
84         $rs = $db->Execute("INSERT INTO user_students (user, course, start, end, time) 
85                         VALUES ($user, $course, 0, 0, $timenow)");
86         if ($rs) {
87                 return true;
88         } else {
89             return false;
90         }
91 }
93 ?>