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 |
* mod_unilabel
|
|
|
19 |
*
|
|
|
20 |
* @package mod_unilabel
|
|
|
21 |
* @author Andreas Grabs <info@grabs-edv.de>
|
|
|
22 |
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
namespace mod_unilabel\output;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Content type definition.
|
|
|
30 |
* @package mod_unilabel
|
|
|
31 |
* @author Andreas Grabs <info@grabs-edv.de>
|
|
|
32 |
* @copyright 2018 onwards Grabs EDV {@link https://www.grabs-edv.de}
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
abstract class edit_element_base implements \templatable, \renderable {
|
|
|
36 |
|
|
|
37 |
/** @var \stdClass */
|
|
|
38 |
protected $data;
|
|
|
39 |
|
|
|
40 |
/** @var \stdClass */
|
|
|
41 |
protected $course;
|
|
|
42 |
/** @var string */
|
|
|
43 |
protected $formid;
|
|
|
44 |
/** @var string */
|
|
|
45 |
protected $context;
|
|
|
46 |
/** @var string */
|
|
|
47 |
protected $type;
|
|
|
48 |
/** @var string */
|
|
|
49 |
protected $prefix;
|
|
|
50 |
/** @var string */
|
|
|
51 |
protected $component;
|
|
|
52 |
/** @var int */
|
|
|
53 |
protected $repeatindex;
|
|
|
54 |
/** @var \core_renderer */
|
|
|
55 |
protected $output;
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Constructor
|
|
|
59 |
*
|
|
|
60 |
* @param string $formid The id the edit_content form (mform) is using
|
|
|
61 |
* @param \context $context The context of the cm
|
|
|
62 |
* @param \stdClass $course
|
|
|
63 |
* @param string $type The unilabel type like "grid" or "carousel"
|
|
|
64 |
* @param int $repeatindex
|
|
|
65 |
*/
|
|
|
66 |
public function __construct(string $formid, \context $context, \stdClass $course, string $type, int $repeatindex) {
|
|
|
67 |
global $CFG, $OUTPUT;
|
|
|
68 |
|
|
|
69 |
require_once($CFG->libdir . '/formslib.php');
|
|
|
70 |
require_once($CFG->libdir . '/form/filemanager.php');
|
|
|
71 |
require_once($CFG->libdir . '/form/editor.php');
|
|
|
72 |
require_once($CFG->libdir . '/form/text.php');
|
|
|
73 |
require_once($CFG->libdir . '/form/hidden.php');
|
|
|
74 |
require_once($CFG->libdir . '/form/header.php');
|
|
|
75 |
require_once($CFG->libdir . '/form/static.php');
|
|
|
76 |
require_once($CFG->libdir . '/form/group.php');
|
|
|
77 |
require_once($CFG->libdir . '/form/select.php');
|
|
|
78 |
require_once($CFG->libdir . '/form/checkbox.php');
|
|
|
79 |
|
|
|
80 |
// Set the global properties.
|
|
|
81 |
$this->output = $OUTPUT;
|
|
|
82 |
$this->formid = $formid;
|
|
|
83 |
$this->context = $context;
|
|
|
84 |
$this->course = $course;
|
|
|
85 |
$this->type = $type;
|
|
|
86 |
$this->component = 'unilabeltype_' . $type;
|
|
|
87 |
$this->prefix = $this->component . '_';
|
|
|
88 |
$this->repeatindex = $repeatindex;
|
|
|
89 |
|
|
|
90 |
// Set the common values for the output array.
|
|
|
91 |
$this->data = new \stdClass();
|
|
|
92 |
$this->data->formid = $this->formid;
|
|
|
93 |
$this->data->type = $this->type;
|
|
|
94 |
$this->data->repeatindex = $this->repeatindex;
|
|
|
95 |
$this->data->prefix = $this->prefix;
|
|
|
96 |
$this->data->repeatnr = $this->repeatindex + 1;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Get the name of the elements group.
|
|
|
101 |
*
|
|
|
102 |
* @return string
|
|
|
103 |
*/
|
|
|
104 |
abstract public function get_elements_name();
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Get the form elements as array in the order they should be printed out.
|
|
|
108 |
*
|
|
|
109 |
* @return \HTML_QuickForm_element[]
|
|
|
110 |
*/
|
|
|
111 |
abstract public function get_elements();
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Add a sortorder element to the form fragment.
|
|
|
115 |
*
|
|
|
116 |
* @return void
|
|
|
117 |
*/
|
|
|
118 |
protected function add_sortorder() {
|
|
|
119 |
$this->data->sortorderelement = $this->render_element(
|
|
|
120 |
$this->get_hidden('sortorder')
|
|
|
121 |
);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Export for template.
|
|
|
126 |
*
|
|
|
127 |
* @param renderer_base $output The renderer.
|
|
|
128 |
* @return stdClass
|
|
|
129 |
*/
|
|
|
130 |
public function export_for_template(\renderer_base $output) {
|
|
|
131 |
$elements = $this->get_elements();
|
|
|
132 |
$this->data->elements = [];
|
|
|
133 |
foreach ($elements as $element) {
|
|
|
134 |
$this->data->elements[] = $this->render_element($element);
|
|
|
135 |
}
|
|
|
136 |
$this->data->elementsname = $this->get_elements_name();
|
|
|
137 |
return $this->data;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Get the rendered html from the given QuickForm element.
|
|
|
142 |
*
|
|
|
143 |
* @param \HTML_QuickForm_element $element
|
|
|
144 |
* @return void
|
|
|
145 |
*/
|
|
|
146 |
protected function render_element(\HTML_QuickForm_element $element) {
|
|
|
147 |
if ($element->getType() == 'hidden') {
|
|
|
148 |
return $element->toHtml();
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
return $this->output->mform_element(
|
|
|
152 |
$element,
|
|
|
153 |
false,
|
|
|
154 |
false,
|
|
|
155 |
'',
|
|
|
156 |
false
|
|
|
157 |
);
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
/**
|
|
|
161 |
* Get an mform filemanager element.
|
|
|
162 |
*
|
|
|
163 |
* @param string $name The element name without the prefix.
|
|
|
164 |
* @param array $attributes
|
|
|
165 |
* @param array $options The options for file handling
|
|
|
166 |
* @param string $helpbutton
|
|
|
167 |
* @param string $extralabel In from the name independent label.
|
|
|
168 |
* @return \MoodleQuickForm_filemanager The element
|
|
|
169 |
*/
|
|
|
170 |
protected function get_filemanager(string $name, array $attributes = [],
|
|
|
171 |
array $options = [], $helpbutton = '', string $extralabel = '') {
|
|
|
172 |
|
|
|
173 |
$elementname = $this->prefix . $name . '[' . $this->repeatindex . ']';
|
|
|
174 |
$attributes['id'] = 'id_' . $this->prefix . $name . '_' . $this->repeatindex;
|
|
|
175 |
$attributes['name'] = $elementname;
|
|
|
176 |
|
|
|
177 |
if (empty($extralabel)) {
|
|
|
178 |
$label = get_string($name, $this->component) . '-' . ($this->repeatindex + 1);
|
|
|
179 |
} else {
|
|
|
180 |
$label = $extralabel;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
$element = new \MoodleQuickForm_filemanager($elementname, $label, $attributes, $options);
|
|
|
184 |
if ($helpbutton) {
|
|
|
185 |
$element->_helpbutton = $this->output->help_icon($helpbutton, $this->component);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
return $element;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Get an mform editor element.
|
|
|
193 |
*
|
|
|
194 |
* @param string $name The element name without the prefix.
|
|
|
195 |
* @param array $attributes
|
|
|
196 |
* @param array $options The options for file handling
|
|
|
197 |
* @param boolean $helpbutton
|
|
|
198 |
* @param string $extralabel In from the name independent label.
|
|
|
199 |
* @return \MoodleQuickForm_editor The element
|
|
|
200 |
*/
|
|
|
201 |
protected function get_editor(string $name, array $attributes = [],
|
|
|
202 |
array $options = [], $helpbutton = '', string $extralabel = '') {
|
|
|
203 |
|
|
|
204 |
$elementname = $this->prefix . $name . '[' . $this->repeatindex . ']';
|
|
|
205 |
$attributes['id'] = 'id_' . $this->prefix . $name . '_' . $this->repeatindex;
|
|
|
206 |
$attributes['name'] = $elementname;
|
|
|
207 |
|
|
|
208 |
if (empty($extralabel)) {
|
|
|
209 |
$label = get_string($name, $this->component) . '-' . ($this->repeatindex + 1);
|
|
|
210 |
} else {
|
|
|
211 |
$label = $extralabel;
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
$element = new \MoodleQuickForm_editor($elementname, $label, $attributes, $options);
|
|
|
215 |
if ($helpbutton) {
|
|
|
216 |
$element->_helpbutton = $this->output->help_icon($helpbutton, $this->component);
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
return $element;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Get an mform text element.
|
|
|
224 |
*
|
|
|
225 |
* @param string $name The element name without the prefix.
|
|
|
226 |
* @param array $attributes
|
|
|
227 |
* @param boolean $helpbutton
|
|
|
228 |
* @param string $extralabel In from the name independent label.
|
|
|
229 |
* @return \MoodleQuickForm_text The element
|
|
|
230 |
*/
|
|
|
231 |
protected function get_textfield(string $name, array $attributes = [], $helpbutton = '', string $extralabel = '') {
|
|
|
232 |
$elementname = $this->prefix . $name . '[' . $this->repeatindex . ']';
|
|
|
233 |
$attributes['id'] = 'id_' . $this->prefix . $name . '_' . $this->repeatindex;
|
|
|
234 |
$attributes['name'] = $elementname;
|
|
|
235 |
|
|
|
236 |
if (empty($extralabel)) {
|
|
|
237 |
$label = get_string($name, $this->component) . '-' . ($this->repeatindex + 1);
|
|
|
238 |
} else {
|
|
|
239 |
$label = $extralabel;
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
$element = new \MoodleQuickForm_text($elementname, $label, $attributes);
|
|
|
243 |
if ($helpbutton) {
|
|
|
244 |
$element->_helpbutton = $this->output->help_icon($helpbutton, $this->component);
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
return $element;
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
/**
|
|
|
251 |
* Get an mform checkbox element.
|
|
|
252 |
*
|
|
|
253 |
* @param string $name The element name without the prefix.
|
|
|
254 |
* @param array $attributes
|
|
|
255 |
* @param boolean $helpbutton
|
|
|
256 |
* @param string $extralabel In from the name independent label.
|
|
|
257 |
* @return \MoodleQuickForm_text The element
|
|
|
258 |
*/
|
|
|
259 |
protected function get_checkbox(string $name, array $attributes = [], $helpbutton = '', string $extralabel = '') {
|
|
|
260 |
$elementname = $this->prefix . $name . '[' . $this->repeatindex . ']';
|
|
|
261 |
$attributes['id'] = 'id_' . $this->prefix . $name . '_' . $this->repeatindex;
|
|
|
262 |
$attributes['name'] = $elementname;
|
|
|
263 |
|
|
|
264 |
if (empty($extralabel)) {
|
|
|
265 |
$label = get_string($name, $this->component) . '-' . ($this->repeatindex + 1);
|
|
|
266 |
} else {
|
|
|
267 |
$label = $extralabel;
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
$element = new \MoodleQuickForm_checkbox($elementname, $label, '', $attributes);
|
|
|
271 |
if ($helpbutton) {
|
|
|
272 |
$element->_helpbutton = $this->output->help_icon($helpbutton, $this->component);
|
|
|
273 |
}
|
|
|
274 |
|
|
|
275 |
return $element;
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
/**
|
|
|
279 |
* Get an mform select element.
|
|
|
280 |
*
|
|
|
281 |
* @param string $name The element name without the prefix.
|
|
|
282 |
* @param array $options
|
|
|
283 |
* @param array $attributes
|
|
|
284 |
* @param boolean $helpbutton
|
|
|
285 |
* @param string $extralabel In from the name independent label.
|
|
|
286 |
* @return \MoodleQuickForm_text The element
|
|
|
287 |
*/
|
|
|
288 |
protected function get_select(string $name, array $options, array $attributes = [], $helpbutton = '', string $extralabel = '') {
|
|
|
289 |
$elementname = $this->prefix . $name . '[' . $this->repeatindex . ']';
|
|
|
290 |
$attributes['id'] = 'id_' . $this->prefix . $name . '_' . $this->repeatindex;
|
|
|
291 |
$attributes['name'] = $elementname;
|
|
|
292 |
|
|
|
293 |
if (empty($extralabel)) {
|
|
|
294 |
$label = get_string($name, $this->component) . '-' . ($this->repeatindex + 1);
|
|
|
295 |
} else {
|
|
|
296 |
$label = $extralabel;
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
$element = new \MoodleQuickForm_select($elementname, $label, $options, $attributes);
|
|
|
300 |
if ($helpbutton) {
|
|
|
301 |
$element->_helpbutton = $this->output->help_icon($helpbutton, $this->component);
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
return $element;
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
/**
|
|
|
308 |
* Get an mform hidden element.
|
|
|
309 |
*
|
|
|
310 |
* @param string $name The element name without the prefix.
|
|
|
311 |
* @return \MoodleQuickForm_hidden The element
|
|
|
312 |
*/
|
|
|
313 |
protected function get_hidden(string $name) {
|
|
|
314 |
$elementname = $this->prefix . $name . '[' . $this->repeatindex . ']';
|
|
|
315 |
$attributes = [];
|
|
|
316 |
$attributes['name'] = $elementname;
|
|
|
317 |
|
|
|
318 |
$element = new \MoodleQuickForm_hidden($elementname, $this->repeatindex, $attributes);
|
|
|
319 |
|
|
|
320 |
return $element;
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
/**
|
|
|
324 |
* Get an mform static element.
|
|
|
325 |
*
|
|
|
326 |
* @param string $name The element name without the prefix.
|
|
|
327 |
* @param string $html
|
|
|
328 |
* @return \MoodleQuickForm_static The element
|
|
|
329 |
*/
|
|
|
330 |
protected function get_static(string $name, string $html) {
|
|
|
331 |
$elementname = $this->prefix . $name . '_' . $this->repeatindex;
|
|
|
332 |
$attributes = [];
|
|
|
333 |
$attributes['id'] = 'id_' . $this->prefix . $name . '_' . $this->repeatindex;
|
|
|
334 |
$attributes['name'] = $elementname;
|
|
|
335 |
|
|
|
336 |
$element = new \MoodleQuickForm_static($elementname, '', $html);
|
|
|
337 |
$element->setAttributes($attributes);
|
|
|
338 |
|
|
|
339 |
return $element;
|
|
|
340 |
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
/**
|
|
|
344 |
* Get an mform group element.
|
|
|
345 |
*
|
|
|
346 |
* @param string $name The element name without the prefix.
|
|
|
347 |
* @param \HTML_QuickForm_element[] $elements
|
|
|
348 |
* @param string $separator String to seperate elements
|
|
|
349 |
* @param bool $appendname
|
|
|
350 |
* @param string $helpbutton
|
|
|
351 |
* @param string $extralabel In from the name independent label.
|
|
|
352 |
* @return \MoodleQuickForm_group The group element
|
|
|
353 |
*/
|
|
|
354 |
protected function get_group(string $name, array $elements, string $separator = null,
|
|
|
355 |
bool $appendname = false, $helpbutton = '', string $extralabel = '') {
|
|
|
356 |
|
|
|
357 |
$elementname = $this->prefix . $name . '_' . $this->repeatindex;
|
|
|
358 |
$attributes = [];
|
|
|
359 |
$attributes['id'] = 'id_' . $this->prefix . $name . '_' . $this->repeatindex;
|
|
|
360 |
$attributes['name'] = $elementname;
|
|
|
361 |
|
|
|
362 |
if (empty($extralabel)) {
|
|
|
363 |
$label = get_string($name, $this->component) . '-' . ($this->repeatindex + 1);
|
|
|
364 |
} else {
|
|
|
365 |
$label = $extralabel;
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
$element = new \MoodleQuickForm_group($elementname, $label, $elements, $separator, $appendname);
|
|
|
369 |
if ($helpbutton) {
|
|
|
370 |
$element->_helpbutton = $this->output->help_icon($helpbutton, $this->component);
|
|
|
371 |
}
|
|
|
372 |
$element->setAttributes($attributes); // The group element needs at least this attributes!!!
|
|
|
373 |
|
|
|
374 |
return $element;
|
|
|
375 |
}
|
|
|
376 |
}
|