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 * 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
);
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();
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);
50 $title = get_string('editmodel', 'tool_analytics', $model->get_name());
53 $title = get_string('evaluatemodel', 'tool_analytics');
55 case 'scheduledanalysis':
56 $title = get_string('analysis', 'tool_analytics');
59 $title = get_string('viewlog', 'tool_analytics');
62 $title = get_string('enable');
65 $title = get_string('disable');
68 $title = get_string('delete');
71 $title = get_string('exporttrainingdata', 'tool_analytics');
74 $title = get_string('exportmodel', 'tool_analytics');
77 $title = get_string('clearpredictions', 'tool_analytics');
79 case 'insightsreport':
80 $title = get_string('insightsreport', 'tool_analytics');
82 case 'invalidanalysables':
83 $title = get_string('invalidanalysables', 'tool_analytics');
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.
103 redirect($returnurl);
109 $model->update(0, false, false);
110 redirect($returnurl);
117 redirect($returnurl);
123 $invalidcurrenttimesplitting = $model->invalid_timesplitting_selected();
124 $potentialtimesplittings = $model->get_potential_timesplittings();
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
);
157 // These fields can not be modified.
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);
188 if ($model->is_static()) {
189 throw new moodle_exception('errornostaticevaluate', 'tool_analytics');
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';
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());
228 case 'scheduledanalysis':
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();
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);
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);
271 if ($model->is_static() ||
!$model->is_trained()) {
272 throw new moodle_exception('errornoexport', 'tool_analytics');
275 $file = $model->get_training_data();
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);
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);
298 redirect($returnurl);
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);
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);
328 echo $OUTPUT->footer();