New file
[moodle.git] / enrol / enrol.class.php
bloba7706ede77e5b21769668bf6540651c4d895da4d
1 <?php /// $Id$
2 ///////////////////////////////////////////////////////////////////////////
3 // //
4 // NOTICE OF COPYRIGHT //
5 // //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
8 // //
9 // Copyright (C) 2004 Martin Dougiamas http://moodle.com //
10 // //
11 // This program is free software; you can redistribute it and/or modify //
12 // it under the terms of the GNU General Public License as published by //
13 // the Free Software Foundation; either version 2 of the License, or //
14 // (at your option) any later version. //
15 // //
16 // This program is distributed in the hope that it will be useful, //
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19 // GNU General Public License for more details: //
20 // //
21 // http://www.gnu.org/copyleft/gpl.html //
22 // //
23 ///////////////////////////////////////////////////////////////////////////
26 /**
27 * enrolment_base is the base class for enrolment plugins
29 * This class provides all the functionality for an enrolment plugin
30 * In fact it includes all the code for the default, "internal" method
31 * so that other plugins can override these as necessary.
34 class enrolment_base {
36 var $errormsg;
40 /**
41 * Returns information about the courses a student has access to
43 * Set the $user->student course array
44 * Set the $user->timeaccess course array
46 * @param user referenced object, must contain $user->id already set
48 function get_student_courses(&$user) {
50 if ($students = get_records("user_students", "userid", $user->id)) {
51 $currenttime = time();
52 foreach ($students as $student) {
54 /// Is course visible?
56 if (get_field("course", "visible", "id", $student->course)) {
58 /// Is the student enrolment active right now?
60 if ( ( $student->timestart == 0 or ( $currenttime > $student->timestart )) and
61 ( $student->timeend == 0 or ( $currenttime < $student->timeend )) ) {
62 $user->student[$student->course] = true;
63 $user->timeaccess[$student->course] = $student->timeaccess;
72 /**
73 * Returns information about the courses a student has access to
75 * Set the $user->teacher course array
76 * Set the $user->teacheredit course array
77 * Set the $user->timeaccess course array
79 * @param user referenced object, must contain $user->id already set
81 function get_teacher_courses(&$user) {
83 if ($teachers = get_records("user_teachers", "userid", $user->id)) {
84 $currenttime = time();
85 foreach ($teachers as $teacher) {
87 /// Is teacher only teaching this course for a specific time period?
89 if ( ( $teacher->timestart == 0 or ( $currenttime > $teacher->timestart )) and
90 ( $teacher->timeend == 0 or ( $currenttime < $teacher->timeend )) ) {
92 $user->teacher[$teacher->course] = true;
94 if ($teacher->editall) {
95 $user->teacheredit[$teacher->course] = true;
98 $user->timeaccess[$teacher->course] = $teacher->timeaccess;
108 * Prints the entry form/page for this enrolment
110 * This is only called from course/enrol.php
111 * Most plugins will probably override this to print payment
112 * forms etc, or even just a notice to say that manual enrolment
113 * is disabled
115 * @param course current course object
117 function print_entry($course) {
118 global $CFG, $USER, $SESSION, $THEME;
120 $strloginto = get_string("loginto", "", $course->shortname);
121 $strcourses = get_string("courses");
125 /// Automatically enrol into courses without password
127 if ($course->password == "") { // no password, so enrol
129 if (isguest()) {
130 add_to_log($course->id, "course", "guest", "view.php?id=$course->id", "$USER->id");
132 } else if (empty($_GET['confirm'])) {
134 print_header($strloginto, $course->fullname, "<a href=\".\">$strcourses</a> -> $strloginto");
135 echo "<br />";
136 notice_yesno(get_string("enrolmentconfirmation"), "enrol.php?id=$course->id&confirm=1", $CFG->wwwroot);
137 print_footer();
138 exit;
140 } else {
141 if ($course->enrolperiod) {
142 $timestart = time();
143 $timeend = time() + $course->enrolperiod;
144 } else {
145 $timestart = $timeend = 0;
148 if (! enrol_student($USER->id, $course->id, $timestart, $timeend)) {
149 error("An error occurred while trying to enrol you.");
152 $subject = get_string("welcometocourse", "", $course->fullname);
153 $a->coursename = $course->fullname;
154 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id";
155 $message = get_string("welcometocoursetext", "", $a);
156 if (! $teacher = get_teacher($course->id)) {
157 $teacher = get_admin();
159 email_to_user($USER, $teacher, $subject, $message);
161 add_to_log($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->id");
163 $USER->student[$course->id] = true;
165 if ($SESSION->wantsurl) {
166 $destination = $SESSION->wantsurl;
167 unset($SESSION->wantsurl);
168 } else {
169 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
172 redirect($destination);
176 $teacher = get_teacher($course->id);
177 if (!isset($password)) {
178 $password = "";
182 print_header($strloginto, $course->fullname, "<A HREF=\".\">$strcourses</A> -> $strloginto", "form.password");
184 print_course($course);
186 include("$CFG->dirroot/enrol/internal/enrol.html");
188 print_footer();
195 * The other half to print_entry, this checks the form data
197 * This function checks that the user has completed the task on the
198 * enrolment entry page and then enrolls them.
200 * @param form the form data submitted, as an object
201 * @param course the current course, as an object
203 function check_entry($form, $course) {
204 global $CFG, $USER, $SESSION, $THEME;
206 if ($form->password == $course->password) {
208 if (isguest()) {
210 add_to_log($course->id, "course", "guest", "view.php?id=$course->id", $_SERVER['REMOTE_ADDR']);
212 } else { /// Update or add new enrolment
214 if ($course->enrolperiod) {
215 $timestart = time();
216 $timeend = $timestart + $course->enrolperiod;
217 } else {
218 $timestart = $timeend = 0;
221 if (! enrol_student($USER->id, $course->id, $timestart, $timeend)) {
222 error("An error occurred while trying to enrol you.");
225 $subject = get_string("welcometocourse", "", $course->fullname);
226 $a->coursename = $course->fullname;
227 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id";
228 $message = get_string("welcometocoursetext", "", $a);
230 if (! $teacher = get_teacher($course->id)) {
231 $teacher = get_admin();
234 email_to_user($USER, $teacher, $subject, $message);
235 add_to_log($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->id");
238 $USER->student[$course->id] = true;
240 if ($SESSION->wantsurl) {
241 $destination = $SESSION->wantsurl;
242 unset($SESSION->wantsurl);
243 } else {
244 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
247 redirect($destination);
249 } else {
250 $this->errormsg = get_string("enrolmentkeyhint", "", substr($course->password,0,1));
257 * Prints a form for configuring the current enrolment plugin
259 * This function is called from admin/enrol.php, and outputs a
260 * full page with a form for defining the current enrolment plugin.
262 * @param page an object containing all the data for this page
264 function config_form($page) {
270 * Processes and stored configuration data for the enrolment plugin
272 * Processes and stored configuration data for the enrolment plugin
274 * @param config all the configuration data as entered by the admin
276 function process_config($config) {
278 $return = true;
280 foreach ($config as $name => $value) {
281 if (!set_config($name, $value)) {
282 $return = false;
286 return $return;
291 * This function is run by admin/cron.php every time
293 * The cron function can perform regular checks for the current
294 * enrollment plugin. For example it can check a foreign database,
295 * all look for a file to pull data in from
298 function cron() {
299 // Delete students from all courses where their enrolment period has expired
301 $select = "timeend > '0' AND timeend < '" . time() . "'";
303 if ($students = get_records_select('user_students', $select)) {
304 foreach ($students as $student) {
305 unenrol_student($student->userid, $student->course);
308 if ($teachers = get_records_select('user_teachers', $select)) {
309 foreach ($teachers as $teacher) {
310 remove_teacher($teacher->userid, $teacher->course);
317 * Returns the relevant icons for a course
319 * Returns the relevant icons for a course
321 * @param course the current course, as an object
323 function get_access_icons($course) {
324 global $CFG;
326 $str = '';
328 if ($course->guest) {
329 $strallowguests = get_string("allowguests");
330 $str .= "<a title=\"$strallowguests\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
331 $str .= "<img vspace=4 alt=\"$strallowguests\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/guest.gif\"></a>&nbsp;&nbsp;";
333 if ($course->password) {
334 $strrequireskey = get_string("requireskey");
335 $str .= "<a title=\"$strrequireskey\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">";
336 $str .= "<img vspace=4 alt=\"$strrequireskey\" height=16 width=16 border=0 src=\"$CFG->pixpath/i/key.gif\"></a>";
339 return $str;
343 } /// end of class