Merge branch 'MDL-32657-master-1' of git://git.luns.net.uk/moodle
[moodle.git] / lib / form / select.php
blob22c8752d1b7b2bb07008d15efbeaee4ec937af4e
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 * select type form element
21 * Contains HTML class for a select type element
23 * @package core_form
24 * @copyright 2006 Jamie Pratt <me@jamiep.org>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once('HTML/QuickForm/select.php');
30 /**
31 * select type form element
33 * HTML class for a select type element
35 * @package core_form
36 * @category form
37 * @copyright 2006 Jamie Pratt <me@jamiep.org>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class MoodleQuickForm_select extends HTML_QuickForm_select{
41 /** @var string html for help button, if empty then no help */
42 var $_helpbutton='';
44 /** @var bool if true label will be hidden */
45 var $_hiddenLabel=false;
47 /**
48 * constructor
50 * @param string $elementName Select name attribute
51 * @param mixed $elementLabel Label(s) for the select
52 * @param mixed $options Data to be used to populate options
53 * @param mixed $attributes Either a typical HTML attribute string or an associative array
55 function MoodleQuickForm_select($elementName=null, $elementLabel=null, $options=null, $attributes=null) {
56 parent::HTML_QuickForm_select($elementName, $elementLabel, $options, $attributes);
59 /**
60 * Sets label to be hidden
62 * @param bool $hiddenLabel sets if label should be hidden
64 function setHiddenLabel($hiddenLabel){
65 $this->_hiddenLabel = $hiddenLabel;
68 /**
69 * Returns HTML for select form element.
71 * @return string
73 function toHtml(){
74 if ($this->_hiddenLabel){
75 $this->_generateId();
76 return '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
77 $this->getLabel().'</label>'.parent::toHtml();
78 } else {
79 return parent::toHtml();
83 /**
84 * set html for help button
86 * @param array $helpbuttonargs array of arguments to make a help button
87 * @param string $function function name to call to get html
88 * @deprecated since Moodle 2.0. Please do not call this function any more.
89 * @todo MDL-31047 this api will be removed.
90 * @see MoodleQuickForm::setHelpButton()
92 function setHelpButton($helpbuttonargs, $function='helpbutton'){
93 debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
96 /**
97 * get html for help button
99 * @return string html for help button
101 function getHelpButton(){
102 return $this->_helpbutton;
106 * Removes an OPTION from the SELECT
108 * @param string $value Value for the OPTION to remove
109 * @return void
111 function removeOption($value)
113 $key=array_search($value, $this->_values);
114 if ($key!==FALSE and $key!==null) {
115 unset($this->_values[$key]);
117 foreach ($this->_options as $key=>$option){
118 if ($option['attr']['value']==$value){
119 unset($this->_options[$key]);
120 // we must reindex the options because the ugly code in quickforms' select.php expects that keys are 0,1,2,3... !?!?
121 $this->_options = array_merge($this->_options);
122 return;
128 * Removes all OPTIONs from the SELECT
130 function removeOptions()
132 $this->_options = array();
136 * Slightly different container template when frozen. Don't want to use a label tag
137 * with a for attribute in that case for the element label but instead use a div.
138 * Templates are defined in renderer constructor.
140 * @return string
142 function getElementTemplateType(){
143 if ($this->_flagFrozen){
144 return 'static';
145 } else {
146 return 'default';
151 * We check the options and return only the values that _could_ have been
152 * selected. We also return a scalar value if select is not "multiple"
154 * @param array $submitValues submitted values
155 * @param bool $assoc if true the retured value is associated array
156 * @return mixed
158 function exportValue(&$submitValues, $assoc = false)
160 if (empty($this->_options)) {
161 return $this->_prepareValue(null, $assoc);
164 $value = $this->_findValue($submitValues);
165 if (is_null($value)) {
166 $value = $this->getValue();
168 $value = (array)$value;
170 $cleaned = array();
171 foreach ($value as $v) {
172 foreach ($this->_options as $option) {
173 if ((string)$option['attr']['value'] === (string)$v) {
174 $cleaned[] = (string)$option['attr']['value'];
175 break;
180 if (empty($cleaned)) {
181 return $this->_prepareValue(null, $assoc);
183 if ($this->getMultiple()) {
184 return $this->_prepareValue($cleaned, $assoc);
185 } else {
186 return $this->_prepareValue($cleaned[0], $assoc);