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 - 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   * Abstract binary indicator.
  19   *
  20   * @package   core_analytics
  21   * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_analytics\local\indicator;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Abstract binary indicator.
  31   *
  32   * @package   core_analytics
  33   * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  abstract class binary extends discrete {
  37  
  38      /**
  39       * get_classes
  40       *
  41       * @return array
  42       */
  43      public static final function get_classes() {
  44          return [-1, 1];
  45      }
  46  
  47      /**
  48       * It should always be displayed.
  49       *
  50       * Binary values have no subtypes by default, please overwrite if
  51       * your indicator is adding extra features.
  52       *
  53       * @param float $value
  54       * @param string $subtype
  55       * @return bool
  56       */
  57      public function should_be_displayed($value, $subtype) {
  58          if ($subtype != false) {
  59              return false;
  60          }
  61          return true;
  62      }
  63  
  64      /**
  65       * get_display_value
  66       *
  67       * @param float $value
  68       * @param string $subtype
  69       * @return string
  70       */
  71      public function get_display_value($value, $subtype = false) {
  72  
  73          // No subtypes for binary values by default.
  74          if ($value == -1) {
  75              return get_string('no');
  76          } else if ($value == 1) {
  77              return get_string('yes');
  78          } else {
  79              throw new \moodle_exception('errorpredictionformat', 'analytics');
  80          }
  81      }
  82  
  83      /**
  84       * get_calculation_outcome
  85       *
  86       * @param float $value
  87       * @param string $subtype
  88       * @return int
  89       */
  90      public function get_calculation_outcome($value, $subtype = false) {
  91  
  92          // No subtypes for binary values by default.
  93          if ($value == -1) {
  94              return self::OUTCOME_NEGATIVE;
  95          } else if ($value == 1) {
  96              return self::OUTCOME_OK;
  97          } else {
  98              throw new \moodle_exception('errorpredictionformat', 'analytics');
  99          }
 100      }
 101  
 102      /**
 103       * get_feature_headers
 104       *
 105       * @return array
 106       */
 107      public static function get_feature_headers() {
 108          // Just 1 single feature obtained from the calculated value.
 109          return array('\\' . get_called_class());
 110      }
 111  
 112      /**
 113       * to_features
 114       *
 115       * @param array $calculatedvalues
 116       * @return array
 117       */
 118      protected function to_features($calculatedvalues) {
 119          // Indicators with binary values have only 1 feature for indicator, here we do nothing else
 120          // than converting each sample scalar value to an array of scalars with 1 element.
 121          array_walk($calculatedvalues, function(&$calculatedvalue) {
 122              // Just return it as an array.
 123              $calculatedvalue = array($calculatedvalue);
 124          });
 125  
 126          return $calculatedvalues;
 127      }
 128  }