2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Analytics basic actions manager.
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
25 namespace core_analytics;
27 defined('MOODLE_INTERNAL') || die();
30 * Analytics basic actions manager.
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
39 * @var \core_analytics\predictor[]
41 protected static $predictionprocessors = null;
44 * @var \core_analytics\local\indicator\base[]
46 protected static $allindicators = null;
49 * @var \core_analytics\local\time_splitting\base[]
51 protected static $alltimesplittings = null;
53 public static function get_all_models($enabled = false, $trained = false, $predictioncontext = false) {
58 $filters['enabled'] = 1;
61 $filters['trained'] = 1;
63 $modelobjs = $DB->get_records('analytics_models', $filters);
66 foreach ($modelobjs as $modelobj) {
67 $model = new \core_analytics\model($modelobj);
68 if (!$predictioncontext || $model->predictions_exist($predictioncontext)) {
69 $models[$modelobj->id] = $model;
76 * Returns the site selected predictions processor.
78 * @param string $predictionclass
79 * @param bool $checkisready
80 * @return \core_analytics\predictor
82 public static function get_predictions_processor($predictionclass = false, $checkisready = true) {
84 // We want 0 or 1 so we can use it as an array key for caching.
85 $checkisready = intval($checkisready);
87 if ($predictionclass === false) {
88 $predictionclass = get_config('analytics', 'predictionsprocessor');
91 if (empty($predictionclass)) {
92 // Use the default one if nothing set.
93 $predictionclass = '\mlbackend_php\processor';
96 if (!class_exists($predictionclass)) {
97 throw new \coding_exception('Invalid predictions processor ' . $predictionclass . '.');
100 $interfaces = class_implements($predictionclass);
101 if (empty($interfaces['core_analytics\predictor'])) {
102 throw new \coding_exception($predictionclass . ' should implement \core_analytics\predictor.');
105 // Return it from the cached list.
106 if (!isset(self::$predictionprocessors[$checkisready][$predictionclass])) {
108 $instance = new $predictionclass();
110 $isready = $instance->is_ready();
111 if ($isready !== true) {
112 throw new \moodle_exception('errorprocessornotready', 'analytics', '', $isready);
115 self::$predictionprocessors[$checkisready][$predictionclass] = $instance;
118 return self::$predictionprocessors[$checkisready][$predictionclass];
121 public static function get_all_prediction_processors() {
123 $mlbackends = \core_component::get_plugin_list('mlbackend');
125 $predictionprocessors = array();
126 foreach ($mlbackends as $mlbackend => $unused) {
127 $classfullpath = '\\mlbackend_' . $mlbackend . '\\processor';
128 $predictionprocessors[$classfullpath] = self::get_predictions_processor($classfullpath, false);
130 return $predictionprocessors;
134 * Get all available time splitting methods.
136 * @return \core_analytics\time_splitting\base[]
138 public static function get_all_time_splittings() {
139 if (self::$alltimesplittings !== null) {
140 return self::$alltimesplittings;
143 $classes = self::get_analytics_classes('time_splitting');
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.
150 self::$alltimesplittings[$instance->get_id()] = $instance;
154 return self::$alltimesplittings;
158 * Returns the enabled time splitting methods.
160 * @return \core_analytics\local\time_splitting\base[]
162 public static function get_enabled_time_splitting_methods() {
164 if ($enabledtimesplittings = get_config('analytics', 'timesplittings')) {
165 $enabledtimesplittings = array_flip(explode(',', $enabledtimesplittings));
168 $timesplittings = self::get_all_time_splittings();
169 foreach ($timesplittings as $key => $timesplitting) {
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]);
176 return $timesplittings;
180 * Returns a time splitting method by its classname.
182 * @param string $fullclassname
183 * @return \core_analytics\local\time_splitting\base|false False if it is not valid.
185 public static function get_time_splitting($fullclassname) {
186 if (!self::is_valid($fullclassname, '\core_analytics\local\time_splitting\base')) {
189 return new $fullclassname();
193 * Return all system indicators.
195 * @return \core_analytics\local\indicator\base[]
197 public static function get_all_indicators() {
198 if (self::$allindicators !== null) {
199 return self::$allindicators;
202 $classes = self::get_analytics_classes('indicator');
204 self::$allindicators = [];
205 foreach ($classes as $fullclassname => $classpath) {
206 $instance = self::get_indicator($fullclassname);
208 // Using get_class as get_component_classes_in_namespace returns double escaped fully qualified class names.
209 self::$allindicators[$instance->get_id()] = $instance;
213 return self::$allindicators;
216 public static function get_target($fullclassname) {
217 if (!self::is_valid($fullclassname, 'core_analytics\local\target\base')) {
220 return new $fullclassname();
224 * Returns an instance of the provided indicator.
226 * @param string $fullclassname
227 * @return \core_analytics\local\indicator\base|false False if it is not valid.
229 public static function get_indicator($fullclassname) {
230 if (!self::is_valid($fullclassname, 'core_analytics\local\indicator\base')) {
233 return new $fullclassname();
237 * Returns whether a time splitting method is valid or not.
239 * @param string $fullclassname
242 public static function is_valid($fullclassname, $baseclass) {
243 if (is_subclass_of($fullclassname, $baseclass)) {
244 if ((new \ReflectionClass($fullclassname))->isInstantiable()) {
252 * get_analytics_logstore
254 * @return \core\log\reader
256 public static function get_analytics_logstore() {
257 $readers = get_log_manager()->get_readers('core\log\sql_reader');
258 $analyticsstore = get_config('analytics', 'logstore');
259 if (empty($analyticsstore)) {
260 $logstore = reset($readers);
261 } else if (!empty($readers[$analyticsstore])) {
262 $logstore = $readers[$analyticsstore];
264 $logstore = reset($readers);
265 debugging('The selected log store for analytics is not available anymore. Using "' .
266 $logstore->get_name() . '"', DEBUG_DEVELOPER);
269 if (!$logstore->is_logging()) {
270 debugging('The selected log store for analytics "' . $logstore->get_name() .
271 '" is not logging activity logs', DEBUG_DEVELOPER);
278 * Returns the provided element classes in the site.
280 * @param string $element
281 * @return string[] Array keys are the FQCN and the values the class path.
283 private static function get_analytics_classes($element) {
286 $element = clean_param($element, PARAM_ALPHAEXT);
288 $classes = \core_component::get_component_classes_in_namespace('core_analytics', 'local\\' . $element);
289 foreach (\core_component::get_plugin_types() as $type => $unusedplugintypepath) {
290 foreach (\core_component::get_plugin_list($type) as $pluginname => $unusedpluginpath) {
291 $frankenstyle = $type . '_' . $pluginname;
292 $classes += \core_component::get_component_classes_in_namespace($frankenstyle, 'analytics\\' . $element);