MDL-64506 templates: replace spans where and col-x appears
[moodle.git] / admin / tool / analytics / model.php
CommitLineData
efaf8534
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 * Model-related actions.
19 *
e10b29ed 20 * @package tool_analytics
efaf8534
DM
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
25require_once(__DIR__ . '/../../../config.php');
e4453adc 26require_once($CFG->libdir . '/filelib.php');
efaf8534
DM
27
28$id = required_param('id', PARAM_INT);
29$action = required_param('action', PARAM_ALPHANUMEXT);
30
efaf8534 31require_login();
efaf8534
DM
32
33$model = new \core_analytics\model($id);
1611308b 34\core_analytics\manager::check_can_manage_models();
efaf8534 35
e4453adc 36$returnurl = new \moodle_url('/admin/tool/analytics/index.php');
efaf8534 37$params = array('id' => $id, 'action' => $action);
e10b29ed 38$url = new \moodle_url('/admin/tool/analytics/model.php', $params);
efaf8534
DM
39
40switch ($action) {
41
42 case 'edit':
e10b29ed 43 $title = get_string('editmodel', 'tool_analytics', $model->get_target()->get_name());
efaf8534
DM
44 break;
45 case 'evaluate':
e10b29ed 46 $title = get_string('evaluatemodel', 'tool_analytics');
efaf8534 47 break;
357507b4 48 case 'getpredictions':
e10b29ed 49 $title = get_string('getpredictions', 'tool_analytics');
efaf8534
DM
50 break;
51 case 'log':
e10b29ed 52 $title = get_string('viewlog', 'tool_analytics');
efaf8534 53 break;
5c140ac4
DM
54 case 'enable':
55 $title = get_string('enable');
56 break;
57 case 'disable':
58 $title = get_string('disable');
59 break;
e4453adc
DM
60 case 'delete':
61 $title = get_string('delete');
62 break;
349c4412
AA
63 case 'exportdata':
64 $title = get_string('exporttrainingdata', 'tool_analytics');
65 break;
66 case 'exportmodel':
67 $title = get_string('exportmodel', 'tool_analytics');
d126f838 68 break;
8816022d
DM
69 case 'clear':
70 $title = get_string('clearpredictions', 'tool_analytics');
71 break;
019ad9c7
DM
72 case 'invalidanalysables':
73 $title = get_string('invalidanalysables', 'tool_analytics');
74 break;
efaf8534 75 default:
1cc2b4ba 76 throw new moodle_exception('errorunknownaction', 'analytics');
efaf8534
DM
77}
78
e4453adc 79\tool_analytics\output\helper::set_navbar($title, $url);
efaf8534 80
f19719c1
DM
81$onlycli = get_config('analytics', 'onlycli');
82if ($onlycli === false) {
83 // Default applied if no config found.
84 $onlycli = 1;
85}
86
efaf8534
DM
87switch ($action) {
88
5c140ac4 89 case 'enable':
8816022d
DM
90 confirm_sesskey();
91
5c140ac4 92 $model->enable();
e4453adc 93 redirect($returnurl);
8816022d 94 break;
5c140ac4
DM
95
96 case 'disable':
8816022d
DM
97 confirm_sesskey();
98
5c140ac4 99 $model->update(0, false, false);
e4453adc
DM
100 redirect($returnurl);
101 break;
102
103 case 'delete':
104 confirm_sesskey();
105
106 $model->delete();
107 redirect($returnurl);
8816022d 108 break;
5c140ac4 109
efaf8534 110 case 'edit':
8816022d 111 confirm_sesskey();
efaf8534 112
357507b4
DM
113 if ($model->is_static()) {
114 echo $OUTPUT->header();
e10b29ed 115 throw new moodle_exception('errornostaticedit', 'tool_analytics');
357507b4
DM
116 }
117
efaf8534
DM
118 $customdata = array(
119 'id' => $model->get_id(),
e4453adc 120 'trainedmodel' => $model->is_trained(),
efaf8534 121 'indicators' => $model->get_potential_indicators(),
3576b66b 122 'timesplittings' => \core_analytics\manager::get_all_time_splittings(),
ed12ba6b 123 'predictionprocessors' => \core_analytics\manager::get_all_prediction_processors()
efaf8534 124 );
e10b29ed 125 $mform = new \tool_analytics\output\form\edit_model(null, $customdata);
efaf8534
DM
126
127 if ($mform->is_cancelled()) {
e4453adc 128 redirect($returnurl);
efaf8534
DM
129
130 } else if ($data = $mform->get_data()) {
efaf8534
DM
131
132 // Converting option names to class names.
133 $indicators = array();
134 foreach ($data->indicators as $indicator) {
e10b29ed 135 $indicatorclass = \tool_analytics\output\helper::option_to_class($indicator);
efaf8534
DM
136 $indicators[] = \core_analytics\manager::get_indicator($indicatorclass);
137 }
e10b29ed 138 $timesplitting = \tool_analytics\output\helper::option_to_class($data->timesplitting);
ed12ba6b
DM
139 $predictionsprocessor = \tool_analytics\output\helper::option_to_class($data->predictionsprocessor);
140 $model->update($data->enabled, $indicators, $timesplitting, $predictionsprocessor);
e4453adc 141 redirect($returnurl);
efaf8534
DM
142 }
143
144 echo $OUTPUT->header();
145
146 $modelobj = $model->get_model_obj();
147
e10b29ed 148 $callable = array('\tool_analytics\output\helper', 'class_to_option');
efaf8534 149 $modelobj->indicators = array_map($callable, json_decode($modelobj->indicators));
e10b29ed 150 $modelobj->timesplitting = \tool_analytics\output\helper::class_to_option($modelobj->timesplitting);
ed12ba6b 151 $modelobj->predictionsprocessor = \tool_analytics\output\helper::class_to_option($modelobj->predictionsprocessor);
efaf8534
DM
152 $mform->set_data($modelobj);
153 $mform->display();
154 break;
155
156 case 'evaluate':
8816022d
DM
157 confirm_sesskey();
158
efaf8534 159 echo $OUTPUT->header();
357507b4
DM
160
161 if ($model->is_static()) {
e10b29ed 162 throw new moodle_exception('errornostaticevaluate', 'tool_analytics');
357507b4
DM
163 }
164
f19719c1
DM
165 if ($onlycli) {
166 throw new moodle_exception('erroronlycli', 'tool_analytics');
167 }
168
efaf8534
DM
169 // Web interface is used by people who can not use CLI nor code stuff, always use
170 // cached stuff as they will change the model through the web interface as well
171 // which invalidates the previously analysed stuff.
74823933
DM
172 $options = ['reuseprevanalysed' => true];
173
174 $mode = optional_param('mode', false, PARAM_ALPHANUM);
175 if ($mode == 'trainedmodel') {
176 $options['mode'] = 'trainedmodel';
177 }
178 $results = $model->evaluate($options);
e10b29ed 179 $renderer = $PAGE->get_renderer('tool_analytics');
efaf8534
DM
180 echo $renderer->render_evaluate_results($results, $model->get_analyser()->get_logs());
181 break;
182
357507b4 183 case 'getpredictions':
8816022d
DM
184 confirm_sesskey();
185
efaf8534
DM
186 echo $OUTPUT->header();
187
f19719c1
DM
188 if ($onlycli) {
189 throw new moodle_exception('erroronlycli', 'tool_analytics');
190 }
191
efaf8534
DM
192 $trainresults = $model->train();
193 $trainlogs = $model->get_analyser()->get_logs();
194
195 // Looks dumb to get a new instance but better be conservative.
196 $model = new \core_analytics\model($model->get_model_obj());
ce99f1fa
DM
197 if ($model->is_trained()) {
198 $predictresults = $model->predict();
199 $predictlogs = $model->get_analyser()->get_logs();
200 } else {
201 $predictresults = false;
202 $predictlogs = array();
203 }
efaf8534 204
e10b29ed 205 $renderer = $PAGE->get_renderer('tool_analytics');
1cc2b4ba 206 echo $renderer->render_get_predictions_results($trainresults, $trainlogs, $predictresults, $predictlogs);
efaf8534
DM
207 break;
208
209 case 'log':
210 echo $OUTPUT->header();
357507b4
DM
211
212 if ($model->is_static()) {
e10b29ed 213 throw new moodle_exception('errornostaticlog', 'tool_analytics');
357507b4
DM
214 }
215
e10b29ed
DM
216 $renderer = $PAGE->get_renderer('tool_analytics');
217 $modellogstable = new \tool_analytics\output\model_logs('model-' . $model->get_id(), $model);
efaf8534
DM
218 echo $renderer->render_table($modellogstable);
219 break;
d126f838 220
349c4412 221 case 'exportdata':
d126f838
DM
222
223 if ($model->is_static() || !$model->is_trained()) {
224 throw new moodle_exception('errornoexport', 'tool_analytics');
225 }
226
227 $file = $model->get_training_data();
228 if (!$file) {
e4453adc 229 redirect($returnurl, get_string('errortrainingdataexport', 'tool_analytics'),
d126f838
DM
230 null, \core\output\notification::NOTIFY_ERROR);
231 }
232
233 $filename = 'training-data.' . $model->get_id() . '.' . time() . '.csv';
234 send_file($file, $filename, null, 0, false, true);
235 break;
8816022d 236
349c4412 237 case 'exportmodel':
c70a7194
DM
238 $zipfilename = 'model-' . $model->get_unique_id() . '-' . microtime(false) . '.zip';
239 $zipfilepath = $model->export_model($zipfilename);
240 send_temp_file($zipfilepath, $zipfilename);
349c4412
AA
241 break;
242
8816022d
DM
243 case 'clear':
244 confirm_sesskey();
245
246 $model->clear();
e4453adc 247 redirect($returnurl);
8816022d 248 break;
019ad9c7
DM
249
250 case 'invalidanalysables':
251
252 echo $OUTPUT->header();
253
254 $page = optional_param('page', 0, PARAM_INT);
255 // No option in the UI to change this, only for url hackers ;).
256 $perpage = optional_param('perpage', 10, PARAM_INT);
257
258 $renderable = new \tool_analytics\output\invalid_analysables($model, $page, $perpage);
259 $renderer = $PAGE->get_renderer('tool_analytics');
260 echo $renderer->render($renderable);
261
262 break;
efaf8534
DM
263}
264
265echo $OUTPUT->footer();