weekly release 4.5dev
[moodle.git] / admin / tool / analytics / createmodel.php
blob678eec8201b909b4e283ea9c94d3ae87b2f2e925
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 * Create model form.
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
25 require_once(__DIR__ . '/../../../config.php');
27 require_login();
28 \core_analytics\manager::check_can_manage_models();
30 if (!\core_analytics\manager::is_analytics_enabled()) {
31 $PAGE->set_context(\context_system::instance());
32 $renderer = $PAGE->get_renderer('tool_analytics');
33 echo $renderer->render_analytics_disabled();
34 exit(0);
37 $returnurl = new \moodle_url('/admin/tool/analytics/index.php');
38 $url = new \moodle_url('/admin/tool/analytics/createmodel.php');
39 $title = get_string('createmodel', 'tool_analytics');
41 \tool_analytics\output\helper::set_navbar($title, $url);
43 // Static targets are not editable, we discard them.
44 $targets = array_filter(\core_analytics\manager::get_all_targets(), function($target) {
45 return (!$target->based_on_assumptions());
46 });
48 // Set 'supportscontexts' to true as at this stage we don't know if the contexts are supported by
49 // the selected target.
50 $customdata = array(
51 'trainedmodel' => false,
52 'staticmodel' => false,
53 'targets' => $targets,
54 'indicators' => \core_analytics\manager::get_all_indicators(),
55 'timesplittings' => \core_analytics\manager::get_all_time_splittings(),
56 'predictionprocessors' => \core_analytics\manager::get_all_prediction_processors(),
57 'supportscontexts' => true,
59 $mform = new \tool_analytics\output\form\edit_model(null, $customdata);
61 if ($mform->is_cancelled()) {
62 redirect($returnurl);
64 } else if ($data = $mform->get_data()) {
66 // Converting option names to class names.
67 $targetclass = \tool_analytics\output\helper::option_to_class($data->target);
68 if (empty($targets[$targetclass])) {
69 throw new \moodle_exception('errorinvalidtarget', 'analytics', '', $targetclass);
71 $target = $targets[$targetclass];
73 $indicators = array();
74 foreach ($data->indicators as $indicator) {
75 $indicatorinstance = \core_analytics\manager::get_indicator(
76 \tool_analytics\output\helper::option_to_class($indicator)
78 $indicators[$indicatorinstance->get_id()] = $indicatorinstance;
80 $timesplitting = \tool_analytics\output\helper::option_to_class($data->timesplitting);
81 $predictionsprocessor = \tool_analytics\output\helper::option_to_class($data->predictionsprocessor);
83 // Insert the model into db.
84 $model = \core_analytics\model::create($target, []);
86 // Filter out indicators that can not be used by this target.
87 $invalidindicators = array_diff_key($indicators, $model->get_potential_indicators());
88 if ($invalidindicators) {
89 $indicators = array_diff_key($indicators, $invalidindicators);
92 // Update the model with the rest of the data provided in the form.
93 $model->update($data->enabled, $indicators, $timesplitting, $predictionsprocessor, $data->contexts);
95 $message = '';
96 $messagetype = \core\output\notification::NOTIFY_SUCCESS;
97 if (!empty($invalidindicators)) {
98 $message = get_string('invalidindicatorsremoved', 'tool_analytics');
100 redirect($returnurl, $message, 0, $messagetype);
103 echo $OUTPUT->header();
104 $mform->display();
105 echo $OUTPUT->footer();