Merge branch 'MDL-72496-master_assert_tag' of https://github.com/call-learning/moodle
[moodle.git] / admin / tool / analytics / model.php
blobab9f304143a69c78b7b0d95c8761910af3bca4b4
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 * Model-related actions.
20 * @package tool_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 require_once(__DIR__ . '/../../../config.php');
26 require_once($CFG->libdir . '/filelib.php');
28 $id = required_param('id', PARAM_INT);
29 $action = required_param('action', PARAM_ALPHANUMEXT);
31 require_login();
33 $model = new \core_analytics\model($id);
34 \core_analytics\manager::check_can_manage_models();
36 if (!\core_analytics\manager::is_analytics_enabled()) {
37 $PAGE->set_context(\context_system::instance());
38 $renderer = $PAGE->get_renderer('tool_analytics');
39 echo $renderer->render_analytics_disabled();
40 exit(0);
43 $returnurl = new \moodle_url('/admin/tool/analytics/index.php');
44 $params = array('id' => $id, 'action' => $action);
45 $url = new \moodle_url('/admin/tool/analytics/model.php', $params);
47 switch ($action) {
49 case 'edit':
50 $title = get_string('editmodel', 'tool_analytics', $model->get_name());
51 break;
52 case 'evaluate':
53 $title = get_string('evaluatemodel', 'tool_analytics');
54 break;
55 case 'scheduledanalysis':
56 $title = get_string('analysis', 'tool_analytics');
57 break;
58 case 'log':
59 $title = get_string('viewlog', 'tool_analytics');
60 break;
61 case 'enable':
62 $title = get_string('enable');
63 break;
64 case 'disable':
65 $title = get_string('disable');
66 break;
67 case 'delete':
68 $title = get_string('delete');
69 break;
70 case 'exportdata':
71 $title = get_string('exporttrainingdata', 'tool_analytics');
72 break;
73 case 'exportmodel':
74 $title = get_string('exportmodel', 'tool_analytics');
75 break;
76 case 'clear':
77 $title = get_string('clearpredictions', 'tool_analytics');
78 break;
79 case 'insightsreport':
80 $title = get_string('insightsreport', 'tool_analytics');
81 break;
82 case 'invalidanalysables':
83 $title = get_string('invalidanalysables', 'tool_analytics');
84 break;
85 default:
86 throw new moodle_exception('errorunknownaction', 'analytics');
89 \tool_analytics\output\helper::set_navbar($title, $url);
91 $onlycli = get_config('analytics', 'onlycli');
92 if ($onlycli === false) {
93 // Default applied if no config found.
94 $onlycli = 1;
97 switch ($action) {
99 case 'enable':
100 confirm_sesskey();
102 $model->enable();
103 redirect($returnurl);
104 break;
106 case 'disable':
107 confirm_sesskey();
109 $model->update(0, false, false);
110 redirect($returnurl);
111 break;
113 case 'delete':
114 confirm_sesskey();
116 $model->delete();
117 redirect($returnurl);
118 break;
120 case 'edit':
121 confirm_sesskey();
123 $invalidcurrenttimesplitting = $model->invalid_timesplitting_selected();
124 $potentialtimesplittings = $model->get_potential_timesplittings();
126 $customdata = array(
127 'id' => $model->get_id(),
128 'trainedmodel' => $model->is_trained(),
129 'staticmodel' => $model->is_static(),
130 'invalidcurrenttimesplitting' => (!empty($invalidcurrenttimesplitting)),
131 'targetclass' => $model->get_target()->get_id(),
132 'targetname' => $model->get_target()->get_name(),
133 'indicators' => $model->get_potential_indicators(),
134 'timesplittings' => $potentialtimesplittings,
135 'predictionprocessors' => \core_analytics\manager::get_all_prediction_processors(),
136 'supportscontexts' => ($model->get_analyser(['notimesplitting' => true]))::context_restriction_support(),
137 'contexts' => $model->get_contexts(),
139 $mform = new \tool_analytics\output\form\edit_model(null, $customdata);
141 if ($mform->is_cancelled()) {
142 redirect($returnurl);
144 } else if ($data = $mform->get_data()) {
146 $timesplitting = \tool_analytics\output\helper::option_to_class($data->timesplitting);
148 if (!$model->is_static()) {
149 // Converting option names to class names.
150 $indicators = array();
151 foreach ($data->indicators as $indicator) {
152 $indicatorclass = \tool_analytics\output\helper::option_to_class($indicator);
153 $indicators[] = \core_analytics\manager::get_indicator($indicatorclass);
155 $predictionsprocessor = \tool_analytics\output\helper::option_to_class($data->predictionsprocessor);
156 } else {
157 // These fields can not be modified.
158 $indicators = false;
159 $predictionsprocessor = false;
162 if (!isset($data->contexts)) {
163 $data->contexts = null;
166 $model->update($data->enabled, $indicators, $timesplitting, $predictionsprocessor, $data->contexts);
167 redirect($returnurl);
170 echo $OUTPUT->header();
172 $modelobj = $model->get_model_obj();
174 $callable = array('\tool_analytics\output\helper', 'class_to_option');
175 $modelobj->indicators = array_map($callable, json_decode($modelobj->indicators));
176 $modelobj->timesplitting = \tool_analytics\output\helper::class_to_option($modelobj->timesplitting);
177 if ($modelobj->contextids) {
178 $modelobj->contexts = array_map($callable, json_decode($modelobj->contextids));
180 $modelobj->predictionsprocessor = \tool_analytics\output\helper::class_to_option($modelobj->predictionsprocessor);
181 $mform->set_data($modelobj);
182 $mform->display();
183 break;
185 case 'evaluate':
186 confirm_sesskey();
188 if ($model->is_static()) {
189 throw new moodle_exception('errornostaticevaluate', 'tool_analytics');
192 if ($onlycli) {
193 throw new moodle_exception('erroronlycli', 'tool_analytics');
196 // Web interface is used by people who can not use CLI nor code stuff, always use
197 // cached stuff as they will change the model through the web interface as well
198 // which invalidates the previously analysed stuff.
199 $options = ['reuseprevanalysed' => true];
201 $mode = optional_param('mode', false, PARAM_ALPHANUM);
202 if ($mode == 'trainedmodel') {
203 $options['mode'] = 'trainedmodel';
204 } else {
206 // All is the default in core_analytics\model::evaluate() as well.
207 $timesplitting = optional_param('timesplitting', 'all', PARAM_ALPHANUMEXT);
208 if ($timesplitting === 'current') {
209 $options['timesplitting'] = \core_analytics\manager::get_time_splitting($model->get_model_obj()->timesplitting);
210 } else if ($timesplitting !== 'all') {
211 $options['timesplitting'] = \core_analytics\manager::get_time_splitting(
212 \tool_analytics\output\helper::option_to_class($timesplitting)
217 $results = $model->evaluate($options);
219 // We reset the theme and the output as some indicators may be using external functions
220 // which reset $PAGE.
221 \tool_analytics\output\helper::reset_page();
222 echo $OUTPUT->header();
224 $renderer = $PAGE->get_renderer('tool_analytics');
225 echo $renderer->render_evaluate_results($results, $model->get_analyser()->get_logs());
226 break;
228 case 'scheduledanalysis':
229 confirm_sesskey();
231 if ($onlycli) {
232 throw new moodle_exception('erroronlycli', 'tool_analytics');
235 $trainresults = $model->train();
236 $trainlogs = $model->get_analyser()->get_logs();
238 // Looks dumb to get a new instance but better be conservative.
239 $model = new \core_analytics\model($model->get_model_obj());
240 if ($model->is_trained()) {
241 $predictresults = $model->predict();
242 $predictlogs = $model->get_analyser()->get_logs();
243 } else {
244 $predictresults = false;
245 $predictlogs = array();
248 // We reset the theme and the output as some indicators may be using external functions
249 // which reset $PAGE.
250 \tool_analytics\output\helper::reset_page();
251 echo $OUTPUT->header();
253 $renderer = $PAGE->get_renderer('tool_analytics');
254 echo $renderer->render_get_predictions_results($trainresults, $trainlogs, $predictresults, $predictlogs);
255 break;
257 case 'log':
258 echo $OUTPUT->header();
260 if ($model->is_static()) {
261 throw new moodle_exception('errornostaticlog', 'tool_analytics');
264 $renderer = $PAGE->get_renderer('tool_analytics');
265 $modellogstable = new \tool_analytics\output\model_logs('model-' . $model->get_id(), $model);
266 echo $renderer->render_table($modellogstable);
267 break;
269 case 'exportdata':
271 if ($model->is_static() || !$model->is_trained()) {
272 throw new moodle_exception('errornoexport', 'tool_analytics');
275 $file = $model->get_training_data();
276 if (!$file) {
277 redirect($returnurl, get_string('errortrainingdataexport', 'tool_analytics'),
278 null, \core\output\notification::NOTIFY_ERROR);
281 $filename = 'training-data.' . $model->get_id() . '.' . time() . '.csv';
282 send_file($file, $filename, null, 0, false, true);
283 break;
285 case 'exportmodel':
287 $includeweights = optional_param('includeweights', 1, PARAM_INT);
289 $zipfilename = 'model-' . $model->get_unique_id() . '-' . microtime(false) . '.zip';
290 $zipfilepath = $model->export_model($zipfilename, $includeweights);
291 send_temp_file($zipfilepath, $zipfilename);
292 break;
294 case 'clear':
295 confirm_sesskey();
297 $model->clear();
298 redirect($returnurl);
299 break;
301 case 'insightsreport':
303 $contextid = optional_param('contextid', null, PARAM_INT);
305 echo $OUTPUT->header();
307 $renderable = new \tool_analytics\output\insights_report($model, $contextid);
308 $renderer = $PAGE->get_renderer('tool_analytics');
309 echo $renderer->render($renderable);
311 break;
313 case 'invalidanalysables':
315 echo $OUTPUT->header();
317 $page = optional_param('page', 0, PARAM_INT);
318 // No option in the UI to change this, only for url hackers ;).
319 $perpage = optional_param('perpage', 10, PARAM_INT);
321 $renderable = new \tool_analytics\output\invalid_analysables($model, $page, $perpage);
322 $renderer = $PAGE->get_renderer('tool_analytics');
323 echo $renderer->render($renderable);
325 break;
328 echo $OUTPUT->footer();