| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php /// $Id: enrol.php,v 1.25.2.9 2008/09/24 18:48:08 skodak Exp $ 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 /////////////////////////////////////////////////////////////////////////// 24 25 require_once($CFG->dirroot.'/group/lib.php'); 26 27 /** 28 * enrolment_plugin_manual is the default enrolment plugin 29 * 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. 33 */ 34 35 class enrolment_plugin_manual { 36 37 var $errormsg; 38 39 /** 40 * Prints the entry form/page for this enrolment 41 * 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 46 * 47 * @param course current course object 48 */ 49 function print_entry($course) { 50 global $CFG, $USER, $SESSION, $THEME; 51 52 $strloginto = get_string('loginto', '', $course->shortname); 53 $strcourses = get_string('courses'); 54 55 /// Automatically enrol into courses without password 56 57 $context = get_context_instance(CONTEXT_SYSTEM); 58 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); 63 64 if ($course->password == '') { // no password, so enrol 65 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()); 68 69 } else if (empty($_GET['confirm']) && empty($_GET['cancel'])) { 70 71 print_header($strloginto, $course->fullname, $navigation); 72 echo '<br />'; 73 notice_yesno(get_string('enrolmentconfirmation'), "enrol.php?id=$course->id&confirm=1", 74 "enrol.php?id=$course->id&cancel=1"); 75 print_footer(); 76 exit; 77 78 } else if (!empty($_GET['confirm'])) { 79 80 if (!enrol_into_course($course, $USER, 'manual')) { 81 print_error('couldnotassignrole'); 82 } 83 // force a refresh of mycourses 84 unset($USER->mycourses); 85 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"; 91 } 92 93 redirect($destination); 94 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; 102 } 103 redirect($destination); 104 } 105 } 106 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 = ''; 111 } 112 113 print_header($strloginto, $course->fullname, $navigation, "form.password"); 114 115 print_course($course, "80%"); 116 117 include("$CFG->dirroot/enrol/manual/enrol.html"); 118 119 print_footer(); 120 121 } 122 123 124 125 /** 126 * The other half to print_entry, this checks the form data 127 * 128 * This function checks that the user has completed the task on the 129 * enrolment entry page and then enrolls them. 130 * 131 * @param form the form data submitted, as an object 132 * @param course the current course, as an object 133 */ 134 function check_entry($form, $course) { 135 global $CFG, $USER, $SESSION, $THEME; 136 137 if (empty($form->password)) { 138 $form->password = ''; 139 } 140 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'); 145 } 146 147 $groupid = $this->check_group_entry($course->id, $form->password); 148 149 if ((stripslashes($form->password) == $course->password) or ($groupid !== false) ) { 150 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()); 154 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'); 162 } 163 } 164 } else { 165 print_error('couldnotassignrole'); 166 } 167 } 168 169 if ($SESSION->wantsurl) { 170 $destination = $SESSION->wantsurl; 171 unset($SESSION->wantsurl); 172 } else { 173 $destination = "$CFG->wwwroot/course/view.php?id=$course->id"; 174 } 175 176 redirect($destination); 177 178 } else if (!isset($CFG->enrol_manual_showhint) or $CFG->enrol_manual_showhint) { 179 $this->errormsg = get_string('enrolmentkeyhint', '', substr($course->password, 0, 1)); 180 181 } else { 182 $this->errormsg = get_string('enrolmentkeyerror', 'enrol_manual'); 183 } 184 } 185 186 187 /** 188 * Check if the given enrolment key matches a group enrolment key for the given course 189 * 190 * @param courseid the current course id 191 * @param password the submitted enrolment key 192 */ 193 function check_group_entry ($courseid, $password) { 194 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; 199 } 200 } 201 } 202 203 return false; 204 } 205 206 207 /** 208 * Prints a form for configuring the current enrolment plugin 209 * 210 * This function is called from admin/enrol.php, and outputs a 211 * full page with a form for defining the current enrolment plugin. 212 * 213 * @param frm an object containing all the data for this page 214 */ 215 function config_form($frm) { 216 global $CFG; 217 218 if (!isset( $frm->enrol_manual_keyholderrole )) { 219 $frm->enrol_manual_keyholderrole = ''; 220 } 221 222 if (!isset($frm->enrol_manual_showhint)) { 223 $frm->enrol_manual_showhint = 1; 224 } 225 226 include ("$CFG->dirroot/enrol/manual/config.html"); 227 } 228 229 230 /** 231 * Processes and stored configuration data for the enrolment plugin 232 * 233 * @param config all the configuration data as entered by the admin 234 */ 235 function process_config($config) { 236 237 $return = true; 238 239 foreach ($config as $name => $value) { 240 if (!set_config($name, $value)) { 241 $return = false; 242 } 243 } 244 245 return $return; 246 } 247 248 249 /** 250 * Notify users about enrolments that are going to expire soon! 251 * This function is run by admin/cron.php 252 * @return void 253 */ 254 function cron() { 255 global $CFG, $USER, $SITE; 256 257 if (!isset($CFG->lastexpirynotify)) { 258 set_config('lastexpirynotify', 0); 259 } 260 261 // notify once a day only - TODO: add some tz handling here, maybe use timestamps 262 if ($CFG->lastexpirynotify == date('Ymd')) { 263 return; 264 } 265 266 if ($rs = get_recordset_select('course', 'enrolperiod > 0 AND expirynotify > 0 AND expirythreshold > 0')) { 267 268 $cronuser = clone($USER); 269 270 $admin = get_admin(); 271 272 while($course = rs_fetch_next_record($rs)) { 273 $a = new object(); 274 $a->coursename = $course->shortname .'/'. $course->fullname; // must be processed by format_string later 275 $a->threshold = $course->expirythreshold / 86400; 276 $a->extendurl = $CFG->wwwroot . '/user/index.php?id=' . $course->id; 277 $a->current = array(); 278 $a->past = array(); 279 280 $expiry = time() + $course->expirythreshold; 281 $cname = $course->fullname; 282 283 /// Get all the manual role assignments for this course that have expired. 284 285 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) { 286 continue; 287 } 288 289 if ($oldenrolments = get_records_sql(" 290 SELECT u.*, ra.timeend 291 FROM {$CFG->prefix}user u 292 JOIN {$CFG->prefix}role_assignments ra ON (ra.userid = u.id) 293 WHERE ra.contextid = $context->id 294 AND ra.timeend > 0 AND ra.timeend <= $expiry 295 AND ra.enrol = 'manual'")) { 296 297 // inform user who can assign roles or admin 298 if ($teachers = get_users_by_capability($context, 'moodle/role:assign', '', '', '', '', '', '', false)) { 299 $teachers = sort_by_roleassignment_authority($teachers, $context); 300 $teacher = reset($teachers); 301 } else { 302 $teachers = array($admin); 303 $teacher = $admin; 304 } 305 306 $a->teacherstr = fullname($teacher, true); 307 308 foreach ($oldenrolments as $user) { /// Email all users about to expire 309 $a->studentstr = fullname($user, true); 310 if ($user->timeend < ($expiry - 86400)) { 311 $a->past[] = fullname($user) . " <$user->email>"; 312 } else { 313 $a->current[] = fullname($user) . " <$user->email>"; 314 if ($course->notifystudents) { // Send this guy notice 315 // setup global $COURSE properly - needed for languages 316 $USER = $user; 317 course_setup($course); 318 $a->coursename = format_string($cname); 319 $a->course = $a->coursename; 320 $strexpirynotifystudentsemail = get_string('expirynotifystudentsemail', '', $a); 321 $strexpirynotify = get_string('expirynotify'); 322 323 email_to_user($user, $teacher, format_string($SITE->fullname) .' '. $strexpirynotify, 324 $strexpirynotifystudentsemail); 325 } 326 } 327 } 328 329 $a->current = implode("\n", $a->current); 330 $a->past = implode("\n", $a->past); 331 332 if ($a->current || $a->past) { 333 foreach ($teachers as $teacher) { 334 // setup global $COURSE properly - needed for languages 335 $USER = $teacher; 336 course_setup($course); 337 $a->coursename = format_string($cname); 338 $strexpirynotifyemail = get_string('expirynotifyemail', '', $a); 339 $strexpirynotify = get_string('expirynotify'); 340 341 email_to_user($teacher, $admin, $a->coursename .' '. $strexpirynotify, $strexpirynotifyemail); 342 } 343 } 344 } 345 } 346 $USER = $cronuser; 347 course_setup($SITE); // More environment 348 } 349 350 set_config('lastexpirynotify', date('Ymd')); 351 } 352 353 354 /** 355 * Returns the relevant icons for a course 356 * 357 * @param course the current course, as an object 358 */ 359 function get_access_icons($course) { 360 global $CFG; 361 362 global $strallowguests; 363 global $strrequireskey; 364 365 if (empty($strallowguests)) { 366 $strallowguests = get_string('allowguests'); 367 $strrequireskey = get_string('requireskey'); 368 } 369 370 $str = ''; 371 372 if (!empty($course->guest)) { 373 $str .= '<a title="'.$strallowguests.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'; 374 $str .= '<img class="accessicon" alt="'.$strallowguests.'" src="'.$CFG->pixpath.'/i/guest.gif" /></a> '; 375 } 376 if (!empty($course->password)) { 377 $str .= '<a title="'.$strrequireskey.'" href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'; 378 $str .= '<img class="accessicon" alt="'.$strrequireskey.'" src="'.$CFG->pixpath.'/i/key.gif" /></a>'; 379 } 380 381 return $str; 382 } 383 384 /** 385 * Prints the message telling you were to get the enrolment key 386 * appropriate for the prevailing circumstances 387 * A bit clunky because I didn't want to change the standard strings 388 */ 389 function print_enrolmentkeyfrom($course) { 390 global $CFG; 391 global $USER; 392 393 $context = get_context_instance(CONTEXT_SYSTEM); 394 $guest = has_capability('moodle/legacy:guest', $context, $USER->id, false); 395 396 // if a keyholder role is defined we list teachers in that role (if any exist) 397 $contactslisted = false; 398 $canseehidden = has_capability('moodle/role:viewhiddenassigns', $context); 399 if (!empty($CFG->enrol_manual_keyholderrole)) { 400 if ($contacts = get_role_users($CFG->enrol_manual_keyholderrole, get_context_instance(CONTEXT_COURSE, $course->id),true,'','u.lastname ASC',$canseehidden )) { 401 // guest user has a slightly different message 402 if ($guest) { 403 print_string('enrolmentkeyfromguest', '', ':<br />' ); 404 } 405 else { 406 print_string('enrolmentkeyfrom', '', ':<br />'); 407 } 408 foreach ($contacts as $contact) { 409 $contactname = "<a href=\"../user/view.php?id=$contact->id&course=".SITEID."\">".fullname($contact)."</a>."; 410 echo "$contactname<br />"; 411 } 412 $contactslisted = true; 413 } 414 } 415 416 // if no keyholder role is defined OR nobody is in that role we do this the 'old' way 417 // (show the first person with update rights) 418 if (!$contactslisted) { 419 if ($teachers = get_users_by_capability(get_context_instance(CONTEXT_COURSE, $course->id), 'moodle/course:update', 420 'u.*', 'u.id ASC', 0, 1, '', '', false, true)) { 421 $teacher = array_shift($teachers); 422 } 423 if (!empty($teacher)) { 424 $teachername = "<a href=\"../user/view.php?id=$teacher->id&course=".SITEID."\">".fullname($teacher)."</a>."; 425 } else { 426 $teachername = strtolower( get_string('defaultcourseteacher') ); //get_string('yourteacher', '', $course->teacher); 427 } 428 429 // guest user has a slightly different message 430 if ($guest) { 431 print_string('enrolmentkeyfromguest', '', $teachername ); 432 } 433 else { 434 print_string('enrolmentkeyfrom', '', $teachername); 435 } 436 } 437 } 438 439 } /// end of class 440 441 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |