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 ///////////////////////////////////////////////////////////////////////////
25 require_once($CFG->dirroot
.'/group/lib.php');
28 * enrolment_plugin_manual is the default enrolment plugin
30 * This class provides all the functionality for an enrolment plugin
31 * In fact it includes all the code for the default, "manual" method
32 * so that other plugins can override these as necessary.
35 class enrolment_plugin_manual
{
40 * Prints the entry form/page for this enrolment
42 * This is only called from course/enrol.php
43 * Most plugins will probably override this to print payment
44 * forms etc, or even just a notice to say that manual enrolment
47 * @param course current course object
49 function print_entry($course) {
50 global $CFG, $USER, $SESSION, $THEME;
52 $strloginto = get_string('loginto', '', $course->shortname
);
53 $strcourses = get_string('courses');
55 /// Automatically enrol into courses without password
57 $context = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
60 $navlinks[] = array('name' => $strcourses, 'link' => ".", 'type' => 'misc');
61 $navlinks[] = array('name' => $strloginto, 'link' => null, 'type' => 'misc');
62 $navigation = build_navigation($navlinks);
64 if ($course->password
== '') { // no password, so enrol
66 if (has_capability('moodle/legacy:guest', $context, $USER->id
, false)) {
67 add_to_log($course->id
, 'course', 'guest', 'view.php?id='.$course->id
, getremoteaddr());
69 } else if (empty($_GET['confirm']) && empty($_GET['cancel'])) {
71 print_header($strloginto, $course->fullname
, $navigation);
73 notice_yesno(get_string('enrolmentconfirmation'), "enrol.php?id=$course->id&confirm=1",
74 "enrol.php?id=$course->id&cancel=1");
78 } else if (!empty($_GET['confirm'])) {
80 if (!enrol_into_course($course, $USER, 'manual')) {
81 print_error('couldnotassignrole');
83 // force a refresh of mycourses
84 unset($USER->mycourses
);
86 if (!empty($SESSION->wantsurl
)) {
87 $destination = $SESSION->wantsurl
;
88 unset($SESSION->wantsurl
);
90 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
93 redirect($destination);
95 } else if (!empty($_GET['cancel'])) {
96 unset($SESSION->wantsurl
);
97 if (!empty($SESSION->enrolcancel
)) {
98 $destination = $SESSION->enrolcancel
;
99 unset($SESSION->enrolcancel
);
101 $destination = $CFG->wwwroot
;
103 redirect($destination);
107 // if we get here we are going to display the form asking for the enrolment key
108 // and (hopefully) provide information about who to ask for it.
109 if (!isset($password)) {
113 print_header($strloginto, $course->fullname
, $navigation, "form.password");
115 print_course($course, "80%");
117 include("$CFG->dirroot/enrol/manual/enrol.html");
126 * The other half to print_entry, this checks the form data
128 * This function checks that the user has completed the task on the
129 * enrolment entry page and then enrolls them.
131 * @param form the form data submitted, as an object
132 * @param course the current course, as an object
134 function check_entry($form, $course) {
135 global $CFG, $USER, $SESSION, $THEME;
137 if (empty($form->password
)) {
138 $form->password
= '';
141 if (empty($course->password
)) {
142 // do not allow entry when no course password set
143 // automatic login when manual primary, no login when secondary at all!!
144 error('illegal enrolment attempted');
147 $groupid = $this->check_group_entry($course->id
, $form->password
);
149 if ((stripslashes($form->password
) == $course->password
) or ($groupid !== false) ) {
151 if (isguestuser()) { // only real user guest, do not use this for users with guest role
152 $USER->enrolkey
[$course->id
] = true;
153 add_to_log($course->id
, 'course', 'guest', 'view.php?id='.$course->id
, getremoteaddr());
155 } else { /// Update or add new enrolment
156 if (enrol_into_course($course, $USER, 'manual')) {
157 // force a refresh of mycourses
158 unset($USER->mycourses
);
159 if ($groupid !== false) {
160 if (!groups_add_member($groupid, $USER->id
)) {
161 print_error('couldnotassigngroup');
165 print_error('couldnotassignrole');
169 if ($SESSION->wantsurl
) {
170 $destination = $SESSION->wantsurl
;
171 unset($SESSION->wantsurl
);
173 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
176 redirect($destination);
179 $this->errormsg
= get_string('enrolmentkeyhint', '', substr($course->password
,0,1));
185 * Check if the given enrolment key matches a group enrolment key for the given course
187 * @param courseid the current course id
188 * @param password the submitted enrolment key
190 function check_group_entry ($courseid, $password) {
192 if ($groups = groups_get_all_groups($courseid)) {
193 foreach ($groups as $group) {
194 if ( !empty($group->enrolmentkey
) and (stripslashes($password) == $group->enrolmentkey
) ) {
205 * Prints a form for configuring the current enrolment plugin
207 * This function is called from admin/enrol.php, and outputs a
208 * full page with a form for defining the current enrolment plugin.
210 * @param frm an object containing all the data for this page
212 function config_form($frm) {
215 if (!isset( $frm->enrol_manual_keyholderrole
)) {
216 $frm->enrol_manual_keyholderrole
= '';
219 include ("$CFG->dirroot/enrol/manual/config.html");
224 * Processes and stored configuration data for the enrolment plugin
226 * @param config all the configuration data as entered by the admin
228 function process_config($config) {
232 foreach ($config as $name => $value) {
233 if (!set_config($name, $value)) {
243 * This function is run by admin/cron.php every time
245 * The cron function can perform regular checks for the current
246 * enrollment plugin. For example it can check a foreign database,
247 * all look for a file to pull data in from
253 // Notify users about enrolments that are going to expire soon!
255 if (empty($CFG->lastexpirynotify
)) {
256 $CFG->lastexpirynotify
= 0;
259 if ($CFG->lastexpirynotify
< date('Ymd') &&
260 ($courses = get_records_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0'))) {
262 $admin = get_admin();
264 $strexpirynotify = get_string('expirynotify');
265 foreach ($courses as $course) {
267 $a->coursename
= $course->shortname
.'/'. $course->fullname
;
268 $a->threshold
= $course->expirythreshold
/ 86400;
269 $a->extendurl
= $CFG->wwwroot
. '/user/index.php?id=' . $course->id
;
270 $a->current
= array();
272 $a->current
= $a->past
= array();
273 $expiry = time() +
$course->expirythreshold
;
275 /// Get all the role assignments for this course that have expired.
277 if (!$context = get_context_instance(CONTEXT_COURSE
, $course->id
)) {
281 if ($oldenrolments = get_records_sql('
283 FROM '.$CFG->prefix
.'role_assignments ra,
284 '.$CFG->prefix
.'user u
285 WHERE ra.contextid = '.$context->id
.'
286 AND ra.timeend > 0 AND ra.timeend <= '.$expiry.'
287 AND ra.userid = u.id ')) {
290 if (!$teacher = get_teacher($course->id
)) {
291 $teacher = get_admin();
294 $a->teacherstr
= fullname($teacher, true);
296 $strexpirynotifystudentsemail = get_string('expirynotifystudentsemail', '', $a);
298 foreach ($oldenrolments as $user) { /// Email all users about to expire
299 $a->studentstr
= fullname($user, true);
300 if ($user->timeend
< ($expiry - 86400)) {
301 $a->past
[] = fullname($user) . " <$user->email>";
303 $a->current
[] = fullname($user) . " <$user->email>";
304 if ($course->notifystudents
) { // Send this guy notice
305 email_to_user($user, $teacher, $SITE->fullname
.' '. $strexpirynotify,
306 $strexpirynotifystudentsemail);
311 $a->current
= implode("\n", $a->current
);
312 $a->past
= implode("\n", $a->past
);
314 $strexpirynotifyemail = get_string('expirynotifyemail', '', $a);
316 if ($a->current ||
$a->past
) {
317 if ($teachers = get_users_by_capability($context, 'moodle/course:update',
318 'u.*', 'u.username ASC',
319 '', '', '', '', false)) {
320 foreach ($teachers as $teacher) {
321 email_to_user($teacher, $admin, $a->coursename
.' '. $strexpirynotify, $strexpirynotifyemail);
326 set_config('lastexpirynotify', date('Ymd'));
333 * Returns the relevant icons for a course
335 * @param course the current course, as an object
337 function get_access_icons($course) {
340 global $strallowguests;
341 global $strrequireskey;
343 if (empty($strallowguests)) {
344 $strallowguests = get_string('allowguests');
345 $strrequireskey = get_string('requireskey');
350 if (!empty($course->guest
)) {
351 $str .= '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot
.'/course/view.php?id='.$course->id
.'">';
352 $str .= '<img class="accessicon" alt="'.$strallowguests.'" src="'.$CFG->pixpath
.'/i/guest.gif" /></a> ';
354 if (!empty($course->password
)) {
355 $str .= '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot
.'/course/view.php?id='.$course->id
.'">';
356 $str .= '<img class="accessicon" alt="'.$strrequireskey.'" src="'.$CFG->pixpath
.'/i/key.gif" /></a>';
363 * Prints the message telling you were to get the enrolment key
364 * appropriate for the prevailing circumstances
365 * A bit clunky because I didn't want to change the standard strings
367 function print_enrolmentkeyfrom($course) {
371 $context = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
372 $guest = has_capability('moodle/legacy:guest', $context, $USER->id
, false);
374 // if a keyholder role is defined we list teachers in that role (if any exist)
375 $contactslisted = false;
376 $canseehidden = has_capability('moodle/role:viewhiddenassigns', $context);
377 if (!empty($CFG->enrol_manual_keyholderrole
)) {
378 if ($contacts = get_role_users($CFG->enrol_manual_keyholderrole
, get_context_instance(CONTEXT_COURSE
, $course->id
),$canseehidden )) {
379 // guest user has a slightly different message
381 print_string('enrolmentkeyfromguest', '', ':<br />' );
384 print_string('enrolmentkeyfrom', '', ':<br />');
386 foreach ($contacts as $contact) {
387 $contactname = "<a href=\"../user/view.php?id=$contact->id&course=".SITEID
."\">".fullname($contact)."</a>.";
388 echo "$contactname<br />";
390 $contactslisted = true;
394 // if no keyholder role is defined OR nobody is in that role we do this the 'old' way
395 // (show the first person with update rights)
396 if (!$contactslisted) {
397 if ($teachers = get_users_by_capability(get_context_instance(CONTEXT_COURSE
, $course->id
), 'moodle/course:update',
398 'u.*', 'u.id ASC', 0, 1, '', '', false, true)) {
399 $teacher = array_shift($teachers);
401 if (!empty($teacher)) {
402 $teachername = "<a href=\"../user/view.php?id=$teacher->id&course=".SITEID
."\">".fullname($teacher)."</a>.";
404 $teachername = strtolower( get_string('defaultcourseteacher') ); //get_string('yourteacher', '', $course->teacher);
407 // guest user has a slightly different message
409 print_string('enrolmentkeyfromguest', '', $teachername );
412 print_string('enrolmentkeyfrom', '', $teachername);