Skip to content

Commit

Permalink
log out duplicate campus ids.
Browse files Browse the repository at this point in the history
  • Loading branch information
stopfstedt committed Feb 10, 2025
1 parent b1f4c89 commit 8a8edf6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,18 +692,23 @@ public function restore_user_enrolment(
* 2. If all duplicates are inactive user records, then use the most recently created one.
* 3. If there is a mix between active and inactive user records, then use the most recently created active one.
* @param array $users An array of Ilios user records.
* @param progress_trace $trace A logger ("trace") object.
* @return array The given array of Ilios user records, with duplicates removed.
*/
protected function deduplicate_ilios_users(array $users): array {
protected function deduplicate_ilios_users(array $users, progress_trace $trace): array {
// Reverse-sort users by their user ID. In other words, from most-recently created to oldest.
array_multisort(array_column($users, 'id'), SORT_DESC, SORT_NUMERIC, $users);
$cache = [];
$duplicatekeys = [];
foreach ($users as $user) {
$key = $user->campusId;
// If this is the first time we encounter this user, cache them and move on.
if (!array_key_exists($key, $cache)) {
$cache[$key] = $user;
continue;
} else {
// Track the duplicate campus id.
$duplicatekeys[] = $key;
}

// And now we're dealing with duplicates.
Expand All @@ -714,6 +719,15 @@ protected function deduplicate_ilios_users(array $users): array {
}
}

// Log the duplicate campus IDs.
if (!empty($duplicatekeys)) {
sort($duplicatekeys);
$trace->output(
'Duplicate Ilios user records found, with the following campus IDs: '
. implode(', ', array_unique($duplicatekeys))
);
}

return array_values($cache);
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,11 @@ public function test_sync_deduplication_of_ilios_users_by_campus_id(): void {
"unassigning role: {$user5->id} ==> {$course->id} as {$studentrole->shortname}",
$output,
);
$this->assertStringContainsString(
'Duplicate Ilios user records found, with the following campus IDs: '
. 'xx1000001, xx1000002, xx1000003, xx1000004, xx1000005',
$output
);

// Check user enrollments and role assignments post-sync.
// Users 1-4 should still be actively enrolled as students.
Expand Down Expand Up @@ -2201,6 +2206,11 @@ public function test_sync_deduplication_of_ilios_users_by_campus_id(): void {
$trace->reset_buffer();

// Check the logging output.
$this->assertStringContainsString(
'Duplicate Ilios user records found, with the following campus IDs: '
. 'xx1000001, xx1000002, xx1000003, xx1000004, xx1000005',
$output
);
// There should be nothing in there pertaining to new (un-)enrollments nor role (un-)assignments.
$this->assertStringNotContainsString('unenrolling:', $output);
$this->assertStringNotContainsString('unassigning role:', $output);
Expand Down

0 comments on commit 8a8edf6

Please sign in to comment.