2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * select type form element
21 * Class to dynamically create an HTML SELECT with all options grouped in optgroups
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/element.php');
29 require_once('templatable_form_element.php');
32 * select type form element
34 * Class to dynamically create an HTML SELECT with all options grouped in optgroups
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_selectgroups
extends HTML_QuickForm_element
implements templatable
{
43 use templatable_form_element
{
44 export_for_template
as export_for_template_base
;
47 /** @var bool add choose option */
48 var $showchoose = false;
50 /** @var array Contains the select optgroups */
51 var $_optGroups = array();
53 /** @var array Default values of the SELECT */
56 /** @var string html for help button, if empty then no help */
59 /** @var bool if true label will be hidden */
60 var $_hiddenLabel=false;
65 * @param string $elementName Select name attribute
66 * @param mixed $elementLabel Label(s) for the select
67 * @param array $optgrps Data to be used to populate options
68 * @param mixed $attributes Either a typical HTML attribute string or an associative array
69 * @param bool $showchoose add standard moodle "Choose..." option as first item
71 public function __construct($elementName=null, $elementLabel=null, $optgrps=null, $attributes=null, $showchoose=false)
73 $this->showchoose
= $showchoose;
74 parent
::__construct($elementName, $elementLabel, $attributes);
75 $this->_persistantFreeze
= true;
76 $this->_type
= 'selectgroups';
77 if (isset($optgrps)) {
78 $this->loadArrayOptGroups($optgrps);
83 * Old syntax of class constructor. Deprecated in PHP7.
85 * @deprecated since Moodle 3.1
87 public function MoodleQuickForm_selectgroups($elementName=null, $elementLabel=null, $optgrps=null, $attributes=null, $showchoose=false) {
88 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
89 self
::__construct($elementName, $elementLabel, $optgrps, $attributes, $showchoose);
93 * Sets the default values of the select box
95 * @param mixed $values Array or comma delimited string of selected values
97 function setSelected($values)
99 if (is_string($values) && $this->getMultiple()) {
100 $values = preg_split("/[ ]?,[ ]?/", $values);
102 if (is_array($values)) {
103 $this->_values
= array_values($values);
105 $this->_values
= array($values);
110 * Returns an array of the selected values
112 * @return array of selected values
114 function getSelected()
116 return $this->_values
;
120 * Sets the input field name
122 * @param string $name Input field name attribute
124 function setName($name)
126 $this->updateAttributes(array('name' => $name));
130 * Returns the element name
136 return $this->getAttribute('name');
140 * Returns the element name (possibly with brackets appended)
144 function getPrivateName()
146 if ($this->getAttribute('multiple')) {
147 return $this->getName() . '[]';
149 return $this->getName();
154 * Sets the value of the form element
156 * @param mixed $value Array or comma delimited string of selected values
158 function setValue($value)
160 $this->setSelected($value);
164 * Returns an array of the selected values
166 * @return array of selected values
170 return $this->_values
;
174 * Sets the select field size, only applies to 'multiple' selects
176 * @param int $size Size of select field
178 function setSize($size)
180 $this->updateAttributes(array('size' => $size));
184 * Returns the select field size
190 return $this->getAttribute('size');
194 * Sets the select mutiple attribute
196 * @param bool $multiple Whether the select supports multi-selections
198 function setMultiple($multiple)
201 $this->updateAttributes(array('multiple' => 'multiple'));
203 $this->removeAttribute('multiple');
208 * Returns the select mutiple attribute
210 * @return bool true if multiple select, false otherwise
212 function getMultiple()
214 return (bool)$this->getAttribute('multiple');
218 * Loads the options from an associative array
220 * @param array $arr Associative array of options
221 * @param mixed $values (optional) Array or comma delimited string of selected values
222 * @return PEAR_Error|bool on error or true
225 function loadArrayOptGroups($arr, $values=null)
227 if (!is_array($arr)) {
228 return self
::raiseError('Argument 1 of HTML_Select::loadArrayOptGroups is not a valid array');
230 if (isset($values)) {
231 $this->setSelected($values);
233 foreach ($arr as $key => $val) {
234 // Warning: new API since release 2.3
235 $this->addOptGroup($key, $val);
241 * Adds a new OPTION to the SELECT
243 * @param string $text Display text for the OPTION
244 * @param string $value Value for the OPTION
245 * @param mixed $attributes Either a typical HTML attribute string
246 * or an associative array
248 function addOptGroup($text, $value, $attributes=null)
250 if (null === $attributes) {
251 $attributes = array('label' => $text);
253 $attributes = $this->_parseAttributes($attributes);
254 $this->_updateAttrArray($attributes, array('label' => $text));
256 $index = count($this->_optGroups
);
257 $this->_optGroups
[$index] = array('attr' => $attributes);
258 $this->loadArrayOptions($index, $value);
262 * Loads the options from an associative array
264 * @param string $optgroup name of the options group
265 * @param array $arr Associative array of options
266 * @param mixed $values (optional) Array or comma delimited string of selected values
267 * @return PEAR_Error|bool on error or true
270 function loadArrayOptions($optgroup, $arr, $values=null)
272 if (!is_array($arr)) {
273 return self
::raiseError('Argument 1 of HTML_Select::loadArray is not a valid array');
275 if (isset($values)) {
276 $this->setSelected($values);
278 foreach ($arr as $key => $val) {
279 // Warning: new API since release 2.3
280 $this->addOption($optgroup, $val, $key);
286 * Adds a new OPTION to an optgroup
288 * @param string $optgroup name of the option group
289 * @param string $text Display text for the OPTION
290 * @param string $value Value for the OPTION
291 * @param mixed $attributes Either a typical HTML attribute string
292 * or an associative array
294 function addOption($optgroup, $text, $value, $attributes=null)
296 if (null === $attributes) {
297 $attributes = array('value' => $value);
299 $attributes = $this->_parseAttributes($attributes);
300 if (isset($attributes['selected'])) {
301 // the 'selected' attribute will be set in toHtml()
302 $this->_removeAttr('selected', $attributes);
303 if (is_null($this->_values
)) {
304 $this->_values
= array($value);
305 } elseif (!in_array($value, $this->_values
)) {
306 $this->_values
[] = $value;
309 $this->_updateAttrArray($attributes, array('value' => $value));
311 $this->_optGroups
[$optgroup]['options'][] = array('text' => $text, 'attr' => $attributes);
315 * Returns the SELECT in HTML
321 if ($this->_flagFrozen
) {
322 return $this->getFrozenHtml();
324 $tabs = $this->_getTabs();
327 if ($this->getComment() != '') {
328 $strHtml .= $tabs . '<!-- ' . $this->getComment() . " //-->\n";
331 if (!$this->getMultiple()) {
332 $attrString = $this->_getAttrString($this->_attributes
);
334 $myName = $this->getName();
335 $this->setName($myName . '[]');
336 $attrString = $this->_getAttrString($this->_attributes
);
337 $this->setName($myName);
340 if ($this->_hiddenLabel
){
341 $this->_generateId();
342 $strHtml .= '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
343 $this->getLabel().'</label>';
345 $strHtml .= '<select' . $attrString . ">\n";
346 if ($this->showchoose
) {
347 $strHtml .= $tabs . "\t\t<option value=\"\">" . get_string('choose') . "...</option>\n";
349 foreach ($this->_optGroups
as $optGroup) {
350 if (empty($optGroup['options'])) {
354 $strHtml .= $tabs . "\t<optgroup" . ($this->_getAttrString($optGroup['attr'])) . '>';
355 foreach ($optGroup['options'] as $option){
356 if (is_array($this->_values
) && in_array((string)$option['attr']['value'], $this->_values
)) {
357 $this->_updateAttrArray($option['attr'], array('selected' => 'selected'));
359 $strHtml .= $tabs . "\t\t<option" . $this->_getAttrString($option['attr']) . '>' .
360 $option['text'] . "</option>\n";
362 $strHtml .= $tabs . "\t</optgroup>\n";
364 return $strHtml . $tabs . '</select>';
369 * Returns the value of field without HTML tags
373 function getFrozenHtml()
376 if (is_array($this->_values
)) {
377 foreach ($this->_values
as $key => $val) {
378 foreach ($this->_optGroups
as $optGroup) {
379 if (empty($optGroup['options'])) {
382 for ($i = 0, $optCount = count($optGroup['options']); $i < $optCount; $i++
) {
383 if ((string)$val == (string)$optGroup['options'][$i]['attr']['value']) {
384 $value[$key] = $optGroup['options'][$i]['text'];
391 $html = empty($value)?
' ': join('<br />', $value);
392 if ($this->_persistantFreeze
) {
393 $name = $this->getPrivateName();
394 // Only use id attribute if doing single hidden input
395 if (1 == count($value)) {
396 $id = $this->getAttribute('id');
397 $idAttr = isset($id)?
array('id' => $id): array();
401 foreach ($value as $key => $item) {
402 $html .= '<input' . $this->_getAttrString(array(
405 'value' => $this->_values
[$key]
406 ) +
$idAttr) . ' />';
413 * We check the options and return only the values that _could_ have been
414 * selected. We also return a scalar value if select is not "multiple"
416 * @param array $submitValues submitted values
417 * @param bool $assoc if true the retured value is associated array
420 function exportValue(&$submitValues, $assoc = false)
422 if (empty($this->_optGroups
)) {
423 return $this->_prepareValue(null, $assoc);
426 $value = $this->_findValue($submitValues);
427 if (is_null($value)) {
428 $value = $this->getValue();
430 $value = (array)$value;
433 foreach ($value as $v) {
434 foreach ($this->_optGroups
as $optGroup){
435 if (empty($optGroup['options'])) {
438 foreach ($optGroup['options'] as $option) {
439 if ((string)$option['attr']['value'] === (string)$v) {
440 $cleaned[] = (string)$option['attr']['value'];
447 if (empty($cleaned)) {
448 return $this->_prepareValue(null, $assoc);
450 if ($this->getMultiple()) {
451 return $this->_prepareValue($cleaned, $assoc);
453 return $this->_prepareValue($cleaned[0], $assoc);
458 * Called by HTML_QuickForm whenever form event is made on this element
460 * @param string $event Name of event
461 * @param mixed $arg event arguments
462 * @param object $caller calling object
465 function onQuickFormEvent($event, $arg, &$caller)
467 if ('updateValue' == $event) {
468 $value = $this->_findValue($caller->_constantValues
);
469 if (null === $value) {
470 $value = $this->_findValue($caller->_submitValues
);
471 // Fix for bug #4465 & #5269
472 // XXX: should we push this to element::onQuickFormEvent()?
473 if (null === $value && (!$caller->isSubmitted() ||
!$this->getMultiple())) {
474 $value = $this->_findValue($caller->_defaultValues
);
477 if (null !== $value) {
478 $this->setValue($value);
482 return parent
::onQuickFormEvent($event, $arg, $caller);
487 * Sets label to be hidden
489 * @param bool $hiddenLabel sets if label should be hidden
491 function setHiddenLabel($hiddenLabel){
492 $this->_hiddenLabel
= $hiddenLabel;
496 * get html for help button
498 * @return string html for help button
500 function getHelpButton(){
501 return $this->_helpbutton
;
505 * Slightly different container template when frozen. Don't want to use a label tag
506 * with a for attribute in that case for the element label but instead use a div.
507 * Templates are defined in renderer constructor.
511 function getElementTemplateType(){
512 if ($this->_flagFrozen
){
519 public function export_for_template(renderer_base
$output) {
520 $context = $this->export_for_template_base($output);
522 if ($this->showchoose
) {
524 'text' => get_string('choosedots')
528 // Standard option attributes.
529 $standardoptionattributes = ['text', 'value', 'selected', 'disabled'];
530 foreach ($this->_optGroups
as $group) {
533 if (empty($group['options'])) {
536 foreach ($group['options'] as $option) {
537 $o = ['value' => (string)$option['attr']['value']];
538 if (is_array($this->_values
) && in_array($o['value'], $this->_values
)) {
539 $o['selected'] = true;
541 $o['selected'] = false;
543 $o['text'] = $option['text'];
544 $o['disabled'] = !empty($option['attr']['disabled']);
545 // Set other attributes.
546 $otheroptionattributes = [];
547 foreach ($option['attr'] as $attr => $value) {
548 if (!in_array($attr, $standardoptionattributes) && $attr != 'class' && !is_object($value)) {
549 $otheroptionattributes[] = $attr . '="' . s($value) . '"';
552 $o['optionattributes'] = implode(' ', $otheroptionattributes);
557 'text' => $group['attr']['label'],
558 'options' => $options
561 $optiongroups[] = $og;
563 $context['optiongroups'] = $optiongroups;
564 // If the select is a static element and does not allow the user to change the value (Ex: Auth method),
565 // we need to change the label to static.
566 $context['staticlabel'] = $this->_flagFrozen
;