Skip to content

Commit

Permalink
Bug fix in function to clean up teams connections on invalid or delet…
Browse files Browse the repository at this point in the history
…ed teams
  • Loading branch information
weilai-irl committed Feb 4, 2025
1 parent d65c9fc commit 6ab46aa
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions local/o365/classes/feature/coursesync/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -1130,11 +1130,31 @@ public function cleanup_teams_connections() {
if ($teamobjectids) {
// If there are records in teams cache, delete teams connection records with object IDs not in the cache.
[$teamobjectidsql, $params] = $DB->get_in_or_equal($teamobjectids, SQL_PARAMS_QM, 'param', false);
$DB->delete_records_select('local_o365_objects',
"type = 'group' AND subtype IN ('classteam', 'teamfromgroup') AND objectid {$teamobjectidsql}", $params);

if (count($params) < 65535) {
$DB->delete_records_select('local_o365_objects',
"type = 'group' AND subtype IN ('courseteam', 'teamfromgroup') AND objectid {$teamobjectidsql}", $params);
} else {
// PostgreSQL can't handle more than 65535 parameters in a query. Special care is needed.
$groupobjectids = $DB->get_records_select_menu('local_o365_objects',
"type = 'group' AND subtype IN ('courseteam', 'teamfromgroup')", [], '', 'objectid, id');
$objectrecordidstodelete = [];
foreach ($groupobjectids as $objectid => $objectrecordid) {
if (!in_array($objectid, $teamobjectids)) {
$objectrecordidstodelete[] = $objectrecordid;
}
}
if ($objectrecordidstodelete) {
$objectrecordidstodeletechunk = array_chunk($objectrecordidstodelete, 10000);
foreach ($objectrecordidstodeletechunk as $objectrecordidstodelete) {
[$teamsobjectidsql, $params] = $DB->get_in_or_equal($objectrecordidstodelete);
$DB->delete_records_select('local_o365_objects', "id {$teamsobjectidsql}", $params);
}
}
}
} else {
// If there are no records in teams cache, delete all teams connection records.
$DB->delete_records_select('local_o365_objects', "type = 'group' AND subtype IN ('classteam', 'teamfromgroup')");
$DB->delete_records_select('local_o365_objects', "type = 'group' AND subtype IN ('courseteam', 'teamfromgroup')");
}
$this->mtrace('Finished cleaning up teams connection records.');
$this->mtrace('');
Expand Down

0 comments on commit 6ab46aa

Please sign in to comment.