Merge branch 'MDL-26937' of git://github.com/timhunt/moodle into MOODLE_19_STABLE
[moodle.git] / enrol / manual / enrol.php
blob349e4775d5392a6319bad6a00dd0b126dad634d2
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 ///////////////////////////////////////////////////////////////////////////
25 require_once($CFG->dirroot.'/group/lib.php');
27 /**
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 {
37 var $errormsg;
39 /**
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
45 * is disabled
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);
59 $navlinks = array();
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);
72 echo '<br />';
73 notice_yesno(get_string('enrolmentconfirmation'), "enrol.php?id=$course->id&amp;confirm=1&amp;sesskey=".sesskey(),
74 "enrol.php?id=$course->id&amp;cancel=1");
75 print_footer();
76 exit;
78 } else if (!empty($_GET['confirm']) and confirm_sesskey()) {
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);
89 } else {
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);
100 } else {
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)) {
110 $password = '';
113 print_header($strloginto, $course->fullname, $navigation, "form.password");
115 print_course($course, "80%");
117 include("$CFG->dirroot/enrol/manual/enrol.html");
119 print_footer();
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) or !confirm_sesskey()) {
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');
164 } else {
165 print_error('couldnotassignrole');
169 if ($SESSION->wantsurl) {
170 $destination = $SESSION->wantsurl;
171 unset($SESSION->wantsurl);
172 } else {
173 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
176 redirect($destination);
178 } else if (!isset($CFG->enrol_manual_showhint) or $CFG->enrol_manual_showhint) {
179 $this->errormsg = get_string('enrolmentkeyhint', '', substr($course->password, 0, 1));
181 } else {
182 $this->errormsg = get_string('enrolmentkeyerror', 'enrol_manual');
188 * Check if the given enrolment key matches a group enrolment key for the given course
190 * @param courseid the current course id
191 * @param password the submitted enrolment key
193 function check_group_entry ($courseid, $password) {
195 if ($groups = groups_get_all_groups($courseid)) {
196 foreach ($groups as $group) {
197 if ( !empty($group->enrolmentkey) and (stripslashes($password) == $group->enrolmentkey) ) {
198 return $group->id;
203 return false;
208 * Prints a form for configuring the current enrolment plugin
210 * This function is called from admin/enrol.php, and outputs a
211 * full page with a form for defining the current enrolment plugin.
213 * @param frm an object containing all the data for this page
215 function config_form($frm) {
216 global $CFG;
218 if (!isset( $frm->enrol_manual_keyholderrole )) {
219 $frm->enrol_manual_keyholderrole = '';
222 if (!isset($frm->enrol_manual_showhint)) {
223 $frm->enrol_manual_showhint = 1;
226 if (!isset($frm->enrol_manual_usepasswordpolicy)) {
227 $frm->enrol_manual_usepasswordpolicy = 0;
230 if (!isset($frm->enrol_manual_requirekey)) {
231 $frm->enrol_manual_requirekey = 0;
234 include ("$CFG->dirroot/enrol/manual/config.html");
239 * Processes and stored configuration data for the enrolment plugin
241 * @param config all the configuration data as entered by the admin
243 function process_config($config) {
245 $return = true;
247 foreach ($config as $name => $value) {
248 if (!set_config($name, $value)) {
249 $return = false;
253 return $return;
258 * Notify users about enrolments that are going to expire soon!
259 * This function is run by admin/cron.php
260 * @return void
262 function cron() {
263 global $CFG, $USER, $SITE;
265 if (!isset($CFG->lastexpirynotify)) {
266 set_config('lastexpirynotify', 0);
269 // notify once a day only - TODO: add some tz handling here, maybe use timestamps
270 if ($CFG->lastexpirynotify == date('Ymd')) {
271 return;
274 if ($rs = get_recordset_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0')) {
276 $cronuser = clone($USER);
278 $admin = get_admin();
280 while($course = rs_fetch_next_record($rs)) {
281 $a = new object();
282 $a->coursename = $course->shortname .'/'. $course->fullname; // must be processed by format_string later
283 $a->threshold = $course->expirythreshold / 86400;
284 $a->extendurl = $CFG->wwwroot . '/user/index.php?id=' . $course->id;
285 $a->current = array();
286 $a->past = array();
288 $expiry = time() + $course->expirythreshold;
289 $cname = $course->fullname;
291 /// Get all the manual role assignments for this course that have expired.
293 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
294 continue;
297 if ($oldenrolments = get_records_sql("
298 SELECT u.*, ra.timeend
299 FROM {$CFG->prefix}user u
300 JOIN {$CFG->prefix}role_assignments ra ON (ra.userid = u.id)
301 WHERE ra.contextid = $context->id
302 AND ra.timeend > 0 AND ra.timeend <= $expiry
303 AND ra.enrol = 'manual'")) {
305 // inform user who can assign roles or admin
306 if ($teachers = get_users_by_capability($context, 'moodle/role:assign', '', '', '', '', '', '', false)) {
307 $teachers = sort_by_roleassignment_authority($teachers, $context);
308 $teacher = reset($teachers);
309 } else {
310 $teachers = array($admin);
311 $teacher = $admin;
314 $a->teacherstr = fullname($teacher, true);
316 foreach ($oldenrolments as $user) { /// Email all users about to expire
317 $a->studentstr = fullname($user, true);
318 if ($user->timeend < ($expiry - 86400)) {
319 $a->past[] = fullname($user) . " <$user->email>";
320 } else {
321 $a->current[] = fullname($user) . " <$user->email>";
322 if ($course->notifystudents) { // Send this guy notice
323 // setup global $COURSE properly - needed for languages
324 $USER = $user;
325 course_setup($course);
326 $a->coursename = format_string($cname);
327 $a->course = $a->coursename;
328 $strexpirynotifystudentsemail = get_string('expirynotifystudentsemail', '', $a);
329 $strexpirynotify = get_string('expirynotify');
331 email_to_user($user, $teacher, format_string($SITE->fullname) .' '. $strexpirynotify,
332 $strexpirynotifystudentsemail);
337 $a->current = implode("\n", $a->current);
338 $a->past = implode("\n", $a->past);
340 if ($a->current || $a->past) {
341 foreach ($teachers as $teacher) {
342 // setup global $COURSE properly - needed for languages
343 $USER = $teacher;
344 course_setup($course);
345 $a->coursename = format_string($cname);
346 $strexpirynotifyemail = get_string('expirynotifyemail', '', $a);
347 $strexpirynotify = get_string('expirynotify');
349 email_to_user($teacher, $admin, $a->coursename .' '. $strexpirynotify, $strexpirynotifyemail);
354 $USER = $cronuser;
355 course_setup($SITE); // More environment
358 set_config('lastexpirynotify', date('Ymd'));
363 * Returns the relevant icons for a course
365 * @param course the current course, as an object
367 function get_access_icons($course) {
368 global $CFG;
370 global $strallowguests;
371 global $strrequireskey;
373 if (empty($strallowguests)) {
374 $strallowguests = get_string('allowguests');
375 $strrequireskey = get_string('requireskey');
378 $str = '';
380 if (!empty($course->guest)) {
381 $str .= '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
382 $str .= '<img class="accessicon" alt="'.$strallowguests.'" src="'.$CFG->pixpath.'/i/guest.gif" /></a>&nbsp;&nbsp;';
384 if (!empty($course->password)) {
385 $str .= '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
386 $str .= '<img class="accessicon" alt="'.$strrequireskey.'" src="'.$CFG->pixpath.'/i/key.gif" /></a>';
389 return $str;
393 * Prints the message telling you were to get the enrolment key
394 * appropriate for the prevailing circumstances
395 * A bit clunky because I didn't want to change the standard strings
397 function print_enrolmentkeyfrom($course) {
398 global $CFG;
399 global $USER;
401 $context = get_context_instance(CONTEXT_SYSTEM);
402 $guest = has_capability('moodle/legacy:guest', $context, $USER->id, false);
404 // if a keyholder role is defined we list teachers in that role (if any exist)
405 $contactslisted = false;
406 $canseehidden = has_capability('moodle/role:viewhiddenassigns', $context);
407 if (!empty($CFG->enrol_manual_keyholderrole)) {
408 if ($contacts = get_role_users($CFG->enrol_manual_keyholderrole, get_context_instance(CONTEXT_COURSE, $course->id),true,'','u.lastname ASC',$canseehidden )) {
409 // guest user has a slightly different message
410 if ($guest) {
411 print_string('enrolmentkeyfromguest', '', ':<br />' );
413 else {
414 print_string('enrolmentkeyfrom', '', ':<br />');
416 foreach ($contacts as $contact) {
417 $contactname = "<a href=\"../user/view.php?id=$contact->id&course=".SITEID."\">".fullname($contact)."</a>.";
418 echo "$contactname<br />";
420 $contactslisted = true;
424 // if no keyholder role is defined OR nobody is in that role we do this the 'old' way
425 // (show the first person with update rights)
426 if (!$contactslisted) {
427 if ($teachers = get_users_by_capability(get_context_instance(CONTEXT_COURSE, $course->id), 'moodle/course:update',
428 'u.*', 'u.id ASC', 0, 1, '', '', false, true)) {
429 $teacher = array_shift($teachers);
431 if (!empty($teacher)) {
432 $teachername = "<a href=\"../user/view.php?id=$teacher->id&course=".SITEID."\">".fullname($teacher)."</a>.";
433 } else {
434 $teachername = strtolower( get_string('defaultcourseteacher') ); //get_string('yourteacher', '', $course->teacher);
437 // guest user has a slightly different message
438 if ($guest) {
439 print_string('enrolmentkeyfromguest', '', $teachername );
441 else {
442 print_string('enrolmentkeyfrom', '', $teachername);
447 } /// end of class