Moodle release 3.9.3
[moodle.git] / enrol / locallib.php
blob0a46f0f02ef63194ef33416993c00b8bcf52f499
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This file contains the course_enrolment_manager class which is used to interface
19 * with the functions that exist in enrollib.php in relation to a single course.
21 * @package core_enrol
22 * @copyright 2010 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * This class provides a targeted tied together means of interfacing the enrolment
30 * tasks together with a course.
32 * It is provided as a convenience more than anything else.
34 * @copyright 2010 Sam Hemelryk
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class course_enrolment_manager {
39 /**
40 * The course context
41 * @var context
43 protected $context;
44 /**
45 * The course we are managing enrolments for
46 * @var stdClass
48 protected $course = null;
49 /**
50 * Limits the focus of the manager to one enrolment plugin instance
51 * @var string
53 protected $instancefilter = null;
54 /**
55 * Limits the focus of the manager to users with specified role
56 * @var int
58 protected $rolefilter = 0;
59 /**
60 * Limits the focus of the manager to users who match search string
61 * @var string
63 protected $searchfilter = '';
64 /**
65 * Limits the focus of the manager to users in specified group
66 * @var int
68 protected $groupfilter = 0;
69 /**
70 * Limits the focus of the manager to users who match status active/inactive
71 * @var int
73 protected $statusfilter = -1;
75 /**
76 * The total number of users enrolled in the course
77 * Populated by course_enrolment_manager::get_total_users
78 * @var int
80 protected $totalusers = null;
81 /**
82 * An array of users currently enrolled in the course
83 * Populated by course_enrolment_manager::get_users
84 * @var array
86 protected $users = array();
88 /**
89 * An array of users who have roles within this course but who have not
90 * been enrolled in the course
91 * @var array
93 protected $otherusers = array();
95 /**
96 * The total number of users who hold a role within the course but who
97 * arn't enrolled.
98 * @var int
100 protected $totalotherusers = null;
103 * The current moodle_page object
104 * @var moodle_page
106 protected $moodlepage = null;
108 /**#@+
109 * These variables are used to cache the information this class uses
110 * please never use these directly instead use their get_ counterparts.
111 * @access private
112 * @var array
114 private $_instancessql = null;
115 private $_instances = null;
116 private $_inames = null;
117 private $_plugins = null;
118 private $_allplugins = null;
119 private $_roles = null;
120 private $_visibleroles = null;
121 private $_assignableroles = null;
122 private $_assignablerolesothers = null;
123 private $_groups = null;
124 /**#@-*/
127 * Constructs the course enrolment manager
129 * @param moodle_page $moodlepage
130 * @param stdClass $course
131 * @param string $instancefilter
132 * @param int $rolefilter If non-zero, filters to users with specified role
133 * @param string $searchfilter If non-blank, filters to users with search text
134 * @param int $groupfilter if non-zero, filter users with specified group
135 * @param int $statusfilter if not -1, filter users with active/inactive enrollment.
137 public function __construct(moodle_page $moodlepage, $course, $instancefilter = null,
138 $rolefilter = 0, $searchfilter = '', $groupfilter = 0, $statusfilter = -1) {
139 $this->moodlepage = $moodlepage;
140 $this->context = context_course::instance($course->id);
141 $this->course = $course;
142 $this->instancefilter = $instancefilter;
143 $this->rolefilter = $rolefilter;
144 $this->searchfilter = $searchfilter;
145 $this->groupfilter = $groupfilter;
146 $this->statusfilter = $statusfilter;
150 * Returns the current moodle page
151 * @return moodle_page
153 public function get_moodlepage() {
154 return $this->moodlepage;
158 * Returns the total number of enrolled users in the course.
160 * If a filter was specificed this will be the total number of users enrolled
161 * in this course by means of that instance.
163 * @global moodle_database $DB
164 * @return int
166 public function get_total_users() {
167 global $DB;
168 if ($this->totalusers === null) {
169 list($instancessql, $params, $filter) = $this->get_instance_sql();
170 list($filtersql, $moreparams) = $this->get_filter_sql();
171 $params += $moreparams;
172 $sqltotal = "SELECT COUNT(DISTINCT u.id)
173 FROM {user} u
174 JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid $instancessql)
175 JOIN {enrol} e ON (e.id = ue.enrolid)";
176 if ($this->groupfilter) {
177 $sqltotal .= " LEFT JOIN ({groups_members} gm JOIN {groups} g ON (g.id = gm.groupid))
178 ON (u.id = gm.userid AND g.courseid = e.courseid)";
180 $sqltotal .= "WHERE $filtersql";
181 $this->totalusers = (int)$DB->count_records_sql($sqltotal, $params);
183 return $this->totalusers;
187 * Returns the total number of enrolled users in the course.
189 * If a filter was specificed this will be the total number of users enrolled
190 * in this course by means of that instance.
192 * @global moodle_database $DB
193 * @return int
195 public function get_total_other_users() {
196 global $DB;
197 if ($this->totalotherusers === null) {
198 list($ctxcondition, $params) = $DB->get_in_or_equal($this->context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'ctx');
199 $params['courseid'] = $this->course->id;
200 $sql = "SELECT COUNT(DISTINCT u.id)
201 FROM {role_assignments} ra
202 JOIN {user} u ON u.id = ra.userid
203 JOIN {context} ctx ON ra.contextid = ctx.id
204 LEFT JOIN (
205 SELECT ue.id, ue.userid
206 FROM {user_enrolments} ue
207 LEFT JOIN {enrol} e ON e.id=ue.enrolid
208 WHERE e.courseid = :courseid
209 ) ue ON ue.userid=u.id
210 WHERE ctx.id $ctxcondition AND
211 ue.id IS NULL";
212 $this->totalotherusers = (int)$DB->count_records_sql($sql, $params);
214 return $this->totalotherusers;
218 * Gets all of the users enrolled in this course.
220 * If a filter was specified this will be the users who were enrolled
221 * in this course by means of that instance. If role or search filters were
222 * specified then these will also be applied.
224 * @global moodle_database $DB
225 * @param string $sort
226 * @param string $direction ASC or DESC
227 * @param int $page First page should be 0
228 * @param int $perpage Defaults to 25
229 * @return array
231 public function get_users($sort, $direction='ASC', $page=0, $perpage=25) {
232 global $DB;
233 if ($direction !== 'ASC') {
234 $direction = 'DESC';
236 $key = md5("$sort-$direction-$page-$perpage");
237 if (!array_key_exists($key, $this->users)) {
238 list($instancessql, $params, $filter) = $this->get_instance_sql();
239 list($filtersql, $moreparams) = $this->get_filter_sql();
240 $params += $moreparams;
241 $extrafields = get_extra_user_fields($this->get_context());
242 $extrafields[] = 'lastaccess';
243 $ufields = user_picture::fields('u', $extrafields);
244 $sql = "SELECT DISTINCT $ufields, COALESCE(ul.timeaccess, 0) AS lastcourseaccess
245 FROM {user} u
246 JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid $instancessql)
247 JOIN {enrol} e ON (e.id = ue.enrolid)
248 LEFT JOIN {user_lastaccess} ul ON (ul.courseid = e.courseid AND ul.userid = u.id)";
249 if ($this->groupfilter) {
250 $sql .= " LEFT JOIN ({groups_members} gm JOIN {groups} g ON (g.id = gm.groupid))
251 ON (u.id = gm.userid AND g.courseid = e.courseid)";
253 $sql .= "WHERE $filtersql
254 ORDER BY $sort $direction";
255 $this->users[$key] = $DB->get_records_sql($sql, $params, $page*$perpage, $perpage);
257 return $this->users[$key];
261 * Obtains WHERE clause to filter results by defined search and role filter
262 * (instance filter is handled separately in JOIN clause, see
263 * get_instance_sql).
265 * @return array Two-element array with SQL and params for WHERE clause
267 protected function get_filter_sql() {
268 global $DB;
270 // Search condition.
271 $extrafields = get_extra_user_fields($this->get_context());
272 list($sql, $params) = users_search_sql($this->searchfilter, 'u', true, $extrafields);
274 // Role condition.
275 if ($this->rolefilter) {
276 // Get context SQL.
277 $contextids = $this->context->get_parent_context_ids();
278 $contextids[] = $this->context->id;
279 list($contextsql, $contextparams) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED);
280 $params += $contextparams;
282 // Role check condition.
283 $sql .= " AND (SELECT COUNT(1) FROM {role_assignments} ra WHERE ra.userid = u.id " .
284 "AND ra.roleid = :roleid AND ra.contextid $contextsql) > 0";
285 $params['roleid'] = $this->rolefilter;
288 // Group condition.
289 if ($this->groupfilter) {
290 if ($this->groupfilter < 0) {
291 // Show users who are not in any group.
292 $sql .= " AND gm.groupid IS NULL";
293 } else {
294 $sql .= " AND gm.groupid = :groupid";
295 $params['groupid'] = $this->groupfilter;
299 // Status condition.
300 if ($this->statusfilter === ENROL_USER_ACTIVE) {
301 $sql .= " AND ue.status = :active AND e.status = :enabled AND ue.timestart < :now1
302 AND (ue.timeend = 0 OR ue.timeend > :now2)";
303 $now = round(time(), -2); // rounding helps caching in DB
304 $params += array('enabled' => ENROL_INSTANCE_ENABLED,
305 'active' => ENROL_USER_ACTIVE,
306 'now1' => $now,
307 'now2' => $now);
308 } else if ($this->statusfilter === ENROL_USER_SUSPENDED) {
309 $sql .= " AND (ue.status = :inactive OR e.status = :disabled OR ue.timestart > :now1
310 OR (ue.timeend <> 0 AND ue.timeend < :now2))";
311 $now = round(time(), -2); // rounding helps caching in DB
312 $params += array('disabled' => ENROL_INSTANCE_DISABLED,
313 'inactive' => ENROL_USER_SUSPENDED,
314 'now1' => $now,
315 'now2' => $now);
318 return array($sql, $params);
322 * Gets and array of other users.
324 * Other users are users who have been assigned roles or inherited roles
325 * within this course but who have not been enrolled in the course
327 * @global moodle_database $DB
328 * @param string $sort
329 * @param string $direction
330 * @param int $page
331 * @param int $perpage
332 * @return array
334 public function get_other_users($sort, $direction='ASC', $page=0, $perpage=25) {
335 global $DB;
336 if ($direction !== 'ASC') {
337 $direction = 'DESC';
339 $key = md5("$sort-$direction-$page-$perpage");
340 if (!array_key_exists($key, $this->otherusers)) {
341 list($ctxcondition, $params) = $DB->get_in_or_equal($this->context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'ctx');
342 $params['courseid'] = $this->course->id;
343 $params['cid'] = $this->course->id;
344 $extrafields = get_extra_user_fields($this->get_context());
345 $ufields = user_picture::fields('u', $extrafields);
346 $sql = "SELECT ra.id as raid, ra.contextid, ra.component, ctx.contextlevel, ra.roleid, $ufields,
347 coalesce(u.lastaccess,0) AS lastaccess
348 FROM {role_assignments} ra
349 JOIN {user} u ON u.id = ra.userid
350 JOIN {context} ctx ON ra.contextid = ctx.id
351 LEFT JOIN (
352 SELECT ue.id, ue.userid
353 FROM {user_enrolments} ue
354 JOIN {enrol} e ON e.id = ue.enrolid
355 WHERE e.courseid = :courseid
356 ) ue ON ue.userid=u.id
357 WHERE ctx.id $ctxcondition AND
358 ue.id IS NULL
359 ORDER BY $sort $direction, ctx.depth DESC";
360 $this->otherusers[$key] = $DB->get_records_sql($sql, $params, $page*$perpage, $perpage);
362 return $this->otherusers[$key];
366 * Helper method used by {@link get_potential_users()} and {@link search_other_users()}.
368 * @param string $search the search term, if any.
369 * @param bool $searchanywhere Can the search term be anywhere, or must it be at the start.
370 * @return array with three elements:
371 * string list of fields to SELECT,
372 * string contents of SQL WHERE clause,
373 * array query params. Note that the SQL snippets use named parameters.
375 protected function get_basic_search_conditions($search, $searchanywhere) {
376 global $DB, $CFG;
378 // Add some additional sensible conditions
379 $tests = array("u.id <> :guestid", 'u.deleted = 0', 'u.confirmed = 1');
380 $params = array('guestid' => $CFG->siteguest);
381 if (!empty($search)) {
382 $conditions = get_extra_user_fields($this->get_context());
383 foreach (get_all_user_name_fields() as $field) {
384 $conditions[] = 'u.'.$field;
386 $conditions[] = $DB->sql_fullname('u.firstname', 'u.lastname');
387 if ($searchanywhere) {
388 $searchparam = '%' . $search . '%';
389 } else {
390 $searchparam = $search . '%';
392 $i = 0;
393 foreach ($conditions as $key => $condition) {
394 $conditions[$key] = $DB->sql_like($condition, ":con{$i}00", false);
395 $params["con{$i}00"] = $searchparam;
396 $i++;
398 $tests[] = '(' . implode(' OR ', $conditions) . ')';
400 $wherecondition = implode(' AND ', $tests);
402 $extrafields = get_extra_user_fields($this->get_context(), array('username', 'lastaccess'));
403 $extrafields[] = 'username';
404 $extrafields[] = 'lastaccess';
405 $extrafields[] = 'maildisplay';
406 $ufields = user_picture::fields('u', $extrafields);
408 return array($ufields, $params, $wherecondition);
412 * Helper method used by {@link get_potential_users()} and {@link search_other_users()}.
414 * @param string $search the search string, if any.
415 * @param string $fields the first bit of the SQL when returning some users.
416 * @param string $countfields fhe first bit of the SQL when counting the users.
417 * @param string $sql the bulk of the SQL statement.
418 * @param array $params query parameters.
419 * @param int $page which page number of the results to show.
420 * @param int $perpage number of users per page.
421 * @param int $addedenrollment number of users added to enrollment.
422 * @param bool $returnexactcount Return the exact total users using count_record or not.
423 * @return array with two or three elements:
424 * int totalusers Number users matching the search. (This element only exist if $returnexactcount was set to true)
425 * array users List of user objects returned by the query.
426 * boolean moreusers True if there are still more users, otherwise is False.
427 * @throws dml_exception
429 protected function execute_search_queries($search, $fields, $countfields, $sql, array $params, $page, $perpage,
430 $addedenrollment = 0, $returnexactcount = false) {
431 global $DB, $CFG;
433 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->get_context());
434 $order = ' ORDER BY ' . $sort;
436 $totalusers = 0;
437 $moreusers = false;
438 $results = [];
440 $availableusers = $DB->get_records_sql($fields . $sql . $order,
441 array_merge($params, $sortparams), ($page * $perpage) - $addedenrollment, $perpage + 1);
442 if ($availableusers) {
443 $totalusers = count($availableusers);
444 $moreusers = $totalusers > $perpage;
446 if ($moreusers) {
447 // We need to discard the last record.
448 array_pop($availableusers);
451 if ($returnexactcount && $moreusers) {
452 // There is more data. We need to do the exact count.
453 $totalusers = $DB->count_records_sql($countfields . $sql, $params);
457 $results['users'] = $availableusers;
458 $results['moreusers'] = $moreusers;
460 if ($returnexactcount) {
461 // Include totalusers in result if $returnexactcount flag is true.
462 $results['totalusers'] = $totalusers;
465 return $results;
469 * Gets an array of the users that can be enrolled in this course.
471 * @global moodle_database $DB
472 * @param int $enrolid
473 * @param string $search
474 * @param bool $searchanywhere
475 * @param int $page Defaults to 0
476 * @param int $perpage Defaults to 25
477 * @param int $addedenrollment Defaults to 0
478 * @param bool $returnexactcount Return the exact total users using count_record or not.
479 * @return array with two or three elements:
480 * int totalusers Number users matching the search. (This element only exist if $returnexactcount was set to true)
481 * array users List of user objects returned by the query.
482 * boolean moreusers True if there are still more users, otherwise is False.
483 * @throws dml_exception
485 public function get_potential_users($enrolid, $search = '', $searchanywhere = false, $page = 0, $perpage = 25,
486 $addedenrollment = 0, $returnexactcount = false) {
487 global $DB;
489 list($ufields, $params, $wherecondition) = $this->get_basic_search_conditions($search, $searchanywhere);
491 $fields = 'SELECT '.$ufields;
492 $countfields = 'SELECT COUNT(1)';
493 $sql = " FROM {user} u
494 LEFT JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid = :enrolid)
495 WHERE $wherecondition
496 AND ue.id IS NULL";
497 $params['enrolid'] = $enrolid;
499 return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, $addedenrollment,
500 $returnexactcount);
504 * Searches other users and returns paginated results
506 * @global moodle_database $DB
507 * @param string $search
508 * @param bool $searchanywhere
509 * @param int $page Starting at 0
510 * @param int $perpage
511 * @param bool $returnexactcount Return the exact total users using count_record or not.
512 * @return array with two or three elements:
513 * int totalusers Number users matching the search. (This element only exist if $returnexactcount was set to true)
514 * array users List of user objects returned by the query.
515 * boolean moreusers True if there are still more users, otherwise is False.
516 * @throws dml_exception
518 public function search_other_users($search = '', $searchanywhere = false, $page = 0, $perpage = 25, $returnexactcount = false) {
519 global $DB, $CFG;
521 list($ufields, $params, $wherecondition) = $this->get_basic_search_conditions($search, $searchanywhere);
523 $fields = 'SELECT ' . $ufields;
524 $countfields = 'SELECT COUNT(u.id)';
525 $sql = " FROM {user} u
526 LEFT JOIN {role_assignments} ra ON (ra.userid = u.id AND ra.contextid = :contextid)
527 WHERE $wherecondition
528 AND ra.id IS NULL";
529 $params['contextid'] = $this->context->id;
531 return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, 0, $returnexactcount);
535 * Searches through the enrolled users in this course.
537 * @param string $search The search term.
538 * @param bool $searchanywhere Can the search term be anywhere, or must it be at the start.
539 * @param int $page Starting at 0.
540 * @param int $perpage Number of users returned per page.
541 * @param bool $returnexactcount Return the exact total users using count_record or not.
542 * @return array with two or three elements:
543 * int totalusers Number users matching the search. (This element only exist if $returnexactcount was set to true)
544 * array users List of user objects returned by the query.
545 * boolean moreusers True if there are still more users, otherwise is False.
547 public function search_users(string $search = '', bool $searchanywhere = false, int $page = 0, int $perpage = 25,
548 bool $returnexactcount = false) {
549 list($ufields, $params, $wherecondition) = $this->get_basic_search_conditions($search, $searchanywhere);
551 $fields = 'SELECT ' . $ufields;
552 $countfields = 'SELECT COUNT(u.id)';
553 $sql = " FROM {user} u
554 JOIN {user_enrolments} ue ON ue.userid = u.id
555 JOIN {enrol} e ON ue.enrolid = e.id
556 WHERE $wherecondition
557 AND e.courseid = :courseid";
558 $params['courseid'] = $this->course->id;
560 return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, 0, $returnexactcount);
564 * Gets an array containing some SQL to user for when selecting, params for
565 * that SQL, and the filter that was used in constructing the sql.
567 * @global moodle_database $DB
568 * @return string
570 protected function get_instance_sql() {
571 global $DB;
572 if ($this->_instancessql === null) {
573 $instances = $this->get_enrolment_instances();
574 $filter = $this->get_enrolment_filter();
575 if ($filter && array_key_exists($filter, $instances)) {
576 $sql = " = :ifilter";
577 $params = array('ifilter'=>$filter);
578 } else {
579 $filter = 0;
580 if ($instances) {
581 list($sql, $params) = $DB->get_in_or_equal(array_keys($this->get_enrolment_instances()), SQL_PARAMS_NAMED);
582 } else {
583 // no enabled instances, oops, we should probably say something
584 $sql = "= :never";
585 $params = array('never'=>-1);
588 $this->instancefilter = $filter;
589 $this->_instancessql = array($sql, $params, $filter);
591 return $this->_instancessql;
595 * Returns all of the enrolment instances for this course.
597 * @param bool $onlyenabled Whether to return data from enabled enrolment instance names only.
598 * @return array
600 public function get_enrolment_instances($onlyenabled = false) {
601 if ($this->_instances === null) {
602 $this->_instances = enrol_get_instances($this->course->id, $onlyenabled);
604 return $this->_instances;
608 * Returns the names for all of the enrolment instances for this course.
610 * @param bool $onlyenabled Whether to return data from enabled enrolment instance names only.
611 * @return array
613 public function get_enrolment_instance_names($onlyenabled = false) {
614 if ($this->_inames === null) {
615 $instances = $this->get_enrolment_instances($onlyenabled);
616 $plugins = $this->get_enrolment_plugins(false);
617 foreach ($instances as $key=>$instance) {
618 if (!isset($plugins[$instance->enrol])) {
619 // weird, some broken stuff in plugin
620 unset($instances[$key]);
621 continue;
623 $this->_inames[$key] = $plugins[$instance->enrol]->get_instance_name($instance);
626 return $this->_inames;
630 * Gets all of the enrolment plugins that are available for this course.
632 * @param bool $onlyenabled return only enabled enrol plugins
633 * @return array
635 public function get_enrolment_plugins($onlyenabled = true) {
636 if ($this->_plugins === null) {
637 $this->_plugins = enrol_get_plugins(true);
640 if ($onlyenabled) {
641 return $this->_plugins;
644 if ($this->_allplugins === null) {
645 // Make sure we have the same objects in _allplugins and _plugins.
646 $this->_allplugins = $this->_plugins;
647 foreach (enrol_get_plugins(false) as $name=>$plugin) {
648 if (!isset($this->_allplugins[$name])) {
649 $this->_allplugins[$name] = $plugin;
654 return $this->_allplugins;
658 * Gets all of the roles this course can contain.
660 * @return array
662 public function get_all_roles() {
663 if ($this->_roles === null) {
664 $this->_roles = role_fix_names(get_all_roles($this->context), $this->context);
666 return $this->_roles;
670 * Gets all of the roles this course can contain.
672 * @return array
674 public function get_viewable_roles() {
675 if ($this->_visibleroles === null) {
676 $this->_visibleroles = get_viewable_roles($this->context);
678 return $this->_visibleroles;
682 * Gets all of the assignable roles for this course.
684 * @return array
686 public function get_assignable_roles($otherusers = false) {
687 if ($this->_assignableroles === null) {
688 $this->_assignableroles = get_assignable_roles($this->context, ROLENAME_ALIAS, false); // verifies unassign access control too
691 if ($otherusers) {
692 if (!is_array($this->_assignablerolesothers)) {
693 $this->_assignablerolesothers = array();
694 list($courseviewroles, $ignored) = get_roles_with_cap_in_context($this->context, 'moodle/course:view');
695 foreach ($this->_assignableroles as $roleid=>$role) {
696 if (isset($courseviewroles[$roleid])) {
697 $this->_assignablerolesothers[$roleid] = $role;
701 return $this->_assignablerolesothers;
702 } else {
703 return $this->_assignableroles;
708 * Gets all of the assignable roles for this course, wrapped in an array to ensure
709 * role sort order is not lost during json deserialisation.
711 * @param boolean $otherusers whether to include the assignable roles for other users
712 * @return array
714 public function get_assignable_roles_for_json($otherusers = false) {
715 $rolesarray = array();
716 $assignable = $this->get_assignable_roles($otherusers);
717 foreach ($assignable as $id => $role) {
718 $rolesarray[] = array('id' => $id, 'name' => $role);
720 return $rolesarray;
724 * Gets all of the groups for this course.
726 * @return array
728 public function get_all_groups() {
729 if ($this->_groups === null) {
730 $this->_groups = groups_get_all_groups($this->course->id);
731 foreach ($this->_groups as $gid=>$group) {
732 $this->_groups[$gid]->name = format_string($group->name);
735 return $this->_groups;
739 * Unenrols a user from the course given the users ue entry
741 * @global moodle_database $DB
742 * @param stdClass $ue
743 * @return bool
745 public function unenrol_user($ue) {
746 global $DB;
747 list ($instance, $plugin) = $this->get_user_enrolment_components($ue);
748 if ($instance && $plugin && $plugin->allow_unenrol_user($instance, $ue) && has_capability("enrol/$instance->enrol:unenrol", $this->context)) {
749 $plugin->unenrol_user($instance, $ue->userid);
750 return true;
752 return false;
756 * Given a user enrolment record this method returns the plugin and enrolment
757 * instance that relate to it.
759 * @param stdClass|int $userenrolment
760 * @return array array($instance, $plugin)
762 public function get_user_enrolment_components($userenrolment) {
763 global $DB;
764 if (is_numeric($userenrolment)) {
765 $userenrolment = $DB->get_record('user_enrolments', array('id'=>(int)$userenrolment));
767 $instances = $this->get_enrolment_instances();
768 $plugins = $this->get_enrolment_plugins(false);
769 if (!$userenrolment || !isset($instances[$userenrolment->enrolid])) {
770 return array(false, false);
772 $instance = $instances[$userenrolment->enrolid];
773 $plugin = $plugins[$instance->enrol];
774 return array($instance, $plugin);
778 * Removes an assigned role from a user.
780 * @global moodle_database $DB
781 * @param int $userid
782 * @param int $roleid
783 * @return bool
785 public function unassign_role_from_user($userid, $roleid) {
786 global $DB;
787 // Admins may unassign any role, others only those they could assign.
788 if (!is_siteadmin() and !array_key_exists($roleid, $this->get_assignable_roles())) {
789 if (defined('AJAX_SCRIPT')) {
790 throw new moodle_exception('invalidrole');
792 return false;
794 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
795 $ras = $DB->get_records('role_assignments', array('contextid'=>$this->context->id, 'userid'=>$user->id, 'roleid'=>$roleid));
796 foreach ($ras as $ra) {
797 if ($ra->component) {
798 if (strpos($ra->component, 'enrol_') !== 0) {
799 continue;
801 if (!$plugin = enrol_get_plugin(substr($ra->component, 6))) {
802 continue;
804 if ($plugin->roles_protected()) {
805 continue;
808 role_unassign($ra->roleid, $ra->userid, $ra->contextid, $ra->component, $ra->itemid);
810 return true;
814 * Assigns a role to a user.
816 * @param int $roleid
817 * @param int $userid
818 * @return int|false
820 public function assign_role_to_user($roleid, $userid) {
821 require_capability('moodle/role:assign', $this->context);
822 if (!array_key_exists($roleid, $this->get_assignable_roles())) {
823 if (defined('AJAX_SCRIPT')) {
824 throw new moodle_exception('invalidrole');
826 return false;
828 return role_assign($roleid, $userid, $this->context->id, '', NULL);
832 * Adds a user to a group
834 * @param stdClass $user
835 * @param int $groupid
836 * @return bool
838 public function add_user_to_group($user, $groupid) {
839 require_capability('moodle/course:managegroups', $this->context);
840 $group = $this->get_group($groupid);
841 if (!$group) {
842 return false;
844 return groups_add_member($group->id, $user->id);
848 * Removes a user from a group
850 * @global moodle_database $DB
851 * @param StdClass $user
852 * @param int $groupid
853 * @return bool
855 public function remove_user_from_group($user, $groupid) {
856 global $DB;
857 require_capability('moodle/course:managegroups', $this->context);
858 $group = $this->get_group($groupid);
859 if (!groups_remove_member_allowed($group, $user)) {
860 return false;
862 if (!$group) {
863 return false;
865 return groups_remove_member($group, $user);
869 * Gets the requested group
871 * @param int $groupid
872 * @return stdClass|int
874 public function get_group($groupid) {
875 $groups = $this->get_all_groups();
876 if (!array_key_exists($groupid, $groups)) {
877 return false;
879 return $groups[$groupid];
883 * Edits an enrolment
885 * @param stdClass $userenrolment
886 * @param stdClass $data
887 * @return bool
889 public function edit_enrolment($userenrolment, $data) {
890 //Only allow editing if the user has the appropriate capability
891 //Already checked in /user/index.php but checking again in case this function is called from elsewhere
892 list($instance, $plugin) = $this->get_user_enrolment_components($userenrolment);
893 if ($instance && $plugin && $plugin->allow_manage($instance) && has_capability("enrol/$instance->enrol:manage", $this->context)) {
894 if (!isset($data->status)) {
895 $data->status = $userenrolment->status;
897 $plugin->update_user_enrol($instance, $userenrolment->userid, $data->status, $data->timestart, $data->timeend);
898 return true;
900 return false;
904 * Returns the current enrolment filter that is being applied by this class
905 * @return string
907 public function get_enrolment_filter() {
908 return $this->instancefilter;
912 * Gets the roles assigned to this user that are applicable for this course.
914 * @param int $userid
915 * @return array
917 public function get_user_roles($userid) {
918 $roles = array();
919 $ras = get_user_roles($this->context, $userid, true, 'c.contextlevel DESC, r.sortorder ASC');
920 $plugins = $this->get_enrolment_plugins(false);
921 foreach ($ras as $ra) {
922 if ($ra->contextid != $this->context->id) {
923 if (!array_key_exists($ra->roleid, $roles)) {
924 $roles[$ra->roleid] = null;
926 // higher ras, course always takes precedence
927 continue;
929 if (array_key_exists($ra->roleid, $roles) && $roles[$ra->roleid] === false) {
930 continue;
932 $changeable = true;
933 if ($ra->component) {
934 $changeable = false;
935 if (strpos($ra->component, 'enrol_') === 0) {
936 $plugin = substr($ra->component, 6);
937 if (isset($plugins[$plugin])) {
938 $changeable = !$plugins[$plugin]->roles_protected();
943 $roles[$ra->roleid] = $changeable;
945 return $roles;
949 * Gets the enrolments this user has in the course - including all suspended plugins and instances.
951 * @global moodle_database $DB
952 * @param int $userid
953 * @return array
955 public function get_user_enrolments($userid) {
956 global $DB;
957 list($instancessql, $params, $filter) = $this->get_instance_sql();
958 $params['userid'] = $userid;
959 $userenrolments = $DB->get_records_select('user_enrolments', "enrolid $instancessql AND userid = :userid", $params);
960 $instances = $this->get_enrolment_instances();
961 $plugins = $this->get_enrolment_plugins(false);
962 $inames = $this->get_enrolment_instance_names();
963 foreach ($userenrolments as &$ue) {
964 $ue->enrolmentinstance = $instances[$ue->enrolid];
965 $ue->enrolmentplugin = $plugins[$ue->enrolmentinstance->enrol];
966 $ue->enrolmentinstancename = $inames[$ue->enrolmentinstance->id];
968 return $userenrolments;
972 * Gets the groups this user belongs to
974 * @param int $userid
975 * @return array
977 public function get_user_groups($userid) {
978 return groups_get_all_groups($this->course->id, $userid, 0, 'g.id');
982 * Retursn an array of params that would go into the URL to return to this
983 * exact page.
985 * @return array
987 public function get_url_params() {
988 $args = array(
989 'id' => $this->course->id
991 if (!empty($this->instancefilter)) {
992 $args['ifilter'] = $this->instancefilter;
994 if (!empty($this->rolefilter)) {
995 $args['role'] = $this->rolefilter;
997 if ($this->searchfilter !== '') {
998 $args['search'] = $this->searchfilter;
1000 if (!empty($this->groupfilter)) {
1001 $args['filtergroup'] = $this->groupfilter;
1003 if ($this->statusfilter !== -1) {
1004 $args['status'] = $this->statusfilter;
1006 return $args;
1010 * Returns the course this object is managing enrolments for
1012 * @return stdClass
1014 public function get_course() {
1015 return $this->course;
1019 * Returns the course context
1021 * @return context
1023 public function get_context() {
1024 return $this->context;
1028 * Gets an array of other users in this course ready for display.
1030 * Other users are users who have been assigned or inherited roles within this
1031 * course but have not been enrolled.
1033 * @param core_enrol_renderer $renderer
1034 * @param moodle_url $pageurl
1035 * @param string $sort
1036 * @param string $direction ASC | DESC
1037 * @param int $page Starting from 0
1038 * @param int $perpage
1039 * @return array
1041 public function get_other_users_for_display(core_enrol_renderer $renderer, moodle_url $pageurl, $sort, $direction, $page, $perpage) {
1043 $userroles = $this->get_other_users($sort, $direction, $page, $perpage);
1044 $roles = $this->get_all_roles();
1045 $plugins = $this->get_enrolment_plugins(false);
1047 $context = $this->get_context();
1048 $now = time();
1049 $extrafields = get_extra_user_fields($context);
1051 $users = array();
1052 foreach ($userroles as $userrole) {
1053 $contextid = $userrole->contextid;
1054 unset($userrole->contextid); // This would collide with user avatar.
1055 if (!array_key_exists($userrole->id, $users)) {
1056 $users[$userrole->id] = $this->prepare_user_for_display($userrole, $extrafields, $now);
1058 $a = new stdClass;
1059 $a->role = $roles[$userrole->roleid]->localname;
1060 if ($contextid == $this->context->id) {
1061 $changeable = true;
1062 if ($userrole->component) {
1063 $changeable = false;
1064 if (strpos($userrole->component, 'enrol_') === 0) {
1065 $plugin = substr($userrole->component, 6);
1066 if (isset($plugins[$plugin])) {
1067 $changeable = !$plugins[$plugin]->roles_protected();
1071 $roletext = get_string('rolefromthiscourse', 'enrol', $a);
1072 } else {
1073 $changeable = false;
1074 switch ($userrole->contextlevel) {
1075 case CONTEXT_COURSE :
1076 // Meta course
1077 $roletext = get_string('rolefrommetacourse', 'enrol', $a);
1078 break;
1079 case CONTEXT_COURSECAT :
1080 $roletext = get_string('rolefromcategory', 'enrol', $a);
1081 break;
1082 case CONTEXT_SYSTEM:
1083 default:
1084 $roletext = get_string('rolefromsystem', 'enrol', $a);
1085 break;
1088 if (!isset($users[$userrole->id]['roles'])) {
1089 $users[$userrole->id]['roles'] = array();
1091 $users[$userrole->id]['roles'][$userrole->roleid] = array(
1092 'text' => $roletext,
1093 'unchangeable' => !$changeable
1096 return $users;
1100 * Gets an array of users for display, this includes minimal user information
1101 * as well as minimal information on the users roles, groups, and enrolments.
1103 * @param core_enrol_renderer $renderer
1104 * @param moodle_url $pageurl
1105 * @param int $sort
1106 * @param string $direction ASC or DESC
1107 * @param int $page
1108 * @param int $perpage
1109 * @return array
1111 public function get_users_for_display(course_enrolment_manager $manager, $sort, $direction, $page, $perpage) {
1112 $pageurl = $manager->get_moodlepage()->url;
1113 $users = $this->get_users($sort, $direction, $page, $perpage);
1115 $now = time();
1116 $straddgroup = get_string('addgroup', 'group');
1117 $strunenrol = get_string('unenrol', 'enrol');
1118 $stredit = get_string('edit');
1120 $visibleroles = $this->get_viewable_roles();
1121 $assignable = $this->get_assignable_roles();
1122 $allgroups = $this->get_all_groups();
1123 $context = $this->get_context();
1124 $canmanagegroups = has_capability('moodle/course:managegroups', $context);
1126 $url = new moodle_url($pageurl, $this->get_url_params());
1127 $extrafields = get_extra_user_fields($context);
1129 $enabledplugins = $this->get_enrolment_plugins(true);
1131 $userdetails = array();
1132 foreach ($users as $user) {
1133 $details = $this->prepare_user_for_display($user, $extrafields, $now);
1135 // Roles
1136 $details['roles'] = array();
1137 foreach ($this->get_user_roles($user->id) as $rid=>$rassignable) {
1138 $unchangeable = !$rassignable;
1139 if (!is_siteadmin() and !isset($assignable[$rid])) {
1140 $unchangeable = true;
1143 if (isset($visibleroles[$rid])) {
1144 $label = $visibleroles[$rid];
1145 } else {
1146 $label = get_string('novisibleroles', 'role');
1147 $unchangeable = true;
1150 $details['roles'][$rid] = array('text' => $label, 'unchangeable' => $unchangeable);
1153 // Users
1154 $usergroups = $this->get_user_groups($user->id);
1155 $details['groups'] = array();
1156 foreach($usergroups as $gid=>$unused) {
1157 $details['groups'][$gid] = $allgroups[$gid]->name;
1160 // Enrolments
1161 $details['enrolments'] = array();
1162 foreach ($this->get_user_enrolments($user->id) as $ue) {
1163 if (!isset($enabledplugins[$ue->enrolmentinstance->enrol])) {
1164 $details['enrolments'][$ue->id] = array(
1165 'text' => $ue->enrolmentinstancename,
1166 'period' => null,
1167 'dimmed' => true,
1168 'actions' => array()
1170 continue;
1171 } else if ($ue->timestart and $ue->timeend) {
1172 $period = get_string('periodstartend', 'enrol', array('start'=>userdate($ue->timestart), 'end'=>userdate($ue->timeend)));
1173 $periodoutside = ($ue->timestart && $ue->timeend && ($now < $ue->timestart || $now > $ue->timeend));
1174 } else if ($ue->timestart) {
1175 $period = get_string('periodstart', 'enrol', userdate($ue->timestart));
1176 $periodoutside = ($ue->timestart && $now < $ue->timestart);
1177 } else if ($ue->timeend) {
1178 $period = get_string('periodend', 'enrol', userdate($ue->timeend));
1179 $periodoutside = ($ue->timeend && $now > $ue->timeend);
1180 } else {
1181 // If there is no start or end show when user was enrolled.
1182 $period = get_string('periodnone', 'enrol', userdate($ue->timecreated));
1183 $periodoutside = false;
1185 $details['enrolments'][$ue->id] = array(
1186 'text' => $ue->enrolmentinstancename,
1187 'period' => $period,
1188 'dimmed' => ($periodoutside or $ue->status != ENROL_USER_ACTIVE or $ue->enrolmentinstance->status != ENROL_INSTANCE_ENABLED),
1189 'actions' => $ue->enrolmentplugin->get_user_enrolment_actions($manager, $ue)
1192 $userdetails[$user->id] = $details;
1194 return $userdetails;
1198 * Prepare a user record for display
1200 * This function is called by both {@link get_users_for_display} and {@link get_other_users_for_display} to correctly
1201 * prepare user fields for display
1203 * Please note that this function does not check capability for moodle/coures:viewhiddenuserfields
1205 * @param object $user The user record
1206 * @param array $extrafields The list of fields as returned from get_extra_user_fields used to determine which
1207 * additional fields may be displayed
1208 * @param int $now The time used for lastaccess calculation
1209 * @return array The fields to be displayed including userid, courseid, picture, firstname, lastcourseaccess, lastaccess and any
1210 * additional fields from $extrafields
1212 private function prepare_user_for_display($user, $extrafields, $now) {
1213 $details = array(
1214 'userid' => $user->id,
1215 'courseid' => $this->get_course()->id,
1216 'picture' => new user_picture($user),
1217 'userfullnamedisplay' => fullname($user, has_capability('moodle/site:viewfullnames', $this->get_context())),
1218 'lastaccess' => get_string('never'),
1219 'lastcourseaccess' => get_string('never'),
1222 foreach ($extrafields as $field) {
1223 $details[$field] = s($user->{$field});
1226 // Last time user has accessed the site.
1227 if (!empty($user->lastaccess)) {
1228 $details['lastaccess'] = format_time($now - $user->lastaccess);
1231 // Last time user has accessed the course.
1232 if (!empty($user->lastcourseaccess)) {
1233 $details['lastcourseaccess'] = format_time($now - $user->lastcourseaccess);
1235 return $details;
1238 public function get_manual_enrol_buttons() {
1239 $plugins = $this->get_enrolment_plugins(true); // Skip disabled plugins.
1240 $buttons = array();
1241 foreach ($plugins as $plugin) {
1242 $newbutton = $plugin->get_manual_enrol_button($this);
1243 if (is_array($newbutton)) {
1244 $buttons += $newbutton;
1245 } else if ($newbutton instanceof enrol_user_button) {
1246 $buttons[] = $newbutton;
1249 return $buttons;
1252 public function has_instance($enrolpluginname) {
1253 // Make sure manual enrolments instance exists
1254 foreach ($this->get_enrolment_instances() as $instance) {
1255 if ($instance->enrol == $enrolpluginname) {
1256 return true;
1259 return false;
1263 * Returns the enrolment plugin that the course manager was being filtered to.
1265 * If no filter was being applied then this function returns false.
1267 * @return enrol_plugin
1269 public function get_filtered_enrolment_plugin() {
1270 $instances = $this->get_enrolment_instances();
1271 $plugins = $this->get_enrolment_plugins(false);
1273 if (empty($this->instancefilter) || !array_key_exists($this->instancefilter, $instances)) {
1274 return false;
1277 $instance = $instances[$this->instancefilter];
1278 return $plugins[$instance->enrol];
1282 * Returns and array of users + enrolment details.
1284 * Given an array of user id's this function returns and array of user enrolments for those users
1285 * as well as enough user information to display the users name and picture for each enrolment.
1287 * @global moodle_database $DB
1288 * @param array $userids
1289 * @return array
1291 public function get_users_enrolments(array $userids) {
1292 global $DB;
1294 $instances = $this->get_enrolment_instances();
1295 $plugins = $this->get_enrolment_plugins(false);
1297 if (!empty($this->instancefilter)) {
1298 $instancesql = ' = :instanceid';
1299 $instanceparams = array('instanceid' => $this->instancefilter);
1300 } else {
1301 list($instancesql, $instanceparams) = $DB->get_in_or_equal(array_keys($instances), SQL_PARAMS_NAMED, 'instanceid0000');
1304 $userfields = user_picture::fields('u');
1305 list($idsql, $idparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'userid0000');
1307 list($sort, $sortparams) = users_order_by_sql('u');
1309 $sql = "SELECT ue.id AS ueid, ue.status, ue.enrolid, ue.userid, ue.timestart, ue.timeend, ue.modifierid, ue.timecreated, ue.timemodified, $userfields
1310 FROM {user_enrolments} ue
1311 LEFT JOIN {user} u ON u.id = ue.userid
1312 WHERE ue.enrolid $instancesql AND
1313 u.id $idsql
1314 ORDER BY $sort";
1316 $rs = $DB->get_recordset_sql($sql, $idparams + $instanceparams + $sortparams);
1317 $users = array();
1318 foreach ($rs as $ue) {
1319 $user = user_picture::unalias($ue);
1320 $ue->id = $ue->ueid;
1321 unset($ue->ueid);
1322 if (!array_key_exists($user->id, $users)) {
1323 $user->enrolments = array();
1324 $users[$user->id] = $user;
1326 $ue->enrolmentinstance = $instances[$ue->enrolid];
1327 $ue->enrolmentplugin = $plugins[$ue->enrolmentinstance->enrol];
1328 $users[$user->id]->enrolments[$ue->id] = $ue;
1330 $rs->close();
1331 return $users;
1336 * A button that is used to enrol users in a course
1338 * @copyright 2010 Sam Hemelryk
1339 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1341 class enrol_user_button extends single_button {
1344 * An array containing JS YUI modules required by this button
1345 * @var array
1347 protected $jsyuimodules = array();
1350 * An array containing JS initialisation calls required by this button
1351 * @var array
1353 protected $jsinitcalls = array();
1356 * An array strings required by JS for this button
1357 * @var array
1359 protected $jsstrings = array();
1362 * Initialises the new enrol_user_button
1364 * @staticvar int $count The number of enrol user buttons already created
1365 * @param moodle_url $url
1366 * @param string $label The text to display in the button
1367 * @param string $method Either post or get
1369 public function __construct(moodle_url $url, $label, $method = 'post') {
1370 static $count = 0;
1371 $count ++;
1372 parent::__construct($url, $label, $method);
1373 $this->class = 'singlebutton enrolusersbutton';
1374 $this->formid = 'enrolusersbutton-'.$count;
1378 * Adds a YUI module call that will be added to the page when the button is used.
1380 * @param string|array $modules One or more modules to require
1381 * @param string $function The JS function to call
1382 * @param array $arguments An array of arguments to pass to the function
1383 * @param string $galleryversion Deprecated: The gallery version to use
1384 * @param bool $ondomready If true the call is postponed until the DOM is finished loading
1386 public function require_yui_module($modules, $function, array $arguments = null, $galleryversion = null, $ondomready = false) {
1387 if ($galleryversion != null) {
1388 debugging('The galleryversion parameter to yui_module has been deprecated since Moodle 2.3.', DEBUG_DEVELOPER);
1391 $js = new stdClass;
1392 $js->modules = (array)$modules;
1393 $js->function = $function;
1394 $js->arguments = $arguments;
1395 $js->ondomready = $ondomready;
1396 $this->jsyuimodules[] = $js;
1400 * Adds a JS initialisation call to the page when the button is used.
1402 * @param string $function The function to call
1403 * @param array $extraarguments An array of arguments to pass to the function
1404 * @param bool $ondomready If true the call is postponed until the DOM is finished loading
1405 * @param array $module A module definition
1407 public function require_js_init_call($function, array $extraarguments = null, $ondomready = false, array $module = null) {
1408 $js = new stdClass;
1409 $js->function = $function;
1410 $js->extraarguments = $extraarguments;
1411 $js->ondomready = $ondomready;
1412 $js->module = $module;
1413 $this->jsinitcalls[] = $js;
1417 * Requires strings for JS that will be loaded when the button is used.
1419 * @param type $identifiers
1420 * @param string $component
1421 * @param mixed $a
1423 public function strings_for_js($identifiers, $component = 'moodle', $a = null) {
1424 $string = new stdClass;
1425 $string->identifiers = (array)$identifiers;
1426 $string->component = $component;
1427 $string->a = $a;
1428 $this->jsstrings[] = $string;
1432 * Initialises the JS that is required by this button
1434 * @param moodle_page $page
1436 public function initialise_js(moodle_page $page) {
1437 foreach ($this->jsyuimodules as $js) {
1438 $page->requires->yui_module($js->modules, $js->function, $js->arguments, null, $js->ondomready);
1440 foreach ($this->jsinitcalls as $js) {
1441 $page->requires->js_init_call($js->function, $js->extraarguments, $js->ondomready, $js->module);
1443 foreach ($this->jsstrings as $string) {
1444 $page->requires->strings_for_js($string->identifiers, $string->component, $string->a);
1450 * User enrolment action
1452 * This class is used to manage a renderable ue action such as editing an user enrolment or deleting
1453 * a user enrolment.
1455 * @copyright 2011 Sam Hemelryk
1456 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1458 class user_enrolment_action implements renderable {
1461 * The icon to display for the action
1462 * @var pix_icon
1464 protected $icon;
1467 * The title for the action
1468 * @var string
1470 protected $title;
1473 * The URL to the action
1474 * @var moodle_url
1476 protected $url;
1479 * An array of HTML attributes
1480 * @var array
1482 protected $attributes = array();
1485 * Constructor
1486 * @param pix_icon $icon
1487 * @param string $title
1488 * @param moodle_url $url
1489 * @param array $attributes
1491 public function __construct(pix_icon $icon, $title, $url, array $attributes = null) {
1492 $this->icon = $icon;
1493 $this->title = $title;
1494 $this->url = new moodle_url($url);
1495 if (!empty($attributes)) {
1496 $this->attributes = $attributes;
1498 $this->attributes['title'] = $title;
1502 * Returns the icon for this action
1503 * @return pix_icon
1505 public function get_icon() {
1506 return $this->icon;
1510 * Returns the title for this action
1511 * @return string
1513 public function get_title() {
1514 return $this->title;
1518 * Returns the URL for this action
1519 * @return moodle_url
1521 public function get_url() {
1522 return $this->url;
1526 * Returns the attributes to use for this action
1527 * @return array
1529 public function get_attributes() {
1530 return $this->attributes;
1534 class enrol_ajax_exception extends moodle_exception {
1536 * Constructor
1537 * @param string $errorcode The name of the string from error.php to print
1538 * @param string $module name of module
1539 * @param string $link The url where the user will be prompted to continue. If no url is provided the user will be directed to the site index page.
1540 * @param object $a Extra words and phrases that might be required in the error string
1541 * @param string $debuginfo optional debugging information
1543 public function __construct($errorcode, $link = '', $a = NULL, $debuginfo = null) {
1544 parent::__construct($errorcode, 'enrol', $link, $a, $debuginfo);
1549 * This class is used to manage a bulk operations for enrolment plugins.
1551 * @copyright 2011 Sam Hemelryk
1552 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1554 abstract class enrol_bulk_enrolment_operation {
1557 * The course enrolment manager
1558 * @var course_enrolment_manager
1560 protected $manager;
1563 * The enrolment plugin to which this operation belongs
1564 * @var enrol_plugin
1566 protected $plugin;
1569 * Contructor
1570 * @param course_enrolment_manager $manager
1571 * @param stdClass $plugin
1573 public function __construct(course_enrolment_manager $manager, enrol_plugin $plugin = null) {
1574 $this->manager = $manager;
1575 $this->plugin = $plugin;
1579 * Returns a moodleform used for this operation, or false if no form is required and the action
1580 * should be immediatly processed.
1582 * @param moodle_url|string $defaultaction
1583 * @param mixed $defaultcustomdata
1584 * @return enrol_bulk_enrolment_change_form|moodleform|false
1586 public function get_form($defaultaction = null, $defaultcustomdata = null) {
1587 return false;
1591 * Returns the title to use for this bulk operation
1593 * @return string
1595 abstract public function get_title();
1598 * Returns the identifier for this bulk operation.
1599 * This should be the same identifier used by the plugins function when returning
1600 * all of its bulk operations.
1602 * @return string
1604 abstract public function get_identifier();
1607 * Processes the bulk operation on the given users
1609 * @param course_enrolment_manager $manager
1610 * @param array $users
1611 * @param stdClass $properties
1613 abstract public function process(course_enrolment_manager $manager, array $users, stdClass $properties);