Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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   * Test target.
  19   *
  20   * @package   core_analytics
  21   * @copyright 2018 David MonllaĆ³ {@link http://www.davidmonllao.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once (__DIR__ . '/test_site_users_analyser.php');
  28  
  29  /**
  30   * Test target.
  31   *
  32   * @package   core_analytics
  33   * @copyright 2018 David MonllaĆ³ {@link http://www.davidmonllao.com}
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class test_target_site_users extends \core_analytics\local\target\binary {
  37  
  38      /**
  39       * Returns a lang_string object representing the name for the indicator.
  40       *
  41       * Used as column identificator.
  42       *
  43       * If there is a corresponding '_help' string this will be shown as well.
  44       *
  45       * @return \lang_string
  46       */
  47      public static function get_name() : \lang_string {
  48          // Using a string that exists and contains a corresponding '_help' string.
  49          return new \lang_string('adminhelplogs');
  50      }
  51  
  52      /**
  53       * predictions
  54       *
  55       * @var array
  56       */
  57      protected $predictions = array();
  58  
  59      /**
  60       * get_analyser_class
  61       *
  62       * @return string
  63       */
  64      public function get_analyser_class() {
  65          return 'test_site_users_analyser';
  66      }
  67  
  68      /**
  69       * Everything yep, this is just for testing.
  70       *
  71       * @param  \core_analytics\local\time_splitting\base $timesplitting
  72       * @return bool
  73       */
  74      public function can_use_timesplitting(\core_analytics\local\time_splitting\base $timesplitting): bool {
  75          return true;
  76      }
  77  
  78      /**
  79       * classes_description
  80       *
  81       * @return string[]
  82       */
  83      public static function classes_description() {
  84          return array(
  85              'firstname first char is A',
  86              'firstname first char is not A'
  87          );
  88      }
  89  
  90      /**
  91       * We don't want to discard results.
  92       * @return float
  93       */
  94      protected function min_prediction_score() {
  95          return null;
  96      }
  97  
  98      /**
  99       * We don't want to discard results.
 100       * @return array
 101       */
 102      public function ignored_predicted_classes() {
 103          return array();
 104      }
 105  
 106      /**
 107       * is_valid_analysable
 108       *
 109       * @param \core_analytics\analysable $analysable
 110       * @param bool $fortraining
 111       * @return bool
 112       */
 113      public function is_valid_analysable(\core_analytics\analysable $analysable, $fortraining = true) {
 114          // This is testing, let's make things easy.
 115          return true;
 116      }
 117  
 118      /**
 119       * is_valid_sample
 120       *
 121       * @param int $sampleid
 122       * @param \core_analytics\analysable $analysable
 123       * @param bool $fortraining
 124       * @return bool
 125       */
 126      public function is_valid_sample($sampleid, \core_analytics\analysable $analysable, $fortraining = true) {
 127          // We skip not-visible courses during training as a way to emulate the training data / prediction data difference.
 128          // In normal circumstances is_valid_sample will return false when they receive a sample that can not be
 129          // processed.
 130          if (!$fortraining) {
 131              return true;
 132          }
 133  
 134          $sample = $this->retrieve('user', $sampleid);
 135          if ($sample->lastname == 'b') {
 136              return false;
 137          }
 138          return true;
 139      }
 140  
 141      /**
 142       * calculate_sample
 143       *
 144       * @param int $sampleid
 145       * @param \core_analytics\analysable $analysable
 146       * @param int $starttime
 147       * @param int $endtime
 148       * @return float
 149       */
 150      protected function calculate_sample($sampleid, \core_analytics\analysable $analysable, $starttime = false, $endtime = false) {
 151  
 152          $sample = $this->retrieve('user', $sampleid);
 153  
 154          $firstchar = substr($sample->firstname, 0, 1);
 155          if ($firstchar === 'a') {
 156              return 1;
 157          } else {
 158              return 0;
 159          }
 160      }
 161  }