diff --git a/lib.php b/lib.php index e710abf..a928cbd 100644 --- a/lib.php +++ b/lib.php @@ -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. @@ -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); } } diff --git a/tests/lib_test.php b/tests/lib_test.php index 26ec22b..f15ef2d 100644 --- a/tests/lib_test.php +++ b/tests/lib_test.php @@ -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. @@ -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);