mirrored from git://git.moodle.org/moodle.git
/
rule_form.php
152 lines (128 loc) · 5.55 KB
/
rule_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* The mform for creating and editing a rule.
*
* @copyright 2014 onwards Simey Lameze <lameze@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package tool_monitor
*/
namespace tool_monitor;
require_once($CFG->dirroot.'/lib/formslib.php');
/**
* The mform for creating and editing a rule.
*
* @since Moodle 2.8
* @copyright 2014 onwards Simey Lameze <lameze@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package tool_monitor
*/
class rule_form extends \moodleform {
/**
* Mform class definition
*
*/
public function definition () {
$mform = $this->_form;
$eventlist = $this->_customdata['eventlist'];
$pluginlist = $this->_customdata['pluginlist'];
$rule = $this->_customdata['rule'];
$courseid = $this->_customdata['courseid'];
$subscriptioncount = $this->_customdata['subscriptioncount'];
// General section header.
$mform->addElement('header', 'general', get_string('general'));
// Hidden course ID.
$mform->addElement('hidden', 'courseid');
$mform->setType('courseid', PARAM_INT);
// We are editing a existing rule.
if (!empty($rule->id)) {
// Hidden rule id.
$mform->addElement('hidden', 'ruleid');
$mform->setType('ruleid', PARAM_INT);
$mform->setConstant('ruleid', $rule->id);
// Force course id.
$courseid = $rule->courseid;
}
// Make course id a constant.
$mform->setConstant('courseid', $courseid);
if (empty($courseid)) {
$context = \context_system::instance();
} else {
$context = \context_course::instance($courseid);
}
$editoroptions = array(
'subdirs' => 0,
'maxbytes' => 0,
'maxfiles' => 0,
'changeformat' => 0,
'context' => $context,
'noclean' => 0,
'trusttext' => 0
);
// Name field.
$mform->addElement('text', 'name', get_string('rulename', 'tool_monitor'), 'size="50"');
$mform->addRule('name', get_string('required'), 'required');
$mform->setType('name', PARAM_TEXT);
// Plugin field.
$mform->addElement('select', 'plugin', get_string('areatomonitor', 'tool_monitor'), $pluginlist);
$mform->addRule('plugin', get_string('required'), 'required');
// Event field.
$mform->addElement('select', 'eventname', get_string('event', 'tool_monitor'), $eventlist);
$mform->addRule('eventname', get_string('required'), 'required');
// Freeze plugin and event fields for editing if there's a subscription for this rule.
if ($subscriptioncount > 0) {
$mform->freeze('plugin');
$mform->setConstant('plugin', $rule->plugin);
$mform->freeze('eventname');
$mform->setConstant('eventname', $rule->eventname);
}
// Description field.
$mform->addElement('editor', 'description', get_string('description'), $editoroptions);
// Filters.
$freq = array(1 => 1, 5 => 5, 10 => 10, 20 => 20, 30 => 30, 40 => 40, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90,
100 => 100, 1000 => 1000);
$mform->addElement('select', 'frequency', get_string('frequency', 'tool_monitor'), $freq);
$mform->addRule('frequency', get_string('required'), 'required');
$mform->addHelpButton('frequency', 'frequency', 'tool_monitor');
$mins = array(1 => 1, 5 => 5, 10 => 10, 15 => 15, 20 => 20, 25 => 25, 30 => 30, 35 => 35, 40 => 40, 45 => 45, 50 => 50,
55 => 55, 60 => 60);
$mform->addElement('select', 'minutes', get_string('inminutes', 'tool_monitor'), $mins);
$mform->addRule('minutes', get_string('required'), 'required');
// Message template.
$mform->addElement('editor', 'template', get_string('messagetemplate', 'tool_monitor'), $editoroptions);
$mform->setDefault('template', array('text' => get_string('defaultmessagetemplate', 'tool_monitor'),
'format' => FORMAT_HTML));
$mform->addRule('template', get_string('required'), 'required');
$mform->addHelpButton('template', 'messagetemplate', 'tool_monitor');
// Action buttons.
$this->add_action_buttons(true, get_string('savechanges'));
}
/**
* Form validation
*
* @param array $data data from the form.
* @param array $files files uploaded.
*
* @return array of errors.
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (!eventlist::validate_event_plugin($data['plugin'], $data['eventname'])) {
$errors['eventname'] = get_string('errorincorrectevent', 'tool_monitor');
}
return $errors;
}
}