MDL-63926 block_recentlyaccesseditems: Improved get_contexts_for_userid
authorMichael Hawkins <michaelh@moodle.com>
Mon, 12 Nov 2018 04:24:52 +0000 (12:24 +0800)
committerMichael Hawkins <michaelh@moodle.com>
Wed, 14 Nov 2018 03:13:00 +0000 (11:13 +0800)
Updated the provider::get_contexts_for_userid method, to prevent false
positives. Previously, the user context would always be returned, now
the method checks data exists for the block first.

blocks/recentlyaccesseditems/classes/privacy/provider.php

index f56589b..f4b460a 100644 (file)
@@ -69,9 +69,12 @@ class provider implements \core_privacy\local\metadata\provider, \core_privacy\l
      */
     public static function get_contexts_for_userid(int $userid) : contextlist {
         $params = ['userid' => $userid, 'contextuser' => CONTEXT_USER];
-        $sql = "SELECT id
-                  FROM {context}
-                 WHERE instanceid = :userid and contextlevel = :contextuser";
+        $sql = "SELECT c.id
+                  FROM {context} c
+                  JOIN {block_recentlyaccesseditems} b
+                    ON b.userid = c.instanceid
+                 WHERE c.instanceid = :userid
+                   AND c.contextlevel = :contextuser";
         $contextlist = new contextlist();
         $contextlist->add_from_sql($sql, $params);
         return $contextlist;