Commit | Line | Data |
---|---|---|
48cc3a8d | 1 | <?php |
58097ddf AA |
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/>. | |
16 | ||
48cc3a8d | 17 | /** |
58097ddf | 18 | * The mform for creating and editing a rule. |
48cc3a8d SL |
19 | * |
20 | * @copyright 2014 onwards Simey Lameze <lameze@gmail.com> | |
58097ddf AA |
21 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
22 | * @package tool_monitor | |
48cc3a8d | 23 | */ |
58097ddf | 24 | |
48cc3a8d SL |
25 | namespace tool_monitor; |
26 | ||
27 | require_once($CFG->dirroot.'/lib/formslib.php'); | |
28 | ||
58097ddf AA |
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 | */ | |
48cc3a8d SL |
37 | class rule_form extends \moodleform { |
38 | ||
58097ddf AA |
39 | /** |
40 | * Mform class definition | |
41 | * | |
42 | */ | |
43 | public function definition () { | |
48cc3a8d | 44 | $mform = $this->_form; |
58097ddf AA |
45 | $eventlist = $this->_customdata['eventlist']; |
46 | $pluginlist = $this->_customdata['pluginlist']; | |
47 | $rule = $this->_customdata['rule']; | |
48 | $courseid = $this->_customdata['courseid']; | |
107b55bd | 49 | $subscriptioncount = $this->_customdata['subscriptioncount']; |
48cc3a8d | 50 | |
58097ddf | 51 | // General section header. |
48cc3a8d | 52 | $mform->addElement('header', 'general', get_string('general')); |
58097ddf AA |
53 | |
54 | // Hidden course ID. | |
48cc3a8d SL |
55 | $mform->addElement('hidden', 'courseid'); |
56 | $mform->setType('courseid', PARAM_INT); | |
58097ddf AA |
57 | |
58 | // We are editing a existing rule. | |
59 | if (!empty($rule->id)) { | |
60 | // Hidden rule id. | |
61 | $mform->addElement('hidden', 'ruleid'); | |
62 | $mform->setType('ruleid', PARAM_INT); | |
63 | $mform->setConstant('ruleid', $rule->id); | |
64 | ||
65 | // Force course id. | |
66 | $courseid = $rule->courseid; | |
67 | } | |
68 | ||
69 | // Make course id a constant. | |
70 | $mform->setConstant('courseid', $courseid); | |
71 | ||
72 | if (empty($courseid)) { | |
73 | $context = \context_system::instance(); | |
74 | } else { | |
75 | $context = \context_course::instance($courseid); | |
76 | } | |
77 | ||
78 | $editoroptions = array( | |
79 | 'subdirs' => 0, | |
80 | 'maxbytes' => 0, | |
81 | 'maxfiles' => 0, | |
82 | 'changeformat' => 0, | |
83 | 'context' => $context, | |
84 | 'noclean' => 0, | |
85 | 'trusttext' => 0 | |
86 | ); | |
87 | ||
88 | // Name field. | |
7293e971 | 89 | $mform->addElement('text', 'name', get_string('rulename', 'tool_monitor'), 'size="50"'); |
48cc3a8d SL |
90 | $mform->addRule('name', get_string('required'), 'required'); |
91 | $mform->setType('name', PARAM_TEXT); | |
58097ddf AA |
92 | |
93 | // Plugin field. | |
7293e971 | 94 | $mform->addElement('select', 'plugin', get_string('areatomonitor', 'tool_monitor'), $pluginlist); |
48cc3a8d | 95 | $mform->addRule('plugin', get_string('required'), 'required'); |
58097ddf AA |
96 | |
97 | // Event field. | |
7293e971 | 98 | $mform->addElement('select', 'eventname', get_string('event', 'tool_monitor'), $eventlist); |
58097ddf | 99 | $mform->addRule('eventname', get_string('required'), 'required'); |
58097ddf | 100 | |
107b55bd SL |
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); | |
107 | } | |
108 | ||
58097ddf | 109 | // Description field. |
7293e971 | 110 | $mform->addElement('editor', 'description', get_string('description'), $editoroptions); |
58097ddf AA |
111 | |
112 | // Filters. | |
58097ddf AA |
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); | |
7293e971 | 115 | $mform->addElement('select', 'frequency', get_string('frequency', 'tool_monitor'), $freq); |
58097ddf | 116 | $mform->addRule('frequency', get_string('required'), 'required'); |
7293e971 | 117 | $mform->addHelpButton('frequency', 'frequency', 'tool_monitor'); |
58097ddf AA |
118 | |
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, | |
120 | 55 => 55, 60 => 60); | |
7293e971 | 121 | $mform->addElement('select', 'minutes', get_string('inminutes', 'tool_monitor'), $mins); |
58097ddf AA |
122 | $mform->addRule('minutes', get_string('required'), 'required'); |
123 | ||
124 | // Message template. | |
58097ddf | 125 | $mform->addElement('editor', 'template', get_string('messagetemplate', 'tool_monitor'), $editoroptions); |
0194d87e SL |
126 | $mform->setDefault('template', array('text' => get_string('defaultmessagetemplate', 'tool_monitor'), |
127 | 'format' => FORMAT_HTML)); | |
58097ddf AA |
128 | $mform->addRule('template', get_string('required'), 'required'); |
129 | $mform->addHelpButton('template', 'messagetemplate', 'tool_monitor'); | |
130 | ||
131 | // Action buttons. | |
7293e971 | 132 | $this->add_action_buttons(true, get_string('savechanges')); |
48cc3a8d SL |
133 | } |
134 | ||
135 | /** | |
136 | * Form validation | |
137 | * | |
138 | * @param array $data data from the form. | |
139 | * @param array $files files uploaded. | |
58097ddf | 140 | * |
48cc3a8d SL |
141 | * @return array of errors. |
142 | */ | |
58097ddf AA |
143 | public function validation($data, $files) { |
144 | $errors = parent::validation($data, $files); | |
145 | ||
146 | if (!eventlist::validate_event_plugin($data['plugin'], $data['eventname'])) { | |
147 | $errors['eventname'] = get_string('errorincorrectevent', 'tool_monitor'); | |
148 | } | |
48cc3a8d | 149 | |
58097ddf | 150 | return $errors; |
48cc3a8d | 151 | } |
58097ddf | 152 | } |