1 |
efrain |
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 linear 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 linear 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 linear extends base {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Set to false to avoid context features to be added as dataset features.
|
|
|
40 |
*
|
|
|
41 |
* @return bool
|
|
|
42 |
*/
|
|
|
43 |
protected static function include_averages() {
|
|
|
44 |
return true;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* get_feature_headers
|
|
|
49 |
*
|
|
|
50 |
* @return array
|
|
|
51 |
*/
|
|
|
52 |
public static function get_feature_headers() {
|
|
|
53 |
|
|
|
54 |
$fullclassname = '\\' . get_called_class();
|
|
|
55 |
|
|
|
56 |
if (static::include_averages()) {
|
|
|
57 |
// The calculated value + context indicators.
|
|
|
58 |
$headers = array($fullclassname, $fullclassname . '/mean');
|
|
|
59 |
} else {
|
|
|
60 |
$headers = array($fullclassname);
|
|
|
61 |
}
|
|
|
62 |
return $headers;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Show only the main feature.
|
|
|
67 |
*
|
|
|
68 |
* @param float $value
|
|
|
69 |
* @param string $subtype
|
|
|
70 |
* @return bool
|
|
|
71 |
*/
|
|
|
72 |
public function should_be_displayed($value, $subtype) {
|
|
|
73 |
if ($subtype != false) {
|
|
|
74 |
return false;
|
|
|
75 |
}
|
|
|
76 |
return true;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* get_display_value
|
|
|
81 |
*
|
|
|
82 |
* @param float $value
|
|
|
83 |
* @param string $subtype
|
|
|
84 |
* @return string
|
|
|
85 |
*/
|
|
|
86 |
public function get_display_value($value, $subtype = false) {
|
|
|
87 |
$diff = static::get_max_value() - static::get_min_value();
|
|
|
88 |
return round(100 * ($value - static::get_min_value()) / $diff) . '%';
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* get_calculation_outcome
|
|
|
93 |
*
|
|
|
94 |
* @param float $value
|
|
|
95 |
* @param string $subtype
|
|
|
96 |
* @return int
|
|
|
97 |
*/
|
|
|
98 |
public function get_calculation_outcome($value, $subtype = false) {
|
|
|
99 |
if ($value < 0) {
|
|
|
100 |
return self::OUTCOME_NEGATIVE;
|
|
|
101 |
} else {
|
|
|
102 |
return self::OUTCOME_OK;
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Converts the calculated values to a list of features for the dataset.
|
|
|
108 |
*
|
|
|
109 |
* @param array $calculatedvalues
|
|
|
110 |
* @return array
|
|
|
111 |
*/
|
|
|
112 |
protected function to_features($calculatedvalues) {
|
|
|
113 |
|
|
|
114 |
// Null mean if all calculated values are null.
|
|
|
115 |
$nullmean = true;
|
|
|
116 |
foreach ($calculatedvalues as $value) {
|
|
|
117 |
if (!is_null($value)) {
|
|
|
118 |
// Early break, we don't want to spend a lot of time here.
|
|
|
119 |
$nullmean = false;
|
|
|
120 |
break;
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
if ($nullmean) {
|
|
|
125 |
$mean = null;
|
|
|
126 |
} else {
|
|
|
127 |
$mean = round(array_sum($calculatedvalues) / count($calculatedvalues), 2);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
foreach ($calculatedvalues as $sampleid => $calculatedvalue) {
|
|
|
131 |
|
|
|
132 |
if (!is_null($calculatedvalue)) {
|
|
|
133 |
$calculatedvalue = round($calculatedvalue, 2);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
if (static::include_averages()) {
|
|
|
137 |
$calculatedvalues[$sampleid] = array($calculatedvalue, $mean);
|
|
|
138 |
} else {
|
|
|
139 |
// Basically just convert the scalar to an array of scalars with a single value.
|
|
|
140 |
$calculatedvalues[$sampleid] = array($calculatedvalue);
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
// Returns each sample as an array of values, appending the mean to the calculated value.
|
|
|
145 |
return $calculatedvalues;
|
|
|
146 |
}
|
|
|
147 |
}
|