weekly release 3.2.1+
[moodle.git] / lib / form / group.php
blobda9359ddbeaca1cfa606e3c0c4dc73510287be3b
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 MoodleQuickForm */
50 protected $_mform = null;
52 protected $_renderedfromtemplate = false;
54 /**
55 * constructor
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.
63 public function __construct($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true) {
64 parent::__construct($elementName, $elementLabel, $elements, $separator, $appendName);
67 /**
68 * Old syntax of class constructor. Deprecated in PHP7.
70 * @deprecated since Moodle 3.1
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);
77 /** @var string template type, would cause problems with client side validation so will leave for now */
78 //var $_elementTemplateType='fieldset';
80 /**
81 * set html for help button
83 function getHelpButton(){
84 return $this->_helpbutton;
87 /**
88 * Returns element template, nodisplay/static/fieldset
90 * @return string
92 function getElementTemplateType(){
93 if ($this->_flagFrozen){
94 if ($this->getGroupType() == 'submit'){
95 return 'nodisplay';
96 } else {
97 return 'static';
99 } else {
100 if ($this->getGroupType() == 'submit') {
101 return 'actionbuttons';
103 return 'fieldset';
108 * Sets the grouped elements and hides label
110 * @param array $elements
112 function setElements($elements){
113 parent::setElements($elements);
114 foreach ($this->_elements as $element){
115 if (method_exists($element, 'setHiddenLabel')){
116 $element->setHiddenLabel(true);
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
126 public function setMoodleForm($mform) {
127 if ($mform && $mform instanceof MoodleQuickForm) {
128 $this->_mform = $mform;
133 * Called by HTML_QuickForm whenever form event is made on this element
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()}
138 * @param string $event Name of event
139 * @param mixed $arg event arguments
140 * @param mixed $caller calling object
142 public function onQuickFormEvent($event, $arg, &$caller) {
143 $this->setMoodleForm($caller);
144 return parent::onQuickFormEvent($event, $arg, $caller);
148 * Creates an element to add to the group
149 * Expects the same arguments as MoodleQuickForm::createElement()
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.');
155 return call_user_func_array([$this->_mform, 'createElement'], func_get_args());
158 public function export_for_template(renderer_base $output) {
159 global $OUTPUT;
161 $context = $this->export_for_template_base($output);
163 $this->_renderedfromtemplate = true;
165 include_once('HTML/QuickForm/Renderer/Default.php');
167 $elements = [];
168 $name = $this->getName();
169 $i = 0;
170 foreach ($this->_elements as $key => $element) {
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);
180 $element->_generateId();
182 $out = $OUTPUT->mform_element($element, false, false, '', true);
184 if (empty($out)) {
185 $renderer = new HTML_QuickForm_Renderer_Default();
186 $renderer->setElementTemplate('{element}');
187 $element->accept($renderer);
188 $out = $renderer->toHtml();
191 // Replicates the separator logic from 'pear/HTML/QuickForm/Renderer/Default.php'.
192 $separator = '';
193 if ($i > 0) {
194 if (is_array($this->_separator)) {
195 $separator = $this->_separator[($i - 1) % count($this->_separator)];
196 } else if ($this->_separator === null) {
197 $separator = '&nbsp;';
198 } else {
199 $separator = (string) $this->_separator;
203 $elements[] = [
204 'separator' => $separator,
205 'html' => $out
208 // Restore the element's name.
209 if ($this->_appendName) {
210 $element->setName($elementname);
213 $i++;
216 $context['elements'] = $elements;
217 return $context;
221 * Accepts a renderer
223 * @param object An HTML_QuickForm_Renderer object
224 * @param bool Whether a group is required
225 * @param string An error message associated with a group
226 * @access public
227 * @return void
229 public function accept(&$renderer, $required = false, $error = null) {
230 $this->_createElementsIfNotExist();
231 $renderer->startGroup($this, $required, $error);
232 if (!$this->_renderedfromtemplate) {
233 // Backwards compatible path - only do this if we didn't render the sub-elements already.
234 $name = $this->getName();
235 foreach (array_keys($this->_elements) as $key) {
236 $element =& $this->_elements[$key];
237 $elementname = '';
238 if ($this->_appendName) {
239 $elementname = $element->getName();
240 if (isset($elementname)) {
241 $element->setName($name . '['. (strlen($elementname) ? $elementname : $key) .']');
242 } else {
243 $element->setName($name);
247 $required = !$element->isFrozen() && in_array($element->getName(), $this->_required);
249 $element->accept($renderer, $required);
251 // Restore the element's name.
252 if ($this->_appendName) {
253 $element->setName($elementname);
257 $renderer->finishGroup($this);