MDL-64506 templates: replace spans where and col-x appears
[moodle.git] / admin / tool / analytics / createmodel.php
CommitLineData
e4453adc
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 * Create model form.
19 *
20 * @package tool_analytics
21 * @copyright 2019 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');
26
27require_login();
28\core_analytics\manager::check_can_manage_models();
29
30$returnurl = new \moodle_url('/admin/tool/analytics/index.php');
31$url = new \moodle_url('/admin/tool/analytics/createmodel.php');
32$title = get_string('createmodel', 'tool_analytics');
33
34\tool_analytics\output\helper::set_navbar($title, $url);
35
36// Static targets are not editable, we discard them.
37$targets = array_filter(\core_analytics\manager::get_all_targets(), function($target) {
38 return (!$target->based_on_assumptions());
39});
40
41$customdata = array(
42 'trainedmodel' => false,
43 'targets' => $targets,
44 'indicators' => \core_analytics\manager::get_all_indicators(),
45 'timesplittings' => \core_analytics\manager::get_enabled_time_splitting_methods(),
46 'predictionprocessors' => \core_analytics\manager::get_all_prediction_processors(),
47);
48$mform = new \tool_analytics\output\form\edit_model(null, $customdata);
49
50if ($mform->is_cancelled()) {
51 redirect($returnurl);
52
53} else if ($data = $mform->get_data()) {
54
55 // Converting option names to class names.
56 $targetclass = \tool_analytics\output\helper::option_to_class($data->target);
c70a7194
DM
57 if (empty($targets[$targetclass])) {
58 throw new \moodle_exception('errorinvalidtarget', 'analytics', '', $targetclass);
59 }
60 $target = $targets[$targetclass];
e4453adc
DM
61
62 $indicators = array();
63 foreach ($data->indicators as $indicator) {
64 $indicatorinstance = \core_analytics\manager::get_indicator(
65 \tool_analytics\output\helper::option_to_class($indicator)
66 );
67 $indicators[$indicatorinstance->get_id()] = $indicatorinstance;
68 }
69 $timesplitting = \tool_analytics\output\helper::option_to_class($data->timesplitting);
70 $predictionsprocessor = \tool_analytics\output\helper::option_to_class($data->predictionsprocessor);
71
72 // Insert the model into db.
73 $model = \core_analytics\model::create($target, []);
74
75 // Filter out indicators that can not be used by this target.
76 $invalidindicators = array_diff_key($indicators, $model->get_potential_indicators());
77 if ($invalidindicators) {
78 $indicators = array_diff_key($indicators, $invalidindicators);
79 }
80
81 // Update the model with the valid list of indicators.
82 $model->update($data->enabled, $indicators, $timesplitting, $predictionsprocessor);
83
84 $message = '';
85 $messagetype = \core\output\notification::NOTIFY_SUCCESS;
86 if (!empty($invalidindicators)) {
87 $message = get_string('invalidindicatorsremoved', 'tool_analytics');
88 }
89 redirect($returnurl, $message, 0, $messagetype);
90}
91
92echo $OUTPUT->header();
93$mform->display();
c70a7194 94echo $OUTPUT->footer();