MDL-67458 tool_cohortroles: remove role mapping for deleted roles
authorJake Dallimore <jake@moodle.com>
Fri, 6 Dec 2019 02:34:58 +0000 (10:34 +0800)
committerJake Dallimore <jake@moodle.com>
Wed, 11 Dec 2019 01:33:10 +0000 (09:33 +0800)
Before syncing anything, clean up records pointing to deleted roles.

admin/tool/cohortroles/classes/api.php

index e94710f..89f54ae 100644 (file)
@@ -144,6 +144,10 @@ class api {
         $rolesadded = array();
         $rolesremoved = array();
 
+        // Remove any cohort role mappings for roles which have been deleted.
+        // The role assignments are not a consideration because these will have been removed when the role was.
+        $DB->delete_records_select('tool_cohortroles', "roleid NOT IN (SELECT id FROM {role})");
+
         // Get all cohort role assignments and group them by user and role.
         $all = cohort_role_assignment::get_records(array(), 'userid, roleid');
         // We build an better structure to loop on.