MDL-58859 analytics: Analytics API added to core
[moodle.git] / analytics / classes / manager.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 * Inspire tool manager.
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;
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Inspire tool manager.
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 */
36class manager {
37
38 /**
39 * @var \core_analytics\predictor[]
40 */
41 protected static $predictionprocessors = null;
42
43 /**
44 * @var \core_analytics\local\indicator\base[]
45 */
46 protected static $allindicators = null;
47
48 /**
49 * @var \core_analytics\local\time_splitting\base[]
50 */
51 protected static $alltimesplittings = null;
52
53 public static function get_all_models($enabled = false, $trained = false, $predictioncontext = false) {
54 global $DB;
55
56 $filters = array();
57 if ($enabled) {
58 $filters['enabled'] = 1;
59 }
60 if ($trained) {
61 $filters['trained'] = 1;
62 }
63 $modelobjs = $DB->get_records('analytics_models', $filters);
64
65 $models = array();
66 foreach ($modelobjs as $modelobj) {
67 $model = new \core_analytics\model($modelobj);
68 if (!$predictioncontext || $model->predictions_exist($predictioncontext)) {
69 $models[$modelobj->id] = $model;
70 }
71 }
72 return $models;
73 }
74
75 /**
76 * Returns the site selected predictions processor.
77 *
78 * @param string $predictionclass
79 * @param bool $checkisready
80 * @return \core_analytics\predictor
81 */
82 public static function get_predictions_processor($predictionclass = false, $checkisready = true) {
83
84 // We want 0 or 1 so we can use it as an array key for caching.
85 $checkisready = intval($checkisready);
86
87 if ($predictionclass === false) {
88 $predictionclass = get_config('analytics', 'predictionsprocessor');
89 }
90
91 if (empty($predictionclass)) {
92 // Use the default one if nothing set.
93 $predictionclass = '\mlbackend_php\processor';
94 }
95
96 if (!class_exists($predictionclass)) {
97 throw new \coding_exception('Invalid predictions processor ' . $predictionclass . '.');
98 }
99
100 $interfaces = class_implements($predictionclass);
101 if (empty($interfaces['core_analytics\predictor'])) {
102 throw new \coding_exception($predictionclass . ' should implement \core_analytics\predictor.');
103 }
104
105 // Return it from the cached list.
106 if (!isset(self::$predictionprocessors[$checkisready][$predictionclass])) {
107
108 $instance = new $predictionclass();
109 if ($checkisready) {
110 $isready = $instance->is_ready();
111 if ($isready !== true) {
112 throw new \moodle_exception('errorprocessornotready', 'analytics', '', $isready);
113 }
114 }
115 self::$predictionprocessors[$checkisready][$predictionclass] = $instance;
116 }
117
118 return self::$predictionprocessors[$checkisready][$predictionclass];
119 }
120
121 public static function get_all_prediction_processors() {
122
123 $mlbackends = \core_component::get_plugin_list('mlbackend');
124
125 $predictionprocessors = array();
126 foreach ($mlbackends as $mlbackend => $unused) {
127 $classfullpath = '\\mlbackend_' . $mlbackend . '\\processor';
128 $predictionprocessors[$classfullpath] = self::get_predictions_processor($classfullpath, false);
129 }
130 return $predictionprocessors;
131 }
132
133 /**
134 * Get all available time splitting methods.
135 *
136 * @return \core_analytics\time_splitting\base[]
137 */
138 public static function get_all_time_splittings() {
139 if (self::$alltimesplittings !== null) {
140 return self::$alltimesplittings;
141 }
142
143 $classes = self::get_analytics_classes('time_splitting');
144
145 self::$alltimesplittings = [];
146 foreach ($classes as $fullclassname => $classpath) {
147 $instance = self::get_time_splitting($fullclassname);
148 // We need to check that it is a valid time splitting method, it may be an abstract class.
149 if ($instance) {
150 self::$alltimesplittings[$instance->get_id()] = $instance;
151 }
152 }
153
154 return self::$alltimesplittings;
155 }
156
157 /**
158 * Returns the enabled time splitting methods.
159 *
160 * @return \core_analytics\local\time_splitting\base[]
161 */
162 public static function get_enabled_time_splitting_methods() {
163
164 if ($enabledtimesplittings = get_config('analytics', 'timesplittings')) {
165 $enabledtimesplittings = array_flip(explode(',', $enabledtimesplittings));
166 }
167
168 $timesplittings = self::get_all_time_splittings();
169 foreach ($timesplittings as $key => $timesplitting) {
170
171 // We remove the ones that are not enabled. This also respects the default value (all methods enabled).
172 if (!empty($enabledtimesplittings) && !isset($enabledtimesplittings[$key])) {
173 unset($timesplittings[$key]);
174 }
175 }
176 return $timesplittings;
177 }
178
179 /**
180 * Returns a time splitting method by its classname.
181 *
182 * @param string $fullclassname
183 * @return \core_analytics\local\time_splitting\base|false False if it is not valid.
184 */
185 public static function get_time_splitting($fullclassname) {
186 if (!self::is_valid($fullclassname, '\core_analytics\local\time_splitting\base')) {
187 return false;
188 }
189 return new $fullclassname();
190 }
191
192 /**
193 * Return all system indicators.
194 *
195 * @return \core_analytics\local\indicator\base[]
196 */
197 public static function get_all_indicators() {
198 if (self::$allindicators !== null) {
199 return self::$allindicators;
200 }
201
202 $classes = self::get_analytics_classes('indicator');
203
204 self::$allindicators = [];
205 foreach ($classes as $fullclassname => $classpath) {
206 $instance = self::get_indicator($fullclassname);
207 if ($instance) {
208 // Using get_class as get_component_classes_in_namespace returns double escaped fully qualified class names.
209 self::$allindicators['\\' . get_class($instance)] = $instance;
210 }
211 }
212
213 return self::$allindicators;
214 }
215
216 public static function get_target($fullclassname) {
217 if (!self::is_valid($fullclassname, 'core_analytics\local\target\base')) {
218 return false;
219 }
220 return new $fullclassname();
221 }
222
223 /**
224 * Returns an instance of the provided indicator.
225 *
226 * @param string $fullclassname
227 * @return \core_analytics\local\indicator\base|false False if it is not valid.
228 */
229 public static function get_indicator($fullclassname) {
230 if (!self::is_valid($fullclassname, 'core_analytics\local\indicator\base')) {
231 return false;
232 }
233 return new $fullclassname();
234 }
235
236 /**
237 * Returns whether a time splitting method is valid or not.
238 *
239 * @param string $fullclassname
240 * @return bool
241 */
242 public static function is_valid($fullclassname, $baseclass) {
243 if (is_subclass_of($fullclassname, $baseclass)) {
244 if ((new \ReflectionClass($fullclassname))->isInstantiable()) {
245 return true;
246 }
247 }
248 return false;
249 }
250
251 /**
252 * Returns the provided element classes in the site.
253 *
254 * @param string $element
255 * @return string[] Array keys are the FQCN and the values the class path.
256 */
257 private static function get_analytics_classes($element) {
258
259 // Just in case...
260 $element = clean_param($element, PARAM_ALPHAEXT);
261
262 $classes = \core_component::get_component_classes_in_namespace('core_analytics', 'local\\' . $element);
263 foreach (\core_component::get_plugin_types() as $type => $unusedplugintypepath) {
264 foreach (\core_component::get_plugin_list($type) as $pluginname => $unusedpluginpath) {
265 $frankenstyle = $type . '_' . $pluginname;
266 $classes += \core_component::get_component_classes_in_namespace($frankenstyle, 'analytics\\' . $element);
267 }
268 }
269 return $classes;
270 }
271}