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');
29 require_once('templatable_form_element.php');
32 * select type form element
34 * HTML class for a select type element with options containing link
36 * @deprecated since 3.2
39 * @copyright 2008 Nicolas Connault <nicolasconnault@gmail.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class MoodleQuickForm_selectwithlink
extends HTML_QuickForm_select
implements templatable
{
44 use templatable_form_element
{
45 export_for_template
as export_for_template_base
;
47 /** @var string html for help button, if empty then no help */
50 /** @var bool if true label will be hidden */
51 var $_hiddenLabel=false;
53 /** @var string url to which select option will be posted */
56 /** @var string data which will be posted to link */
59 /** @var string url return link */
60 var $_linkreturn=null;
65 * @param string $elementName Select name attribute
66 * @param mixed $elementLabel Label(s) for the select
67 * @param array $options Data to be used to populate options
68 * @param mixed $attributes Either a typical HTML attribute string or an associative array
69 * @param bool $linkdata data to be posted
71 public function __construct($elementName=null, $elementLabel=null, $options=null, $attributes=null, $linkdata=null) {
72 if (!empty($linkdata['link']) && !empty($linkdata['label'])) {
73 $this->_link
= $linkdata['link'];
74 $this->_linklabel
= $linkdata['label'];
77 if (!empty($linkdata['return'])) {
78 $this->_linkreturn
= $linkdata['return'];
81 parent
::__construct($elementName, $elementLabel, $options, $attributes);
83 $this->_type
= 'selectwithlink';
87 * Old syntax of class constructor. Deprecated in PHP7.
89 * @deprecated since Moodle 3.1
91 public function MoodleQuickForm_selectwithlink($elementName=null, $elementLabel=null, $options=null, $attributes=null, $linkdata=null) {
92 debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER
);
93 self
::__construct($elementName, $elementLabel, $options, $attributes, $linkdata);
97 * Sets label to be hidden
99 * @param bool $hiddenLabel sets if label should be hidden
101 function setHiddenLabel($hiddenLabel){
102 $this->_hiddenLabel
= $hiddenLabel;
106 * Returns the SELECT in HTML
112 if ($this->_hiddenLabel
){
113 $this->_generateId();
114 $retval = '<label class="accesshide" for="'.$this->getAttribute('id').'" >'.
115 $this->getLabel().'</label>'.parent
::toHtml();
117 $retval = parent
::toHtml();
120 if (!empty($this->_link
)) {
121 if (!empty($this->_linkreturn
) && is_array($this->_linkreturn
)) {
123 if (strstr($this->_link
, '?')) {
124 $appendchar = '&';
127 foreach ($this->_linkreturn
as $key => $val) {
128 $this->_link
.= $appendchar."$key=$val";
129 $appendchar = '&';
133 $retval .= '<a style="margin-left: 5px" href="'.$this->_link
.'">'.$this->_linklabel
.'</a>';
140 * get html for help button
142 * @return string html for help button
144 function getHelpButton(){
145 return $this->_helpbutton
;
149 * Removes an OPTION from the SELECT
151 * @param string $value Value for the OPTION to remove
153 function removeOption($value)
155 $key=array_search($value, $this->_values
);
156 if ($key!==FALSE and $key!==null) {
157 unset($this->_values
[$key]);
159 foreach ($this->_options
as $key=>$option){
160 if ($option['attr']['value']==$value){
161 unset($this->_options
[$key]);
168 * Removes all OPTIONs from the SELECT
170 function removeOptions()
172 $this->_options
= array();
176 * Slightly different container template when frozen. Don't want to use a label tag
177 * with a for attribute in that case for the element label but instead use a div.
178 * Templates are defined in renderer constructor.
182 function getElementTemplateType(){
183 if ($this->_flagFrozen
){
191 * We check the options and return only the values that _could_ have been
192 * selected. We also return a scalar value if select is not "multiple"
194 * @param array $submitValues submitted values
195 * @param bool $assoc if true the retured value is associated array
198 function exportValue(&$submitValues, $assoc = false)
200 if (empty($this->_options
)) {
201 return $this->_prepareValue(null, $assoc);
204 $value = $this->_findValue($submitValues);
205 if (is_null($value)) {
206 $value = $this->getValue();
208 $value = (array)$value;
211 foreach ($value as $v) {
212 foreach ($this->_options
as $option) {
213 if ((string)$option['attr']['value'] === (string)$v) {
214 $cleaned[] = (string)$option['attr']['value'];
220 if (empty($cleaned)) {
221 return $this->_prepareValue(null, $assoc);
223 if ($this->getMultiple()) {
224 return $this->_prepareValue($cleaned, $assoc);
226 return $this->_prepareValue($cleaned[0], $assoc);
230 public function export_for_template(renderer_base
$output) {
231 $context = $this->export_for_template_base($output);
234 // Standard option attributes.
235 $standardoptionattributes = ['text', 'value', 'selected', 'disabled'];
236 foreach ($this->_options
as $option) {
237 if (is_array($this->_values
) && in_array( (string) $option['attr']['value'], $this->_values
)) {
238 $this->_updateAttrArray($option['attr'], ['selected' => 'selected']);
241 'text' => $option['text'],
242 'value' => $option['attr']['value'],
243 'selected' => !empty($option['attr']['selected']),
244 'disabled' => !empty($option['attr']['disabled']),
246 // Set other attributes.
247 $otheroptionattributes = [];
248 foreach ($option['attr'] as $attr => $value) {
249 if (!in_array($attr, $standardoptionattributes) && $attr != 'class' && !is_object($value)) {
250 $otheroptionattributes[] = $attr . '="' . s($value) . '"';
253 $o['optionattributes'] = implode(' ', $otheroptionattributes);
256 $context['options'] = $options;
257 if (!empty($this->_link
)) {
258 if (!empty($this->_linkreturn
) && is_array($this->_linkreturn
)) {
260 if (strstr($this->_link
, '?')) {
261 $appendchar = '&';
264 foreach ($this->_linkreturn
as $key => $val) {
265 $this->_link
.= $appendchar."$key=$val";
266 $appendchar = '&';
270 $context['link'] = $this->_link
;
271 $context['linklabel'] = $this->_linklabel
;