MDL-47465 tool_monitor: Ignore used events
[moodle.git] / admin / tool / monitor / classes / rule_form.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  * The mform for creating and editing a rule.
19  *
20  * @copyright 2014 onwards Simey Lameze <lameze@gmail.com>
21  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22  * @package   tool_monitor
23  */
25 namespace tool_monitor;
27 require_once($CFG->dirroot.'/lib/formslib.php');
29 /**
30  * The mform for creating and editing a rule.
31  *
32  * @since     Moodle 2.8
33  * @copyright 2014 onwards Simey Lameze <lameze@gmail.com>
34  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  * @package   tool_monitor
36  */
37 class rule_form extends \moodleform {
39     /**
40      * Mform class definition
41      *
42      */
43     public function definition () {
44         $mform = $this->_form;
45         $eventlist = $this->_customdata['eventlist'];
46         $pluginlist = $this->_customdata['pluginlist'];
47         $rule = $this->_customdata['rule'];
48         $courseid = $this->_customdata['courseid'];
50         // General section header.
51         $mform->addElement('header', 'general', get_string('general'));
53         // Hidden course ID.
54         $mform->addElement('hidden', 'courseid');
55         $mform->setType('courseid', PARAM_INT);
57         // We are editing a existing rule.
58         if (!empty($rule->id)) {
59             // Hidden rule id.
60             $mform->addElement('hidden', 'ruleid');
61             $mform->setType('ruleid', PARAM_INT);
62             $mform->setConstant('ruleid', $rule->id);
64             // Force course id.
65             $courseid = $rule->courseid;
66         }
68         // Make course id a constant.
69         $mform->setConstant('courseid', $courseid);
71         if (empty($courseid)) {
72             $context = \context_system::instance();
73         } else {
74             $context = \context_course::instance($courseid);
75         }
77         $editoroptions = array(
78             'subdirs' => 0,
79             'maxbytes' => 0,
80             'maxfiles' => 0,
81             'changeformat' => 0,
82             'context' => $context,
83             'noclean' => 0,
84             'trusttext' => 0
85         );
87         // Name field.
88         $mform->addElement('text', 'name', get_string('name', 'tool_monitor'), 'size="50"');
89         $mform->addRule('name', get_string('required'), 'required');
90         $mform->setType('name', PARAM_TEXT);
91         $mform->addHelpButton('name', 'name', 'tool_monitor');
93         // Plugin field.
94         $mform->addElement('select', 'plugin', get_string('selectplugin', 'tool_monitor'), $pluginlist);
95         $mform->addRule('plugin', get_string('required'), 'required');
96         $mform->addHelpButton('plugin', 'selectplugin', 'tool_monitor');
98         // Event field.
99         $mform->addElement('select', 'eventname', get_string('selectevent', 'tool_monitor'), $eventlist);
100         $mform->addRule('eventname', get_string('required'), 'required');
101         $mform->addHelpButton('eventname', 'selectevent', 'tool_monitor');
103         // Description field.
104         $mform->addElement('editor', 'description', get_string('description', 'tool_monitor'), $editoroptions);
105         $mform->addHelpButton('description', 'description', 'tool_monitor');
107         // Filters.
108         $mform->addElement('header', 'customisefilters', get_string('customisefilters', 'tool_monitor'));
109         $freq = array(1 => 1, 5 => 5, 10 => 10, 20 => 20, 30 => 30, 40 => 40, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90,
110                 100 => 100, 1000 => 1000);
111         $mform->addElement('select', 'frequency', get_string('selectfrequency', 'tool_monitor'), $freq);
112         $mform->addRule('frequency', get_string('required'), 'required');
113         $mform->addHelpButton('frequency', 'selectfrequency', 'tool_monitor');
115         $mins = array(1 => 1, 5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25, 30 => 30, 35 => 35, 40 => 40, 45 => 45, 50 => 50,
116                 55 => 55,  60 => 60);
117         $mform->addElement('select', 'minutes', get_string('selectminutes', 'tool_monitor'), $mins);
118         $mform->addRule('minutes', get_string('required'), 'required');
120         // Message template.
121         $mform->addElement('header', 'customisemessage', get_string('customisemessage', 'tool_monitor'));
122         $mform->addElement('editor', 'template', get_string('messagetemplate', 'tool_monitor'), $editoroptions);
123         $mform->setDefault('template', get_string('defaultmessagetpl', 'tool_monitor'));
124         $mform->addRule('template', get_string('required'), 'required');
125         $mform->addHelpButton('template', 'messagetemplate', 'tool_monitor');
127         // Action buttons.
128         $this->add_action_buttons(false, get_string('savechanges'));
129     }
131     /**
132      * Form validation
133      *
134      * @param array $data data from the form.
135      * @param array $files files uploaded.
136      *
137      * @return array of errors.
138      */
139     public function validation($data, $files) {
140         $errors = parent::validation($data, $files);
142         if (!eventlist::validate_event_plugin($data['plugin'], $data['eventname'])) {
143             $errors['eventname'] = get_string('errorincorrectevent', 'tool_monitor');
144         }
146         return $errors;
147     }