[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Self enrolment plugin. 19 * 20 * @package enrol_self 21 * @copyright 2010 Petr Skoda {@link http://skodak.org} 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 /** 26 * Self enrolment plugin implementation. 27 * @author Petr Skoda 28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 29 */ 30 class enrol_self_plugin extends enrol_plugin { 31 32 protected $lasternoller = null; 33 protected $lasternollerinstanceid = 0; 34 35 /** 36 * Returns optional enrolment information icons. 37 * 38 * This is used in course list for quick overview of enrolment options. 39 * 40 * We are not using single instance parameter because sometimes 41 * we might want to prevent icon repetition when multiple instances 42 * of one type exist. One instance may also produce several icons. 43 * 44 * @param array $instances all enrol instances of this type in one course 45 * @return array of pix_icon 46 */ 47 public function get_info_icons(array $instances) { 48 $key = false; 49 $nokey = false; 50 foreach ($instances as $instance) { 51 if ($this->can_self_enrol($instance, false) !== true) { 52 // User can not enrol himself. 53 // Note that we do not check here if user is already enrolled for performance reasons - 54 // such check would execute extra queries for each course in the list of courses and 55 // would hide self-enrolment icons from guests. 56 continue; 57 } 58 if ($instance->password or $instance->customint1) { 59 $key = true; 60 } else { 61 $nokey = true; 62 } 63 } 64 $icons = array(); 65 if ($nokey) { 66 $icons[] = new pix_icon('withoutkey', get_string('pluginname', 'enrol_self'), 'enrol_self'); 67 } 68 if ($key) { 69 $icons[] = new pix_icon('withkey', get_string('pluginname', 'enrol_self'), 'enrol_self'); 70 } 71 return $icons; 72 } 73 74 /** 75 * Returns localised name of enrol instance 76 * 77 * @param stdClass $instance (null is accepted too) 78 * @return string 79 */ 80 public function get_instance_name($instance) { 81 global $DB; 82 83 if (empty($instance->name)) { 84 if (!empty($instance->roleid) and $role = $DB->get_record('role', array('id'=>$instance->roleid))) { 85 $role = ' (' . role_get_name($role, context_course::instance($instance->courseid, IGNORE_MISSING)) . ')'; 86 } else { 87 $role = ''; 88 } 89 $enrol = $this->get_name(); 90 return get_string('pluginname', 'enrol_'.$enrol) . $role; 91 } else { 92 return format_string($instance->name); 93 } 94 } 95 96 public function roles_protected() { 97 // Users may tweak the roles later. 98 return false; 99 } 100 101 public function allow_unenrol(stdClass $instance) { 102 // Users with unenrol cap may unenrol other users manually manually. 103 return true; 104 } 105 106 public function allow_manage(stdClass $instance) { 107 // Users with manage cap may tweak period and status. 108 return true; 109 } 110 111 public function show_enrolme_link(stdClass $instance) { 112 113 if (true !== $this->can_self_enrol($instance, false)) { 114 return false; 115 } 116 117 return true; 118 } 119 120 /** 121 * Sets up navigation entries. 122 * 123 * @param stdClass $instancesnode 124 * @param stdClass $instance 125 * @return void 126 */ 127 public function add_course_navigation($instancesnode, stdClass $instance) { 128 if ($instance->enrol !== 'self') { 129 throw new coding_exception('Invalid enrol instance type!'); 130 } 131 132 $context = context_course::instance($instance->courseid); 133 if (has_capability('enrol/self:config', $context)) { 134 $managelink = new moodle_url('/enrol/self/edit.php', array('courseid'=>$instance->courseid, 'id'=>$instance->id)); 135 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING); 136 } 137 } 138 139 /** 140 * Returns edit icons for the page with list of instances 141 * @param stdClass $instance 142 * @return array 143 */ 144 public function get_action_icons(stdClass $instance) { 145 global $OUTPUT; 146 147 if ($instance->enrol !== 'self') { 148 throw new coding_exception('invalid enrol instance!'); 149 } 150 $context = context_course::instance($instance->courseid); 151 152 $icons = array(); 153 154 if (has_capability('enrol/self:config', $context)) { 155 $editlink = new moodle_url("/enrol/self/edit.php", array('courseid'=>$instance->courseid, 'id'=>$instance->id)); 156 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit'), 'core', 157 array('class' => 'iconsmall'))); 158 } 159 160 return $icons; 161 } 162 163 /** 164 * Returns link to page which may be used to add new instance of enrolment plugin in course. 165 * @param int $courseid 166 * @return moodle_url page url 167 */ 168 public function get_newinstance_link($courseid) { 169 $context = context_course::instance($courseid, MUST_EXIST); 170 171 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/self:config', $context)) { 172 return NULL; 173 } 174 // Multiple instances supported - different roles with different password. 175 return new moodle_url('/enrol/self/edit.php', array('courseid'=>$courseid)); 176 } 177 178 /** 179 * Self enrol user to course 180 * 181 * @param stdClass $instance enrolment instance 182 * @param stdClass $data data needed for enrolment. 183 * @return bool|array true if enroled else eddor code and messege 184 */ 185 public function enrol_self(stdClass $instance, $data = null) { 186 global $DB, $USER, $CFG; 187 188 // Don't enrol user if password is not passed when required. 189 if ($instance->password && !isset($data->enrolpassword)) { 190 return; 191 } 192 193 $timestart = time(); 194 if ($instance->enrolperiod) { 195 $timeend = $timestart + $instance->enrolperiod; 196 } else { 197 $timeend = 0; 198 } 199 200 $this->enrol_user($instance, $USER->id, $instance->roleid, $timestart, $timeend); 201 202 if ($instance->password and $instance->customint1 and $data->enrolpassword !== $instance->password) { 203 // It must be a group enrolment, let's assign group too. 204 $groups = $DB->get_records('groups', array('courseid'=>$instance->courseid), 'id', 'id, enrolmentkey'); 205 foreach ($groups as $group) { 206 if (empty($group->enrolmentkey)) { 207 continue; 208 } 209 if ($group->enrolmentkey === $data->enrolpassword) { 210 // Add user to group. 211 require_once($CFG->dirroot.'/group/lib.php'); 212 groups_add_member($group->id, $USER->id); 213 break; 214 } 215 } 216 } 217 // Send welcome message. 218 if ($instance->customint4) { 219 $this->email_welcome_message($instance, $USER); 220 } 221 } 222 223 /** 224 * Creates course enrol form, checks if form submitted 225 * and enrols user if necessary. It can also redirect. 226 * 227 * @param stdClass $instance 228 * @return string html text, usually a form in a text box 229 */ 230 public function enrol_page_hook(stdClass $instance) { 231 global $CFG, $OUTPUT, $USER; 232 233 require_once("$CFG->dirroot/enrol/self/locallib.php"); 234 235 $enrolstatus = $this->can_self_enrol($instance); 236 237 // Don't show enrolment instance form, if user can't enrol using it. 238 if (true === $enrolstatus) { 239 $form = new enrol_self_enrol_form(NULL, $instance); 240 $instanceid = optional_param('instance', 0, PARAM_INT); 241 if ($instance->id == $instanceid) { 242 if ($data = $form->get_data()) { 243 $this->enrol_self($instance, $data); 244 } 245 } 246 247 ob_start(); 248 $form->display(); 249 $output = ob_get_clean(); 250 return $OUTPUT->box($output); 251 } 252 } 253 254 /** 255 * Checks if user can self enrol. 256 * 257 * @param stdClass $instance enrolment instance 258 * @param bool $checkuserenrolment if true will check if user enrolment is inactive. 259 * used by navigation to improve performance. 260 * @return bool|string true if successful, else error message or false. 261 */ 262 public function can_self_enrol(stdClass $instance, $checkuserenrolment = true) { 263 global $DB, $USER, $CFG; 264 265 if ($checkuserenrolment) { 266 if (isguestuser()) { 267 // Can not enrol guest. 268 return get_string('canntenrol', 'enrol_self'); 269 } 270 // Check if user is already enroled. 271 if ($DB->get_record('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) { 272 return get_string('canntenrol', 'enrol_self'); 273 } 274 } 275 276 if ($instance->status != ENROL_INSTANCE_ENABLED) { 277 return get_string('canntenrol', 'enrol_self'); 278 } 279 280 if ($instance->enrolstartdate != 0 and $instance->enrolstartdate > time()) { 281 return get_string('canntenrol', 'enrol_self'); 282 } 283 284 if ($instance->enrolenddate != 0 and $instance->enrolenddate < time()) { 285 return get_string('canntenrol', 'enrol_self'); 286 } 287 288 if (!$instance->customint6) { 289 // New enrols not allowed. 290 return get_string('canntenrol', 'enrol_self'); 291 } 292 293 if ($DB->record_exists('user_enrolments', array('userid' => $USER->id, 'enrolid' => $instance->id))) { 294 return get_string('canntenrol', 'enrol_self'); 295 } 296 297 if ($instance->customint3 > 0) { 298 // Max enrol limit specified. 299 $count = $DB->count_records('user_enrolments', array('enrolid' => $instance->id)); 300 if ($count >= $instance->customint3) { 301 // Bad luck, no more self enrolments here. 302 return get_string('maxenrolledreached', 'enrol_self'); 303 } 304 } 305 306 if ($instance->customint5) { 307 require_once("$CFG->dirroot/cohort/lib.php"); 308 if (!cohort_is_member($instance->customint5, $USER->id)) { 309 $cohort = $DB->get_record('cohort', array('id' => $instance->customint5)); 310 if (!$cohort) { 311 return null; 312 } 313 $a = format_string($cohort->name, true, array('context' => context::instance_by_id($cohort->contextid))); 314 return markdown_to_html(get_string('cohortnonmemberinfo', 'enrol_self', $a)); 315 } 316 } 317 318 return true; 319 } 320 321 /** 322 * Return information for enrolment instance containing list of parameters required 323 * for enrolment, name of enrolment plugin etc. 324 * 325 * @param stdClass $instance enrolment instance 326 * @return stdClass instance info. 327 */ 328 public function get_enrol_info(stdClass $instance) { 329 330 $instanceinfo = new stdClass(); 331 $instanceinfo->id = $instance->id; 332 $instanceinfo->courseid = $instance->courseid; 333 $instanceinfo->type = $this->get_name(); 334 $instanceinfo->name = $this->get_instance_name($instance); 335 $instanceinfo->status = $this->can_self_enrol($instance); 336 337 if ($instance->password) { 338 $instanceinfo->requiredparam = new stdClass(); 339 $instanceinfo->requiredparam->enrolpassword = get_string('password', 'enrol_self'); 340 } 341 342 // If enrolment is possible and password is required then return ws function name to get more information. 343 if ((true === $instanceinfo->status) && $instance->password) { 344 $instanceinfo->wsfunction = 'enrol_self_get_instance_info'; 345 } 346 return $instanceinfo; 347 } 348 349 /** 350 * Add new instance of enrol plugin with default settings. 351 * @param stdClass $course 352 * @return int id of new instance 353 */ 354 public function add_default_instance($course) { 355 $fields = $this->get_instance_defaults(); 356 357 if ($this->get_config('requirepassword')) { 358 $fields['password'] = generate_password(20); 359 } 360 361 return $this->add_instance($course, $fields); 362 } 363 364 /** 365 * Returns defaults for new instances. 366 * @return array 367 */ 368 public function get_instance_defaults() { 369 $expirynotify = $this->get_config('expirynotify'); 370 if ($expirynotify == 2) { 371 $expirynotify = 1; 372 $notifyall = 1; 373 } else { 374 $notifyall = 0; 375 } 376 377 $fields = array(); 378 $fields['status'] = $this->get_config('status'); 379 $fields['roleid'] = $this->get_config('roleid'); 380 $fields['enrolperiod'] = $this->get_config('enrolperiod'); 381 $fields['expirynotify'] = $expirynotify; 382 $fields['notifyall'] = $notifyall; 383 $fields['expirythreshold'] = $this->get_config('expirythreshold'); 384 $fields['customint1'] = $this->get_config('groupkey'); 385 $fields['customint2'] = $this->get_config('longtimenosee'); 386 $fields['customint3'] = $this->get_config('maxenrolled'); 387 $fields['customint4'] = $this->get_config('sendcoursewelcomemessage'); 388 $fields['customint5'] = 0; 389 $fields['customint6'] = $this->get_config('newenrols'); 390 391 return $fields; 392 } 393 394 /** 395 * Send welcome email to specified user. 396 * 397 * @param stdClass $instance 398 * @param stdClass $user user record 399 * @return void 400 */ 401 protected function email_welcome_message($instance, $user) { 402 global $CFG, $DB; 403 404 $course = $DB->get_record('course', array('id'=>$instance->courseid), '*', MUST_EXIST); 405 $context = context_course::instance($course->id); 406 407 $a = new stdClass(); 408 $a->coursename = format_string($course->fullname, true, array('context'=>$context)); 409 $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id"; 410 411 if (trim($instance->customtext1) !== '') { 412 $message = $instance->customtext1; 413 $message = str_replace('{$a->coursename}', $a->coursename, $message); 414 $message = str_replace('{$a->profileurl}', $a->profileurl, $message); 415 if (strpos($message, '<') === false) { 416 // Plain text only. 417 $messagetext = $message; 418 $messagehtml = text_to_html($messagetext, null, false, true); 419 } else { 420 // This is most probably the tag/newline soup known as FORMAT_MOODLE. 421 $messagehtml = format_text($message, FORMAT_MOODLE, array('context'=>$context, 'para'=>false, 'newlines'=>true, 'filter'=>true)); 422 $messagetext = html_to_text($messagehtml); 423 } 424 } else { 425 $messagetext = get_string('welcometocoursetext', 'enrol_self', $a); 426 $messagehtml = text_to_html($messagetext, null, false, true); 427 } 428 429 $subject = get_string('welcometocourse', 'enrol_self', format_string($course->fullname, true, array('context'=>$context))); 430 431 $rusers = array(); 432 if (!empty($CFG->coursecontact)) { 433 $croles = explode(',', $CFG->coursecontact); 434 list($sort, $sortparams) = users_order_by_sql('u'); 435 $rusers = get_role_users($croles, $context, true, '', 'r.sortorder ASC, ' . $sort, null, '', '', '', '', $sortparams); 436 } 437 if ($rusers) { 438 $contact = reset($rusers); 439 } else { 440 $contact = core_user::get_support_user(); 441 } 442 443 // Directly emailing welcome message rather than using messaging. 444 email_to_user($user, $contact, $subject, $messagetext, $messagehtml); 445 } 446 447 /** 448 * Enrol self cron support. 449 * @return void 450 */ 451 public function cron() { 452 $trace = new text_progress_trace(); 453 $this->sync($trace, null); 454 $this->send_expiry_notifications($trace); 455 } 456 457 /** 458 * Sync all meta course links. 459 * 460 * @param progress_trace $trace 461 * @param int $courseid one course, empty mean all 462 * @return int 0 means ok, 1 means error, 2 means plugin disabled 463 */ 464 public function sync(progress_trace $trace, $courseid = null) { 465 global $DB; 466 467 if (!enrol_is_enabled('self')) { 468 $trace->finished(); 469 return 2; 470 } 471 472 // Unfortunately this may take a long time, execution can be interrupted safely here. 473 core_php_time_limit::raise(); 474 raise_memory_limit(MEMORY_HUGE); 475 476 $trace->output('Verifying self-enrolments...'); 477 478 $params = array('now'=>time(), 'useractive'=>ENROL_USER_ACTIVE, 'courselevel'=>CONTEXT_COURSE); 479 $coursesql = ""; 480 if ($courseid) { 481 $coursesql = "AND e.courseid = :courseid"; 482 $params['courseid'] = $courseid; 483 } 484 485 // Note: the logic of self enrolment guarantees that user logged in at least once (=== u.lastaccess set) 486 // and that user accessed course at least once too (=== user_lastaccess record exists). 487 488 // First deal with users that did not log in for a really long time - they do not have user_lastaccess records. 489 $sql = "SELECT e.*, ue.userid 490 FROM {user_enrolments} ue 491 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self' AND e.customint2 > 0) 492 JOIN {user} u ON u.id = ue.userid 493 WHERE :now - u.lastaccess > e.customint2 494 $coursesql"; 495 $rs = $DB->get_recordset_sql($sql, $params); 496 foreach ($rs as $instance) { 497 $userid = $instance->userid; 498 unset($instance->userid); 499 $this->unenrol_user($instance, $userid); 500 $days = $instance->customint2 / 60*60*24; 501 $trace->output("unenrolling user $userid from course $instance->courseid as they have did not log in for at least $days days", 1); 502 } 503 $rs->close(); 504 505 // Now unenrol from course user did not visit for a long time. 506 $sql = "SELECT e.*, ue.userid 507 FROM {user_enrolments} ue 508 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self' AND e.customint2 > 0) 509 JOIN {user_lastaccess} ul ON (ul.userid = ue.userid AND ul.courseid = e.courseid) 510 WHERE :now - ul.timeaccess > e.customint2 511 $coursesql"; 512 $rs = $DB->get_recordset_sql($sql, $params); 513 foreach ($rs as $instance) { 514 $userid = $instance->userid; 515 unset($instance->userid); 516 $this->unenrol_user($instance, $userid); 517 $days = $instance->customint2 / 60*60*24; 518 $trace->output("unenrolling user $userid from course $instance->courseid as they have did not access course for at least $days days", 1); 519 } 520 $rs->close(); 521 522 $trace->output('...user self-enrolment updates finished.'); 523 $trace->finished(); 524 525 $this->process_expirations($trace, $courseid); 526 527 return 0; 528 } 529 530 /** 531 * Returns the user who is responsible for self enrolments in given instance. 532 * 533 * Usually it is the first editing teacher - the person with "highest authority" 534 * as defined by sort_by_roleassignment_authority() having 'enrol/self:manage' 535 * capability. 536 * 537 * @param int $instanceid enrolment instance id 538 * @return stdClass user record 539 */ 540 protected function get_enroller($instanceid) { 541 global $DB; 542 543 if ($this->lasternollerinstanceid == $instanceid and $this->lasternoller) { 544 return $this->lasternoller; 545 } 546 547 $instance = $DB->get_record('enrol', array('id'=>$instanceid, 'enrol'=>$this->get_name()), '*', MUST_EXIST); 548 $context = context_course::instance($instance->courseid); 549 550 if ($users = get_enrolled_users($context, 'enrol/self:manage')) { 551 $users = sort_by_roleassignment_authority($users, $context); 552 $this->lasternoller = reset($users); 553 unset($users); 554 } else { 555 $this->lasternoller = parent::get_enroller($instanceid); 556 } 557 558 $this->lasternollerinstanceid = $instanceid; 559 560 return $this->lasternoller; 561 } 562 563 /** 564 * Gets an array of the user enrolment actions. 565 * 566 * @param course_enrolment_manager $manager 567 * @param stdClass $ue A user enrolment object 568 * @return array An array of user_enrolment_actions 569 */ 570 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) { 571 $actions = array(); 572 $context = $manager->get_context(); 573 $instance = $ue->enrolmentinstance; 574 $params = $manager->get_moodlepage()->url->params(); 575 $params['ue'] = $ue->id; 576 if ($this->allow_unenrol($instance) && has_capability("enrol/self:unenrol", $context)) { 577 $url = new moodle_url('/enrol/unenroluser.php', $params); 578 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id)); 579 } 580 if ($this->allow_manage($instance) && has_capability("enrol/self:manage", $context)) { 581 $url = new moodle_url('/enrol/editenrolment.php', $params); 582 $actions[] = new user_enrolment_action(new pix_icon('t/edit', ''), get_string('edit'), $url, array('class'=>'editenrollink', 'rel'=>$ue->id)); 583 } 584 return $actions; 585 } 586 587 /** 588 * Restore instance and map settings. 589 * 590 * @param restore_enrolments_structure_step $step 591 * @param stdClass $data 592 * @param stdClass $course 593 * @param int $oldid 594 */ 595 public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) { 596 global $DB; 597 if ($step->get_task()->get_target() == backup::TARGET_NEW_COURSE) { 598 $merge = false; 599 } else { 600 $merge = array( 601 'courseid' => $data->courseid, 602 'enrol' => $this->get_name(), 603 'roleid' => $data->roleid, 604 ); 605 } 606 if ($merge and $instances = $DB->get_records('enrol', $merge, 'id')) { 607 $instance = reset($instances); 608 $instanceid = $instance->id; 609 } else { 610 if (!empty($data->customint5)) { 611 if ($step->get_task()->is_samesite()) { 612 // Keep cohort restriction unchanged - we are on the same site. 613 } else { 614 // Use some id that can not exist in order to prevent self enrolment, 615 // because we do not know what cohort it is in this site. 616 $data->customint5 = -1; 617 } 618 } 619 $instanceid = $this->add_instance($course, (array)$data); 620 } 621 $step->set_mapping('enrol', $oldid, $instanceid); 622 } 623 624 /** 625 * Restore user enrolment. 626 * 627 * @param restore_enrolments_structure_step $step 628 * @param stdClass $data 629 * @param stdClass $instance 630 * @param int $oldinstancestatus 631 * @param int $userid 632 */ 633 public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) { 634 $this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $data->status); 635 } 636 637 /** 638 * Restore role assignment. 639 * 640 * @param stdClass $instance 641 * @param int $roleid 642 * @param int $userid 643 * @param int $contextid 644 */ 645 public function restore_role_assignment($instance, $roleid, $userid, $contextid) { 646 // This is necessary only because we may migrate other types to this instance, 647 // we do not use component in manual or self enrol. 648 role_assign($roleid, $userid, $contextid, '', 0); 649 } 650 651 /** 652 * Is it possible to delete enrol instance via standard UI? 653 * 654 * @param stdClass $instance 655 * @return bool 656 */ 657 public function can_delete_instance($instance) { 658 $context = context_course::instance($instance->courseid); 659 return has_capability('enrol/self:config', $context); 660 } 661 662 /** 663 * Is it possible to hide/show enrol instance via standard UI? 664 * 665 * @param stdClass $instance 666 * @return bool 667 */ 668 public function can_hide_show_instance($instance) { 669 $context = context_course::instance($instance->courseid); 670 return has_capability('enrol/self:config', $context); 671 } 672 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |