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 * The mform for creating and editing a rule.
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
25 namespace tool_monitor;
27 require_once($CFG->dirroot.'/lib/formslib.php');
30 * The mform for creating and editing a rule.
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
37 class rule_form extends \moodleform {
40 * Mform class definition
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'));
54 $mform->addElement('hidden', 'courseid');
55 $mform->setType('courseid', PARAM_INT);
57 // We are editing a existing rule.
58 if (!empty($rule->id)) {
60 $mform->addElement('hidden', 'ruleid');
61 $mform->setType('ruleid', PARAM_INT);
62 $mform->setConstant('ruleid', $rule->id);
65 $courseid = $rule->courseid;
68 // Make course id a constant.
69 $mform->setConstant('courseid', $courseid);
71 if (empty($courseid)) {
72 $context = \context_system::instance();
74 $context = \context_course::instance($courseid);
77 $editoroptions = array(
82 'context' => $context,
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');
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');
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');
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,
117 $mform->addElement('select', 'minutes', get_string('selectminutes', 'tool_monitor'), $mins);
118 $mform->addRule('minutes', get_string('required'), 'required');
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');
128 $this->add_action_buttons(false, get_string('savechanges'));
134 * @param array $data data from the form.
135 * @param array $files files uploaded.
137 * @return array of errors.
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');