Polished.
[moodle.git] / course / enrol.php
blobe9b64a2df3ef061a955d93d2fbb8457710306ca3
1 <?PHP // $Id$
2 // Asks for a course pass key, once only, and enrols that user
4 require_once("../config.php");
5 require_once("lib.php");
7 require_variable($id);
9 require_login();
11 if (! $course = get_record("course", "id", $id) ) {
12 error("That's an invalid course id");
15 if ($form = data_submitted()) {
17 if ($form->password == $course->password) {
19 if (isguest()) {
20 add_to_log($course->id, "course", "guest", "view.php?id=$course->id", "$REMOTE_ADDR, $REMOTE_HOST");
22 } else if (!record_exists("user_students", "userid", $USER->id, "course", $course->id)) {
24 if (! enrol_student($USER->id, $course->id)) {
25 error("An error occurred while trying to enrol you.");
28 $subject = get_string("welcometocourse", "", $course->fullname);
30 $a->coursename = $course->fullname;
31 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id";
32 $message = get_string("welcometocoursetext", "", $a);
34 if (! $teacher = get_teacher($course->id)) {
35 $teacher = get_admin();
37 email_to_user($USER, $teacher, $subject, $message);
39 add_to_log($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->id");
42 $USER->student["$id"] = true;
44 if ($SESSION->wantsurl) {
45 $destination = $SESSION->wantsurl;
46 unset($SESSION->wantsurl);
47 } else {
48 $destination = "$CFG->wwwroot/course/view.php?id=$id";
51 redirect($destination);
53 } else {
54 $errormsg = get_string("enrolmentkeyhint", "", substr($course->password,0,1));
59 if (! $site = get_site()) {
60 error("Could not find a site!");
63 if ($course->password == "") { // no password, so enrol
65 if (isguest()) {
66 add_to_log($course->id, "course", "guest", "view.php?id=$course->id", "$USER->id");
67 } else {
68 if (! enrol_student($USER->id, $course->id)) {
69 error("An error occurred while trying to enrol you.");
71 add_to_log($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->id");
74 $USER->student["$id"] = true;
76 if ($SESSION->wantsurl) {
77 $destination = $SESSION->wantsurl;
78 unset($SESSION->wantsurl);
79 } else {
80 $destination = "$CFG->wwwroot/course/view.php?id=$id";
83 redirect($destination);
86 $teacher = get_teacher($course->id);
88 $strloginto = get_string("loginto", "", $course->shortname);
89 $strcourses = get_string("courses");
91 print_header($strloginto, $strloginto, "<A HREF=\".\">$strcourses</A> -> $strloginto", "form.password");
93 print_course($course);
95 include("enrol.html");
97 print_footer();