calendar/lib: calendar_set_filters() use pre-fetched context and course recs
[moodle-pu.git] / enrol / manual / enrol.php
blob49fb53194428a521327de22c4d19a601c4cd9131
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, SITEID);
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",
74 "enrol.php?id=$course->id&amp;cancel=1");
75 print_footer();
76 exit;
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);
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)) {
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 (($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 {
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) {
191 $ingroup = false;
192 if (($groups = groups_get_all_groups($courseid))) {
193 foreach ($groups as $group) {
194 if ( !empty($group->enrolmentkey) and ($password == $group->enrolmentkey) ) {
195 $ingroup = $group->id;
199 return $ingroup;
204 * Prints a form for configuring the current enrolment plugin
206 * This function is called from admin/enrol.php, and outputs a
207 * full page with a form for defining the current enrolment plugin.
209 * @param frm an object containing all the data for this page
211 function config_form($frm) {
212 global $CFG;
214 if (!isset( $frm->enrol_manual_keyholderrole )) {
215 $frm->enrol_manual_keyholderrole = '';
218 include ("$CFG->dirroot/enrol/manual/config.html");
223 * Processes and stored configuration data for the enrolment plugin
225 * @param config all the configuration data as entered by the admin
227 function process_config($config) {
229 $return = true;
231 foreach ($config as $name => $value) {
232 if (!set_config($name, $value)) {
233 $return = false;
237 return $return;
242 * This function is run by admin/cron.php every time
244 * The cron function can perform regular checks for the current
245 * enrollment plugin. For example it can check a foreign database,
246 * all look for a file to pull data in from
249 function cron() {
250 global $CFG;
252 // Notify users about enrolments that are going to expire soon!
254 if (empty($CFG->lastexpirynotify)) {
255 $CFG->lastexpirynotify = 0;
258 if ($CFG->lastexpirynotify < date('Ymd') &&
259 ($courses = get_records_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0'))) {
261 $admin = get_admin();
263 $strexpirynotify = get_string('expirynotify');
264 foreach ($courses as $course) {
265 $a = new object();
266 $a->coursename = $course->shortname .'/'. $course->fullname;
267 $a->threshold = $course->expirythreshold / 86400;
268 $a->extendurl = $CFG->wwwroot . '/user/index.php?id=' . $course->id;
269 $a->current = array();
270 $a->past = array();
271 $a->current = $a->past = array();
272 $expiry = time() + $course->expirythreshold;
274 /// Get all the role assignments for this course that have expired.
276 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
277 continue;
280 if ($oldenrolments = get_records_sql('
281 SELECT u.*
282 FROM '.$CFG->prefix.'role_assignments ra,
283 '.$CFG->prefix.'user u
284 WHERE ra.contextid = '.$context->id.'
285 AND ra.timeend > 0 AND ra.timeend <= '.$expiry.'
286 AND ra.userid = u.id ')) {
289 if (!$teacher = get_teacher($course->id)) {
290 $teacher = get_admin();
293 $a->teacherstr = fullname($teacher, true);
295 $strexpirynotifystudentsemail = get_string('expirynotifystudentsemail', '', $a);
297 foreach ($oldenrolments as $user) { /// Email all users about to expire
298 $a->studentstr = fullname($user, true);
299 if ($user->timeend < ($expiry - 86400)) {
300 $a->past[] = fullname($user) . " <$user->email>";
301 } else {
302 $a->current[] = fullname($user) . " <$user->email>";
303 if ($course->notifystudents) { // Send this guy notice
304 email_to_user($user, $teacher, $SITE->fullname .' '. $strexpirynotify,
305 $strexpirynotifystudentsemail);
310 $a->current = implode("\n", $a->current);
311 $a->past = implode("\n", $a->past);
313 $strexpirynotifyemail = get_string('expirynotifyemail', '', $a);
315 if ($a->current || $a->past) {
316 if ($teachers = get_users_by_capability($context, 'moodle/course:update',
317 'u.*,ra.hidden', 'r.sortorder ASC',
318 '', '', '', '', false)) {
319 foreach ($teachers as $teacher) {
320 email_to_user($teacher, $admin, $a->coursename .' '. $strexpirynotify, $strexpirynotifyemail);
325 set_config('lastexpirynotify', date('Ymd'));
332 * Returns the relevant icons for a course
334 * @param course the current course, as an object
336 function get_access_icons($course) {
337 global $CFG;
339 global $strallowguests;
340 global $strrequireskey;
342 if (empty($strallowguests)) {
343 $strallowguests = get_string('allowguests');
344 $strrequireskey = get_string('requireskey');
347 $str = '';
349 if (!empty($course->guest)) {
350 $str .= '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
351 $str .= '<img class="accessicon" alt="'.$strallowguests.'" src="'.$CFG->pixpath.'/i/guest.gif" /></a>&nbsp;&nbsp;';
353 if (!empty($course->password)) {
354 $str .= '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
355 $str .= '<img class="accessicon" alt="'.$strrequireskey.'" src="'.$CFG->pixpath.'/i/key.gif" /></a>';
358 return $str;
362 * Prints the message telling you were to get the enrolment key
363 * appropriate for the prevailing circumstances
364 * A bit clunky because I didn't want to change the standard strings
366 function print_enrolmentkeyfrom($course) {
367 global $CFG;
368 global $USER;
370 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
371 $guest = has_capability('moodle/legacy:guest', $context, $USER->id, false);
373 // if a keyholder role is defined we list teachers in that role (if any exist)
374 $contactslisted = false;
375 $canseehidden = has_capability('moodle/role:viewhiddenassigns', $context);
376 if (!empty($CFG->enrol_manual_keyholderrole)) {
377 if ($contacts = get_role_users($CFG->enrol_manual_keyholderrole, get_context_instance(CONTEXT_COURSE, $course->id),$canseehidden )) {
378 // guest user has a slightly different message
379 if ($guest) {
380 print_string('enrolmentkeyfromguest', '', ':<br />' );
382 else {
383 print_string('enrolmentkeyfrom', '', ':<br />');
385 foreach ($contacts as $contact) {
386 $contactname = "<a href=\"../user/view.php?id=$contact->id&course=".SITEID."\">".fullname($contact)."</a>.";
387 echo "$contactname<br />";
389 $contactslisted = true;
393 // if no keyholder role is defined OR nobody is in that role we do this the 'old' way
394 // (show the first person with update rights)
395 if (!$contactslisted) {
396 if ($teachers = get_users_by_capability(get_context_instance(CONTEXT_COURSE, $course->id), 'moodle/course:update',
397 'u.*,ra.hidden', 'r.sortorder ASC', 0, 1, '', '', false, true)) {
398 $teacher = array_shift($teachers);
400 if (!empty($teacher)) {
401 $teachername = "<a href=\"../user/view.php?id=$teacher->id&course=".SITEID."\">".fullname($teacher)."</a>.";
402 } else {
403 $teachername = strtolower( get_string('defaultcourseteacher') ); //get_string('yourteacher', '', $course->teacher);
406 // guest user has a slightly different message
407 if ($guest) {
408 print_string('enrolmentkeyfromguest', '', $teachername );
410 else {
411 print_string('enrolmentkeyfrom', '', $teachername);
416 } /// end of class