MDL-64777 analytics: Add method to load all model definitions
authorDavid Mudrák <david@moodle.com>
Wed, 27 Mar 2019 09:39:20 +0000 (10:39 +0100)
committerDavid Mudrák <david@moodle.com>
Mon, 15 Apr 2019 06:16:58 +0000 (08:16 +0200)
analytics/classes/manager.php
analytics/tests/manager_test.php

index 1e31268..c809253 100644 (file)
@@ -683,6 +683,43 @@ class manager {
         return $models;
     }
 
+    /**
+     * Return the list of all the models declared anywhere in this Moodle installation.
+     *
+     * Models defined by the core and core subsystems come first, followed by those provided by plugins.
+     *
+     * @return array indexed by the frankenstyle component
+     */
+    public static function load_default_models_for_all_components(): array {
+
+        $tmp = [];
+
+        foreach (\core_component::get_component_list() as $type => $components) {
+            foreach (array_keys($components) as $component) {
+                if ($loaded = static::load_default_models_for_component($component)) {
+                    $tmp[$type][$component] = $loaded;
+                }
+            }
+        }
+
+        $result = [];
+
+        if ($loaded = static::load_default_models_for_component('core')) {
+            $result['core'] = $loaded;
+        }
+
+        if (!empty($tmp['core'])) {
+            $result += $tmp['core'];
+            unset($tmp['core']);
+        }
+
+        foreach ($tmp as $components) {
+            $result += $components;
+        }
+
+        return $result;
+    }
+
     /**
      * Validate the declaration of prediction models according the syntax expected in the component's db folder.
      *
index 923ad40..004b7de 100644 (file)
@@ -172,6 +172,20 @@ class analytics_manager_testcase extends advanced_testcase {
         $this->assertSame([], \core_analytics\manager::load_default_models_for_component('foo_bar2776327736558'));
     }
 
+    /**
+     * Tests for the {@link \core_analytics\manager::load_default_models_for_all_components()} implementation.
+     */
+    public function test_load_default_models_for_all_components() {
+        $this->resetAfterTest();
+
+        $models = \core_analytics\manager::load_default_models_for_all_components();
+
+        $this->assertTrue(is_array($models['core']));
+        $this->assertNotEmpty($models['core']);
+        $this->assertNotEmpty($models['core'][0]['target']);
+        $this->assertNotEmpty($models['core'][0]['indicators']);
+    }
+
     /**
      * Tests for the successful execution of the {@link \core_analytics\manager::validate_models_declaration()}.
      */