MDL-63495 core_rating: Add helper to fetch users in context
authorAndrew Nicols <andrew@nicols.co.uk>
Mon, 3 Sep 2018 00:57:45 +0000 (08:57 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 17 Oct 2018 05:12:18 +0000 (13:12 +0800)
This issue is a part of the MDL-62560 Epic.

rating/classes/privacy/provider.php

index 4237ea2..6ce9905 100644 (file)
@@ -25,6 +25,7 @@
 namespace core_rating\privacy;
 
 use \core_privacy\local\metadata\collection;
+use \core_privacy\local\request\userlist;
 
 defined('MOODLE_INTERNAL') || die();
 
@@ -214,4 +215,29 @@ class provider implements
             'contextid = :contextid AND component = :component AND ratingarea = :ratingarea AND itemid ' . $itemidstest,
             $params);
     }
+
+    /**
+     * Add the list of users who have rated in the specified constraints.
+     *
+     * @param   userlist    $userlist The userlist to add the users to.
+     * @param   string      $alias An alias prefix to use for rating selects to avoid interference with your own sql.
+     * @param   string      $component The component to check.
+     * @param   string      $area The rating area to check.
+     * @param   string      $insql The SQL to use in a sub-select for the itemid query.
+     * @param   array       $params The params required for the insql.
+     */
+    public static function get_users_in_context_from_sql(
+            userlist $userlist, string $alias, string $component, string $area, string $insql, $params) {
+        // Discussion authors.
+        $sql = "SELECT {$alias}.userid
+                  FROM {rating} {$alias}
+                 WHERE {$alias}.component = :{$alias}component
+                   AND {$alias}.ratingarea = :{$alias}ratingarea
+                   AND {$alias}.itemid IN ({$insql})";
+
+        $params["{$alias}component"] = $component;
+        $params["{$alias}ratingarea"] = $area;
+
+        $userlist->add_from_sql('userid', $sql, $params);
+    }
 }