[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * Local stuff for cohort enrolment plugin. 19 * 20 * @package enrol_cohort 21 * @copyright 2010 Petr Skoda {@link http://skodak.org} 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 require_once($CFG->dirroot . '/enrol/locallib.php'); 28 require_once($CFG->dirroot . '/cohort/lib.php'); 29 30 31 /** 32 * Event handler for cohort enrolment plugin. 33 * 34 * We try to keep everything in sync via listening to events, 35 * it may fail sometimes, so we always do a full sync in cron too. 36 */ 37 class enrol_cohort_handler { 38 /** 39 * Event processor - cohort member added. 40 * @param \core\event\cohort_member_added $event 41 * @return bool 42 */ 43 public static function member_added(\core\event\cohort_member_added $event) { 44 global $DB, $CFG; 45 require_once("$CFG->dirroot/group/lib.php"); 46 47 if (!enrol_is_enabled('cohort')) { 48 return true; 49 } 50 51 // Does any enabled cohort instance want to sync with this cohort? 52 $sql = "SELECT e.*, r.id as roleexists 53 FROM {enrol} e 54 LEFT JOIN {role} r ON (r.id = e.roleid) 55 WHERE e.customint1 = :cohortid AND e.enrol = 'cohort' 56 ORDER BY e.id ASC"; 57 if (!$instances = $DB->get_records_sql($sql, array('cohortid'=>$event->objectid))) { 58 return true; 59 } 60 61 $plugin = enrol_get_plugin('cohort'); 62 foreach ($instances as $instance) { 63 if ($instance->status != ENROL_INSTANCE_ENABLED ) { 64 // No roles for disabled instances. 65 $instance->roleid = 0; 66 } else if ($instance->roleid and !$instance->roleexists) { 67 // Invalid role - let's just enrol, they will have to create new sync and delete this one. 68 $instance->roleid = 0; 69 } 70 unset($instance->roleexists); 71 // No problem if already enrolled. 72 $plugin->enrol_user($instance, $event->relateduserid, $instance->roleid, 0, 0, ENROL_USER_ACTIVE); 73 74 // Sync groups. 75 if ($instance->customint2) { 76 if (!groups_is_member($instance->customint2, $event->relateduserid)) { 77 if ($group = $DB->get_record('groups', array('id'=>$instance->customint2, 'courseid'=>$instance->courseid))) { 78 groups_add_member($group->id, $event->relateduserid, 'enrol_cohort', $instance->id); 79 } 80 } 81 } 82 } 83 84 return true; 85 } 86 87 /** 88 * Event processor - cohort member removed. 89 * @param \core\event\cohort_member_removed $event 90 * @return bool 91 */ 92 public static function member_removed(\core\event\cohort_member_removed $event) { 93 global $DB; 94 95 // Does anything want to sync with this cohort? 96 if (!$instances = $DB->get_records('enrol', array('customint1'=>$event->objectid, 'enrol'=>'cohort'), 'id ASC')) { 97 return true; 98 } 99 100 $plugin = enrol_get_plugin('cohort'); 101 $unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); 102 103 foreach ($instances as $instance) { 104 if (!$ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$event->relateduserid))) { 105 continue; 106 } 107 if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) { 108 $plugin->unenrol_user($instance, $event->relateduserid); 109 110 } else { 111 if ($ue->status != ENROL_USER_SUSPENDED) { 112 $plugin->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED); 113 $context = context_course::instance($instance->courseid); 114 role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$context->id, 'component'=>'enrol_cohort', 'itemid'=>$instance->id)); 115 } 116 } 117 } 118 119 return true; 120 } 121 122 /** 123 * Event processor - cohort deleted. 124 * @param \core\event\cohort_deleted $event 125 * @return bool 126 */ 127 public static function deleted(\core\event\cohort_deleted $event) { 128 global $DB; 129 130 // Does anything want to sync with this cohort? 131 if (!$instances = $DB->get_records('enrol', array('customint1'=>$event->objectid, 'enrol'=>'cohort'), 'id ASC')) { 132 return true; 133 } 134 135 $plugin = enrol_get_plugin('cohort'); 136 $unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); 137 138 foreach ($instances as $instance) { 139 if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) { 140 $context = context_course::instance($instance->courseid); 141 role_unassign_all(array('contextid'=>$context->id, 'component'=>'enrol_cohort', 'itemid'=>$instance->id)); 142 $plugin->update_status($instance, ENROL_INSTANCE_DISABLED); 143 } else { 144 $plugin->delete_instance($instance); 145 } 146 } 147 148 return true; 149 } 150 } 151 152 153 /** 154 * Sync all cohort course links. 155 * @param progress_trace $trace 156 * @param int $courseid one course, empty mean all 157 * @return int 0 means ok, 1 means error, 2 means plugin disabled 158 */ 159 function enrol_cohort_sync(progress_trace $trace, $courseid = NULL) { 160 global $CFG, $DB; 161 require_once("$CFG->dirroot/group/lib.php"); 162 163 // Purge all roles if cohort sync disabled, those can be recreated later here by cron or CLI. 164 if (!enrol_is_enabled('cohort')) { 165 $trace->output('Cohort sync plugin is disabled, unassigning all plugin roles and stopping.'); 166 role_unassign_all(array('component'=>'enrol_cohort')); 167 return 2; 168 } 169 170 // Unfortunately this may take a long time, this script can be interrupted without problems. 171 core_php_time_limit::raise(); 172 raise_memory_limit(MEMORY_HUGE); 173 174 $trace->output('Starting user enrolment synchronisation...'); 175 176 $allroles = get_all_roles(); 177 $instances = array(); //cache 178 179 $plugin = enrol_get_plugin('cohort'); 180 $unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); 181 182 183 // Iterate through all not enrolled yet users. 184 $onecourse = $courseid ? "AND e.courseid = :courseid" : ""; 185 $sql = "SELECT cm.userid, e.id AS enrolid, ue.status 186 FROM {cohort_members} cm 187 JOIN {enrol} e ON (e.customint1 = cm.cohortid AND e.enrol = 'cohort' $onecourse) 188 JOIN {user} u ON (u.id = cm.userid AND u.deleted = 0) 189 LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = cm.userid) 190 WHERE ue.id IS NULL OR ue.status = :suspended"; 191 $params = array(); 192 $params['courseid'] = $courseid; 193 $params['suspended'] = ENROL_USER_SUSPENDED; 194 $rs = $DB->get_recordset_sql($sql, $params); 195 foreach($rs as $ue) { 196 if (!isset($instances[$ue->enrolid])) { 197 $instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid)); 198 } 199 $instance = $instances[$ue->enrolid]; 200 if ($ue->status == ENROL_USER_SUSPENDED) { 201 $plugin->update_user_enrol($instance, $ue->userid, ENROL_USER_ACTIVE); 202 $trace->output("unsuspending: $ue->userid ==> $instance->courseid via cohort $instance->customint1", 1); 203 } else { 204 $plugin->enrol_user($instance, $ue->userid); 205 $trace->output("enrolling: $ue->userid ==> $instance->courseid via cohort $instance->customint1", 1); 206 } 207 } 208 $rs->close(); 209 210 211 // Unenrol as necessary. 212 $sql = "SELECT ue.*, e.courseid 213 FROM {user_enrolments} ue 214 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'cohort' $onecourse) 215 LEFT JOIN {cohort_members} cm ON (cm.cohortid = e.customint1 AND cm.userid = ue.userid) 216 WHERE cm.id IS NULL"; 217 $rs = $DB->get_recordset_sql($sql, array('courseid'=>$courseid)); 218 foreach($rs as $ue) { 219 if (!isset($instances[$ue->enrolid])) { 220 $instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid)); 221 } 222 $instance = $instances[$ue->enrolid]; 223 if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) { 224 // Remove enrolment together with group membership, grades, preferences, etc. 225 $plugin->unenrol_user($instance, $ue->userid); 226 $trace->output("unenrolling: $ue->userid ==> $instance->courseid via cohort $instance->customint1", 1); 227 228 } else { // ENROL_EXT_REMOVED_SUSPENDNOROLES 229 // Just disable and ignore any changes. 230 if ($ue->status != ENROL_USER_SUSPENDED) { 231 $plugin->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED); 232 $context = context_course::instance($instance->courseid); 233 role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$context->id, 'component'=>'enrol_cohort', 'itemid'=>$instance->id)); 234 $trace->output("suspending and unsassigning all roles: $ue->userid ==> $instance->courseid", 1); 235 } 236 } 237 } 238 $rs->close(); 239 unset($instances); 240 241 242 // Now assign all necessary roles to enrolled users - skip suspended instances and users. 243 $onecourse = $courseid ? "AND e.courseid = :courseid" : ""; 244 $sql = "SELECT e.roleid, ue.userid, c.id AS contextid, e.id AS itemid, e.courseid 245 FROM {user_enrolments} ue 246 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'cohort' AND e.status = :statusenabled $onecourse) 247 JOIN {role} r ON (r.id = e.roleid) 248 JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :coursecontext) 249 JOIN {user} u ON (u.id = ue.userid AND u.deleted = 0) 250 LEFT JOIN {role_assignments} ra ON (ra.contextid = c.id AND ra.userid = ue.userid AND ra.itemid = e.id AND ra.component = 'enrol_cohort' AND e.roleid = ra.roleid) 251 WHERE ue.status = :useractive AND ra.id IS NULL"; 252 $params = array(); 253 $params['statusenabled'] = ENROL_INSTANCE_ENABLED; 254 $params['useractive'] = ENROL_USER_ACTIVE; 255 $params['coursecontext'] = CONTEXT_COURSE; 256 $params['courseid'] = $courseid; 257 258 $rs = $DB->get_recordset_sql($sql, $params); 259 foreach($rs as $ra) { 260 role_assign($ra->roleid, $ra->userid, $ra->contextid, 'enrol_cohort', $ra->itemid); 261 $trace->output("assigning role: $ra->userid ==> $ra->courseid as ".$allroles[$ra->roleid]->shortname, 1); 262 } 263 $rs->close(); 264 265 266 // Remove unwanted roles - sync role can not be changed, we only remove role when unenrolled. 267 $onecourse = $courseid ? "AND e.courseid = :courseid" : ""; 268 $sql = "SELECT ra.roleid, ra.userid, ra.contextid, ra.itemid, e.courseid 269 FROM {role_assignments} ra 270 JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = :coursecontext) 271 JOIN {enrol} e ON (e.id = ra.itemid AND e.enrol = 'cohort' $onecourse) 272 LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = ra.userid AND ue.status = :useractive) 273 WHERE ra.component = 'enrol_cohort' AND (ue.id IS NULL OR e.status <> :statusenabled)"; 274 $params = array(); 275 $params['statusenabled'] = ENROL_INSTANCE_ENABLED; 276 $params['useractive'] = ENROL_USER_ACTIVE; 277 $params['coursecontext'] = CONTEXT_COURSE; 278 $params['courseid'] = $courseid; 279 280 $rs = $DB->get_recordset_sql($sql, $params); 281 foreach($rs as $ra) { 282 role_unassign($ra->roleid, $ra->userid, $ra->contextid, 'enrol_cohort', $ra->itemid); 283 $trace->output("unassigning role: $ra->userid ==> $ra->courseid as ".$allroles[$ra->roleid]->shortname, 1); 284 } 285 $rs->close(); 286 287 288 // Finally sync groups. 289 $affectedusers = groups_sync_with_enrolment('cohort', $courseid); 290 foreach ($affectedusers['removed'] as $gm) { 291 $trace->output("removing user from group: $gm->userid ==> $gm->courseid - $gm->groupname", 1); 292 } 293 foreach ($affectedusers['added'] as $ue) { 294 $trace->output("adding user to group: $ue->userid ==> $ue->courseid - $ue->groupname", 1); 295 } 296 297 $trace->output('...user enrolment synchronisation finished.'); 298 299 return 0; 300 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |