See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * 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. 20 * 21 * @package core_enrol 22 * @copyright 2010 Sam Hemelryk 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 /** 29 * This class provides a targeted tied together means of interfacing the enrolment 30 * tasks together with a course. 31 * 32 * It is provided as a convenience more than anything else. 33 * 34 * @copyright 2010 Sam Hemelryk 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class course_enrolment_manager { 38 39 /** 40 * The course context 41 * @var context 42 */ 43 protected $context; 44 /** 45 * The course we are managing enrolments for 46 * @var stdClass 47 */ 48 protected $course = null; 49 /** 50 * Limits the focus of the manager to one enrolment plugin instance 51 * @var string 52 */ 53 protected $instancefilter = null; 54 /** 55 * Limits the focus of the manager to users with specified role 56 * @var int 57 */ 58 protected $rolefilter = 0; 59 /** 60 * Limits the focus of the manager to users who match search string 61 * @var string 62 */ 63 protected $searchfilter = ''; 64 /** 65 * Limits the focus of the manager to users in specified group 66 * @var int 67 */ 68 protected $groupfilter = 0; 69 /** 70 * Limits the focus of the manager to users who match status active/inactive 71 * @var int 72 */ 73 protected $statusfilter = -1; 74 75 /** 76 * The total number of users enrolled in the course 77 * Populated by course_enrolment_manager::get_total_users 78 * @var int 79 */ 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 85 */ 86 protected $users = array(); 87 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 92 */ 93 protected $otherusers = array(); 94 95 /** 96 * The total number of users who hold a role within the course but who 97 * arn't enrolled. 98 * @var int 99 */ 100 protected $totalotherusers = null; 101 102 /** 103 * The current moodle_page object 104 * @var moodle_page 105 */ 106 protected $moodlepage = null; 107 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 113 */ 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 /**#@-*/ 125 126 /** 127 * Constructs the course enrolment manager 128 * 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. 136 */ 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; 147 } 148 149 /** 150 * Returns the current moodle page 151 * @return moodle_page 152 */ 153 public function get_moodlepage() { 154 return $this->moodlepage; 155 } 156 157 /** 158 * Returns the total number of enrolled users in the course. 159 * 160 * If a filter was specificed this will be the total number of users enrolled 161 * in this course by means of that instance. 162 * 163 * @global moodle_database $DB 164 * @return int 165 */ 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)"; 179 } 180 $sqltotal .= "WHERE $filtersql"; 181 $this->totalusers = (int)$DB->count_records_sql($sqltotal, $params); 182 } 183 return $this->totalusers; 184 } 185 186 /** 187 * Returns the total number of enrolled users in the course. 188 * 189 * If a filter was specificed this will be the total number of users enrolled 190 * in this course by means of that instance. 191 * 192 * @global moodle_database $DB 193 * @return int 194 */ 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); 213 } 214 return $this->totalotherusers; 215 } 216 217 /** 218 * Gets all of the users enrolled in this course. 219 * 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. 223 * 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 230 */ 231 public function get_users($sort, $direction='ASC', $page=0, $perpage=25) { 232 global $DB; 233 if ($direction !== 'ASC') { 234 $direction = 'DESC'; 235 } 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)"; 252 } 253 $sql .= "WHERE $filtersql 254 ORDER BY $sort $direction"; 255 $this->users[$key] = $DB->get_records_sql($sql, $params, $page*$perpage, $perpage); 256 } 257 return $this->users[$key]; 258 } 259 260 /** 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). 264 * 265 * @return array Two-element array with SQL and params for WHERE clause 266 */ 267 protected function get_filter_sql() { 268 global $DB; 269 270 // Search condition. 271 $extrafields = get_extra_user_fields($this->get_context()); 272 list($sql, $params) = users_search_sql($this->searchfilter, 'u', true, $extrafields); 273 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; 281 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; 286 } 287 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; 296 } 297 } 298 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); 316 } 317 318 return array($sql, $params); 319 } 320 321 /** 322 * Gets and array of other users. 323 * 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 326 * 327 * @global moodle_database $DB 328 * @param string $sort 329 * @param string $direction 330 * @param int $page 331 * @param int $perpage 332 * @return array 333 */ 334 public function get_other_users($sort, $direction='ASC', $page=0, $perpage=25) { 335 global $DB; 336 if ($direction !== 'ASC') { 337 $direction = 'DESC'; 338 } 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); 361 } 362 return $this->otherusers[$key]; 363 } 364 365 /** 366 * Helper method used by {@link get_potential_users()} and {@link search_other_users()}. 367 * 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. 374 */ 375 protected function get_basic_search_conditions($search, $searchanywhere) { 376 global $DB, $CFG; 377 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; 385 } 386 $conditions[] = $DB->sql_fullname('u.firstname', 'u.lastname'); 387 if ($searchanywhere) { 388 $searchparam = '%' . $search . '%'; 389 } else { 390 $searchparam = $search . '%'; 391 } 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++; 397 } 398 $tests[] = '(' . implode(' OR ', $conditions) . ')'; 399 } 400 $wherecondition = implode(' AND ', $tests); 401 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); 407 408 return array($ufields, $params, $wherecondition); 409 } 410 411 /** 412 * Helper method used by {@link get_potential_users()} and {@link search_other_users()}. 413 * 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 428 */ 429 protected function execute_search_queries($search, $fields, $countfields, $sql, array $params, $page, $perpage, 430 $addedenrollment = 0, $returnexactcount = false) { 431 global $DB, $CFG; 432 433 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->get_context()); 434 $order = ' ORDER BY ' . $sort; 435 436 $totalusers = 0; 437 $moreusers = false; 438 $results = []; 439 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; 445 446 if ($moreusers) { 447 // We need to discard the last record. 448 array_pop($availableusers); 449 } 450 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); 454 } 455 } 456 457 $results['users'] = $availableusers; 458 $results['moreusers'] = $moreusers; 459 460 if ($returnexactcount) { 461 // Include totalusers in result if $returnexactcount flag is true. 462 $results['totalusers'] = $totalusers; 463 } 464 465 return $results; 466 } 467 468 /** 469 * Gets an array of the users that can be enrolled in this course. 470 * 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 484 */ 485 public function get_potential_users($enrolid, $search = '', $searchanywhere = false, $page = 0, $perpage = 25, 486 $addedenrollment = 0, $returnexactcount = false) { 487 global $DB; 488 489 list($ufields, $params, $wherecondition) = $this->get_basic_search_conditions($search, $searchanywhere); 490 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; 498 499 return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, $addedenrollment, 500 $returnexactcount); 501 } 502 503 /** 504 * Searches other users and returns paginated results 505 * 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 517 */ 518 public function search_other_users($search = '', $searchanywhere = false, $page = 0, $perpage = 25, $returnexactcount = false) { 519 global $DB, $CFG; 520 521 list($ufields, $params, $wherecondition) = $this->get_basic_search_conditions($search, $searchanywhere); 522 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; 530 531 return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, 0, $returnexactcount); 532 } 533 534 /** 535 * Searches through the enrolled users in this course. 536 * 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. 546 */ 547 public function search_users(string $search = '', bool $searchanywhere = false, int $page = 0, int $perpage = 25, 548 bool $returnexactcount = false) { 549 global $USER; 550 551 list($ufields, $params, $wherecondition) = $this->get_basic_search_conditions($search, $searchanywhere); 552 553 $groupmode = groups_get_course_groupmode($this->course); 554 if ($groupmode == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $this->context)) { 555 $groups = groups_get_all_groups($this->course->id, $USER->id, 0, 'g.id'); 556 $groupids = array_column($groups, 'id'); 557 } else { 558 $groupids = []; 559 } 560 561 [$enrolledsql, $enrolledparams] = get_enrolled_sql($this->context, '', $groupids); 562 563 $fields = 'SELECT ' . $ufields; 564 $countfields = 'SELECT COUNT(u.id)'; 565 $sql = " FROM {user} u 566 JOIN ($enrolledsql) je ON je.id = u.id 567 WHERE $wherecondition"; 568 569 $params = array_merge($params, $enrolledparams); 570 571 return $this->execute_search_queries($search, $fields, $countfields, $sql, $params, $page, $perpage, 0, $returnexactcount); 572 } 573 574 /** 575 * Gets an array containing some SQL to user for when selecting, params for 576 * that SQL, and the filter that was used in constructing the sql. 577 * 578 * @global moodle_database $DB 579 * @return string 580 */ 581 protected function get_instance_sql() { 582 global $DB; 583 if ($this->_instancessql === null) { 584 $instances = $this->get_enrolment_instances(); 585 $filter = $this->get_enrolment_filter(); 586 if ($filter && array_key_exists($filter, $instances)) { 587 $sql = " = :ifilter"; 588 $params = array('ifilter'=>$filter); 589 } else { 590 $filter = 0; 591 if ($instances) { 592 list($sql, $params) = $DB->get_in_or_equal(array_keys($this->get_enrolment_instances()), SQL_PARAMS_NAMED); 593 } else { 594 // no enabled instances, oops, we should probably say something 595 $sql = "= :never"; 596 $params = array('never'=>-1); 597 } 598 } 599 $this->instancefilter = $filter; 600 $this->_instancessql = array($sql, $params, $filter); 601 } 602 return $this->_instancessql; 603 } 604 605 /** 606 * Returns all of the enrolment instances for this course. 607 * 608 * @param bool $onlyenabled Whether to return data from enabled enrolment instance names only. 609 * @return array 610 */ 611 public function get_enrolment_instances($onlyenabled = false) { 612 if ($this->_instances === null) { 613 $this->_instances = enrol_get_instances($this->course->id, $onlyenabled); 614 } 615 return $this->_instances; 616 } 617 618 /** 619 * Returns the names for all of the enrolment instances for this course. 620 * 621 * @param bool $onlyenabled Whether to return data from enabled enrolment instance names only. 622 * @return array 623 */ 624 public function get_enrolment_instance_names($onlyenabled = false) { 625 if ($this->_inames === null) { 626 $instances = $this->get_enrolment_instances($onlyenabled); 627 $plugins = $this->get_enrolment_plugins(false); 628 foreach ($instances as $key=>$instance) { 629 if (!isset($plugins[$instance->enrol])) { 630 // weird, some broken stuff in plugin 631 unset($instances[$key]); 632 continue; 633 } 634 $this->_inames[$key] = $plugins[$instance->enrol]->get_instance_name($instance); 635 } 636 } 637 return $this->_inames; 638 } 639 640 /** 641 * Gets all of the enrolment plugins that are available for this course. 642 * 643 * @param bool $onlyenabled return only enabled enrol plugins 644 * @return array 645 */ 646 public function get_enrolment_plugins($onlyenabled = true) { 647 if ($this->_plugins === null) { 648 $this->_plugins = enrol_get_plugins(true); 649 } 650 651 if ($onlyenabled) { 652 return $this->_plugins; 653 } 654 655 if ($this->_allplugins === null) { 656 // Make sure we have the same objects in _allplugins and _plugins. 657 $this->_allplugins = $this->_plugins; 658 foreach (enrol_get_plugins(false) as $name=>$plugin) { 659 if (!isset($this->_allplugins[$name])) { 660 $this->_allplugins[$name] = $plugin; 661 } 662 } 663 } 664 665 return $this->_allplugins; 666 } 667 668 /** 669 * Gets all of the roles this course can contain. 670 * 671 * @return array 672 */ 673 public function get_all_roles() { 674 if ($this->_roles === null) { 675 $this->_roles = role_fix_names(get_all_roles($this->context), $this->context); 676 } 677 return $this->_roles; 678 } 679 680 /** 681 * Gets all of the roles this course can contain. 682 * 683 * @return array 684 */ 685 public function get_viewable_roles() { 686 if ($this->_visibleroles === null) { 687 $this->_visibleroles = get_viewable_roles($this->context); 688 } 689 return $this->_visibleroles; 690 } 691 692 /** 693 * Gets all of the assignable roles for this course. 694 * 695 * @return array 696 */ 697 public function get_assignable_roles($otherusers = false) { 698 if ($this->_assignableroles === null) { 699 $this->_assignableroles = get_assignable_roles($this->context, ROLENAME_ALIAS, false); // verifies unassign access control too 700 } 701 702 if ($otherusers) { 703 if (!is_array($this->_assignablerolesothers)) { 704 $this->_assignablerolesothers = array(); 705 list($courseviewroles, $ignored) = get_roles_with_cap_in_context($this->context, 'moodle/course:view'); 706 foreach ($this->_assignableroles as $roleid=>$role) { 707 if (isset($courseviewroles[$roleid])) { 708 $this->_assignablerolesothers[$roleid] = $role; 709 } 710 } 711 } 712 return $this->_assignablerolesothers; 713 } else { 714 return $this->_assignableroles; 715 } 716 } 717 718 /** 719 * Gets all of the assignable roles for this course, wrapped in an array to ensure 720 * role sort order is not lost during json deserialisation. 721 * 722 * @param boolean $otherusers whether to include the assignable roles for other users 723 * @return array 724 */ 725 public function get_assignable_roles_for_json($otherusers = false) { 726 $rolesarray = array(); 727 $assignable = $this->get_assignable_roles($otherusers); 728 foreach ($assignable as $id => $role) { 729 $rolesarray[] = array('id' => $id, 'name' => $role); 730 } 731 return $rolesarray; 732 } 733 734 /** 735 * Gets all of the groups for this course. 736 * 737 * @return array 738 */ 739 public function get_all_groups() { 740 if ($this->_groups === null) { 741 $this->_groups = groups_get_all_groups($this->course->id); 742 foreach ($this->_groups as $gid=>$group) { 743 $this->_groups[$gid]->name = format_string($group->name); 744 } 745 } 746 return $this->_groups; 747 } 748 749 /** 750 * Unenrols a user from the course given the users ue entry 751 * 752 * @global moodle_database $DB 753 * @param stdClass $ue 754 * @return bool 755 */ 756 public function unenrol_user($ue) { 757 global $DB; 758 list ($instance, $plugin) = $this->get_user_enrolment_components($ue); 759 if ($instance && $plugin && $plugin->allow_unenrol_user($instance, $ue) && has_capability("enrol/$instance->enrol:unenrol", $this->context)) { 760 $plugin->unenrol_user($instance, $ue->userid); 761 return true; 762 } 763 return false; 764 } 765 766 /** 767 * Given a user enrolment record this method returns the plugin and enrolment 768 * instance that relate to it. 769 * 770 * @param stdClass|int $userenrolment 771 * @return array array($instance, $plugin) 772 */ 773 public function get_user_enrolment_components($userenrolment) { 774 global $DB; 775 if (is_numeric($userenrolment)) { 776 $userenrolment = $DB->get_record('user_enrolments', array('id'=>(int)$userenrolment)); 777 } 778 $instances = $this->get_enrolment_instances(); 779 $plugins = $this->get_enrolment_plugins(false); 780 if (!$userenrolment || !isset($instances[$userenrolment->enrolid])) { 781 return array(false, false); 782 } 783 $instance = $instances[$userenrolment->enrolid]; 784 $plugin = $plugins[$instance->enrol]; 785 return array($instance, $plugin); 786 } 787 788 /** 789 * Removes an assigned role from a user. 790 * 791 * @global moodle_database $DB 792 * @param int $userid 793 * @param int $roleid 794 * @return bool 795 */ 796 public function unassign_role_from_user($userid, $roleid) { 797 global $DB; 798 // Admins may unassign any role, others only those they could assign. 799 if (!is_siteadmin() and !array_key_exists($roleid, $this->get_assignable_roles())) { 800 if (defined('AJAX_SCRIPT')) { 801 throw new moodle_exception('invalidrole'); 802 } 803 return false; 804 } 805 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST); 806 $ras = $DB->get_records('role_assignments', array('contextid'=>$this->context->id, 'userid'=>$user->id, 'roleid'=>$roleid)); 807 foreach ($ras as $ra) { 808 if ($ra->component) { 809 if (strpos($ra->component, 'enrol_') !== 0) { 810 continue; 811 } 812 if (!$plugin = enrol_get_plugin(substr($ra->component, 6))) { 813 continue; 814 } 815 if ($plugin->roles_protected()) { 816 continue; 817 } 818 } 819 role_unassign($ra->roleid, $ra->userid, $ra->contextid, $ra->component, $ra->itemid); 820 } 821 return true; 822 } 823 824 /** 825 * Assigns a role to a user. 826 * 827 * @param int $roleid 828 * @param int $userid 829 * @return int|false 830 */ 831 public function assign_role_to_user($roleid, $userid) { 832 require_capability('moodle/role:assign', $this->context); 833 if (!array_key_exists($roleid, $this->get_assignable_roles())) { 834 if (defined('AJAX_SCRIPT')) { 835 throw new moodle_exception('invalidrole'); 836 } 837 return false; 838 } 839 return role_assign($roleid, $userid, $this->context->id, '', NULL); 840 } 841 842 /** 843 * Adds a user to a group 844 * 845 * @param stdClass $user 846 * @param int $groupid 847 * @return bool 848 */ 849 public function add_user_to_group($user, $groupid) { 850 require_capability('moodle/course:managegroups', $this->context); 851 $group = $this->get_group($groupid); 852 if (!$group) { 853 return false; 854 } 855 return groups_add_member($group->id, $user->id); 856 } 857 858 /** 859 * Removes a user from a group 860 * 861 * @global moodle_database $DB 862 * @param StdClass $user 863 * @param int $groupid 864 * @return bool 865 */ 866 public function remove_user_from_group($user, $groupid) { 867 global $DB; 868 require_capability('moodle/course:managegroups', $this->context); 869 $group = $this->get_group($groupid); 870 if (!groups_remove_member_allowed($group, $user)) { 871 return false; 872 } 873 if (!$group) { 874 return false; 875 } 876 return groups_remove_member($group, $user); 877 } 878 879 /** 880 * Gets the requested group 881 * 882 * @param int $groupid 883 * @return stdClass|int 884 */ 885 public function get_group($groupid) { 886 $groups = $this->get_all_groups(); 887 if (!array_key_exists($groupid, $groups)) { 888 return false; 889 } 890 return $groups[$groupid]; 891 } 892 893 /** 894 * Edits an enrolment 895 * 896 * @param stdClass $userenrolment 897 * @param stdClass $data 898 * @return bool 899 */ 900 public function edit_enrolment($userenrolment, $data) { 901 //Only allow editing if the user has the appropriate capability 902 //Already checked in /user/index.php but checking again in case this function is called from elsewhere 903 list($instance, $plugin) = $this->get_user_enrolment_components($userenrolment); 904 if ($instance && $plugin && $plugin->allow_manage($instance) && has_capability("enrol/$instance->enrol:manage", $this->context)) { 905 if (!isset($data->status)) { 906 $data->status = $userenrolment->status; 907 } 908 $plugin->update_user_enrol($instance, $userenrolment->userid, $data->status, $data->timestart, $data->timeend); 909 return true; 910 } 911 return false; 912 } 913 914 /** 915 * Returns the current enrolment filter that is being applied by this class 916 * @return string 917 */ 918 public function get_enrolment_filter() { 919 return $this->instancefilter; 920 } 921 922 /** 923 * Gets the roles assigned to this user that are applicable for this course. 924 * 925 * @param int $userid 926 * @return array 927 */ 928 public function get_user_roles($userid) { 929 $roles = array(); 930 $ras = get_user_roles($this->context, $userid, true, 'c.contextlevel DESC, r.sortorder ASC'); 931 $plugins = $this->get_enrolment_plugins(false); 932 foreach ($ras as $ra) { 933 if ($ra->contextid != $this->context->id) { 934 if (!array_key_exists($ra->roleid, $roles)) { 935 $roles[$ra->roleid] = null; 936 } 937 // higher ras, course always takes precedence 938 continue; 939 } 940 if (array_key_exists($ra->roleid, $roles) && $roles[$ra->roleid] === false) { 941 continue; 942 } 943 $changeable = true; 944 if ($ra->component) { 945 $changeable = false; 946 if (strpos($ra->component, 'enrol_') === 0) { 947 $plugin = substr($ra->component, 6); 948 if (isset($plugins[$plugin])) { 949 $changeable = !$plugins[$plugin]->roles_protected(); 950 } 951 } 952 } 953 954 $roles[$ra->roleid] = $changeable; 955 } 956 return $roles; 957 } 958 959 /** 960 * Gets the enrolments this user has in the course - including all suspended plugins and instances. 961 * 962 * @global moodle_database $DB 963 * @param int $userid 964 * @return array 965 */ 966 public function get_user_enrolments($userid) { 967 global $DB; 968 list($instancessql, $params, $filter) = $this->get_instance_sql(); 969 $params['userid'] = $userid; 970 $userenrolments = $DB->get_records_select('user_enrolments', "enrolid $instancessql AND userid = :userid", $params); 971 $instances = $this->get_enrolment_instances(); 972 $plugins = $this->get_enrolment_plugins(false); 973 $inames = $this->get_enrolment_instance_names(); 974 foreach ($userenrolments as &$ue) { 975 $ue->enrolmentinstance = $instances[$ue->enrolid]; 976 $ue->enrolmentplugin = $plugins[$ue->enrolmentinstance->enrol]; 977 $ue->enrolmentinstancename = $inames[$ue->enrolmentinstance->id]; 978 } 979 return $userenrolments; 980 } 981 982 /** 983 * Gets the groups this user belongs to 984 * 985 * @param int $userid 986 * @return array 987 */ 988 public function get_user_groups($userid) { 989 return groups_get_all_groups($this->course->id, $userid, 0, 'g.id'); 990 } 991 992 /** 993 * Retursn an array of params that would go into the URL to return to this 994 * exact page. 995 * 996 * @return array 997 */ 998 public function get_url_params() { 999 $args = array( 1000 'id' => $this->course->id 1001 ); 1002 if (!empty($this->instancefilter)) { 1003 $args['ifilter'] = $this->instancefilter; 1004 } 1005 if (!empty($this->rolefilter)) { 1006 $args['role'] = $this->rolefilter; 1007 } 1008 if ($this->searchfilter !== '') { 1009 $args['search'] = $this->searchfilter; 1010 } 1011 if (!empty($this->groupfilter)) { 1012 $args['filtergroup'] = $this->groupfilter; 1013 } 1014 if ($this->statusfilter !== -1) { 1015 $args['status'] = $this->statusfilter; 1016 } 1017 return $args; 1018 } 1019 1020 /** 1021 * Returns the course this object is managing enrolments for 1022 * 1023 * @return stdClass 1024 */ 1025 public function get_course() { 1026 return $this->course; 1027 } 1028 1029 /** 1030 * Returns the course context 1031 * 1032 * @return context 1033 */ 1034 public function get_context() { 1035 return $this->context; 1036 } 1037 1038 /** 1039 * Gets an array of other users in this course ready for display. 1040 * 1041 * Other users are users who have been assigned or inherited roles within this 1042 * course but have not been enrolled. 1043 * 1044 * @param core_enrol_renderer $renderer 1045 * @param moodle_url $pageurl 1046 * @param string $sort 1047 * @param string $direction ASC | DESC 1048 * @param int $page Starting from 0 1049 * @param int $perpage 1050 * @return array 1051 */ 1052 public function get_other_users_for_display(core_enrol_renderer $renderer, moodle_url $pageurl, $sort, $direction, $page, $perpage) { 1053 1054 $userroles = $this->get_other_users($sort, $direction, $page, $perpage); 1055 $roles = $this->get_all_roles(); 1056 $plugins = $this->get_enrolment_plugins(false); 1057 1058 $context = $this->get_context(); 1059 $now = time(); 1060 $extrafields = get_extra_user_fields($context); 1061 1062 $users = array(); 1063 foreach ($userroles as $userrole) { 1064 $contextid = $userrole->contextid; 1065 unset($userrole->contextid); // This would collide with user avatar. 1066 if (!array_key_exists($userrole->id, $users)) { 1067 $users[$userrole->id] = $this->prepare_user_for_display($userrole, $extrafields, $now); 1068 } 1069 $a = new stdClass; 1070 $a->role = $roles[$userrole->roleid]->localname; 1071 if ($contextid == $this->context->id) { 1072 $changeable = true; 1073 if ($userrole->component) { 1074 $changeable = false; 1075 if (strpos($userrole->component, 'enrol_') === 0) { 1076 $plugin = substr($userrole->component, 6); 1077 if (isset($plugins[$plugin])) { 1078 $changeable = !$plugins[$plugin]->roles_protected(); 1079 } 1080 } 1081 } 1082 $roletext = get_string('rolefromthiscourse', 'enrol', $a); 1083 } else { 1084 $changeable = false; 1085 switch ($userrole->contextlevel) { 1086 case CONTEXT_COURSE : 1087 // Meta course 1088 $roletext = get_string('rolefrommetacourse', 'enrol', $a); 1089 break; 1090 case CONTEXT_COURSECAT : 1091 $roletext = get_string('rolefromcategory', 'enrol', $a); 1092 break; 1093 case CONTEXT_SYSTEM: 1094 default: 1095 $roletext = get_string('rolefromsystem', 'enrol', $a); 1096 break; 1097 } 1098 } 1099 if (!isset($users[$userrole->id]['roles'])) { 1100 $users[$userrole->id]['roles'] = array(); 1101 } 1102 $users[$userrole->id]['roles'][$userrole->roleid] = array( 1103 'text' => $roletext, 1104 'unchangeable' => !$changeable 1105 ); 1106 } 1107 return $users; 1108 } 1109 1110 /** 1111 * Gets an array of users for display, this includes minimal user information 1112 * as well as minimal information on the users roles, groups, and enrolments. 1113 * 1114 * @param core_enrol_renderer $renderer 1115 * @param moodle_url $pageurl 1116 * @param int $sort 1117 * @param string $direction ASC or DESC 1118 * @param int $page 1119 * @param int $perpage 1120 * @return array 1121 */ 1122 public function get_users_for_display(course_enrolment_manager $manager, $sort, $direction, $page, $perpage) { 1123 $pageurl = $manager->get_moodlepage()->url; 1124 $users = $this->get_users($sort, $direction, $page, $perpage); 1125 1126 $now = time(); 1127 $straddgroup = get_string('addgroup', 'group'); 1128 $strunenrol = get_string('unenrol', 'enrol'); 1129 $stredit = get_string('edit'); 1130 1131 $visibleroles = $this->get_viewable_roles(); 1132 $assignable = $this->get_assignable_roles(); 1133 $allgroups = $this->get_all_groups(); 1134 $context = $this->get_context(); 1135 $canmanagegroups = has_capability('moodle/course:managegroups', $context); 1136 1137 $url = new moodle_url($pageurl, $this->get_url_params()); 1138 $extrafields = get_extra_user_fields($context); 1139 1140 $enabledplugins = $this->get_enrolment_plugins(true); 1141 1142 $userdetails = array(); 1143 foreach ($users as $user) { 1144 $details = $this->prepare_user_for_display($user, $extrafields, $now); 1145 1146 // Roles 1147 $details['roles'] = array(); 1148 foreach ($this->get_user_roles($user->id) as $rid=>$rassignable) { 1149 $unchangeable = !$rassignable; 1150 if (!is_siteadmin() and !isset($assignable[$rid])) { 1151 $unchangeable = true; 1152 } 1153 1154 if (isset($visibleroles[$rid])) { 1155 $label = $visibleroles[$rid]; 1156 } else { 1157 $label = get_string('novisibleroles', 'role'); 1158 $unchangeable = true; 1159 } 1160 1161 $details['roles'][$rid] = array('text' => $label, 'unchangeable' => $unchangeable); 1162 } 1163 1164 // Users 1165 $usergroups = $this->get_user_groups($user->id); 1166 $details['groups'] = array(); 1167 foreach($usergroups as $gid=>$unused) { 1168 $details['groups'][$gid] = $allgroups[$gid]->name; 1169 } 1170 1171 // Enrolments 1172 $details['enrolments'] = array(); 1173 foreach ($this->get_user_enrolments($user->id) as $ue) { 1174 if (!isset($enabledplugins[$ue->enrolmentinstance->enrol])) { 1175 $details['enrolments'][$ue->id] = array( 1176 'text' => $ue->enrolmentinstancename, 1177 'period' => null, 1178 'dimmed' => true, 1179 'actions' => array() 1180 ); 1181 continue; 1182 } else if ($ue->timestart and $ue->timeend) { 1183 $period = get_string('periodstartend', 'enrol', array('start'=>userdate($ue->timestart), 'end'=>userdate($ue->timeend))); 1184 $periodoutside = ($ue->timestart && $ue->timeend && ($now < $ue->timestart || $now > $ue->timeend)); 1185 } else if ($ue->timestart) { 1186 $period = get_string('periodstart', 'enrol', userdate($ue->timestart)); 1187 $periodoutside = ($ue->timestart && $now < $ue->timestart); 1188 } else if ($ue->timeend) { 1189 $period = get_string('periodend', 'enrol', userdate($ue->timeend)); 1190 $periodoutside = ($ue->timeend && $now > $ue->timeend); 1191 } else { 1192 // If there is no start or end show when user was enrolled. 1193 $period = get_string('periodnone', 'enrol', userdate($ue->timecreated)); 1194 $periodoutside = false; 1195 } 1196 $details['enrolments'][$ue->id] = array( 1197 'text' => $ue->enrolmentinstancename, 1198 'period' => $period, 1199 'dimmed' => ($periodoutside or $ue->status != ENROL_USER_ACTIVE or $ue->enrolmentinstance->status != ENROL_INSTANCE_ENABLED), 1200 'actions' => $ue->enrolmentplugin->get_user_enrolment_actions($manager, $ue) 1201 ); 1202 } 1203 $userdetails[$user->id] = $details; 1204 } 1205 return $userdetails; 1206 } 1207 1208 /** 1209 * Prepare a user record for display 1210 * 1211 * This function is called by both {@link get_users_for_display} and {@link get_other_users_for_display} to correctly 1212 * prepare user fields for display 1213 * 1214 * Please note that this function does not check capability for moodle/coures:viewhiddenuserfields 1215 * 1216 * @param object $user The user record 1217 * @param array $extrafields The list of fields as returned from get_extra_user_fields used to determine which 1218 * additional fields may be displayed 1219 * @param int $now The time used for lastaccess calculation 1220 * @return array The fields to be displayed including userid, courseid, picture, firstname, lastcourseaccess, lastaccess and any 1221 * additional fields from $extrafields 1222 */ 1223 private function prepare_user_for_display($user, $extrafields, $now) { 1224 $details = array( 1225 'userid' => $user->id, 1226 'courseid' => $this->get_course()->id, 1227 'picture' => new user_picture($user), 1228 'userfullnamedisplay' => fullname($user, has_capability('moodle/site:viewfullnames', $this->get_context())), 1229 'lastaccess' => get_string('never'), 1230 'lastcourseaccess' => get_string('never'), 1231 ); 1232 1233 foreach ($extrafields as $field) { 1234 $details[$field] = s($user->{$field}); 1235 } 1236 1237 // Last time user has accessed the site. 1238 if (!empty($user->lastaccess)) { 1239 $details['lastaccess'] = format_time($now - $user->lastaccess); 1240 } 1241 1242 // Last time user has accessed the course. 1243 if (!empty($user->lastcourseaccess)) { 1244 $details['lastcourseaccess'] = format_time($now - $user->lastcourseaccess); 1245 } 1246 return $details; 1247 } 1248 1249 public function get_manual_enrol_buttons() { 1250 $plugins = $this->get_enrolment_plugins(true); // Skip disabled plugins. 1251 $buttons = array(); 1252 foreach ($plugins as $plugin) { 1253 $newbutton = $plugin->get_manual_enrol_button($this); 1254 if (is_array($newbutton)) { 1255 $buttons += $newbutton; 1256 } else if ($newbutton instanceof enrol_user_button) { 1257 $buttons[] = $newbutton; 1258 } 1259 } 1260 return $buttons; 1261 } 1262 1263 public function has_instance($enrolpluginname) { 1264 // Make sure manual enrolments instance exists 1265 foreach ($this->get_enrolment_instances() as $instance) { 1266 if ($instance->enrol == $enrolpluginname) { 1267 return true; 1268 } 1269 } 1270 return false; 1271 } 1272 1273 /** 1274 * Returns the enrolment plugin that the course manager was being filtered to. 1275 * 1276 * If no filter was being applied then this function returns false. 1277 * 1278 * @return enrol_plugin 1279 */ 1280 public function get_filtered_enrolment_plugin() { 1281 $instances = $this->get_enrolment_instances(); 1282 $plugins = $this->get_enrolment_plugins(false); 1283 1284 if (empty($this->instancefilter) || !array_key_exists($this->instancefilter, $instances)) { 1285 return false; 1286 } 1287 1288 $instance = $instances[$this->instancefilter]; 1289 return $plugins[$instance->enrol]; 1290 } 1291 1292 /** 1293 * Returns and array of users + enrolment details. 1294 * 1295 * Given an array of user id's this function returns and array of user enrolments for those users 1296 * as well as enough user information to display the users name and picture for each enrolment. 1297 * 1298 * @global moodle_database $DB 1299 * @param array $userids 1300 * @return array 1301 */ 1302 public function get_users_enrolments(array $userids) { 1303 global $DB; 1304 1305 $instances = $this->get_enrolment_instances(); 1306 $plugins = $this->get_enrolment_plugins(false); 1307 1308 if (!empty($this->instancefilter)) { 1309 $instancesql = ' = :instanceid'; 1310 $instanceparams = array('instanceid' => $this->instancefilter); 1311 } else { 1312 list($instancesql, $instanceparams) = $DB->get_in_or_equal(array_keys($instances), SQL_PARAMS_NAMED, 'instanceid0000'); 1313 } 1314 1315 $userfields = user_picture::fields('u'); 1316 list($idsql, $idparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'userid0000'); 1317 1318 list($sort, $sortparams) = users_order_by_sql('u'); 1319 1320 $sql = "SELECT ue.id AS ueid, ue.status, ue.enrolid, ue.userid, ue.timestart, ue.timeend, ue.modifierid, ue.timecreated, ue.timemodified, $userfields 1321 FROM {user_enrolments} ue 1322 LEFT JOIN {user} u ON u.id = ue.userid 1323 WHERE ue.enrolid $instancesql AND 1324 u.id $idsql 1325 ORDER BY $sort"; 1326 1327 $rs = $DB->get_recordset_sql($sql, $idparams + $instanceparams + $sortparams); 1328 $users = array(); 1329 foreach ($rs as $ue) { 1330 $user = user_picture::unalias($ue); 1331 $ue->id = $ue->ueid; 1332 unset($ue->ueid); 1333 if (!array_key_exists($user->id, $users)) { 1334 $user->enrolments = array(); 1335 $users[$user->id] = $user; 1336 } 1337 $ue->enrolmentinstance = $instances[$ue->enrolid]; 1338 $ue->enrolmentplugin = $plugins[$ue->enrolmentinstance->enrol]; 1339 $users[$user->id]->enrolments[$ue->id] = $ue; 1340 } 1341 $rs->close(); 1342 return $users; 1343 } 1344 } 1345 1346 /** 1347 * A button that is used to enrol users in a course 1348 * 1349 * @copyright 2010 Sam Hemelryk 1350 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 1351 */ 1352 class enrol_user_button extends single_button { 1353 1354 /** 1355 * An array containing JS YUI modules required by this button 1356 * @var array 1357 */ 1358 protected $jsyuimodules = array(); 1359 1360 /** 1361 * An array containing JS initialisation calls required by this button 1362 * @var array 1363 */ 1364 protected $jsinitcalls = array(); 1365 1366 /** 1367 * An array strings required by JS for this button 1368 * @var array 1369 */ 1370 protected $jsstrings = array(); 1371 1372 /** 1373 * Initialises the new enrol_user_button 1374 * 1375 * @staticvar int $count The number of enrol user buttons already created 1376 * @param moodle_url $url 1377 * @param string $label The text to display in the button 1378 * @param string $method Either post or get 1379 */ 1380 public function __construct(moodle_url $url, $label, $method = 'post') { 1381 static $count = 0; 1382 $count ++; 1383 parent::__construct($url, $label, $method); 1384 $this->class = 'singlebutton enrolusersbutton'; 1385 $this->formid = 'enrolusersbutton-'.$count; 1386 } 1387 1388 /** 1389 * Adds a YUI module call that will be added to the page when the button is used. 1390 * 1391 * @param string|array $modules One or more modules to require 1392 * @param string $function The JS function to call 1393 * @param array $arguments An array of arguments to pass to the function 1394 * @param string $galleryversion Deprecated: The gallery version to use 1395 * @param bool $ondomready If true the call is postponed until the DOM is finished loading 1396 */ 1397 public function require_yui_module($modules, $function, array $arguments = null, $galleryversion = null, $ondomready = false) { 1398 if ($galleryversion != null) { 1399 debugging('The galleryversion parameter to yui_module has been deprecated since Moodle 2.3.', DEBUG_DEVELOPER); 1400 } 1401 1402 $js = new stdClass; 1403 $js->modules = (array)$modules; 1404 $js->function = $function; 1405 $js->arguments = $arguments; 1406 $js->ondomready = $ondomready; 1407 $this->jsyuimodules[] = $js; 1408 } 1409 1410 /** 1411 * Adds a JS initialisation call to the page when the button is used. 1412 * 1413 * @param string $function The function to call 1414 * @param array $extraarguments An array of arguments to pass to the function 1415 * @param bool $ondomready If true the call is postponed until the DOM is finished loading 1416 * @param array $module A module definition 1417 */ 1418 public function require_js_init_call($function, array $extraarguments = null, $ondomready = false, array $module = null) { 1419 $js = new stdClass; 1420 $js->function = $function; 1421 $js->extraarguments = $extraarguments; 1422 $js->ondomready = $ondomready; 1423 $js->module = $module; 1424 $this->jsinitcalls[] = $js; 1425 } 1426 1427 /** 1428 * Requires strings for JS that will be loaded when the button is used. 1429 * 1430 * @param type $identifiers 1431 * @param string $component 1432 * @param mixed $a 1433 */ 1434 public function strings_for_js($identifiers, $component = 'moodle', $a = null) { 1435 $string = new stdClass; 1436 $string->identifiers = (array)$identifiers; 1437 $string->component = $component; 1438 $string->a = $a; 1439 $this->jsstrings[] = $string; 1440 } 1441 1442 /** 1443 * Initialises the JS that is required by this button 1444 * 1445 * @param moodle_page $page 1446 */ 1447 public function initialise_js(moodle_page $page) { 1448 foreach ($this->jsyuimodules as $js) { 1449 $page->requires->yui_module($js->modules, $js->function, $js->arguments, null, $js->ondomready); 1450 } 1451 foreach ($this->jsinitcalls as $js) { 1452 $page->requires->js_init_call($js->function, $js->extraarguments, $js->ondomready, $js->module); 1453 } 1454 foreach ($this->jsstrings as $string) { 1455 $page->requires->strings_for_js($string->identifiers, $string->component, $string->a); 1456 } 1457 } 1458 } 1459 1460 /** 1461 * User enrolment action 1462 * 1463 * This class is used to manage a renderable ue action such as editing an user enrolment or deleting 1464 * a user enrolment. 1465 * 1466 * @copyright 2011 Sam Hemelryk 1467 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 1468 */ 1469 class user_enrolment_action implements renderable { 1470 1471 /** 1472 * The icon to display for the action 1473 * @var pix_icon 1474 */ 1475 protected $icon; 1476 1477 /** 1478 * The title for the action 1479 * @var string 1480 */ 1481 protected $title; 1482 1483 /** 1484 * The URL to the action 1485 * @var moodle_url 1486 */ 1487 protected $url; 1488 1489 /** 1490 * An array of HTML attributes 1491 * @var array 1492 */ 1493 protected $attributes = array(); 1494 1495 /** 1496 * Constructor 1497 * @param pix_icon $icon 1498 * @param string $title 1499 * @param moodle_url $url 1500 * @param array $attributes 1501 */ 1502 public function __construct(pix_icon $icon, $title, $url, array $attributes = null) { 1503 $this->icon = $icon; 1504 $this->title = $title; 1505 $this->url = new moodle_url($url); 1506 if (!empty($attributes)) { 1507 $this->attributes = $attributes; 1508 } 1509 $this->attributes['title'] = $title; 1510 } 1511 1512 /** 1513 * Returns the icon for this action 1514 * @return pix_icon 1515 */ 1516 public function get_icon() { 1517 return $this->icon; 1518 } 1519 1520 /** 1521 * Returns the title for this action 1522 * @return string 1523 */ 1524 public function get_title() { 1525 return $this->title; 1526 } 1527 1528 /** 1529 * Returns the URL for this action 1530 * @return moodle_url 1531 */ 1532 public function get_url() { 1533 return $this->url; 1534 } 1535 1536 /** 1537 * Returns the attributes to use for this action 1538 * @return array 1539 */ 1540 public function get_attributes() { 1541 return $this->attributes; 1542 } 1543 } 1544 1545 class enrol_ajax_exception extends moodle_exception { 1546 /** 1547 * Constructor 1548 * @param string $errorcode The name of the string from error.php to print 1549 * @param string $module name of module 1550 * @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. 1551 * @param object $a Extra words and phrases that might be required in the error string 1552 * @param string $debuginfo optional debugging information 1553 */ 1554 public function __construct($errorcode, $link = '', $a = NULL, $debuginfo = null) { 1555 parent::__construct($errorcode, 'enrol', $link, $a, $debuginfo); 1556 } 1557 } 1558 1559 /** 1560 * This class is used to manage a bulk operations for enrolment plugins. 1561 * 1562 * @copyright 2011 Sam Hemelryk 1563 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 1564 */ 1565 abstract class enrol_bulk_enrolment_operation { 1566 1567 /** 1568 * The course enrolment manager 1569 * @var course_enrolment_manager 1570 */ 1571 protected $manager; 1572 1573 /** 1574 * The enrolment plugin to which this operation belongs 1575 * @var enrol_plugin 1576 */ 1577 protected $plugin; 1578 1579 /** 1580 * Contructor 1581 * @param course_enrolment_manager $manager 1582 * @param stdClass $plugin 1583 */ 1584 public function __construct(course_enrolment_manager $manager, enrol_plugin $plugin = null) { 1585 $this->manager = $manager; 1586 $this->plugin = $plugin; 1587 } 1588 1589 /** 1590 * Returns a moodleform used for this operation, or false if no form is required and the action 1591 * should be immediatly processed. 1592 * 1593 * @param moodle_url|string $defaultaction 1594 * @param mixed $defaultcustomdata 1595 * @return enrol_bulk_enrolment_change_form|moodleform|false 1596 */ 1597 public function get_form($defaultaction = null, $defaultcustomdata = null) { 1598 return false; 1599 } 1600 1601 /** 1602 * Returns the title to use for this bulk operation 1603 * 1604 * @return string 1605 */ 1606 abstract public function get_title(); 1607 1608 /** 1609 * Returns the identifier for this bulk operation. 1610 * This should be the same identifier used by the plugins function when returning 1611 * all of its bulk operations. 1612 * 1613 * @return string 1614 */ 1615 abstract public function get_identifier(); 1616 1617 /** 1618 * Processes the bulk operation on the given users 1619 * 1620 * @param course_enrolment_manager $manager 1621 * @param array $users 1622 * @param stdClass $properties 1623 */ 1624 abstract public function process(course_enrolment_manager $manager, array $users, stdClass $properties); 1625 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body