2 ///////////////////////////////////////////////////////////////////////////
4 // NOTICE OF COPYRIGHT //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
9 // Copyright (C) 2004 Martin Dougiamas http://moodle.com //
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. //
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: //
21 // http://www.gnu.org/copyleft/gpl.html //
23 ///////////////////////////////////////////////////////////////////////////
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
{
39 * Prints the entry form/page for this enrolment
41 * This is only called from course/enrol.php
42 * Most plugins will probably override this to print payment
43 * forms etc, or even just a notice to say that manual enrolment
46 * @param course current course object
48 function print_entry($course) {
49 global $CFG, $USER, $SESSION, $THEME;
51 $strloginto = get_string('loginto', '', $course->shortname
);
52 $strcourses = get_string('courses');
54 /// Automatically enrol into courses without password
56 $context = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
58 if ($course->password
== '') { // no password, so enrol
60 if (has_capability('moodle/legacy:guest', $context, $USER->id
, false)) {
61 add_to_log($course->id
, 'course', 'guest', 'view.php?id='.$course->id
, getremoteaddr());
63 } else if (empty($_GET['confirm']) && empty($_GET['cancel'])) {
65 print_header($strloginto, $course->fullname
, "<a href=\".\">$strcourses</a> -> $strloginto");
67 notice_yesno(get_string('enrolmentconfirmation'), "enrol.php?id=$course->id&confirm=1",
68 "enrol.php?id=$course->id&cancel=1");
72 } else if (!empty($_GET['confirm'])) {
74 if (!enrol_into_course($course, $USER, 'manual')) {
75 print_error('couldnotassignrole');
78 if (!empty($SESSION->wantsurl
)) {
79 $destination = $SESSION->wantsurl
;
80 unset($SESSION->wantsurl
);
82 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
85 redirect($destination);
87 } else if (!empty($_GET['cancel'])) {
88 unset($SESSION->wantsurl
);
89 if (!empty($SESSION->enrolcancel
)) {
90 $destination = $SESSION->enrolcancel
;
91 unset($SESSION->enrolcancel
);
93 $destination = $CFG->wwwroot
;
95 redirect($destination);
99 $teacher = get_teacher($course->id
);
100 if (!isset($password)) {
104 print_header($strloginto, $course->fullname
, "<a href=\".\">$strcourses</a> -> $strloginto", "form.password");
106 print_course($course, "80%");
108 include("$CFG->dirroot/enrol/manual/enrol.html");
117 * The other half to print_entry, this checks the form data
119 * This function checks that the user has completed the task on the
120 * enrolment entry page and then enrolls them.
122 * @param form the form data submitted, as an object
123 * @param course the current course, as an object
125 function check_entry($form, $course) {
126 global $CFG, $USER, $SESSION, $THEME;
128 if (empty($form->password
)) {
129 $form->password
= '';
132 if (empty($course->password
)) {
133 // do not allow entry when no course password set
134 // automatic login when manual primary, no login when secondary at all!!
135 error('illegal enrolment attempted');
138 $groupid = $this->check_group_entry($course->id
, $form->password
);
140 if (($form->password
== $course->password
) or ($groupid !== false) ) {
142 if ($USER->username
== 'guest') { // only real user guest, do not use this for users with guest role
143 $USER->enrolkey
[$course->id
] = true;
144 add_to_log($course->id
, 'course', 'guest', 'view.php?id='.$course->id
, getremoteaddr());
146 } else { /// Update or add new enrolment
147 if (enrol_into_course($course, $USER, 'manual')) {
148 if ($groupid !== false) {
149 if (!add_user_to_group($groupid, $USER->id
)) {
150 print_error('couldnotassigngroup');
154 print_error('couldnotassignrole');
158 if ($SESSION->wantsurl
) {
159 $destination = $SESSION->wantsurl
;
160 unset($SESSION->wantsurl
);
162 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
165 redirect($destination);
168 $this->errormsg
= get_string('enrolmentkeyhint', '', substr($course->password
,0,1));
174 * Check if the given enrolment key matches a group enrolment key for the given course
176 * @param courseid the current course id
177 * @param password the submitted enrolment key
179 function check_group_entry ($courseid, $password) {
182 if (($groups = get_groups($courseid)) !== false) {
183 foreach ($groups as $group) {
184 if ( !empty($group->password
) and ($password == $group->password
) ) {
185 $ingroup = $group->id
;
194 * Prints a form for configuring the current enrolment plugin
196 * This function is called from admin/enrol.php, and outputs a
197 * full page with a form for defining the current enrolment plugin.
199 * @param page an object containing all the data for this page
201 function config_form($page) {
207 * Processes and stored configuration data for the enrolment plugin
209 * @param config all the configuration data as entered by the admin
211 function process_config($config) {
215 foreach ($config as $name => $value) {
216 if (!set_config($name, $value)) {
226 * This function is run by admin/cron.php every time
228 * The cron function can perform regular checks for the current
229 * enrollment plugin. For example it can check a foreign database,
230 * all look for a file to pull data in from
237 // Delete all assignments from the database that have already expired
241 if ($assignments = get_records_sql("SELECT ra.*, c.instanceid as courseid FROM
242 {$CFG->prefix}role_assignments ra,
243 {$CFG->prefix}context c
244 WHERE ra.enrol = 'manual'
246 AND ra.timeend < $timenow
247 AND ra.contextid = c.id
248 AND c.contextlevel = ".CONTEXT_COURSE
)) {
249 foreach ($assignments as $assignment) {
250 if ($course = get_record('course', 'id', $assignment->courseid
)) {
251 if (empty($course->enrolperiod
)) { // This overrides student timeend
255 role_unassign($assignment->roleid
, $assignment->userid
, 0, $assignment->contextid
);
261 // Notify users about enrolments that are going to expire soon!
263 if (empty($CFG->lastexpirynotify
)) {
264 $CFG->lastexpirynotify
= 0;
267 if ($CFG->lastexpirynotify
< date('Ymd') &&
268 ($courses = get_records_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0'))) {
270 $admin = get_admin();
272 $strexpirynotify = get_string('expirynotify');
273 foreach ($courses as $course) {
275 $a->coursename
= $course->shortname
.'/'. $course->fullname
;
276 $a->threshold
= $course->expirythreshold
/ 86400;
277 $a->extendurl
= $CFG->wwwroot
. '/user/index.php?id=' . $course->id
;
278 $a->current
= array();
280 $a->current
= $a->past
= array();
281 $expiry = time() +
$course->expirythreshold
;
283 /// Get all the role assignments for this course that have expired.
285 if (!$context = get_context_instance(CONTEXT_COURSE
, $course->id
)) {
289 if ($oldenrolments = get_records_sql('
291 FROM '.$CFG->prefix
.'role_assignments ra,
292 '.$CFG->prefix
.'user u
293 WHERE ra.contextid = '.$context->id
.'
294 AND ra.timeend > 0 AND ra.timeend <= '.$expiry.'
295 AND ra.userid = u.id ')) {
298 if (!$teacher = get_teacher($course->id
)) {
299 $teacher = get_admin();
302 $a->studentstr
= fullname($user, true);
303 $a->teacherstr
= fullname($teacher, true);
305 $strexpirynotifystudentsemail = get_string('expirynotifystudentsemail', '', $a);
307 foreach ($oldenrolments as $user) { /// Email all users about to expire
308 if ($user->timeend
< ($expiry - 86400)) {
309 $a->past
[] = fullname($user) . " <$user->email>";
311 $a->current
[] = fullname($user) . " <$user->email>";
312 if ($course->notifystudents
) { // Send this guy notice
313 email_to_user($student, $teacher, $SITE->fullname
.' '. $strexpirynotify,
314 $strexpirynotifystudentsemail);
319 $a->current
= implode("\n", $a->current
);
320 $a->past
= implode("\n", $a->past
);
322 $strexpirynotifyemail = get_string('expirynotifyemail', '', $a);
324 if ($a->current ||
$a->past
) {
325 if ($teachers = get_users_by_capability($context, 'moodle/course:update',
326 'u.*,ra.hidden', 'r.sortorder ASC',
327 '', '', '', '', false)) {
328 foreach ($teachers as $teacher) {
329 email_to_user($teacher, $admin, $a->coursename
.' '. $strexpirynotify, $strexpirynotifyemail);
334 set_config('lastexpirynotify', date('Ymd'));
341 * Returns the relevant icons for a course
343 * @param course the current course, as an object
345 function get_access_icons($course) {
348 global $strallowguests;
349 global $strrequireskey;
351 if (empty($strallowguests)) {
352 $strallowguests = get_string('allowguests');
353 $strrequireskey = get_string('requireskey');
358 if (!empty($course->guest
)) {
359 $str .= '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot
.'/course/view.php?id='.$course->id
.'">';
360 $str .= '<img class="accessicon" alt="'.$strallowguests.'" src="'.$CFG->pixpath
.'/i/guest.gif" /></a> ';
362 if (!empty($course->password
)) {
363 $str .= '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot
.'/course/view.php?id='.$course->id
.'">';
364 $str .= '<img class="accessicon" alt="'.$strrequireskey.'" src="'.$CFG->pixpath
.'/i/key.gif" /></a>';