MDL-64786 analytics: Make the list of indicators collapsed by default
[moodle.git] / admin / tool / analytics / classes / output / models_list.php
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/>.
17 /**
18  * Prediction models list page.
19  *
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
23  */
25 namespace tool_analytics\output;
27 defined('MOODLE_INTERNAL') || die();
29 /**
30  * Shows tool_analytics models list.
31  *
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
35  */
36 class models_list implements \renderable, \templatable {
38     /**
39      * models
40      *
41      * @var \core_analytics\model[]
42      */
43     protected $models = array();
45     /**
46      * __construct
47      *
48      * @param \core_analytics\model[] $models
49      * @return void
50      */
51     public function __construct($models) {
52         $this->models = $models;
53     }
55     /**
56      * Exports the data.
57      *
58      * @param \renderer_base $output
59      * @return \stdClass
60      */
61     public function export_for_template(\renderer_base $output) {
62         global $PAGE;
64         $data = new \stdClass();
65         $data->importmodelurl = new \moodle_url('/admin/tool/analytics/importmodel.php');
66         $data->createmodelurl = new \moodle_url('/admin/tool/analytics/createmodel.php');
68         $onlycli = get_config('analytics', 'onlycli');
69         if ($onlycli === false) {
70             // Default applied if no config found.
71             $onlycli = 1;
72         }
74         // Evaluation options.
75         $timesplittingmethods = [
76             ['id' => 'all', 'text' => get_string('alltimesplittingmethods', 'tool_analytics')],
77         ];
78         foreach (\core_analytics\manager::get_time_splitting_methods_for_evaluation(true) as $timesplitting) {
79             $timesplittingmethods[] = [
80                 'id' => \tool_analytics\output\helper::class_to_option($timesplitting->get_id()),
81                 'text' => $timesplitting->get_name()->out(),
82             ];
83         }
85         $data->models = array();
86         foreach ($this->models as $model) {
87             $modeldata = $model->export($output);
89             // Check if there is a help icon for the target to show.
90             $identifier = $modeldata->target->get_identifier();
91             $component = $modeldata->target->get_component();
92             if (get_string_manager()->string_exists($identifier . '_help', $component)) {
93                 $helpicon = new \help_icon($identifier, $component);
94                 $modeldata->targethelp = $helpicon->export_for_template($output);
95             } else {
96                 // We really want to encourage developers to add help to their targets.
97                 debugging("The target '{$modeldata->target}' should include a '{$identifier}_help' string to
98                     describe its purpose.", DEBUG_DEVELOPER);
99             }
101             // Check if there is a help icon for the indicators to show.
102             if (!empty($modeldata->indicators)) {
103                 $indicators = array();
104                 foreach ($modeldata->indicators as $ind) {
105                     // Create the indicator with the details we want for the context.
106                     $indicator = new \stdClass();
107                     $indicator->name = $ind->out();
108                     $identifier = $ind->get_identifier();
109                     $component = $ind->get_component();
110                     if (get_string_manager()->string_exists($identifier . '_help', $component)) {
111                         $helpicon = new \help_icon($identifier, $component);
112                         $indicator->help = $helpicon->export_for_template($output);
113                     } else {
114                         // We really want to encourage developers to add help to their indicators.
115                         debugging("The indicator '{$ind}' should include a '{$identifier}_help' string to
116                             describe its purpose.", DEBUG_DEVELOPER);
117                     }
118                     $indicators[] = $indicator;
119                 }
120                 $modeldata->indicators = $indicators;
121             }
123             $modeldata->indicatorsnum = count($modeldata->indicators);
125             // Check if there is a help icon for the time splitting method.
126             if (!empty($modeldata->timesplitting)) {
127                 $identifier = $modeldata->timesplitting->get_identifier();
128                 $component = $modeldata->timesplitting->get_component();
129                 if (get_string_manager()->string_exists($identifier . '_help', $component)) {
130                     $helpicon = new \help_icon($identifier, $component);
131                     $modeldata->timesplittinghelp = $helpicon->export_for_template($output);
132                 } else {
133                     // We really want to encourage developers to add help to their time splitting methods.
134                     debugging("The time splitting method '{$modeldata->timesplitting}' should include a '{$identifier}_help'
135                         string to describe its purpose.", DEBUG_DEVELOPER);
136                 }
137             } else {
138                 $helpicon = new \help_icon('timesplittingnotdefined', 'tool_analytics');
139                 $modeldata->timesplittinghelp = $helpicon->export_for_template($output);
140             }
142             // Has this model generated predictions?.
143             $predictioncontexts = $model->get_predictions_contexts();
145             // Model predictions list.
146             if (!$model->is_enabled()) {
147                 $modeldata->noinsights = get_string('disabledmodel', 'analytics');
148             } else if ($model->uses_insights()) {
149                 if ($predictioncontexts) {
151                     foreach ($predictioncontexts as $contextid => $unused) {
152                         // We prepare this to be used as single_select template options.
153                         $context = \context::instance_by_id($contextid);
155                         // Special name for system level predictions as showing "System is not visually nice".
156                         if ($contextid == SYSCONTEXTID) {
157                             $contextname = get_string('allpredictions', 'tool_analytics');
158                         } else {
159                             $contextname = shorten_text($context->get_context_name(true, true), 90);
160                         }
161                         $predictioncontexts[$contextid] = $contextname;
162                     }
163                     \core_collator::asort($predictioncontexts);
165                     if (!empty($predictioncontexts)) {
166                         $url = new \moodle_url('/report/insights/insights.php', array('modelid' => $model->get_id()));
167                         $singleselect = new \single_select($url, 'contextid', $predictioncontexts);
168                         $modeldata->insights = $singleselect->export_for_template($output);
169                     }
170                 }
172                 if (empty($modeldata->insights)) {
173                     if ($model->any_prediction_obtained()) {
174                         $modeldata->noinsights = get_string('noinsights', 'analytics');
175                     } else {
176                         $modeldata->noinsights = get_string('nopredictionsyet', 'analytics');
177                     }
178                 }
180             } else {
181                 $modeldata->noinsights = get_string('noinsightsmodel', 'analytics');
182             }
184             // Actions.
185             $actionsmenu = new \action_menu();
186             $actionsmenu->set_menu_trigger(get_string('actions'));
187             $actionsmenu->set_owner_selector('model-actions-' . $model->get_id());
188             $actionsmenu->set_alignment(\action_menu::TL, \action_menu::BL);
190             $urlparams = ['id' => $model->get_id(), 'sesskey' => sesskey()];
192             // Get predictions.
193             if (!$onlycli && $modeldata->enabled && !empty($modeldata->timesplitting)) {
194                 $urlparams['action'] = 'getpredictions';
195                 $url = new \moodle_url('model.php', $urlparams);
196                 $icon = new \action_menu_link_secondary($url, new \pix_icon('i/notifications',
197                     get_string('getpredictions', 'tool_analytics')), get_string('getpredictions', 'tool_analytics'));
198                 $actionsmenu->add($icon);
199             }
201             // Evaluate machine-learning-based models.
202             if (!$onlycli && $model->get_indicators() && !$model->is_static()) {
204                 // Extra is_trained call as trained_locally returns false if the model has not been trained yet.
205                 $trainedonlyexternally = !$model->trained_locally() && $model->is_trained();
207                 $actionid = 'evaluate-' . $model->get_id();
209                 $modeltimesplittingmethods = $timesplittingmethods;
210                 // Include the current time-splitting method as the default selection method the model already have one.
211                 if ($model->get_model_obj()->timesplitting) {
212                     $currenttimesplitting = ['id' => 'current', 'text' => get_string('currenttimesplitting', 'tool_analytics')];
213                     array_unshift($modeltimesplittingmethods, $currenttimesplitting);
214                 }
216                 $evaluateparams = [$actionid, $trainedonlyexternally, $modeltimesplittingmethods];
217                 $PAGE->requires->js_call_amd('tool_analytics/model', 'selectEvaluationOptions', $evaluateparams);
218                 $urlparams['action'] = 'evaluate';
219                 $url = new \moodle_url('model.php', $urlparams);
220                 $icon = new \action_menu_link_secondary($url, new \pix_icon('i/calc', get_string('evaluate', 'tool_analytics')),
221                     get_string('evaluate', 'tool_analytics'), ['data-action-id' => $actionid]);
222                 $actionsmenu->add($icon);
223             }
225             // Machine-learning-based models evaluation log.
226             if (!$model->is_static() && $model->get_logs()) {
227                 $urlparams['action'] = 'log';
228                 $url = new \moodle_url('model.php', $urlparams);
229                 $icon = new \action_menu_link_secondary($url, new \pix_icon('i/report', get_string('viewlog', 'tool_analytics')),
230                     get_string('viewlog', 'tool_analytics'));
231                 $actionsmenu->add($icon);
232             }
234             // Edit model.
235             $urlparams['action'] = 'edit';
236             $url = new \moodle_url('model.php', $urlparams);
237             $icon = new \action_menu_link_secondary($url, new \pix_icon('t/edit', get_string('edit')), get_string('edit'));
238             $actionsmenu->add($icon);
240             // Enable / disable.
241             if ($model->is_enabled() || !empty($modeldata->timesplitting)) {
242                 // If there is no timesplitting method set, the model can not be enabled.
243                 if ($model->is_enabled()) {
244                     $action = 'disable';
245                     $text = get_string('disable');
246                     $icontype = 't/block';
247                 } else {
248                     $action = 'enable';
249                     $text = get_string('enable');
250                     $icontype = 'i/checked';
251                 }
252                 $urlparams['action'] = $action;
253                 $url = new \moodle_url('model.php', $urlparams);
254                 $icon = new \action_menu_link_secondary($url, new \pix_icon($icontype, $text), $text);
255                 $actionsmenu->add($icon);
256             }
258             // Export.
259             if (!$model->is_static()) {
261                 $fullysetup = $model->get_indicators() && !empty($modeldata->timesplitting);
262                 $istrained = $model->is_trained();
264                 if ($fullysetup || $istrained) {
266                     $url = new \moodle_url('model.php', $urlparams);
267                     // Clear the previous action param from the URL, we will set it in JS.
268                     $url->remove_params('action');
270                     $actionid = 'export-' . $model->get_id();
271                     $PAGE->requires->js_call_amd('tool_analytics/model', 'selectExportOptions',
272                         [$actionid, $istrained]);
274                     $icon = new \action_menu_link_secondary($url, new \pix_icon('i/export',
275                         get_string('export', 'tool_analytics')), get_string('export', 'tool_analytics'),
276                         ['data-action-id' => $actionid]);
277                     $actionsmenu->add($icon);
278                 }
279             }
281             // Invalid analysables.
282             $analyser = $model->get_analyser(['notimesplitting' => true]);
283             if (!$analyser instanceof \core_analytics\local\analyser\sitewide) {
284                 $urlparams['action'] = 'invalidanalysables';
285                 $url = new \moodle_url('model.php', $urlparams);
286                 $pix = new \pix_icon('i/report', get_string('invalidanalysables', 'tool_analytics'));
287                 $icon = new \action_menu_link_secondary($url, $pix, get_string('invalidanalysables', 'tool_analytics'));
288                 $actionsmenu->add($icon);
289             }
291             // Clear model.
292             if (!empty($predictioncontexts) || $model->is_trained()) {
293                 $actionid = 'clear-' . $model->get_id();
294                 $PAGE->requires->js_call_amd('tool_analytics/model', 'confirmAction', [$actionid, 'clear']);
295                 $urlparams['action'] = 'clear';
296                 $url = new \moodle_url('model.php', $urlparams);
297                 $icon = new \action_menu_link_secondary($url, new \pix_icon('e/cleanup_messy_code',
298                     get_string('clearpredictions', 'tool_analytics')), get_string('clearpredictions', 'tool_analytics'),
299                     ['data-action-id' => $actionid]);
300                 $actionsmenu->add($icon);
301             }
303             // Delete model.
304             if (!$model->is_static()) {
305                 $actionid = 'delete-' . $model->get_id();
306                 $PAGE->requires->js_call_amd('tool_analytics/model', 'confirmAction', [$actionid, 'delete']);
307                 $urlparams['action'] = 'delete';
308                 $url = new \moodle_url('model.php', $urlparams);
309                 $icon = new \action_menu_link_secondary($url, new \pix_icon('t/delete',
310                     get_string('delete', 'tool_analytics')), get_string('delete', 'tool_analytics'),
311                     ['data-action-id' => $actionid]);
312                 $actionsmenu->add($icon);
313             }
315             $modeldata->actions = $actionsmenu->export_for_template($output);
317             $data->models[] = $modeldata;
318         }
320         if (!$onlycli) {
321             $data->warnings = array(
322                 (object)array('message' => get_string('bettercli', 'tool_analytics'), 'closebutton' => true)
323             );
324         } else {
325             $url = new \moodle_url('/admin/settings.php', array('section' => 'analyticssettings'),
326                 'id_s_analytics_onlycli');
328             $langstrid = 'clievaluationandpredictionsnoadmin';
329             if (is_siteadmin()) {
330                 $langstrid = 'clievaluationandpredictions';
331             }
332             $data->infos = array(
333                 (object)array('message' => get_string($langstrid, 'tool_analytics', $url->out()),
334                     'closebutton' => true)
335             );
336         }
338         return $data;
339     }