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 moodle_url;
|
|
|
21 |
use stdClass;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Simple URL selection widget description.
|
|
|
25 |
*
|
|
|
26 |
* @copyright 2009 Petr Skoda
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
* @since Moodle 2.0
|
|
|
29 |
* @package core
|
|
|
30 |
* @category output
|
|
|
31 |
*/
|
|
|
32 |
class url_select implements renderable, templatable {
|
|
|
33 |
/**
|
|
|
34 |
* @var array $urls associative array value=>label ex.: array(1=>'One, 2=>Two)
|
|
|
35 |
* it is also possible to specify optgroup as complex label array ex.:
|
|
|
36 |
* array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
|
|
|
37 |
* array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
|
|
|
38 |
*/
|
|
|
39 |
public $urls;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* @var string Selected option
|
|
|
43 |
*/
|
|
|
44 |
public $selected;
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* @var array Nothing selected
|
|
|
48 |
*/
|
|
|
49 |
public $nothing;
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* @var array Extra select field attributes
|
|
|
53 |
*/
|
|
|
54 |
public $attributes = [];
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* @var string Button label
|
|
|
58 |
*/
|
|
|
59 |
public $label = '';
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* @var array Button label's attributes
|
|
|
63 |
*/
|
|
|
64 |
public $labelattributes = [];
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* @var string Wrapping div class
|
|
|
68 |
*/
|
|
|
69 |
public $class = 'urlselect';
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* @var bool True if button disabled, false if normal
|
|
|
73 |
*/
|
|
|
74 |
public $disabled = false;
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* @var string Button tooltip
|
|
|
78 |
*/
|
|
|
79 |
public $tooltip = null;
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* @var string Form id
|
|
|
83 |
*/
|
|
|
84 |
public $formid = null;
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* @var help_icon The help icon for this element.
|
|
|
88 |
*/
|
|
|
89 |
public $helpicon = null;
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* @var string If set, makes button visible with given name for button
|
|
|
93 |
*/
|
|
|
94 |
public $showbutton = null;
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* @var array $disabledoptions array of disabled options
|
|
|
98 |
*/
|
|
|
99 |
public $disabledoptions = [];
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Constructor
|
|
|
103 |
* @param array $urls list of options
|
|
|
104 |
* @param string $selected selected element
|
|
|
105 |
* @param array $nothing
|
|
|
106 |
* @param string $formid
|
|
|
107 |
* @param string $showbutton Set to text of button if it should be visible
|
|
|
108 |
* or null if it should be hidden (hidden version always has text 'go')
|
|
|
109 |
*/
|
|
|
110 |
public function __construct(array $urls, $selected = '', $nothing = ['' => 'choosedots'], $formid = null, $showbutton = null) {
|
|
|
111 |
$this->urls = $urls;
|
|
|
112 |
$this->selected = $selected;
|
|
|
113 |
$this->nothing = $nothing;
|
|
|
114 |
$this->formid = $formid;
|
|
|
115 |
$this->showbutton = $showbutton;
|
|
|
116 |
$this->disabledoptions = [];
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Disable the option(url).
|
|
|
121 |
*
|
|
|
122 |
* @param string $urlkey
|
|
|
123 |
* @param bool $disabled
|
|
|
124 |
*/
|
|
|
125 |
public function set_option_disabled(string $urlkey, bool $disabled = true) {
|
|
|
126 |
$this->disabledoptions[$urlkey] = $disabled;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Adds help icon.
|
|
|
131 |
*
|
|
|
132 |
* @param string $identifier The keyword that defines a help page
|
|
|
133 |
* @param string $component
|
|
|
134 |
*/
|
|
|
135 |
public function set_help_icon($identifier, $component = 'moodle') {
|
|
|
136 |
$this->helpicon = new help_icon($identifier, $component);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Sets select's label
|
|
|
141 |
*
|
|
|
142 |
* @param string $label
|
|
|
143 |
* @param array $attributes (optional)
|
|
|
144 |
*/
|
|
|
145 |
public function set_label($label, $attributes = []) {
|
|
|
146 |
$this->label = $label;
|
|
|
147 |
$this->labelattributes = $attributes;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Clean a URL.
|
|
|
152 |
*
|
|
|
153 |
* @param string $value The URL.
|
|
|
154 |
* @return string The cleaned URL.
|
|
|
155 |
*/
|
|
|
156 |
protected function clean_url($value) {
|
|
|
157 |
global $CFG;
|
|
|
158 |
|
|
|
159 |
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedIf
|
|
|
160 |
if (empty($value)) {
|
|
|
161 |
// Nothing.
|
|
|
162 |
} else if (strpos($value, $CFG->wwwroot . '/') === 0) {
|
|
|
163 |
$value = str_replace($CFG->wwwroot, '', $value);
|
|
|
164 |
} else if (strpos($value, '/') !== 0) {
|
|
|
165 |
debugging("Invalid url_select urls parameter: url '$value' is not local relative url!", DEBUG_DEVELOPER);
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
return $value;
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
* Flatten the options for Mustache.
|
|
|
173 |
*
|
|
|
174 |
* This also cleans the URLs.
|
|
|
175 |
*
|
|
|
176 |
* @param array $options The options.
|
|
|
177 |
* @param array $nothing The nothing option.
|
|
|
178 |
* @return array
|
|
|
179 |
*/
|
|
|
180 |
protected function flatten_options($options, $nothing) {
|
|
|
181 |
$flattened = [];
|
|
|
182 |
|
|
|
183 |
foreach ($options as $value => $option) {
|
|
|
184 |
if (is_array($option)) {
|
|
|
185 |
foreach ($option as $groupname => $optoptions) {
|
|
|
186 |
if (!isset($flattened[$groupname])) {
|
|
|
187 |
$flattened[$groupname] = [
|
|
|
188 |
'name' => $groupname,
|
|
|
189 |
'isgroup' => true,
|
|
|
190 |
'options' => [],
|
|
|
191 |
];
|
|
|
192 |
}
|
|
|
193 |
foreach ($optoptions as $optvalue => $optoption) {
|
|
|
194 |
$cleanedvalue = $this->clean_url($optvalue);
|
|
|
195 |
$flattened[$groupname]['options'][$cleanedvalue] = [
|
|
|
196 |
'name' => $optoption,
|
|
|
197 |
'value' => $cleanedvalue,
|
|
|
198 |
'selected' => $this->selected == $optvalue,
|
|
|
199 |
'disabled' => $this->disabledoptions[$optvalue] ?? false,
|
|
|
200 |
];
|
|
|
201 |
}
|
|
|
202 |
}
|
|
|
203 |
} else {
|
|
|
204 |
$cleanedvalue = $this->clean_url($value);
|
|
|
205 |
$flattened[$cleanedvalue] = [
|
|
|
206 |
'name' => $option,
|
|
|
207 |
'value' => $cleanedvalue,
|
|
|
208 |
'selected' => $this->selected == $value,
|
|
|
209 |
'disabled' => $this->disabledoptions[$value] ?? false,
|
|
|
210 |
];
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
if (!empty($nothing)) {
|
|
|
215 |
$value = key($nothing);
|
|
|
216 |
$name = reset($nothing);
|
|
|
217 |
$flattened = [
|
|
|
218 |
$value => ['name' => $name, 'value' => $value, 'selected' => $this->selected == $value],
|
|
|
219 |
] + $flattened;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
// Make non-associative array.
|
|
|
223 |
foreach ($flattened as $key => $value) {
|
|
|
224 |
if (!empty($value['options'])) {
|
|
|
225 |
$flattened[$key]['options'] = array_values($value['options']);
|
|
|
226 |
}
|
|
|
227 |
}
|
|
|
228 |
$flattened = array_values($flattened);
|
|
|
229 |
|
|
|
230 |
return $flattened;
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
/**
|
|
|
234 |
* Export for template.
|
|
|
235 |
*
|
|
|
236 |
* @param renderer_base $output Renderer.
|
|
|
237 |
* @return stdClass
|
|
|
238 |
*/
|
|
|
239 |
public function export_for_template(renderer_base $output) {
|
|
|
240 |
$attributes = $this->attributes;
|
|
|
241 |
|
|
|
242 |
$data = new stdClass();
|
|
|
243 |
$data->formid = !empty($this->formid) ? $this->formid : html_writer::random_id('url_select_f');
|
|
|
244 |
$data->classes = $this->class;
|
|
|
245 |
$data->label = $this->label;
|
|
|
246 |
$data->disabled = $this->disabled;
|
|
|
247 |
$data->title = $this->tooltip;
|
|
|
248 |
$data->id = !empty($attributes['id']) ? $attributes['id'] : html_writer::random_id('url_select');
|
|
|
249 |
$data->sesskey = sesskey();
|
|
|
250 |
$data->action = (new moodle_url('/course/jumpto.php'))->out(false);
|
|
|
251 |
|
|
|
252 |
// Remove attributes passed as property directly.
|
|
|
253 |
unset($attributes['class']);
|
|
|
254 |
unset($attributes['id']);
|
|
|
255 |
unset($attributes['name']);
|
|
|
256 |
unset($attributes['title']);
|
|
|
257 |
unset($attributes['disabled']);
|
|
|
258 |
|
|
|
259 |
$data->showbutton = $this->showbutton;
|
|
|
260 |
|
|
|
261 |
// Select options.
|
|
|
262 |
$nothing = false;
|
|
|
263 |
if (is_string($this->nothing) && $this->nothing !== '') {
|
|
|
264 |
$nothing = ['' => $this->nothing];
|
|
|
265 |
} else if (is_array($this->nothing)) {
|
|
|
266 |
$nothingvalue = reset($this->nothing);
|
|
|
267 |
if ($nothingvalue === 'choose' || $nothingvalue === 'choosedots') {
|
|
|
268 |
$nothing = [key($this->nothing) => get_string('choosedots')];
|
|
|
269 |
} else {
|
|
|
270 |
$nothing = $this->nothing;
|
|
|
271 |
}
|
|
|
272 |
}
|
|
|
273 |
$data->options = $this->flatten_options($this->urls, $nothing);
|
|
|
274 |
|
|
|
275 |
// Label attributes.
|
|
|
276 |
$data->labelattributes = [];
|
|
|
277 |
// Unset label attributes that are already in the template.
|
|
|
278 |
unset($this->labelattributes['for']);
|
|
|
279 |
// Map the label attributes.
|
|
|
280 |
foreach ($this->labelattributes as $key => $value) {
|
|
|
281 |
$data->labelattributes[] = ['name' => $key, 'value' => $value];
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
// Help icon.
|
|
|
285 |
$data->helpicon = !empty($this->helpicon) ? $this->helpicon->export_for_template($output) : false;
|
|
|
286 |
|
|
|
287 |
// Finally all the remaining attributes.
|
|
|
288 |
$data->attributes = [];
|
|
|
289 |
foreach ($attributes as $key => $value) {
|
|
|
290 |
$data->attributes[] = ['name' => $key, 'value' => $value];
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
return $data;
|
|
|
294 |
}
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
// Alias this class to the old name.
|
|
|
298 |
// This file will be autoloaded by the legacyclasses autoload system.
|
|
|
299 |
// In future all uses of this class will be corrected and the legacy references will be removed.
|
|
|
300 |
class_alias(url_select::class, \url_select::class);
|