MDL-58974 tool_analytics: Adding missing absolute paths
[moodle.git] / admin / tool / analytics / classes / output / models_list.php
blob47cd54acd1aee6f598ee5bf134796d8ab81ae45e
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.
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/>.
17 /**
18 * Prediction models list page.
20 * @package tool_analytics
21 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace tool_analytics\output;
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Shows tool_analytics models list.
32 * @package tool_analytics
33 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class models_list implements \renderable, \templatable {
38 /**
39 * models
41 * @var \core_analytics\model[]
43 protected $models = array();
45 /**
46 * __construct
48 * @param \core_analytics\model[] $models
49 * @return void
51 public function __construct($models) {
52 $this->models = $models;
55 /**
56 * Exports the data.
58 * @param \renderer_base $output
59 * @return \stdClass
61 public function export_for_template(\renderer_base $output) {
62 global $PAGE;
64 $data = new \stdClass();
66 $newmodelmenu = new \action_menu();
67 $newmodelmenu->set_menu_trigger(get_string('newmodel', 'tool_analytics'), 'btn btn-default');
68 $newmodelmenu->set_alignment(\action_menu::TL, \action_menu::BL);
70 $newmodelmenu->add(new \action_menu_link(
71 new \moodle_url('/admin/tool/analytics/createmodel.php'),
72 new \pix_icon('i/edit', ''),
73 get_string('createmodel', 'tool_analytics'),
74 false
75 ));
77 $newmodelmenu->add(new \action_menu_link(
78 new \moodle_url('/admin/tool/analytics/importmodel.php'),
79 new \pix_icon('i/import', ''),
80 get_string('importmodel', 'tool_analytics'),
81 false
82 ));
84 $newmodelmenu->add(new \action_menu_link(
85 new \moodle_url('/admin/tool/analytics/restoredefault.php'),
86 new \pix_icon('i/reload', ''),
87 get_string('restoredefault', 'tool_analytics'),
88 false
89 ));
91 $data->newmodelmenu = $newmodelmenu->export_for_template($output);
93 $onlycli = get_config('analytics', 'onlycli');
94 if ($onlycli === false) {
95 // Default applied if no config found.
96 $onlycli = 1;
99 // Evaluation options.
100 $timesplittingsforevaluation = \core_analytics\manager::get_time_splitting_methods_for_evaluation(true);
102 $misconfiguredmodels = [];
103 $data->models = array();
104 foreach ($this->models as $model) {
105 $modeldata = $model->export($output);
107 // Check if there is a help icon for the target to show.
108 $identifier = $modeldata->target->get_identifier();
109 $component = $modeldata->target->get_component();
110 if (get_string_manager()->string_exists($identifier . '_help', $component)) {
111 $helpicon = new \help_icon($identifier, $component);
112 $modeldata->targethelp = $helpicon->export_for_template($output);
113 } else {
114 // We really want to encourage developers to add help to their targets.
115 debugging("The target '{$modeldata->target}' should include a '{$identifier}_help' string to
116 describe its purpose.", DEBUG_DEVELOPER);
119 if ($model->invalid_timesplitting_selected()) {
120 $misconfiguredmodels[$model->get_id()] = $model->get_name();
123 // Check if there is a help icon for the indicators to show.
124 if (!empty($modeldata->indicators)) {
125 $indicators = array();
126 foreach ($modeldata->indicators as $ind) {
127 // Create the indicator with the details we want for the context.
128 $indicator = new \stdClass();
129 $indicator->name = $ind->out();
130 $identifier = $ind->get_identifier();
131 $component = $ind->get_component();
132 if (get_string_manager()->string_exists($identifier . '_help', $component)) {
133 $helpicon = new \help_icon($identifier, $component);
134 $indicator->help = $helpicon->export_for_template($output);
135 } else {
136 // We really want to encourage developers to add help to their indicators.
137 debugging("The indicator '{$ind}' should include a '{$identifier}_help' string to
138 describe its purpose.", DEBUG_DEVELOPER);
140 $indicators[] = $indicator;
142 $modeldata->indicators = $indicators;
145 $modeldata->indicatorsnum = count($modeldata->indicators);
147 // Check if there is a help icon for the time splitting method.
148 if (!empty($modeldata->timesplitting)) {
149 $identifier = $modeldata->timesplitting->get_identifier();
150 $component = $modeldata->timesplitting->get_component();
151 if (get_string_manager()->string_exists($identifier . '_help', $component)) {
152 $helpicon = new \help_icon($identifier, $component);
153 $modeldata->timesplittinghelp = $helpicon->export_for_template($output);
154 } else {
155 // We really want to encourage developers to add help to their time splitting methods.
156 debugging("The analysis interval '{$modeldata->timesplitting}' should include a '{$identifier}_help'
157 string to describe its purpose.", DEBUG_DEVELOPER);
159 } else {
160 $helpicon = new \help_icon('timesplittingnotdefined', 'tool_analytics');
161 $modeldata->timesplittinghelp = $helpicon->export_for_template($output);
164 // Has this model generated predictions?.
165 $predictioncontexts = $model->get_predictions_contexts();
166 $anypredictionobtained = $model->any_prediction_obtained();
168 // Model predictions list.
169 if (!$model->is_enabled()) {
170 $modeldata->noinsights = get_string('disabledmodel', 'analytics');
171 } else if ($model->uses_insights()) {
172 if ($predictioncontexts) {
173 $url = new \moodle_url('/report/insights/insights.php', array('modelid' => $model->get_id()));
174 $modeldata->insights = \tool_analytics\output\helper::prediction_context_selector($predictioncontexts,
175 $url, $output);
178 if (empty($modeldata->insights)) {
179 if ($anypredictionobtained) {
180 $modeldata->noinsights = get_string('noinsights', 'analytics');
181 } else {
182 $modeldata->noinsights = get_string('nopredictionsyet', 'analytics');
186 } else {
187 $modeldata->noinsights = get_string('noinsightsmodel', 'analytics');
190 // Actions.
191 $actionsmenu = new \action_menu();
192 $actionsmenu->set_menu_trigger(get_string('actions'));
193 $actionsmenu->set_owner_selector('model-actions-' . $model->get_id());
194 $actionsmenu->set_alignment(\action_menu::TL, \action_menu::BL);
196 $urlparams = ['id' => $model->get_id(), 'sesskey' => sesskey()];
198 // Get predictions.
199 if (!$onlycli && $modeldata->enabled && !empty($modeldata->timesplitting)) {
200 $urlparams['action'] = 'scheduledanalysis';
201 $url = new \moodle_url('/admin/tool/analytics/model.php', $urlparams);
202 $icon = new \action_menu_link_secondary($url,
203 new \pix_icon('i/notifications', get_string('executescheduledanalysis', 'tool_analytics')),
204 get_string('executescheduledanalysis', 'tool_analytics'));
205 $actionsmenu->add($icon);
208 // Evaluate machine-learning-based models.
209 if (!$onlycli && $model->get_indicators() && !$model->is_static()) {
211 // Extra is_trained call as trained_locally returns false if the model has not been trained yet.
212 $trainedonlyexternally = !$model->trained_locally() && $model->is_trained();
214 $actionid = 'evaluate-' . $model->get_id();
216 // Evaluation options.
217 $modeltimesplittingmethods = $this->timesplittings_options_for_evaluation($model, $timesplittingsforevaluation);
219 // Include the current time-splitting method as the default selection method the model already have one.
220 if ($model->get_model_obj()->timesplitting) {
221 $currenttimesplitting = ['id' => 'current', 'text' => get_string('currenttimesplitting', 'tool_analytics')];
222 array_unshift($modeltimesplittingmethods, $currenttimesplitting);
225 $evaluateparams = [$actionid, $trainedonlyexternally, $modeltimesplittingmethods];
226 $PAGE->requires->js_call_amd('tool_analytics/model', 'selectEvaluationOptions', $evaluateparams);
227 $urlparams['action'] = 'evaluate';
228 $url = new \moodle_url('/admin/tool/analytics/model.php', $urlparams);
229 $icon = new \action_menu_link_secondary($url, new \pix_icon('i/calc', get_string('evaluate', 'tool_analytics')),
230 get_string('evaluate', 'tool_analytics'), ['data-action-id' => $actionid]);
231 $actionsmenu->add($icon);
234 // Machine-learning-based models evaluation log.
235 if (!$model->is_static() && $model->get_logs()) {
236 $urlparams['action'] = 'log';
237 $url = new \moodle_url('/admin/tool/analytics/model.php', $urlparams);
238 $icon = new \action_menu_link_secondary($url, new \pix_icon('i/report', get_string('viewlog', 'tool_analytics')),
239 get_string('viewlog', 'tool_analytics'));
240 $actionsmenu->add($icon);
243 // Edit model.
244 $urlparams['action'] = 'edit';
245 $url = new \moodle_url('/admin/tool/analytics/model.php', $urlparams);
246 $icon = new \action_menu_link_secondary($url, new \pix_icon('t/edit', get_string('edit')), get_string('edit'));
247 $actionsmenu->add($icon);
249 // Enable / disable.
250 if ($model->is_enabled() || !empty($modeldata->timesplitting)) {
251 // If there is no timesplitting method set, the model can not be enabled.
252 if ($model->is_enabled()) {
253 $action = 'disable';
254 $text = get_string('disable');
255 $icontype = 't/block';
256 } else {
257 $action = 'enable';
258 $text = get_string('enable');
259 $icontype = 'i/checked';
261 $urlparams['action'] = $action;
262 $url = new \moodle_url('/admin/tool/analytics/model.php', $urlparams);
263 $icon = new \action_menu_link_secondary($url, new \pix_icon($icontype, $text), $text);
264 $actionsmenu->add($icon);
267 // Export.
268 if (!$model->is_static()) {
270 $fullysetup = $model->get_indicators() && !empty($modeldata->timesplitting);
271 $istrained = $model->is_trained();
273 if ($fullysetup || $istrained) {
275 $url = new \moodle_url('/admin/tool/analytics/model.php', $urlparams);
276 // Clear the previous action param from the URL, we will set it in JS.
277 $url->remove_params('action');
279 $actionid = 'export-' . $model->get_id();
280 $PAGE->requires->js_call_amd('tool_analytics/model', 'selectExportOptions',
281 [$actionid, $istrained]);
283 $icon = new \action_menu_link_secondary($url, new \pix_icon('i/export',
284 get_string('export', 'tool_analytics')), get_string('export', 'tool_analytics'),
285 ['data-action-id' => $actionid]);
286 $actionsmenu->add($icon);
290 // Effectivity report.
291 if (!empty($anypredictionobtained) && $model->uses_insights()) {
292 $urlparams['action'] = 'effectivenessreport';
293 $url = new \moodle_url('/admin/tool/analytics/model.php', $urlparams);
294 $pix = new \pix_icon('i/report', get_string('effectivenessreport', 'tool_analytics'));
295 $icon = new \action_menu_link_secondary($url, $pix, get_string('effectivenessreport', 'tool_analytics'));
296 $actionsmenu->add($icon);
299 // Invalid analysables.
300 $analyser = $model->get_analyser(['notimesplitting' => true]);
301 if (!$analyser instanceof \core_analytics\local\analyser\sitewide) {
302 $urlparams['action'] = 'invalidanalysables';
303 $url = new \moodle_url('/admin/tool/analytics/model.php', $urlparams);
304 $pix = new \pix_icon('i/report', get_string('invalidanalysables', 'tool_analytics'));
305 $icon = new \action_menu_link_secondary($url, $pix, get_string('invalidanalysables', 'tool_analytics'));
306 $actionsmenu->add($icon);
309 // Clear model.
310 if (!empty($anypredictionobtained) || $model->is_trained()) {
311 $actionid = 'clear-' . $model->get_id();
312 $PAGE->requires->js_call_amd('tool_analytics/model', 'confirmAction', [$actionid, 'clear']);
313 $urlparams['action'] = 'clear';
314 $url = new \moodle_url('/admin/tool/analytics/model.php', $urlparams);
315 $icon = new \action_menu_link_secondary($url, new \pix_icon('e/cleanup_messy_code',
316 get_string('clearpredictions', 'tool_analytics')), get_string('clearpredictions', 'tool_analytics'),
317 ['data-action-id' => $actionid]);
318 $actionsmenu->add($icon);
321 // Delete model.
322 $actionid = 'delete-' . $model->get_id();
323 $PAGE->requires->js_call_amd('tool_analytics/model', 'confirmAction', [$actionid, 'delete']);
324 $urlparams['action'] = 'delete';
325 $url = new \moodle_url('/admin/tool/analytics/model.php', $urlparams);
326 $icon = new \action_menu_link_secondary($url, new \pix_icon('t/delete',
327 get_string('delete', 'tool_analytics')), get_string('delete', 'tool_analytics'),
328 ['data-action-id' => $actionid]);
329 $actionsmenu->add($icon);
331 $modeldata->actions = $actionsmenu->export_for_template($output);
333 $data->models[] = $modeldata;
336 $data->warnings = [];
337 $data->infos = [];
338 if (!$onlycli) {
339 $data->warnings[] = (object)array('message' => get_string('bettercli', 'tool_analytics'), 'closebutton' => true);
340 } else {
341 $url = new \moodle_url('/admin/settings.php', array('section' => 'analyticssettings'),
342 'id_s_analytics_onlycli');
344 $langstrid = 'clievaluationandpredictionsnoadmin';
345 if (is_siteadmin()) {
346 $langstrid = 'clievaluationandpredictions';
348 $data->infos[] = (object)array('message' => get_string($langstrid, 'tool_analytics', $url->out()),
349 'closebutton' => true);
352 if ($misconfiguredmodels) {
353 $warningstr = get_string('invalidtimesplittinginmodels', 'tool_analytics', implode(', ', $misconfiguredmodels));
354 $data->warnings[] = (object)array('message' => $warningstr, 'closebutton' => true);
357 return $data;
361 * Returns the list of time splitting methods that are available for evaluation.
363 * @param \core_analytics\model $model
364 * @param array $timesplittingsforevaluation
365 * @return array
367 private function timesplittings_options_for_evaluation(\core_analytics\model $model,
368 array $timesplittingsforevaluation): array {
370 $modeltimesplittingmethods = [
371 ['id' => 'all', 'text' => get_string('alltimesplittingmethods', 'tool_analytics')],
373 $potentialtimesplittingmethods = $model->get_potential_timesplittings();
374 foreach ($timesplittingsforevaluation as $timesplitting) {
375 if (empty($potentialtimesplittingmethods[$timesplitting->get_id()])) {
376 // This time-splitting method can not be used for this model.
377 continue;
379 $modeltimesplittingmethods[] = [
380 'id' => \tool_analytics\output\helper::class_to_option($timesplitting->get_id()),
381 'text' => $timesplitting->get_name()->out(),
385 return $modeltimesplittingmethods;