1 |
efrain |
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/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Example form to showcase the rendering of form fields.
|
|
|
19 |
*
|
|
|
20 |
* @package tool_componentlibrary
|
|
|
21 |
* @copyright 2021 Bas Brands <bas@moodle.com>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace tool_componentlibrary\local\examples\formelements;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Example form to showcase the rendering of form fields.
|
|
|
29 |
*
|
|
|
30 |
* @copyright 2021 Bas Brands <bas@moodle.com>
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
|
|
33 |
class example extends \moodleform {
|
|
|
34 |
/**
|
|
|
35 |
* Elements of the test form.
|
|
|
36 |
*/
|
|
|
37 |
public function definition() {
|
|
|
38 |
$mform = $this->_form;
|
|
|
39 |
|
|
|
40 |
$required = optional_param('required', false, PARAM_BOOL);
|
|
|
41 |
$help = optional_param('help', false, PARAM_BOOL);
|
|
|
42 |
$mixed = optional_param('mixed', false, PARAM_BOOL);
|
|
|
43 |
|
|
|
44 |
// Text.
|
|
|
45 |
$mform->addElement('text', 'textelement', 'Text');
|
|
|
46 |
$mform->setType('textelement', 'text');
|
|
|
47 |
if ($required) {
|
|
|
48 |
$mform->addRule('textelement', null, 'required', null, 'client');
|
|
|
49 |
}
|
|
|
50 |
if ($help && !$mixed) {
|
|
|
51 |
$mform->addHelpButton('textelement', 'summary');
|
|
|
52 |
}
|
|
|
53 |
$mform->setAdvanced('textelement', true);
|
|
|
54 |
|
|
|
55 |
// Text with a long label.
|
|
|
56 |
$mform->addElement('text', 'textelementtwo', 'Text element with a long label that can span multiple lines.
|
|
|
57 |
The next field has no label. ');
|
|
|
58 |
$mform->setType('textelementtwo', 'text');
|
|
|
59 |
if ($required) {
|
|
|
60 |
$mform->addRule('textelementtwo', 'This element is required', 'required', null, 'client');
|
|
|
61 |
}
|
|
|
62 |
if ($help) {
|
|
|
63 |
$mform->addHelpButton('textelementtwo', 'summary');
|
|
|
64 |
}
|
|
|
65 |
$mform->setAdvanced('textelementtwo', true);
|
|
|
66 |
|
|
|
67 |
// Text without label.
|
|
|
68 |
$mform->addElement('text', 'textelementhree', '', '');
|
|
|
69 |
$mform->setType('textelementhree', 'text');
|
|
|
70 |
if ($required && !$mixed) {
|
|
|
71 |
$mform->addRule('textelementhree', 'This element is required', 'required', null, 'client');
|
|
|
72 |
}
|
|
|
73 |
if ($help && !$mixed) {
|
|
|
74 |
$mform->addHelpButton('textelementhree', 'summary');
|
|
|
75 |
}
|
|
|
76 |
$mform->setAdvanced('textelementhree', true);
|
|
|
77 |
|
|
|
78 |
// Button.
|
|
|
79 |
$mform->addElement('button', 'buttonelement', 'Button');
|
|
|
80 |
if ($required) {
|
|
|
81 |
$mform->addRule('buttonelement', 'This element is required', 'required', null, 'client');
|
|
|
82 |
}
|
|
|
83 |
if ($help) {
|
|
|
84 |
$mform->addHelpButton('buttonelement', 'summary');
|
|
|
85 |
}
|
|
|
86 |
$mform->setAdvanced('buttonelement', true);
|
|
|
87 |
|
|
|
88 |
// Date.
|
|
|
89 |
$mform->addElement('date_selector', 'date', 'Date selector');
|
|
|
90 |
if ($required && !$mixed) {
|
|
|
91 |
$mform->addRule('date', 'This element is required', 'required', null, 'client');
|
|
|
92 |
}
|
|
|
93 |
if ($help) {
|
|
|
94 |
$mform->addHelpButton('date', 'summary');
|
|
|
95 |
}
|
|
|
96 |
$mform->setAdvanced('date', true);
|
|
|
97 |
|
|
|
98 |
// Date time.
|
|
|
99 |
$mform->addElement('date_time_selector', 'datetimesel', 'Date time selector');
|
|
|
100 |
if ($required) {
|
|
|
101 |
$mform->addRule('datetimesel', 'This element is required', 'required', null, 'client');
|
|
|
102 |
}
|
|
|
103 |
if ($help && !$mixed) {
|
|
|
104 |
$mform->addHelpButton('datetimesel', 'summary');
|
|
|
105 |
}
|
|
|
106 |
$mform->setAdvanced('datetimesel', true);
|
|
|
107 |
|
|
|
108 |
// Duration (does not support required form fields).
|
|
|
109 |
$mform->addElement('duration', 'duration', 'Duration');
|
|
|
110 |
if ($help) {
|
|
|
111 |
$mform->addHelpButton('duration', 'summary');
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
// Editor.
|
|
|
115 |
$mform->addElement('editor', 'editor', 'Editor');
|
|
|
116 |
$mform->setType('editor', PARAM_RAW);
|
|
|
117 |
if ($required) {
|
|
|
118 |
$mform->addRule('editor', 'This element is required', 'required', null, 'client');
|
|
|
119 |
}
|
|
|
120 |
if ($help && !$mixed) {
|
|
|
121 |
$mform->addHelpButton('editor', 'summary');
|
|
|
122 |
}
|
|
|
123 |
$mform->setAdvanced('editor', true);
|
|
|
124 |
|
|
|
125 |
// Filepicker.
|
|
|
126 |
$mform->addElement('filepicker', 'userfile', 'Filepicker', null, ['maxbytes' => 100, 'accepted_types' => '*']);
|
|
|
127 |
if ($required) {
|
|
|
128 |
$mform->addRule('userfile', 'This element is required', 'required', null, 'client');
|
|
|
129 |
}
|
|
|
130 |
if ($help) {
|
|
|
131 |
$mform->addHelpButton('userfile', 'summary');
|
|
|
132 |
}
|
|
|
133 |
$mform->setAdvanced('userfile', true);
|
|
|
134 |
|
|
|
135 |
// Html.
|
|
|
136 |
$mform->addElement('html', '<div class="text-success h2 ">The HTML only formfield</div>');
|
|
|
137 |
|
|
|
138 |
// Passwords.
|
|
|
139 |
$mform->addElement('passwordunmask', 'passwordunmask', 'Passwordunmask');
|
|
|
140 |
if ($required && !$mixed) {
|
|
|
141 |
$mform->addRule('passwordunmask', 'This element is required', 'required', null, 'client');
|
|
|
142 |
}
|
|
|
143 |
if ($help && !$mixed) {
|
|
|
144 |
$mform->addHelpButton('passwordunmask', 'summary');
|
|
|
145 |
}
|
|
|
146 |
$mform->setAdvanced('passwordunmask', true);
|
|
|
147 |
|
|
|
148 |
// Radio.
|
|
|
149 |
$mform->addElement('radio', 'radio', 'Radio', 'Radio label', 'choice_value');
|
|
|
150 |
if ($required) {
|
|
|
151 |
$mform->addRule('radio', 'This element is required', 'required', null, 'client');
|
|
|
152 |
}
|
|
|
153 |
if ($help && !$mixed) {
|
|
|
154 |
$mform->addHelpButton('radio', 'summary');
|
|
|
155 |
}
|
|
|
156 |
$mform->setAdvanced('radio', true);
|
|
|
157 |
|
|
|
158 |
// Checkbox.
|
|
|
159 |
$mform->addElement('checkbox', 'checkbox', 'Checkbox', 'Checkbox Text');
|
|
|
160 |
if ($required) {
|
|
|
161 |
$mform->addRule('checkbox', 'This element is required', 'required', null, 'client');
|
|
|
162 |
}
|
|
|
163 |
if ($help) {
|
|
|
164 |
$mform->addHelpButton('checkbox', 'summary');
|
|
|
165 |
}
|
|
|
166 |
$mform->setAdvanced('checkbox', true);
|
|
|
167 |
|
|
|
168 |
// Select.
|
|
|
169 |
$mform->addElement('select', 'auth', 'Select', ['cow', 'crow', 'dog', 'cat']);
|
|
|
170 |
if ($required && !$mixed) {
|
|
|
171 |
$mform->addRule('auth', 'This element is required', 'required', null, 'client');
|
|
|
172 |
}
|
|
|
173 |
if ($help) {
|
|
|
174 |
$mform->addHelpButton('auth', 'summary');
|
|
|
175 |
}
|
|
|
176 |
$mform->setAdvanced('auth', true);
|
|
|
177 |
|
|
|
178 |
// Yes No.
|
|
|
179 |
$mform->addElement('selectyesno', 'selectyesno', 'Selectyesno');
|
|
|
180 |
if ($required && !$mixed) {
|
|
|
181 |
$mform->addRule('selectyesno', 'This element is required', 'required', null, 'client');
|
|
|
182 |
}
|
|
|
183 |
if ($help) {
|
|
|
184 |
$mform->addHelpButton('selectyesno', 'summary');
|
|
|
185 |
}
|
|
|
186 |
$mform->setAdvanced('selectyesno', true);
|
|
|
187 |
|
|
|
188 |
// Static.
|
|
|
189 |
$mform->addElement('static', 'static', 'Static', 'static description');
|
|
|
190 |
|
|
|
191 |
// Float.
|
|
|
192 |
$mform->addElement('float', 'float', 'Floating number');
|
|
|
193 |
if ($required) {
|
|
|
194 |
$mform->addRule('float', 'This element is required', 'required', null, 'client');
|
|
|
195 |
}
|
|
|
196 |
if ($help) {
|
|
|
197 |
$mform->addHelpButton('float', 'summary');
|
|
|
198 |
}
|
|
|
199 |
$mform->setAdvanced('float', true);
|
|
|
200 |
|
|
|
201 |
// Textarea.
|
|
|
202 |
$mform->addElement('textarea', 'textarea', 'Text area', 'wrap="virtual" rows="20" cols="50"');
|
|
|
203 |
if ($required && !$mixed) {
|
|
|
204 |
$mform->addRule('textarea', 'This element is required', 'required', null, 'client');
|
|
|
205 |
}
|
|
|
206 |
if ($help && !$mixed) {
|
|
|
207 |
$mform->addHelpButton('textarea', 'summary');
|
|
|
208 |
}
|
|
|
209 |
$mform->setAdvanced('textarea', true);
|
|
|
210 |
|
|
|
211 |
// Recaptcha. (does not support required).
|
|
|
212 |
$mform->addElement('recaptcha', 'recaptcha', 'Recaptcha');
|
|
|
213 |
if ($help) {
|
|
|
214 |
$mform->addHelpButton('recaptcha', 'summary');
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
// Tags.
|
|
|
218 |
$mform->addElement('tags', 'tags', 'Tags', ['itemtype' => 'course_modules', 'component' => 'core']);
|
|
|
219 |
if ($required && !$mixed) {
|
|
|
220 |
$mform->addRule('tags', 'This element is required', 'required', null, 'client');
|
|
|
221 |
}
|
|
|
222 |
if ($help && !$mixed) {
|
|
|
223 |
$mform->addHelpButton('tags', 'summary');
|
|
|
224 |
}
|
|
|
225 |
$mform->setAdvanced('tags', true);
|
|
|
226 |
|
|
|
227 |
// Filetypes. (does not support required).
|
|
|
228 |
$mform->addElement('filetypes', 'filetypes', 'Allowedfiletypes', ['onlytypes' => ['document', 'image'],
|
|
|
229 |
'allowunknown' => true]);
|
|
|
230 |
if ($help) {
|
|
|
231 |
$mform->addHelpButton('filetypes', 'summary');
|
|
|
232 |
}
|
|
|
233 |
$mform->setAdvanced('filetypes', true);
|
|
|
234 |
|
|
|
235 |
// Advanced checkbox.
|
|
|
236 |
$mform->addElement('advcheckbox', 'advcheckbox', 'Advanced checkbox', 'Advanced checkbox name', ['group' => 1],
|
|
|
237 |
[0, 1]);
|
|
|
238 |
if ($required) {
|
|
|
239 |
$mform->addRule('advcheckbox', 'This element is required', 'required', null, 'client');
|
|
|
240 |
}
|
|
|
241 |
if ($help) {
|
|
|
242 |
$mform->addHelpButton('advcheckbox', 'summary');
|
|
|
243 |
}
|
|
|
244 |
$mform->setAdvanced('advcheckbox', true);
|
|
|
245 |
|
|
|
246 |
// Autocomplete.
|
|
|
247 |
$searchareas = \core_search\manager::get_search_areas_list(true);
|
|
|
248 |
$areanames = [];
|
|
|
249 |
foreach ($searchareas as $areaid => $searcharea) {
|
|
|
250 |
$areanames[$areaid] = $searcharea->get_visible_name();
|
|
|
251 |
}
|
|
|
252 |
$options = [
|
|
|
253 |
'multiple' => true,
|
|
|
254 |
'noselectionstring' => get_string('allareas', 'search'),
|
|
|
255 |
];
|
|
|
256 |
$mform->addElement('autocomplete', 'autocomplete', get_string('searcharea', 'search'), $areanames, $options);
|
|
|
257 |
if ($required) {
|
|
|
258 |
$mform->addRule('autocomplete', 'This element is required', 'required', null, 'client');
|
|
|
259 |
}
|
|
|
260 |
if ($help && !$mixed) {
|
|
|
261 |
$mform->addHelpButton('autocomplete', 'summary');
|
|
|
262 |
}
|
|
|
263 |
$mform->setAdvanced('autocomplete', true);
|
|
|
264 |
|
|
|
265 |
// Group.
|
|
|
266 |
$radiogrp = [
|
|
|
267 |
$mform->createElement('text', 'rtext', 'Text'),
|
|
|
268 |
$mform->createElement('radio', 'rradio', 'Radio label', 'After one ', 1),
|
|
|
269 |
$mform->createElement('checkbox', 'rchecbox', 'Checkbox label', 'After two ', 2)
|
|
|
270 |
];
|
|
|
271 |
$mform->setType('rtext', PARAM_RAW);
|
|
|
272 |
$mform->addGroup($radiogrp, 'group', 'Group', ' ', false);
|
|
|
273 |
if ($required) {
|
|
|
274 |
$mform->addRule('group', 'This element is required', 'required', null, 'client');
|
|
|
275 |
}
|
|
|
276 |
if ($help) {
|
|
|
277 |
$mform->addHelpButton('group', 'summary');
|
|
|
278 |
}
|
|
|
279 |
$mform->setAdvanced('group', true);
|
|
|
280 |
|
|
|
281 |
$group = $mform->getElement('group');
|
|
|
282 |
|
|
|
283 |
// Group of groups.
|
|
|
284 |
$group = [];
|
|
|
285 |
$group[] = $mform->createElement('select', 'profilefield', '', [0 => 'Username', 1 => 'Email']);
|
|
|
286 |
$elements = [];
|
|
|
287 |
$elements[] = $mform->createElement('select', 'operator', null, [0 => 'equal', 1 => 'not equal']);
|
|
|
288 |
$elements[] = $mform->createElement('text', 'value', null);
|
|
|
289 |
$elements[] = $mform->createElement('static', 'desc', 'Just a static text', 'Just a static text');
|
|
|
290 |
$mform->setType('value', PARAM_RAW);
|
|
|
291 |
$group[] = $mform->createElement('group', 'fieldvalue', '', $elements, '', false);
|
|
|
292 |
$mform->addGroup($group, 'fieldsgroup', 'Group containing another group', '', false);
|
|
|
293 |
if ($required) {
|
|
|
294 |
$mform->addRule('fieldsgroup', 'This element is required', 'required', null, 'client');
|
|
|
295 |
}
|
|
|
296 |
if ($help) {
|
|
|
297 |
$mform->addHelpButton('fieldsgroup', 'summary');
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
$this->add_action_buttons();
|
|
|
301 |
}
|
|
|
302 |
}
|