Fix subject lines and corrupted emails from messages MDL-13364 (merged)
[moodle.git] / enrol / manual / enrol.php
blobe231e5d35b65f010212a924e44ed14030f5304f2
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_plugin_manual is the default enrolment plugin
29 * This class provides all the functionality for an enrolment plugin
30 * In fact it includes all the code for the default, "manual" method
31 * so that other plugins can override these as necessary.
34 class enrolment_plugin_manual {
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] = $student->enrol;
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] = $teacher->enrol;
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']) && empty($_GET['cancel'])) {
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&amp;confirm=1", "enrol.php?id=$course->id&amp;cancel=1");
137 print_footer();
138 exit;
140 } elseif (!empty($_GET['confirm'])) {
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, 'manual')) {
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);
173 } elseif (!empty($_GET['cancel'])) {
174 unset($SESSION->wantsurl);
175 redirect($CFG->wwwroot);
179 $teacher = get_teacher($course->id);
180 if (!isset($password)) {
181 $password = "";
185 print_header($strloginto, $course->fullname, "<a href=\".\">$strcourses</a> -> $strloginto", "form.password");
187 print_course($course, "80%");
189 include("$CFG->dirroot/enrol/manual/enrol.html");
191 print_footer();
198 * The other half to print_entry, this checks the form data
200 * This function checks that the user has completed the task on the
201 * enrolment entry page and then enrolls them.
203 * @param form the form data submitted, as an object
204 * @param course the current course, as an object
206 function check_entry($form, $course) {
207 global $CFG, $USER, $SESSION, $THEME;
209 if (empty($form->password)) {
210 $form->password = '';
213 if (empty($course->password)) {
214 // do not allow entry when no course password set
215 // automatic login when manual primary, no login when secondary at all!!
216 error("illegal enrolment attempted");
219 $groupid = $this->check_group_entry($course->id, $form->password);
220 if (($form->password == $course->password) or ($groupid !== false) ) {
222 if (isguest()) {
224 add_to_log($course->id, "course", "guest", "view.php?id=$course->id", $_SERVER['REMOTE_ADDR']);
226 } else { /// Update or add new enrolment
228 if ($course->enrolperiod) {
229 $timestart = time();
230 $timeend = $timestart + $course->enrolperiod;
231 } else {
232 $timestart = $timeend = 0;
235 if (! enrol_student($USER->id, $course->id, $timestart, $timeend, 'manual')) {
236 error("An error occurred while trying to enrol you.");
239 if ($groupid !== false) {
240 if (add_user_to_group($groupid, $USER->id)) {
241 $USER->groupmember[$course->id] = $groupid;
242 } else {
243 error("An error occurred while trying to add you to a group");
247 $subject = get_string("welcometocourse", "", $course->fullname);
248 $a->coursename = $course->fullname;
249 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id";
250 $message = get_string("welcometocoursetext", "", $a);
252 if (! $teacher = get_teacher($course->id)) {
253 $teacher = get_admin();
256 email_to_user($USER, $teacher, $subject, $message);
257 add_to_log($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->id");
260 $USER->student[$course->id] = true;
262 if ($SESSION->wantsurl) {
263 $destination = $SESSION->wantsurl;
264 unset($SESSION->wantsurl);
265 } else {
266 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
269 redirect($destination);
271 } else {
272 $this->errormsg = get_string("enrolmentkeyhint", "", substr($course->password,0,1));
279 * Check if the given enrolment key matches a group enrolment key for the given course
281 * @param courseid the current course id
282 * @param password the submitted enrolment key
284 function check_group_entry ($courseid, $password) {
285 $ingroup = false;
286 if ( ($groups = get_groups($courseid)) !== false ) {
287 foreach ($groups as $group)
288 if ( !empty($group->password) and ($password == $group->password) )
289 $ingroup = $group->id;
291 return $ingroup;
296 * Prints a form for configuring the current enrolment plugin
298 * This function is called from admin/enrol.php, and outputs a
299 * full page with a form for defining the current enrolment plugin.
301 * @param page an object containing all the data for this page
303 function config_form($page) {
309 * Processes and stored configuration data for the enrolment plugin
311 * @param config all the configuration data as entered by the admin
313 function process_config($config) {
315 $return = true;
317 foreach ($config as $name => $value) {
318 if (!set_config($name, $value)) {
319 $return = false;
323 return $return;
328 * This function is run by admin/cron.php every time
330 * The cron function can perform regular checks for the current
331 * enrollment plugin. For example it can check a foreign database,
332 * all look for a file to pull data in from
335 function cron() {
336 // Delete students from all courses where their enrolment period has expired
338 $select = "timeend > '0' AND timeend < '" . time() . "'";
340 if ($students = get_records_select('user_students', $select)) {
341 foreach ($students as $student) {
342 if ($course = get_record('course', 'id', $student->course)) {
343 if (empty($course->enrolperiod)) { // This overrides student timeend
344 continue;
347 unenrol_student($student->userid, $student->course);
350 if ($teachers = get_records_select('user_teachers', $select)) {
351 foreach ($teachers as $teacher) {
352 remove_teacher($teacher->userid, $teacher->course);
356 // Notify teachers/students about students who's enrolment are going to expire
357 global $CFG;
358 if (empty($CFG->lastexpirynotify)) {
359 $CFG->lastexpirynotify = 0;
362 if ($CFG->lastexpirynotify < date('Ymd') && ($courses = get_records_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0'))) {
363 $site = get_site();
364 $admin = get_admin();
365 $strexpirynotify = get_string('expirynotify');
366 foreach ($courses as $course) {
367 $a = new stdClass();
368 $a->course = $course->shortname .' '. $course->fullname;
369 $a->threshold = $course->expirythreshold / 86400;
370 $a->extendurl = $CFG->wwwroot . '/user/index.php?id=' . $course->id;
371 $a->current = array();
372 $a->past = array();
373 $a->studentstr = $course->student;
374 $a->teacherstr = $course->teacher;
375 $a->current = $a->past = array();
376 $expiry = time() + $course->expirythreshold;
377 $sql = "SELECT * FROM {$CFG->prefix}user u INNER JOIN {$CFG->prefix}user_students s ON u.id=s.userid WHERE s.course = $course->id AND s.timeend > 0 AND s.timeend <= $expiry";
378 if ($students = get_records_sql($sql)) {
379 $teacher = get_teacher($course->id);
380 $strexpirynotifystudentsemail = get_string('expirynotifystudentsemail', '', $a);
381 foreach ($students as $student) {
382 if ($student->timeend < ($expiry - 86400)) {
383 $a->past[] = fullname($student) . " <$student->email>";
384 } else {
385 $a->current[] = fullname($student) . " <$student->email>";
386 if ($course->notifystudents) {
387 // Send this guy notice
388 email_to_user($student, $teacher, $site->fullname .' '. $strexpirynotify, $strexpirynotifystudentsemail);
393 $a->current = implode("\n", $a->current);
394 $a->past = implode("\n", $a->past);
395 $strexpirynotifyemail = get_string('expirynotifyemail', '', $a);
396 if ($a->current || $a->past) {
397 $sql = "SELECT u.* FROM {$CFG->prefix}user u INNER JOIN {$CFG->prefix}user_teachers t ON u.id=t.userid WHERE t.course = $course->id";
398 if ($teachers = get_records_sql($sql)) {
399 foreach ($teachers as $teacher) {
400 email_to_user($teacher, $admin, $a->course .' '. $strexpirynotify, $strexpirynotifyemail);
404 set_config('lastexpirynotify', date('Ymd'));
411 * Returns the relevant icons for a course
413 * @param course the current course, as an object
415 function get_access_icons($course) {
416 global $CFG;
418 $str = '';
420 if (!empty($course->guest)) {
421 $strallowguests = get_string("allowguests");
422 $str .= '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
423 $str .= '<img vspace="4" alt="'.$strallowguests.'" height="16" width="16" border="0" '.
424 'src="'.$CFG->pixpath.'/i/guest.gif" /></a>&nbsp;&nbsp;';
426 if (!empty($course->password)) {
427 $strrequireskey = get_string("requireskey");
428 $str .= '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
429 $str .= '<img vspace="4" alt="'.$strrequireskey.'" height="16" width="16" border="0" src="'.$CFG->pixpath.'/i/key.gif" /></a>';
432 return $str;
436 } /// end of class