MDL-61637 tool_dataprivacy: Context-based fetching
authorDavid Monllao <davidm@moodle.com>
Thu, 10 May 2018 13:54:56 +0000 (15:54 +0200)
committerDavid Monllao <davidm@moodle.com>
Thu, 10 May 2018 13:56:50 +0000 (15:56 +0200)
Blocks API do not provide methods to retrieve a list of
blocks without filtering by UI-availability. We need to
get course blocks based on their course context.

admin/tool/dataprivacy/classes/output/data_registry_page.php

index b16bfac..5263f8e 100644 (file)
@@ -312,29 +312,29 @@ class data_registry_page implements renderable, templatable {
 
         $branches = [];
 
-        $blockinstances = \core_block_external::get_course_blocks($coursecontext->instanceid);
-        if (empty($blockinstances['blocks'])) {
-            return $branches;
-        }
+        $children = $coursecontext->get_child_contexts();
+        foreach ($children as $childcontext) {
+
+            if ($childcontext->contextlevel !== CONTEXT_BLOCK) {
+                continue;
+            }
 
-        foreach ($blockinstances['blocks'] as $bi) {
             if (function_exists('block_instance_by_id')) {
-                $blockinstance = block_instance_by_id($bi['instanceid']);
+                $blockinstance = block_instance_by_id($childcontext->instanceid);
             } else {
                 // TODO To be removed when MDL-61621 gets integrated.
-                $blockinstance = $DB->get_record('block_instances', ['id' => $bi['instanceid']]);
+                $blockinstance = $DB->get_record('block_instances', ['id' => $childcontext->instanceid]);
                 $blockinstance = block_instance($blockinstance->blockname, $blockinstance);
             }
-            $blockcontext = \context_block::instance($bi['instanceid']);
-            $displayname = shorten_text(format_string($blockinstance->get_title(), true, ['context' => $blockcontext->id]));
+            $displayname = shorten_text(format_string($blockinstance->get_title(), true, ['context' => $childcontext->id]));
             $branches[] = self::complete([
                 'text' => $displayname,
-                'contextid' => $blockcontext->id,
+                'contextid' => $childcontext->id,
             ]);
+
         }
 
         return $branches;
-
     }
 
     /**