Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Check and create missing default prediction models.
  19   *
  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
  23   */
  24  
  25  require_once(__DIR__ . '/../../../config.php');
  26  
  27  require_login();
  28  \core_analytics\manager::check_can_manage_models();
  29  
  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);
  35  }
  36  
  37  $confirmed = optional_param('confirmed', false, PARAM_BOOL);
  38  $restoreids = optional_param_array('restoreid', [], PARAM_ALPHANUM);
  39  
  40  $returnurl = new \moodle_url('/admin/tool/analytics/index.php');
  41  $myurl = new \moodle_url('/admin/tool/analytics/restoredefault.php');
  42  
  43  \tool_analytics\output\helper::set_navbar(get_string('restoredefault', 'tool_analytics'), $myurl);
  44  
  45  if (data_submitted()) {
  46      require_sesskey();
  47  
  48      if (empty($restoreids)) {
  49          $message = get_string('restoredefaultempty', 'tool_analytics');
  50          $type = \core\output\notification::NOTIFY_WARNING;
  51          redirect($myurl, $message, null, $type);
  52      }
  53  
  54      $numcreated = 0;
  55  
  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;
  61              }
  62  
  63              list($target, $indicators) = \core_analytics\manager::get_declared_target_and_indicators_instances($definition);
  64  
  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;
  68              }
  69  
  70              \core_analytics\manager::create_declared_model($definition);
  71              $numcreated++;
  72          }
  73      }
  74  
  75      $message = get_string('restoredefaultsome', 'tool_analytics', ['count' => $numcreated]);
  76      $type = \core\output\notification::NOTIFY_SUCCESS;
  77  
  78      redirect($returnurl, $message, null, $type);
  79  }
  80  
  81  $models = \core_analytics\manager::load_default_models_for_all_components();
  82  $ui = new \tool_analytics\output\restorable_models($models);
  83  
  84  echo $OUTPUT->header();
  85  echo $PAGE->get_renderer('tool_analytics')->render($ui);
  86  echo $OUTPUT->footer();