MDL-31627: Arialist theme do not resize content when there are no blocks
[moodle.git] / lib / enrollib.php
blob62e791ae2e5636665b98c0a15f1211ff6f4c31b5
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * This library includes the basic parts of enrol api.
20 * It is available on each page.
22 * @package core
23 * @subpackage enrol
24 * @copyright 2010 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 /** Course enrol instance enabled. (used in enrol->status) */
31 define('ENROL_INSTANCE_ENABLED', 0);
33 /** Course enrol instance disabled, user may enter course if other enrol instance enabled. (used in enrol->status)*/
34 define('ENROL_INSTANCE_DISABLED', 1);
36 /** User is active participant (used in user_enrolments->status)*/
37 define('ENROL_USER_ACTIVE', 0);
39 /** User participation in course is suspended (used in user_enrolments->status) */
40 define('ENROL_USER_SUSPENDED', 1);
42 /** @deprecated - enrol caching was reworked, use ENROL_MAX_TIMESTAMP instead */
43 define('ENROL_REQUIRE_LOGIN_CACHE_PERIOD', 1800);
45 /** The timestamp indicating forever */
46 define('ENROL_MAX_TIMESTAMP', 2147483647);
48 /** When user disappears from external source, the enrolment is completely removed */
49 define('ENROL_EXT_REMOVED_UNENROL', 0);
51 /** When user disappears from external source, the enrolment is kept as is - one way sync */
52 define('ENROL_EXT_REMOVED_KEEP', 1);
54 /** enrol plugin feature describing requested restore type */
55 define('ENROL_RESTORE_TYPE', 'enrolrestore');
56 /** User custom backup/restore class stored in backup/moodle2/ subdirectory */
57 define('ENROL_RESTORE_CLASS', 'class');
58 /** Restore all custom fields from enrol table without any changes and all user_enrolments records */
59 define('ENROL_RESTORE_EXACT', 'exact');
60 /** Restore enrol record like ENROL_RESTORE_EXACT, but no user enrolments */
61 define('ENROL_RESTORE_NOUSERS', 'nousers');
63 /**
64 * When user disappears from external source, user enrolment is suspended, roles are kept as is.
65 * In some cases user needs a role with some capability to be visible in UI - suc has in gradebook,
66 * assignments, etc.
68 define('ENROL_EXT_REMOVED_SUSPEND', 2);
70 /**
71 * When user disappears from external source, the enrolment is suspended and roles assigned
72 * by enrol instance are removed. Please note that user may "disappear" from gradebook and other areas.
73 * */
74 define('ENROL_EXT_REMOVED_SUSPENDNOROLES', 3);
76 /**
77 * Returns instances of enrol plugins
78 * @param bool $enabled return enabled only
79 * @return array of enrol plugins name=>instance
81 function enrol_get_plugins($enabled) {
82 global $CFG;
84 $result = array();
86 if ($enabled) {
87 // sorted by enabled plugin order
88 $enabled = explode(',', $CFG->enrol_plugins_enabled);
89 $plugins = array();
90 foreach ($enabled as $plugin) {
91 $plugins[$plugin] = "$CFG->dirroot/enrol/$plugin";
93 } else {
94 // sorted alphabetically
95 $plugins = get_plugin_list('enrol');
96 ksort($plugins);
99 foreach ($plugins as $plugin=>$location) {
100 if (!file_exists("$location/lib.php")) {
101 continue;
103 include_once("$location/lib.php");
104 $class = "enrol_{$plugin}_plugin";
105 if (!class_exists($class)) {
106 continue;
109 $result[$plugin] = new $class();
112 return $result;
116 * Returns instance of enrol plugin
117 * @param string $name name of enrol plugin ('manual', 'guest', ...)
118 * @return enrol_plugin
120 function enrol_get_plugin($name) {
121 global $CFG;
123 $name = clean_param($name, PARAM_PLUGIN);
125 if (empty($name)) {
126 // ignore malformed or missing plugin names completely
127 return null;
130 $location = "$CFG->dirroot/enrol/$name";
132 if (!file_exists("$location/lib.php")) {
133 return null;
135 include_once("$location/lib.php");
136 $class = "enrol_{$name}_plugin";
137 if (!class_exists($class)) {
138 return null;
141 return new $class();
145 * Returns enrolment instances in given course.
146 * @param int $courseid
147 * @param bool $enabled
148 * @return array of enrol instances
150 function enrol_get_instances($courseid, $enabled) {
151 global $DB, $CFG;
153 if (!$enabled) {
154 return $DB->get_records('enrol', array('courseid'=>$courseid), 'sortorder,id');
157 $result = $DB->get_records('enrol', array('courseid'=>$courseid, 'status'=>ENROL_INSTANCE_ENABLED), 'sortorder,id');
159 $enabled = explode(',', $CFG->enrol_plugins_enabled);
160 foreach ($result as $key=>$instance) {
161 if (!in_array($instance->enrol, $enabled)) {
162 unset($result[$key]);
163 continue;
165 if (!file_exists("$CFG->dirroot/enrol/$instance->enrol/lib.php")) {
166 // broken plugin
167 unset($result[$key]);
168 continue;
172 return $result;
176 * Checks if a given plugin is in the list of enabled enrolment plugins.
178 * @param string $enrol Enrolment plugin name
179 * @return boolean Whether the plugin is enabled
181 function enrol_is_enabled($enrol) {
182 global $CFG;
184 if (empty($CFG->enrol_plugins_enabled)) {
185 return false;
187 return in_array($enrol, explode(',', $CFG->enrol_plugins_enabled));
191 * Check all the login enrolment information for the given user object
192 * by querying the enrolment plugins
194 * This function may be very slow, use only once after log-in or login-as.
196 * @param stdClass $user
197 * @return void
199 function enrol_check_plugins($user) {
200 global $CFG;
202 if (empty($user->id) or isguestuser($user)) {
203 // shortcut - there is no enrolment work for guests and not-logged-in users
204 return;
207 if (is_siteadmin()) {
208 // no sync for admin user, please use admin accounts only for admin tasks like the unix root user!
209 // if plugin fails on sync admins need to be able to log in and fix the settings
210 return;
213 static $inprogress = array(); // To prevent this function being called more than once in an invocation
215 if (!empty($inprogress[$user->id])) {
216 return;
219 $inprogress[$user->id] = true; // Set the flag
221 $enabled = enrol_get_plugins(true);
223 foreach($enabled as $enrol) {
224 $enrol->sync_user_enrolments($user);
227 unset($inprogress[$user->id]); // Unset the flag
231 * Do these two students share any course?
233 * The courses has to be visible and enrolments has to be active,
234 * timestart and timeend restrictions are ignored.
236 * This function calls {@see enrol_get_shared_courses()} setting checkexistsonly
237 * to true.
239 * @param stdClass|int $user1
240 * @param stdClass|int $user2
241 * @return bool
243 function enrol_sharing_course($user1, $user2) {
244 return enrol_get_shared_courses($user1, $user2, false, true);
248 * Returns any courses shared by the two users
250 * The courses has to be visible and enrolments has to be active,
251 * timestart and timeend restrictions are ignored.
253 * @global moodle_database $DB
254 * @param stdClass|int $user1
255 * @param stdClass|int $user2
256 * @param bool $preloadcontexts If set to true contexts for the returned courses
257 * will be preloaded.
258 * @param bool $checkexistsonly If set to true then this function will return true
259 * if the users share any courses and false if not.
260 * @return array|bool An array of courses that both users are enrolled in OR if
261 * $checkexistsonly set returns true if the users share any courses
262 * and false if not.
264 function enrol_get_shared_courses($user1, $user2, $preloadcontexts = false, $checkexistsonly = false) {
265 global $DB, $CFG;
267 $user1 = !empty($user1->id) ? $user1->id : $user1;
268 $user2 = !empty($user2->id) ? $user2->id : $user2;
270 if (empty($user1) or empty($user2)) {
271 return false;
274 if (!$plugins = explode(',', $CFG->enrol_plugins_enabled)) {
275 return false;
278 list($plugins, $params) = $DB->get_in_or_equal($plugins, SQL_PARAMS_NAMED, 'ee');
279 $params['enabled'] = ENROL_INSTANCE_ENABLED;
280 $params['active1'] = ENROL_USER_ACTIVE;
281 $params['active2'] = ENROL_USER_ACTIVE;
282 $params['user1'] = $user1;
283 $params['user2'] = $user2;
285 $ctxselect = '';
286 $ctxjoin = '';
287 if ($preloadcontexts) {
288 list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
291 $sql = "SELECT c.* $ctxselect
292 FROM {course} c
293 JOIN (
294 SELECT DISTINCT c.id
295 FROM {enrol} e
296 JOIN {user_enrolments} ue1 ON (ue1.enrolid = e.id AND ue1.status = :active1 AND ue1.userid = :user1)
297 JOIN {user_enrolments} ue2 ON (ue2.enrolid = e.id AND ue2.status = :active2 AND ue2.userid = :user2)
298 JOIN {course} c ON (c.id = e.courseid AND c.visible = 1)
299 WHERE e.status = :enabled AND e.enrol $plugins
300 ) ec ON ec.id = c.id
301 $ctxjoin";
303 if ($checkexistsonly) {
304 return $DB->record_exists_sql($sql, $params);
305 } else {
306 $courses = $DB->get_records_sql($sql, $params);
307 if ($preloadcontexts) {
308 array_map('context_instance_preload', $courses);
310 return $courses;
315 * This function adds necessary enrol plugins UI into the course edit form.
317 * @param MoodleQuickForm $mform
318 * @param object $data course edit form data
319 * @param object $context context of existing course or parent category if course does not exist
320 * @return void
322 function enrol_course_edit_form(MoodleQuickForm $mform, $data, $context) {
323 $plugins = enrol_get_plugins(true);
324 if (!empty($data->id)) {
325 $instances = enrol_get_instances($data->id, false);
326 foreach ($instances as $instance) {
327 if (!isset($plugins[$instance->enrol])) {
328 continue;
330 $plugin = $plugins[$instance->enrol];
331 $plugin->course_edit_form($instance, $mform, $data, $context);
333 } else {
334 foreach ($plugins as $plugin) {
335 $plugin->course_edit_form(NULL, $mform, $data, $context);
341 * Validate course edit form data
343 * @param array $data raw form data
344 * @param object $context context of existing course or parent category if course does not exist
345 * @return array errors array
347 function enrol_course_edit_validation(array $data, $context) {
348 $errors = array();
349 $plugins = enrol_get_plugins(true);
351 if (!empty($data['id'])) {
352 $instances = enrol_get_instances($data['id'], false);
353 foreach ($instances as $instance) {
354 if (!isset($plugins[$instance->enrol])) {
355 continue;
357 $plugin = $plugins[$instance->enrol];
358 $errors = array_merge($errors, $plugin->course_edit_validation($instance, $data, $context));
360 } else {
361 foreach ($plugins as $plugin) {
362 $errors = array_merge($errors, $plugin->course_edit_validation(NULL, $data, $context));
366 return $errors;
370 * Update enrol instances after course edit form submission
371 * @param bool $inserted true means new course added, false course already existed
372 * @param object $course
373 * @param object $data form data
374 * @return void
376 function enrol_course_updated($inserted, $course, $data) {
377 global $DB, $CFG;
379 $plugins = enrol_get_plugins(true);
381 foreach ($plugins as $plugin) {
382 $plugin->course_updated($inserted, $course, $data);
387 * Add navigation nodes
388 * @param navigation_node $coursenode
389 * @param object $course
390 * @return void
392 function enrol_add_course_navigation(navigation_node $coursenode, $course) {
393 global $CFG;
395 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
397 $instances = enrol_get_instances($course->id, true);
398 $plugins = enrol_get_plugins(true);
400 // we do not want to break all course pages if there is some borked enrol plugin, right?
401 foreach ($instances as $k=>$instance) {
402 if (!isset($plugins[$instance->enrol])) {
403 unset($instances[$k]);
407 $usersnode = $coursenode->add(get_string('users'), null, navigation_node::TYPE_CONTAINER, null, 'users');
409 if ($course->id != SITEID) {
410 // list all participants - allows assigning roles, groups, etc.
411 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
412 $url = new moodle_url('/enrol/users.php', array('id'=>$course->id));
413 $usersnode->add(get_string('enrolledusers', 'enrol'), $url, navigation_node::TYPE_SETTING, null, 'review', new pix_icon('i/users', ''));
416 // manage enrol plugin instances
417 if (has_capability('moodle/course:enrolconfig', $coursecontext) or has_capability('moodle/course:enrolreview', $coursecontext)) {
418 $url = new moodle_url('/enrol/instances.php', array('id'=>$course->id));
419 } else {
420 $url = NULL;
422 $instancesnode = $usersnode->add(get_string('enrolmentinstances', 'enrol'), $url, navigation_node::TYPE_SETTING, null, 'manageinstances');
424 // each instance decides how to configure itself or how many other nav items are exposed
425 foreach ($instances as $instance) {
426 if (!isset($plugins[$instance->enrol])) {
427 continue;
429 $plugins[$instance->enrol]->add_course_navigation($instancesnode, $instance);
432 if (!$url) {
433 $instancesnode->trim_if_empty();
437 // Manage groups in this course or even frontpage
438 if (($course->groupmode || !$course->groupmodeforce) && has_capability('moodle/course:managegroups', $coursecontext)) {
439 $url = new moodle_url('/group/index.php', array('id'=>$course->id));
440 $usersnode->add(get_string('groups'), $url, navigation_node::TYPE_SETTING, null, 'groups', new pix_icon('i/group', ''));
443 if (has_any_capability(array( 'moodle/role:assign', 'moodle/role:safeoverride','moodle/role:override', 'moodle/role:review'), $coursecontext)) {
444 // Override roles
445 if (has_capability('moodle/role:review', $coursecontext)) {
446 $url = new moodle_url('/admin/roles/permissions.php', array('contextid'=>$coursecontext->id));
447 } else {
448 $url = NULL;
450 $permissionsnode = $usersnode->add(get_string('permissions', 'role'), $url, navigation_node::TYPE_SETTING, null, 'override');
452 // Add assign or override roles if allowed
453 if ($course->id == SITEID or (!empty($CFG->adminsassignrolesincourse) and is_siteadmin())) {
454 if (has_capability('moodle/role:assign', $coursecontext)) {
455 $url = new moodle_url('/admin/roles/assign.php', array('contextid'=>$coursecontext->id));
456 $permissionsnode->add(get_string('assignedroles', 'role'), $url, navigation_node::TYPE_SETTING, null, 'roles', new pix_icon('i/roles', ''));
459 // Check role permissions
460 if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride','moodle/role:override', 'moodle/role:assign'), $coursecontext)) {
461 $url = new moodle_url('/admin/roles/check.php', array('contextid'=>$coursecontext->id));
462 $permissionsnode->add(get_string('checkpermissions', 'role'), $url, navigation_node::TYPE_SETTING, null, 'permissions', new pix_icon('i/checkpermissions', ''));
466 // Deal somehow with users that are not enrolled but still got a role somehow
467 if ($course->id != SITEID) {
468 //TODO, create some new UI for role assignments at course level
469 if (has_capability('moodle/role:assign', $coursecontext)) {
470 $url = new moodle_url('/enrol/otherusers.php', array('id'=>$course->id));
471 $usersnode->add(get_string('notenrolledusers', 'enrol'), $url, navigation_node::TYPE_SETTING, null, 'otherusers', new pix_icon('i/roles', ''));
475 // just in case nothing was actually added
476 $usersnode->trim_if_empty();
478 if ($course->id != SITEID) {
479 // Unenrol link
480 if (is_enrolled($coursecontext)) {
481 foreach ($instances as $instance) {
482 if (!isset($plugins[$instance->enrol])) {
483 continue;
485 $plugin = $plugins[$instance->enrol];
486 if ($unenrollink = $plugin->get_unenrolself_link($instance)) {
487 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
488 $coursenode->add(get_string('unenrolme', 'core_enrol', $shortname), $unenrollink, navigation_node::TYPE_SETTING, null, 'unenrolself', new pix_icon('i/user', ''));
489 break;
490 //TODO. deal with multiple unenrol links - not likely case, but still...
493 } else {
494 if (is_viewing($coursecontext)) {
495 // better not show any enrol link, this is intended for managers and inspectors
496 } else {
497 foreach ($instances as $instance) {
498 if (!isset($plugins[$instance->enrol])) {
499 continue;
501 $plugin = $plugins[$instance->enrol];
502 if ($plugin->show_enrolme_link($instance)) {
503 $url = new moodle_url('/enrol/index.php', array('id'=>$course->id));
504 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
505 $coursenode->add(get_string('enrolme', 'core_enrol', $shortname), $url, navigation_node::TYPE_SETTING, null, 'enrolself', new pix_icon('i/user', ''));
506 break;
515 * Returns list of courses current $USER is enrolled in and can access
517 * - $fields is an array of field names to ADD
518 * so name the fields you really need, which will
519 * be added and uniq'd
521 * @param string|array $fields
522 * @param string $sort
523 * @param int $limit max number of courses
524 * @return array
526 function enrol_get_my_courses($fields = NULL, $sort = 'visible DESC,sortorder ASC', $limit = 0) {
527 global $DB, $USER;
529 // Guest account does not have any courses
530 if (isguestuser() or !isloggedin()) {
531 return(array());
534 $basefields = array('id', 'category', 'sortorder',
535 'shortname', 'fullname', 'idnumber',
536 'startdate', 'visible',
537 'groupmode', 'groupmodeforce');
539 if (empty($fields)) {
540 $fields = $basefields;
541 } else if (is_string($fields)) {
542 // turn the fields from a string to an array
543 $fields = explode(',', $fields);
544 $fields = array_map('trim', $fields);
545 $fields = array_unique(array_merge($basefields, $fields));
546 } else if (is_array($fields)) {
547 $fields = array_unique(array_merge($basefields, $fields));
548 } else {
549 throw new coding_exception('Invalid $fileds parameter in enrol_get_my_courses()');
551 if (in_array('*', $fields)) {
552 $fields = array('*');
555 $orderby = "";
556 $sort = trim($sort);
557 if (!empty($sort)) {
558 $rawsorts = explode(',', $sort);
559 $sorts = array();
560 foreach ($rawsorts as $rawsort) {
561 $rawsort = trim($rawsort);
562 if (strpos($rawsort, 'c.') === 0) {
563 $rawsort = substr($rawsort, 2);
565 $sorts[] = trim($rawsort);
567 $sort = 'c.'.implode(',c.', $sorts);
568 $orderby = "ORDER BY $sort";
571 $wheres = array("c.id <> :siteid");
572 $params = array('siteid'=>SITEID);
574 if (isset($USER->loginascontext) and $USER->loginascontext->contextlevel == CONTEXT_COURSE) {
575 // list _only_ this course - anything else is asking for trouble...
576 $wheres[] = "courseid = :loginas";
577 $params['loginas'] = $USER->loginascontext->instanceid;
580 $coursefields = 'c.' .join(',c.', $fields);
581 list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
582 $wheres = implode(" AND ", $wheres);
584 //note: we can not use DISTINCT + text fields due to Oracle and MS limitations, that is why we have the subselect there
585 $sql = "SELECT $coursefields $ccselect
586 FROM {course} c
587 JOIN (SELECT DISTINCT e.courseid
588 FROM {enrol} e
589 JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = :userid)
590 WHERE ue.status = :active AND e.status = :enabled AND ue.timestart < :now1 AND (ue.timeend = 0 OR ue.timeend > :now2)
591 ) en ON (en.courseid = c.id)
592 $ccjoin
593 WHERE $wheres
594 $orderby";
595 $params['userid'] = $USER->id;
596 $params['active'] = ENROL_USER_ACTIVE;
597 $params['enabled'] = ENROL_INSTANCE_ENABLED;
598 $params['now1'] = round(time(), -2); // improves db caching
599 $params['now2'] = $params['now1'];
601 $courses = $DB->get_records_sql($sql, $params, 0, $limit);
603 // preload contexts and check visibility
604 foreach ($courses as $id=>$course) {
605 context_instance_preload($course);
606 if (!$course->visible) {
607 if (!$context = get_context_instance(CONTEXT_COURSE, $id)) {
608 unset($courses[$id]);
609 continue;
611 if (!has_capability('moodle/course:viewhiddencourses', $context)) {
612 unset($courses[$id]);
613 continue;
616 $courses[$id] = $course;
619 //wow! Is that really all? :-D
621 return $courses;
625 * Returns course enrolment information icons.
627 * @param object $course
628 * @param array $instances enrol instances of this course, improves performance
629 * @return array of pix_icon
631 function enrol_get_course_info_icons($course, array $instances = NULL) {
632 $icons = array();
633 if (is_null($instances)) {
634 $instances = enrol_get_instances($course->id, true);
636 $plugins = enrol_get_plugins(true);
637 foreach ($plugins as $name => $plugin) {
638 $pis = array();
639 foreach ($instances as $instance) {
640 if ($instance->status != ENROL_INSTANCE_ENABLED or $instance->courseid != $course->id) {
641 debugging('Invalid instances parameter submitted in enrol_get_info_icons()');
642 continue;
644 if ($instance->enrol == $name) {
645 $pis[$instance->id] = $instance;
648 if ($pis) {
649 $icons = array_merge($icons, $plugin->get_info_icons($pis));
652 return $icons;
656 * Returns course enrolment detailed information.
658 * @param object $course
659 * @return array of html fragments - can be used to construct lists
661 function enrol_get_course_description_texts($course) {
662 $lines = array();
663 $instances = enrol_get_instances($course->id, true);
664 $plugins = enrol_get_plugins(true);
665 foreach ($instances as $instance) {
666 if (!isset($plugins[$instance->enrol])) {
667 //weird
668 continue;
670 $plugin = $plugins[$instance->enrol];
671 $text = $plugin->get_description_text($instance);
672 if ($text !== NULL) {
673 $lines[] = $text;
676 return $lines;
680 * Returns list of courses user is enrolled into.
682 * - $fields is an array of fieldnames to ADD
683 * so name the fields you really need, which will
684 * be added and uniq'd
686 * @param int $userid
687 * @param bool $onlyactive return only active enrolments in courses user may see
688 * @param string|array $fields
689 * @param string $sort
690 * @return array
692 function enrol_get_users_courses($userid, $onlyactive = false, $fields = NULL, $sort = 'visible DESC,sortorder ASC') {
693 global $DB;
695 // Guest account does not have any courses
696 if (isguestuser($userid) or empty($userid)) {
697 return(array());
700 $basefields = array('id', 'category', 'sortorder',
701 'shortname', 'fullname', 'idnumber',
702 'startdate', 'visible',
703 'groupmode', 'groupmodeforce');
705 if (empty($fields)) {
706 $fields = $basefields;
707 } else if (is_string($fields)) {
708 // turn the fields from a string to an array
709 $fields = explode(',', $fields);
710 $fields = array_map('trim', $fields);
711 $fields = array_unique(array_merge($basefields, $fields));
712 } else if (is_array($fields)) {
713 $fields = array_unique(array_merge($basefields, $fields));
714 } else {
715 throw new coding_exception('Invalid $fileds parameter in enrol_get_my_courses()');
717 if (in_array('*', $fields)) {
718 $fields = array('*');
721 $orderby = "";
722 $sort = trim($sort);
723 if (!empty($sort)) {
724 $rawsorts = explode(',', $sort);
725 $sorts = array();
726 foreach ($rawsorts as $rawsort) {
727 $rawsort = trim($rawsort);
728 if (strpos($rawsort, 'c.') === 0) {
729 $rawsort = substr($rawsort, 2);
731 $sorts[] = trim($rawsort);
733 $sort = 'c.'.implode(',c.', $sorts);
734 $orderby = "ORDER BY $sort";
737 $params = array('siteid'=>SITEID);
739 if ($onlyactive) {
740 $subwhere = "WHERE ue.status = :active AND e.status = :enabled AND ue.timestart < :now1 AND (ue.timeend = 0 OR ue.timeend > :now2)";
741 $params['now1'] = round(time(), -2); // improves db caching
742 $params['now2'] = $params['now1'];
743 $params['active'] = ENROL_USER_ACTIVE;
744 $params['enabled'] = ENROL_INSTANCE_ENABLED;
745 } else {
746 $subwhere = "";
749 $coursefields = 'c.' .join(',c.', $fields);
750 list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
752 //note: we can not use DISTINCT + text fields due to Oracle and MS limitations, that is why we have the subselect there
753 $sql = "SELECT $coursefields $ccselect
754 FROM {course} c
755 JOIN (SELECT DISTINCT e.courseid
756 FROM {enrol} e
757 JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = :userid)
758 $subwhere
759 ) en ON (en.courseid = c.id)
760 $ccjoin
761 WHERE c.id <> :siteid
762 $orderby";
763 $params['userid'] = $userid;
765 $courses = $DB->get_records_sql($sql, $params);
767 // preload contexts and check visibility
768 foreach ($courses as $id=>$course) {
769 context_instance_preload($course);
770 if ($onlyactive) {
771 if (!$course->visible) {
772 if (!$context = get_context_instance(CONTEXT_COURSE, $id)) {
773 unset($courses[$id]);
774 continue;
776 if (!has_capability('moodle/course:viewhiddencourses', $context, $userid)) {
777 unset($courses[$id]);
778 continue;
782 $courses[$id] = $course;
785 //wow! Is that really all? :-D
787 return $courses;
792 * Called when user is about to be deleted.
793 * @param object $user
794 * @return void
796 function enrol_user_delete($user) {
797 global $DB;
799 $plugins = enrol_get_plugins(true);
800 foreach ($plugins as $plugin) {
801 $plugin->user_delete($user);
804 // force cleanup of all broken enrolments
805 $DB->delete_records('user_enrolments', array('userid'=>$user->id));
809 * Called when course is about to be deleted.
810 * @param stdClass $course
811 * @return void
813 function enrol_course_delete($course) {
814 global $DB;
816 $instances = enrol_get_instances($course->id, false);
817 $plugins = enrol_get_plugins(true);
818 foreach ($instances as $instance) {
819 if (isset($plugins[$instance->enrol])) {
820 $plugins[$instance->enrol]->delete_instance($instance);
822 // low level delete in case plugin did not do it
823 $DB->delete_records('user_enrolments', array('enrolid'=>$instance->id));
824 $DB->delete_records('role_assignments', array('itemid'=>$instance->id, 'component'=>'enrol_'.$instance->enrol));
825 $DB->delete_records('user_enrolments', array('enrolid'=>$instance->id));
826 $DB->delete_records('enrol', array('id'=>$instance->id));
831 * Try to enrol user via default internal auth plugin.
833 * For now this is always using the manual enrol plugin...
835 * @param $courseid
836 * @param $userid
837 * @param $roleid
838 * @param $timestart
839 * @param $timeend
840 * @return bool success
842 function enrol_try_internal_enrol($courseid, $userid, $roleid = null, $timestart = 0, $timeend = 0) {
843 global $DB;
845 //note: this is hardcoded to manual plugin for now
847 if (!enrol_is_enabled('manual')) {
848 return false;
851 if (!$enrol = enrol_get_plugin('manual')) {
852 return false;
854 if (!$instances = $DB->get_records('enrol', array('enrol'=>'manual', 'courseid'=>$courseid, 'status'=>ENROL_INSTANCE_ENABLED), 'sortorder,id ASC')) {
855 return false;
857 $instance = reset($instances);
859 $enrol->enrol_user($instance, $userid, $roleid, $timestart, $timeend);
861 return true;
865 * Is there a chance users might self enrol
866 * @param int $courseid
867 * @return bool
869 function enrol_selfenrol_available($courseid) {
870 $result = false;
872 $plugins = enrol_get_plugins(true);
873 $enrolinstances = enrol_get_instances($courseid, true);
874 foreach($enrolinstances as $instance) {
875 if (!isset($plugins[$instance->enrol])) {
876 continue;
878 if ($instance->enrol === 'guest') {
879 // blacklist known temporary guest plugins
880 continue;
882 if ($plugins[$instance->enrol]->show_enrolme_link($instance)) {
883 $result = true;
884 break;
888 return $result;
892 * This function returns the end of current active user enrolment.
894 * It deals correctly with multiple overlapping user enrolments.
896 * @param int $courseid
897 * @param int $userid
898 * @return int|bool timestamp when active enrolment ends, false means no active enrolment now, 0 means never
900 function enrol_get_enrolment_end($courseid, $userid) {
901 global $DB;
903 $sql = "SELECT ue.*
904 FROM {user_enrolments} ue
905 JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid)
906 JOIN {user} u ON u.id = ue.userid
907 WHERE ue.userid = :userid AND ue.status = :active AND e.status = :enabled AND u.deleted = 0";
908 $params = array('enabled'=>ENROL_INSTANCE_ENABLED, 'active'=>ENROL_USER_ACTIVE, 'userid'=>$userid, 'courseid'=>$courseid);
910 if (!$enrolments = $DB->get_records_sql($sql, $params)) {
911 return false;
914 $changes = array();
916 foreach ($enrolments as $ue) {
917 $start = (int)$ue->timestart;
918 $end = (int)$ue->timeend;
919 if ($end != 0 and $end < $start) {
920 debugging('Invalid enrolment start or end in user_enrolment id:'.$ue->id);
921 continue;
923 if (isset($changes[$start])) {
924 $changes[$start] = $changes[$start] + 1;
925 } else {
926 $changes[$start] = 1;
928 if ($end === 0) {
929 // no end
930 } else if (isset($changes[$end])) {
931 $changes[$end] = $changes[$end] - 1;
932 } else {
933 $changes[$end] = -1;
937 // let's sort then enrolment starts&ends and go through them chronologically,
938 // looking for current status and the next future end of enrolment
939 ksort($changes);
941 $now = time();
942 $current = 0;
943 $present = null;
945 foreach ($changes as $time => $change) {
946 if ($time > $now) {
947 if ($present === null) {
948 // we have just went past current time
949 $present = $current;
950 if ($present < 1) {
951 // no enrolment active
952 return false;
955 if ($present !== null) {
956 // we are already in the future - look for possible end
957 if ($current + $change < 1) {
958 return $time;
962 $current += $change;
965 if ($current > 0) {
966 return 0;
967 } else {
968 return false;
974 * All enrol plugins should be based on this class,
975 * this is also the main source of documentation.
977 abstract class enrol_plugin {
978 protected $config = null;
981 * Returns name of this enrol plugin
982 * @return string
984 public function get_name() {
985 // second word in class is always enrol name, sorry, no fancy plugin names with _
986 $words = explode('_', get_class($this));
987 return $words[1];
991 * Returns localised name of enrol instance
993 * @param object $instance (null is accepted too)
994 * @return string
996 public function get_instance_name($instance) {
997 if (empty($instance->name)) {
998 $enrol = $this->get_name();
999 return get_string('pluginname', 'enrol_'.$enrol);
1000 } else {
1001 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid);
1002 return format_string($instance->name, true, array('context'=>$context));
1007 * Returns optional enrolment information icons.
1009 * This is used in course list for quick overview of enrolment options.
1011 * We are not using single instance parameter because sometimes
1012 * we might want to prevent icon repetition when multiple instances
1013 * of one type exist. One instance may also produce several icons.
1015 * @param array $instances all enrol instances of this type in one course
1016 * @return array of pix_icon
1018 public function get_info_icons(array $instances) {
1019 return array();
1023 * Returns optional enrolment instance description text.
1025 * This is used in detailed course information.
1028 * @param object $instance
1029 * @return string short html text
1031 public function get_description_text($instance) {
1032 return null;
1036 * Makes sure config is loaded and cached.
1037 * @return void
1039 protected function load_config() {
1040 if (!isset($this->config)) {
1041 $name = $this->get_name();
1042 $this->config = get_config("enrol_$name");
1047 * Returns plugin config value
1048 * @param string $name
1049 * @param string $default value if config does not exist yet
1050 * @return string value or default
1052 public function get_config($name, $default = NULL) {
1053 $this->load_config();
1054 return isset($this->config->$name) ? $this->config->$name : $default;
1058 * Sets plugin config value
1059 * @param string $name name of config
1060 * @param string $value string config value, null means delete
1061 * @return string value
1063 public function set_config($name, $value) {
1064 $pluginname = $this->get_name();
1065 $this->load_config();
1066 if ($value === NULL) {
1067 unset($this->config->$name);
1068 } else {
1069 $this->config->$name = $value;
1071 set_config($name, $value, "enrol_$pluginname");
1075 * Does this plugin assign protected roles are can they be manually removed?
1076 * @return bool - false means anybody may tweak roles, it does not use itemid and component when assigning roles
1078 public function roles_protected() {
1079 return true;
1083 * Does this plugin allow manual enrolments?
1085 * @param stdClass $instance course enrol instance
1086 * All plugins allowing this must implement 'enrol/xxx:enrol' capability
1088 * @return bool - true means user with 'enrol/xxx:enrol' may enrol others freely, false means nobody may add more enrolments manually
1090 public function allow_enrol(stdClass $instance) {
1091 return false;
1095 * Does this plugin allow manual unenrolment of all users?
1096 * All plugins allowing this must implement 'enrol/xxx:unenrol' capability
1098 * @param stdClass $instance course enrol instance
1099 * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol others freely, false means nobody may touch user_enrolments
1101 public function allow_unenrol(stdClass $instance) {
1102 return false;
1106 * Does this plugin allow manual unenrolment of a specific user?
1107 * All plugins allowing this must implement 'enrol/xxx:unenrol' capability
1109 * This is useful especially for synchronisation plugins that
1110 * do suspend instead of full unenrolment.
1112 * @param stdClass $instance course enrol instance
1113 * @param stdClass $ue record from user_enrolments table, specifies user
1115 * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol this user, false means nobody may touch this user enrolment
1117 public function allow_unenrol_user(stdClass $instance, stdClass $ue) {
1118 return $this->allow_unenrol($instance);
1122 * Does this plugin allow manual changes in user_enrolments table?
1124 * All plugins allowing this must implement 'enrol/xxx:manage' capability
1126 * @param stdClass $instance course enrol instance
1127 * @return bool - true means it is possible to change enrol period and status in user_enrolments table
1129 public function allow_manage(stdClass $instance) {
1130 return false;
1134 * Does this plugin support some way to user to self enrol?
1136 * @param stdClass $instance course enrol instance
1138 * @return bool - true means show "Enrol me in this course" link in course UI
1140 public function show_enrolme_link(stdClass $instance) {
1141 return false;
1145 * Attempt to automatically enrol current user in course without any interaction,
1146 * calling code has to make sure the plugin and instance are active.
1148 * This should return either a timestamp in the future or false.
1150 * @param stdClass $instance course enrol instance
1151 * @return bool|int false means not enrolled, integer means timeend
1153 public function try_autoenrol(stdClass $instance) {
1154 global $USER;
1156 return false;
1160 * Attempt to automatically gain temporary guest access to course,
1161 * calling code has to make sure the plugin and instance are active.
1163 * This should return either a timestamp in the future or false.
1165 * @param stdClass $instance course enrol instance
1166 * @return bool|int false means no guest access, integer means timeend
1168 public function try_guestaccess(stdClass $instance) {
1169 global $USER;
1171 return false;
1175 * Enrol user into course via enrol instance.
1177 * @param stdClass $instance
1178 * @param int $userid
1179 * @param int $roleid optional role id
1180 * @param int $timestart 0 means unknown
1181 * @param int $timeend 0 means forever
1182 * @param int $status default to ENROL_USER_ACTIVE for new enrolments, no change by default in updates
1183 * @return void
1185 public function enrol_user(stdClass $instance, $userid, $roleid = NULL, $timestart = 0, $timeend = 0, $status = NULL) {
1186 global $DB, $USER, $CFG; // CFG necessary!!!
1188 if ($instance->courseid == SITEID) {
1189 throw new coding_exception('invalid attempt to enrol into frontpage course!');
1192 $name = $this->get_name();
1193 $courseid = $instance->courseid;
1195 if ($instance->enrol !== $name) {
1196 throw new coding_exception('invalid enrol instance!');
1198 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid, MUST_EXIST);
1200 $inserted = false;
1201 $updated = false;
1202 if ($ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid))) {
1203 //only update if timestart or timeend or status are different.
1204 if ($ue->timestart != $timestart or $ue->timeend != $timeend or (!is_null($status) and $ue->status != $status)) {
1205 $ue->timestart = $timestart;
1206 $ue->timeend = $timeend;
1207 if (!is_null($status)) {
1208 $ue->status = $status;
1210 $ue->modifierid = $USER->id;
1211 $ue->timemodified = time();
1212 $DB->update_record('user_enrolments', $ue);
1214 $updated = true;
1216 } else {
1217 $ue = new stdClass();
1218 $ue->enrolid = $instance->id;
1219 $ue->status = is_null($status) ? ENROL_USER_ACTIVE : $status;
1220 $ue->userid = $userid;
1221 $ue->timestart = $timestart;
1222 $ue->timeend = $timeend;
1223 $ue->modifierid = $USER->id;
1224 $ue->timecreated = time();
1225 $ue->timemodified = $ue->timecreated;
1226 $ue->id = $DB->insert_record('user_enrolments', $ue);
1228 $inserted = true;
1231 if ($inserted) {
1232 // add extra info and trigger event
1233 $ue->courseid = $courseid;
1234 $ue->enrol = $name;
1235 events_trigger('user_enrolled', $ue);
1236 } else if ($updated) {
1237 $ue->courseid = $courseid;
1238 $ue->enrol = $name;
1239 events_trigger('user_enrol_modified', $ue);
1240 // resets current enrolment caches
1241 $context->mark_dirty();
1244 if ($roleid) {
1245 // this must be done after the enrolment event so that the role_assigned event is triggered afterwards
1246 if ($this->roles_protected()) {
1247 role_assign($roleid, $userid, $context->id, 'enrol_'.$name, $instance->id);
1248 } else {
1249 role_assign($roleid, $userid, $context->id);
1253 // reset current user enrolment caching
1254 if ($userid == $USER->id) {
1255 if (isset($USER->enrol['enrolled'][$courseid])) {
1256 unset($USER->enrol['enrolled'][$courseid]);
1258 if (isset($USER->enrol['tempguest'][$courseid])) {
1259 unset($USER->enrol['tempguest'][$courseid]);
1260 remove_temp_course_roles($context);
1266 * Store user_enrolments changes and trigger event.
1268 * @param stdClass $instance
1269 * @param int $userid
1270 * @param int $status
1271 * @param int $timestart
1272 * @param int $timeend
1273 * @return void
1275 public function update_user_enrol(stdClass $instance, $userid, $status = NULL, $timestart = NULL, $timeend = NULL) {
1276 global $DB, $USER;
1278 $name = $this->get_name();
1280 if ($instance->enrol !== $name) {
1281 throw new coding_exception('invalid enrol instance!');
1284 if (!$ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid))) {
1285 // weird, user not enrolled
1286 return;
1289 $modified = false;
1290 if (isset($status) and $ue->status != $status) {
1291 $ue->status = $status;
1292 $modified = true;
1294 if (isset($timestart) and $ue->timestart != $timestart) {
1295 $ue->timestart = $timestart;
1296 $modified = true;
1298 if (isset($timeend) and $ue->timeend != $timeend) {
1299 $ue->timeend = $timeend;
1300 $modified = true;
1303 if (!$modified) {
1304 // no change
1305 return;
1308 $ue->modifierid = $USER->id;
1309 $DB->update_record('user_enrolments', $ue);
1310 context_course::instance($instance->courseid)->mark_dirty(); // reset enrol caches
1312 // trigger event
1313 $ue->courseid = $instance->courseid;
1314 $ue->enrol = $instance->name;
1315 events_trigger('user_enrol_modified', $ue);
1319 * Unenrol user from course,
1320 * the last unenrolment removes all remaining roles.
1322 * @param stdClass $instance
1323 * @param int $userid
1324 * @return void
1326 public function unenrol_user(stdClass $instance, $userid) {
1327 global $CFG, $USER, $DB;
1329 $name = $this->get_name();
1330 $courseid = $instance->courseid;
1332 if ($instance->enrol !== $name) {
1333 throw new coding_exception('invalid enrol instance!');
1335 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid, MUST_EXIST);
1337 if (!$ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid))) {
1338 // weird, user not enrolled
1339 return;
1342 role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id, 'component'=>'enrol_'.$name, 'itemid'=>$instance->id));
1343 $DB->delete_records('user_enrolments', array('id'=>$ue->id));
1345 // add extra info and trigger event
1346 $ue->courseid = $courseid;
1347 $ue->enrol = $name;
1349 $sql = "SELECT 'x'
1350 FROM {user_enrolments} ue
1351 JOIN {enrol} e ON (e.id = ue.enrolid)
1352 WHERE ue.userid = :userid AND e.courseid = :courseid";
1353 if ($DB->record_exists_sql($sql, array('userid'=>$userid, 'courseid'=>$courseid))) {
1354 $ue->lastenrol = false;
1355 events_trigger('user_unenrolled', $ue);
1356 // user still has some enrolments, no big cleanup yet
1357 } else {
1358 // the big cleanup IS necessary!
1360 require_once("$CFG->dirroot/group/lib.php");
1361 require_once("$CFG->libdir/gradelib.php");
1363 // remove all remaining roles
1364 role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id), true, false);
1366 //clean up ALL invisible user data from course if this is the last enrolment - groups, grades, etc.
1367 groups_delete_group_members($courseid, $userid);
1369 grade_user_unenrol($courseid, $userid);
1371 $DB->delete_records('user_lastaccess', array('userid'=>$userid, 'courseid'=>$courseid));
1373 $ue->lastenrol = true; // means user not enrolled any more
1374 events_trigger('user_unenrolled', $ue);
1377 // reset all enrol caches
1378 $context->mark_dirty();
1380 // reset current user enrolment caching
1381 if ($userid == $USER->id) {
1382 if (isset($USER->enrol['enrolled'][$courseid])) {
1383 unset($USER->enrol['enrolled'][$courseid]);
1385 if (isset($USER->enrol['tempguest'][$courseid])) {
1386 unset($USER->enrol['tempguest'][$courseid]);
1387 remove_temp_course_roles($context);
1393 * Forces synchronisation of user enrolments.
1395 * This is important especially for external enrol plugins,
1396 * this function is called for all enabled enrol plugins
1397 * right after every user login.
1399 * @param object $user user record
1400 * @return void
1402 public function sync_user_enrolments($user) {
1403 // override if necessary
1407 * Returns link to page which may be used to add new instance of enrolment plugin in course.
1408 * @param int $courseid
1409 * @return moodle_url page url
1411 public function get_newinstance_link($courseid) {
1412 // override for most plugins, check if instance already exists in cases only one instance is supported
1413 return NULL;
1417 * Is it possible to delete enrol instance via standard UI?
1419 * @param object $instance
1420 * @return bool
1422 public function instance_deleteable($instance) {
1423 return true;
1427 * Returns link to manual enrol UI if exists.
1428 * Does the access control tests automatically.
1430 * @param object $instance
1431 * @return moodle_url
1433 public function get_manual_enrol_link($instance) {
1434 return NULL;
1438 * Returns list of unenrol links for all enrol instances in course.
1440 * @param int $instance
1441 * @return moodle_url or NULL if self unenrolment not supported
1443 public function get_unenrolself_link($instance) {
1444 global $USER, $CFG, $DB;
1446 $name = $this->get_name();
1447 if ($instance->enrol !== $name) {
1448 throw new coding_exception('invalid enrol instance!');
1451 if ($instance->courseid == SITEID) {
1452 return NULL;
1455 if (!enrol_is_enabled($name)) {
1456 return NULL;
1459 if ($instance->status != ENROL_INSTANCE_ENABLED) {
1460 return NULL;
1463 if (!file_exists("$CFG->dirroot/enrol/$name/unenrolself.php")) {
1464 return NULL;
1467 $context = get_context_instance(CONTEXT_COURSE, $instance->courseid, MUST_EXIST);
1469 if (!has_capability("enrol/$name:unenrolself", $context)) {
1470 return NULL;
1473 if (!$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$USER->id, 'status'=>ENROL_USER_ACTIVE))) {
1474 return NULL;
1477 return new moodle_url("/enrol/$name/unenrolself.php", array('enrolid'=>$instance->id));;
1481 * Adds enrol instance UI to course edit form
1483 * @param object $instance enrol instance or null if does not exist yet
1484 * @param MoodleQuickForm $mform
1485 * @param object $data
1486 * @param object $context context of existing course or parent category if course does not exist
1487 * @return void
1489 public function course_edit_form($instance, MoodleQuickForm $mform, $data, $context) {
1490 // override - usually at least enable/disable switch, has to add own form header
1494 * Validates course edit form data
1496 * @param object $instance enrol instance or null if does not exist yet
1497 * @param array $data
1498 * @param object $context context of existing course or parent category if course does not exist
1499 * @return array errors array
1501 public function course_edit_validation($instance, array $data, $context) {
1502 return array();
1506 * Called after updating/inserting course.
1508 * @param bool $inserted true if course just inserted
1509 * @param object $course
1510 * @param object $data form data
1511 * @return void
1513 public function course_updated($inserted, $course, $data) {
1514 if ($inserted) {
1515 if ($this->get_config('defaultenrol')) {
1516 $this->add_default_instance($course);
1522 * Add new instance of enrol plugin.
1523 * @param object $course
1524 * @param array instance fields
1525 * @return int id of new instance, null if can not be created
1527 public function add_instance($course, array $fields = NULL) {
1528 global $DB;
1530 if ($course->id == SITEID) {
1531 throw new coding_exception('Invalid request to add enrol instance to frontpage.');
1534 $instance = new stdClass();
1535 $instance->enrol = $this->get_name();
1536 $instance->status = ENROL_INSTANCE_ENABLED;
1537 $instance->courseid = $course->id;
1538 $instance->enrolstartdate = 0;
1539 $instance->enrolenddate = 0;
1540 $instance->timemodified = time();
1541 $instance->timecreated = $instance->timemodified;
1542 $instance->sortorder = $DB->get_field('enrol', 'COALESCE(MAX(sortorder), -1) + 1', array('courseid'=>$course->id));
1544 $fields = (array)$fields;
1545 unset($fields['enrol']);
1546 unset($fields['courseid']);
1547 unset($fields['sortorder']);
1548 foreach($fields as $field=>$value) {
1549 $instance->$field = $value;
1552 return $DB->insert_record('enrol', $instance);
1556 * Add new instance of enrol plugin with default settings,
1557 * called when adding new instance manually or when adding new course.
1559 * Not all plugins support this.
1561 * @param object $course
1562 * @return int id of new instance or null if no default supported
1564 public function add_default_instance($course) {
1565 return null;
1569 * Update instance status
1571 * Override when plugin needs to do some action when enabled or disabled.
1573 * @param stdClass $instance
1574 * @param int $newstatus ENROL_INSTANCE_ENABLED, ENROL_INSTANCE_DISABLED
1575 * @return void
1577 public function update_status($instance, $newstatus) {
1578 global $DB;
1580 $instance->status = $newstatus;
1581 $DB->update_record('enrol', $instance);
1583 // invalidate all enrol caches
1584 $context = context_course::instance($instance->courseid);
1585 $context->mark_dirty();
1589 * Delete course enrol plugin instance, unenrol all users.
1590 * @param object $instance
1591 * @return void
1593 public function delete_instance($instance) {
1594 global $DB;
1596 $name = $this->get_name();
1597 if ($instance->enrol !== $name) {
1598 throw new coding_exception('invalid enrol instance!');
1601 //first unenrol all users
1602 $participants = $DB->get_recordset('user_enrolments', array('enrolid'=>$instance->id));
1603 foreach ($participants as $participant) {
1604 $this->unenrol_user($instance, $participant->userid);
1606 $participants->close();
1608 // now clean up all remainders that were not removed correctly
1609 $DB->delete_records('role_assignments', array('itemid'=>$instance->id, 'component'=>$name));
1610 $DB->delete_records('user_enrolments', array('enrolid'=>$instance->id));
1612 // finally drop the enrol row
1613 $DB->delete_records('enrol', array('id'=>$instance->id));
1615 // invalidate all enrol caches
1616 $context = context_course::instance($instance->courseid);
1617 $context->mark_dirty();
1621 * Creates course enrol form, checks if form submitted
1622 * and enrols user if necessary. It can also redirect.
1624 * @param stdClass $instance
1625 * @return string html text, usually a form in a text box
1627 public function enrol_page_hook(stdClass $instance) {
1628 return null;
1632 * Adds navigation links into course admin block.
1634 * By defaults looks for manage links only.
1636 * @param navigation_node $instancesnode
1637 * @param stdClass $instance
1638 * @return void
1640 public function add_course_navigation($instancesnode, stdClass $instance) {
1641 // usually adds manage users
1645 * Returns edit icons for the page with list of instances
1646 * @param stdClass $instance
1647 * @return array
1649 public function get_action_icons(stdClass $instance) {
1650 return array();
1654 * Reads version.php and determines if it is necessary
1655 * to execute the cron job now.
1656 * @return bool
1658 public function is_cron_required() {
1659 global $CFG;
1661 $name = $this->get_name();
1662 $versionfile = "$CFG->dirroot/enrol/$name/version.php";
1663 $plugin = new stdClass();
1664 include($versionfile);
1665 if (empty($plugin->cron)) {
1666 return false;
1668 $lastexecuted = $this->get_config('lastcron', 0);
1669 if ($lastexecuted + $plugin->cron < time()) {
1670 return true;
1671 } else {
1672 return false;
1677 * Called for all enabled enrol plugins that returned true from is_cron_required().
1678 * @return void
1680 public function cron() {
1684 * Called when user is about to be deleted
1685 * @param object $user
1686 * @return void
1688 public function user_delete($user) {
1689 global $DB;
1691 $sql = "SELECT e.*
1692 FROM {enrol} e
1693 JOIN {user_enrolments} ue ON (ue.enrolid = e.id)
1694 WHERE e.enrol = :name AND ue.userid = :userid";
1695 $params = array('name'=>$this->get_name(), 'userid'=>$user->id);
1697 $rs = $DB->get_recordset_sql($sql, $params);
1698 foreach($rs as $instance) {
1699 $this->unenrol_user($instance, $user->id);
1701 $rs->close();
1705 * Returns an enrol_user_button that takes the user to a page where they are able to
1706 * enrol users into the managers course through this plugin.
1708 * Optional: If the plugin supports manual enrolments it can choose to override this
1709 * otherwise it shouldn't
1711 * @param course_enrolment_manager $manager
1712 * @return enrol_user_button|false
1714 public function get_manual_enrol_button(course_enrolment_manager $manager) {
1715 return false;
1719 * Gets an array of the user enrolment actions
1721 * @param course_enrolment_manager $manager
1722 * @param stdClass $ue
1723 * @return array An array of user_enrolment_actions
1725 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
1726 return array();
1730 * Returns true if the plugin has one or more bulk operations that can be performed on
1731 * user enrolments.
1733 * @return bool
1735 public function has_bulk_operations() {
1736 return false;
1740 * Return an array of enrol_bulk_enrolment_operation objects that define
1741 * the bulk actions that can be performed on user enrolments by the plugin.
1743 * @return array
1745 public function get_bulk_operations() {
1746 return array();