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'];
49 $subscriptioncount = $this->_customdata['subscriptioncount'];
51 // General section header.
52 $mform->addElement('header', 'general', get_string('general'));
55 $mform->addElement('hidden', 'courseid');
56 $mform->setType('courseid', PARAM_INT);
58 // We are editing a existing rule.
59 if (!empty($rule->id)) {
61 $mform->addElement('hidden', 'ruleid');
62 $mform->setType('ruleid', PARAM_INT);
63 $mform->setConstant('ruleid', $rule->id);
66 $courseid = $rule->courseid;
69 // Make course id a constant.
70 $mform->setConstant('courseid', $courseid);
72 if (empty($courseid)) {
73 $context = \context_system::instance();
75 $context = \context_course::instance($courseid);
78 $editoroptions = array(
83 'context' => $context,
89 $mform->addElement('text', 'name', get_string('rulename', 'tool_monitor'), 'size="50"');
90 $mform->addRule('name', get_string('required'), 'required');
91 $mform->setType('name', PARAM_TEXT);
94 $mform->addElement('select', 'plugin', get_string('areatomonitor', 'tool_monitor'), $pluginlist);
95 $mform->addRule('plugin', get_string('required'), 'required');
98 $mform->addElement('select', 'eventname', get_string('event', 'tool_monitor'), $eventlist);
99 $mform->addRule('eventname', get_string('required'), 'required');
101 // Freeze plugin and event fields for editing if there's a subscription for this rule.
102 if ($subscriptioncount > 0) {
103 $mform->freeze('plugin');
104 $mform->setConstant('plugin', $rule->plugin);
105 $mform->freeze('eventname');
106 $mform->setConstant('eventname', $rule->eventname);
109 // Description field.
110 $mform->addElement('editor', 'description', get_string('description'), $editoroptions);
113 $freq = array(1 => 1, 5 => 5, 10 => 10, 20 => 20, 30 => 30, 40 => 40, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90,
114 100 => 100, 1000 => 1000);
115 $mform->addElement('select', 'frequency', get_string('frequency', 'tool_monitor'), $freq);
116 $mform->addRule('frequency', get_string('required'), 'required');
117 $mform->addHelpButton('frequency', 'frequency', 'tool_monitor');
119 $mins = array(1 => 1, 5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25, 30 => 30, 35 => 35, 40 => 40, 45 => 45, 50 => 50,
121 $mform->addElement('select', 'minutes', get_string('inminutes', 'tool_monitor'), $mins);
122 $mform->addRule('minutes', get_string('required'), 'required');
125 $mform->addElement('editor', 'template', get_string('messagetemplate', 'tool_monitor'), $editoroptions);
126 $mform->setDefault('template', array('text' => get_string('defaultmessagetemplate', 'tool_monitor'),
127 'format' => FORMAT_HTML));
128 $mform->addRule('template', get_string('required'), 'required');
129 $mform->addHelpButton('template', 'messagetemplate', 'tool_monitor');
132 $this->add_action_buttons(true, get_string('savechanges'));
138 * @param array $data data from the form.
139 * @param array $files files uploaded.
141 * @return array of errors.
143 public function validation($data, $files) {
144 $errors = parent::validation($data, $files);
146 if (!eventlist::validate_event_plugin($data['plugin'], $data['eventname'])) {
147 $errors['eventname'] = get_string('errorincorrectevent', 'tool_monitor');