MDL-63764 core_competency: Support bulk user delete course competencies
authorMichael Hawkins <michaelh@moodle.com>
Fri, 26 Oct 2018 03:21:41 +0000 (11:21 +0800)
committerMichael Hawkins <michaelh@moodle.com>
Mon, 29 Oct 2018 04:59:55 +0000 (12:59 +0800)
This issue is a part of the MDL-62560 Epic.
Also made user IDs optional, so user course competencies can be deleted by course ID only.

competency/classes/privacy/provider.php

index 39ff45c..dd0191d 100644 (file)
@@ -478,8 +478,7 @@ class provider implements
                 break;
 
             case CONTEXT_COURSE:
-                $courseid = $context->instanceid;
-                $DB->delete_records(user_competency_course::TABLE, ['courseid' => $courseid]);
+                static::delete_user_competencies_in_course($context->instanceid);
                 break;
         }
     }
@@ -511,7 +510,7 @@ class provider implements
                     break;
 
                 case CONTEXT_COURSE:
-                    static::delete_user_competencies_in_course($userid, $context->instanceid);
+                    static::delete_user_competencies_in_course($context->instanceid, [$userid]);
                     break;
             }
         }
@@ -602,15 +601,26 @@ class provider implements
     }
 
     /**
-     * Delete the record of competencies for a user in a course.
+     * Delete the record of competencies for user(s) in a course.
      *
-     * @param int $userid The user ID.
      * @param int $courseid The course ID.
+     * @param int[] $userids The user IDs, if deleting for specific user(s).
      * @return void
      */
-    protected static function delete_user_competencies_in_course($userid, $courseid) {
+    protected static function delete_user_competencies_in_course($courseid, $userids = []) {
         global $DB;
-        $DB->delete_records(user_competency_course::TABLE, ['userid' => $userid, 'courseid' => $courseid]);
+
+        $params = ['courseid' => $courseid];
+        $where = "courseid = :courseid";
+
+        if (!empty($userids)) {
+            list($insql, $inparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
+            $params = $params + $inparams;
+
+            $where .= " AND userid {$insql}";
+        }
+
+        $DB->delete_records_select(user_competency_course::TABLE, $where, $params);
     }
 
     /**