Skip to content

Commit

Permalink
corrects closing of users loop.
Browse files Browse the repository at this point in the history
see #50

this addresses the regression outline in
  • Loading branch information
stopfstedt committed Oct 1, 2024
1 parent 134c4d5 commit 9b44e05
Showing 1 changed file with 56 additions and 55 deletions.
111 changes: 56 additions & 55 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public function sync($trace, $courseid = null): int {
if (!empty($user->campusId)) {
$urec = $DB->get_record('user', ["idnumber" => $user->campusId]);
if (!empty($urec)) {
$iliosusers[$user->id] = [ 'id' => $urec->id, 'syncfield' => $urec->idnumber ];
$iliosusers[$user->id] = ['id' => $urec->id, 'syncfield' => $urec->idnumber];
}
}
}
Expand Down Expand Up @@ -328,19 +328,19 @@ public function sync($trace, $courseid = null): int {
}

// Don't re-enroll suspended enrollments for disabled Ilios users.
if (!empty($ue) && ENROL_USER_SUSPENDED === (int) $ue->status && !$user->enabled) {
if (!empty($ue) && ENROL_USER_SUSPENDED === (int)$ue->status && !$user->enabled) {
continue;
}

// Flag actively enrolled users that are disabled in Ilios
// for enrollment suspension further downstream.
if (!empty($ue) && ENROL_USER_ACTIVE === (int) $ue->status && !$user->enabled) {
if (!empty($ue) && ENROL_USER_ACTIVE === (int)$ue->status && !$user->enabled) {
$suspendenrolments[] = $ue;
continue;
}

// Continue if already enrolled with active status.
if (!empty($ue) && ENROL_USER_ACTIVE === (int) $ue->status) {
if (!empty($ue) && ENROL_USER_ACTIVE === (int)$ue->status) {
continue;
}

Expand All @@ -353,7 +353,7 @@ public function sync($trace, $courseid = null): int {
0,
ENROL_USER_ACTIVE
);
if (!empty($ue) && ENROL_USER_ACTIVE !== (int) $ue->status) {
if (!empty($ue) && ENROL_USER_ACTIVE !== (int)$ue->status) {
$trace->output(
"changing enrollment status to '"
. ENROL_USER_ACTIVE
Expand All @@ -371,70 +371,71 @@ public function sync($trace, $courseid = null): int {
);
}
}
}

// Suspend active enrollments for users that are disabled in Ilios.
foreach ($suspendenrolments as $ue) {
$trace->output(
"Suspending enrollment for disabled Ilios user: userid "
. " {$ue->userid} ==> courseid {$instance->courseid}."
, 1
);
$this->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
}

// Unenrol as necessary.
// Suspend active enrollments for users that are disabled in Ilios.
foreach ($suspendenrolments as $ue) {
$trace->output(
"Unenrolling users from Course ID "
. $instance->courseid." with Role ID "
. $instance->roleid
. " that no longer associate with Ilios Sync ID "
. $instance->id
. "."
"Suspending enrollment for disabled Ilios user: userid "
. " {$ue->userid} ==> courseid {$instance->courseid}."
, 1
);
$this->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
}

$sql = "SELECT ue.*
FROM {user_enrolments} ue
WHERE ue.enrolid = $instance->id";
// Unenrol as necessary.
$trace->output(
"Unenrolling users from Course ID "
. $instance->courseid." with Role ID "
. $instance->roleid
. " that no longer associate with Ilios Sync ID "
. $instance->id
. "."
);

if (!empty($enrolleduserids)) {
$sql .= " AND ue.userid NOT IN ( ".implode(",", $enrolleduserids)." )";
}
$sql = "SELECT ue.*
FROM {user_enrolments} ue
WHERE ue.enrolid = $instance->id";

if (!empty($enrolleduserids)) {
$sql .= " AND ue.userid NOT IN ( ".implode(",", $enrolleduserids)." )";
}

$rs = $DB->get_recordset_sql($sql);
foreach ($rs as $ue) {
if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) {
// Remove enrolment together with group membership, grades, preferences, etc.
$this->unenrol_user($instance, $ue->userid);
$rs = $DB->get_recordset_sql($sql);
foreach ($rs as $ue) {
if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) {
// Remove enrolment together with group membership, grades, preferences, etc.
$this->unenrol_user($instance, $ue->userid);
$trace->output(
"unenrolling: $ue->userid ==> "
. $instance->courseid
. " via Ilios $synctype $syncid"
, 1
);
} else { // Would be ENROL_EXT_REMOVED_SUSPENDNOROLES.
// Just disable and ignore any changes.
if ($ue->status != ENROL_USER_SUSPENDED) {
$this->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
$context = context_course::instance($instance->courseid);
role_unassign_all([
'userid' => $ue->userid,
'contextid' => $context->id,
'component' => 'enrol_ilios',
'itemid' => $instance->id,
]);
$trace->output(
"unenrolling: $ue->userid ==> "
"suspending and unassigning all roles: userid "
. $ue->userid
. " ==> courseid "
. $instance->courseid
. " via Ilios $synctype $syncid"
, 1
);
} else { // Would be ENROL_EXT_REMOVED_SUSPENDNOROLES.
// Just disable and ignore any changes.
if ($ue->status != ENROL_USER_SUSPENDED) {
$this->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
$context = context_course::instance($instance->courseid);
role_unassign_all([
'userid' => $ue->userid,
'contextid' => $context->id,
'component' => 'enrol_ilios',
'itemid' => $instance->id,
]);
$trace->output(
"suspending and unassigning all roles: userid "
. $ue->userid
. " ==> courseid "
. $instance->courseid
, 1
);
}
}
}
$rs->close();
}
$rs->close();
}

$instances->close();
unset($iliosusers);

Expand Down

0 comments on commit 9b44e05

Please sign in to comment.