MDL-14566
[moodle-linuxchix.git] / enrol / manual / enrol.php
blobfec78fb66b52da9142705b561128562a9a1d59f1
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",
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 ((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 {
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) ) {
195 return $group->id;
200 return false;
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) {
213 global $CFG;
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) {
230 $return = true;
232 foreach ($config as $name => $value) {
233 if (!set_config($name, $value)) {
234 $return = false;
238 return $return;
243 * Notify users about enrolments that are going to expire soon!
244 * This function is run by admin/cron.php
245 * @return void
247 function cron() {
248 global $CFG, $USER, $SITE;
250 if (!isset($CFG->lastexpirynotify)) {
251 set_config('lastexpirynotify', 0);
254 // notify once a day only - TODO: add some tz handling here, maybe use timestamps
255 if ($CFG->lastexpirynotify == date('Ymd')) {
256 return;
259 if ($rs = get_recordset_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0')) {
261 $cronuser = clone($USER);
263 $admin = get_admin();
265 while($course = rs_fetch_next_record($rs)) {
266 $a = new object();
267 $a->coursename = $course->shortname .'/'. $course->fullname; // must be processed by format_string later
268 $a->threshold = $course->expirythreshold / 86400;
269 $a->extendurl = $CFG->wwwroot . '/user/index.php?id=' . $course->id;
270 $a->current = array();
271 $a->past = array();
273 $expiry = time() + $course->expirythreshold;
274 $cname = $course->fullname;
276 /// Get all the manual role assignments for this course that have expired.
278 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
279 continue;
282 if ($oldenrolments = get_records_sql("
283 SELECT u.*, ra.timeend
284 FROM {$CFG->prefix}user u
285 JOIN {$CFG->prefix}role_assignments ra ON (ra.userid = u.id)
286 WHERE ra.contextid = $context->id
287 AND ra.timeend > 0 AND ra.timeend <= $expiry
288 AND ra.enrol = 'manual'")) {
290 // inform user who can assign roles or admin
291 if ($teachers = get_users_by_capability($context, 'moodle/role:assign', '', '', '', '', '', '', false)) {
292 $teachers = sort_by_roleassignment_authority($teachers, $context);
293 $teacher = reset($teachers);
294 } else {
295 $teachers = array($admin);
296 $teacher = $admin;
299 $a->teacherstr = fullname($teacher, true);
301 foreach ($oldenrolments as $user) { /// Email all users about to expire
302 $a->studentstr = fullname($user, true);
303 if ($user->timeend < ($expiry - 86400)) {
304 $a->past[] = fullname($user) . " <$user->email>";
305 } else {
306 $a->current[] = fullname($user) . " <$user->email>";
307 if ($course->notifystudents) { // Send this guy notice
308 // setup global $COURSE properly - needed for languages
309 $USER = $user;
310 course_setup($course);
311 $a->coursename = format_string($cname);
312 $a->course = $a->coursename;
313 $strexpirynotifystudentsemail = get_string('expirynotifystudentsemail', '', $a);
314 $strexpirynotify = get_string('expirynotify');
316 email_to_user($user, $teacher, format_string($SITE->fullname) .' '. $strexpirynotify,
317 $strexpirynotifystudentsemail);
322 $a->current = implode("\n", $a->current);
323 $a->past = implode("\n", $a->past);
325 if ($a->current || $a->past) {
326 foreach ($teachers as $teacher) {
327 // setup global $COURSE properly - needed for languages
328 $USER = $teacher;
329 course_setup($course);
330 $a->coursename = format_string($cname);
331 $strexpirynotifyemail = get_string('expirynotifyemail', '', $a);
332 $strexpirynotify = get_string('expirynotify');
334 email_to_user($teacher, $admin, $a->coursename .' '. $strexpirynotify, $strexpirynotifyemail);
339 $USER = $cronuser;
340 course_setup($course); // More environment
343 set_config('lastexpirynotify', date('Ymd'));
348 * Returns the relevant icons for a course
350 * @param course the current course, as an object
352 function get_access_icons($course) {
353 global $CFG;
355 global $strallowguests;
356 global $strrequireskey;
358 if (empty($strallowguests)) {
359 $strallowguests = get_string('allowguests');
360 $strrequireskey = get_string('requireskey');
363 $str = '';
365 if (!empty($course->guest)) {
366 $str .= '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
367 $str .= '<img class="accessicon" alt="'.$strallowguests.'" src="'.$CFG->pixpath.'/i/guest.gif" /></a>&nbsp;&nbsp;';
369 if (!empty($course->password)) {
370 $str .= '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
371 $str .= '<img class="accessicon" alt="'.$strrequireskey.'" src="'.$CFG->pixpath.'/i/key.gif" /></a>';
374 return $str;
378 * Prints the message telling you were to get the enrolment key
379 * appropriate for the prevailing circumstances
380 * A bit clunky because I didn't want to change the standard strings
382 function print_enrolmentkeyfrom($course) {
383 global $CFG;
384 global $USER;
386 $context = get_context_instance(CONTEXT_SYSTEM);
387 $guest = has_capability('moodle/legacy:guest', $context, $USER->id, false);
389 // if a keyholder role is defined we list teachers in that role (if any exist)
390 $contactslisted = false;
391 $canseehidden = has_capability('moodle/role:viewhiddenassigns', $context);
392 if (!empty($CFG->enrol_manual_keyholderrole)) {
393 if ($contacts = get_role_users($CFG->enrol_manual_keyholderrole, get_context_instance(CONTEXT_COURSE, $course->id),true,'','u.lastname ASC',$canseehidden )) {
394 // guest user has a slightly different message
395 if ($guest) {
396 print_string('enrolmentkeyfromguest', '', ':<br />' );
398 else {
399 print_string('enrolmentkeyfrom', '', ':<br />');
401 foreach ($contacts as $contact) {
402 $contactname = "<a href=\"../user/view.php?id=$contact->id&course=".SITEID."\">".fullname($contact)."</a>.";
403 echo "$contactname<br />";
405 $contactslisted = true;
409 // if no keyholder role is defined OR nobody is in that role we do this the 'old' way
410 // (show the first person with update rights)
411 if (!$contactslisted) {
412 if ($teachers = get_users_by_capability(get_context_instance(CONTEXT_COURSE, $course->id), 'moodle/course:update',
413 'u.*', 'u.id ASC', 0, 1, '', '', false, true)) {
414 $teacher = array_shift($teachers);
416 if (!empty($teacher)) {
417 $teachername = "<a href=\"../user/view.php?id=$teacher->id&course=".SITEID."\">".fullname($teacher)."</a>.";
418 } else {
419 $teachername = strtolower( get_string('defaultcourseteacher') ); //get_string('yourteacher', '', $course->teacher);
422 // guest user has a slightly different message
423 if ($guest) {
424 print_string('enrolmentkeyfromguest', '', $teachername );
426 else {
427 print_string('enrolmentkeyfrom', '', $teachername);
432 } /// end of class