1441 |
ariadna |
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.
|
|
|
13 |
//
|
|
|
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/>.
|
|
|
16 |
|
|
|
17 |
namespace core\output;
|
|
|
18 |
|
|
|
19 |
use core\exception\coding_exception;
|
|
|
20 |
use core\output\actions\component_action;
|
|
|
21 |
use moodle_url;
|
|
|
22 |
use stdClass;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Simple form with just one select field that gets submitted automatically.
|
|
|
26 |
*
|
|
|
27 |
* If JS not enabled small go button is printed too.
|
|
|
28 |
*
|
|
|
29 |
* @copyright 2009 Petr Skoda
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
* @since Moodle 2.0
|
|
|
32 |
* @package core
|
|
|
33 |
* @category output
|
|
|
34 |
*/
|
|
|
35 |
class single_select implements renderable, templatable {
|
|
|
36 |
/**
|
|
|
37 |
* @var moodle_url Target url - includes hidden fields
|
|
|
38 |
*/
|
|
|
39 |
public $url;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* @var string Name of the select element.
|
|
|
43 |
*/
|
|
|
44 |
public $name;
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* @var array $options associative array value=>label ex.: array(1=>'One, 2=>Two)
|
|
|
48 |
* it is also possible to specify optgroup as complex label array ex.:
|
|
|
49 |
* array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
|
|
|
50 |
* array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
|
|
|
51 |
*/
|
|
|
52 |
public $options;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* @var string Selected option
|
|
|
56 |
*/
|
|
|
57 |
public $selected;
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* @var array Nothing selected
|
|
|
61 |
*/
|
|
|
62 |
public $nothing;
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* @var array Extra select field attributes
|
|
|
66 |
*/
|
|
|
67 |
public $attributes = [];
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* @var string Button label
|
|
|
71 |
*/
|
|
|
72 |
public $label = '';
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* @var array Button label's attributes
|
|
|
76 |
*/
|
|
|
77 |
public $labelattributes = [];
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* @var string Form submit method post or get
|
|
|
81 |
*/
|
|
|
82 |
public $method = 'get';
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* @var string Wrapping div class
|
|
|
86 |
*/
|
|
|
87 |
public $class = 'singleselect';
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* @var bool True if button disabled, false if normal
|
|
|
91 |
*/
|
|
|
92 |
public $disabled = false;
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* @var string Button tooltip
|
|
|
96 |
*/
|
|
|
97 |
public $tooltip = null;
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* @var string Form id
|
|
|
101 |
*/
|
|
|
102 |
public $formid = null;
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* @var help_icon The help icon for this element.
|
|
|
106 |
*/
|
|
|
107 |
public $helpicon = null;
|
|
|
108 |
|
|
|
109 |
/** @var component_action[] component action. */
|
|
|
110 |
public $actions = [];
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Constructor
|
|
|
114 |
* @param moodle_url $url form action target, includes hidden fields
|
|
|
115 |
* @param string $name name of selection field - the changing parameter in url
|
|
|
116 |
* @param array $options list of options
|
|
|
117 |
* @param string $selected selected element
|
|
|
118 |
* @param ?array $nothing
|
|
|
119 |
* @param string $formid
|
|
|
120 |
*/
|
|
|
121 |
public function __construct(
|
|
|
122 |
moodle_url $url,
|
|
|
123 |
$name,
|
|
|
124 |
array $options,
|
|
|
125 |
$selected = '',
|
|
|
126 |
$nothing = ['' => 'choosedots'],
|
|
|
127 |
$formid = null,
|
|
|
128 |
) {
|
|
|
129 |
$this->url = $url;
|
|
|
130 |
$this->name = $name;
|
|
|
131 |
$this->options = $options;
|
|
|
132 |
$this->selected = $selected;
|
|
|
133 |
$this->nothing = $nothing;
|
|
|
134 |
$this->formid = $formid;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
/**
|
|
|
138 |
* Shortcut for adding a JS confirm dialog when the button is clicked.
|
|
|
139 |
* The message must be a yes/no question.
|
|
|
140 |
*
|
|
|
141 |
* @param string $confirmmessage The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
|
|
|
142 |
*/
|
|
|
143 |
public function add_confirm_action($confirmmessage) {
|
|
|
144 |
$this->add_action(new component_action('submit', 'M.util.show_confirm_dialog', ['message' => $confirmmessage]));
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Add action to the button.
|
|
|
149 |
*
|
|
|
150 |
* @param component_action $action
|
|
|
151 |
*/
|
|
|
152 |
public function add_action(component_action $action) {
|
|
|
153 |
$this->actions[] = $action;
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Adds help icon.
|
|
|
158 |
*
|
|
|
159 |
* @param string $identifier The keyword that defines a help page
|
|
|
160 |
* @param string $component
|
|
|
161 |
*/
|
|
|
162 |
public function set_help_icon($identifier, $component = 'moodle') {
|
|
|
163 |
$this->helpicon = new help_icon($identifier, $component);
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Sets select's label
|
|
|
168 |
*
|
|
|
169 |
* @param string $label
|
|
|
170 |
* @param array $attributes (optional)
|
|
|
171 |
*/
|
|
|
172 |
public function set_label($label, $attributes = []) {
|
|
|
173 |
$this->label = $label;
|
|
|
174 |
$this->labelattributes = $attributes;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Export data.
|
|
|
179 |
*
|
|
|
180 |
* @param renderer_base $output Renderer.
|
|
|
181 |
* @return stdClass
|
|
|
182 |
*/
|
|
|
183 |
public function export_for_template(renderer_base $output) {
|
|
|
184 |
$attributes = $this->attributes;
|
|
|
185 |
|
|
|
186 |
$data = new stdClass();
|
|
|
187 |
$data->name = $this->name;
|
|
|
188 |
$data->method = $this->method;
|
|
|
189 |
$data->action = $this->method === 'get' ? $this->url->out_omit_querystring(true) : $this->url->out_omit_querystring();
|
|
|
190 |
$data->classes = $this->class;
|
|
|
191 |
$data->label = $this->label;
|
|
|
192 |
$data->disabled = $this->disabled;
|
|
|
193 |
$data->title = $this->tooltip;
|
|
|
194 |
$data->formid = !empty($this->formid) ? $this->formid : html_writer::random_id('single_select_f');
|
|
|
195 |
$data->id = !empty($attributes['id']) ? $attributes['id'] : html_writer::random_id('single_select');
|
|
|
196 |
|
|
|
197 |
// Select element attributes.
|
|
|
198 |
// Unset attributes that are already predefined in the template.
|
|
|
199 |
unset($attributes['id']);
|
|
|
200 |
unset($attributes['class']);
|
|
|
201 |
unset($attributes['name']);
|
|
|
202 |
unset($attributes['title']);
|
|
|
203 |
unset($attributes['disabled']);
|
|
|
204 |
|
|
|
205 |
// Map the attributes.
|
|
|
206 |
$data->attributes = array_map(function ($key) use ($attributes) {
|
|
|
207 |
return ['name' => $key, 'value' => $attributes[$key]];
|
|
|
208 |
}, array_keys($attributes));
|
|
|
209 |
|
|
|
210 |
// Form parameters.
|
|
|
211 |
$actionurl = new moodle_url($this->url);
|
|
|
212 |
if ($this->method === 'post') {
|
|
|
213 |
$actionurl->param('sesskey', sesskey());
|
|
|
214 |
}
|
|
|
215 |
$data->params = $actionurl->export_params_for_template();
|
|
|
216 |
|
|
|
217 |
// Select options.
|
|
|
218 |
$hasnothing = false;
|
|
|
219 |
if (is_string($this->nothing) && $this->nothing !== '') {
|
|
|
220 |
$nothing = ['' => $this->nothing];
|
|
|
221 |
$hasnothing = true;
|
|
|
222 |
$nothingkey = '';
|
|
|
223 |
} else if (is_array($this->nothing)) {
|
|
|
224 |
$nothingvalue = reset($this->nothing);
|
|
|
225 |
if ($nothingvalue === 'choose' || $nothingvalue === 'choosedots') {
|
|
|
226 |
$nothing = [key($this->nothing) => get_string('choosedots')];
|
|
|
227 |
} else {
|
|
|
228 |
$nothing = $this->nothing;
|
|
|
229 |
}
|
|
|
230 |
$hasnothing = true;
|
|
|
231 |
$nothingkey = key($this->nothing);
|
|
|
232 |
}
|
|
|
233 |
if ($hasnothing) {
|
|
|
234 |
$options = $nothing + $this->options;
|
|
|
235 |
} else {
|
|
|
236 |
$options = $this->options;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
foreach ($options as $value => $name) {
|
|
|
240 |
if (is_array($options[$value])) {
|
|
|
241 |
foreach ($options[$value] as $optgroupname => $optgroupvalues) {
|
|
|
242 |
$sublist = [];
|
|
|
243 |
foreach ($optgroupvalues as $optvalue => $optname) {
|
|
|
244 |
$option = [
|
|
|
245 |
'value' => $optvalue,
|
|
|
246 |
'name' => $optname,
|
|
|
247 |
'selected' => strval($this->selected) === strval($optvalue),
|
|
|
248 |
];
|
|
|
249 |
|
|
|
250 |
if ($hasnothing && $nothingkey === $optvalue) {
|
|
|
251 |
$option['ignore'] = 'data-ignore';
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
$sublist[] = $option;
|
|
|
255 |
}
|
|
|
256 |
$data->options[] = [
|
|
|
257 |
'name' => $optgroupname,
|
|
|
258 |
'optgroup' => true,
|
|
|
259 |
'options' => $sublist,
|
|
|
260 |
];
|
|
|
261 |
}
|
|
|
262 |
} else {
|
|
|
263 |
$option = [
|
|
|
264 |
'value' => $value,
|
|
|
265 |
'name' => $options[$value],
|
|
|
266 |
'selected' => strval($this->selected) === strval($value),
|
|
|
267 |
'optgroup' => false,
|
|
|
268 |
];
|
|
|
269 |
|
|
|
270 |
if ($hasnothing && $nothingkey === $value) {
|
|
|
271 |
$option['ignore'] = 'data-ignore';
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
$data->options[] = $option;
|
|
|
275 |
}
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
// Label attributes.
|
|
|
279 |
$data->labelattributes = [];
|
|
|
280 |
// Unset label attributes that are already in the template.
|
|
|
281 |
unset($this->labelattributes['for']);
|
|
|
282 |
// Map the label attributes.
|
|
|
283 |
foreach ($this->labelattributes as $key => $value) {
|
|
|
284 |
$data->labelattributes[] = ['name' => $key, 'value' => $value];
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
// Help icon.
|
|
|
288 |
$data->helpicon = !empty($this->helpicon) ? $this->helpicon->export_for_template($output) : false;
|
|
|
289 |
|
|
|
290 |
return $data;
|
|
|
291 |
}
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
// Alias this class to the old name.
|
|
|
295 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
296 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
297 |
class_alias(single_select::class, \single_select::class);
|