|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00028 defined('MOODLE_INTERNAL') || die(); 00029 00039 class course_enrolment_manager { 00040 00045 protected $context; 00050 protected $course = null; 00055 protected $instancefilter = null; 00056 00062 protected $totalusers = null; 00068 protected $users = array(); 00069 00075 protected $otherusers = array(); 00076 00082 protected $totalotherusers = null; 00083 00088 protected $moodlepage = null; 00089 00096 private $_instancessql = null; 00097 private $_instances = null; 00098 private $_inames = null; 00099 private $_plugins = null; 00100 private $_roles = null; 00101 private $_assignableroles = null; 00102 private $_assignablerolesothers = null; 00103 private $_groups = null; 00113 public function __construct(moodle_page $moodlepage, $course, $instancefilter = null) { 00114 $this->moodlepage = $moodlepage; 00115 $this->context = get_context_instance(CONTEXT_COURSE, $course->id); 00116 $this->course = $course; 00117 $this->instancefilter = $instancefilter; 00118 } 00119 00124 public function get_moodlepage() { 00125 return $this->moodlepage; 00126 } 00127 00137 public function get_total_users() { 00138 global $DB; 00139 if ($this->totalusers === null) { 00140 list($instancessql, $params, $filter) = $this->get_instance_sql(); 00141 $sqltotal = "SELECT COUNT(DISTINCT u.id) 00142 FROM {user} u 00143 JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid $instancessql) 00144 JOIN {enrol} e ON (e.id = ue.enrolid)"; 00145 $this->totalusers = (int)$DB->count_records_sql($sqltotal, $params); 00146 } 00147 return $this->totalusers; 00148 } 00149 00159 public function get_total_other_users() { 00160 global $DB; 00161 if ($this->totalotherusers === null) { 00162 list($ctxcondition, $params) = $DB->get_in_or_equal(get_parent_contexts($this->context, true), SQL_PARAMS_NAMED, 'ctx'); 00163 $params['courseid'] = $this->course->id; 00164 $sql = "SELECT COUNT(DISTINCT u.id) 00165 FROM {role_assignments} ra 00166 JOIN {user} u ON u.id = ra.userid 00167 JOIN {context} ctx ON ra.contextid = ctx.id 00168 LEFT JOIN ( 00169 SELECT ue.id, ue.userid 00170 FROM {user_enrolments} ue 00171 LEFT JOIN {enrol} e ON e.id=ue.enrolid 00172 WHERE e.courseid = :courseid 00173 ) ue ON ue.userid=u.id 00174 WHERE ctx.id $ctxcondition AND 00175 ue.id IS NULL"; 00176 $this->totalotherusers = (int)$DB->count_records_sql($sql, $params); 00177 } 00178 return $this->totalotherusers; 00179 } 00180 00194 public function get_users($sort, $direction='ASC', $page=0, $perpage=25) { 00195 global $DB; 00196 if ($direction !== 'ASC') { 00197 $direction = 'DESC'; 00198 } 00199 $key = md5("$sort-$direction-$page-$perpage"); 00200 if (!array_key_exists($key, $this->users)) { 00201 list($instancessql, $params, $filter) = $this->get_instance_sql(); 00202 $extrafields = get_extra_user_fields($this->get_context()); 00203 $extrafields[] = 'lastaccess'; 00204 $ufields = user_picture::fields('u', $extrafields); 00205 $sql = "SELECT DISTINCT $ufields, ul.timeaccess AS lastseen 00206 FROM {user} u 00207 JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid $instancessql) 00208 JOIN {enrol} e ON (e.id = ue.enrolid) 00209 LEFT JOIN {user_lastaccess} ul ON (ul.courseid = e.courseid AND ul.userid = u.id)"; 00210 if ($sort === 'firstname') { 00211 $sql .= " ORDER BY u.firstname $direction, u.lastname $direction"; 00212 } else if ($sort === 'lastname') { 00213 $sql .= " ORDER BY u.lastname $direction, u.firstname $direction"; 00214 } else if ($sort === 'email') { 00215 $sql .= " ORDER BY u.email $direction, u.lastname $direction, u.firstname $direction"; 00216 } else if ($sort === 'lastseen') { 00217 $sql .= " ORDER BY ul.timeaccess $direction, u.lastname $direction, u.firstname $direction"; 00218 } 00219 $this->users[$key] = $DB->get_records_sql($sql, $params, $page*$perpage, $perpage); 00220 } 00221 return $this->users[$key]; 00222 } 00223 00237 public function get_other_users($sort, $direction='ASC', $page=0, $perpage=25) { 00238 global $DB; 00239 if ($direction !== 'ASC') { 00240 $direction = 'DESC'; 00241 } 00242 $key = md5("$sort-$direction-$page-$perpage"); 00243 if (!array_key_exists($key, $this->otherusers)) { 00244 list($ctxcondition, $params) = $DB->get_in_or_equal(get_parent_contexts($this->context, true), SQL_PARAMS_NAMED, 'ctx'); 00245 $params['courseid'] = $this->course->id; 00246 $params['cid'] = $this->course->id; 00247 $sql = "SELECT ra.id as raid, ra.contextid, ra.component, ctx.contextlevel, ra.roleid, u.*, ue.lastseen 00248 FROM {role_assignments} ra 00249 JOIN {user} u ON u.id = ra.userid 00250 JOIN {context} ctx ON ra.contextid = ctx.id 00251 LEFT JOIN ( 00252 SELECT ue.id, ue.userid, ul.timeaccess AS lastseen 00253 FROM {user_enrolments} ue 00254 LEFT JOIN {enrol} e ON e.id=ue.enrolid 00255 LEFT JOIN {user_lastaccess} ul ON (ul.courseid = e.courseid AND ul.userid = ue.userid) 00256 WHERE e.courseid = :courseid 00257 ) ue ON ue.userid=u.id 00258 WHERE ctx.id $ctxcondition AND 00259 ue.id IS NULL 00260 ORDER BY u.$sort $direction, ctx.depth DESC"; 00261 $this->otherusers[$key] = $DB->get_records_sql($sql, $params, $page*$perpage, $perpage); 00262 } 00263 return $this->otherusers[$key]; 00264 } 00265 00277 public function get_potential_users($enrolid, $search='', $searchanywhere=false, $page=0, $perpage=25) { 00278 global $DB, $CFG; 00279 00280 // Add some additional sensible conditions 00281 $tests = array("id <> :guestid", 'u.deleted = 0', 'u.confirmed = 1'); 00282 $params = array('guestid' => $CFG->siteguest); 00283 if (!empty($search)) { 00284 $conditions = get_extra_user_fields($this->get_context()); 00285 $conditions[] = $DB->sql_concat('u.firstname', "' '", 'u.lastname'); 00286 if ($searchanywhere) { 00287 $searchparam = '%' . $search . '%'; 00288 } else { 00289 $searchparam = $search . '%'; 00290 } 00291 $i = 0; 00292 foreach ($conditions as $key=>$condition) { 00293 $conditions[$key] = $DB->sql_like($condition,":con{$i}00", false); 00294 $params["con{$i}00"] = $searchparam; 00295 $i++; 00296 } 00297 $tests[] = '(' . implode(' OR ', $conditions) . ')'; 00298 } 00299 $wherecondition = implode(' AND ', $tests); 00300 00301 $extrafields = get_extra_user_fields($this->get_context(), array('username', 'lastaccess')); 00302 $extrafields[] = 'username'; 00303 $extrafields[] = 'lastaccess'; 00304 $ufields = user_picture::fields('u', $extrafields); 00305 00306 $fields = 'SELECT '.$ufields; 00307 $countfields = 'SELECT COUNT(1)'; 00308 $sql = " FROM {user} u 00309 WHERE $wherecondition 00310 AND u.id NOT IN (SELECT ue.userid 00311 FROM {user_enrolments} ue 00312 JOIN {enrol} e ON (e.id = ue.enrolid AND e.id = :enrolid))"; 00313 $order = ' ORDER BY u.lastname ASC, u.firstname ASC'; 00314 $params['enrolid'] = $enrolid; 00315 $totalusers = $DB->count_records_sql($countfields . $sql, $params); 00316 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage); 00317 return array('totalusers'=>$totalusers, 'users'=>$availableusers); 00318 } 00319 00330 public function search_other_users($search='', $searchanywhere=false, $page=0, $perpage=25) { 00331 global $DB, $CFG; 00332 00333 // Add some additional sensible conditions 00334 $tests = array("u.id <> :guestid", 'u.deleted = 0', 'u.confirmed = 1'); 00335 $params = array('guestid'=>$CFG->siteguest); 00336 if (!empty($search)) { 00337 $conditions = array('u.firstname','u.lastname'); 00338 if ($searchanywhere) { 00339 $searchparam = '%' . $search . '%'; 00340 } else { 00341 $searchparam = $search . '%'; 00342 } 00343 $i = 0; 00344 foreach ($conditions as $key=>$condition) { 00345 $conditions[$key] = $DB->sql_like($condition, ":con{$i}00", false); 00346 $params["con{$i}00"] = $searchparam; 00347 $i++; 00348 } 00349 $tests[] = '(' . implode(' OR ', $conditions) . ')'; 00350 } 00351 $wherecondition = implode(' AND ', $tests); 00352 00353 $fields = 'SELECT '.user_picture::fields('u', array('username','lastaccess')); 00354 $countfields = 'SELECT COUNT(u.id)'; 00355 $sql = " FROM {user} u 00356 WHERE $wherecondition 00357 AND u.id NOT IN ( 00358 SELECT u.id 00359 FROM {role_assignments} r, {user} u 00360 WHERE r.contextid = :contextid AND 00361 u.id = r.userid)"; 00362 $order = ' ORDER BY lastname ASC, firstname ASC'; 00363 00364 $params['contextid'] = $this->context->id; 00365 $totalusers = $DB->count_records_sql($countfields . $sql, $params); 00366 $availableusers = $DB->get_records_sql($fields . $sql . $order, $params, $page*$perpage, $perpage); 00367 return array('totalusers'=>$totalusers, 'users'=>$availableusers); 00368 } 00369 00377 protected function get_instance_sql() { 00378 global $DB; 00379 if ($this->_instancessql === null) { 00380 $instances = $this->get_enrolment_instances(); 00381 $filter = $this->get_enrolment_filter(); 00382 if ($filter && array_key_exists($filter, $instances)) { 00383 $sql = " = :ifilter"; 00384 $params = array('ifilter'=>$filter); 00385 } else { 00386 $filter = 0; 00387 if ($instances) { 00388 list($sql, $params) = $DB->get_in_or_equal(array_keys($this->get_enrolment_instances()), SQL_PARAMS_NAMED); 00389 } else { 00390 // no enabled instances, oops, we should probably say something 00391 $sql = "= :never"; 00392 $params = array('never'=>-1); 00393 } 00394 } 00395 $this->instancefilter = $filter; 00396 $this->_instancessql = array($sql, $params, $filter); 00397 } 00398 return $this->_instancessql; 00399 } 00400 00406 public function get_enrolment_instances() { 00407 if ($this->_instances === null) { 00408 $this->_instances = enrol_get_instances($this->course->id, true); 00409 } 00410 return $this->_instances; 00411 } 00412 00418 public function get_enrolment_instance_names() { 00419 if ($this->_inames === null) { 00420 $instances = $this->get_enrolment_instances(); 00421 $plugins = $this->get_enrolment_plugins(); 00422 foreach ($instances as $key=>$instance) { 00423 if (!isset($plugins[$instance->enrol])) { 00424 // weird, some broken stuff in plugin 00425 unset($instances[$key]); 00426 continue; 00427 } 00428 $this->_inames[$key] = $plugins[$instance->enrol]->get_instance_name($instance); 00429 } 00430 } 00431 return $this->_inames; 00432 } 00433 00439 public function get_enrolment_plugins() { 00440 if ($this->_plugins === null) { 00441 $this->_plugins = enrol_get_plugins(true); 00442 } 00443 return $this->_plugins; 00444 } 00445 00451 public function get_all_roles() { 00452 if ($this->_roles === null) { 00453 $this->_roles = role_fix_names(get_all_roles(), $this->context); 00454 } 00455 return $this->_roles; 00456 } 00457 00463 public function get_assignable_roles($otherusers = false) { 00464 if ($this->_assignableroles === null) { 00465 $this->_assignableroles = get_assignable_roles($this->context, ROLENAME_ALIAS, false); // verifies unassign access control too 00466 } 00467 00468 if ($otherusers) { 00469 if (!is_array($this->_assignablerolesothers)) { 00470 $this->_assignablerolesothers = array(); 00471 list($courseviewroles, $ignored) = get_roles_with_cap_in_context($this->context, 'moodle/course:view'); 00472 foreach ($this->_assignableroles as $roleid=>$role) { 00473 if (isset($courseviewroles[$roleid])) { 00474 $this->_assignablerolesothers[$roleid] = $role; 00475 } 00476 } 00477 } 00478 return $this->_assignablerolesothers; 00479 } else { 00480 return $this->_assignableroles; 00481 } 00482 } 00483 00489 public function get_all_groups() { 00490 if ($this->_groups === null) { 00491 $this->_groups = groups_get_all_groups($this->course->id); 00492 foreach ($this->_groups as $gid=>$group) { 00493 $this->_groups[$gid]->name = format_string($group->name); 00494 } 00495 } 00496 return $this->_groups; 00497 } 00498 00506 public function unenrol_user($ue) { 00507 global $DB; 00508 list ($instance, $plugin) = $this->get_user_enrolment_components($ue); 00509 if ($instance && $plugin && $plugin->allow_unenrol_user($instance, $ue) && has_capability("enrol/$instance->enrol:unenrol", $this->context)) { 00510 $plugin->unenrol_user($instance, $ue->userid); 00511 return true; 00512 } 00513 return false; 00514 } 00515 00523 public function get_user_enrolment_components($userenrolment) { 00524 global $DB; 00525 if (is_numeric($userenrolment)) { 00526 $userenrolment = $DB->get_record('user_enrolments', array('id'=>(int)$userenrolment)); 00527 } 00528 $instances = $this->get_enrolment_instances(); 00529 $plugins = $this->get_enrolment_plugins(); 00530 if (!$userenrolment || !isset($instances[$userenrolment->enrolid])) { 00531 return array(false, false); 00532 } 00533 $instance = $instances[$userenrolment->enrolid]; 00534 $plugin = $plugins[$instance->enrol]; 00535 return array($instance, $plugin); 00536 } 00537 00546 public function unassign_role_from_user($userid, $roleid) { 00547 global $DB; 00548 require_capability('moodle/role:assign', $this->context); 00549 $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST); 00550 try { 00551 role_unassign($roleid, $user->id, $this->context->id, '', NULL); 00552 } catch (Exception $e) { 00553 if (defined('AJAX_SCRIPT')) { 00554 throw $e; 00555 } 00556 return false; 00557 } 00558 return true; 00559 } 00560 00568 public function assign_role_to_user($roleid, $userid) { 00569 require_capability('moodle/role:assign', $this->context); 00570 if (!array_key_exists($roleid, $this->get_assignable_roles())) { 00571 if (defined('AJAX_SCRIPT')) { 00572 throw new moodle_exception('invalidrole'); 00573 } 00574 return false; 00575 } 00576 return role_assign($roleid, $userid, $this->context->id, '', NULL); 00577 } 00578 00586 public function add_user_to_group($user, $groupid) { 00587 require_capability('moodle/course:managegroups', $this->context); 00588 $group = $this->get_group($groupid); 00589 if (!$group) { 00590 return false; 00591 } 00592 return groups_add_member($group->id, $user->id); 00593 } 00594 00603 public function remove_user_from_group($user, $groupid) { 00604 global $DB; 00605 require_capability('moodle/course:managegroups', $this->context); 00606 $group = $this->get_group($groupid); 00607 if (!$group) { 00608 return false; 00609 } 00610 return groups_remove_member($group, $user); 00611 } 00612 00619 public function get_group($groupid) { 00620 $groups = $this->get_all_groups(); 00621 if (!array_key_exists($groupid, $groups)) { 00622 return false; 00623 } 00624 return $groups[$groupid]; 00625 } 00626 00634 public function edit_enrolment($userenrolment, $data) { 00635 //Only allow editing if the user has the appropriate capability 00636 //Already checked in /enrol/users.php but checking again in case this function is called from elsewhere 00637 list($instance, $plugin) = $this->get_user_enrolment_components($userenrolment); 00638 if ($instance && $plugin && $plugin->allow_manage($instance) && has_capability("enrol/$instance->enrol:manage", $this->context)) { 00639 if (!isset($data->status)) { 00640 $data->status = $userenrolment->status; 00641 } 00642 $plugin->update_user_enrol($instance, $userenrolment->userid, $data->status, $data->timestart, $data->timeend); 00643 return true; 00644 } 00645 return false; 00646 } 00647 00652 public function get_enrolment_filter() { 00653 return $this->instancefilter; 00654 } 00655 00662 public function get_user_roles($userid) { 00663 $roles = array(); 00664 $ras = get_user_roles($this->context, $userid, true, 'c.contextlevel DESC, r.sortorder ASC'); 00665 foreach ($ras as $ra) { 00666 if ($ra->contextid != $this->context->id) { 00667 if (!array_key_exists($ra->roleid, $roles)) { 00668 $roles[$ra->roleid] = null; 00669 } 00670 // higher ras, course always takes precedence 00671 continue; 00672 } 00673 if (array_key_exists($ra->roleid, $roles) && $roles[$ra->roleid] === false) { 00674 continue; 00675 } 00676 $roles[$ra->roleid] = ($ra->itemid == 0 and $ra->component === ''); 00677 } 00678 return $roles; 00679 } 00680 00688 public function get_user_enrolments($userid) { 00689 global $DB; 00690 list($instancessql, $params, $filter) = $this->get_instance_sql(); 00691 $params['userid'] = $userid; 00692 $userenrolments = $DB->get_records_select('user_enrolments', "enrolid $instancessql AND userid = :userid", $params); 00693 $instances = $this->get_enrolment_instances(); 00694 $plugins = $this->get_enrolment_plugins(); 00695 $inames = $this->get_enrolment_instance_names(); 00696 foreach ($userenrolments as &$ue) { 00697 $ue->enrolmentinstance = $instances[$ue->enrolid]; 00698 $ue->enrolmentplugin = $plugins[$ue->enrolmentinstance->enrol]; 00699 $ue->enrolmentinstancename = $inames[$ue->enrolmentinstance->id]; 00700 } 00701 return $userenrolments; 00702 } 00703 00710 public function get_user_groups($userid) { 00711 return groups_get_all_groups($this->course->id, $userid, 0, 'g.id'); 00712 } 00713 00720 public function get_url_params() { 00721 $args = array( 00722 'id' => $this->course->id 00723 ); 00724 if (!empty($this->instancefilter)) { 00725 $args['ifilter'] = $this->instancefilter; 00726 } 00727 return $args; 00728 } 00729 00735 public function get_course() { 00736 return $this->course; 00737 } 00738 00744 public function get_context() { 00745 return $this->context; 00746 } 00747 00762 public function get_other_users_for_display(core_enrol_renderer $renderer, moodle_url $pageurl, $sort, $direction, $page, $perpage) { 00763 00764 $userroles = $this->get_other_users($sort, $direction, $page, $perpage); 00765 $roles = $this->get_all_roles(); 00766 00767 $courseid = $this->get_course()->id; 00768 $context = $this->get_context(); 00769 00770 $users = array(); 00771 foreach ($userroles as $userrole) { 00772 if (!array_key_exists($userrole->id, $users)) { 00773 $users[$userrole->id] = array( 00774 'userid' => $userrole->id, 00775 'courseid' => $courseid, 00776 'picture' => new user_picture($userrole), 00777 'firstname' => fullname($userrole, true), 00778 'email' => $userrole->email, 00779 'roles' => array() 00780 ); 00781 } 00782 $a = new stdClass; 00783 $a->role = $roles[$userrole->roleid]->localname; 00784 $changeable = ($userrole->component == ''); 00785 if ($userrole->contextid == $this->context->id) { 00786 $roletext = get_string('rolefromthiscourse', 'enrol', $a); 00787 } else { 00788 $changeable = false; 00789 switch ($userrole->contextlevel) { 00790 case CONTEXT_COURSE : 00791 // Meta course 00792 $roletext = get_string('rolefrommetacourse', 'enrol', $a); 00793 break; 00794 case CONTEXT_COURSECAT : 00795 $roletext = get_string('rolefromcategory', 'enrol', $a); 00796 break; 00797 case CONTEXT_SYSTEM: 00798 default: 00799 $roletext = get_string('rolefromsystem', 'enrol', $a); 00800 break; 00801 } 00802 } 00803 $users[$userrole->id]['roles'][$userrole->roleid] = array( 00804 'text' => $roletext, 00805 'unchangeable' => !$changeable 00806 ); 00807 } 00808 return $users; 00809 } 00810 00823 public function get_users_for_display(course_enrolment_manager $manager, $sort, $direction, $page, $perpage) { 00824 $pageurl = $manager->get_moodlepage()->url; 00825 $users = $this->get_users($sort, $direction, $page, $perpage); 00826 00827 $now = time(); 00828 $strnever = get_string('never'); 00829 $straddgroup = get_string('addgroup', 'group'); 00830 $strunenrol = get_string('unenrol', 'enrol'); 00831 $stredit = get_string('edit'); 00832 00833 $allroles = $this->get_all_roles(); 00834 $assignable = $this->get_assignable_roles(); 00835 $allgroups = $this->get_all_groups(); 00836 $courseid = $this->get_course()->id; 00837 $context = $this->get_context(); 00838 $canmanagegroups = has_capability('moodle/course:managegroups', $context); 00839 00840 $url = new moodle_url($pageurl, $this->get_url_params()); 00841 $extrafields = get_extra_user_fields($context); 00842 00843 $userdetails = array(); 00844 foreach ($users as $user) { 00845 $details = array( 00846 'userid' => $user->id, 00847 'courseid' => $courseid, 00848 'picture' => new user_picture($user), 00849 'firstname' => fullname($user, true), 00850 'lastseen' => $strnever, 00851 'roles' => array(), 00852 'groups' => array(), 00853 'enrolments' => array() 00854 ); 00855 foreach ($extrafields as $field) { 00856 $details[$field] = $user->{$field}; 00857 } 00858 00859 if ($user->lastaccess) { 00860 $details['lastseen'] = format_time($now - $user->lastaccess); 00861 } 00862 00863 // Roles 00864 foreach ($this->get_user_roles($user->id) as $rid=>$rassignable) { 00865 $details['roles'][$rid] = array('text'=>$allroles[$rid]->localname, 'unchangeable'=>(!$rassignable || !isset($assignable[$rid]))); 00866 } 00867 00868 // Users 00869 $usergroups = $this->get_user_groups($user->id); 00870 foreach($usergroups as $gid=>$unused) { 00871 $details['groups'][$gid] = $allgroups[$gid]->name; 00872 } 00873 00874 // Enrolments 00875 foreach ($this->get_user_enrolments($user->id) as $ue) { 00876 if ($ue->timestart and $ue->timeend) { 00877 $period = get_string('periodstartend', 'enrol', array('start'=>userdate($ue->timestart), 'end'=>userdate($ue->timeend))); 00878 $periodoutside = ($ue->timestart && $ue->timeend && $now < $ue->timestart && $now > $ue->timeend); 00879 } else if ($ue->timestart) { 00880 $period = get_string('periodstart', 'enrol', userdate($ue->timestart)); 00881 $periodoutside = ($ue->timestart && $now < $ue->timestart); 00882 } else if ($ue->timeend) { 00883 $period = get_string('periodend', 'enrol', userdate($ue->timeend)); 00884 $periodoutside = ($ue->timeend && $now > $ue->timeend); 00885 } else { 00886 $period = ''; 00887 $periodoutside = false; 00888 } 00889 $details['enrolments'][$ue->id] = array( 00890 'text' => $ue->enrolmentinstancename, 00891 'period' => $period, 00892 'dimmed' => ($periodoutside || $ue->status != ENROL_USER_ACTIVE), 00893 'actions' => $ue->enrolmentplugin->get_user_enrolment_actions($manager, $ue) 00894 ); 00895 } 00896 $userdetails[$user->id] = $details; 00897 } 00898 return $userdetails; 00899 } 00900 00901 public function get_manual_enrol_buttons() { 00902 $plugins = $this->get_enrolment_plugins(); 00903 $buttons = array(); 00904 foreach ($plugins as $plugin) { 00905 $newbutton = $plugin->get_manual_enrol_button($this); 00906 if (is_array($newbutton)) { 00907 $buttons += $newbutton; 00908 } else if ($newbutton instanceof enrol_user_button) { 00909 $buttons[] = $newbutton; 00910 } 00911 } 00912 return $buttons; 00913 } 00914 00915 public function has_instance($enrolpluginname) { 00916 // Make sure manual enrolments instance exists 00917 foreach ($this->get_enrolment_instances() as $instance) { 00918 if ($instance->enrol == $enrolpluginname) { 00919 return true; 00920 } 00921 } 00922 return false; 00923 } 00924 00932 public function get_filtered_enrolment_plugin() { 00933 $instances = $this->get_enrolment_instances(); 00934 $plugins = $this->get_enrolment_plugins(); 00935 00936 if (empty($this->instancefilter) || !array_key_exists($this->instancefilter, $instances)) { 00937 return false; 00938 } 00939 00940 $instance = $instances[$this->instancefilter]; 00941 return $plugins[$instance->enrol]; 00942 } 00943 00954 public function get_users_enrolments(array $userids) { 00955 global $DB; 00956 00957 $instances = $this->get_enrolment_instances(); 00958 $plugins = $this->get_enrolment_plugins(); 00959 00960 if (!empty($this->instancefilter)) { 00961 $instancesql = ' = :instanceid'; 00962 $instanceparams = array('instanceid' => $this->instancefilter); 00963 } else { 00964 list($instancesql, $instanceparams) = $DB->get_in_or_equal(array_keys($instances), SQL_PARAMS_NAMED, 'instanceid0000'); 00965 } 00966 00967 $userfields = user_picture::fields('u'); 00968 list($idsql, $idparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED, 'userid0000'); 00969 00970 $sql = "SELECT ue.id AS ueid, ue.status, ue.enrolid, ue.userid, ue.timestart, ue.timeend, ue.modifierid, ue.timecreated, ue.timemodified, $userfields 00971 FROM {user_enrolments} ue 00972 LEFT JOIN {user} u ON u.id = ue.userid 00973 WHERE ue.enrolid $instancesql AND 00974 u.id $idsql 00975 ORDER BY u.firstname ASC, u.lastname ASC"; 00976 00977 $rs = $DB->get_recordset_sql($sql, $idparams + $instanceparams); 00978 $users = array(); 00979 foreach ($rs as $ue) { 00980 $user = user_picture::unalias($ue); 00981 $ue->id = $ue->ueid; 00982 unset($ue->ueid); 00983 if (!array_key_exists($user->id, $users)) { 00984 $user->enrolments = array(); 00985 $users[$user->id] = $user; 00986 } 00987 $ue->enrolmentinstance = $instances[$ue->enrolid]; 00988 $ue->enrolmentplugin = $plugins[$ue->enrolmentinstance->enrol]; 00989 $users[$user->id]->enrolments[$ue->id] = $ue; 00990 } 00991 $rs->close(); 00992 return $users; 00993 } 00994 } 00995 01002 class enrol_user_button extends single_button { 01003 01008 protected $jsyuimodules = array(); 01009 01014 protected $jsinitcalls = array(); 01015 01020 protected $jsstrings = array(); 01021 01030 public function __construct(moodle_url $url, $label, $method = 'post') { 01031 static $count = 0; 01032 $count ++; 01033 parent::__construct($url, $label, $method); 01034 $this->class = 'singlebutton enrolusersbutton'; 01035 $this->formid = 'enrolusersbutton-'.$count; 01036 } 01037 01047 public function require_yui_module($modules, $function, array $arguments = null, $galleryversion = '2010.04.08-12-35', $ondomready = false) { 01048 $js = new stdClass; 01049 $js->modules = (array)$modules; 01050 $js->function = $function; 01051 $js->arguments = $arguments; 01052 $js->galleryversion = $galleryversion; 01053 $js->ondomready = $ondomready; 01054 $this->jsyuimodules[] = $js; 01055 } 01056 01065 public function require_js_init_call($function, array $extraarguments = null, $ondomready = false, array $module = null) { 01066 $js = new stdClass; 01067 $js->function = $function; 01068 $js->extraarguments = $extraarguments; 01069 $js->ondomready = $ondomready; 01070 $js->module = $module; 01071 $this->jsinitcalls[] = $js; 01072 } 01073 01081 public function strings_for_js($identifiers, $component = 'moodle', $a = null) { 01082 $string = new stdClass; 01083 $string->identifiers = (array)$identifiers; 01084 $string->component = $component; 01085 $string->a = $a; 01086 $this->jsstrings[] = $string; 01087 } 01088 01094 public function initialise_js(moodle_page $page) { 01095 foreach ($this->jsyuimodules as $js) { 01096 $page->requires->yui_module($js->modules, $js->function, $js->arguments, $js->galleryversion, $js->ondomready); 01097 } 01098 foreach ($this->jsinitcalls as $js) { 01099 $page->requires->js_init_call($js->function, $js->extraarguments, $js->ondomready, $js->module); 01100 } 01101 foreach ($this->jsstrings as $string) { 01102 $page->requires->strings_for_js($string->identifiers, $string->component, $string->a); 01103 } 01104 } 01105 } 01106 01116 class user_enrolment_action implements renderable { 01117 01122 protected $icon; 01123 01128 protected $title; 01129 01134 protected $url; 01135 01140 protected $attributes = array(); 01141 01149 public function __construct(pix_icon $icon, $title, $url, array $attributes = null) { 01150 $this->icon = $icon; 01151 $this->title = $title; 01152 $this->url = new moodle_url($url); 01153 if (!empty($attributes)) { 01154 $this->attributes = $attributes; 01155 } 01156 $this->attributes['title'] = $title; 01157 } 01158 01163 public function get_icon() { 01164 return $this->icon; 01165 } 01166 01171 public function get_title() { 01172 return $this->title; 01173 } 01174 01179 public function get_url() { 01180 return $this->url; 01181 } 01182 01187 public function get_attributes() { 01188 return $this->attributes; 01189 } 01190 } 01191 01192 class enrol_ajax_exception extends moodle_exception { 01201 public function __construct($errorcode, $link = '', $a = NULL, $debuginfo = null) { 01202 parent::__construct($errorcode, 'enrol', $link, $a, $debuginfo); 01203 } 01204 } 01205 01212 abstract class enrol_bulk_enrolment_operation { 01213 01218 protected $manager; 01219 01224 protected $plugin; 01225 01231 public function __construct(course_enrolment_manager $manager, enrol_plugin $plugin = null) { 01232 $this->manager = $manager; 01233 $this->plugin = $plugin; 01234 } 01235 01244 public function get_form($defaultaction = null, $defaultcustomdata = null) { 01245 return false; 01246 } 01247 01253 abstract public function get_title(); 01254 01262 abstract public function get_identifier(); 01263 01271 abstract public function process(course_enrolment_manager $manager, array $users, stdClass $properties); 01272 }