Skip to content

Commit

Permalink
MDL-39786 add support for suspend only in meta course plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jul 26, 2013
1 parent bdd045c commit 1344bc7
Show file tree
Hide file tree
Showing 3 changed files with 519 additions and 45 deletions.
115 changes: 72 additions & 43 deletions enrol/meta/locallib.php
Expand Up @@ -90,12 +90,6 @@ protected static function sync_with_parent_course(stdClass $instance, $userid) {

$context = context_course::instance($instance->courseid);

if (!$parentcontext = context_course::instance($instance->customint1, IGNORE_MISSING)) {
// linking to missing course is not possible
role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id, 'component'=>'enrol_meta'));
return;
}

// list of enrolments in parent course (we ignore meta enrols in parents completely)
list($enabled, $params) = $DB->get_in_or_equal(explode(',', $CFG->enrol_plugins_enabled), SQL_PARAMS_NAMED, 'e');
$params['userid'] = $userid;
Expand All @@ -114,10 +108,8 @@ protected static function sync_with_parent_course(stdClass $instance, $userid) {
return;
}

if (!enrol_is_enabled('meta')) {
if ($ue) {
role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id, 'component'=>'enrol_meta'));
}
if (!$parentcontext = context_course::instance($instance->customint1, IGNORE_MISSING)) {
// Weird, we should not get here.
return;
}

Expand Down Expand Up @@ -172,9 +164,13 @@ protected static function sync_with_parent_course(stdClass $instance, $userid) {
$ue->status = $parentstatus;
}

$unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);

// only active users in enabled instances are supposed to have roles (we can reassign the roles any time later)
if ($ue->status != ENROL_USER_ACTIVE or $instance->status != ENROL_INSTANCE_ENABLED) {
if ($roles) {
if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND) {
// Always keep the roles.
} else if ($roles) {
role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id, 'component'=>'enrol_meta', 'itemid'=>$instance->id));
}
return;
Expand All @@ -187,6 +183,11 @@ protected static function sync_with_parent_course(stdClass $instance, $userid) {
}
}

if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND) {
// Always keep the roles.
return;
}

// remove roles
foreach ($roles as $rid) {
if (!isset($parentroles[$rid])) {
Expand All @@ -206,25 +207,30 @@ protected static function sync_with_parent_course(stdClass $instance, $userid) {
*/
protected static function user_not_supposed_to_be_here($instance, $ue, context_course $context, $plugin) {
if (!$ue) {
// not enrolled yet - simple!
// Not enrolled yet - simple!
return;
}

$userid = $ue->userid;
$unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
$unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);

if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) {
// purges grades, group membership, preferences, etc. - admins were warned!
// Purges grades, group membership, preferences, etc. - admins were warned!
$plugin->unenrol_user($instance, $userid);
return;

} else { // ENROL_EXT_REMOVED_SUSPENDNOROLES
// just suspend users and remove all roles (we can reassign the roles any time later)
} else if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND) {
if ($ue->status != ENROL_USER_SUSPENDED) {
$plugin->update_user_enrol($instance, $userid, ENROL_USER_SUSPENDED);
role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id, 'component'=>'enrol_meta', 'itemid'=>$instance->id));
}
return;

} else if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
if ($ue->status != ENROL_USER_SUSPENDED) {
$plugin->update_user_enrol($instance, $userid, ENROL_USER_SUSPENDED);
}
role_unassign_all(array('userid'=>$userid, 'contextid'=>$context->id, 'component'=>'enrol_meta', 'itemid'=>$instance->id));

} else {
debugging('Unknown unenrol action '.$unenrolaction);
}
}

Expand Down Expand Up @@ -316,8 +322,10 @@ public static function user_enrolled($ue) {
* @return bool success
*/
public static function user_unenrolled($ue) {

// keep unenrolling even if plugin disabled
if (!enrol_is_enabled('meta')) {
// This is slow, let enrol_meta_sync() deal with disabled plugin.
return true;
}

if ($ue->enrol === 'meta') {
// prevent circular dependencies - we can not sync meta enrolments recursively
Expand Down Expand Up @@ -360,27 +368,40 @@ public static function user_enrol_modified($ue) {
public static function course_deleted($course) {
global $DB;

// NOTE: do not test if plugin enabled, we want to keep disabling instances with invalid course links
if (!enrol_is_enabled('meta')) {
// This is slow, let enrol_meta_sync() deal with disabled plugin.
return true;
}

// does anything want to sync with this parent?
if (!$enrols = $DB->get_records('enrol', array('customint1'=>$course->id, 'enrol'=>'meta'), 'courseid ASC, id ASC')) {
return true;
}

$plugin = enrol_get_plugin('meta');
$unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);

// hack the DB info for all courses first
foreach ($enrols as $enrol) {
$enrol->customint1 = 0;
$enrol->status = ENROL_INSTANCE_DISABLED;
$DB->update_record('enrol', $enrol);
$context = context_course::instance($enrol->courseid);
role_unassign_all(array('contextid'=>$context->id, 'component'=>'enrol_meta', 'itemid'=>$enrol->id));
if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) {
// Simple, just delete this instance which purges all enrolments,
// admins were warned that this is risky setting!
foreach ($enrols as $enrol) {
$plugin->delete_instance($enrol);
}
return true;
}

// now trigger sync for each instance and purge caches
foreach ($enrols as $enrol) {
$plugin->update_status($enrol, ENROL_INSTANCE_DISABLED);
$enrol->customint = 0;
$DB->update_record('enrol', $enrol);

if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND or $unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
// This makes all enrolments suspended very quickly.
$plugin->update_status($enrol, ENROL_INSTANCE_DISABLED);
}
if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
$context = context_course::instance($enrol->courseid);
role_unassign_all(array('contextid'=>$context->id, 'component'=>'enrol_meta', 'itemid'=>$enrol->id));
}
}

return true;
Expand Down Expand Up @@ -419,7 +440,7 @@ function enrol_meta_sync($courseid = NULL, $verbose = false) {

$meta = enrol_get_plugin('meta');

$unenrolaction = $meta->get_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
$unenrolaction = $meta->get_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
$skiproles = $meta->get_config('nosyncroleids', '');
$skiproles = empty($skiproles) ? array() : explode(',', $skiproles);
$syncall = $meta->get_config('syncall', 1);
Expand Down Expand Up @@ -493,19 +514,24 @@ function enrol_meta_sync($courseid = NULL, $verbose = false) {
if ($verbose) {
mtrace(" unenrolling: $ue->userid ==> $instance->courseid");
}
continue;

} else { // ENROL_EXT_REMOVED_SUSPENDNOROLES
// just disable and ignore any changes
} else if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND) {
if ($ue->status != ENROL_USER_SUSPENDED) {
$meta->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
if ($verbose) {
mtrace(" suspending: $ue->userid ==> $instance->courseid");
}
}

} else if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
if ($ue->status != ENROL_USER_SUSPENDED) {
$meta->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
$context = context_course::instance($instance->courseid);
role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$context->id, 'component'=>'enrol_meta'));
role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$context->id, 'component'=>'enrol_meta', 'itemid'=>$instance->id));
if ($verbose) {
mtrace(" suspending and removing all roles: $ue->userid ==> $instance->courseid");
}
}
continue;
}
}
$rs->close();
Expand Down Expand Up @@ -617,6 +643,7 @@ function enrol_meta_sync($courseid = NULL, $verbose = false) {
} else {
$notignored = "";
}

$sql = "SELECT ra.roleid, ra.userid, ra.contextid, ra.itemid, e.courseid
FROM {role_assignments} ra
JOIN {enrol} e ON (e.id = ra.itemid AND ra.component = 'enrol_meta' AND e.enrol = 'meta' $onecourse)
Expand All @@ -625,14 +652,16 @@ function enrol_meta_sync($courseid = NULL, $verbose = false) {
LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = ra.userid AND ue.status = :activeuser)
WHERE pra.id IS NULL OR ue.id IS NULL OR e.status <> :enabledinstance";

$rs = $DB->get_recordset_sql($sql, $params);
foreach($rs as $ra) {
role_unassign($ra->roleid, $ra->userid, $ra->contextid, 'enrol_meta', $ra->itemid);
if ($verbose) {
mtrace(" unassigning role: $ra->userid ==> $ra->courseid as ".$allroles[$ra->roleid]->shortname);
if ($unenrolaction != ENROL_EXT_REMOVED_SUSPEND) {
$rs = $DB->get_recordset_sql($sql, $params);
foreach($rs as $ra) {
role_unassign($ra->roleid, $ra->userid, $ra->contextid, 'enrol_meta', $ra->itemid);
if ($verbose) {
mtrace(" unassigning role: $ra->userid ==> $ra->courseid as ".$allroles[$ra->roleid]->shortname);
}
}
$rs->close();
}
$rs->close();


// kick out or suspend users without synced roles if syncall disabled
Expand Down
6 changes: 4 additions & 2 deletions enrol/meta/settings.php
Expand Up @@ -36,8 +36,10 @@
$settings->add(new admin_setting_configcheckbox('enrol_meta/syncall', get_string('syncall', 'enrol_meta'), get_string('syncall_desc', 'enrol_meta'), 1));

$options = array(
ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'enrol'),
ENROL_EXT_REMOVED_SUSPENDNOROLES => get_string('extremovedsuspendnoroles', 'enrol'));
ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'core_enrol'),
ENROL_EXT_REMOVED_SUSPEND => get_string('extremovedsuspend', 'core_enrol'),
ENROL_EXT_REMOVED_SUSPENDNOROLES => get_string('extremovedsuspendnoroles', 'core_enrol'),
);
$settings->add(new admin_setting_configselect('enrol_meta/unenrolaction', get_string('extremovedaction', 'enrol'), get_string('extremovedaction_help', 'enrol'), ENROL_EXT_REMOVED_SUSPENDNOROLES, $options));
}
}

0 comments on commit 1344bc7

Please sign in to comment.