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