731d82921ba409ca9d01da82828d085c428dea3a
[moodle.git] / admin / tool / analytics / classes / output / form / edit_model.php
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/>.
17 /**
18  * Model edit form.
19  *
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
23  */
25 namespace tool_analytics\output\form;
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot.'/lib/formslib.php');
31 /**
32  * Model edit form.
33  *
34  * @package   tool_analytics
35  * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
36  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37  */
38 class edit_model extends \moodleform {
40     /**
41      * Form definition
42      */
43     public function definition() {
44         global $OUTPUT;
46         $mform = $this->_form;
48         if ($this->_customdata['trainedmodel'] && $this->_customdata['staticmodel'] === false) {
49             $message = get_string('edittrainedwarning', 'tool_analytics');
50             $mform->addElement('html', $OUTPUT->notification($message, \core\output\notification::NOTIFY_WARNING));
51         }
53         $mform->addElement('advcheckbox', 'enabled', get_string('enabled', 'tool_analytics'));
55         // Target.
56         if (!empty($this->_customdata['targets'])) {
57             $targets = array('' => '');
58             foreach ($this->_customdata['targets'] as $classname => $target) {
59                 $optionname = \tool_analytics\output\helper::class_to_option($classname);
60                 $targets[$optionname] = $target->get_name();
61             }
63             $mform->addElement('select', 'target', get_string('target', 'tool_analytics'), $targets);
64             $mform->addHelpButton('target', 'target', 'tool_analytics');
65             $mform->addRule('target', get_string('required'), 'required', null, 'client');
66         }
68         if (!empty($this->_customdata['targetname']) && !empty($this->_customdata['targetclass'])) {
69             $mform->addElement('static', 'targetname', get_string('target', 'tool_analytics'), $this->_customdata['targetname']);
70             $mform->addElement('hidden', 'target',
71                 \tool_analytics\output\helper::class_to_option($this->_customdata['targetclass']));
72             // We won't update the model's target so no worries about its format (we can't use PARAM_ALPHANUMEXT
73             // because of class_to_option).
74             $mform->setType('target', PARAM_TEXT);
75         }
77         // Indicators.
78         if (!$this->_customdata['staticmodel']) {
79             $indicators = array();
80             foreach ($this->_customdata['indicators'] as $classname => $indicator) {
81                 $optionname = \tool_analytics\output\helper::class_to_option($classname);
82                 $indicators[$optionname] = $indicator->get_name();
83             }
84             $options = array(
85                 'multiple' => true
86             );
87             $mform->addElement('autocomplete', 'indicators', get_string('indicators', 'tool_analytics'), $indicators, $options);
88             $mform->setType('indicators', PARAM_ALPHANUMEXT);
89             $mform->addHelpButton('indicators', 'indicators', 'tool_analytics');
90         }
92         // Time-splitting methods.
93         if (!empty($this->_customdata['invalidcurrenttimesplitting'])) {
94             $mform->addElement('html', $OUTPUT->notification(
95                 get_string('invalidcurrenttimesplitting', 'tool_analytics'),
96                 \core\output\notification::NOTIFY_WARNING)
97             );
98         }
100         $timesplittings = array('' => '');
101         foreach ($this->_customdata['timesplittings'] as $classname => $timesplitting) {
102             $optionname = \tool_analytics\output\helper::class_to_option($classname);
103             $timesplittings[$optionname] = $timesplitting->get_name();
104         }
105         $mform->addElement('select', 'timesplitting', get_string('timesplittingmethod', 'analytics'), $timesplittings);
106         $mform->addHelpButton('timesplitting', 'timesplittingmethod', 'analytics');
108         // Contexts restriction.
109         if (!empty($this->_customdata['contexts'])) {
111             \core_collator::asort($this->_customdata['contexts']);
112             $options = ['multiple' => true, 'noselectionstring' => get_string('all')];
113             $mform->addElement('autocomplete', 'contexts', get_string('contexts', 'tool_analytics'), $this->_customdata['contexts'],
114                 $options);
115             $mform->setType('contexts', PARAM_INT);
116             $mform->addHelpButton('contexts', 'contexts', 'tool_analytics');
117         }
119         // Predictions processor.
120         if (!$this->_customdata['staticmodel']) {
121             $defaultprocessor = \core_analytics\manager::get_predictions_processor_name(
122                 \core_analytics\manager::get_predictions_processor()
123             );
124             $predictionprocessors = ['' => get_string('defaultpredictoroption', 'analytics', $defaultprocessor)];
125             foreach ($this->_customdata['predictionprocessors'] as $classname => $predictionsprocessor) {
126                 if ($predictionsprocessor->is_ready() !== true) {
127                     continue;
128                 }
129                 $optionname = \tool_analytics\output\helper::class_to_option($classname);
130                 $predictionprocessors[$optionname] = \core_analytics\manager::get_predictions_processor_name($predictionsprocessor);
131             }
133             $mform->addElement('select', 'predictionsprocessor', get_string('predictionsprocessor', 'analytics'),
134                 $predictionprocessors);
135             $mform->addHelpButton('predictionsprocessor', 'predictionsprocessor', 'analytics');
136         }
138         if (!empty($this->_customdata['id'])) {
139             $mform->addElement('hidden', 'id', $this->_customdata['id']);
140             $mform->setType('id', PARAM_INT);
142             $mform->addElement('hidden', 'action', 'edit');
143             $mform->setType('action', PARAM_ALPHANUMEXT);
144         }
146         $this->add_action_buttons();
147     }
149     /**
150      * Form validation
151      *
152      * @param array $data data from the form.
153      * @param array $files files uploaded.
154      *
155      * @return array of errors.
156      */
157     public function validation($data, $files) {
158         $errors = parent::validation($data, $files);
160         $targetclass = \tool_analytics\output\helper::option_to_class($data['target']);
161         $target = \core_analytics\manager::get_target($targetclass);
163         if (!empty($data['timesplitting'])) {
164             $timesplittingclass = \tool_analytics\output\helper::option_to_class($data['timesplitting']);
165             if (\core_analytics\manager::is_valid($timesplittingclass, '\core_analytics\local\time_splitting\base') === false) {
166                 $errors['timesplitting'] = get_string('errorinvalidtimesplitting', 'analytics');
167             }
169             $timesplitting = \core_analytics\manager::get_time_splitting($timesplittingclass);
170             if (!$target->can_use_timesplitting($timesplitting)) {
171                 $errors['timesplitting'] = get_string('invalidtimesplitting', 'tool_analytics');
172             }
173         }
175         if (!empty($data['contexts'])) {
177             $analyserclass = $target->get_analyser_class();
178             if (!$potentialcontexts = $analyserclass::potential_context_restrictions()) {
179                 $errors['contexts'] = get_string('errornocontextrestrictions', 'analytics');
180             } else {
182                 // Flip the contexts array so we can just diff by key.
183                 $selectedcontexts = array_flip($data['contexts']);
184                 $invalidcontexts = array_diff_key($selectedcontexts, $potentialcontexts);
185                 if (!empty($invalidcontexts)) {
186                     $errors['contexts'] = get_string('errorinvalidcontexts', 'analytics');
187                 }
188             }
189         }
191         if (!$this->_customdata['staticmodel']) {
192             if (empty($data['indicators'])) {
193                 $errors['indicators'] = get_string('errornoindicators', 'analytics');
194             } else {
195                 foreach ($data['indicators'] as $indicator) {
196                     $realindicatorname = \tool_analytics\output\helper::option_to_class($indicator);
197                     if (\core_analytics\manager::is_valid($realindicatorname, '\core_analytics\local\indicator\base') === false) {
198                         $errors['indicators'] = get_string('errorinvalidindicator', 'analytics', $realindicatorname);
199                     }
200                 }
201             }
202         }
204         if (!empty($data['enabled']) && empty($data['timesplitting'])) {
205             $errors['enabled'] = get_string('errorcantenablenotimesplitting', 'tool_analytics');
206         }
208         return $errors;
209     }