adding current groupid to grade_export class - soon to be used in plugins
[moodle-pu.git] / enrol / manual / enrol.php
blobdb8964f8880c8b71bf586071cef593aa95205be2
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');
84 if (!empty($SESSION->wantsurl)) {
85 $destination = $SESSION->wantsurl;
86 unset($SESSION->wantsurl);
87 } else {
88 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
91 redirect($destination);
93 } else if (!empty($_GET['cancel'])) {
94 unset($SESSION->wantsurl);
95 if (!empty($SESSION->enrolcancel)) {
96 $destination = $SESSION->enrolcancel;
97 unset($SESSION->enrolcancel);
98 } else {
99 $destination = $CFG->wwwroot;
101 redirect($destination);
105 // if we get here we are going to display the form asking for the enrolment key
106 // and (hopefully) provide information about who to ask for it.
107 if (!isset($password)) {
108 $password = '';
111 print_header($strloginto, $course->fullname, $navigation, "form.password");
113 print_course($course, "80%");
115 include("$CFG->dirroot/enrol/manual/enrol.html");
117 print_footer();
124 * The other half to print_entry, this checks the form data
126 * This function checks that the user has completed the task on the
127 * enrolment entry page and then enrolls them.
129 * @param form the form data submitted, as an object
130 * @param course the current course, as an object
132 function check_entry($form, $course) {
133 global $CFG, $USER, $SESSION, $THEME;
135 if (empty($form->password)) {
136 $form->password = '';
139 if (empty($course->password)) {
140 // do not allow entry when no course password set
141 // automatic login when manual primary, no login when secondary at all!!
142 error('illegal enrolment attempted');
145 $groupid = $this->check_group_entry($course->id, $form->password);
147 if (($form->password == $course->password) or ($groupid !== false) ) {
149 if (isguestuser()) { // only real user guest, do not use this for users with guest role
150 $USER->enrolkey[$course->id] = true;
151 add_to_log($course->id, 'course', 'guest', 'view.php?id='.$course->id, getremoteaddr());
153 } else { /// Update or add new enrolment
154 if (enrol_into_course($course, $USER, 'manual')) {
155 if ($groupid !== false) {
156 if (!groups_add_member($groupid, $USER->id)) {
157 print_error('couldnotassigngroup');
160 } else {
161 print_error('couldnotassignrole');
165 if ($SESSION->wantsurl) {
166 $destination = $SESSION->wantsurl;
167 unset($SESSION->wantsurl);
168 } else {
169 $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
172 redirect($destination);
174 } else {
175 $this->errormsg = get_string('enrolmentkeyhint', '', substr($course->password,0,1));
181 * Check if the given enrolment key matches a group enrolment key for the given course
183 * @param courseid the current course id
184 * @param password the submitted enrolment key
186 function check_group_entry ($courseid, $password) {
187 $ingroup = false;
188 if (($groups = groups_get_all_groups($courseid))) {
189 foreach ($groups as $group) {
190 if ( !empty($group->enrolmentkey) and ($password == $group->enrolmentkey) ) {
191 $ingroup = $group->id;
195 return $ingroup;
200 * Prints a form for configuring the current enrolment plugin
202 * This function is called from admin/enrol.php, and outputs a
203 * full page with a form for defining the current enrolment plugin.
205 * @param frm an object containing all the data for this page
207 function config_form($frm) {
208 global $CFG;
210 if (!isset( $frm->enrol_manual_keyholderrole )) {
211 $frm->enrol_manual_keyholderrole = '';
214 include ("$CFG->dirroot/enrol/manual/config.html");
219 * Processes and stored configuration data for the enrolment plugin
221 * @param config all the configuration data as entered by the admin
223 function process_config($config) {
225 $return = true;
227 foreach ($config as $name => $value) {
228 if (!set_config($name, $value)) {
229 $return = false;
233 return $return;
238 * This function is run by admin/cron.php every time
240 * The cron function can perform regular checks for the current
241 * enrollment plugin. For example it can check a foreign database,
242 * all look for a file to pull data in from
245 function cron() {
246 global $CFG;
248 // Notify users about enrolments that are going to expire soon!
250 if (empty($CFG->lastexpirynotify)) {
251 $CFG->lastexpirynotify = 0;
254 if ($CFG->lastexpirynotify < date('Ymd') &&
255 ($courses = get_records_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0'))) {
257 $admin = get_admin();
259 $strexpirynotify = get_string('expirynotify');
260 foreach ($courses as $course) {
261 $a = new object();
262 $a->coursename = $course->shortname .'/'. $course->fullname;
263 $a->threshold = $course->expirythreshold / 86400;
264 $a->extendurl = $CFG->wwwroot . '/user/index.php?id=' . $course->id;
265 $a->current = array();
266 $a->past = array();
267 $a->current = $a->past = array();
268 $expiry = time() + $course->expirythreshold;
270 /// Get all the role assignments for this course that have expired.
272 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
273 continue;
276 if ($oldenrolments = get_records_sql('
277 SELECT u.*
278 FROM '.$CFG->prefix.'role_assignments ra,
279 '.$CFG->prefix.'user u
280 WHERE ra.contextid = '.$context->id.'
281 AND ra.timeend > 0 AND ra.timeend <= '.$expiry.'
282 AND ra.userid = u.id ')) {
285 if (!$teacher = get_teacher($course->id)) {
286 $teacher = get_admin();
289 $a->teacherstr = fullname($teacher, true);
291 $strexpirynotifystudentsemail = get_string('expirynotifystudentsemail', '', $a);
293 foreach ($oldenrolments as $user) { /// Email all users about to expire
294 $a->studentstr = fullname($user, true);
295 if ($user->timeend < ($expiry - 86400)) {
296 $a->past[] = fullname($user) . " <$user->email>";
297 } else {
298 $a->current[] = fullname($user) . " <$user->email>";
299 if ($course->notifystudents) { // Send this guy notice
300 email_to_user($user, $teacher, $SITE->fullname .' '. $strexpirynotify,
301 $strexpirynotifystudentsemail);
306 $a->current = implode("\n", $a->current);
307 $a->past = implode("\n", $a->past);
309 $strexpirynotifyemail = get_string('expirynotifyemail', '', $a);
311 if ($a->current || $a->past) {
312 if ($teachers = get_users_by_capability($context, 'moodle/course:update',
313 'u.*,ra.hidden', 'r.sortorder ASC',
314 '', '', '', '', false)) {
315 foreach ($teachers as $teacher) {
316 email_to_user($teacher, $admin, $a->coursename .' '. $strexpirynotify, $strexpirynotifyemail);
321 set_config('lastexpirynotify', date('Ymd'));
328 * Returns the relevant icons for a course
330 * @param course the current course, as an object
332 function get_access_icons($course) {
333 global $CFG;
335 global $strallowguests;
336 global $strrequireskey;
338 if (empty($strallowguests)) {
339 $strallowguests = get_string('allowguests');
340 $strrequireskey = get_string('requireskey');
343 $str = '';
345 if (!empty($course->guest)) {
346 $str .= '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
347 $str .= '<img class="accessicon" alt="'.$strallowguests.'" src="'.$CFG->pixpath.'/i/guest.gif" /></a>&nbsp;&nbsp;';
349 if (!empty($course->password)) {
350 $str .= '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">';
351 $str .= '<img class="accessicon" alt="'.$strrequireskey.'" src="'.$CFG->pixpath.'/i/key.gif" /></a>';
354 return $str;
358 * Prints the message telling you were to get the enrolment key
359 * appropriate for the prevailing circumstances
360 * A bit clunky because I didn't want to change the standard strings
362 function print_enrolmentkeyfrom($course) {
363 global $CFG;
364 global $USER;
366 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
367 $guest = has_capability('moodle/legacy:guest', $context, $USER->id, false);
369 // if a keyholder role is defined we list teachers in that role (if any exist)
370 $contactslisted = false;
371 if (!empty($CFG->enrol_manual_keyholderrole)) {
372 if ($contacts = get_role_users($CFG->enrol_manual_keyholderrole, get_context_instance(CONTEXT_COURSE, $course->id), true )) {
373 // guest user has a slightly different message
374 if ($guest) {
375 print_string('enrolmentkeyfromguest', '', ':<br />' );
377 else {
378 print_string('enrolmentkeyfrom', '', ':<br />');
380 foreach ($contacts as $contact) {
381 $contactname = "<a href=\"../user/view.php?id=$contact->id&course=".SITEID."\">".fullname($contact)."</a>.";
382 echo "$contactname<br />";
384 $contactslisted = true;
388 // if no keyholder role is defined OR nobody is in that role we do this the 'old' way
389 // (show the first person with update rights)
390 if (!$contactslisted) {
391 if ($teachers = get_users_by_capability(get_context_instance(CONTEXT_COURSE, $course->id), 'moodle/course:update',
392 'u.*,ra.hidden', 'r.sortorder ASC', 0, 1, '', '', false, true)) {
393 $teacher = array_shift($teachers);
395 if (!empty($teacher)) {
396 $teachername = "<a href=\"../user/view.php?id=$teacher->id&course=".SITEID."\">".fullname($teacher)."</a>.";
397 } else {
398 $teachername = strtolower( get_string('defaultcourseteacher') ); //get_string('yourteacher', '', $course->teacher);
401 // guest user has a slightly different message
402 if ($guest) {
403 print_string('enrolmentkeyfromguest', '', $teachername );
405 else {
406 print_string('enrolmentkeyfrom', '', $teachername);
411 } /// end of class