Merge branch 'MDL-81073' of https://github.com/paulholden/moodle
[moodle.git] / lib / form / group.php
blobaa5132b648ca9758437a695906e55ff2daf3abfc
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.
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 /**
19 * Form element group
21 * Contains HTML class for group form element
23 * @package core_form
24 * @copyright 2007 Jamie Pratt <me@jamiep.org>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once("HTML/QuickForm/group.php");
29 require_once('templatable_form_element.php');
31 /**
32 * HTML class for a form element group
34 * Overloaded {@link HTML_QuickForm_group} with default behavior modified for Moodle.
36 * @package core_form
37 * @category form
38 * @copyright 2007 Jamie Pratt <me@jamiep.org>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class MoodleQuickForm_group extends HTML_QuickForm_group implements templatable {
42 use templatable_form_element {
43 export_for_template as export_for_template_base;
46 /** @var string html for help button, if empty then no help */
47 var $_helpbutton='';
49 /** @var bool if true label will be hidden. */
50 protected $_hiddenLabel = false;
52 /** @var MoodleQuickForm */
53 protected $_mform = null;
55 protected $_renderedfromtemplate = false;
57 /**
58 * constructor
60 * @param string $elementName (optional) name of the group
61 * @param string $elementLabel (optional) group label
62 * @param array $elements (optional) array of HTML_QuickForm_element elements to group
63 * @param string $separator (optional) string to seperate elements.
64 * @param string $appendName (optional) string to appened to grouped elements.
65 * @param mixed $attributes (optional) Either a typical HTML attribute string
66 * or an associative array
68 public function __construct(
69 $elementName = null,
70 $elementLabel = null,
71 $elements = null,
72 $separator = null,
73 $appendName = true,
74 $attributes = null
75 ) {
76 parent::__construct($elementName, $elementLabel, $elements, $separator, $appendName, $attributes);
79 /**
80 * Old syntax of class constructor. Deprecated in PHP7.
82 * @deprecated since Moodle 3.1
84 public function MoodleQuickForm_group($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true) {
85 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
86 self::__construct($elementName, $elementLabel, $elements, $separator, $appendName);
89 /** @var string template type, would cause problems with client side validation so will leave for now */
90 //var $_elementTemplateType='fieldset';
92 /**
93 * set html for help button
95 function getHelpButton(){
96 return $this->_helpbutton;
99 /**
100 * Returns element template, nodisplay/static/fieldset
102 * @return string
104 function getElementTemplateType(){
105 if ($this->_flagFrozen){
106 if ($this->getGroupType() == 'submit'){
107 return 'nodisplay';
108 } else {
109 return 'static';
111 } else {
112 if ($this->getGroupType() == 'submit') {
113 return 'actionbuttons';
115 return 'fieldset';
120 * Sets label to be hidden
122 * @param bool $hiddenLabel sets if label should be hidden
124 public function setHiddenLabel($hiddenLabel) {
125 $this->_hiddenLabel = $hiddenLabel;
129 * Sets the grouped elements and hides label
131 * @param array $elements
133 function setElements($elements){
134 parent::setElements($elements);
135 foreach ($this->_elements as $element){
136 if (method_exists($element, 'setHiddenLabel')){
137 $element->setHiddenLabel(true);
143 * Stores the form this element was added to
144 * This object is later used by {@link MoodleQuickForm_group::createElement()}
145 * @param null|MoodleQuickForm $mform
147 public function setMoodleForm($mform) {
148 if ($mform && $mform instanceof MoodleQuickForm) {
149 $this->_mform = $mform;
154 * Called by HTML_QuickForm whenever form event is made on this element
156 * If this function is overridden and parent is not called the element must be responsible for
157 * storing the MoodleQuickForm object, see {@link MoodleQuickForm_group::setMoodleForm()}
159 * @param string $event Name of event
160 * @param mixed $arg event arguments
161 * @param mixed $caller calling object
163 public function onQuickFormEvent($event, $arg, &$caller) {
164 $this->setMoodleForm($caller);
165 return parent::onQuickFormEvent($event, $arg, $caller);
169 * Creates an element to add to the group
170 * Expects the same arguments as MoodleQuickForm::createElement()
172 public function createFormElement() {
173 if (!$this->_mform) {
174 throw new coding_exception('You can not call createFormElement() on the group element that was not yet added to a form.');
176 return call_user_func_array([$this->_mform, 'createElement'], func_get_args());
180 * Return attributes suitable for passing to {@see createFormElement}, comprised of all group attributes without ID in
181 * order to ensure uniqueness of that value within the group
183 * @return array
185 public function getAttributesForFormElement(): array {
186 return array_diff_key((array) $this->getAttributes(), array_flip(['id']));
189 public function export_for_template(renderer_base $output) {
190 global $OUTPUT;
192 $context = $this->export_for_template_base($output);
194 $this->_renderedfromtemplate = true;
196 include_once('HTML/QuickForm/Renderer/Default.php');
198 $elements = [];
199 $name = $this->getName();
200 $i = 0;
201 foreach ($this->_elements as $key => $element) {
202 $elementname = '';
203 if ($this->_appendName) {
204 $elementname = $element->getName();
205 if (isset($elementname)) {
206 $element->setName($name . '['. (strlen($elementname) ? $elementname : $key) .']');
207 } else {
208 $element->setName($name);
211 $element->_generateId();
213 $out = $OUTPUT->mform_element($element, false, false, '', true);
215 if (empty($out)) {
216 $renderer = new HTML_QuickForm_Renderer_Default();
217 $renderer->setElementTemplate('{element}');
218 $element->accept($renderer);
219 $out = $renderer->toHtml();
222 // Replicates the separator logic from 'pear/HTML/QuickForm/Renderer/Default.php'.
223 $separator = '';
224 if ($i > 0) {
225 if (is_array($this->_separator)) {
226 $separator = $this->_separator[($i - 1) % count($this->_separator)];
227 } else if ($this->_separator === null) {
228 $separator = '&nbsp;';
229 } else {
230 $separator = (string) $this->_separator;
234 $elements[] = [
235 'separator' => $separator,
236 'html' => $out
239 // Restore the element's name.
240 if ($this->_appendName) {
241 $element->setName($elementname);
244 $i++;
247 $context['groupname'] = $name;
248 $context['elements'] = $elements;
249 return $context;
253 * Accepts a renderer
255 * @param object An HTML_QuickForm_Renderer object
256 * @param bool Whether a group is required
257 * @param string An error message associated with a group
258 * @access public
259 * @return void
261 public function accept(&$renderer, $required = false, $error = null) {
262 $this->_createElementsIfNotExist();
263 $renderer->startGroup($this, $required, $error);
264 if (!$this->_renderedfromtemplate) {
265 // Backwards compatible path - only do this if we didn't render the sub-elements already.
266 $name = $this->getName();
267 foreach (array_keys($this->_elements) as $key) {
268 $element =& $this->_elements[$key];
269 $elementname = '';
270 if ($this->_appendName) {
271 $elementname = $element->getName();
272 if (isset($elementname)) {
273 $element->setName($name . '['. (strlen($elementname) ? $elementname : $key) .']');
274 } else {
275 $element->setName($name);
279 $required = !$element->isFrozen() && in_array($element->getName(), $this->_required);
281 $element->accept($renderer, $required);
283 // Restore the element's name.
284 if ($this->_appendName) {
285 $element->setName($elementname);
289 $renderer->finishGroup($this);
293 * Calls the validateSubmitValue function for the containing elements and returns an error string as soon as it finds one.
295 * @param array $values Values of the containing elements.
296 * @return string|null Validation error message or null.
298 public function validateSubmitValue($values) {
299 foreach ($this->_elements as $element) {
300 if (method_exists($element, 'validateSubmitValue')) {
301 $value = $values[$element->getName()] ?? null;
302 $result = $element->validateSubmitValue($value);
303 if (!empty($result) && is_string($result)) {
304 return $result;