2 // This file is part of Moodle - http://moodle.org/
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.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Local stuff for meta course enrolment plugin.
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
29 * Event handler for meta enrolment plugin.
31 * We try to keep everything in sync via listening to events,
32 * it may fail sometimes, so we always do a full sync in cron too.
34 class enrol_meta_handler
{
37 * Synchronise meta enrolments of this user in this course
39 * @param int $courseid
43 protected static function sync_course_instances($courseid, $userid) {
46 static $preventrecursion = false;
48 // does anything want to sync with this parent?
49 if (!$enrols = $DB->get_records('enrol', array('customint1'=>$courseid, 'enrol'=>'meta'), 'id ASC')) {
53 if ($preventrecursion) {
57 $preventrecursion = true;
60 foreach ($enrols as $enrol) {
61 self
::sync_with_parent_course($enrol, $userid);
63 } catch (Exception
$e) {
64 $preventrecursion = false;
68 $preventrecursion = false;
72 * Synchronise user enrolments in given instance as fast as possible.
74 * All roles are removed if the meta plugin disabled.
77 * @param stdClass $instance
81 protected static function sync_with_parent_course(stdClass
$instance, $userid) {
84 $plugin = enrol_get_plugin('meta');
86 if ($instance->customint1
== $instance->courseid
) {
87 // can not sync with self!!!
91 $context = context_course
::instance($instance->courseid
);
93 // list of enrolments in parent course (we ignore meta enrols in parents completely)
94 list($enabled, $params) = $DB->get_in_or_equal(explode(',', $CFG->enrol_plugins_enabled
), SQL_PARAMS_NAMED
, 'e');
95 $params['userid'] = $userid;
96 $params['parentcourse'] = $instance->customint1
;
98 FROM {user_enrolments} ue
99 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol <> 'meta' AND e.courseid = :parentcourse AND e.enrol $enabled)
100 WHERE ue.userid = :userid";
101 $parentues = $DB->get_records_sql($sql, $params);
102 // current enrolments for this instance
103 $ue = $DB->get_record('user_enrolments', array('enrolid'=>$instance->id
, 'userid'=>$userid));
105 // first deal with users that are not enrolled in parent
106 if (empty($parentues)) {
107 self
::user_not_supposed_to_be_here($instance, $ue, $context, $plugin);
111 if (!$parentcontext = context_course
::instance($instance->customint1
, IGNORE_MISSING
)) {
112 // Weird, we should not get here.
116 $skiproles = $plugin->get_config('nosyncroleids', '');
117 $skiproles = empty($skiproles) ?
array() : explode(',', $skiproles);
118 $syncall = $plugin->get_config('syncall', 1);
120 // roles in parent course (meta enrols must be ignored!)
121 $parentroles = array();
122 list($ignoreroles, $params) = $DB->get_in_or_equal($skiproles, SQL_PARAMS_NAMED
, 'ri', false, -1);
123 $params['contextid'] = $parentcontext->id
;
124 $params['userid'] = $userid;
125 $select = "contextid = :contextid AND userid = :userid AND component <> 'enrol_meta' AND roleid $ignoreroles";
126 foreach($DB->get_records_select('role_assignments', $select, $params) as $ra) {
127 $parentroles[$ra->roleid
] = $ra->roleid
;
130 // roles from this instance
132 $ras = $DB->get_records('role_assignments', array('contextid'=>$context->id
, 'userid'=>$userid, 'component'=>'enrol_meta', 'itemid'=>$instance->id
));
133 foreach($ras as $ra) {
134 $roles[$ra->roleid
] = $ra->roleid
;
138 // do we want users without roles?
139 if (!$syncall and empty($parentroles)) {
140 self
::user_not_supposed_to_be_here($instance, $ue, $context, $plugin);
144 // is parent enrol active? (we ignore enrol starts and ends, sorry it would be too complex)
145 $parentstatus = ENROL_USER_SUSPENDED
;
146 foreach ($parentues as $pue) {
147 if ($pue->status
== ENROL_USER_ACTIVE
) {
148 $parentstatus = ENROL_USER_ACTIVE
;
153 // enrol user if not enrolled yet or fix status
155 if ($parentstatus != $ue->status
) {
156 $plugin->update_user_enrol($instance, $userid, $parentstatus);
157 $ue->status
= $parentstatus;
160 $plugin->enrol_user($instance, $userid, NULL, 0, 0, $parentstatus);
161 $ue = new stdClass();
162 $ue->userid
= $userid;
163 $ue->enrolid
= $instance->id
;
164 $ue->status
= $parentstatus;
167 $unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES
);
169 // only active users in enabled instances are supposed to have roles (we can reassign the roles any time later)
170 if ($ue->status
!= ENROL_USER_ACTIVE
or $instance->status
!= ENROL_INSTANCE_ENABLED
) {
171 if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND
) {
172 // Always keep the roles.
174 role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id
, 'component'=>'enrol_meta', 'itemid'=>$instance->id
));
180 foreach ($parentroles as $rid) {
181 if (!isset($roles[$rid])) {
182 role_assign($rid, $userid, $context->id
, 'enrol_meta', $instance->id
);
186 if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND
) {
187 // Always keep the roles.
192 foreach ($roles as $rid) {
193 if (!isset($parentroles[$rid])) {
194 role_unassign($rid, $userid, $context->id
, 'enrol_meta', $instance->id
);
200 * Deal with users that are not supposed to be enrolled via this instance
202 * @param stdClass $instance
203 * @param stdClass $ue
204 * @param context_course $context
205 * @param enrol_meta $plugin
208 protected static function user_not_supposed_to_be_here($instance, $ue, context_course
$context, $plugin) {
210 // Not enrolled yet - simple!
214 $userid = $ue->userid
;
215 $unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES
);
217 if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL
) {
218 // Purges grades, group membership, preferences, etc. - admins were warned!
219 $plugin->unenrol_user($instance, $userid);
221 } else if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND
) {
222 if ($ue->status
!= ENROL_USER_SUSPENDED
) {
223 $plugin->update_user_enrol($instance, $userid, ENROL_USER_SUSPENDED
);
226 } else if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES
) {
227 if ($ue->status
!= ENROL_USER_SUSPENDED
) {
228 $plugin->update_user_enrol($instance, $userid, ENROL_USER_SUSPENDED
);
230 role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id
, 'component'=>'enrol_meta', 'itemid'=>$instance->id
));
233 debugging('Unknown unenrol action '.$unenrolaction);
239 * Sync all meta course links.
241 * @param int $courseid one course, empty mean all
242 * @param bool $verbose verbose CLI output
243 * @return int 0 means ok, 1 means error, 2 means plugin disabled
245 function enrol_meta_sync($courseid = NULL, $verbose = false) {
248 // purge all roles if meta sync disabled, those can be recreated later here in cron
249 if (!enrol_is_enabled('meta')) {
251 mtrace('Meta sync plugin is disabled, unassigning all plugin roles and stopping.');
253 role_unassign_all(array('component'=>'enrol_meta'));
257 // unfortunately this may take a long time, execution can be interrupted safely
258 core_php_time_limit
::raise();
259 raise_memory_limit(MEMORY_HUGE
);
262 mtrace('Starting user enrolment synchronisation...');
265 $instances = array(); // cache instances
267 $meta = enrol_get_plugin('meta');
269 $unenrolaction = $meta->get_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES
);
270 $skiproles = $meta->get_config('nosyncroleids', '');
271 $skiproles = empty($skiproles) ?
array() : explode(',', $skiproles);
272 $syncall = $meta->get_config('syncall', 1);
274 $allroles = get_all_roles();
277 // iterate through all not enrolled yet users
278 $onecourse = $courseid ?
"AND e.courseid = :courseid" : "";
279 list($enabled, $params) = $DB->get_in_or_equal(explode(',', $CFG->enrol_plugins_enabled
), SQL_PARAMS_NAMED
, 'e');
280 $params['courseid'] = $courseid;
281 $sql = "SELECT pue.userid, e.id AS enrolid, pue.status
282 FROM {user_enrolments} pue
283 JOIN {enrol} pe ON (pe.id = pue.enrolid AND pe.enrol <> 'meta' AND pe.enrol $enabled)
284 JOIN {enrol} e ON (e.customint1 = pe.courseid AND e.enrol = 'meta' $onecourse)
285 JOIN {user} u ON (u.id = pue.userid AND u.deleted = 0)
286 LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = pue.userid)
287 WHERE ue.id IS NULL";
289 $rs = $DB->get_recordset_sql($sql, $params);
290 foreach($rs as $ue) {
291 if (!isset($instances[$ue->enrolid
])) {
292 $instances[$ue->enrolid
] = $DB->get_record('enrol', array('id'=>$ue->enrolid
));
294 $instance = $instances[$ue->enrolid
];
297 // this may be slow if very many users are ignored in sync
298 $parentcontext = context_course
::instance($instance->customint1
);
299 list($ignoreroles, $params) = $DB->get_in_or_equal($skiproles, SQL_PARAMS_NAMED
, 'ri', false, -1);
300 $params['contextid'] = $parentcontext->id
;
301 $params['userid'] = $ue->userid
;
302 $select = "contextid = :contextid AND userid = :userid AND component <> 'enrol_meta' AND roleid $ignoreroles";
303 if (!$DB->record_exists_select('role_assignments', $select, $params)) {
304 // bad luck, this user does not have any role we want in parent course
306 mtrace(" skipping enrolling: $ue->userid ==> $instance->courseid (user without role)");
312 $meta->enrol_user($instance, $ue->userid
, $ue->status
);
314 mtrace(" enrolling: $ue->userid ==> $instance->courseid");
320 // unenrol as necessary - ignore enabled flag, we want to get rid of existing enrols in any case
321 $onecourse = $courseid ?
"AND e.courseid = :courseid" : "";
322 list($enabled, $params) = $DB->get_in_or_equal(explode(',', $CFG->enrol_plugins_enabled
), SQL_PARAMS_NAMED
, 'e');
323 $params['courseid'] = $courseid;
325 FROM {user_enrolments} ue
326 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'meta' $onecourse)
327 LEFT JOIN ({user_enrolments} xpue
328 JOIN {enrol} xpe ON (xpe.id = xpue.enrolid AND xpe.enrol <> 'meta' AND xpe.enrol $enabled)
329 ) ON (xpe.courseid = e.customint1 AND xpue.userid = ue.userid)
330 WHERE xpue.userid IS NULL";
331 $rs = $DB->get_recordset_sql($sql, $params);
332 foreach($rs as $ue) {
333 if (!isset($instances[$ue->enrolid
])) {
334 $instances[$ue->enrolid
] = $DB->get_record('enrol', array('id'=>$ue->enrolid
));
336 $instance = $instances[$ue->enrolid
];
338 if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL
) {
339 $meta->unenrol_user($instance, $ue->userid
);
341 mtrace(" unenrolling: $ue->userid ==> $instance->courseid");
344 } else if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND
) {
345 if ($ue->status
!= ENROL_USER_SUSPENDED
) {
346 $meta->update_user_enrol($instance, $ue->userid
, ENROL_USER_SUSPENDED
);
348 mtrace(" suspending: $ue->userid ==> $instance->courseid");
352 } else if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES
) {
353 if ($ue->status
!= ENROL_USER_SUSPENDED
) {
354 $meta->update_user_enrol($instance, $ue->userid
, ENROL_USER_SUSPENDED
);
355 $context = context_course
::instance($instance->courseid
);
356 role_unassign_all(array('userid'=>$ue->userid
, 'contextid'=>$context->id
, 'component'=>'enrol_meta', 'itemid'=>$instance->id
));
358 mtrace(" suspending and removing all roles: $ue->userid ==> $instance->courseid");
366 // update status - meta enrols + start and end dates are ignored, sorry
367 // note the trick here is that the active enrolment and instance constants have value 0
368 $onecourse = $courseid ?
"AND e.courseid = :courseid" : "";
369 list($enabled, $params) = $DB->get_in_or_equal(explode(',', $CFG->enrol_plugins_enabled
), SQL_PARAMS_NAMED
, 'e');
370 $params['courseid'] = $courseid;
371 $sql = "SELECT ue.userid, ue.enrolid, pue.pstatus
372 FROM {user_enrolments} ue
373 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'meta' $onecourse)
374 JOIN (SELECT xpue.userid, xpe.courseid, MIN(xpue.status + xpe.status) AS pstatus
375 FROM {user_enrolments} xpue
376 JOIN {enrol} xpe ON (xpe.id = xpue.enrolid AND xpe.enrol <> 'meta' AND xpe.enrol $enabled)
377 GROUP BY xpue.userid, xpe.courseid
378 ) pue ON (pue.courseid = e.customint1 AND pue.userid = ue.userid)
379 WHERE (pue.pstatus = 0 AND ue.status > 0) OR (pue.pstatus > 0 and ue.status = 0)";
380 $rs = $DB->get_recordset_sql($sql, $params);
381 foreach($rs as $ue) {
382 if (!isset($instances[$ue->enrolid
])) {
383 $instances[$ue->enrolid
] = $DB->get_record('enrol', array('id'=>$ue->enrolid
));
385 $instance = $instances[$ue->enrolid
];
386 $ue->pstatus
= ($ue->pstatus
== ENROL_USER_ACTIVE
) ? ENROL_USER_ACTIVE
: ENROL_USER_SUSPENDED
;
388 if ($ue->pstatus
== ENROL_USER_ACTIVE
and !$syncall and $unenrolaction != ENROL_EXT_REMOVED_UNENROL
) {
389 // this may be slow if very many users are ignored in sync
390 $parentcontext = context_course
::instance($instance->customint1
);
391 list($ignoreroles, $params) = $DB->get_in_or_equal($skiproles, SQL_PARAMS_NAMED
, 'ri', false, -1);
392 $params['contextid'] = $parentcontext->id
;
393 $params['userid'] = $ue->userid
;
394 $select = "contextid = :contextid AND userid = :userid AND component <> 'enrol_meta' AND roleid $ignoreroles";
395 if (!$DB->record_exists_select('role_assignments', $select, $params)) {
396 // bad luck, this user does not have any role we want in parent course
398 mtrace(" skipping unsuspending: $ue->userid ==> $instance->courseid (user without role)");
404 $meta->update_user_enrol($instance, $ue->userid
, $ue->pstatus
);
406 if ($ue->pstatus
== ENROL_USER_ACTIVE
) {
407 mtrace(" unsuspending: $ue->userid ==> $instance->courseid");
409 mtrace(" suspending: $ue->userid ==> $instance->courseid");
416 // now assign all necessary roles
417 $enabled = explode(',', $CFG->enrol_plugins_enabled
);
418 foreach($enabled as $k=>$v) {
420 continue; // no meta sync of meta roles
422 $enabled[$k] = 'enrol_'.$v;
424 $enabled[] = ''; // manual assignments are replicated too
426 $onecourse = $courseid ?
"AND e.courseid = :courseid" : "";
427 list($enabled, $params) = $DB->get_in_or_equal($enabled, SQL_PARAMS_NAMED
, 'e');
428 $params['coursecontext'] = CONTEXT_COURSE
;
429 $params['courseid'] = $courseid;
430 $params['activeuser'] = ENROL_USER_ACTIVE
;
431 $params['enabledinstance'] = ENROL_INSTANCE_ENABLED
;
432 $sql = "SELECT DISTINCT pra.roleid, pra.userid, c.id AS contextid, e.id AS enrolid, e.courseid
433 FROM {role_assignments} pra
434 JOIN {user} u ON (u.id = pra.userid AND u.deleted = 0)
435 JOIN {context} pc ON (pc.id = pra.contextid AND pc.contextlevel = :coursecontext AND pra.component $enabled)
436 JOIN {enrol} e ON (e.customint1 = pc.instanceid AND e.enrol = 'meta' $onecourse AND e.status = :enabledinstance)
437 JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = u.id AND ue.status = :activeuser)
438 JOIN {context} c ON (c.contextlevel = pc.contextlevel AND c.instanceid = e.courseid)
439 LEFT JOIN {role_assignments} ra ON (ra.contextid = c.id AND ra.userid = pra.userid AND ra.roleid = pra.roleid AND ra.itemid = e.id AND ra.component = 'enrol_meta')
440 WHERE ra.id IS NULL";
442 if ($ignored = $meta->get_config('nosyncroleids')) {
443 list($notignored, $xparams) = $DB->get_in_or_equal(explode(',', $ignored), SQL_PARAMS_NAMED
, 'ig', false);
444 $params = array_merge($params, $xparams);
445 $sql = "$sql AND pra.roleid $notignored";
448 $rs = $DB->get_recordset_sql($sql, $params);
449 foreach($rs as $ra) {
450 role_assign($ra->roleid
, $ra->userid
, $ra->contextid
, 'enrol_meta', $ra->enrolid
);
452 mtrace(" assigning role: $ra->userid ==> $ra->courseid as ".$allroles[$ra->roleid
]->shortname
);
458 // remove unwanted roles - include ignored roles and disabled plugins too
459 $onecourse = $courseid ?
"AND e.courseid = :courseid" : "";
461 $params['coursecontext'] = CONTEXT_COURSE
;
462 $params['courseid'] = $courseid;
463 $params['activeuser'] = ENROL_USER_ACTIVE
;
464 $params['enabledinstance'] = ENROL_INSTANCE_ENABLED
;
465 if ($ignored = $meta->get_config('nosyncroleids')) {
466 list($notignored, $xparams) = $DB->get_in_or_equal(explode(',', $ignored), SQL_PARAMS_NAMED
, 'ig', false);
467 $params = array_merge($params, $xparams);
468 $notignored = "AND pra.roleid $notignored";
473 $sql = "SELECT ra.roleid, ra.userid, ra.contextid, ra.itemid, e.courseid
474 FROM {role_assignments} ra
475 JOIN {enrol} e ON (e.id = ra.itemid AND ra.component = 'enrol_meta' AND e.enrol = 'meta' $onecourse)
476 JOIN {context} pc ON (pc.instanceid = e.customint1 AND pc.contextlevel = :coursecontext)
477 LEFT JOIN {role_assignments} pra ON (pra.contextid = pc.id AND pra.userid = ra.userid AND pra.roleid = ra.roleid AND pra.component <> 'enrol_meta' $notignored)
478 LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = ra.userid AND ue.status = :activeuser)
479 WHERE pra.id IS NULL OR ue.id IS NULL OR e.status <> :enabledinstance";
481 if ($unenrolaction != ENROL_EXT_REMOVED_SUSPEND
) {
482 $rs = $DB->get_recordset_sql($sql, $params);
483 foreach($rs as $ra) {
484 role_unassign($ra->roleid
, $ra->userid
, $ra->contextid
, 'enrol_meta', $ra->itemid
);
486 mtrace(" unassigning role: $ra->userid ==> $ra->courseid as ".$allroles[$ra->roleid
]->shortname
);
493 // kick out or suspend users without synced roles if syncall disabled
495 if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL
) {
496 $onecourse = $courseid ?
"AND e.courseid = :courseid" : "";
498 $params['coursecontext'] = CONTEXT_COURSE
;
499 $params['courseid'] = $courseid;
500 $sql = "SELECT ue.userid, ue.enrolid
501 FROM {user_enrolments} ue
502 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'meta' $onecourse)
503 JOIN {context} c ON (e.courseid = c.instanceid AND c.contextlevel = :coursecontext)
504 LEFT JOIN {role_assignments} ra ON (ra.contextid = c.id AND ra.itemid = e.id AND ra.userid = ue.userid)
505 WHERE ra.id IS NULL";
506 $ues = $DB->get_recordset_sql($sql, $params);
507 foreach($ues as $ue) {
508 if (!isset($instances[$ue->enrolid
])) {
509 $instances[$ue->enrolid
] = $DB->get_record('enrol', array('id'=>$ue->enrolid
));
511 $instance = $instances[$ue->enrolid
];
512 $meta->unenrol_user($instance, $ue->userid
);
514 mtrace(" unenrolling: $ue->userid ==> $instance->courseid (user without role)");
520 // just suspend the users
521 $onecourse = $courseid ?
"AND e.courseid = :courseid" : "";
523 $params['coursecontext'] = CONTEXT_COURSE
;
524 $params['courseid'] = $courseid;
525 $params['active'] = ENROL_USER_ACTIVE
;
526 $sql = "SELECT ue.userid, ue.enrolid
527 FROM {user_enrolments} ue
528 JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'meta' $onecourse)
529 JOIN {context} c ON (e.courseid = c.instanceid AND c.contextlevel = :coursecontext)
530 LEFT JOIN {role_assignments} ra ON (ra.contextid = c.id AND ra.itemid = e.id AND ra.userid = ue.userid)
531 WHERE ra.id IS NULL AND ue.status = :active";
532 $ues = $DB->get_recordset_sql($sql, $params);
533 foreach($ues as $ue) {
534 if (!isset($instances[$ue->enrolid
])) {
535 $instances[$ue->enrolid
] = $DB->get_record('enrol', array('id'=>$ue->enrolid
));
537 $instance = $instances[$ue->enrolid
];
538 $meta->update_user_enrol($instance, $ue->userid
, ENROL_USER_SUSPENDED
);
540 mtrace(" suspending: $ue->userid ==> $instance->courseid (user without role)");
548 mtrace('...user enrolment synchronisation finished.');