Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is 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   * Multi-class classifier target.
  19   *
  20   * @package   core_analytics
  21   * @copyright 2019 Apetrei Vlad
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Multi-class classifier target.
  29   *
  30   * @package   core_analytics
  31   * @copyright 2019 Apetrei Vlad
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class test_target_shortname_multiclass extends \core_analytics\local\target\discrete {
  35  
  36      /**
  37       * Returns a lang_string object representing the name for the indicator.
  38       *
  39       * Used as column identificator.
  40       *
  41       * If there is a corresponding '_help' string this will be shown as well.
  42       *
  43       * @return \lang_string
  44       */
  45      public static function get_name() : \lang_string {
  46          // Using a string that exists and contains a corresponding '_help' string.
  47          return new \lang_string('allowstealthmodules');
  48      }
  49  
  50      /**
  51       * predictions
  52       *
  53       * @var array
  54       */
  55      protected $predictions = array();
  56  
  57      /**
  58       * is_linear
  59       *
  60       * @return bool
  61       */
  62      public function is_linear() {
  63          return false;
  64      }
  65  
  66      /**
  67       * Returns the target discrete values.
  68       *
  69       * Only useful for targets using discrete values, must be overwriten if it is the case.
  70       *
  71       * @return array
  72       */
  73      public static final function get_classes() {
  74          return array(0, 1, 2);
  75      }
  76  
  77      /**
  78       * Is the calculated value a positive outcome of this target?
  79       *
  80       * @param string $value
  81       * @param string $ignoredsubtype
  82       * @return int
  83       */
  84      public function get_calculation_outcome($value, $ignoredsubtype = false) {
  85  
  86          if (!self::is_a_class($value)) {
  87              throw new \moodle_exception('errorpredictionformat', 'analytics');
  88          }
  89  
  90          if (in_array($value, $this->ignored_predicted_classes(), false)) {
  91              // Just in case, if it is ignored the prediction should not even be recorded but if it would, it is ignored now,
  92              // which should mean that is it nothing serious.
  93              return self::OUTCOME_VERY_POSITIVE;
  94          }
  95  
  96          // By default binaries are danger when prediction = 1.
  97          if ($value) {
  98              return self::OUTCOME_VERY_NEGATIVE;
  99          }
 100          return self::OUTCOME_VERY_POSITIVE;
 101      }
 102  
 103      /**
 104       * get_analyser_class
 105       *
 106       * @return string
 107       */
 108      public function get_analyser_class() {
 109          return '\core\analytics\analyser\site_courses';
 110      }
 111  
 112      /**
 113       * We don't want to discard results.
 114       * @return float
 115       */
 116      protected function min_prediction_score() {
 117          return null;
 118      }
 119  
 120      /**
 121       * We don't want to discard results.
 122       * @return array
 123       */
 124      public function ignored_predicted_classes() {
 125          return array();
 126      }
 127  
 128      /**
 129       * is_valid_analysable
 130       *
 131       * @param \core_analytics\analysable $analysable
 132       * @param bool $fortraining
 133       * @return bool
 134       */
 135      public function is_valid_analysable(\core_analytics\analysable $analysable, $fortraining = true) {
 136          // This is testing, let's make things easy.
 137          return true;
 138      }
 139  
 140      /**
 141       * is_valid_sample
 142       *
 143       * @param int $sampleid
 144       * @param \core_analytics\analysable $analysable
 145       * @param bool $fortraining
 146       * @return bool
 147       */
 148      public function is_valid_sample($sampleid, \core_analytics\analysable $analysable, $fortraining = true) {
 149          // We skip not-visible courses during training as a way to emulate the training data / prediction data difference.
 150          // In normal circumstances is_valid_sample will return false when they receive a sample that can not be
 151          // processed.
 152          if (!$fortraining) {
 153              return true;
 154          }
 155  
 156          $sample = $this->retrieve('course', $sampleid);
 157          if ($sample->visible == 0) {
 158              return false;
 159          }
 160          return true;
 161      }
 162  
 163      /**
 164       * classes_description
 165       *
 166       * @return string[]
 167       */
 168      protected static function classes_description() {
 169          return array(
 170              get_string('first class'),
 171              get_string('second class'),
 172              get_string('third class')
 173          );
 174      }
 175  
 176      /**
 177       * calculate_sample
 178       *
 179       * @param int $sampleid
 180       * @param \core_analytics\analysable $analysable
 181       * @param int $starttime
 182       * @param int $endtime
 183       * @return float
 184       */
 185      protected function calculate_sample($sampleid, \core_analytics\analysable $analysable, $starttime = false, $endtime = false) {
 186  
 187          $sample = $this->retrieve('course', $sampleid);
 188  
 189          $firstchar = substr($sample->shortname, 0, 1);
 190          switch ($firstchar) {
 191              case 'a':
 192                  return 0;
 193              case 'b':
 194                  return 1;
 195              case 'c':
 196                  return 2;
 197          }
 198      }
 199  
 200      /**
 201       * Can the provided time-splitting method be used on this target?.
 202       *
 203       * Time-splitting methods not matching the target requirements will not be selectable by models based on this target.
 204       *
 205       * @param \core_analytics\local\time_splitting\base $timesplitting
 206       * @return bool
 207       */
 208      public function can_use_timesplitting(\core_analytics\local\time_splitting\base $timesplitting):bool {
 209          return true;
 210      }
 211  }