MDL-64777 analytics: Add method to load all model definitions

This commit is contained in:
David Mudrák 2019-03-27 10:39:20 +01:00
parent 01d620ee67
commit 1297fa4156
2 changed files with 51 additions and 0 deletions

View 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.
*

View 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()}.
*/