Merge branch 'MDL-67410-37' of https://github.com/felicemcc/moodle into MOODLE_37_STABLE
[moodle.git] / lib / enrollib.php
blob5b668941c479325fc1b6cf57b4502e84cc52d3aa
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 /** @deprecated since 2.4 not used any more, migrate plugin to new restore methods */
55 define('ENROL_RESTORE_TYPE', 'enrolrestore');
57 /**
58 * When user disappears from external source, user enrolment is suspended, roles are kept as is.
59 * In some cases user needs a role with some capability to be visible in UI - suc has in gradebook,
60 * assignments, etc.
62 define('ENROL_EXT_REMOVED_SUSPEND', 2);
64 /**
65 * When user disappears from external source, the enrolment is suspended and roles assigned
66 * by enrol instance are removed. Please note that user may "disappear" from gradebook and other areas.
67 * */
68 define('ENROL_EXT_REMOVED_SUSPENDNOROLES', 3);
70 /**
71 * Do not send email.
73 define('ENROL_DO_NOT_SEND_EMAIL', 0);
75 /**
76 * Send email from course contact.
78 define('ENROL_SEND_EMAIL_FROM_COURSE_CONTACT', 1);
80 /**
81 * Send email from enrolment key holder.
83 define('ENROL_SEND_EMAIL_FROM_KEY_HOLDER', 2);
85 /**
86 * Send email from no reply address.
88 define('ENROL_SEND_EMAIL_FROM_NOREPLY', 3);
90 /** Edit enrolment action. */
91 define('ENROL_ACTION_EDIT', 'editenrolment');
93 /** Unenrol action. */
94 define('ENROL_ACTION_UNENROL', 'unenrol');
96 /**
97 * Returns instances of enrol plugins
98 * @param bool $enabled return enabled only
99 * @return array of enrol plugins name=>instance
101 function enrol_get_plugins($enabled) {
102 global $CFG;
104 $result = array();
106 if ($enabled) {
107 // sorted by enabled plugin order
108 $enabled = explode(',', $CFG->enrol_plugins_enabled);
109 $plugins = array();
110 foreach ($enabled as $plugin) {
111 $plugins[$plugin] = "$CFG->dirroot/enrol/$plugin";
113 } else {
114 // sorted alphabetically
115 $plugins = core_component::get_plugin_list('enrol');
116 ksort($plugins);
119 foreach ($plugins as $plugin=>$location) {
120 $class = "enrol_{$plugin}_plugin";
121 if (!class_exists($class)) {
122 if (!file_exists("$location/lib.php")) {
123 continue;
125 include_once("$location/lib.php");
126 if (!class_exists($class)) {
127 continue;
131 $result[$plugin] = new $class();
134 return $result;
138 * Returns instance of enrol plugin
139 * @param string $name name of enrol plugin ('manual', 'guest', ...)
140 * @return enrol_plugin
142 function enrol_get_plugin($name) {
143 global $CFG;
145 $name = clean_param($name, PARAM_PLUGIN);
147 if (empty($name)) {
148 // ignore malformed or missing plugin names completely
149 return null;
152 $location = "$CFG->dirroot/enrol/$name";
154 $class = "enrol_{$name}_plugin";
155 if (!class_exists($class)) {
156 if (!file_exists("$location/lib.php")) {
157 return null;
159 include_once("$location/lib.php");
160 if (!class_exists($class)) {
161 return null;
165 return new $class();
169 * Returns enrolment instances in given course.
170 * @param int $courseid
171 * @param bool $enabled
172 * @return array of enrol instances
174 function enrol_get_instances($courseid, $enabled) {
175 global $DB, $CFG;
177 if (!$enabled) {
178 return $DB->get_records('enrol', array('courseid'=>$courseid), 'sortorder,id');
181 $result = $DB->get_records('enrol', array('courseid'=>$courseid, 'status'=>ENROL_INSTANCE_ENABLED), 'sortorder,id');
183 $enabled = explode(',', $CFG->enrol_plugins_enabled);
184 foreach ($result as $key=>$instance) {
185 if (!in_array($instance->enrol, $enabled)) {
186 unset($result[$key]);
187 continue;
189 if (!file_exists("$CFG->dirroot/enrol/$instance->enrol/lib.php")) {
190 // broken plugin
191 unset($result[$key]);
192 continue;
196 return $result;
200 * Checks if a given plugin is in the list of enabled enrolment plugins.
202 * @param string $enrol Enrolment plugin name
203 * @return boolean Whether the plugin is enabled
205 function enrol_is_enabled($enrol) {
206 global $CFG;
208 if (empty($CFG->enrol_plugins_enabled)) {
209 return false;
211 return in_array($enrol, explode(',', $CFG->enrol_plugins_enabled));
215 * Check all the login enrolment information for the given user object
216 * by querying the enrolment plugins
218 * This function may be very slow, use only once after log-in or login-as.
220 * @param stdClass $user
221 * @return void
223 function enrol_check_plugins($user) {
224 global $CFG;
226 if (empty($user->id) or isguestuser($user)) {
227 // shortcut - there is no enrolment work for guests and not-logged-in users
228 return;
231 // originally there was a broken admin test, but accidentally it was non-functional in 2.2,
232 // which proved it was actually not necessary.
234 static $inprogress = array(); // To prevent this function being called more than once in an invocation
236 if (!empty($inprogress[$user->id])) {
237 return;
240 $inprogress[$user->id] = true; // Set the flag
242 $enabled = enrol_get_plugins(true);
244 foreach($enabled as $enrol) {
245 $enrol->sync_user_enrolments($user);
248 unset($inprogress[$user->id]); // Unset the flag
252 * Do these two students share any course?
254 * The courses has to be visible and enrolments has to be active,
255 * timestart and timeend restrictions are ignored.
257 * This function calls {@see enrol_get_shared_courses()} setting checkexistsonly
258 * to true.
260 * @param stdClass|int $user1
261 * @param stdClass|int $user2
262 * @return bool
264 function enrol_sharing_course($user1, $user2) {
265 return enrol_get_shared_courses($user1, $user2, false, true);
269 * Returns any courses shared by the two users
271 * The courses has to be visible and enrolments has to be active,
272 * timestart and timeend restrictions are ignored.
274 * @global moodle_database $DB
275 * @param stdClass|int $user1
276 * @param stdClass|int $user2
277 * @param bool $preloadcontexts If set to true contexts for the returned courses
278 * will be preloaded.
279 * @param bool $checkexistsonly If set to true then this function will return true
280 * if the users share any courses and false if not.
281 * @return array|bool An array of courses that both users are enrolled in OR if
282 * $checkexistsonly set returns true if the users share any courses
283 * and false if not.
285 function enrol_get_shared_courses($user1, $user2, $preloadcontexts = false, $checkexistsonly = false) {
286 global $DB, $CFG;
288 $user1 = isset($user1->id) ? $user1->id : $user1;
289 $user2 = isset($user2->id) ? $user2->id : $user2;
291 if (empty($user1) or empty($user2)) {
292 return false;
295 if (!$plugins = explode(',', $CFG->enrol_plugins_enabled)) {
296 return false;
299 list($plugins1, $params1) = $DB->get_in_or_equal($plugins, SQL_PARAMS_NAMED, 'ee1');
300 list($plugins2, $params2) = $DB->get_in_or_equal($plugins, SQL_PARAMS_NAMED, 'ee2');
301 $params = array_merge($params1, $params2);
302 $params['enabled1'] = ENROL_INSTANCE_ENABLED;
303 $params['enabled2'] = ENROL_INSTANCE_ENABLED;
304 $params['active1'] = ENROL_USER_ACTIVE;
305 $params['active2'] = ENROL_USER_ACTIVE;
306 $params['user1'] = $user1;
307 $params['user2'] = $user2;
309 $ctxselect = '';
310 $ctxjoin = '';
311 if ($preloadcontexts) {
312 $ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
313 $ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
314 $params['contextlevel'] = CONTEXT_COURSE;
317 $sql = "SELECT c.* $ctxselect
318 FROM {course} c
319 JOIN (
320 SELECT DISTINCT c.id
321 FROM {course} c
322 JOIN {enrol} e1 ON (c.id = e1.courseid AND e1.status = :enabled1 AND e1.enrol $plugins1)
323 JOIN {user_enrolments} ue1 ON (ue1.enrolid = e1.id AND ue1.status = :active1 AND ue1.userid = :user1)
324 JOIN {enrol} e2 ON (c.id = e2.courseid AND e2.status = :enabled2 AND e2.enrol $plugins2)
325 JOIN {user_enrolments} ue2 ON (ue2.enrolid = e2.id AND ue2.status = :active2 AND ue2.userid = :user2)
326 WHERE c.visible = 1
327 ) ec ON ec.id = c.id
328 $ctxjoin";
330 if ($checkexistsonly) {
331 return $DB->record_exists_sql($sql, $params);
332 } else {
333 $courses = $DB->get_records_sql($sql, $params);
334 if ($preloadcontexts) {
335 array_map('context_helper::preload_from_record', $courses);
337 return $courses;
342 * This function adds necessary enrol plugins UI into the course edit form.
344 * @param MoodleQuickForm $mform
345 * @param object $data course edit form data
346 * @param object $context context of existing course or parent category if course does not exist
347 * @return void
349 function enrol_course_edit_form(MoodleQuickForm $mform, $data, $context) {
350 $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 $plugin->course_edit_form($instance, $mform, $data, $context);
360 } else {
361 foreach ($plugins as $plugin) {
362 $plugin->course_edit_form(NULL, $mform, $data, $context);
368 * Validate course edit form data
370 * @param array $data raw form data
371 * @param object $context context of existing course or parent category if course does not exist
372 * @return array errors array
374 function enrol_course_edit_validation(array $data, $context) {
375 $errors = array();
376 $plugins = enrol_get_plugins(true);
378 if (!empty($data['id'])) {
379 $instances = enrol_get_instances($data['id'], false);
380 foreach ($instances as $instance) {
381 if (!isset($plugins[$instance->enrol])) {
382 continue;
384 $plugin = $plugins[$instance->enrol];
385 $errors = array_merge($errors, $plugin->course_edit_validation($instance, $data, $context));
387 } else {
388 foreach ($plugins as $plugin) {
389 $errors = array_merge($errors, $plugin->course_edit_validation(NULL, $data, $context));
393 return $errors;
397 * Update enrol instances after course edit form submission
398 * @param bool $inserted true means new course added, false course already existed
399 * @param object $course
400 * @param object $data form data
401 * @return void
403 function enrol_course_updated($inserted, $course, $data) {
404 global $DB, $CFG;
406 $plugins = enrol_get_plugins(true);
408 foreach ($plugins as $plugin) {
409 $plugin->course_updated($inserted, $course, $data);
414 * Add navigation nodes
415 * @param navigation_node $coursenode
416 * @param object $course
417 * @return void
419 function enrol_add_course_navigation(navigation_node $coursenode, $course) {
420 global $CFG;
422 $coursecontext = context_course::instance($course->id);
424 $instances = enrol_get_instances($course->id, true);
425 $plugins = enrol_get_plugins(true);
427 // we do not want to break all course pages if there is some borked enrol plugin, right?
428 foreach ($instances as $k=>$instance) {
429 if (!isset($plugins[$instance->enrol])) {
430 unset($instances[$k]);
434 $usersnode = $coursenode->add(get_string('users'), null, navigation_node::TYPE_CONTAINER, null, 'users');
436 if ($course->id != SITEID) {
437 // list all participants - allows assigning roles, groups, etc.
438 if (has_capability('moodle/course:enrolreview', $coursecontext)) {
439 $url = new moodle_url('/user/index.php', array('id'=>$course->id));
440 $usersnode->add(get_string('enrolledusers', 'enrol'), $url, navigation_node::TYPE_SETTING, null, 'review', new pix_icon('i/enrolusers', ''));
443 // manage enrol plugin instances
444 if (has_capability('moodle/course:enrolconfig', $coursecontext) or has_capability('moodle/course:enrolreview', $coursecontext)) {
445 $url = new moodle_url('/enrol/instances.php', array('id'=>$course->id));
446 } else {
447 $url = NULL;
449 $instancesnode = $usersnode->add(get_string('enrolmentinstances', 'enrol'), $url, navigation_node::TYPE_SETTING, null, 'manageinstances');
451 // each instance decides how to configure itself or how many other nav items are exposed
452 foreach ($instances as $instance) {
453 if (!isset($plugins[$instance->enrol])) {
454 continue;
456 $plugins[$instance->enrol]->add_course_navigation($instancesnode, $instance);
459 if (!$url) {
460 $instancesnode->trim_if_empty();
464 // Manage groups in this course or even frontpage
465 if (($course->groupmode || !$course->groupmodeforce) && has_capability('moodle/course:managegroups', $coursecontext)) {
466 $url = new moodle_url('/group/index.php', array('id'=>$course->id));
467 $usersnode->add(get_string('groups'), $url, navigation_node::TYPE_SETTING, null, 'groups', new pix_icon('i/group', ''));
470 if (has_any_capability(array( 'moodle/role:assign', 'moodle/role:safeoverride','moodle/role:override', 'moodle/role:review'), $coursecontext)) {
471 // Override roles
472 if (has_capability('moodle/role:review', $coursecontext)) {
473 $url = new moodle_url('/admin/roles/permissions.php', array('contextid'=>$coursecontext->id));
474 } else {
475 $url = NULL;
477 $permissionsnode = $usersnode->add(get_string('permissions', 'role'), $url, navigation_node::TYPE_SETTING, null, 'override');
479 // Add assign or override roles if allowed
480 if ($course->id == SITEID or (!empty($CFG->adminsassignrolesincourse) and is_siteadmin())) {
481 if (has_capability('moodle/role:assign', $coursecontext)) {
482 $url = new moodle_url('/admin/roles/assign.php', array('contextid'=>$coursecontext->id));
483 $permissionsnode->add(get_string('assignedroles', 'role'), $url, navigation_node::TYPE_SETTING, null, 'roles', new pix_icon('i/assignroles', ''));
486 // Check role permissions
487 if (has_any_capability(array('moodle/role:assign', 'moodle/role:safeoverride', 'moodle/role:override'), $coursecontext)) {
488 $url = new moodle_url('/admin/roles/check.php', array('contextid'=>$coursecontext->id));
489 $permissionsnode->add(get_string('checkpermissions', 'role'), $url, navigation_node::TYPE_SETTING, null, 'permissions', new pix_icon('i/checkpermissions', ''));
493 // Deal somehow with users that are not enrolled but still got a role somehow
494 if ($course->id != SITEID) {
495 //TODO, create some new UI for role assignments at course level
496 if (has_capability('moodle/course:reviewotherusers', $coursecontext)) {
497 $url = new moodle_url('/enrol/otherusers.php', array('id'=>$course->id));
498 $usersnode->add(get_string('notenrolledusers', 'enrol'), $url, navigation_node::TYPE_SETTING, null, 'otherusers', new pix_icon('i/assignroles', ''));
502 // just in case nothing was actually added
503 $usersnode->trim_if_empty();
505 if ($course->id != SITEID) {
506 if (isguestuser() or !isloggedin()) {
507 // guest account can not be enrolled - no links for them
508 } else if (is_enrolled($coursecontext)) {
509 // unenrol link if possible
510 foreach ($instances as $instance) {
511 if (!isset($plugins[$instance->enrol])) {
512 continue;
514 $plugin = $plugins[$instance->enrol];
515 if ($unenrollink = $plugin->get_unenrolself_link($instance)) {
516 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
517 $coursenode->add(get_string('unenrolme', 'core_enrol', $shortname), $unenrollink, navigation_node::TYPE_SETTING, null, 'unenrolself', new pix_icon('i/user', ''));
518 break;
519 //TODO. deal with multiple unenrol links - not likely case, but still...
522 } else {
523 // enrol link if possible
524 if (is_viewing($coursecontext)) {
525 // better not show any enrol link, this is intended for managers and inspectors
526 } else {
527 foreach ($instances as $instance) {
528 if (!isset($plugins[$instance->enrol])) {
529 continue;
531 $plugin = $plugins[$instance->enrol];
532 if ($plugin->show_enrolme_link($instance)) {
533 $url = new moodle_url('/enrol/index.php', array('id'=>$course->id));
534 $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
535 $coursenode->add(get_string('enrolme', 'core_enrol', $shortname), $url, navigation_node::TYPE_SETTING, null, 'enrolself', new pix_icon('i/user', ''));
536 break;
545 * Returns list of courses current $USER is enrolled in and can access
547 * The $fields param is a list of field names to ADD so name just the fields you really need,
548 * which will be added and uniq'd.
550 * If $allaccessible is true, this will additionally return courses that the current user is not
551 * enrolled in, but can access because they are open to the user for other reasons (course view
552 * permission, currently viewing course as a guest, or course allows guest access without
553 * password).
555 * @param string|array $fields Extra fields to be returned (array or comma-separated list).
556 * @param string|null $sort Comma separated list of fields to sort by, defaults to respecting navsortmycoursessort.
557 * Allowed prefixes for sort fields are: "ul" for the user_lastaccess table, "c" for the courses table,
558 * "ue" for the user_enrolments table.
559 * @param int $limit max number of courses
560 * @param array $courseids the list of course ids to filter by
561 * @param bool $allaccessible Include courses user is not enrolled in, but can access
562 * @param int $offset Offset the result set by this number
563 * @param array $excludecourses IDs of hidden courses to exclude from search
564 * @return array
566 function enrol_get_my_courses($fields = null, $sort = null, $limit = 0, $courseids = [], $allaccessible = false,
567 $offset = 0, $excludecourses = []) {
568 global $DB, $USER, $CFG;
570 if ($sort === null) {
571 if (empty($CFG->navsortmycoursessort)) {
572 $sort = 'visible DESC, sortorder ASC';
573 } else {
574 $sort = 'visible DESC, '.$CFG->navsortmycoursessort.' ASC';
578 // Guest account does not have any enrolled courses.
579 if (!$allaccessible && (isguestuser() or !isloggedin())) {
580 return array();
583 $basefields = array('id', 'category', 'sortorder',
584 'shortname', 'fullname', 'idnumber',
585 'startdate', 'visible',
586 'groupmode', 'groupmodeforce', 'cacherev');
588 if (empty($fields)) {
589 $fields = $basefields;
590 } else if (is_string($fields)) {
591 // turn the fields from a string to an array
592 $fields = explode(',', $fields);
593 $fields = array_map('trim', $fields);
594 $fields = array_unique(array_merge($basefields, $fields));
595 } else if (is_array($fields)) {
596 $fields = array_unique(array_merge($basefields, $fields));
597 } else {
598 throw new coding_exception('Invalid $fields parameter in enrol_get_my_courses()');
600 if (in_array('*', $fields)) {
601 $fields = array('*');
604 $orderby = "";
605 $sort = trim($sort);
606 $sorttimeaccess = false;
607 $allowedsortprefixes = array('c', 'ul', 'ue');
608 if (!empty($sort)) {
609 $rawsorts = explode(',', $sort);
610 $sorts = array();
611 foreach ($rawsorts as $rawsort) {
612 $rawsort = trim($rawsort);
613 if (preg_match('/^ul\.(\S*)\s(asc|desc)/i', $rawsort, $matches)) {
614 if (strcasecmp($matches[2], 'asc') == 0) {
615 $sorts[] = 'COALESCE(ul.' . $matches[1] . ', 0) ASC';
616 } else {
617 $sorts[] = 'COALESCE(ul.' . $matches[1] . ', 0) DESC';
619 $sorttimeaccess = true;
620 } else if (strpos($rawsort, '.') !== false) {
621 $prefix = explode('.', $rawsort);
622 if (in_array($prefix[0], $allowedsortprefixes)) {
623 $sorts[] = trim($rawsort);
624 } else {
625 throw new coding_exception('Invalid $sort parameter in enrol_get_my_courses()');
627 } else {
628 $sorts[] = 'c.'.trim($rawsort);
631 $sort = implode(',', $sorts);
632 $orderby = "ORDER BY $sort";
635 $wheres = array("c.id <> :siteid");
636 $params = array('siteid'=>SITEID);
638 if (isset($USER->loginascontext) and $USER->loginascontext->contextlevel == CONTEXT_COURSE) {
639 // list _only_ this course - anything else is asking for trouble...
640 $wheres[] = "courseid = :loginas";
641 $params['loginas'] = $USER->loginascontext->instanceid;
644 $coursefields = 'c.' .join(',c.', $fields);
645 $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
646 $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
647 $params['contextlevel'] = CONTEXT_COURSE;
648 $wheres = implode(" AND ", $wheres);
650 $timeaccessselect = "";
651 $timeaccessjoin = "";
653 if (!empty($courseids)) {
654 list($courseidssql, $courseidsparams) = $DB->get_in_or_equal($courseids, SQL_PARAMS_NAMED);
655 $wheres = sprintf("%s AND c.id %s", $wheres, $courseidssql);
656 $params = array_merge($params, $courseidsparams);
659 if (!empty($excludecourses)) {
660 list($courseidssql, $courseidsparams) = $DB->get_in_or_equal($excludecourses, SQL_PARAMS_NAMED, 'param', false);
661 $wheres = sprintf("%s AND c.id %s", $wheres, $courseidssql);
662 $params = array_merge($params, $courseidsparams);
665 $courseidsql = "";
666 // Logged-in, non-guest users get their enrolled courses.
667 if (!isguestuser() && isloggedin()) {
668 $courseidsql .= "
669 SELECT DISTINCT e.courseid
670 FROM {enrol} e
671 JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = :userid1)
672 WHERE ue.status = :active AND e.status = :enabled AND ue.timestart < :now1
673 AND (ue.timeend = 0 OR ue.timeend > :now2)";
674 $params['userid1'] = $USER->id;
675 $params['active'] = ENROL_USER_ACTIVE;
676 $params['enabled'] = ENROL_INSTANCE_ENABLED;
677 $params['now1'] = round(time(), -2); // Improves db caching.
678 $params['now2'] = $params['now1'];
680 if ($sorttimeaccess) {
681 $params['userid2'] = $USER->id;
682 $timeaccessselect = ', ul.timeaccess as lastaccessed';
683 $timeaccessjoin = "LEFT JOIN {user_lastaccess} ul ON (ul.courseid = c.id AND ul.userid = :userid2)";
687 // When including non-enrolled but accessible courses...
688 if ($allaccessible) {
689 if (is_siteadmin()) {
690 // Site admins can access all courses.
691 $courseidsql = "SELECT DISTINCT c2.id AS courseid FROM {course} c2";
692 } else {
693 // If we used the enrolment as well, then this will be UNIONed.
694 if ($courseidsql) {
695 $courseidsql .= " UNION ";
698 // Include courses with guest access and no password.
699 $courseidsql .= "
700 SELECT DISTINCT e.courseid
701 FROM {enrol} e
702 WHERE e.enrol = 'guest' AND e.password = :emptypass AND e.status = :enabled2";
703 $params['emptypass'] = '';
704 $params['enabled2'] = ENROL_INSTANCE_ENABLED;
706 // Include courses where the current user is currently using guest access (may include
707 // those which require a password).
708 $courseids = [];
709 $accessdata = get_user_accessdata($USER->id);
710 foreach ($accessdata['ra'] as $contextpath => $roles) {
711 if (array_key_exists($CFG->guestroleid, $roles)) {
712 // Work out the course id from context path.
713 $context = context::instance_by_id(preg_replace('~^.*/~', '', $contextpath));
714 if ($context instanceof context_course) {
715 $courseids[$context->instanceid] = true;
720 // Include courses where the current user has moodle/course:view capability.
721 $courses = get_user_capability_course('moodle/course:view', null, false);
722 if (!$courses) {
723 $courses = [];
725 foreach ($courses as $course) {
726 $courseids[$course->id] = true;
729 // If there are any in either category, list them individually.
730 if ($courseids) {
731 list ($allowedsql, $allowedparams) = $DB->get_in_or_equal(
732 array_keys($courseids), SQL_PARAMS_NAMED);
733 $courseidsql .= "
734 UNION
735 SELECT DISTINCT c3.id AS courseid
736 FROM {course} c3
737 WHERE c3.id $allowedsql";
738 $params = array_merge($params, $allowedparams);
743 // Note: we can not use DISTINCT + text fields due to Oracle and MS limitations, that is why
744 // we have the subselect there.
745 $sql = "SELECT $coursefields $ccselect $timeaccessselect
746 FROM {course} c
747 JOIN ($courseidsql) en ON (en.courseid = c.id)
748 $timeaccessjoin
749 $ccjoin
750 WHERE $wheres
751 $orderby";
753 $courses = $DB->get_records_sql($sql, $params, $offset, $limit);
755 // preload contexts and check visibility
756 foreach ($courses as $id=>$course) {
757 context_helper::preload_from_record($course);
758 if (!$course->visible) {
759 if (!$context = context_course::instance($id, IGNORE_MISSING)) {
760 unset($courses[$id]);
761 continue;
763 if (!has_capability('moodle/course:viewhiddencourses', $context)) {
764 unset($courses[$id]);
765 continue;
768 $courses[$id] = $course;
771 //wow! Is that really all? :-D
773 return $courses;
777 * Returns course enrolment information icons.
779 * @param object $course
780 * @param array $instances enrol instances of this course, improves performance
781 * @return array of pix_icon
783 function enrol_get_course_info_icons($course, array $instances = NULL) {
784 $icons = array();
785 if (is_null($instances)) {
786 $instances = enrol_get_instances($course->id, true);
788 $plugins = enrol_get_plugins(true);
789 foreach ($plugins as $name => $plugin) {
790 $pis = array();
791 foreach ($instances as $instance) {
792 if ($instance->status != ENROL_INSTANCE_ENABLED or $instance->courseid != $course->id) {
793 debugging('Invalid instances parameter submitted in enrol_get_info_icons()');
794 continue;
796 if ($instance->enrol == $name) {
797 $pis[$instance->id] = $instance;
800 if ($pis) {
801 $icons = array_merge($icons, $plugin->get_info_icons($pis));
804 return $icons;
808 * Returns course enrolment detailed information.
810 * @param object $course
811 * @return array of html fragments - can be used to construct lists
813 function enrol_get_course_description_texts($course) {
814 $lines = array();
815 $instances = enrol_get_instances($course->id, true);
816 $plugins = enrol_get_plugins(true);
817 foreach ($instances as $instance) {
818 if (!isset($plugins[$instance->enrol])) {
819 //weird
820 continue;
822 $plugin = $plugins[$instance->enrol];
823 $text = $plugin->get_description_text($instance);
824 if ($text !== NULL) {
825 $lines[] = $text;
828 return $lines;
832 * Returns list of courses user is enrolled into.
834 * Note: Use {@link enrol_get_all_users_courses()} if you need the list without any capability checks.
836 * The $fields param is a list of field names to ADD so name just the fields you really need,
837 * which will be added and uniq'd.
839 * @param int $userid User whose courses are returned, defaults to the current user.
840 * @param bool $onlyactive Return only active enrolments in courses user may see.
841 * @param string|array $fields Extra fields to be returned (array or comma-separated list).
842 * @param string|null $sort Comma separated list of fields to sort by, defaults to respecting navsortmycoursessort.
843 * @return array
845 function enrol_get_users_courses($userid, $onlyactive = false, $fields = null, $sort = null) {
846 global $DB;
848 $courses = enrol_get_all_users_courses($userid, $onlyactive, $fields, $sort);
850 // preload contexts and check visibility
851 if ($onlyactive) {
852 foreach ($courses as $id=>$course) {
853 context_helper::preload_from_record($course);
854 if (!$course->visible) {
855 if (!$context = context_course::instance($id)) {
856 unset($courses[$id]);
857 continue;
859 if (!has_capability('moodle/course:viewhiddencourses', $context, $userid)) {
860 unset($courses[$id]);
861 continue;
867 return $courses;
871 * Returns list of roles per users into course.
873 * @param int $courseid Course id.
874 * @return array Array[$userid][$roleid] = role_assignment.
876 function enrol_get_course_users_roles(int $courseid) : array {
877 global $DB;
879 $context = context_course::instance($courseid);
881 $roles = array();
883 $records = $DB->get_recordset('role_assignments', array('contextid' => $context->id));
884 foreach ($records as $record) {
885 if (isset($roles[$record->userid]) === false) {
886 $roles[$record->userid] = array();
888 $roles[$record->userid][$record->roleid] = $record;
890 $records->close();
892 return $roles;
896 * Can user access at least one enrolled course?
898 * Cheat if necessary, but find out as fast as possible!
900 * @param int|stdClass $user null means use current user
901 * @return bool
903 function enrol_user_sees_own_courses($user = null) {
904 global $USER;
906 if ($user === null) {
907 $user = $USER;
909 $userid = is_object($user) ? $user->id : $user;
911 // Guest account does not have any courses
912 if (isguestuser($userid) or empty($userid)) {
913 return false;
916 // Let's cheat here if this is the current user,
917 // if user accessed any course recently, then most probably
918 // we do not need to query the database at all.
919 if ($USER->id == $userid) {
920 if (!empty($USER->enrol['enrolled'])) {
921 foreach ($USER->enrol['enrolled'] as $until) {
922 if ($until > time()) {
923 return true;
929 // Now the slow way.
930 $courses = enrol_get_all_users_courses($userid, true);
931 foreach($courses as $course) {
932 if ($course->visible) {
933 return true;
935 context_helper::preload_from_record($course);
936 $context = context_course::instance($course->id);
937 if (has_capability('moodle/course:viewhiddencourses', $context, $user)) {
938 return true;
942 return false;
946 * Returns list of courses user is enrolled into without performing any capability checks.
948 * The $fields param is a list of field names to ADD so name just the fields you really need,
949 * which will be added and uniq'd.
951 * @param int $userid User whose courses are returned, defaults to the current user.
952 * @param bool $onlyactive Return only active enrolments in courses user may see.
953 * @param string|array $fields Extra fields to be returned (array or comma-separated list).
954 * @param string|null $sort Comma separated list of fields to sort by, defaults to respecting navsortmycoursessort.
955 * @return array
957 function enrol_get_all_users_courses($userid, $onlyactive = false, $fields = null, $sort = null) {
958 global $CFG, $DB;
960 if ($sort === null) {
961 if (empty($CFG->navsortmycoursessort)) {
962 $sort = 'visible DESC, sortorder ASC';
963 } else {
964 $sort = 'visible DESC, '.$CFG->navsortmycoursessort.' ASC';
968 // Guest account does not have any courses
969 if (isguestuser($userid) or empty($userid)) {
970 return(array());
973 $basefields = array('id', 'category', 'sortorder',
974 'shortname', 'fullname', 'idnumber',
975 'startdate', 'visible',
976 'defaultgroupingid',
977 'groupmode', 'groupmodeforce');
979 if (empty($fields)) {
980 $fields = $basefields;
981 } else if (is_string($fields)) {
982 // turn the fields from a string to an array
983 $fields = explode(',', $fields);
984 $fields = array_map('trim', $fields);
985 $fields = array_unique(array_merge($basefields, $fields));
986 } else if (is_array($fields)) {
987 $fields = array_unique(array_merge($basefields, $fields));
988 } else {
989 throw new coding_exception('Invalid $fields parameter in enrol_get_all_users_courses()');
991 if (in_array('*', $fields)) {
992 $fields = array('*');
995 $orderby = "";
996 $sort = trim($sort);
997 if (!empty($sort)) {
998 $rawsorts = explode(',', $sort);
999 $sorts = array();
1000 foreach ($rawsorts as $rawsort) {
1001 $rawsort = trim($rawsort);
1002 if (strpos($rawsort, 'c.') === 0) {
1003 $rawsort = substr($rawsort, 2);
1005 $sorts[] = trim($rawsort);
1007 $sort = 'c.'.implode(',c.', $sorts);
1008 $orderby = "ORDER BY $sort";
1011 $params = array('siteid'=>SITEID);
1013 if ($onlyactive) {
1014 $subwhere = "WHERE ue.status = :active AND e.status = :enabled AND ue.timestart < :now1 AND (ue.timeend = 0 OR ue.timeend > :now2)";
1015 $params['now1'] = round(time(), -2); // improves db caching
1016 $params['now2'] = $params['now1'];
1017 $params['active'] = ENROL_USER_ACTIVE;
1018 $params['enabled'] = ENROL_INSTANCE_ENABLED;
1019 } else {
1020 $subwhere = "";
1023 $coursefields = 'c.' .join(',c.', $fields);
1024 $ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
1025 $ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
1026 $params['contextlevel'] = CONTEXT_COURSE;
1028 //note: we can not use DISTINCT + text fields due to Oracle and MS limitations, that is why we have the subselect there
1029 $sql = "SELECT $coursefields $ccselect
1030 FROM {course} c
1031 JOIN (SELECT DISTINCT e.courseid
1032 FROM {enrol} e
1033 JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = :userid)
1034 $subwhere
1035 ) en ON (en.courseid = c.id)
1036 $ccjoin
1037 WHERE c.id <> :siteid
1038 $orderby";
1039 $params['userid'] = $userid;
1041 $courses = $DB->get_records_sql($sql, $params);
1043 return $courses;
1049 * Called when user is about to be deleted.
1050 * @param object $user
1051 * @return void
1053 function enrol_user_delete($user) {
1054 global $DB;
1056 $plugins = enrol_get_plugins(true);
1057 foreach ($plugins as $plugin) {
1058 $plugin->user_delete($user);
1061 // force cleanup of all broken enrolments
1062 $DB->delete_records('user_enrolments', array('userid'=>$user->id));
1066 * Called when course is about to be deleted.
1067 * @param stdClass $course
1068 * @return void
1070 function enrol_course_delete($course) {
1071 global $DB;
1073 $instances = enrol_get_instances($course->id, false);
1074 $plugins = enrol_get_plugins(true);
1075 foreach ($instances as $instance) {
1076 if (isset($plugins[$instance->enrol])) {
1077 $plugins[$instance->enrol]->delete_instance($instance);
1079 // low level delete in case plugin did not do it
1080 $DB->delete_records('user_enrolments', array('enrolid'=>$instance->id));
1081 $DB->delete_records('role_assignments', array('itemid'=>$instance->id, 'component'=>'enrol_'.$instance->enrol));
1082 $DB->delete_records('user_enrolments', array('enrolid'=>$instance->id));
1083 $DB->delete_records('enrol', array('id'=>$instance->id));
1088 * Try to enrol user via default internal auth plugin.
1090 * For now this is always using the manual enrol plugin...
1092 * @param $courseid
1093 * @param $userid
1094 * @param $roleid
1095 * @param $timestart
1096 * @param $timeend
1097 * @return bool success
1099 function enrol_try_internal_enrol($courseid, $userid, $roleid = null, $timestart = 0, $timeend = 0) {
1100 global $DB;
1102 //note: this is hardcoded to manual plugin for now
1104 if (!enrol_is_enabled('manual')) {
1105 return false;
1108 if (!$enrol = enrol_get_plugin('manual')) {
1109 return false;
1111 if (!$instances = $DB->get_records('enrol', array('enrol'=>'manual', 'courseid'=>$courseid, 'status'=>ENROL_INSTANCE_ENABLED), 'sortorder,id ASC')) {
1112 return false;
1114 $instance = reset($instances);
1116 $enrol->enrol_user($instance, $userid, $roleid, $timestart, $timeend);
1118 return true;
1122 * Is there a chance users might self enrol
1123 * @param int $courseid
1124 * @return bool
1126 function enrol_selfenrol_available($courseid) {
1127 $result = false;
1129 $plugins = enrol_get_plugins(true);
1130 $enrolinstances = enrol_get_instances($courseid, true);
1131 foreach($enrolinstances as $instance) {
1132 if (!isset($plugins[$instance->enrol])) {
1133 continue;
1135 if ($instance->enrol === 'guest') {
1136 // blacklist known temporary guest plugins
1137 continue;
1139 if ($plugins[$instance->enrol]->show_enrolme_link($instance)) {
1140 $result = true;
1141 break;
1145 return $result;
1149 * This function returns the end of current active user enrolment.
1151 * It deals correctly with multiple overlapping user enrolments.
1153 * @param int $courseid
1154 * @param int $userid
1155 * @return int|bool timestamp when active enrolment ends, false means no active enrolment now, 0 means never
1157 function enrol_get_enrolment_end($courseid, $userid) {
1158 global $DB;
1160 $sql = "SELECT ue.*
1161 FROM {user_enrolments} ue
1162 JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid)
1163 JOIN {user} u ON u.id = ue.userid
1164 WHERE ue.userid = :userid AND ue.status = :active AND e.status = :enabled AND u.deleted = 0";
1165 $params = array('enabled'=>ENROL_INSTANCE_ENABLED, 'active'=>ENROL_USER_ACTIVE, 'userid'=>$userid, 'courseid'=>$courseid);
1167 if (!$enrolments = $DB->get_records_sql($sql, $params)) {
1168 return false;
1171 $changes = array();
1173 foreach ($enrolments as $ue) {
1174 $start = (int)$ue->timestart;
1175 $end = (int)$ue->timeend;
1176 if ($end != 0 and $end < $start) {
1177 debugging('Invalid enrolment start or end in user_enrolment id:'.$ue->id);
1178 continue;
1180 if (isset($changes[$start])) {
1181 $changes[$start] = $changes[$start] + 1;
1182 } else {
1183 $changes[$start] = 1;
1185 if ($end === 0) {
1186 // no end
1187 } else if (isset($changes[$end])) {
1188 $changes[$end] = $changes[$end] - 1;
1189 } else {
1190 $changes[$end] = -1;
1194 // let's sort then enrolment starts&ends and go through them chronologically,
1195 // looking for current status and the next future end of enrolment
1196 ksort($changes);
1198 $now = time();
1199 $current = 0;
1200 $present = null;
1202 foreach ($changes as $time => $change) {
1203 if ($time > $now) {
1204 if ($present === null) {
1205 // we have just went past current time
1206 $present = $current;
1207 if ($present < 1) {
1208 // no enrolment active
1209 return false;
1212 if ($present !== null) {
1213 // we are already in the future - look for possible end
1214 if ($current + $change < 1) {
1215 return $time;
1219 $current += $change;
1222 if ($current > 0) {
1223 return 0;
1224 } else {
1225 return false;
1230 * Is current user accessing course via this enrolment method?
1232 * This is intended for operations that are going to affect enrol instances.
1234 * @param stdClass $instance enrol instance
1235 * @return bool
1237 function enrol_accessing_via_instance(stdClass $instance) {
1238 global $DB, $USER;
1240 if (empty($instance->id)) {
1241 return false;
1244 if (is_siteadmin()) {
1245 // Admins may go anywhere.
1246 return false;
1249 return $DB->record_exists('user_enrolments', array('userid'=>$USER->id, 'enrolid'=>$instance->id));
1253 * Returns true if user is enrolled (is participating) in course
1254 * this is intended for students and teachers.
1256 * Since 2.2 the result for active enrolments and current user are cached.
1258 * @param context $context
1259 * @param int|stdClass $user if null $USER is used, otherwise user object or id expected
1260 * @param string $withcapability extra capability name
1261 * @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
1262 * @return bool
1264 function is_enrolled(context $context, $user = null, $withcapability = '', $onlyactive = false) {
1265 global $USER, $DB;
1267 // First find the course context.
1268 $coursecontext = $context->get_course_context();
1270 // Make sure there is a real user specified.
1271 if ($user === null) {
1272 $userid = isset($USER->id) ? $USER->id : 0;
1273 } else {
1274 $userid = is_object($user) ? $user->id : $user;
1277 if (empty($userid)) {
1278 // Not-logged-in!
1279 return false;
1280 } else if (isguestuser($userid)) {
1281 // Guest account can not be enrolled anywhere.
1282 return false;
1285 // Note everybody participates on frontpage, so for other contexts...
1286 if ($coursecontext->instanceid != SITEID) {
1287 // Try cached info first - the enrolled flag is set only when active enrolment present.
1288 if ($USER->id == $userid) {
1289 $coursecontext->reload_if_dirty();
1290 if (isset($USER->enrol['enrolled'][$coursecontext->instanceid])) {
1291 if ($USER->enrol['enrolled'][$coursecontext->instanceid] > time()) {
1292 if ($withcapability and !has_capability($withcapability, $context, $userid)) {
1293 return false;
1295 return true;
1300 if ($onlyactive) {
1301 // Look for active enrolments only.
1302 $until = enrol_get_enrolment_end($coursecontext->instanceid, $userid);
1304 if ($until === false) {
1305 return false;
1308 if ($USER->id == $userid) {
1309 if ($until == 0) {
1310 $until = ENROL_MAX_TIMESTAMP;
1312 $USER->enrol['enrolled'][$coursecontext->instanceid] = $until;
1313 if (isset($USER->enrol['tempguest'][$coursecontext->instanceid])) {
1314 unset($USER->enrol['tempguest'][$coursecontext->instanceid]);
1315 remove_temp_course_roles($coursecontext);
1319 } else {
1320 // Any enrolment is good for us here, even outdated, disabled or inactive.
1321 $sql = "SELECT 'x'
1322 FROM {user_enrolments} ue
1323 JOIN {enrol} e ON (e.id = ue.enrolid AND e.courseid = :courseid)
1324 JOIN {user} u ON u.id = ue.userid
1325 WHERE ue.userid = :userid AND u.deleted = 0";
1326 $params = array('userid' => $userid, 'courseid' => $coursecontext->instanceid);
1327 if (!$DB->record_exists_sql($sql, $params)) {
1328 return false;
1333 if ($withcapability and !has_capability($withcapability, $context, $userid)) {
1334 return false;
1337 return true;
1341 * Returns an array of joins, wheres and params that will limit the group of
1342 * users to only those enrolled and with given capability (if specified).
1344 * Note this join will return duplicate rows for users who have been enrolled
1345 * several times (e.g. as manual enrolment, and as self enrolment). You may
1346 * need to use a SELECT DISTINCT in your query (see get_enrolled_sql for example).
1348 * @param context $context
1349 * @param string $prefix optional, a prefix to the user id column
1350 * @param string|array $capability optional, may include a capability name, or array of names.
1351 * If an array is provided then this is the equivalent of a logical 'OR',
1352 * i.e. the user needs to have one of these capabilities.
1353 * @param int $group optional, 0 indicates no current group and USERSWITHOUTGROUP users without any group; otherwise the group id
1354 * @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
1355 * @param bool $onlysuspended inverse of onlyactive, consider only suspended enrolments
1356 * @param int $enrolid The enrolment ID. If not 0, only users enrolled using this enrolment method will be returned.
1357 * @return \core\dml\sql_join Contains joins, wheres, params
1359 function get_enrolled_with_capabilities_join(context $context, $prefix = '', $capability = '', $group = 0,
1360 $onlyactive = false, $onlysuspended = false, $enrolid = 0) {
1361 $uid = $prefix . 'u.id';
1362 $joins = array();
1363 $wheres = array();
1365 $enrolledjoin = get_enrolled_join($context, $uid, $onlyactive, $onlysuspended, $enrolid);
1366 $joins[] = $enrolledjoin->joins;
1367 $wheres[] = $enrolledjoin->wheres;
1368 $params = $enrolledjoin->params;
1370 if (!empty($capability)) {
1371 $capjoin = get_with_capability_join($context, $capability, $uid);
1372 $joins[] = $capjoin->joins;
1373 $wheres[] = $capjoin->wheres;
1374 $params = array_merge($params, $capjoin->params);
1377 if ($group) {
1378 $groupjoin = groups_get_members_join($group, $uid, $context);
1379 $joins[] = $groupjoin->joins;
1380 $params = array_merge($params, $groupjoin->params);
1381 if (!empty($groupjoin->wheres)) {
1382 $wheres[] = $groupjoin->wheres;
1386 $joins = implode("\n", $joins);
1387 $wheres[] = "{$prefix}u.deleted = 0";
1388 $wheres = implode(" AND ", $wheres);
1390 return new \core\dml\sql_join($joins, $wheres, $params);
1394 * Returns array with sql code and parameters returning all ids
1395 * of users enrolled into course.
1397 * This function is using 'eu[0-9]+_' prefix for table names and parameters.
1399 * @param context $context
1400 * @param string $withcapability
1401 * @param int $groupid 0 means ignore groups, USERSWITHOUTGROUP without any group and any other value limits the result by group id
1402 * @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
1403 * @param bool $onlysuspended inverse of onlyactive, consider only suspended enrolments
1404 * @param int $enrolid The enrolment ID. If not 0, only users enrolled using this enrolment method will be returned.
1405 * @return array list($sql, $params)
1407 function get_enrolled_sql(context $context, $withcapability = '', $groupid = 0, $onlyactive = false, $onlysuspended = false,
1408 $enrolid = 0) {
1410 // Use unique prefix just in case somebody makes some SQL magic with the result.
1411 static $i = 0;
1412 $i++;
1413 $prefix = 'eu' . $i . '_';
1415 $capjoin = get_enrolled_with_capabilities_join(
1416 $context, $prefix, $withcapability, $groupid, $onlyactive, $onlysuspended, $enrolid);
1418 $sql = "SELECT DISTINCT {$prefix}u.id
1419 FROM {user} {$prefix}u
1420 $capjoin->joins
1421 WHERE $capjoin->wheres";
1423 return array($sql, $capjoin->params);
1427 * Returns array with sql joins and parameters returning all ids
1428 * of users enrolled into course.
1430 * This function is using 'ej[0-9]+_' prefix for table names and parameters.
1432 * @throws coding_exception
1434 * @param context $context
1435 * @param string $useridcolumn User id column used the calling query, e.g. u.id
1436 * @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
1437 * @param bool $onlysuspended inverse of onlyactive, consider only suspended enrolments
1438 * @param int $enrolid The enrolment ID. If not 0, only users enrolled using this enrolment method will be returned.
1439 * @return \core\dml\sql_join Contains joins, wheres, params
1441 function get_enrolled_join(context $context, $useridcolumn, $onlyactive = false, $onlysuspended = false, $enrolid = 0) {
1442 // Use unique prefix just in case somebody makes some SQL magic with the result.
1443 static $i = 0;
1444 $i++;
1445 $prefix = 'ej' . $i . '_';
1447 // First find the course context.
1448 $coursecontext = $context->get_course_context();
1450 $isfrontpage = ($coursecontext->instanceid == SITEID);
1452 if ($onlyactive && $onlysuspended) {
1453 throw new coding_exception("Both onlyactive and onlysuspended are set, this is probably not what you want!");
1455 if ($isfrontpage && $onlysuspended) {
1456 throw new coding_exception("onlysuspended is not supported on frontpage; please add your own early-exit!");
1459 $joins = array();
1460 $wheres = array();
1461 $params = array();
1463 $wheres[] = "1 = 1"; // Prevent broken where clauses later on.
1465 // Note all users are "enrolled" on the frontpage, but for others...
1466 if (!$isfrontpage) {
1467 $where1 = "{$prefix}ue.status = :{$prefix}active AND {$prefix}e.status = :{$prefix}enabled";
1468 $where2 = "{$prefix}ue.timestart < :{$prefix}now1 AND ({$prefix}ue.timeend = 0 OR {$prefix}ue.timeend > :{$prefix}now2)";
1470 $enrolconditions = array(
1471 "{$prefix}e.id = {$prefix}ue.enrolid",
1472 "{$prefix}e.courseid = :{$prefix}courseid",
1474 if ($enrolid) {
1475 $enrolconditions[] = "{$prefix}e.id = :{$prefix}enrolid";
1476 $params[$prefix . 'enrolid'] = $enrolid;
1478 $enrolconditionssql = implode(" AND ", $enrolconditions);
1479 $ejoin = "JOIN {enrol} {$prefix}e ON ($enrolconditionssql)";
1481 $params[$prefix.'courseid'] = $coursecontext->instanceid;
1483 if (!$onlysuspended) {
1484 $joins[] = "JOIN {user_enrolments} {$prefix}ue ON {$prefix}ue.userid = $useridcolumn";
1485 $joins[] = $ejoin;
1486 if ($onlyactive) {
1487 $wheres[] = "$where1 AND $where2";
1489 } else {
1490 // Suspended only where there is enrolment but ALL are suspended.
1491 // Consider multiple enrols where one is not suspended or plain role_assign.
1492 $enrolselect = "SELECT DISTINCT {$prefix}ue.userid FROM {user_enrolments} {$prefix}ue $ejoin WHERE $where1 AND $where2";
1493 $joins[] = "JOIN {user_enrolments} {$prefix}ue1 ON {$prefix}ue1.userid = $useridcolumn";
1494 $enrolconditions = array(
1495 "{$prefix}e1.id = {$prefix}ue1.enrolid",
1496 "{$prefix}e1.courseid = :{$prefix}_e1_courseid",
1498 if ($enrolid) {
1499 $enrolconditions[] = "{$prefix}e1.id = :{$prefix}e1_enrolid";
1500 $params[$prefix . 'e1_enrolid'] = $enrolid;
1502 $enrolconditionssql = implode(" AND ", $enrolconditions);
1503 $joins[] = "JOIN {enrol} {$prefix}e1 ON ($enrolconditionssql)";
1504 $params["{$prefix}_e1_courseid"] = $coursecontext->instanceid;
1505 $wheres[] = "$useridcolumn NOT IN ($enrolselect)";
1508 if ($onlyactive || $onlysuspended) {
1509 $now = round(time(), -2); // Rounding helps caching in DB.
1510 $params = array_merge($params, array($prefix . 'enabled' => ENROL_INSTANCE_ENABLED,
1511 $prefix . 'active' => ENROL_USER_ACTIVE,
1512 $prefix . 'now1' => $now, $prefix . 'now2' => $now));
1516 $joins = implode("\n", $joins);
1517 $wheres = implode(" AND ", $wheres);
1519 return new \core\dml\sql_join($joins, $wheres, $params);
1523 * Returns list of users enrolled into course.
1525 * @param context $context
1526 * @param string $withcapability
1527 * @param int $groupid 0 means ignore groups, USERSWITHOUTGROUP without any group and any other value limits the result by group id
1528 * @param string $userfields requested user record fields
1529 * @param string $orderby
1530 * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
1531 * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
1532 * @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
1533 * @return array of user records
1535 function get_enrolled_users(context $context, $withcapability = '', $groupid = 0, $userfields = 'u.*', $orderby = null,
1536 $limitfrom = 0, $limitnum = 0, $onlyactive = false) {
1537 global $DB;
1539 list($esql, $params) = get_enrolled_sql($context, $withcapability, $groupid, $onlyactive);
1540 $sql = "SELECT $userfields
1541 FROM {user} u
1542 JOIN ($esql) je ON je.id = u.id
1543 WHERE u.deleted = 0";
1545 if ($orderby) {
1546 $sql = "$sql ORDER BY $orderby";
1547 } else {
1548 list($sort, $sortparams) = users_order_by_sql('u');
1549 $sql = "$sql ORDER BY $sort";
1550 $params = array_merge($params, $sortparams);
1553 return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
1557 * Counts list of users enrolled into course (as per above function)
1559 * @param context $context
1560 * @param string $withcapability
1561 * @param int $groupid 0 means ignore groups, any other value limits the result by group id
1562 * @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
1563 * @return array of user records
1565 function count_enrolled_users(context $context, $withcapability = '', $groupid = 0, $onlyactive = false) {
1566 global $DB;
1568 $capjoin = get_enrolled_with_capabilities_join(
1569 $context, '', $withcapability, $groupid, $onlyactive);
1571 $sql = "SELECT COUNT(DISTINCT u.id)
1572 FROM {user} u
1573 $capjoin->joins
1574 WHERE $capjoin->wheres AND u.deleted = 0";
1576 return $DB->count_records_sql($sql, $capjoin->params);
1580 * Send welcome email "from" options.
1582 * @return array list of from options
1584 function enrol_send_welcome_email_options() {
1585 return [
1586 ENROL_DO_NOT_SEND_EMAIL => get_string('no'),
1587 ENROL_SEND_EMAIL_FROM_COURSE_CONTACT => get_string('sendfromcoursecontact', 'enrol'),
1588 ENROL_SEND_EMAIL_FROM_KEY_HOLDER => get_string('sendfromkeyholder', 'enrol'),
1589 ENROL_SEND_EMAIL_FROM_NOREPLY => get_string('sendfromnoreply', 'enrol')
1594 * Serve the user enrolment form as a fragment.
1596 * @param array $args List of named arguments for the fragment loader.
1597 * @return string
1599 function enrol_output_fragment_user_enrolment_form($args) {
1600 global $CFG, $DB;
1602 $args = (object) $args;
1603 $context = $args->context;
1604 require_capability('moodle/course:enrolreview', $context);
1606 $ueid = $args->ueid;
1607 $userenrolment = $DB->get_record('user_enrolments', ['id' => $ueid], '*', MUST_EXIST);
1608 $instance = $DB->get_record('enrol', ['id' => $userenrolment->enrolid], '*', MUST_EXIST);
1609 $plugin = enrol_get_plugin($instance->enrol);
1610 $customdata = [
1611 'ue' => $userenrolment,
1612 'modal' => true,
1613 'enrolinstancename' => $plugin->get_instance_name($instance)
1616 // Set the data if applicable.
1617 $data = [];
1618 if (isset($args->formdata)) {
1619 $serialiseddata = json_decode($args->formdata);
1620 parse_str($serialiseddata, $data);
1623 require_once("$CFG->dirroot/enrol/editenrolment_form.php");
1624 $mform = new \enrol_user_enrolment_form(null, $customdata, 'post', '', null, true, $data);
1626 if (!empty($data)) {
1627 $mform->set_data($data);
1628 $mform->is_validated();
1631 return $mform->render();
1635 * Returns the course where a user enrolment belong to.
1637 * @param int $ueid user_enrolments id
1638 * @return stdClass
1640 function enrol_get_course_by_user_enrolment_id($ueid) {
1641 global $DB;
1642 $sql = "SELECT c.* FROM {user_enrolments} ue
1643 JOIN {enrol} e ON e.id = ue.enrolid
1644 JOIN {course} c ON c.id = e.courseid
1645 WHERE ue.id = :ueid";
1646 return $DB->get_record_sql($sql, array('ueid' => $ueid));
1650 * Return all users enrolled in a course.
1652 * @param int $courseid Course id or false if using $uefilter (user enrolment ids may belong to different courses)
1653 * @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions
1654 * @param array $usersfilter Limit the results obtained to this list of user ids. $uefilter compatibility not guaranteed.
1655 * @param array $uefilter Limit the results obtained to this list of user enrolment ids. $usersfilter compatibility not guaranteed.
1656 * @return stdClass[]
1658 function enrol_get_course_users($courseid = false, $onlyactive = false, $usersfilter = array(), $uefilter = array()) {
1659 global $DB;
1661 if (!$courseid && !$usersfilter && !$uefilter) {
1662 throw new \coding_exception('You should specify at least 1 filter: courseid, users or user enrolments');
1665 $sql = "SELECT ue.id AS ueid, ue.status AS uestatus, ue.enrolid AS ueenrolid, ue.timestart AS uetimestart,
1666 ue.timeend AS uetimeend, ue.modifierid AS uemodifierid, ue.timecreated AS uetimecreated,
1667 ue.timemodified AS uetimemodified,
1668 u.* FROM {user_enrolments} ue
1669 JOIN {enrol} e ON e.id = ue.enrolid
1670 JOIN {user} u ON ue.userid = u.id
1671 WHERE ";
1672 $params = array();
1674 if ($courseid) {
1675 $conditions[] = "e.courseid = :courseid";
1676 $params['courseid'] = $courseid;
1679 if ($onlyactive) {
1680 $conditions[] = "ue.status = :active AND e.status = :enabled AND ue.timestart < :now1 AND " .
1681 "(ue.timeend = 0 OR ue.timeend > :now2)";
1682 // Improves db caching.
1683 $params['now1'] = round(time(), -2);
1684 $params['now2'] = $params['now1'];
1685 $params['active'] = ENROL_USER_ACTIVE;
1686 $params['enabled'] = ENROL_INSTANCE_ENABLED;
1689 if ($usersfilter) {
1690 list($usersql, $userparams) = $DB->get_in_or_equal($usersfilter, SQL_PARAMS_NAMED);
1691 $conditions[] = "ue.userid $usersql";
1692 $params = $params + $userparams;
1695 if ($uefilter) {
1696 list($uesql, $ueparams) = $DB->get_in_or_equal($uefilter, SQL_PARAMS_NAMED);
1697 $conditions[] = "ue.id $uesql";
1698 $params = $params + $ueparams;
1701 return $DB->get_records_sql($sql . ' ' . implode(' AND ', $conditions), $params);
1705 * Enrolment plugins abstract class.
1707 * All enrol plugins should be based on this class,
1708 * this is also the main source of documentation.
1710 * @copyright 2010 Petr Skoda {@link http://skodak.org}
1711 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1713 abstract class enrol_plugin {
1714 protected $config = null;
1717 * Returns name of this enrol plugin
1718 * @return string
1720 public function get_name() {
1721 // second word in class is always enrol name, sorry, no fancy plugin names with _
1722 $words = explode('_', get_class($this));
1723 return $words[1];
1727 * Returns localised name of enrol instance
1729 * @param object $instance (null is accepted too)
1730 * @return string
1732 public function get_instance_name($instance) {
1733 if (empty($instance->name)) {
1734 $enrol = $this->get_name();
1735 return get_string('pluginname', 'enrol_'.$enrol);
1736 } else {
1737 $context = context_course::instance($instance->courseid);
1738 return format_string($instance->name, true, array('context'=>$context));
1743 * Returns optional enrolment information icons.
1745 * This is used in course list for quick overview of enrolment options.
1747 * We are not using single instance parameter because sometimes
1748 * we might want to prevent icon repetition when multiple instances
1749 * of one type exist. One instance may also produce several icons.
1751 * @param array $instances all enrol instances of this type in one course
1752 * @return array of pix_icon
1754 public function get_info_icons(array $instances) {
1755 return array();
1759 * Returns optional enrolment instance description text.
1761 * This is used in detailed course information.
1764 * @param object $instance
1765 * @return string short html text
1767 public function get_description_text($instance) {
1768 return null;
1772 * Makes sure config is loaded and cached.
1773 * @return void
1775 protected function load_config() {
1776 if (!isset($this->config)) {
1777 $name = $this->get_name();
1778 $this->config = get_config("enrol_$name");
1783 * Returns plugin config value
1784 * @param string $name
1785 * @param string $default value if config does not exist yet
1786 * @return string value or default
1788 public function get_config($name, $default = NULL) {
1789 $this->load_config();
1790 return isset($this->config->$name) ? $this->config->$name : $default;
1794 * Sets plugin config value
1795 * @param string $name name of config
1796 * @param string $value string config value, null means delete
1797 * @return string value
1799 public function set_config($name, $value) {
1800 $pluginname = $this->get_name();
1801 $this->load_config();
1802 if ($value === NULL) {
1803 unset($this->config->$name);
1804 } else {
1805 $this->config->$name = $value;
1807 set_config($name, $value, "enrol_$pluginname");
1811 * Does this plugin assign protected roles are can they be manually removed?
1812 * @return bool - false means anybody may tweak roles, it does not use itemid and component when assigning roles
1814 public function roles_protected() {
1815 return true;
1819 * Does this plugin allow manual enrolments?
1821 * @param stdClass $instance course enrol instance
1822 * All plugins allowing this must implement 'enrol/xxx:enrol' capability
1824 * @return bool - true means user with 'enrol/xxx:enrol' may enrol others freely, false means nobody may add more enrolments manually
1826 public function allow_enrol(stdClass $instance) {
1827 return false;
1831 * Does this plugin allow manual unenrolment of all users?
1832 * All plugins allowing this must implement 'enrol/xxx:unenrol' capability
1834 * @param stdClass $instance course enrol instance
1835 * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol others freely, false means nobody may touch user_enrolments
1837 public function allow_unenrol(stdClass $instance) {
1838 return false;
1842 * Does this plugin allow manual unenrolment of a specific user?
1843 * All plugins allowing this must implement 'enrol/xxx:unenrol' capability
1845 * This is useful especially for synchronisation plugins that
1846 * do suspend instead of full unenrolment.
1848 * @param stdClass $instance course enrol instance
1849 * @param stdClass $ue record from user_enrolments table, specifies user
1851 * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol this user, false means nobody may touch this user enrolment
1853 public function allow_unenrol_user(stdClass $instance, stdClass $ue) {
1854 return $this->allow_unenrol($instance);
1858 * Does this plugin allow manual changes in user_enrolments table?
1860 * All plugins allowing this must implement 'enrol/xxx:manage' capability
1862 * @param stdClass $instance course enrol instance
1863 * @return bool - true means it is possible to change enrol period and status in user_enrolments table
1865 public function allow_manage(stdClass $instance) {
1866 return false;
1870 * Does this plugin support some way to user to self enrol?
1872 * @param stdClass $instance course enrol instance
1874 * @return bool - true means show "Enrol me in this course" link in course UI
1876 public function show_enrolme_link(stdClass $instance) {
1877 return false;
1881 * Attempt to automatically enrol current user in course without any interaction,
1882 * calling code has to make sure the plugin and instance are active.
1884 * This should return either a timestamp in the future or false.
1886 * @param stdClass $instance course enrol instance
1887 * @return bool|int false means not enrolled, integer means timeend
1889 public function try_autoenrol(stdClass $instance) {
1890 global $USER;
1892 return false;
1896 * Attempt to automatically gain temporary guest access to course,
1897 * calling code has to make sure the plugin and instance are active.
1899 * This should return either a timestamp in the future or false.
1901 * @param stdClass $instance course enrol instance
1902 * @return bool|int false means no guest access, integer means timeend
1904 public function try_guestaccess(stdClass $instance) {
1905 global $USER;
1907 return false;
1911 * Enrol user into course via enrol instance.
1913 * @param stdClass $instance
1914 * @param int $userid
1915 * @param int $roleid optional role id
1916 * @param int $timestart 0 means unknown
1917 * @param int $timeend 0 means forever
1918 * @param int $status default to ENROL_USER_ACTIVE for new enrolments, no change by default in updates
1919 * @param bool $recovergrades restore grade history
1920 * @return void
1922 public function enrol_user(stdClass $instance, $userid, $roleid = null, $timestart = 0, $timeend = 0, $status = null, $recovergrades = null) {
1923 global $DB, $USER, $CFG; // CFG necessary!!!
1925 if ($instance->courseid == SITEID) {
1926 throw new coding_exception('invalid attempt to enrol into frontpage course!');
1929 $name = $this->get_name();
1930 $courseid = $instance->courseid;
1932 if ($instance->enrol !== $name) {
1933 throw new coding_exception('invalid enrol instance!');
1935 $context = context_course::instance($instance->courseid, MUST_EXIST);
1936 if (!isset($recovergrades)) {
1937 $recovergrades = $CFG->recovergradesdefault;
1940 $inserted = false;
1941 $updated = false;
1942 if ($ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid))) {
1943 //only update if timestart or timeend or status are different.
1944 if ($ue->timestart != $timestart or $ue->timeend != $timeend or (!is_null($status) and $ue->status != $status)) {
1945 $this->update_user_enrol($instance, $userid, $status, $timestart, $timeend);
1947 } else {
1948 $ue = new stdClass();
1949 $ue->enrolid = $instance->id;
1950 $ue->status = is_null($status) ? ENROL_USER_ACTIVE : $status;
1951 $ue->userid = $userid;
1952 $ue->timestart = $timestart;
1953 $ue->timeend = $timeend;
1954 $ue->modifierid = $USER->id;
1955 $ue->timecreated = time();
1956 $ue->timemodified = $ue->timecreated;
1957 $ue->id = $DB->insert_record('user_enrolments', $ue);
1959 $inserted = true;
1962 if ($inserted) {
1963 // Trigger event.
1964 $event = \core\event\user_enrolment_created::create(
1965 array(
1966 'objectid' => $ue->id,
1967 'courseid' => $courseid,
1968 'context' => $context,
1969 'relateduserid' => $ue->userid,
1970 'other' => array('enrol' => $name)
1973 $event->trigger();
1974 // Check if course contacts cache needs to be cleared.
1975 core_course_category::user_enrolment_changed($courseid, $ue->userid,
1976 $ue->status, $ue->timestart, $ue->timeend);
1979 if ($roleid) {
1980 // this must be done after the enrolment event so that the role_assigned event is triggered afterwards
1981 if ($this->roles_protected()) {
1982 role_assign($roleid, $userid, $context->id, 'enrol_'.$name, $instance->id);
1983 } else {
1984 role_assign($roleid, $userid, $context->id);
1988 // Recover old grades if present.
1989 if ($recovergrades) {
1990 require_once("$CFG->libdir/gradelib.php");
1991 grade_recover_history_grades($userid, $courseid);
1994 // reset current user enrolment caching
1995 if ($userid == $USER->id) {
1996 if (isset($USER->enrol['enrolled'][$courseid])) {
1997 unset($USER->enrol['enrolled'][$courseid]);
1999 if (isset($USER->enrol['tempguest'][$courseid])) {
2000 unset($USER->enrol['tempguest'][$courseid]);
2001 remove_temp_course_roles($context);
2007 * Store user_enrolments changes and trigger event.
2009 * @param stdClass $instance
2010 * @param int $userid
2011 * @param int $status
2012 * @param int $timestart
2013 * @param int $timeend
2014 * @return void
2016 public function update_user_enrol(stdClass $instance, $userid, $status = NULL, $timestart = NULL, $timeend = NULL) {
2017 global $DB, $USER, $CFG;
2019 $name = $this->get_name();
2021 if ($instance->enrol !== $name) {
2022 throw new coding_exception('invalid enrol instance!');
2025 if (!$ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid))) {
2026 // weird, user not enrolled
2027 return;
2030 $modified = false;
2031 if (isset($status) and $ue->status != $status) {
2032 $ue->status = $status;
2033 $modified = true;
2035 if (isset($timestart) and $ue->timestart != $timestart) {
2036 $ue->timestart = $timestart;
2037 $modified = true;
2039 if (isset($timeend) and $ue->timeend != $timeend) {
2040 $ue->timeend = $timeend;
2041 $modified = true;
2044 if (!$modified) {
2045 // no change
2046 return;
2049 $ue->modifierid = $USER->id;
2050 $ue->timemodified = time();
2051 $DB->update_record('user_enrolments', $ue);
2053 // User enrolments have changed, so mark user as dirty.
2054 mark_user_dirty($userid);
2056 // Invalidate core_access cache for get_suspended_userids.
2057 cache_helper::invalidate_by_definition('core', 'suspended_userids', array(), array($instance->courseid));
2059 // Trigger event.
2060 $event = \core\event\user_enrolment_updated::create(
2061 array(
2062 'objectid' => $ue->id,
2063 'courseid' => $instance->courseid,
2064 'context' => context_course::instance($instance->courseid),
2065 'relateduserid' => $ue->userid,
2066 'other' => array('enrol' => $name)
2069 $event->trigger();
2071 core_course_category::user_enrolment_changed($instance->courseid, $ue->userid,
2072 $ue->status, $ue->timestart, $ue->timeend);
2076 * Unenrol user from course,
2077 * the last unenrolment removes all remaining roles.
2079 * @param stdClass $instance
2080 * @param int $userid
2081 * @return void
2083 public function unenrol_user(stdClass $instance, $userid) {
2084 global $CFG, $USER, $DB;
2085 require_once("$CFG->dirroot/group/lib.php");
2087 $name = $this->get_name();
2088 $courseid = $instance->courseid;
2090 if ($instance->enrol !== $name) {
2091 throw new coding_exception('invalid enrol instance!');
2093 $context = context_course::instance($instance->courseid, MUST_EXIST);
2095 if (!$ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid))) {
2096 // weird, user not enrolled
2097 return;
2100 // Remove all users groups linked to this enrolment instance.
2101 if ($gms = $DB->get_records('groups_members', array('userid'=>$userid, 'component'=>'enrol_'.$name, 'itemid'=>$instance->id))) {
2102 foreach ($gms as $gm) {
2103 groups_remove_member($gm->groupid, $gm->userid);
2107 role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id, 'component'=>'enrol_'.$name, 'itemid'=>$instance->id));
2108 $DB->delete_records('user_enrolments', array('id'=>$ue->id));
2110 // add extra info and trigger event
2111 $ue->courseid = $courseid;
2112 $ue->enrol = $name;
2114 $sql = "SELECT 'x'
2115 FROM {user_enrolments} ue
2116 JOIN {enrol} e ON (e.id = ue.enrolid)
2117 WHERE ue.userid = :userid AND e.courseid = :courseid";
2118 if ($DB->record_exists_sql($sql, array('userid'=>$userid, 'courseid'=>$courseid))) {
2119 $ue->lastenrol = false;
2121 } else {
2122 // the big cleanup IS necessary!
2123 require_once("$CFG->libdir/gradelib.php");
2125 // remove all remaining roles
2126 role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id), true, false);
2128 //clean up ALL invisible user data from course if this is the last enrolment - groups, grades, etc.
2129 groups_delete_group_members($courseid, $userid);
2131 grade_user_unenrol($courseid, $userid);
2133 $DB->delete_records('user_lastaccess', array('userid'=>$userid, 'courseid'=>$courseid));
2135 $ue->lastenrol = true; // means user not enrolled any more
2137 // Trigger event.
2138 $event = \core\event\user_enrolment_deleted::create(
2139 array(
2140 'courseid' => $courseid,
2141 'context' => $context,
2142 'relateduserid' => $ue->userid,
2143 'objectid' => $ue->id,
2144 'other' => array(
2145 'userenrolment' => (array)$ue,
2146 'enrol' => $name
2150 $event->trigger();
2152 // User enrolments have changed, so mark user as dirty.
2153 mark_user_dirty($userid);
2155 // Check if courrse contacts cache needs to be cleared.
2156 core_course_category::user_enrolment_changed($courseid, $ue->userid, ENROL_USER_SUSPENDED);
2158 // reset current user enrolment caching
2159 if ($userid == $USER->id) {
2160 if (isset($USER->enrol['enrolled'][$courseid])) {
2161 unset($USER->enrol['enrolled'][$courseid]);
2163 if (isset($USER->enrol['tempguest'][$courseid])) {
2164 unset($USER->enrol['tempguest'][$courseid]);
2165 remove_temp_course_roles($context);
2171 * Forces synchronisation of user enrolments.
2173 * This is important especially for external enrol plugins,
2174 * this function is called for all enabled enrol plugins
2175 * right after every user login.
2177 * @param object $user user record
2178 * @return void
2180 public function sync_user_enrolments($user) {
2181 // override if necessary
2185 * This returns false for backwards compatibility, but it is really recommended.
2187 * @since Moodle 3.1
2188 * @return boolean
2190 public function use_standard_editing_ui() {
2191 return false;
2195 * Return whether or not, given the current state, it is possible to add a new instance
2196 * of this enrolment plugin to the course.
2198 * Default implementation is just for backwards compatibility.
2200 * @param int $courseid
2201 * @return boolean
2203 public function can_add_instance($courseid) {
2204 $link = $this->get_newinstance_link($courseid);
2205 return !empty($link);
2209 * Return whether or not, given the current state, it is possible to edit an instance
2210 * of this enrolment plugin in the course. Used by the standard editing UI
2211 * to generate a link to the edit instance form if editing is allowed.
2213 * @param stdClass $instance
2214 * @return boolean
2216 public function can_edit_instance($instance) {
2217 $context = context_course::instance($instance->courseid);
2219 return has_capability('enrol/' . $instance->enrol . ':config', $context);
2223 * Returns link to page which may be used to add new instance of enrolment plugin in course.
2224 * @param int $courseid
2225 * @return moodle_url page url
2227 public function get_newinstance_link($courseid) {
2228 // override for most plugins, check if instance already exists in cases only one instance is supported
2229 return NULL;
2233 * @deprecated since Moodle 2.8 MDL-35864 - please use can_delete_instance() instead.
2235 public function instance_deleteable($instance) {
2236 throw new coding_exception('Function enrol_plugin::instance_deleteable() is deprecated, use
2237 enrol_plugin::can_delete_instance() instead');
2241 * Is it possible to delete enrol instance via standard UI?
2243 * @param stdClass $instance
2244 * @return bool
2246 public function can_delete_instance($instance) {
2247 return false;
2251 * Is it possible to hide/show enrol instance via standard UI?
2253 * @param stdClass $instance
2254 * @return bool
2256 public function can_hide_show_instance($instance) {
2257 debugging("The enrolment plugin '".$this->get_name()."' should override the function can_hide_show_instance().", DEBUG_DEVELOPER);
2258 return true;
2262 * Returns link to manual enrol UI if exists.
2263 * Does the access control tests automatically.
2265 * @param object $instance
2266 * @return moodle_url
2268 public function get_manual_enrol_link($instance) {
2269 return NULL;
2273 * Returns list of unenrol links for all enrol instances in course.
2275 * @param int $instance
2276 * @return moodle_url or NULL if self unenrolment not supported
2278 public function get_unenrolself_link($instance) {
2279 global $USER, $CFG, $DB;
2281 $name = $this->get_name();
2282 if ($instance->enrol !== $name) {
2283 throw new coding_exception('invalid enrol instance!');
2286 if ($instance->courseid == SITEID) {
2287 return NULL;
2290 if (!enrol_is_enabled($name)) {
2291 return NULL;
2294 if ($instance->status != ENROL_INSTANCE_ENABLED) {
2295 return NULL;
2298 if (!file_exists("$CFG->dirroot/enrol/$name/unenrolself.php")) {
2299 return NULL;
2302 $context = context_course::instance($instance->courseid, MUST_EXIST);
2304 if (!has_capability("enrol/$name:unenrolself", $context)) {
2305 return NULL;
2308 if (!$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$USER->id, 'status'=>ENROL_USER_ACTIVE))) {
2309 return NULL;
2312 return new moodle_url("/enrol/$name/unenrolself.php", array('enrolid'=>$instance->id));
2316 * Adds enrol instance UI to course edit form
2318 * @param object $instance enrol instance or null if does not exist yet
2319 * @param MoodleQuickForm $mform
2320 * @param object $data
2321 * @param object $context context of existing course or parent category if course does not exist
2322 * @return void
2324 public function course_edit_form($instance, MoodleQuickForm $mform, $data, $context) {
2325 // override - usually at least enable/disable switch, has to add own form header
2329 * Adds form elements to add/edit instance form.
2331 * @since Moodle 3.1
2332 * @param object $instance enrol instance or null if does not exist yet
2333 * @param MoodleQuickForm $mform
2334 * @param context $context
2335 * @return void
2337 public function edit_instance_form($instance, MoodleQuickForm $mform, $context) {
2338 // Do nothing by default.
2342 * Perform custom validation of the data used to edit the instance.
2344 * @since Moodle 3.1
2345 * @param array $data array of ("fieldname"=>value) of submitted data
2346 * @param array $files array of uploaded files "element_name"=>tmp_file_path
2347 * @param object $instance The instance data loaded from the DB.
2348 * @param context $context The context of the instance we are editing
2349 * @return array of "element_name"=>"error_description" if there are errors,
2350 * or an empty array if everything is OK.
2352 public function edit_instance_validation($data, $files, $instance, $context) {
2353 // No errors by default.
2354 debugging('enrol_plugin::edit_instance_validation() is missing. This plugin has no validation!', DEBUG_DEVELOPER);
2355 return array();
2359 * Validates course edit form data
2361 * @param object $instance enrol instance or null if does not exist yet
2362 * @param array $data
2363 * @param object $context context of existing course or parent category if course does not exist
2364 * @return array errors array
2366 public function course_edit_validation($instance, array $data, $context) {
2367 return array();
2371 * Called after updating/inserting course.
2373 * @param bool $inserted true if course just inserted
2374 * @param object $course
2375 * @param object $data form data
2376 * @return void
2378 public function course_updated($inserted, $course, $data) {
2379 if ($inserted) {
2380 if ($this->get_config('defaultenrol')) {
2381 $this->add_default_instance($course);
2387 * Add new instance of enrol plugin.
2388 * @param object $course
2389 * @param array instance fields
2390 * @return int id of new instance, null if can not be created
2392 public function add_instance($course, array $fields = NULL) {
2393 global $DB;
2395 if ($course->id == SITEID) {
2396 throw new coding_exception('Invalid request to add enrol instance to frontpage.');
2399 $instance = new stdClass();
2400 $instance->enrol = $this->get_name();
2401 $instance->status = ENROL_INSTANCE_ENABLED;
2402 $instance->courseid = $course->id;
2403 $instance->enrolstartdate = 0;
2404 $instance->enrolenddate = 0;
2405 $instance->timemodified = time();
2406 $instance->timecreated = $instance->timemodified;
2407 $instance->sortorder = $DB->get_field('enrol', 'COALESCE(MAX(sortorder), -1) + 1', array('courseid'=>$course->id));
2409 $fields = (array)$fields;
2410 unset($fields['enrol']);
2411 unset($fields['courseid']);
2412 unset($fields['sortorder']);
2413 foreach($fields as $field=>$value) {
2414 $instance->$field = $value;
2417 $instance->id = $DB->insert_record('enrol', $instance);
2419 \core\event\enrol_instance_created::create_from_record($instance)->trigger();
2421 return $instance->id;
2425 * Update instance of enrol plugin.
2427 * @since Moodle 3.1
2428 * @param stdClass $instance
2429 * @param stdClass $data modified instance fields
2430 * @return boolean
2432 public function update_instance($instance, $data) {
2433 global $DB;
2434 $properties = array('status', 'name', 'password', 'customint1', 'customint2', 'customint3',
2435 'customint4', 'customint5', 'customint6', 'customint7', 'customint8',
2436 'customchar1', 'customchar2', 'customchar3', 'customdec1', 'customdec2',
2437 'customtext1', 'customtext2', 'customtext3', 'customtext4', 'roleid',
2438 'enrolperiod', 'expirynotify', 'notifyall', 'expirythreshold',
2439 'enrolstartdate', 'enrolenddate', 'cost', 'currency');
2441 foreach ($properties as $key) {
2442 if (isset($data->$key)) {
2443 $instance->$key = $data->$key;
2446 $instance->timemodified = time();
2448 $update = $DB->update_record('enrol', $instance);
2449 if ($update) {
2450 \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
2452 return $update;
2456 * Add new instance of enrol plugin with default settings,
2457 * called when adding new instance manually or when adding new course.
2459 * Not all plugins support this.
2461 * @param object $course
2462 * @return int id of new instance or null if no default supported
2464 public function add_default_instance($course) {
2465 return null;
2469 * Update instance status
2471 * Override when plugin needs to do some action when enabled or disabled.
2473 * @param stdClass $instance
2474 * @param int $newstatus ENROL_INSTANCE_ENABLED, ENROL_INSTANCE_DISABLED
2475 * @return void
2477 public function update_status($instance, $newstatus) {
2478 global $DB;
2480 $instance->status = $newstatus;
2481 $DB->update_record('enrol', $instance);
2483 $context = context_course::instance($instance->courseid);
2484 \core\event\enrol_instance_updated::create_from_record($instance)->trigger();
2486 // Invalidate all enrol caches.
2487 $context->mark_dirty();
2491 * Delete course enrol plugin instance, unenrol all users.
2492 * @param object $instance
2493 * @return void
2495 public function delete_instance($instance) {
2496 global $DB;
2498 $name = $this->get_name();
2499 if ($instance->enrol !== $name) {
2500 throw new coding_exception('invalid enrol instance!');
2503 //first unenrol all users
2504 $participants = $DB->get_recordset('user_enrolments', array('enrolid'=>$instance->id));
2505 foreach ($participants as $participant) {
2506 $this->unenrol_user($instance, $participant->userid);
2508 $participants->close();
2510 // now clean up all remainders that were not removed correctly
2511 if ($gms = $DB->get_records('groups_members', array('itemid' => $instance->id, 'component' => 'enrol_' . $name))) {
2512 foreach ($gms as $gm) {
2513 groups_remove_member($gm->groupid, $gm->userid);
2516 $DB->delete_records('role_assignments', array('itemid'=>$instance->id, 'component'=>'enrol_'.$name));
2517 $DB->delete_records('user_enrolments', array('enrolid'=>$instance->id));
2519 // finally drop the enrol row
2520 $DB->delete_records('enrol', array('id'=>$instance->id));
2522 $context = context_course::instance($instance->courseid);
2523 \core\event\enrol_instance_deleted::create_from_record($instance)->trigger();
2525 // Invalidate all enrol caches.
2526 $context->mark_dirty();
2530 * Creates course enrol form, checks if form submitted
2531 * and enrols user if necessary. It can also redirect.
2533 * @param stdClass $instance
2534 * @return string html text, usually a form in a text box
2536 public function enrol_page_hook(stdClass $instance) {
2537 return null;
2541 * Checks if user can self enrol.
2543 * @param stdClass $instance enrolment instance
2544 * @param bool $checkuserenrolment if true will check if user enrolment is inactive.
2545 * used by navigation to improve performance.
2546 * @return bool|string true if successful, else error message or false
2548 public function can_self_enrol(stdClass $instance, $checkuserenrolment = true) {
2549 return false;
2553 * Return information for enrolment instance containing list of parameters required
2554 * for enrolment, name of enrolment plugin etc.
2556 * @param stdClass $instance enrolment instance
2557 * @return array instance info.
2559 public function get_enrol_info(stdClass $instance) {
2560 return null;
2564 * Adds navigation links into course admin block.
2566 * By defaults looks for manage links only.
2568 * @param navigation_node $instancesnode
2569 * @param stdClass $instance
2570 * @return void
2572 public function add_course_navigation($instancesnode, stdClass $instance) {
2573 if ($this->use_standard_editing_ui()) {
2574 $context = context_course::instance($instance->courseid);
2575 $cap = 'enrol/' . $instance->enrol . ':config';
2576 if (has_capability($cap, $context)) {
2577 $linkparams = array('courseid' => $instance->courseid, 'id' => $instance->id, 'type' => $instance->enrol);
2578 $managelink = new moodle_url('/enrol/editinstance.php', $linkparams);
2579 $instancesnode->add($this->get_instance_name($instance), $managelink, navigation_node::TYPE_SETTING);
2585 * Returns edit icons for the page with list of instances
2586 * @param stdClass $instance
2587 * @return array
2589 public function get_action_icons(stdClass $instance) {
2590 global $OUTPUT;
2592 $icons = array();
2593 if ($this->use_standard_editing_ui()) {
2594 $linkparams = array('courseid' => $instance->courseid, 'id' => $instance->id, 'type' => $instance->enrol);
2595 $editlink = new moodle_url("/enrol/editinstance.php", $linkparams);
2596 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit'), 'core',
2597 array('class' => 'iconsmall')));
2599 return $icons;
2603 * Reads version.php and determines if it is necessary
2604 * to execute the cron job now.
2605 * @return bool
2607 public function is_cron_required() {
2608 global $CFG;
2610 $name = $this->get_name();
2611 $versionfile = "$CFG->dirroot/enrol/$name/version.php";
2612 $plugin = new stdClass();
2613 include($versionfile);
2614 if (empty($plugin->cron)) {
2615 return false;
2617 $lastexecuted = $this->get_config('lastcron', 0);
2618 if ($lastexecuted + $plugin->cron < time()) {
2619 return true;
2620 } else {
2621 return false;
2626 * Called for all enabled enrol plugins that returned true from is_cron_required().
2627 * @return void
2629 public function cron() {
2633 * Called when user is about to be deleted
2634 * @param object $user
2635 * @return void
2637 public function user_delete($user) {
2638 global $DB;
2640 $sql = "SELECT e.*
2641 FROM {enrol} e
2642 JOIN {user_enrolments} ue ON (ue.enrolid = e.id)
2643 WHERE e.enrol = :name AND ue.userid = :userid";
2644 $params = array('name'=>$this->get_name(), 'userid'=>$user->id);
2646 $rs = $DB->get_recordset_sql($sql, $params);
2647 foreach($rs as $instance) {
2648 $this->unenrol_user($instance, $user->id);
2650 $rs->close();
2654 * Returns an enrol_user_button that takes the user to a page where they are able to
2655 * enrol users into the managers course through this plugin.
2657 * Optional: If the plugin supports manual enrolments it can choose to override this
2658 * otherwise it shouldn't
2660 * @param course_enrolment_manager $manager
2661 * @return enrol_user_button|false
2663 public function get_manual_enrol_button(course_enrolment_manager $manager) {
2664 return false;
2668 * Gets an array of the user enrolment actions
2670 * @param course_enrolment_manager $manager
2671 * @param stdClass $ue
2672 * @return array An array of user_enrolment_actions
2674 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
2675 $actions = [];
2676 $context = $manager->get_context();
2677 $instance = $ue->enrolmentinstance;
2678 $params = $manager->get_moodlepage()->url->params();
2679 $params['ue'] = $ue->id;
2681 // Edit enrolment action.
2682 if ($this->allow_manage($instance) && has_capability("enrol/{$instance->enrol}:manage", $context)) {
2683 $title = get_string('editenrolment', 'enrol');
2684 $icon = new pix_icon('t/edit', $title);
2685 $url = new moodle_url('/enrol/editenrolment.php', $params);
2686 $actionparams = [
2687 'class' => 'editenrollink',
2688 'rel' => $ue->id,
2689 'data-action' => ENROL_ACTION_EDIT
2691 $actions[] = new user_enrolment_action($icon, $title, $url, $actionparams);
2694 // Unenrol action.
2695 if ($this->allow_unenrol_user($instance, $ue) && has_capability("enrol/{$instance->enrol}:unenrol", $context)) {
2696 $title = get_string('unenrol', 'enrol');
2697 $icon = new pix_icon('t/delete', $title);
2698 $url = new moodle_url('/enrol/unenroluser.php', $params);
2699 $actionparams = [
2700 'class' => 'unenrollink',
2701 'rel' => $ue->id,
2702 'data-action' => ENROL_ACTION_UNENROL
2704 $actions[] = new user_enrolment_action($icon, $title, $url, $actionparams);
2706 return $actions;
2710 * Returns true if the plugin has one or more bulk operations that can be performed on
2711 * user enrolments.
2713 * @param course_enrolment_manager $manager
2714 * @return bool
2716 public function has_bulk_operations(course_enrolment_manager $manager) {
2717 return false;
2721 * Return an array of enrol_bulk_enrolment_operation objects that define
2722 * the bulk actions that can be performed on user enrolments by the plugin.
2724 * @param course_enrolment_manager $manager
2725 * @return array
2727 public function get_bulk_operations(course_enrolment_manager $manager) {
2728 return array();
2732 * Do any enrolments need expiration processing.
2734 * Plugins that want to call this functionality must implement 'expiredaction' config setting.
2736 * @param progress_trace $trace
2737 * @param int $courseid one course, empty mean all
2738 * @return bool true if any data processed, false if not
2740 public function process_expirations(progress_trace $trace, $courseid = null) {
2741 global $DB;
2743 $name = $this->get_name();
2744 if (!enrol_is_enabled($name)) {
2745 $trace->finished();
2746 return false;
2749 $processed = false;
2750 $params = array();
2751 $coursesql = "";
2752 if ($courseid) {
2753 $coursesql = "AND e.courseid = :courseid";
2756 // Deal with expired accounts.
2757 $action = $this->get_config('expiredaction', ENROL_EXT_REMOVED_KEEP);
2759 if ($action == ENROL_EXT_REMOVED_UNENROL) {
2760 $instances = array();
2761 $sql = "SELECT ue.*, e.courseid, c.id AS contextid
2762 FROM {user_enrolments} ue
2763 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = :enrol)
2764 JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :courselevel)
2765 WHERE ue.timeend > 0 AND ue.timeend < :now $coursesql";
2766 $params = array('now'=>time(), 'courselevel'=>CONTEXT_COURSE, 'enrol'=>$name, 'courseid'=>$courseid);
2768 $rs = $DB->get_recordset_sql($sql, $params);
2769 foreach ($rs as $ue) {
2770 if (!$processed) {
2771 $trace->output("Starting processing of enrol_$name expirations...");
2772 $processed = true;
2774 if (empty($instances[$ue->enrolid])) {
2775 $instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
2777 $instance = $instances[$ue->enrolid];
2778 if (!$this->roles_protected()) {
2779 // Let's just guess what extra roles are supposed to be removed.
2780 if ($instance->roleid) {
2781 role_unassign($instance->roleid, $ue->userid, $ue->contextid);
2784 // The unenrol cleans up all subcontexts if this is the only course enrolment for this user.
2785 $this->unenrol_user($instance, $ue->userid);
2786 $trace->output("Unenrolling expired user $ue->userid from course $instance->courseid", 1);
2788 $rs->close();
2789 unset($instances);
2791 } else if ($action == ENROL_EXT_REMOVED_SUSPENDNOROLES or $action == ENROL_EXT_REMOVED_SUSPEND) {
2792 $instances = array();
2793 $sql = "SELECT ue.*, e.courseid, c.id AS contextid
2794 FROM {user_enrolments} ue
2795 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = :enrol)
2796 JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :courselevel)
2797 WHERE ue.timeend > 0 AND ue.timeend < :now
2798 AND ue.status = :useractive $coursesql";
2799 $params = array('now'=>time(), 'courselevel'=>CONTEXT_COURSE, 'useractive'=>ENROL_USER_ACTIVE, 'enrol'=>$name, 'courseid'=>$courseid);
2800 $rs = $DB->get_recordset_sql($sql, $params);
2801 foreach ($rs as $ue) {
2802 if (!$processed) {
2803 $trace->output("Starting processing of enrol_$name expirations...");
2804 $processed = true;
2806 if (empty($instances[$ue->enrolid])) {
2807 $instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
2809 $instance = $instances[$ue->enrolid];
2811 if ($action == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
2812 if (!$this->roles_protected()) {
2813 // Let's just guess what roles should be removed.
2814 $count = $DB->count_records('role_assignments', array('userid'=>$ue->userid, 'contextid'=>$ue->contextid));
2815 if ($count == 1) {
2816 role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$ue->contextid, 'component'=>'', 'itemid'=>0));
2818 } else if ($count > 1 and $instance->roleid) {
2819 role_unassign($instance->roleid, $ue->userid, $ue->contextid, '', 0);
2822 // In any case remove all roles that belong to this instance and user.
2823 role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$ue->contextid, 'component'=>'enrol_'.$name, 'itemid'=>$instance->id), true);
2824 // Final cleanup of subcontexts if there are no more course roles.
2825 if (0 == $DB->count_records('role_assignments', array('userid'=>$ue->userid, 'contextid'=>$ue->contextid))) {
2826 role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$ue->contextid, 'component'=>'', 'itemid'=>0), true);
2830 $this->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
2831 $trace->output("Suspending expired user $ue->userid in course $instance->courseid", 1);
2833 $rs->close();
2834 unset($instances);
2836 } else {
2837 // ENROL_EXT_REMOVED_KEEP means no changes.
2840 if ($processed) {
2841 $trace->output("...finished processing of enrol_$name expirations");
2842 } else {
2843 $trace->output("No expired enrol_$name enrolments detected");
2845 $trace->finished();
2847 return $processed;
2851 * Send expiry notifications.
2853 * Plugin that wants to have expiry notification MUST implement following:
2854 * - expirynotifyhour plugin setting,
2855 * - configuration options in instance edit form (expirynotify, notifyall and expirythreshold),
2856 * - notification strings (expirymessageenrollersubject, expirymessageenrollerbody,
2857 * expirymessageenrolledsubject and expirymessageenrolledbody),
2858 * - expiry_notification provider in db/messages.php,
2859 * - upgrade code that sets default thresholds for existing courses (should be 1 day),
2860 * - something that calls this method, such as cron.
2862 * @param progress_trace $trace (accepts bool for backwards compatibility only)
2864 public function send_expiry_notifications($trace) {
2865 global $DB, $CFG;
2867 $name = $this->get_name();
2868 if (!enrol_is_enabled($name)) {
2869 $trace->finished();
2870 return;
2873 // Unfortunately this may take a long time, it should not be interrupted,
2874 // otherwise users get duplicate notification.
2876 core_php_time_limit::raise();
2877 raise_memory_limit(MEMORY_HUGE);
2880 $expirynotifylast = $this->get_config('expirynotifylast', 0);
2881 $expirynotifyhour = $this->get_config('expirynotifyhour');
2882 if (is_null($expirynotifyhour)) {
2883 debugging("send_expiry_notifications() in $name enrolment plugin needs expirynotifyhour setting");
2884 $trace->finished();
2885 return;
2888 if (!($trace instanceof progress_trace)) {
2889 $trace = $trace ? new text_progress_trace() : new null_progress_trace();
2890 debugging('enrol_plugin::send_expiry_notifications() now expects progress_trace instance as parameter!', DEBUG_DEVELOPER);
2893 $timenow = time();
2894 $notifytime = usergetmidnight($timenow, $CFG->timezone) + ($expirynotifyhour * 3600);
2896 if ($expirynotifylast > $notifytime) {
2897 $trace->output($name.' enrolment expiry notifications were already sent today at '.userdate($expirynotifylast, '', $CFG->timezone).'.');
2898 $trace->finished();
2899 return;
2901 } else if ($timenow < $notifytime) {
2902 $trace->output($name.' enrolment expiry notifications will be sent at '.userdate($notifytime, '', $CFG->timezone).'.');
2903 $trace->finished();
2904 return;
2907 $trace->output('Processing '.$name.' enrolment expiration notifications...');
2909 // Notify users responsible for enrolment once every day.
2910 $sql = "SELECT ue.*, e.expirynotify, e.notifyall, e.expirythreshold, e.courseid, c.fullname
2911 FROM {user_enrolments} ue
2912 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = :name AND e.expirynotify > 0 AND e.status = :enabled)
2913 JOIN {course} c ON (c.id = e.courseid)
2914 JOIN {user} u ON (u.id = ue.userid AND u.deleted = 0 AND u.suspended = 0)
2915 WHERE ue.status = :active AND ue.timeend > 0 AND ue.timeend > :now1 AND ue.timeend < (e.expirythreshold + :now2)
2916 ORDER BY ue.enrolid ASC, u.lastname ASC, u.firstname ASC, u.id ASC";
2917 $params = array('enabled'=>ENROL_INSTANCE_ENABLED, 'active'=>ENROL_USER_ACTIVE, 'now1'=>$timenow, 'now2'=>$timenow, 'name'=>$name);
2919 $rs = $DB->get_recordset_sql($sql, $params);
2921 $lastenrollid = 0;
2922 $users = array();
2924 foreach($rs as $ue) {
2925 if ($lastenrollid and $lastenrollid != $ue->enrolid) {
2926 $this->notify_expiry_enroller($lastenrollid, $users, $trace);
2927 $users = array();
2929 $lastenrollid = $ue->enrolid;
2931 $enroller = $this->get_enroller($ue->enrolid);
2932 $context = context_course::instance($ue->courseid);
2934 $user = $DB->get_record('user', array('id'=>$ue->userid));
2936 $users[] = array('fullname'=>fullname($user, has_capability('moodle/site:viewfullnames', $context, $enroller)), 'timeend'=>$ue->timeend);
2938 if (!$ue->notifyall) {
2939 continue;
2942 if ($ue->timeend - $ue->expirythreshold + 86400 < $timenow) {
2943 // Notify enrolled users only once at the start of the threshold.
2944 $trace->output("user $ue->userid was already notified that enrolment in course $ue->courseid expires on ".userdate($ue->timeend, '', $CFG->timezone), 1);
2945 continue;
2948 $this->notify_expiry_enrolled($user, $ue, $trace);
2950 $rs->close();
2952 if ($lastenrollid and $users) {
2953 $this->notify_expiry_enroller($lastenrollid, $users, $trace);
2956 $trace->output('...notification processing finished.');
2957 $trace->finished();
2959 $this->set_config('expirynotifylast', $timenow);
2963 * Returns the user who is responsible for enrolments for given instance.
2965 * Override if plugin knows anybody better than admin.
2967 * @param int $instanceid enrolment instance id
2968 * @return stdClass user record
2970 protected function get_enroller($instanceid) {
2971 return get_admin();
2975 * Notify user about incoming expiration of their enrolment,
2976 * it is called only if notification of enrolled users (aka students) is enabled in course.
2978 * This is executed only once for each expiring enrolment right
2979 * at the start of the expiration threshold.
2981 * @param stdClass $user
2982 * @param stdClass $ue
2983 * @param progress_trace $trace
2985 protected function notify_expiry_enrolled($user, $ue, progress_trace $trace) {
2986 global $CFG;
2988 $name = $this->get_name();
2990 $oldforcelang = force_current_language($user->lang);
2992 $enroller = $this->get_enroller($ue->enrolid);
2993 $context = context_course::instance($ue->courseid);
2995 $a = new stdClass();
2996 $a->course = format_string($ue->fullname, true, array('context'=>$context));
2997 $a->user = fullname($user, true);
2998 $a->timeend = userdate($ue->timeend, '', $user->timezone);
2999 $a->enroller = fullname($enroller, has_capability('moodle/site:viewfullnames', $context, $user));
3001 $subject = get_string('expirymessageenrolledsubject', 'enrol_'.$name, $a);
3002 $body = get_string('expirymessageenrolledbody', 'enrol_'.$name, $a);
3004 $message = new \core\message\message();
3005 $message->courseid = $ue->courseid;
3006 $message->notification = 1;
3007 $message->component = 'enrol_'.$name;
3008 $message->name = 'expiry_notification';
3009 $message->userfrom = $enroller;
3010 $message->userto = $user;
3011 $message->subject = $subject;
3012 $message->fullmessage = $body;
3013 $message->fullmessageformat = FORMAT_MARKDOWN;
3014 $message->fullmessagehtml = markdown_to_html($body);
3015 $message->smallmessage = $subject;
3016 $message->contexturlname = $a->course;
3017 $message->contexturl = (string)new moodle_url('/course/view.php', array('id'=>$ue->courseid));
3019 if (message_send($message)) {
3020 $trace->output("notifying user $ue->userid that enrolment in course $ue->courseid expires on ".userdate($ue->timeend, '', $CFG->timezone), 1);
3021 } else {
3022 $trace->output("error notifying user $ue->userid that enrolment in course $ue->courseid expires on ".userdate($ue->timeend, '', $CFG->timezone), 1);
3025 force_current_language($oldforcelang);
3029 * Notify person responsible for enrolments that some user enrolments will be expired soon,
3030 * it is called only if notification of enrollers (aka teachers) is enabled in course.
3032 * This is called repeatedly every day for each course if there are any pending expiration
3033 * in the expiration threshold.
3035 * @param int $eid
3036 * @param array $users
3037 * @param progress_trace $trace
3039 protected function notify_expiry_enroller($eid, $users, progress_trace $trace) {
3040 global $DB;
3042 $name = $this->get_name();
3044 $instance = $DB->get_record('enrol', array('id'=>$eid, 'enrol'=>$name));
3045 $context = context_course::instance($instance->courseid);
3046 $course = $DB->get_record('course', array('id'=>$instance->courseid));
3048 $enroller = $this->get_enroller($instance->id);
3049 $admin = get_admin();
3051 $oldforcelang = force_current_language($enroller->lang);
3053 foreach($users as $key=>$info) {
3054 $users[$key] = '* '.$info['fullname'].' - '.userdate($info['timeend'], '', $enroller->timezone);
3057 $a = new stdClass();
3058 $a->course = format_string($course->fullname, true, array('context'=>$context));
3059 $a->threshold = get_string('numdays', '', $instance->expirythreshold / (60*60*24));
3060 $a->users = implode("\n", $users);
3061 $a->extendurl = (string)new moodle_url('/user/index.php', array('id'=>$instance->courseid));
3063 $subject = get_string('expirymessageenrollersubject', 'enrol_'.$name, $a);
3064 $body = get_string('expirymessageenrollerbody', 'enrol_'.$name, $a);
3066 $message = new \core\message\message();
3067 $message->courseid = $course->id;
3068 $message->notification = 1;
3069 $message->component = 'enrol_'.$name;
3070 $message->name = 'expiry_notification';
3071 $message->userfrom = $admin;
3072 $message->userto = $enroller;
3073 $message->subject = $subject;
3074 $message->fullmessage = $body;
3075 $message->fullmessageformat = FORMAT_MARKDOWN;
3076 $message->fullmessagehtml = markdown_to_html($body);
3077 $message->smallmessage = $subject;
3078 $message->contexturlname = $a->course;
3079 $message->contexturl = $a->extendurl;
3081 if (message_send($message)) {
3082 $trace->output("notifying user $enroller->id about all expiring $name enrolments in course $instance->courseid", 1);
3083 } else {
3084 $trace->output("error notifying user $enroller->id about all expiring $name enrolments in course $instance->courseid", 1);
3087 force_current_language($oldforcelang);
3091 * Backup execution step hook to annotate custom fields.
3093 * @param backup_enrolments_execution_step $step
3094 * @param stdClass $enrol
3096 public function backup_annotate_custom_fields(backup_enrolments_execution_step $step, stdClass $enrol) {
3097 // Override as necessary to annotate custom fields in the enrol table.
3101 * Automatic enrol sync executed during restore.
3102 * Useful for automatic sync by course->idnumber or course category.
3103 * @param stdClass $course course record
3105 public function restore_sync_course($course) {
3106 // Override if necessary.
3110 * Restore instance and map settings.
3112 * @param restore_enrolments_structure_step $step
3113 * @param stdClass $data
3114 * @param stdClass $course
3115 * @param int $oldid
3117 public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
3118 // Do not call this from overridden methods, restore and set new id there.
3119 $step->set_mapping('enrol', $oldid, 0);
3123 * Restore user enrolment.
3125 * @param restore_enrolments_structure_step $step
3126 * @param stdClass $data
3127 * @param stdClass $instance
3128 * @param int $oldinstancestatus
3129 * @param int $userid
3131 public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
3132 // Override as necessary if plugin supports restore of enrolments.
3136 * Restore role assignment.
3138 * @param stdClass $instance
3139 * @param int $roleid
3140 * @param int $userid
3141 * @param int $contextid
3143 public function restore_role_assignment($instance, $roleid, $userid, $contextid) {
3144 // No role assignment by default, override if necessary.
3148 * Restore user group membership.
3149 * @param stdClass $instance
3150 * @param int $groupid
3151 * @param int $userid
3153 public function restore_group_member($instance, $groupid, $userid) {
3154 // Implement if you want to restore protected group memberships,
3155 // usually this is not necessary because plugins should be able to recreate the memberships automatically.
3159 * Returns defaults for new instances.
3160 * @since Moodle 3.1
3161 * @return array
3163 public function get_instance_defaults() {
3164 return array();
3168 * Validate a list of parameter names and types.
3169 * @since Moodle 3.1
3171 * @param array $data array of ("fieldname"=>value) of submitted data
3172 * @param array $rules array of ("fieldname"=>PARAM_X types - or "fieldname"=>array( list of valid options )
3173 * @return array of "element_name"=>"error_description" if there are errors,
3174 * or an empty array if everything is OK.
3176 public function validate_param_types($data, $rules) {
3177 $errors = array();
3178 $invalidstr = get_string('invaliddata', 'error');
3179 foreach ($rules as $fieldname => $rule) {
3180 if (is_array($rule)) {
3181 if (!in_array($data[$fieldname], $rule)) {
3182 $errors[$fieldname] = $invalidstr;
3184 } else {
3185 if ($data[$fieldname] != clean_param($data[$fieldname], $rule)) {
3186 $errors[$fieldname] = $invalidstr;
3190 return $errors;