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.

Differences Between: [Versions 39 and 402]

   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   * Evaluates the provided model.
  19   *
  20   * @package    tool_analytics
  21   * @copyright  2016 David Monllao {@link http://www.davidmonllao.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('CLI_SCRIPT', true);
  26  
  27  require_once(__DIR__ . '/../../../../config.php');
  28  require_once($CFG->libdir.'/clilib.php');
  29  
  30  $help = "Evaluates the provided model.
  31  
  32  Options:
  33  --modelid              Model id
  34  --list                 List models
  35  --non-interactive      Not interactive questions
  36  --analysisinterval     Restrict the evaluation to 1 single analysis interval (Optional)
  37  --mode                 'configuration' or 'trainedmodel'. You can only use mode=trainedmodel when the trained" .
  38      " model was imported" . "
  39  --reuse-prev-analysed  Reuse recently analysed courses instead of analysing the whole site. Set it to false while" .
  40      " coding indicators. Defaults to true (Optional)" . "
  41  -h, --help             Print out this help
  42  
  43  Example:
  44  \$ php admin/tool/analytics/cli/evaluate_model.php --modelid=1 --analysisinterval='\\core\\analytics\\time_splitting\\quarters'
  45  ";
  46  
  47  // Now get cli options.
  48  list($options, $unrecognized) = cli_get_params(
  49      array(
  50          'help'                  => false,
  51          'modelid'               => false,
  52          'list'                  => false,
  53          'analysisinterval'      => false,
  54          'mode'                  => 'configuration',
  55          'reuse-prev-analysed'   => true,
  56          'non-interactive'       => false,
  57      ),
  58      array(
  59          'h' => 'help',
  60      )
  61  );
  62  
  63  if ($options['help']) {
  64      echo $help;
  65      exit(0);
  66  }
  67  
  68  if (!\core_analytics\manager::is_analytics_enabled()) {
  69      echo get_string('analyticsdisabled', 'analytics') . PHP_EOL;
  70      exit(0);
  71  }
  72  
  73  if ($options['list']) {
  74      \tool_analytics\clihelper::list_models();
  75      exit(0);
  76  }
  77  
  78  if ($options['modelid'] === false) {
  79      // All actions but --list require a modelid.
  80      echo $help;
  81      exit(0);
  82  }
  83  
  84  if ($options['mode'] !== 'configuration' && $options['mode'] !== 'trainedmodel') {
  85      cli_error('Error: The provided mode is not supported');
  86  }
  87  
  88  if ($options['mode'] == 'trainedmodel' && $options['analysisinterval']) {
  89      cli_error('Sorry, no analysis interval can be specified when using \'trainedmodel\' mode.');
  90  }
  91  
  92  // We need admin permissions.
  93  \core\session\manager::set_user(get_admin());
  94  
  95  $model = new \core_analytics\model($options['modelid']);
  96  
  97  mtrace(get_string('analysingsitedata', 'tool_analytics'));
  98  
  99  if ($options['reuse-prev-analysed']) {
 100      mtrace(get_string('evaluationinbatches', 'tool_analytics'));
 101  }
 102  
 103  $renderer = $PAGE->get_renderer('tool_analytics');
 104  
 105  $analyseroptions = array(
 106      'timesplitting' => $options['analysisinterval'],
 107      'reuseprevanalysed' => $options['reuse-prev-analysed'],
 108      'mode' => $options['mode'],
 109  );
 110  // Evaluate its suitability to predict accurately.
 111  $results = $model->evaluate($analyseroptions);
 112  
 113  // Reset the page as some indicators may call external functions that overwrite the page context.
 114  \tool_analytics\output\helper::reset_page();
 115  
 116  echo $renderer->render_evaluate_results($results, $model->get_analyser()->get_logs());
 117  
 118  // Check that we have, at leasa,t 1 valid dataset (not necessarily good) to use.
 119  foreach ($results as $result) {
 120      if ($result->status !== \core_analytics\model::NO_DATASET &&
 121              $result->status !== \core_analytics\model::GENERAL_ERROR) {
 122          $validdatasets = true;
 123      }
 124  }
 125  
 126  if (!empty($validdatasets) && !$model->is_enabled() && $options['non-interactive'] === false) {
 127  
 128      // Select a dataset, train and enable the model.
 129      $input = cli_input(get_string('clienablemodel', 'tool_analytics'));
 130      while (!\core_analytics\manager::is_valid($input, '\core_analytics\local\time_splitting\base') && $input !== 'none') {
 131          mtrace(get_string('errorunexistingtimesplitting', 'analytics'));
 132          $input = cli_input(get_string('clienablemodel', 'tool_analytics'));
 133      }
 134  
 135      if ($input === 'none') {
 136          exit(0);
 137      }
 138  
 139      // Refresh the instance to prevent unexpected issues.
 140      $model = new \core_analytics\model($options['modelid']);
 141  
 142      // Set the time splitting method file and enable it.
 143      $model->enable($input);
 144  
 145      mtrace(get_string('trainandpredictmodel', 'tool_analytics'));
 146  
 147      // Train the model with the selected time splitting method and start predicting.
 148      $model->train();
 149      $model->predict();
 150  }
 151  
 152  exit(0);