MDL-21767 fixed input validation
[moodle.git] / lib / form / select.php
CommitLineData
da6f8763 1<?php
2require_once('HTML/QuickForm/select.php');
3
4/**
5 * HTML class for a select type element
d4fe14d3 6 *
da6f8763 7 * @author Jamie Pratt
8 * @access public
9 */
7f40a229 10class MoodleQuickForm_select extends HTML_QuickForm_select{
da6f8763 11 /**
12 * html for help button, if empty then no help
13 *
14 * @var string
15 */
16 var $_helpbutton='';
44875d78 17 var $_hiddenLabel=false;
3e8b394e 18
19 function MoodleQuickForm_select($elementName=null, $elementLabel=null, $options=null, $attributes=null) {
20 parent::HTML_QuickForm_select($elementName, $elementLabel, $options, $attributes);
21 }
44875d78 22 function setHiddenLabel($hiddenLabel){
23 $this->_hiddenLabel = $hiddenLabel;
24 }
25 function toHtml(){
44875d78 26 if ($this->_hiddenLabel){
19194f82 27 $this->_generateId();
44875d78 28 return '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
29 $this->getLabel().'</label>'.parent::toHtml();
30 } else {
31 return parent::toHtml();
32 }
33 }
34 /**
35 * Automatically generates and assigns an 'id' attribute for the element.
36 *
37 * Currently used to ensure that labels work on radio buttons and
38 * checkboxes. Per idea of Alexander Radivanovich.
39 * Overriden in moodleforms to remove qf_ prefix.
40 *
41 * @access private
42 * @return void
43 */
44 function _generateId()
45 {
46 static $idx = 1;
47
48 if (!$this->getAttribute('id')) {
49 $this->updateAttributes(array('id' => 'id_'. substr(md5(microtime() . $idx++), 0, 6)));
50 }
51 } // end func _generateId
da6f8763 52 /**
53 * set html for help button
54 *
55 * @access public
56 * @param array $help array of arguments to make a help button
d4fe14d3 57 * @param string $function function name to call to get html
da6f8763 58 */
d4fe14d3 59 function setHelpButton($helpbuttonargs, $function='helpbutton'){
4bcc5118 60 debugging('component setHelpButton() is not used any more, please use $mform->setHelpButton() instead');
da6f8763 61 }
62 /**
63 * get html for help button
64 *
65 * @access public
66 * @return string html for help button
67 */
68 function getHelpButton(){
69 return $this->_helpbutton;
70 }
f388442b 71 /**
72 * Removes an OPTION from the SELECT
73 *
74 * @param string $value Value for the OPTION to remove
75 * @since 1.0
76 * @access public
77 * @return void
78 */
79 function removeOption($value)
80 {
81 $key=array_search($value, $this->_values);
ec74e82d 82 if ($key!==FALSE and $key!==null) {
f388442b 83 unset($this->_values[$key]);
84 }
85 foreach ($this->_options as $key=>$option){
86 if ($option['attr']['value']==$value){
87 unset($this->_options[$key]);
29712788 88 // we must reindex the options because the ugly code in quickforms' select.php expects that keys are 0,1,2,3... !?!?
89 $this->_options = array_merge($this->_options);
f388442b 90 return;
91 }
92 }
93 } // end func removeOption
a23f0aaf 94 /**
95 * Removes all OPTIONs from the SELECT
96 *
97 * @param string $value Value for the OPTION to remove
98 * @since 1.0
99 * @access public
100 * @return void
101 */
102 function removeOptions()
103 {
104 $this->_options = array();
105 } // end func removeOption
0709d4d5 106 /**
107 * Slightly different container template when frozen. Don't want to use a label tag
108 * with a for attribute in that case for the element label but instead use a div.
109 * Templates are defined in renderer constructor.
110 *
111 * @return string
112 */
113 function getElementTemplateType(){
114 if ($this->_flagFrozen){
115 return 'static';
116 } else {
117 return 'default';
118 }
119 }
6fdbd162
PS
120 /**
121 * We check the options and return only the values that _could_ have been
122 * selected. We also return a scalar value if select is not "multiple"
123 */
124 function exportValue(&$submitValues, $assoc = false)
125 {
126 if (empty($this->_options)) {
127 return $this->_prepareValue(null, $assoc);
128 }
129
130 $value = $this->_findValue($submitValues);
131 if (is_null($value)) {
132 $value = $this->getValue();
133 }
134 $value = (array)$value;
135
136 $cleaned = array();
137 foreach ($value as $v) {
138 foreach ($this->_options as $option) {
139 if ((string)$option['attr']['value'] === (string)$v) {
140 $cleaned[] = (string)$option['attr']['value'];
141 break;
142 }
143 }
144 }
145
146 if (empty($cleaned)) {
147 return $this->_prepareValue(null, $assoc);
148 }
149 if ($this->getMultiple()) {
150 return $this->_prepareValue($cleaned, $assoc);
151 } else {
152 return $this->_prepareValue($cleaned[0], $assoc);
153 }
154 }
da6f8763 155}