Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  // This file is part of Moodle - http://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   * Create model form.
  19   *
  20   * @package    tool_analytics
  21   * @copyright  2019 David Monllao {@link http://www.davidmonllao.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  $returnurl = new \moodle_url('/admin/tool/analytics/index.php');
  38  $url = new \moodle_url('/admin/tool/analytics/createmodel.php');
  39  $title = get_string('createmodel', 'tool_analytics');
  40  
  41  \tool_analytics\output\helper::set_navbar($title, $url);
  42  
  43  // Static targets are not editable, we discard them.
  44  $targets = array_filter(\core_analytics\manager::get_all_targets(), function($target) {
  45      return (!$target->based_on_assumptions());
  46  });
  47  
  48  // Set 'supportscontexts' to true as at this stage we don't know if the contexts are supported by
  49  // the selected target.
  50  $customdata = array(
  51      'trainedmodel' => false,
  52      'staticmodel' => false,
  53      'targets' => $targets,
  54      'indicators' => \core_analytics\manager::get_all_indicators(),
  55      'timesplittings' => \core_analytics\manager::get_all_time_splittings(),
  56      'predictionprocessors' => \core_analytics\manager::get_all_prediction_processors(),
  57      'supportscontexts' => true,
  58  );
  59  $mform = new \tool_analytics\output\form\edit_model(null, $customdata);
  60  
  61  if ($mform->is_cancelled()) {
  62      redirect($returnurl);
  63  
  64  } else if ($data = $mform->get_data()) {
  65  
  66      // Converting option names to class names.
  67      $targetclass = \tool_analytics\output\helper::option_to_class($data->target);
  68      if (empty($targets[$targetclass])) {
  69          throw new \moodle_exception('errorinvalidtarget', 'analytics', '', $targetclass);
  70      }
  71      $target = $targets[$targetclass];
  72  
  73      $indicators = array();
  74      foreach ($data->indicators as $indicator) {
  75          $indicatorinstance = \core_analytics\manager::get_indicator(
  76              \tool_analytics\output\helper::option_to_class($indicator)
  77          );
  78          $indicators[$indicatorinstance->get_id()] = $indicatorinstance;
  79      }
  80      $timesplitting = \tool_analytics\output\helper::option_to_class($data->timesplitting);
  81      $predictionsprocessor = \tool_analytics\output\helper::option_to_class($data->predictionsprocessor);
  82  
  83      // Insert the model into db.
  84      $model = \core_analytics\model::create($target, []);
  85  
  86      // Filter out indicators that can not be used by this target.
  87      $invalidindicators = array_diff_key($indicators, $model->get_potential_indicators());
  88      if ($invalidindicators) {
  89          $indicators = array_diff_key($indicators, $invalidindicators);
  90      }
  91  
  92      // Update the model with the rest of the data provided in the form.
  93      $model->update($data->enabled, $indicators, $timesplitting, $predictionsprocessor, $data->contexts);
  94  
  95      $message = '';
  96      $messagetype = \core\output\notification::NOTIFY_SUCCESS;
  97      if (!empty($invalidindicators)) {
  98          $message = get_string('invalidindicatorsremoved', 'tool_analytics');
  99      }
 100      redirect($returnurl, $message, 0, $messagetype);
 101  }
 102  
 103  echo $OUTPUT->header();
 104  $mform->display();
 105  echo $OUTPUT->footer();