Automatically generated installer lang files
[moodle.git] / admin / tool / analytics / restoredefault.php
blobce0f8ae4e505598cf4528e48aad90757c7108d0b
1 <?php
2 // This file is part of Moodle - https://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Check and create missing default prediction models.
20 * @package tool_analytics
21 * @copyright 2019 David Mudrák <david@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once(__DIR__ . '/../../../config.php');
27 require_login();
28 \core_analytics\manager::check_can_manage_models();
30 if (!\core_analytics\manager::is_analytics_enabled()) {
31 $PAGE->set_context(\context_system::instance());
32 $renderer = $PAGE->get_renderer('tool_analytics');
33 echo $renderer->render_analytics_disabled();
34 exit(0);
37 $confirmed = optional_param('confirmed', false, PARAM_BOOL);
38 $restoreids = optional_param_array('restoreid', [], PARAM_ALPHANUM);
40 $returnurl = new \moodle_url('/admin/tool/analytics/index.php');
41 $myurl = new \moodle_url('/admin/tool/analytics/restoredefault.php');
43 \tool_analytics\output\helper::set_navbar(get_string('restoredefault', 'tool_analytics'), $myurl);
45 if (data_submitted()) {
46 require_sesskey();
48 if (empty($restoreids)) {
49 $message = get_string('restoredefaultempty', 'tool_analytics');
50 $type = \core\output\notification::NOTIFY_WARNING;
51 redirect($myurl, $message, null, $type);
54 $numcreated = 0;
56 foreach (\core_analytics\manager::load_default_models_for_all_components() as $componentname => $modelslist) {
57 foreach ($modelslist as $definition) {
58 if (!in_array(\core_analytics\manager::model_declaration_identifier($definition), $restoreids)) {
59 // This model has not been selected by the user.
60 continue;
63 list($target, $indicators) = \core_analytics\manager::get_declared_target_and_indicators_instances($definition);
65 if (\core_analytics\model::exists($target, $indicators)) {
66 // This model exists (normally this should not happen as we do not show such models in the UI to select).
67 continue;
70 \core_analytics\manager::create_declared_model($definition);
71 $numcreated++;
75 $message = get_string('restoredefaultsome', 'tool_analytics', ['count' => $numcreated]);
76 $type = \core\output\notification::NOTIFY_SUCCESS;
78 redirect($returnurl, $message, null, $type);
81 $models = \core_analytics\manager::load_default_models_for_all_components();
82 $ui = new \tool_analytics\output\restorable_models($models);
84 echo $OUTPUT->header();
85 echo $PAGE->get_renderer('tool_analytics')->render($ui);
86 echo $OUTPUT->footer();