Skip to content

Commit

Permalink
Merge branch 'w04_MDL-37716_m25_enrolexpiry' of git://github.com/skod…
Browse files Browse the repository at this point in the history
…ak/moodle
  • Loading branch information
Sam Hemelryk committed Jan 29, 2013
2 parents 09d6bcc + d8f22c4 commit ab58c6b
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 269 deletions.
106 changes: 0 additions & 106 deletions enrol/flatfile/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,112 +614,6 @@ protected function process_records(progress_trace $trace, $action, $roleid, $use
}
}

/**
* Do any enrolment expiration processing.
*
* @param progress_trace $trace
* @return bool true if any data processed, false if not
*/
protected function process_expirations(progress_trace $trace) {
global $DB;

//TODO: this method should be moved to parent class once we refactor all existing enrols, see MDL-36504.

$processed = false;
$name = $this->get_name();

// Deal with expired accounts.
$action = $this->get_config('expiredaction', ENROL_EXT_REMOVED_KEEP);

if ($action == ENROL_EXT_REMOVED_UNENROL) {
$instances = array();
$sql = "SELECT ue.*, e.courseid, c.id AS contextid
FROM {user_enrolments} ue
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = :enrol)
JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :courselevel)
WHERE ue.timeend > 0 AND ue.timeend < :now";
$params = array('now'=>time(), 'courselevel'=>CONTEXT_COURSE, 'enrol'=>$name);

$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $ue) {
if (!$processed) {
$trace->output("Starting processing of enrol_$name expirations...");
$processed = true;
}
if (empty($instances[$ue->enrolid])) {
$instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
}
$instance = $instances[$ue->enrolid];
if (!$this->roles_protected()) {
// Let's just guess what extra roles are supposed to be removed.
if ($instance->roleid) {
role_unassign($instance->roleid, $ue->userid, $ue->contextid);
}
}
// The unenrol cleans up all subcontexts if this is the only course enrolment for this user.
$this->unenrol_user($instance, $ue->userid);
$trace->output("Unenrolling expired user $ue->userid from course $instance->courseid", 1);
}
$rs->close();
unset($instances);

} else if ($action == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
$instances = array();
$sql = "SELECT ue.*, e.courseid, c.id AS contextid
FROM {user_enrolments} ue
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = :enrol)
JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :courselevel)
WHERE ue.timeend > 0 AND ue.timeend < :now
AND ue.status = :useractive";
$params = array('now'=>time(), 'courselevel'=>CONTEXT_COURSE, 'useractive'=>ENROL_USER_ACTIVE, 'enrol'=>$name);
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $ue) {
if (!$processed) {
$trace->output("Starting processing of enrol_$name expirations...");
$processed = true;
}
if (empty($instances[$ue->enrolid])) {
$instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
}
$instance = $instances[$ue->enrolid];

if (!$this->roles_protected()) {
// Let's just guess what roles should be removed.
$count = $DB->count_records('role_assignments', array('userid'=>$ue->userid, 'contextid'=>$ue->contextid));
if ($count == 1) {
role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$ue->contextid, 'component'=>'', 'itemid'=>0));

} else if ($count > 1 and $instance->roleid) {
role_unassign($instance->roleid, $ue->userid, $ue->contextid, '', 0);
}
}
// In any case remove all roles that belong to this instance and user.
role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$ue->contextid, 'component'=>'enrol_'.$name, 'itemid'=>$instance->id), true);
// Final cleanup of subcontexts if there are no more course roles.
if (0 == $DB->count_records('role_assignments', array('userid'=>$ue->userid, 'contextid'=>$ue->contextid))) {
role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$ue->contextid, 'component'=>'', 'itemid'=>0), true);
}

$this->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
$trace->output("Suspending expired user $ue->userid in course $instance->courseid", 1);
}
$rs->close();
unset($instances);

} else {
// ENROL_EXT_REMOVED_KEEP means no changes.
}

if ($processed) {
$trace->output("...finished processing of enrol_$name expirations");
} else {
$trace->output("No expired enrol_$name enrolments detected");
}
$trace->finished();

return $processed;
}

/**
* Returns the user who is responsible for flatfile enrolments in given curse.
*
Expand Down
106 changes: 0 additions & 106 deletions enrol/paypal/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,110 +302,4 @@ public function sync(progress_trace $trace) {
$this->process_expirations($trace);
return 0;
}

/**
* Do any enrolment expiration processing.
*
* @param progress_trace $trace
* @return bool true if any data processed, false if not
*/
protected function process_expirations(progress_trace $trace) {
global $DB;

//TODO: this method should be moved to parent class once we refactor all existing enrols, see MDL-36504.

$processed = false;
$name = $this->get_name();

// Deal with expired accounts.
$action = $this->get_config('expiredaction', ENROL_EXT_REMOVED_KEEP);

if ($action == ENROL_EXT_REMOVED_UNENROL) {
$instances = array();
$sql = "SELECT ue.*, e.courseid, c.id AS contextid
FROM {user_enrolments} ue
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = :enrol)
JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :courselevel)
WHERE ue.timeend > 0 AND ue.timeend < :now";
$params = array('now'=>time(), 'courselevel'=>CONTEXT_COURSE, 'enrol'=>$name);

$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $ue) {
if (!$processed) {
$trace->output("Starting processing of enrol_$name expirations...");
$processed = true;
}
if (empty($instances[$ue->enrolid])) {
$instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
}
$instance = $instances[$ue->enrolid];
if (!$this->roles_protected()) {
// Let's just guess what extra roles are supposed to be removed.
if ($instance->roleid) {
role_unassign($instance->roleid, $ue->userid, $ue->contextid);
}
}
// The unenrol cleans up all subcontexts if this is the only course enrolment for this user.
$this->unenrol_user($instance, $ue->userid);
$trace->output("Unenrolling expired user $ue->userid from course $instance->courseid", 1);
}
$rs->close();
unset($instances);

} else if ($action == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
$instances = array();
$sql = "SELECT ue.*, e.courseid, c.id AS contextid
FROM {user_enrolments} ue
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = :enrol)
JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :courselevel)
WHERE ue.timeend > 0 AND ue.timeend < :now
AND ue.status = :useractive";
$params = array('now'=>time(), 'courselevel'=>CONTEXT_COURSE, 'useractive'=>ENROL_USER_ACTIVE, 'enrol'=>$name);
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $ue) {
if (!$processed) {
$trace->output("Starting processing of enrol_$name expirations...");
$processed = true;
}
if (empty($instances[$ue->enrolid])) {
$instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
}
$instance = $instances[$ue->enrolid];

if (!$this->roles_protected()) {
// Let's just guess what roles should be removed.
$count = $DB->count_records('role_assignments', array('userid'=>$ue->userid, 'contextid'=>$ue->contextid));
if ($count == 1) {
role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$ue->contextid, 'component'=>'', 'itemid'=>0));

} else if ($count > 1 and $instance->roleid) {
role_unassign($instance->roleid, $ue->userid, $ue->contextid, '', 0);
}
}
// In any case remove all roles that belong to this instance and user.
role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$ue->contextid, 'component'=>'enrol_'.$name, 'itemid'=>$instance->id), true);
// Final cleanup of subcontexts if there are no more course roles.
if (0 == $DB->count_records('role_assignments', array('userid'=>$ue->userid, 'contextid'=>$ue->contextid))) {
role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$ue->contextid, 'component'=>'', 'itemid'=>0), true);
}

$this->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
$trace->output("Suspending expired user $ue->userid in course $instance->courseid", 1);
}
$rs->close();
unset($instances);

} else {
// ENROL_EXT_REMOVED_KEEP means no changes.
}

if ($processed) {
$trace->output("...finished processing of enrol_$name expirations");
} else {
$trace->output("No expired enrol_$name enrolments detected");
}
$trace->finished();

return $processed;
}
}
58 changes: 2 additions & 56 deletions enrol/self/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,65 +446,11 @@ public function sync(progress_trace $trace, $courseid = null) {
}
$rs->close();

// Deal with expired accounts.
$action = $this->get_config('expiredaction', ENROL_EXT_REMOVED_KEEP);

if ($action == ENROL_EXT_REMOVED_UNENROL) {
$instances = array();
$sql = "SELECT ue.*, e.courseid, c.id AS contextid
FROM {user_enrolments} ue
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self')
JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :courselevel)
WHERE ue.timeend > 0 AND ue.timeend < :now
$coursesql";
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $ue) {
if (empty($instances[$ue->enrolid])) {
$instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
}
$instance = $instances[$ue->enrolid];
if ($instance->roleid) {
role_unassign($instance->roleid, $ue->userid, $ue->contextid, '', 0);
}
$this->unenrol_user($instance, $ue->userid);
$trace->output("unenrolling expired user $ue->userid from course $instance->courseid", 1);
}
$rs->close();
unset($instances);

} else if ($action == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
$instances = array();
$sql = "SELECT ue.*, e.courseid, c.id AS contextid
FROM {user_enrolments} ue
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'self')
JOIN {context} c ON (c.instanceid = e.courseid AND c.contextlevel = :courselevel)
WHERE ue.timeend > 0 AND ue.timeend < :now
AND ue.status = :useractive
$coursesql";
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $ue) {
if (empty($instances[$ue->enrolid])) {
$instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
}
$instance = $instances[$ue->enrolid];
if (1 == $DB->count_records('role_assignments', array('userid'=>$ue->userid, 'contextid'=>$ue->contextid))) {
role_unassign_all(array('userid'=>$ue->userid, 'contextid'=>$ue->contextid, 'component'=>'', 'itemid'=>0), true);
} else if ($instance->roleid) {
role_unassign($instance->roleid, $ue->userid, $ue->contextid, '', 0);
}
$this->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
$trace->output("suspending expired user $ue->userid in course $instance->courseid", 1);
}
$rs->close();
unset($instances);

} else {
// ENROL_EXT_REMOVED_KEEP means no changes.
}

$trace->output('...user self-enrolment updates finished.');
$trace->finished();

$this->process_expirations($trace, $courseid);

return 0;
}

Expand Down
Loading

0 comments on commit ab58c6b

Please sign in to comment.