MDL-63535 core_userkey: Add method that returns users in context
authorMihail Geshoski <mihail@moodle.com>
Mon, 22 Oct 2018 08:00:36 +0000 (16:00 +0800)
committerMihail Geshoski <mihail@moodle.com>
Tue, 23 Oct 2018 02:46:25 +0000 (10:46 +0800)
This issue is part of the MDL-62560 Epic.

lib/userkey/classes/privacy/provider.php

index 39dfa45..5b67148 100644 (file)
@@ -29,6 +29,7 @@ defined('MOODLE_INTERNAL') || die();
 use \core_privacy\local\metadata\collection;
 use \core_privacy\local\request\transform;
 use \core_privacy\local\request\writer;
+use core_privacy\local\request\userlist;
 
 /**
  * Privacy class for requesting user data.
@@ -62,6 +63,41 @@ class provider implements
         return $collection;
     }
 
+    /**
+     * Get the list of users within a specific context for this system.
+     *
+     * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
+     * @param context $context The context.
+     * @param string $script The unique target identifier.
+     * @param int $instance The instance ID.
+     */
+    public static function get_user_contexts_with_script(userlist $userlist, \context $context, string $script,
+                                                         int $instance = null) {
+        if (!$context instanceof \context_user) {
+            return;
+        }
+
+        $params = [
+            'userid' => $context->instanceid,
+            'script' => $script
+        ];
+
+        $whereinstance = '';
+
+        if (!empty($instance)) {
+            $params['instance'] = $instance;
+            $whereinstance = ' AND k.instance = :instance';
+        }
+
+        $sql = "SELECT k.userid
+                  FROM {user_private_key} k
+                 WHERE k.script = :script
+                       AND k.userid = :userid
+                       {$whereinstance}";
+
+        $userlist->add_from_sql('userid', $sql, $params);
+    }
+
     /**
      * Exports the data relating to user keys for the specified scripts and instance, within the specified
      * context/subcontext.