New logging format
[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);
11     if (! $course = get_record("course", "id", $id) ) {
12         error("That's an invalid course id");
13     }
15     if (match_referer() && isset($HTTP_POST_VARS)) {    // form submitted
17         if ($password == $course->password) {
19             if (! enrol_student_in_course($USER->id, $course->id)) {
20                 error("An error occurred while trying to enrol you.");
21             }
22             add_to_log($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->id");
24             $USER->student["$id"] = true;
25             
26             if ($SESSION->wantsurl) {
27                 $destination = $SESSION->wantsurl;
28                 unset($SESSION->wantsurl);
29             } else {
30                 $destination = "$CFG->wwwroot/course/view.php?id=$id";
31             }
33             redirect($destination);
34     
35         } else {
36             $errormsg = "That entry key was incorrect, please try again".
37                         "<BR>(Here's a hint - it starts with \"".substr($actual_password,0,1)."\")";
38         }
39     }
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($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->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 ?>