MDL-64320 analytics: Validate discrete indicators separately
[moodle.git] / analytics / classes / local / indicator / binary.php
CommitLineData
369389c9
DM
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
25namespace core_analytics\local\indicator;
26
27defined('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 */
36abstract class binary extends discrete {
37
413f19bc
DM
38 /**
39 * get_classes
40 *
41 * @return array
42 */
369389c9 43 public static final function get_classes() {
e0c88997 44 return [-1, 1];
369389c9
DM
45 }
46
5c5cb3ee
DM
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
27ae9af4
DM
64 /**
65 * get_display_value
66 *
67 * @param float $value
68 * @param string $subtype
69 * @return string
70 */
369389c9
DM
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
27ae9af4
DM
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) {
369389c9
DM
91
92 // No subtypes for binary values by default.
93 if ($value == -1) {
27ae9af4 94 return self::OUTCOME_NEGATIVE;
369389c9 95 } else if ($value == 1) {
27ae9af4 96 return self::OUTCOME_OK;
369389c9
DM
97 } else {
98 throw new \moodle_exception('errorpredictionformat', 'analytics');
99 }
100 }
101
413f19bc
DM
102 /**
103 * get_feature_headers
104 *
105 * @return array
106 */
369389c9
DM
107 public static function get_feature_headers() {
108 // Just 1 single feature obtained from the calculated value.
3a396286 109 return array('\\' . get_called_class());
369389c9
DM
110 }
111
413f19bc
DM
112 /**
113 * to_features
114 *
115 * @param array $calculatedvalues
116 * @return array
117 */
369389c9
DM
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}