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 * Contains HTML class for a select type element with options containing link
24 * @copyright 2008 Nicolas Connault <nicolasconnault@gmail.com>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once('HTML/QuickForm/select.php');
31 * select type form element
33 * HTML class for a select type element with options containing link
37 * @copyright 2008 Nicolas Connault <nicolasconnault@gmail.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class MoodleQuickForm_selectwithlink
extends HTML_QuickForm_select
{
41 /** @var string html for help button, if empty then no help */
44 /** @var bool if true label will be hidden */
45 var $_hiddenLabel=false;
47 /** @var string url to which select option will be posted */
50 /** @var string data which will be posted to link */
53 /** @var string url return link */
54 var $_linkreturn=null;
59 * @param string $elementName Select name attribute
60 * @param mixed $elementLabel Label(s) for the select
61 * @param array $options Data to be used to populate options
62 * @param mixed $attributes Either a typical HTML attribute string or an associative array
63 * @param bool $linkdata data to be posted
65 function MoodleQuickForm_selectwithlink($elementName=null, $elementLabel=null, $options=null, $attributes=null, $linkdata=null)
67 if (!empty($linkdata['link']) && !empty($linkdata['label'])) {
68 $this->_link
= $linkdata['link'];
69 $this->_linklabel
= $linkdata['label'];
72 if (!empty($linkdata['return'])) {
73 $this->_linkreturn
= $linkdata['return'];
76 parent
::HTML_QuickForm_select($elementName, $elementLabel, $options, $attributes);
80 * Sets label to be hidden
82 * @param bool $hiddenLabel sets if label should be hidden
84 function setHiddenLabel($hiddenLabel){
85 $this->_hiddenLabel
= $hiddenLabel;
89 * Returns the SELECT in HTML
95 if ($this->_hiddenLabel
){
97 $retval = '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
98 $this->getLabel().'</label>'.parent
::toHtml();
100 $retval = parent
::toHtml();
103 if (!empty($this->_link
)) {
104 if (!empty($this->_linkreturn
) && is_array($this->_linkreturn
)) {
106 if (strstr($this->_link
, '?')) {
107 $appendchar = '&';
110 foreach ($this->_linkreturn
as $key => $val) {
111 $this->_link
.= $appendchar."$key=$val";
112 $appendchar = '&';
116 $retval .= '<a style="margin-left: 5px" href="'.$this->_link
.'">'.$this->_linklabel
.'</a>';
123 * get html for help button
125 * @return string html for help button
127 function getHelpButton(){
128 return $this->_helpbutton
;
132 * Removes an OPTION from the SELECT
134 * @param string $value Value for the OPTION to remove
136 function removeOption($value)
138 $key=array_search($value, $this->_values
);
139 if ($key!==FALSE and $key!==null) {
140 unset($this->_values
[$key]);
142 foreach ($this->_options
as $key=>$option){
143 if ($option['attr']['value']==$value){
144 unset($this->_options
[$key]);
151 * Removes all OPTIONs from the SELECT
153 function removeOptions()
155 $this->_options
= array();
159 * Slightly different container template when frozen. Don't want to use a label tag
160 * with a for attribute in that case for the element label but instead use a div.
161 * Templates are defined in renderer constructor.
165 function getElementTemplateType(){
166 if ($this->_flagFrozen
){
174 * We check the options and return only the values that _could_ have been
175 * selected. We also return a scalar value if select is not "multiple"
177 * @param array $submitValues submitted values
178 * @param bool $assoc if true the retured value is associated array
181 function exportValue(&$submitValues, $assoc = false)
183 if (empty($this->_options
)) {
184 return $this->_prepareValue(null, $assoc);
187 $value = $this->_findValue($submitValues);
188 if (is_null($value)) {
189 $value = $this->getValue();
191 $value = (array)$value;
194 foreach ($value as $v) {
195 foreach ($this->_options
as $option) {
196 if ((string)$option['attr']['value'] === (string)$v) {
197 $cleaned[] = (string)$option['attr']['value'];
203 if (empty($cleaned)) {
204 return $this->_prepareValue(null, $assoc);
206 if ($this->getMultiple()) {
207 return $this->_prepareValue($cleaned, $assoc);
209 return $this->_prepareValue($cleaned[0], $assoc);