MDL-55656 theme_noname: Include advanced and required field markup
[moodle.git] / lib / form / group.php
CommitLineData
da6f8763 1<?php
6c1fd304
RT
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/**
19 * Form element group
20 *
21 * Contains HTML class for group form element
22 *
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
26 */
27
da6f8763 28require_once("HTML/QuickForm/group.php");
344321e1 29require_once('templatable_form_element.php');
da6f8763 30
31/**
32 * HTML class for a form element group
d4fe14d3 33 *
58b7d48f 34 * Overloaded {@link HTML_QuickForm_group} with default behavior modified for Moodle.
6c1fd304
RT
35 *
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
da6f8763 40 */
91bda4cd 41class MoodleQuickForm_group extends HTML_QuickForm_group implements templatable {
344321e1
DW
42 use templatable_form_element {
43 export_for_template as export_for_template_base;
44 }
45
6c1fd304
RT
46 /** @var string html for help button, if empty then no help */
47 var $_helpbutton='';
48
721e2def
MG
49 /** @var MoodleQuickForm */
50 protected $_mform = null;
51
91bda4cd
DW
52 var $_renderedfromtemplate = false;
53
da6f8763 54 /**
6c1fd304 55 * constructor
da6f8763 56 *
6c1fd304
RT
57 * @param string $elementName (optional) name of the group
58 * @param string $elementLabel (optional) group label
59 * @param array $elements (optional) array of HTML_QuickForm_element elements to group
60 * @param string $separator (optional) string to seperate elements.
61 * @param string $appendName (optional) string to appened to grouped elements.
da6f8763 62 */
1a0df553
MG
63 public function __construct($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true) {
64 parent::__construct($elementName, $elementLabel, $elements, $separator, $appendName);
65 }
66
67 /**
68 * Old syntax of class constructor. Deprecated in PHP7.
69 *
70 * @deprecated since Moodle 3.1
71 */
72 public function MoodleQuickForm_group($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true) {
73 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
74 self::__construct($elementName, $elementLabel, $elements, $separator, $appendName);
3e8b394e 75 }
6c1fd304
RT
76
77 /** @var string template type, would cause problems with client side validation so will leave for now */
ef625b6b 78 //var $_elementTemplateType='fieldset';
6c1fd304 79
da6f8763 80 /**
d4fe14d3 81 * set html for help button
da6f8763 82 */
83 function getHelpButton(){
84 return $this->_helpbutton;
85 }
6c1fd304
RT
86
87 /**
88 * Returns element template, nodisplay/static/fieldset
89 *
90 * @return string
91 */
ef625b6b 92 function getElementTemplateType(){
64360ee7 93 if ($this->_flagFrozen){
94 if ($this->getGroupType() == 'submit'){
95 return 'nodisplay';
96 } else {
97 return 'static';
98 }
99 } else {
95114b18
FM
100 if ($this->getGroupType() == 'submit') {
101 return 'actionbuttons';
102 }
64360ee7 103 return 'fieldset';
104 }
ef625b6b 105 }
64360ee7 106
6c1fd304
RT
107 /**
108 * Sets the grouped elements and hides label
109 *
110 * @param array $elements
111 */
44875d78 112 function setElements($elements){
113 parent::setElements($elements);
114 foreach ($this->_elements as $element){
115 if (method_exists($element, 'setHiddenLabel')){
116 $element->setHiddenLabel(true);
117 }
118 }
119 }
721e2def
MG
120
121 /**
122 * Stores the form this element was added to
123 * This object is later used by {@link MoodleQuickForm_group::createElement()}
124 * @param null|MoodleQuickForm $mform
125 */
126 public function setMoodleForm($mform) {
127 if ($mform && $mform instanceof MoodleQuickForm) {
128 $this->_mform = $mform;
129 }
130 }
131
132 /**
133 * Called by HTML_QuickForm whenever form event is made on this element
134 *
135 * If this function is overridden and parent is not called the element must be responsible for
136 * storing the MoodleQuickForm object, see {@link MoodleQuickForm_group::setMoodleForm()}
137 *
138 * @param string $event Name of event
139 * @param mixed $arg event arguments
140 * @param mixed $caller calling object
141 */
142 public function onQuickFormEvent($event, $arg, &$caller) {
143 $this->setMoodleForm($caller);
144 return parent::onQuickFormEvent($event, $arg, $caller);
145 }
146
147 /**
148 * Creates an element to add to the group
149 * Expects the same arguments as MoodleQuickForm::createElement()
150 */
151 public function createFormElement() {
152 if (!$this->_mform) {
153 throw new coding_exception('You can not call createFormElement() on the group element that was not yet added to a form.');
154 }
155 return call_user_func_array([$this->_mform, 'createElement'], func_get_args());
156 }
91bda4cd
DW
157
158 public function export_for_template(renderer_base $output) {
159 global $OUTPUT;
344321e1
DW
160
161 $context = $this->export_for_template_base($output);
162
91bda4cd
DW
163 $this->_renderedfromtemplate = true;
164
165 include_once('HTML/QuickForm/Renderer/Default.php');
166
91bda4cd
DW
167 $elements = [];
168 foreach ($this->_elements as $key => $element) {
169 $element->_generateId();
170 $name = $this->getName();
171 $elementname = '';
172 if ($this->_appendName) {
173 $elementname = $element->getName();
174 if (isset($elementname)) {
175 $element->setName($name . '['. (strlen($elementname) ? $elementname : $key) .']');
176 } else {
177 $element->setName($name);
178 }
179 }
180
d6f2ce14 181 $out = $OUTPUT->mform_element($element, false, false, '', true);
91bda4cd
DW
182
183 if (empty($out)) {
184 $renderer = new HTML_QuickForm_Renderer_Default();
185 $renderer->setElementTemplate('{element}');
186 $element->accept($renderer);
187 $out = $renderer->toHtml();
188 }
189 $elements[] = $out;
190 // Restore the element's name.
191 if ($this->_appendName) {
192 $element->setName($elementname);
193 }
194
195 }
196
197 $context['elements'] = $elements;
198 return $context;
199 }
200
201 /**
202 * Accepts a renderer
203 *
204 * @param object An HTML_QuickForm_Renderer object
205 * @param bool Whether a group is required
206 * @param string An error message associated with a group
207 * @access public
208 * @return void
209 */
210 function accept(&$renderer, $required = false, $error = null) {
211 $this->_createElementsIfNotExist();
212 $renderer->startGroup($this, $required, $error);
213 if (!$this->_renderedfromtemplate) {
214 // Backwards compatible path - only do this if we didn't render the sub-elements already.
215 $name = $this->getName();
216 foreach (array_keys($this->_elements) as $key) {
217 $element =& $this->_elements[$key];
218
219 if ($this->_appendName) {
220 $elementName = $element->getName();
221 if (isset($elementName)) {
222 $element->setName($name . '['. (strlen($elementName)? $elementName: $key) .']');
223 } else {
224 $element->setName($name);
225 }
226 }
227
228 $required = !$element->isFrozen() && in_array($element->getName(), $this->_required);
229
230 $element->accept($renderer, $required);
231
232 // restore the element's name
233 if ($this->_appendName) {
234 $element->setName($elementName);
235 }
236 }
237 }
238 $renderer->finishGroup($this);
239 }
e24b7f85 240}