Merge branch 'MDL-68343-master' of git://github.com/rezaies/moodle
[moodle.git] / lib / outputcomponents.php
</
CommitLineData
d9c8f425 1<?php
d9c8f425 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/**
18 * Classes representing HTML elements, used by $OUTPUT methods
19 *
20 * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
21 * for an overview.
22 *
f8129210 23 * @package core
76be40cc 24 * @category output
9678c7b8
SH
25 * @copyright 2009 Tim Hunt
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d9c8f425 27 */
28
78bfb562 29defined('MOODLE_INTERNAL') || die();
5d0c95a5
PS
30
31/**
32 * Interface marking other classes as suitable for renderer_base::render()
3d3fae72 33 *
9678c7b8 34 * @copyright 2010 Petr Skoda (skodak) info@skodak.org
f8129210 35 * @package core
76be40cc 36 * @category output
5d0c95a5
PS
37 */
38interface renderable {
39 // intentionally empty
40}
41
9bdcf579
DW
42/**
43 * Interface marking other classes having the ability to export their data for use by templates.
44 *
45 * @copyright 2015 Damyon Wiese
46 * @package core
47 * @category output
48 * @since 2.9
49 */
50interface templatable {
51
52 /**
53 * Function to export the renderer data in a format that is suitable for a
54 * mustache template. This means:
55 * 1. No complex types - only stdClass, array, int, string, float, bool
56 * 2. Any additional info that is required for the template is pre-calculated (e.g. capability checks).
57 *
58 * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
59 * @return stdClass|array
60 */
61 public function export_for_template(renderer_base $output);
62}
63
bb496de7
DC
64/**
65 * Data structure representing a file picker.
66 *
67 * @copyright 2010 Dongsheng Cai
9678c7b8
SH
68 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
69 * @since Moodle 2.0
f8129210 70 * @package core
76be40cc 71 * @category output
bb496de7
DC
72 */
73class file_picker implements renderable {
9678c7b8
SH
74
75 /**
76be40cc 76 * @var stdClass An object containing options for the file picker
9678c7b8 77 */
bb496de7 78 public $options;
9678c7b8
SH
79
80 /**
81 * Constructs a file picker object.
82 *
83 * The following are possible options for the filepicker:
84 * - accepted_types (*)
85 * - return_types (FILE_INTERNAL)
86 * - env (filepicker)
87 * - client_id (uniqid)
88 * - itemid (0)
89 * - maxbytes (-1)
90 * - maxfiles (1)
91 * - buttonname (false)
92 *
93 * @param stdClass $options An object containing options for the file picker.
94 */
bb496de7
DC
95 public function __construct(stdClass $options) {
96 global $CFG, $USER, $PAGE;
97 require_once($CFG->dirroot. '/repository/lib.php');
98 $defaults = array(
99 'accepted_types'=>'*',
bb496de7
DC
100 'return_types'=>FILE_INTERNAL,
101 'env' => 'filepicker',
102 'client_id' => uniqid(),
103 'itemid' => 0,
104 'maxbytes'=>-1,
105 'maxfiles'=>1,
f50a61fb 106 'buttonname'=>false
bb496de7
DC
107 );
108 foreach ($defaults as $key=>$value) {
109 if (empty($options->$key)) {
110 $options->$key = $value;
111 }
112 }
113
114 $options->currentfile = '';
115 if (!empty($options->itemid)) {
116 $fs = get_file_storage();
b0c6dc1c 117 $usercontext = context_user::instance($USER->id);
e4256380 118 if (empty($options->filename)) {
64f93798 119 if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id DESC', false)) {
e4256380
DC
120 $file = reset($files);
121 }
122 } else {
64f93798 123 $file = $fs->get_file($usercontext->id, 'user', 'draft', $options->itemid, $options->filepath, $options->filename);
e4256380
DC
124 }
125 if (!empty($file)) {
ee9a4962 126 $options->currentfile = html_writer::link(moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()), $file->get_filename());
bb496de7
DC
127 }
128 }
129
bb496de7
DC
130 // initilise options, getting files in root path
131 $this->options = initialise_filepicker($options);
132
133 // copying other options
134 foreach ($options as $name=>$value) {
98e7ae63
DC
135 if (!isset($this->options->$name)) {
136 $this->options->$name = $value;
137 }
bb496de7
DC
138 }
139 }
140}
141
5d0c95a5 142/**
bf11293a 143 * Data structure representing a user picture.
5d0c95a5
PS
144 *
145 * @copyright 2009 Nicolas Connault, 2010 Petr Skoda
9678c7b8
SH
146 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
147 * @since Modle 2.0
f8129210 148 * @package core
76be40cc 149 * @category output
5d0c95a5
PS
150 */
151class user_picture implements renderable {
152 /**
76be40cc
SH
153 * @var array List of mandatory fields in user record here. (do not include
154 * TEXT columns because it would break SELECT DISTINCT in MSSQL and ORACLE)
5d0c95a5 155 */
a327f25e
AG
156 protected static $fields = array('id', 'picture', 'firstname', 'lastname', 'firstnamephonetic', 'lastnamephonetic',
157 'middlename', 'alternatename', 'imagealt', 'email');
5d0c95a5
PS
158
159 /**
76be40cc
SH
160 * @var stdClass A user object with at least fields all columns specified
161 * in $fields array constant set.
5d0c95a5
PS
162 */
163 public $user;
9678c7b8 164
5d0c95a5 165 /**
76be40cc
SH
166 * @var int The course id. Used when constructing the link to the user's
167 * profile, page course id used if not specified.
5d0c95a5
PS
168 */
169 public $courseid;
9678c7b8 170
5d0c95a5 171 /**
76be40cc 172 * @var bool Add course profile link to image
5d0c95a5
PS
173 */
174 public $link = true;
9678c7b8 175
5d0c95a5 176 /**
76be40cc
SH
177 * @var int Size in pixels. Special values are (true/1 = 100px) and
178 * (false/0 = 35px)
9678c7b8 179 * for backward compatibility.
5d0c95a5
PS
180 */
181 public $size = 35;
9678c7b8 182
5d0c95a5 183 /**
3d3fae72 184 * @var bool Add non-blank alt-text to the image.
5d0c95a5
PS
185 * Default true, set to false when image alt just duplicates text in screenreaders.
186 */
187 public $alttext = true;
9678c7b8 188
5d0c95a5 189 /**
3d3fae72 190 * @var bool Whether or not to open the link in a popup window.
5d0c95a5
PS
191 */
192 public $popup = false;
9678c7b8 193
5d0c95a5 194 /**
76be40cc 195 * @var string Image class attribute
5d0c95a5
PS
196 */
197 public $class = 'userpicture';
198
e4a1efcb
JC
199 /**
200 * @var bool Whether to be visible to screen readers.
201 */
202 public $visibletoscreenreaders = true;
203
3b1eba06
JP
204 /**
205 * @var bool Whether to include the fullname in the user picture link.
206 */
207 public $includefullname = false;
208
88d89512 209 /**
26e778d1
JL
210 * @var mixed Include user authentication token. True indicates to generate a token for current user, and integer value
211 * indicates to generate a token for the user whose id is the value indicated.
88d89512
AN
212 */
213 public $includetoken = false;
214
5d0c95a5
PS
215 /**
216 * User picture constructor.
217 *
9678c7b8 218 * @param stdClass $user user record with at least id, picture, imagealt, firstname and lastname set.
faa328c3 219 * It is recommended to add also contextid of the user for performance reasons.
5d0c95a5
PS
220 */
221 public function __construct(stdClass $user) {
222 global $DB;
223
5d0c95a5
PS
224 if (empty($user->id)) {
225 throw new coding_exception('User id is required when printing user avatar image.');
226 }
227
228 // only touch the DB if we are missing data and complain loudly...
229 $needrec = false;
3a11c09f 230 foreach (self::$fields as $field) {
f4feabb8 231 if (!property_exists($user, $field)) {
5d0c95a5
PS
232 $needrec = true;
233 debugging('Missing '.$field.' property in $user object, this is a performance problem that needs to be fixed by a developer. '
234 .'Please use user_picture::fields() to get the full list of required fields.', DEBUG_DEVELOPER);
235 break;
236 }
237 }
238
239 if ($needrec) {
3a11c09f 240 $this->user = $DB->get_record('user', array('id'=>$user->id), self::fields(), MUST_EXIST);
5d0c95a5
PS
241 } else {
242 $this->user = clone($user);
243 }
244 }
245
246 /**
1a10840e 247 * Returns a list of required user fields, useful when fetching required user info from db.
f3afba4e
PS
248 *
249 * In some cases we have to fetch the user data together with some other information,
250 * the idalias is useful there because the id would otherwise override the main
251 * id of the result record. Please note it has to be converted back to id before rendering.
252 *
5d0c95a5 253 * @param string $tableprefix name of database table prefix in query
3a11c09f 254 * @param array $extrafields extra fields to be included in result (do not include TEXT columns because it would break SELECT DISTINCT in MSSQL and ORACLE)
f3afba4e 255 * @param string $idalias alias of id field
9958e561 256 * @param string $fieldprefix prefix to add to all columns in their aliases, does not apply to 'id'
5d0c95a5
PS
257 * @return string
258 */
9958e561 259 public static function fields($tableprefix = '', array $extrafields = NULL, $idalias = 'id', $fieldprefix = '') {
3a11c09f
PS
260 if (!$tableprefix and !$extrafields and !$idalias) {
261 return implode(',', self::$fields);
5d0c95a5 262 }
3a11c09f
PS
263 if ($tableprefix) {
264 $tableprefix .= '.';
265 }
3a11c09f
PS
266 foreach (self::$fields as $field) {
267 if ($field === 'id' and $idalias and $idalias !== 'id') {
6f7b89e2 268 $fields[$field] = "$tableprefix$field AS $idalias";
3a11c09f 269 } else {
9958e561
DM
270 if ($fieldprefix and $field !== 'id') {
271 $fields[$field] = "$tableprefix$field AS $fieldprefix$field";
272 } else {
273 $fields[$field] = "$tableprefix$field";
274 }
3a11c09f
PS
275 }
276 }
277 // add extra fields if not already there
278 if ($extrafields) {
279 foreach ($extrafields as $e) {
280 if ($e === 'id' or isset($fields[$e])) {
281 continue;
282 }
5c0d03ea
DM
283 if ($fieldprefix) {
284 $fields[$e] = "$tableprefix$e AS $fieldprefix$e";
285 } else {
286 $fields[$e] = "$tableprefix$e";
287 }
f3afba4e 288 }
f3afba4e
PS
289 }
290 return implode(',', $fields);
5d0c95a5 291 }
5d0c95a5 292
5c0d03ea
DM
293 /**
294 * Extract the aliased user fields from a given record
295 *
296 * Given a record that was previously obtained using {@link self::fields()} with aliases,
297 * this method extracts user related unaliased fields.
298 *
299 * @param stdClass $record containing user picture fields
300 * @param array $extrafields extra fields included in the $record
301 * @param string $idalias alias of the id field
302 * @param string $fieldprefix prefix added to all columns in their aliases, does not apply to 'id'
303 * @return stdClass object with unaliased user fields
304 */
9678c7b8 305 public static function unalias(stdClass $record, array $extrafields = null, $idalias = 'id', $fieldprefix = '') {
5c0d03ea
DM
306
307 if (empty($idalias)) {
308 $idalias = 'id';
309 }
310
311 $return = new stdClass();
312
313 foreach (self::$fields as $field) {
314 if ($field === 'id') {
9ecbf801 315 if (property_exists($record, $idalias)) {
5c0d03ea
DM
316 $return->id = $record->{$idalias};
317 }
318 } else {
9ecbf801 319 if (property_exists($record, $fieldprefix.$field)) {
5c0d03ea
DM
320 $return->{$field} = $record->{$fieldprefix.$field};
321 }
322 }
323 }
324 // add extra fields if not already there
325 if ($extrafields) {
326 foreach ($extrafields as $e) {
9ecbf801 327 if ($e === 'id' or property_exists($return, $e)) {
5c0d03ea
DM
328 continue;
329 }
330 $return->{$e} = $record->{$fieldprefix.$e};
331 }
332 }
333
334 return $return;
871a3ec5
SH
335 }
336
337 /**
338 * Works out the URL for the users picture.
339 *
340 * This method is recommended as it avoids costly redirects of user pictures
341 * if requests are made for non-existent files etc.
342 *
f8129210 343 * @param moodle_page $page
871a3ec5
SH
344 * @param renderer_base $renderer
345 * @return moodle_url
346 */
347 public function get_url(moodle_page $page, renderer_base $renderer = null) {
33dca156 348 global $CFG;
871a3ec5
SH
349
350 if (is_null($renderer)) {
351 $renderer = $page->get_renderer('core');
352 }
353
871a3ec5
SH
354 // Sort out the filename and size. Size is only required for the gravatar
355 // implementation presently.
356 if (empty($this->size)) {
357 $filename = 'f2';
358 $size = 35;
359 } else if ($this->size === true or $this->size == 1) {
360 $filename = 'f1';
361 $size = 100;
33d9f44b
PS
362 } else if ($this->size > 100) {
363 $filename = 'f3';
364 $size = (int)$this->size;
871a3ec5
SH
365 } else if ($this->size >= 50) {
366 $filename = 'f1';
367 $size = (int)$this->size;
368 } else {
369 $filename = 'f2';
370 $size = (int)$this->size;
371 }
372
663640f5 373 $defaulturl = $renderer->image_url('u/'.$filename); // default image
faa328c3
PS
374
375 if ((!empty($CFG->forcelogin) and !isloggedin()) ||
376 (!empty($CFG->forceloginforprofileimage) && (!isloggedin() || isguestuser()))) {
377 // Protect images if login required and not logged in;
378 // also if login is required for profile images and is not logged in or guest
379 // do not use require_login() because it is expensive and not suitable here anyway.
380 return $defaulturl;
03636668 381 }
4125bdc1 382
faa328c3
PS
383 // First try to detect deleted users - but do not read from database for performance reasons!
384 if (!empty($this->user->deleted) or strpos($this->user->email, '@') === false) {
385 // All deleted users should have email replaced by md5 hash,
386 // all active users are expected to have valid email.
387 return $defaulturl;
388 }
389
390 // Did the user upload a picture?
4d254790 391 if ($this->user->picture > 0) {
faa328c3
PS
392 if (!empty($this->user->contextid)) {
393 $contextid = $this->user->contextid;
394 } else {
395 $context = context_user::instance($this->user->id, IGNORE_MISSING);
396 if (!$context) {
397 // This must be an incorrectly deleted user, all other users have context.
398 return $defaulturl;
399 }
400 $contextid = $context->id;
401 }
402
871a3ec5
SH
403 $path = '/';
404 if (clean_param($page->theme->name, PARAM_THEME) == $page->theme->name) {
405 // We append the theme name to the file path if we have it so that
406 // in the circumstance that the profile picture is not available
407 // when the user actually requests it they still get the profile
408 // picture for the correct theme.
409 $path .= $page->theme->name.'/';
410 }
faa328c3 411 // Set the image URL to the URL for the uploaded file and return.
88d89512
AN
412 $url = moodle_url::make_pluginfile_url(
413 $contextid, 'user', 'icon', null, $path, $filename, false, $this->includetoken);
4d254790
PS
414 $url->param('rev', $this->user->picture);
415 return $url;
faa328c3
PS
416 }
417
4d254790 418 if ($this->user->picture == 0 and !empty($CFG->enablegravatar)) {
4125bdc1
SH
419 // Normalise the size variable to acceptable bounds
420 if ($size < 1 || $size > 512) {
871a3ec5
SH
421 $size = 35;
422 }
4125bdc1 423 // Hash the users email address
871a3ec5 424 $md5 = md5(strtolower(trim($this->user->email)));
4125bdc1 425 // Build a gravatar URL with what we know.
f127b1a5
MA
426
427 // Find the best default image URL we can (MDL-35669)
428 if (empty($CFG->gravatardefaulturl)) {
429 $absoluteimagepath = $page->theme->resolve_image_location('u/'.$filename, 'core');
430 if (strpos($absoluteimagepath, $CFG->dirroot) === 0) {
431 $gravatardefault = $CFG->wwwroot . substr($absoluteimagepath, strlen($CFG->dirroot));
432 } else {
433 $gravatardefault = $CFG->wwwroot . '/pix/u/' . $filename . '.png';
434 }
435 } else {
436 $gravatardefault = $CFG->gravatardefaulturl;
437 }
438
4125bdc1
SH
439 // If the currently requested page is https then we'll return an
440 // https gravatar page.
1e31f118 441 if (is_https()) {
f127b1a5 442 return new moodle_url("https://secure.gravatar.com/avatar/{$md5}", array('s' => $size, 'd' => $gravatardefault));
4125bdc1 443 } else {
f127b1a5 444 return new moodle_url("http://www.gravatar.com/avatar/{$md5}", array('s' => $size, 'd' => $gravatardefault));
4125bdc1 445 }
871a3ec5
SH
446 }
447
faa328c3 448 return $defaulturl;
5c0d03ea
DM
449 }
450}
bf11293a 451
49f0d481
PS
452/**
453 * Data structure representing a help icon.
454 *
455 * @copyright 2010 Petr Skoda (info@skodak.org)
9678c7b8
SH
456 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
457 * @since Moodle 2.0
f8129210 458 * @package core
76be40cc 459 * @category output
49f0d481 460 */
b7e95263 461class help_icon implements renderable, templatable {
9678c7b8 462
49f0d481 463 /**
76be40cc 464 * @var string lang pack identifier (without the "_help" suffix),
9678c7b8
SH
465 * both get_string($identifier, $component) and get_string($identifier.'_help', $component)
466 * must exist.
49f0d481
PS
467 */
468 public $identifier;
9678c7b8 469
49f0d481 470 /**
76be40cc 471 * @var string Component name, the same as in get_string()
49f0d481
PS
472 */
473 public $component;
9678c7b8 474
49f0d481 475 /**
76be40cc 476 * @var string Extra descriptive text next to the icon
49f0d481
PS
477 */
478 public $linktext = null;
479
480 /**
481 * Constructor
9678c7b8
SH
482 *
483 * @param string $identifier string for help page title,
5435c9dc
MD
484 * string with _help suffix is used for the actual help text.
485 * string with _link suffix is used to create a link to further info (if it exists)
49f0d481
PS
486 * @param string $component
487 */
259c165d
PS
488 public function __construct($identifier, $component) {
489 $this->identifier = $identifier;
49f0d481
PS
490 $this->component = $component;
491 }
259c165d
PS
492
493 /**
494 * Verifies that both help strings exists, shows debug warnings if not
495 */
496 public function diag_strings() {
497 $sm = get_string_manager();
498 if (!$sm->string_exists($this->identifier, $this->component)) {
499 debugging("Help title string does not exist: [$this->identifier, $this->component]");
500 }
5435c9dc 501 if (!$sm->string_exists($this->identifier.'_help', $this->component)) {
876521ac 502 debugging("Help contents string does not exist: [{$this->identifier}_help, $this->component]");
259c165d
PS
503 }
504 }
b7e95263
DW
505
506 /**
507 * Export this data so it can be used as the context for a mustache template.
508 *
509 * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
510 * @return array
511 */
512 public function export_for_template(renderer_base $output) {
a84eadb8
FM
513 global $CFG;
514
b7e95263
DW
515 $title = get_string($this->identifier, $this->component);
516
517 if (empty($this->linktext)) {
518 $alt = get_string('helpprefix2', '', trim($title, ". \t"));
519 } else {
520 $alt = get_string('helpwiththis');
521 }
522
523 $data = get_formatted_help_string($this->identifier, $this->component, false);
a84eadb8 524
b7e95263 525 $data->alt = $alt;
a84eadb8
FM
526 $data->icon = (new pix_icon('help', $alt, 'core', ['class' => 'iconhelp']))->export_for_template($output);
527 $data->linktext = $this->linktext;
528 $data->title = get_string('helpprefix2', '', trim($title, ". \t"));
1bfed23b
LB
529
530 $options = [
a84eadb8
FM
531 'component' => $this->component,
532 'identifier' => $this->identifier,
533 'lang' => current_language()
1bfed23b
LB
534 ];
535
536 // Debugging feature lets you display string identifier and component.
537 if (isset($CFG->debugstringids) && $CFG->debugstringids && optional_param('strings', 0, PARAM_INT)) {
538 $options['strings'] = 1;
539 }
b7e95263 540
1bfed23b 541 $data->url = (new moodle_url('/help.php', $options))->out(false);
b7e95263
DW
542 $data->ltr = !right_to_left();
543 return $data;
544 }
49f0d481
PS
545}
546
bf11293a 547
95b06c13
DW
548/**
549 * Data structure representing an icon font.
550 *
551 * @copyright 2016 Damyon Wiese
552 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
553 * @package core
554 * @category output
555 */
556class pix_icon_font implements templatable {
557
558 /**
559 * @var pix_icon $pixicon The original icon.
560 */
561 private $pixicon = null;
562
563 /**
564 * @var string $key The mapped key.
565 */
566 private $key;
567
8626dc99
DW
568 /**
569 * @var bool $mapped The icon could not be mapped.
570 */
571 private $mapped;
572
95b06c13
DW
573 /**
574 * Constructor
575 *
576 * @param pix_icon $pixicon The original icon
577 */
578 public function __construct(pix_icon $pixicon) {
579 global $PAGE;
580
581 $this->pixicon = $pixicon;
8626dc99 582 $this->mapped = false;
95b06c13
DW
583 $iconsystem = \core\output\icon_system::instance();
584
585 $this->key = $iconsystem->remap_icon_name($pixicon->pix, $pixicon->component);
8626dc99
DW
586 if (!empty($this->key)) {
587 $this->mapped = true;
95b06c13
DW
588 }
589 }
590
8626dc99
DW
591 /**
592 * Return true if this pix_icon was successfully mapped to an icon font.
593 *
594 * @return bool
595 */
596 public function is_mapped() {
597 return $this->mapped;
598 }
599
95b06c13
DW
600 /**
601 * Export this data so it can be used as the context for a mustache template.
602 *
603 * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
604 * @return array
605 */
606 public function export_for_template(renderer_base $output) {
607
608 $pixdata = $this->pixicon->export_for_template($output);
609
8857c715 610 $title = isset($this->pixicon->attributes['title']) ? $this->pixicon->attributes['title'] : '';
1caabd84 611 $alt = isset($this->pixicon->attributes['alt']) ? $this->pixicon->attributes['alt'] : '';
95b06c13 612 if (empty($title)) {
1caabd84 613 $title = $alt;
95b06c13
DW
614 }
615 $data = array(
616 'extraclasses' => $pixdata['extraclasses'],
617 'title' => $title,
1caabd84 618 'alt' => $alt,
95b06c13
DW
619 'key' => $this->key
620 );
621
622 return $data;
623 }
624}
625
626/**
627 * Data structure representing an icon subtype.
628 *
629 * @copyright 2016 Damyon Wiese
630 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
631 * @package core
632 * @category output
633 */
634class pix_icon_fontawesome extends pix_icon_font {
635
636}
637
000c278c
PS
638/**
639 * Data structure representing an icon.
640 *
641 * @copyright 2010 Petr Skoda
9678c7b8
SH
642 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
643 * @since Moodle 2.0
f8129210 644 * @package core
76be40cc 645 * @category output
000c278c 646 */
eb42bb7e 647class pix_icon implements renderable, templatable {
9678c7b8
SH
648
649 /**
76be40cc 650 * @var string The icon name
9678c7b8 651 */
000c278c 652 var $pix;
9678c7b8
SH
653
654 /**
76be40cc 655 * @var string The component the icon belongs to.
9678c7b8 656 */
000c278c 657 var $component;
9678c7b8
SH
658
659 /**
76be40cc 660 * @var array An array of attributes to use on the icon
9678c7b8 661 */
000c278c
PS
662 var $attributes = array();
663
664 /**
665 * Constructor
9678c7b8 666 *
000c278c 667 * @param string $pix short icon name
7749e187 668 * @param string $alt The alt text to use for the icon
000c278c
PS
669 * @param string $component component name
670 * @param array $attributes html attributes
671 */
672 public function __construct($pix, $alt, $component='moodle', array $attributes = null) {
a26ce248
DW
673 global $PAGE;
674
95b06c13 675 $this->pix = $pix;
c80877aa 676 $this->component = $component;
000c278c
PS
677 $this->attributes = (array)$attributes;
678
000c278c 679 if (empty($this->attributes['class'])) {
95b06c13 680 $this->attributes['class'] = '';
000c278c 681 }
0e55333a 682
750bbb68
DM
683 // Set an additional class for big icons so that they can be styled properly.
684 if (substr($pix, 0, 2) === 'b/') {
685 $this->attributes['class'] .= ' iconsize-big';
686 }
687
0e55333a
EM
688 // If the alt is empty, don't place it in the attributes, otherwise it will override parent alt text.
689 if (!is_null($alt)) {
690 $this->attributes['alt'] = $alt;
691
692 // If there is no title, set it to the attribute.
693 if (!isset($this->attributes['title'])) {
694 $this->attributes['title'] = $this->attributes['alt'];
695 }
696 } else {
697 unset($this->attributes['alt']);
698 }
699
700 if (empty($this->attributes['title'])) {
aad982aa
FM
701 // Remove the title attribute if empty, we probably want to use the parent node's title
702 // and some browsers might overwrite it with an empty title.
703 unset($this->attributes['title']);
000c278c 704 }
ef96eb02
DW
705
706 // Hide icons from screen readers that have no alt.
707 if (empty($this->attributes['alt'])) {
708 $this->attributes['aria-hidden'] = 'true';
709 }
000c278c 710 }
eb42bb7e
DC
711
712 /**
713 * Export this data so it can be used as the context for a mustache template.
714 *
715 * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
716 * @return array
717 */
718 public function export_for_template(renderer_base $output) {
719 $attributes = $this->attributes;
a26ce248
DW
720 $extraclasses = '';
721
722 foreach ($attributes as $key => $item) {
723 if ($key == 'class') {
724 $extraclasses = $item;
725 unset($attributes[$key]);
726 break;
727 }
728 }
729
663640f5 730 $attributes['src'] = $output->image_url($this->pix, $this->component)->out(false);
eb42bb7e
DC
731 $templatecontext = array();
732 foreach ($attributes as $name => $value) {
733 $templatecontext[] = array('name' => $name, 'value' => $value);
734 }
a26ce248 735 $title = isset($attributes['title']) ? $attributes['title'] : '';
ec7b5522
DW
736 if (empty($title)) {
737 $title = isset($attributes['alt']) ? $attributes['alt'] : '';
738 }
a26ce248
DW
739 $data = array(
740 'attributes' => $templatecontext,
95b06c13 741 'extraclasses' => $extraclasses
a26ce248 742 );
eb42bb7e
DC
743
744 return $data;
745 }
7cf6ef7b
DW
746
747 /**
748 * Much simpler version of export that will produce the data required to render this pix with the
749 * pix helper in a mustache tag.
750 *
751 * @return array
752 */
753 public function export_for_pix() {
754 $title = isset($this->attributes['title']) ? $this->attributes['title'] : '';
755 if (empty($title)) {
756 $title = isset($this->attributes['alt']) ? $this->attributes['alt'] : '';
757 }
758 return [
759 'key' => $this->pix,
760 'component' => $this->component,
761 'title' => $title
762 ];
763 }
000c278c
PS
764}
765
c2dde7ee
DW
766/**
767 * Data structure representing an activity icon.
768 *
769 * The difference is that activity icons will always render with the standard icon system (no font icons).
770 *
771 * @copyright 2017 Damyon Wiese
772 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
773 * @package core
774 */
3e6adcd6 775class image_icon extends pix_icon {
c2dde7ee
DW
776}
777
d63c5073
DM
778/**
779 * Data structure representing an emoticon image
780 *
9678c7b8
SH
781 * @copyright 2010 David Mudrak
782 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
783 * @since Moodle 2.0
f8129210 784 * @package core
76be40cc 785 * @category output
d63c5073
DM
786 */
787class pix_emoticon extends pix_icon implements renderable {
788
789 /**
790 * Constructor
791 * @param string $pix short icon name
b9fadae7
DM
792 * @param string $alt alternative text
793 * @param string $component emoticon image provider
794 * @param array $attributes explicit HTML attributes
d63c5073 795 */
b9fadae7
DM
796 public function __construct($pix, $alt, $component = 'moodle', array $attributes = array()) {
797 if (empty($attributes['class'])) {
798 $attributes['class'] = 'emoticon';
799 }
d63c5073
DM
800 parent::__construct($pix, $alt, $component, $attributes);
801 }
802}
000c278c 803
3ba60ee1
PS
804/**
805 * Data structure representing a simple form with only one button.
806 *
807 * @copyright 2009 Petr Skoda
9678c7b8
SH
808 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
809 * @since Moodle 2.0
f8129210 810 * @package core
76be40cc 811 * @category output
3ba60ee1
PS
812 */
813class single_button implements renderable {
9678c7b8 814
574fbea4 815 /**
76be40cc 816 * @var moodle_url Target url
574fbea4 817 */
f2405a5f 818 public $url;
9678c7b8 819
574fbea4 820 /**
76be40cc 821 * @var string Button label
574fbea4 822 */
f2405a5f 823 public $label;
9678c7b8 824
574fbea4 825 /**
76be40cc 826 * @var string Form submit method post or get
574fbea4 827 */
f2405a5f 828 public $method = 'post';
9678c7b8 829
574fbea4 830 /**
76be40cc 831 * @var string Wrapping div class
9678c7b8 832 */
f2405a5f
DW
833 public $class = 'singlebutton';
834
835 /**
836 * @var bool True if button is primary button. Used for styling.
837 */
838 public $primary = false;
9678c7b8 839
574fbea4 840 /**
3d3fae72 841 * @var bool True if button disabled, false if normal
574fbea4 842 */
f2405a5f 843 public $disabled = false;
9678c7b8 844
574fbea4 845 /**
76be40cc 846 * @var string Button tooltip
574fbea4 847 */
f2405a5f 848 public $tooltip = null;
9678c7b8 849
574fbea4 850 /**
76be40cc 851 * @var string Form id
574fbea4 852 */
f2405a5f 853 public $formid;
9678c7b8 854
574fbea4 855 /**
76be40cc 856 * @var array List of attached actions
574fbea4 857 */
f2405a5f 858 public $actions = array();
3ba60ee1 859
9bdcf579
DW
860 /**
861 * @var array $params URL Params
862 */
f2405a5f 863 public $params;
9bdcf579
DW
864
865 /**
866 * @var string Action id
867 */
f2405a5f 868 public $actionid;
9bdcf579 869
ca81e906
DM
870 /**
871 * @var array
872 */
873 protected $attributes = [];
874
3ba60ee1
PS
875 /**
876 * Constructor
9678c7b8 877 * @param moodle_url $url
3ba60ee1
PS
878 * @param string $label button text
879 * @param string $method get or post submit method
ca81e906 880 * @param array $attributes Attributes for the HTML button tag
3ba60ee1 881 */
ca81e906 882 public function __construct(moodle_url $url, $label, $method='post', $primary=false, $attributes = []) {
3ba60ee1
PS
883 $this->url = clone($url);
884 $this->label = $label;
885 $this->method = $method;
f2405a5f 886 $this->primary = $primary;
ca81e906 887 $this->attributes = $attributes;
3ba60ee1
PS
888 }
889
890 /**
574fbea4 891 * Shortcut for adding a JS confirm dialog when the button is clicked.
3ba60ee1 892 * The message must be a yes/no question.
9678c7b8 893 *
f8129210 894 * @param string $confirmmessage The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
3ba60ee1
PS
895 */
896 public function add_confirm_action($confirmmessage) {
8f78cd5a 897 $this->add_action(new confirm_action($confirmmessage));
3ba60ee1
PS
898 }
899
574fbea4
PS
900 /**
901 * Add action to the button.
902 * @param component_action $action
574fbea4 903 */
3ba60ee1
PS
904 public function add_action(component_action $action) {
905 $this->actions[] = $action;
906 }
d7c65752 907
ca81e906
DM
908 /**
909 * Sets an attribute for the HTML button tag.
910 *
911 * @param string $name The attribute name
912 * @param mixed $value The value
913 * @return null
914 */
915 public function set_attribute($name, $value) {
916 $this->attributes[$name] = $value;
917 }
918
d7c65752
FM
919 /**
920 * Export data.
921 *
922 * @param renderer_base $output Renderer.
923 * @return stdClass
924 */
925 public function export_for_template(renderer_base $output) {
926 $url = $this->method === 'get' ? $this->url->out_omit_querystring(true) : $this->url->out_omit_querystring();
927
928 $data = new stdClass();
929 $data->id = html_writer::random_id('single_button');
930 $data->formid = $this->formid;
931 $data->method = $this->method;
932 $data->url = $url === '' ? '#' : $url;
933 $data->label = $this->label;
934 $data->classes = $this->class;
935 $data->disabled = $this->disabled;
936 $data->tooltip = $this->tooltip;
f2405a5f 937 $data->primary = $this->primary;
d7c65752 938
ca81e906
DM
939 $data->attributes = [];
940 foreach ($this->attributes as $key => $value) {
941 $data->attributes[] = ['name' => $key, 'value' => $value];
942 }
943
d7c65752
FM
944 // Form parameters.
945 $params = $this->url->params();
946 if ($this->method === 'post') {
947 $params['sesskey'] = sesskey();
948 }
949 $data->params = array_map(function($key) use ($params) {
950 return ['name' => $key, 'value' => $params[$key]];
951 }, array_keys($params));
952
953 // Button actions.
954 $actions = $this->actions;
11ba3fd4
FM
955 $data->actions = array_map(function($action) use ($output) {
956 return $action->export_for_template($output);
957 }, $actions);
d7c65752
FM
958 $data->hasactions = !empty($data->actions);
959
960 return $data;
961 }
3ba60ee1
PS
962}
963
964
a9967cf5
PS
965/**
966 * Simple form with just one select field that gets submitted automatically.
f8129210 967 *
a9967cf5
PS
968 * If JS not enabled small go button is printed too.
969 *
970 * @copyright 2009 Petr Skoda
9678c7b8
SH
971 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
972 * @since Moodle 2.0
f8129210 973 * @package core
76be40cc 974 * @category output
a9967cf5 975 */
bf7f35e9 976class single_select implements renderable, templatable {
9678c7b8 977
a9967cf5 978 /**
76be40cc 979 * @var moodle_url Target url - includes hidden fields
a9967cf5
PS
980 */
981 var $url;
9678c7b8 982
a9967cf5 983 /**
76be40cc 984 * @var string Name of the select element.
a9967cf5
PS
985 */
986 var $name;
9678c7b8 987
a9967cf5 988 /**
9678c7b8
SH
989 * @var array $options associative array value=>label ex.: array(1=>'One, 2=>Two)
990 * it is also possible to specify optgroup as complex label array ex.:
991 * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
992 * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
a9967cf5
PS
993 */
994 var $options;
9678c7b8 995
a9967cf5 996 /**
76be40cc 997 * @var string Selected option
a9967cf5
PS
998 */
999 var $selected;
9678c7b8 1000
a9967cf5 1001 /**
76be40cc 1002 * @var array Nothing selected
a9967cf5
PS
1003 */
1004 var $nothing;
9678c7b8 1005
a9967cf5 1006 /**
76be40cc 1007 * @var array Extra select field attributes
a9967cf5
PS
1008 */
1009 var $attributes = array();
9678c7b8 1010
a9967cf5 1011 /**
76be40cc 1012 * @var string Button label
a9967cf5
PS
1013 */
1014 var $label = '';
9678c7b8 1015
ecc5cc31 1016 /**
de7a570c 1017 * @var array Button label's attributes
ecc5cc31
RW
1018 */
1019 var $labelattributes = array();
1020
a9967cf5 1021 /**
76be40cc 1022 * @var string Form submit method post or get
a9967cf5
PS
1023 */
1024 var $method = 'get';
9678c7b8 1025
a9967cf5 1026 /**
76be40cc
SH
1027 * @var string Wrapping div class
1028 */
a9967cf5 1029 var $class = 'singleselect';
9678c7b8 1030
a9967cf5 1031 /**
3d3fae72 1032 * @var bool True if button disabled, false if normal
a9967cf5
PS
1033 */
1034 var $disabled = false;
9678c7b8 1035
a9967cf5 1036 /**
76be40cc 1037 * @var string Button tooltip
a9967cf5
PS
1038 */
1039 var $tooltip = null;
9678c7b8 1040
a9967cf5 1041 /**
76be40cc 1042 * @var string Form id
a9967cf5
PS
1043 */
1044 var $formid = null;
9678c7b8 1045
a9967cf5 1046 /**
5f30600f 1047 * @var help_icon The help icon for this element.
a9967cf5
PS
1048 */
1049 var $helpicon = null;
9678c7b8 1050
a9967cf5
PS
1051 /**
1052 * Constructor
1053 * @param moodle_url $url form action target, includes hidden fields
1054 * @param string $name name of selection field - the changing parameter in url
1055 * @param array $options list of options
1056 * @param string $selected selected element
1057 * @param array $nothing
f8dab966 1058 * @param string $formid
a9967cf5 1059 */
9678c7b8 1060 public function __construct(moodle_url $url, $name, array $options, $selected = '', $nothing = array('' => 'choosedots'), $formid = null) {
a9967cf5
PS
1061 $this->url = $url;
1062 $this->name = $name;
1063 $this->options = $options;
1064 $this->selected = $selected;
1065 $this->nothing = $nothing;
f8dab966 1066 $this->formid = $formid;
a9967cf5
PS
1067 }
1068
1069 /**
1070 * Shortcut for adding a JS confirm dialog when the button is clicked.
1071 * The message must be a yes/no question.
9678c7b8 1072 *
f8129210 1073 * @param string $confirmmessage The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
a9967cf5
PS
1074 */
1075 public function add_confirm_action($confirmmessage) {
1076 $this->add_action(new component_action('submit', 'M.util.show_confirm_dialog', array('message' => $confirmmessage)));
1077 }
1078
1079 /**
1080 * Add action to the button.
9678c7b8 1081 *
a9967cf5 1082 * @param component_action $action
a9967cf5
PS
1083 */
1084 public function add_action(component_action $action) {
1085 $this->actions[] = $action;
1086 }
f8dab966
PS
1087
1088 /**
259c165d 1089 * Adds help icon.
9678c7b8 1090 *
a6d81a73 1091 * @deprecated since Moodle 2.0
f8dab966 1092 */
596509e4 1093 public function set_old_help_icon($helppage, $title, $component = 'moodle') {
a6d81a73 1094 throw new coding_exception('set_old_help_icon() can not be used any more, please see set_help_icon().');
f8dab966
PS
1095 }
1096
259c165d
PS
1097 /**
1098 * Adds help icon.
9678c7b8 1099 *
259c165d
PS
1100 * @param string $identifier The keyword that defines a help page
1101 * @param string $component
259c165d
PS
1102 */
1103 public function set_help_icon($identifier, $component = 'moodle') {
9c7b24bf 1104 $this->helpicon = new help_icon($identifier, $component);
259c165d
PS
1105 }
1106
f8dab966 1107 /**
995f2d51 1108 * Sets select's label
9678c7b8 1109 *
f8dab966 1110 * @param string $label
ecc5cc31 1111 * @param array $attributes (optional)
f8dab966 1112 */
ecc5cc31 1113 public function set_label($label, $attributes = array()) {
f8dab966 1114 $this->label = $label;
ecc5cc31
RW
1115 $this->labelattributes = $attributes;
1116
f8dab966 1117 }
bf7f35e9
FM
1118
1119 /**
1120 * Export data.
1121 *
1122 * @param renderer_base $output Renderer.
1123 * @return stdClass
1124 */
1125 public function export_for_template(renderer_base $output) {
1126 $attributes = $this->attributes;
1127
1128 $data = new stdClass();
1129 $data->name = $this->name;
1130 $data->method = $this->method;
1131 $data->action = $this->method === 'get' ? $this->url->out_omit_querystring(true) : $this->url->out_omit_querystring();
588b84f7 1132 $data->classes = $this->class;
bf7f35e9
FM
1133 $data->label = $this->label;
1134 $data->disabled = $this->disabled;
1135 $data->title = $this->tooltip;
588b84f7 1136 $data->formid = !empty($this->formid) ? $this->formid : html_writer::random_id('single_select_f');
bf7f35e9 1137 $data->id = !empty($attributes['id']) ? $attributes['id'] : html_writer::random_id('single_select');
5f30600f
JP
1138
1139 // Select element attributes.
1140 // Unset attributes that are already predefined in the template.
bf7f35e9 1141 unset($attributes['id']);
5f30600f
JP
1142 unset($attributes['class']);
1143 unset($attributes['name']);
1144 unset($attributes['title']);
1145 unset($attributes['disabled']);
1146
1147 // Map the attributes.
1148 $data->attributes = array_map(function($key) use ($attributes) {
1149 return ['name' => $key, 'value' => $attributes[$key]];
1150 }, array_keys($attributes));
bf7f35e9
FM
1151
1152 // Form parameters.
1153 $params = $this->url->params();
1154 if ($this->method === 'post') {
1155 $params['sesskey'] = sesskey();
1156 }
1157 $data->params = array_map(function($key) use ($params) {
1158 return ['name' => $key, 'value' => $params[$key]];
1159 }, array_keys($params));
1160
1161 // Select options.
1162 $hasnothing = false;
1163 if (is_string($this->nothing) && $this->nothing !== '') {
1164 $nothing = ['' => $this->nothing];
1165 $hasnothing = true;
ae4522ff 1166 $nothingkey = '';
bf7f35e9 1167 } else if (is_array($this->nothing)) {
58357d41
FM
1168 $nothingvalue = reset($this->nothing);
1169 if ($nothingvalue === 'choose' || $nothingvalue === 'choosedots') {
1170 $nothing = [key($this->nothing) => get_string('choosedots')];
bf7f35e9
FM
1171 } else {
1172 $nothing = $this->nothing;
1173 }
1174 $hasnothing = true;
ae4522ff 1175 $nothingkey = key($this->nothing);
bf7f35e9
FM
1176 }
1177 if ($hasnothing) {
1178 $options = $nothing + $this->options;
1179 } else {
1180 $options = $this->options;
1181 }
bf7f35e9
FM
1182
1183 foreach ($options as $value => $name) {
63e4df60
DW
1184 if (is_array($options[$value])) {
1185 foreach ($options[$value] as $optgroupname => $optgroupvalues) {
1186 $sublist = [];
1187 foreach ($optgroupvalues as $optvalue => $optname) {
ae4522ff 1188 $option = [
63e4df60
DW
1189 'value' => $optvalue,
1190 'name' => $optname,
1191 'selected' => strval($this->selected) === strval($optvalue),
1192 ];
ae4522ff 1193
3420d307 1194 if ($hasnothing && $nothingkey === $optvalue) {
ae4522ff
MG
1195 $option['ignore'] = 'data-ignore';
1196 }
1197
1198 $sublist[] = $option;
63e4df60
DW
1199 }
1200 $data->options[] = [
1201 'name' => $optgroupname,
1202 'optgroup' => true,
1203 'options' => $sublist
1204 ];
1205 }
1206 } else {
ae4522ff 1207 $option = [
63e4df60
DW
1208 'value' => $value,
1209 'name' => $options[$value],
1210 'selected' => strval($this->selected) === strval($value),
1211 'optgroup' => false
1212 ];
ae4522ff 1213
3420d307 1214 if ($hasnothing && $nothingkey === $value) {
ae4522ff
MG
1215 $option['ignore'] = 'data-ignore';
1216 }
1217
1218 $data->options[] = $option;
63e4df60 1219 }
bf7f35e9
FM
1220 }
1221
1222 // Label attributes.
1223 $data->labelattributes = [];
5f30600f
JP
1224 // Unset label attributes that are already in the template.
1225 unset($this->labelattributes['for']);
1226 // Map the label attributes.
bf7f35e9 1227 foreach ($this->labelattributes as $key => $value) {
5f30600f 1228 $data->labelattributes[] = ['name' => $key, 'value' => $value];
bf7f35e9
FM
1229 }
1230
1231 // Help icon.
1232 $data->helpicon = !empty($this->helpicon) ? $this->helpicon->export_for_template($output) : false;
1233
1234 return $data;
1235 }
a9967cf5
PS
1236}
1237
4d10e579
PS
1238/**
1239 * Simple URL selection widget description.
9678c7b8 1240 *
4d10e579 1241 * @copyright 2009 Petr Skoda
9678c7b8
SH
1242 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1243 * @since Moodle 2.0
f8129210 1244 * @package core
76be40cc 1245 * @category output
4d10e579 1246 */
f1b34660 1247class url_select implements renderable, templatable {
4d10e579 1248 /**
9678c7b8
SH
1249 * @var array $urls associative array value=>label ex.: array(1=>'One, 2=>Two)
1250 * it is also possible to specify optgroup as complex label array ex.:
1251 * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
1252 * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
4d10e579
PS
1253 */
1254 var $urls;
9678c7b8 1255
4d10e579 1256 /**
76be40cc 1257 * @var string Selected option
4d10e579
PS
1258 */
1259 var $selected;
9678c7b8 1260
4d10e579 1261 /**
76be40cc 1262 * @var array Nothing selected
4d10e579
PS
1263 */
1264 var $nothing;
9678c7b8 1265
4d10e579 1266 /**
76be40cc 1267 * @var array Extra select field attributes
4d10e579
PS
1268 */
1269 var $attributes = array();
9678c7b8 1270
4d10e579 1271 /**
76be40cc 1272 * @var string Button label
4d10e579
PS
1273 */
1274 var $label = '';
9678c7b8 1275
ecc5cc31
RW
1276 /**
1277 * @var array Button label's attributes
1278 */
1279 var $labelattributes = array();
1280
4d10e579 1281 /**
76be40cc
SH
1282 * @var string Wrapping div class
1283 */
4d10e579 1284 var $class = 'urlselect';
9678c7b8 1285
4d10e579 1286 /**
3d3fae72 1287 * @var bool True if button disabled, false if normal
4d10e579
PS
1288 */
1289 var $disabled = false;
9678c7b8 1290
4d10e579 1291 /**
76be40cc 1292 * @var string Button tooltip
4d10e579
PS
1293 */
1294 var $tooltip = null;
9678c7b8 1295
4d10e579 1296 /**
76be40cc 1297 * @var string Form id
4d10e579
PS
1298 */
1299 var $formid = null;
9678c7b8 1300
4d10e579 1301 /**
5f30600f 1302 * @var help_icon The help icon for this element.
4d10e579
PS
1303 */
1304 var $helpicon = null;
9678c7b8 1305
15e48a1a
SM
1306 /**
1307 * @var string If set, makes button visible with given name for button
1308 */
1309 var $showbutton = null;
9678c7b8 1310
4d10e579
PS
1311 /**
1312 * Constructor
1313 * @param array $urls list of options
1314 * @param string $selected selected element
1315 * @param array $nothing
1316 * @param string $formid
15e48a1a
SM
1317 * @param string $showbutton Set to text of button if it should be visible
1318 * or null if it should be hidden (hidden version always has text 'go')
1319 */
9678c7b8 1320 public function __construct(array $urls, $selected = '', $nothing = array('' => 'choosedots'), $formid = null, $showbutton = null) {
15e48a1a
SM
1321 $this->urls = $urls;
1322 $this->selected = $selected;
1323 $this->nothing = $nothing;
1324 $this->formid = $formid;
1325 $this->showbutton = $showbutton;
4d10e579
PS
1326 }
1327
1328 /**
259c165d 1329 * Adds help icon.
9678c7b8 1330 *
a6d81a73 1331 * @deprecated since Moodle 2.0
4d10e579 1332 */
596509e4 1333 public function set_old_help_icon($helppage, $title, $component = 'moodle') {
a6d81a73 1334 throw new coding_exception('set_old_help_icon() can not be used any more, please see set_help_icon().');
4d10e579
PS
1335 }
1336
259c165d
PS
1337 /**
1338 * Adds help icon.
9678c7b8 1339 *
259c165d
PS
1340 * @param string $identifier The keyword that defines a help page
1341 * @param string $component
259c165d
PS
1342 */
1343 public function set_help_icon($identifier, $component = 'moodle') {
9c7b24bf 1344 $this->helpicon = new help_icon($identifier, $component);
259c165d
PS
1345 }
1346
4d10e579 1347 /**
995f2d51 1348 * Sets select's label
9678c7b8 1349 *
4d10e579 1350 * @param string $label
ecc5cc31 1351 * @param array $attributes (optional)
4d10e579 1352 */
ecc5cc31 1353 public function set_label($label, $attributes = array()) {
4d10e579 1354 $this->label = $label;
ecc5cc31 1355 $this->labelattributes = $attributes;
4d10e579 1356 }
f1b34660
FM
1357
1358 /**
1359 * Clean a URL.
1360 *
1361 * @param string $value The URL.
1362 * @return The cleaned URL.
1363 */
1364 protected function clean_url($value) {
1365 global $CFG;
1366
1367 if (empty($value)) {
1368 // Nothing.
1369
1370 } else if (strpos($value, $CFG->wwwroot . '/') === 0) {
1371 $value = str_replace($CFG->wwwroot, '', $value);
1372
1373 } else if (strpos($value, '/') !== 0) {
1374 debugging("Invalid url_select urls parameter: url '$value' is not local relative url!", DEBUG_DEVELOPER);
1375 }
1376
1377 return $value;
1378 }
1379
1380 /**
1381 * Flatten the options for Mustache.
1382 *
1383 * This also cleans the URLs.
1384 *
1385 * @param array $options The options.
1386 * @param array $nothing The nothing option.
1387 * @return array
1388 */
1389 protected function flatten_options($options, $nothing) {
1390 $flattened = [];
1391
1392 foreach ($options as $value => $option) {
1393 if (is_array($option)) {
1394 foreach ($option as $groupname => $optoptions) {
1395 if (!isset($flattened[$groupname])) {
1396 $flattened[$groupname] = [
1397 'name' => $groupname,
1398 'isgroup' => true,
1399 'options' => []
1400 ];
1401 }
1402 foreach ($optoptions as $optvalue => $optoption) {
1403 $cleanedvalue = $this->clean_url($optvalue);
1404 $flattened[$groupname]['options'][$cleanedvalue] = [
1405 'name' => $optoption,
1406 'value' => $cleanedvalue,
1407 'selected' => $this->selected == $optvalue,
1408 ];
1409 }
1410 }
1411
1412 } else {
1413 $cleanedvalue = $this->clean_url($value);
1414 $flattened[$cleanedvalue] = [
1415 'name' => $option,
1416 'value' => $cleanedvalue,
1417 'selected' => $this->selected == $value,
1418 ];
1419 }
1420 }
1421
1422 if (!empty($nothing)) {
1423 $value = key($nothing);
1424 $name = reset($nothing);
1425 $flattened = [
1426 $value => ['name' => $name, 'value' => $value, 'selected' => $this->selected == $value]
1427 ] + $flattened;
1428 }
1429
1430 // Make non-associative array.
1431 foreach ($flattened as $key => $value) {
1432 if (!empty($value['options'])) {
1433 $flattened[$key]['options'] = array_values($value['options']);
1434 }
1435 }
1436 $flattened = array_values($flattened);
1437
1438 return $flattened;
1439 }
1440
1441 /**
1442 * Export for template.
1443 *
1444 * @param renderer_base $output Renderer.
1445 * @return stdClass
1446 */
1447 public function export_for_template(renderer_base $output) {
1448 $attributes = $this->attributes;
1449
1450 $data = new stdClass();
1451 $data->formid = !empty($this->formid) ? $this->formid : html_writer::random_id('url_select_f');
1452 $data->classes = $this->class;
1453 $data->label = $this->label;
1454 $data->disabled = $this->disabled;
1455 $data->title = $this->tooltip;
1456 $data->id = !empty($attributes['id']) ? $attributes['id'] : html_writer::random_id('url_select');
1457 $data->sesskey = sesskey();
1458 $data->action = (new moodle_url('/course/jumpto.php'))->out(false);
1459
1460 // Remove attributes passed as property directly.
1461 unset($attributes['class']);
1462 unset($attributes['id']);
1463 unset($attributes['name']);
1464 unset($attributes['title']);
5f30600f 1465 unset($attributes['disabled']);
f1b34660
FM
1466
1467 $data->showbutton = $this->showbutton;
f1b34660
FM
1468
1469 // Select options.
1470 $nothing = false;
1471 if (is_string($this->nothing) && $this->nothing !== '') {
1472 $nothing = ['' => $this->nothing];
f1b34660 1473 } else if (is_array($this->nothing)) {
ecfad63d
AN
1474 $nothingvalue = reset($this->nothing);
1475 if ($nothingvalue === 'choose' || $nothingvalue === 'choosedots') {
1476 $nothing = [key($this->nothing) => get_string('choosedots')];
f1b34660 1477 } else {
ecfad63d 1478 $nothing = $this->nothing;
f1b34660 1479 }
f1b34660 1480 }
f1b34660
FM
1481 $data->options = $this->flatten_options($this->urls, $nothing);
1482
1483 // Label attributes.
1484 $data->labelattributes = [];
5f30600f
JP
1485 // Unset label attributes that are already in the template.
1486 unset($this->labelattributes['for']);
1487 // Map the label attributes.
f1b34660
FM
1488 foreach ($this->labelattributes as $key => $value) {
1489 $data->labelattributes[] = ['name' => $key, 'value' => $value];
1490 }
1491
1492 // Help icon.
1493 $data->helpicon = !empty($this->helpicon) ? $this->helpicon->export_for_template($output) : false;
1494
1495 // Finally all the remaining attributes.
1496 $data->attributes = [];
5f30600f
JP
1497 foreach ($attributes as $key => $value) {
1498 $data->attributes[] = ['name' => $key, 'value' => $value];
f1b34660
FM
1499 }
1500
1501 return $data;
1502 }
4d10e579
PS
1503}
1504
574fbea4
PS
1505/**
1506 * Data structure describing html link with special action attached.
9678c7b8 1507 *
574fbea4 1508 * @copyright 2010 Petr Skoda
9678c7b8
SH
1509 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1510 * @since Moodle 2.0
f8129210 1511 * @package core
76be40cc 1512 * @category output
574fbea4
PS
1513 */
1514class action_link implements renderable {
9678c7b8 1515
574fbea4 1516 /**
76be40cc 1517 * @var moodle_url Href url
574fbea4 1518 */
ea5a01fb 1519 public $url;
9678c7b8 1520
574fbea4 1521 /**
76be40cc 1522 * @var string Link text HTML fragment
574fbea4 1523 */
ea5a01fb 1524 public $text;
9678c7b8 1525
574fbea4 1526 /**
76be40cc 1527 * @var array HTML attributes
574fbea4 1528 */
ea5a01fb 1529 public $attributes;
9678c7b8 1530
574fbea4 1531 /**
76be40cc 1532 * @var array List of actions attached to link
574fbea4 1533 */
ea5a01fb 1534 public $actions;
574fbea4 1535
e282c679
SH
1536 /**
1537 * @var pix_icon Optional pix icon to render with the link
1538 */
ea5a01fb 1539 public $icon;
e282c679 1540
574fbea4
PS
1541 /**
1542 * Constructor
9678c7b8 1543 * @param moodle_url $url
574fbea4
PS
1544 * @param string $text HTML fragment
1545 * @param component_action $action
11820bac 1546 * @param array $attributes associative array of html link attributes + disabled
e282c679 1547 * @param pix_icon $icon optional pix_icon to render with the link text
574fbea4 1548 */
e282c679
SH
1549 public function __construct(moodle_url $url,
1550 $text,
1551 component_action $action=null,
1552 array $attributes=null,
1553 pix_icon $icon=null) {
9678c7b8
SH
1554 $this->url = clone($url);
1555 $this->text = $text;
b0fef57b 1556 $this->attributes = (array)$attributes;
f14b641b 1557 if ($action) {
574fbea4
PS
1558 $this->add_action($action);
1559 }
e282c679 1560 $this->icon = $icon;
574fbea4
PS
1561 }
1562
1563 /**
1564 * Add action to the link.
9678c7b8 1565 *
574fbea4 1566 * @param component_action $action
574fbea4
PS
1567 */
1568 public function add_action(component_action $action) {
1569 $this->actions[] = $action;
1570 }
c63923bd 1571
9678c7b8
SH
1572 /**
1573 * Adds a CSS class to this action link object
1574 * @param string $class
1575 */
c63923bd 1576 public function add_class($class) {
67da0bf7
DM
1577 if (empty($this->attributes['class'])) {
1578 $this->attributes['class'] = $class;
c63923bd 1579 } else {
67da0bf7 1580 $this->attributes['class'] .= ' ' . $class;
c63923bd
PS
1581 }
1582 }
e282c679
SH
1583
1584 /**
1585 * Returns true if the specified class has been added to this link.
1586 * @param string $class
1587 * @return bool
1588 */
1589 public function has_class($class) {
1590 return strpos(' ' . $this->attributes['class'] . ' ', ' ' . $class . ' ') !== false;
1591 }
3f0544b8
DW
1592
1593 /**
1594 * Return the rendered HTML for the icon. Useful for rendering action links in a template.
1595 * @return string
1596 */
1597 public function get_icon_html() {
1598 global $OUTPUT;
1599 if (!$this->icon) {
1600 return '';
1601 }
1602 return $OUTPUT->render($this->icon);
1603 }
11ba3fd4
FM
1604
1605 /**
1606 * Export for template.
1607 *
1608 * @param renderer_base $output The renderer.
1609 * @return stdClass
1610 */
1611 public function export_for_template(renderer_base $output) {
1612 $data = new stdClass();
1613 $attributes = $this->attributes;
1614
1615 if (empty($attributes['id'])) {
1616 $attributes['id'] = html_writer::random_id('action_link');
1617 }
1618 $data->id = $attributes['id'];
1619 unset($attributes['id']);
1620
1621 $data->disabled = !empty($attributes['disabled']);
1622 unset($attributes['disabled']);
1623
1624 $data->text = $this->text instanceof renderable ? $output->render($this->text) : (string) $this->text;
1625 $data->url = $this->url ? $this->url->out(false) : '';
7cf6ef7b 1626 $data->icon = $this->icon ? $this->icon->export_for_pix() : null;
11ba3fd4
FM
1627 $data->classes = isset($attributes['class']) ? $attributes['class'] : '';
1628 unset($attributes['class']);
1629
1630 $data->attributes = array_map(function($key, $value) {
1631 return [
1632 'name' => $key,
1633 'value' => $value
1634 ];
1635 }, array_keys($attributes), $attributes);
1636
1637 $data->actions = array_map(function($action) use ($output) {
1638 return $action->export_for_template($output);
1639 }, !empty($this->actions) ? $this->actions : []);
1640 $data->hasactions = !empty($this->actions);
1641
1642 return $data;
1643 }
574fbea4 1644}
3ba60ee1 1645
5d0c95a5
PS
1646/**
1647 * Simple html output class
9678c7b8 1648 *
5d0c95a5 1649 * @copyright 2009 Tim Hunt, 2010 Petr Skoda
9678c7b8
SH
1650 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1651 * @since Moodle 2.0
f8129210 1652 * @package core
76be40cc 1653 * @category output
5d0c95a5
PS
1654 */
1655class html_writer {
9678c7b8 1656
5d0c95a5
PS
1657 /**
1658 * Outputs a tag with attributes and contents
9678c7b8 1659 *
5d0c95a5 1660 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
5d0c95a5 1661 * @param string $contents What goes between the opening and closing tags
26acc814 1662 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
5d0c95a5
PS
1663 * @return string HTML fragment
1664 */
26acc814 1665 public static function tag($tagname, $contents, array $attributes = null) {
5d0c95a5
PS
1666 return self::start_tag($tagname, $attributes) . $contents . self::end_tag($tagname);
1667 }
1668
1669 /**
1670 * Outputs an opening tag with attributes
9678c7b8 1671 *
5d0c95a5
PS
1672 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
1673 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
1674 * @return string HTML fragment
1675 */
1676 public static function start_tag($tagname, array $attributes = null) {
1677 return '<' . $tagname . self::attributes($attributes) . '>';
1678 }
1679
1680 /**
1681 * Outputs a closing tag
9678c7b8 1682 *
5d0c95a5
PS
1683 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
1684 * @return string HTML fragment
1685 */
1686 public static function end_tag($tagname) {
1687 return '</' . $tagname . '>';
1688 }
1689
1690 /**
1691 * Outputs an empty tag with attributes
9678c7b8 1692 *
5d0c95a5
PS
1693 * @param string $tagname The name of tag ('input', 'img', 'br' etc.)
1694 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
1695 * @return string HTML fragment
1696 */
1697 public static function empty_tag($tagname, array $attributes = null) {
1698 return '<' . $tagname . self::attributes($attributes) . ' />';
1699 }
1700
836c47d7
TH
1701 /**
1702 * Outputs a tag, but only if the contents are not empty
9678c7b8 1703 *
836c47d7
TH
1704 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
1705 * @param string $contents What goes between the opening and closing tags
1706 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
1707 * @return string HTML fragment
1708 */
1709 public static function nonempty_tag($tagname, $contents, array $attributes = null) {
1710 if ($contents === '' || is_null($contents)) {
1711 return '';
1712 }
1713 return self::tag($tagname, $contents, $attributes);
1714 }
1715
5d0c95a5
PS
1716 /**
1717 * Outputs a HTML attribute and value
9678c7b8 1718 *
5d0c95a5
PS
1719 * @param string $name The name of the attribute ('src', 'href', 'class' etc.)
1720 * @param string $value The value of the attribute. The value will be escaped with {@link s()}
1721 * @return string HTML fragment
1722 */
1723 public static function attribute($name, $value) {
bf11293a
PS
1724 if ($value instanceof moodle_url) {
1725 return ' ' . $name . '="' . $value->out() . '"';
1726 }
97c10099
PS
1727
1728 // special case, we do not want these in output
1729 if ($value === null) {
1730 return '';
5d0c95a5 1731 }
97c10099
PS
1732
1733 // no sloppy trimming here!
1734 return ' ' . $name . '="' . s($value) . '"';
5d0c95a5
PS
1735 }
1736
1737 /**
1738 * Outputs a list of HTML attributes and values
9678c7b8 1739 *
5d0c95a5
PS
1740 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
1741 * The values will be escaped with {@link s()}
1742 * @return string HTML fragment
1743 */
1744 public static function attributes(array $attributes = null) {
1745 $attributes = (array)$attributes;
1746 $output = '';
1747 foreach ($attributes as $name => $value) {
1748 $output .= self::attribute($name, $value);
1749 }
1750 return $output;
1751 }
1752
7b218e86 1753 /**
1754 * Generates a simple image tag with attributes.
1755 *
1756 * @param string $src The source of image
1757 * @param string $alt The alternate text for image
1758 * @param array $attributes The tag attributes (array('height' => $max_height, 'class' => 'class1') etc.)
1759 * @return string HTML fragment
1760 */
1761 public static function img($src, $alt, array $attributes = null) {
1762 $attributes = (array)$attributes;
1763 $attributes['src'] = $src;
1764 $attributes['alt'] = $alt;
1765
1766 return self::empty_tag('img', $attributes);
1767 }
1768
5d0c95a5
PS
1769 /**
1770 * Generates random html element id.
9678c7b8
SH
1771 *
1772 * @staticvar int $counter
1773 * @staticvar type $uniq
3d3fae72
SH
1774 * @param string $base A string fragment that will be included in the random ID.
1775 * @return string A unique ID
5d0c95a5
PS
1776 */
1777 public static function random_id($base='random') {
2c479c8f
PS
1778 static $counter = 0;
1779 static $uniq;
1780
1781 if (!isset($uniq)) {
1782 $uniq = uniqid();
1783 }
1784
1785 $counter++;
1786 return $base.$uniq.$counter;
5d0c95a5 1787 }
0f4c64b7
PS
1788
1789 /**
1790 * Generates a simple html link
9678c7b8 1791 *
3d3fae72
SH
1792 * @param string|moodle_url $url The URL
1793 * @param string $text The text
1794 * @param array $attributes HTML attributes
0f4c64b7
PS
1795 * @return string HTML fragment
1796 */
1797 public static function link($url, $text, array $attributes = null) {
1798 $attributes = (array)$attributes;
1799 $attributes['href'] = $url;
26acc814 1800 return self::tag('a', $text, $attributes);
0f4c64b7 1801 }
3ff163c5 1802
14dce022 1803 /**
9678c7b8
SH
1804 * Generates a simple checkbox with optional label
1805 *
3d3fae72
SH
1806 * @param string $name The name of the checkbox
1807 * @param string $value The value of the checkbox
1808 * @param bool $checked Whether the checkbox is checked
1809 * @param string $label The label for the checkbox
1810 * @param array $attributes Any attributes to apply to the checkbox
9a3af544 1811 * @param array $labelattributes Any attributes to apply to the label, if present
14dce022
PS
1812 * @return string html fragment
1813 */
9a3af544
TH
1814 public static function checkbox($name, $value, $checked = true, $label = '',
1815 array $attributes = null, array $labelattributes = null) {
1816 $attributes = (array) $attributes;
14dce022
PS
1817 $output = '';
1818
1819 if ($label !== '' and !is_null($label)) {
1820 if (empty($attributes['id'])) {
1821 $attributes['id'] = self::random_id('checkbox_');
1822 }
1823 }
53868425
PS
1824 $attributes['type'] = 'checkbox';
1825 $attributes['value'] = $value;
1826 $attributes['name'] = $name;
621b4d08 1827 $attributes['checked'] = $checked ? 'checked' : null;
53868425 1828
14dce022
PS
1829 $output .= self::empty_tag('input', $attributes);
1830
1831 if ($label !== '' and !is_null($label)) {
9a3af544
TH
1832 $labelattributes = (array) $labelattributes;
1833 $labelattributes['for'] = $attributes['id'];
1834 $output .= self::tag('label', $label, $labelattributes);
14dce022
PS
1835 }
1836
1837 return $output;
1838 }
1839
78bdac64
PS
1840 /**
1841 * Generates a simple select yes/no form field
9678c7b8 1842 *
78bdac64
PS
1843 * @param string $name name of select element
1844 * @param bool $selected
1845 * @param array $attributes - html select element attributes
9678c7b8 1846 * @return string HTML fragment
78bdac64 1847 */
19f3bbb2 1848 public static function select_yes_no($name, $selected=true, array $attributes = null) {
78bdac64
PS
1849 $options = array('1'=>get_string('yes'), '0'=>get_string('no'));
1850 return self::select($options, $name, $selected, null, $attributes);
1851 }
1852
3ff163c5
PS
1853 /**
1854 * Generates a simple select form field
9678c7b8 1855 *
6770330d
PS
1856 * @param array $options associative array value=>label ex.:
1857 * array(1=>'One, 2=>Two)
1858 * it is also possible to specify optgroup as complex label array ex.:
bde156b3 1859 * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
6770330d 1860 * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
3ff163c5 1861 * @param string $name name of select element
1a10840e 1862 * @param string|array $selected value or array of values depending on multiple attribute
f8129210
SH
1863 * @param array|bool $nothing add nothing selected option, or false of not added
1864 * @param array $attributes html select element attributes
78bdac64 1865 * @return string HTML fragment
3ff163c5 1866 */
9678c7b8 1867 public static function select(array $options, $name, $selected = '', $nothing = array('' => 'choosedots'), array $attributes = null) {
3ff163c5
PS
1868 $attributes = (array)$attributes;
1869 if (is_array($nothing)) {
1870 foreach ($nothing as $k=>$v) {
4b9210f3 1871 if ($v === 'choose' or $v === 'choosedots') {
3ff163c5
PS
1872 $nothing[$k] = get_string('choosedots');
1873 }
1874 }
1875 $options = $nothing + $options; // keep keys, do not override
3750c3bd
PS
1876
1877 } else if (is_string($nothing) and $nothing !== '') {
1878 // BC
1879 $options = array(''=>$nothing) + $options;
bde156b3 1880 }
3ff163c5
PS
1881
1882 // we may accept more values if multiple attribute specified
1883 $selected = (array)$selected;
1884 foreach ($selected as $k=>$v) {
1885 $selected[$k] = (string)$v;
1886 }
1887
1888 if (!isset($attributes['id'])) {
1889 $id = 'menu'.$name;
1890 // name may contaion [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading
1891 $id = str_replace('[', '', $id);
1892 $id = str_replace(']', '', $id);
1893 $attributes['id'] = $id;
1894 }
1895
1896 if (!isset($attributes['class'])) {
1897 $class = 'menu'.$name;
1898 // name may contaion [], which would make an invalid class. e.g. numeric question type editing form, assignment quickgrading
1899 $class = str_replace('[', '', $class);
1900 $class = str_replace(']', '', $class);
1901 $attributes['class'] = $class;
1902 }
270b2e44 1903 $attributes['class'] = 'select custom-select ' . $attributes['class']; // Add 'select' selector always.
3ff163c5
PS
1904
1905 $attributes['name'] = $name;
1906
1a09fa6d
TH
1907 if (!empty($attributes['disabled'])) {
1908 $attributes['disabled'] = 'disabled';
1909 } else {
1910 unset($attributes['disabled']);
1911 }
1912
3ff163c5
PS
1913 $output = '';
1914 foreach ($options as $value=>$label) {
6770330d
PS
1915 if (is_array($label)) {
1916 // ignore key, it just has to be unique
1917 $output .= self::select_optgroup(key($label), current($label), $selected);
1918 } else {
1919 $output .= self::select_option($label, $value, $selected);
3ff163c5 1920 }
3ff163c5 1921 }
26acc814 1922 return self::tag('select', $output, $attributes);
3ff163c5 1923 }
6770330d 1924
9678c7b8
SH
1925 /**
1926 * Returns HTML to display a select box option.
1927 *
1928 * @param string $label The label to display as the option.
1929 * @param string|int $value The value the option represents
1930 * @param array $selected An array of selected options
1931 * @return string HTML fragment
1932 */
6770330d
PS
1933 private static function select_option($label, $value, array $selected) {
1934 $attributes = array();
1935 $value = (string)$value;
1936 if (in_array($value, $selected, true)) {
1937 $attributes['selected'] = 'selected';
1938 }
1939 $attributes['value'] = $value;
26acc814 1940 return self::tag('option', $label, $attributes);
6770330d
PS
1941 }
1942
9678c7b8
SH
1943 /**
1944 * Returns HTML to display a select box option group.
1945 *
1946 * @param string $groupname The label to use for the group
1947 * @param array $options The options in the group
1948 * @param array $selected An array of selected values.
1949 * @return string HTML fragment.
1950 */
6770330d
PS
1951 private static function select_optgroup($groupname, $options, array $selected) {
1952 if (empty($options)) {
1953 return '';
1954 }
1955 $attributes = array('label'=>$groupname);
1956 $output = '';
1957 foreach ($options as $value=>$label) {
1958 $output .= self::select_option($label, $value, $selected);
1959 }
26acc814 1960 return self::tag('optgroup', $output, $attributes);
6770330d 1961 }
6ea66ff3 1962
f83b9b63
PS
1963 /**
1964 * This is a shortcut for making an hour selector menu.
9678c7b8 1965 *
f83b9b63
PS
1966 * @param string $type The type of selector (years, months, days, hours, minutes)
1967 * @param string $name fieldname
1968 * @param int $currenttime A default timestamp in GMT
1969 * @param int $step minute spacing
1970 * @param array $attributes - html select element attributes
1971 * @return HTML fragment
1972 */
9678c7b8 1973 public static function select_time($type, $name, $currenttime = 0, $step = 5, array $attributes = null) {
14395f97
FM
1974 global $OUTPUT;
1975
f83b9b63
PS
1976 if (!$currenttime) {
1977 $currenttime = time();
1978 }
2bb196eb
SR
1979 $calendartype = \core_calendar\type_factory::get_calendar_instance();
1980 $currentdate = $calendartype->timestamp_to_date_array($currenttime);
f83b9b63
PS
1981 $userdatetype = $type;
1982 $timeunits = array();
1983
1984 switch ($type) {
1985 case 'years':
2bb196eb 1986 $timeunits = $calendartype->get_years();
f83b9b63
PS
1987 $userdatetype = 'year';
1988 break;
1989 case 'months':
2bb196eb 1990 $timeunits = $calendartype->get_months();
f83b9b63 1991 $userdatetype = 'month';
0387e003 1992 $currentdate['month'] = (int)$currentdate['mon'];
f83b9b63
PS
1993 break;
1994 case 'days':
2bb196eb 1995 $timeunits = $calendartype->get_days();
f83b9b63
PS
1996 $userdatetype = 'mday';
1997 break;
1998 case 'hours':
1999 for ($i=0; $i<=23; $i++) {
2000 $timeunits[$i] = sprintf("%02d",$i);
2001 }
2002 break;
2003 case 'minutes':
2004 if ($step != 1) {
2005 $currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step;
2006 }
2007
2008 for ($i=0; $i<=59; $i+=$step) {
2009 $timeunits[$i] = sprintf("%02d",$i);
2010 }
2011 break;
2012 default:
2013 throw new coding_exception("Time type $type is not supported by html_writer::select_time().");
2014 }
2015
14395f97
FM
2016 $attributes = (array) $attributes;
2017 $data = (object) [
2018 'name' => $name,
2019 'id' => !empty($attributes['id']) ? $attributes['id'] : self::random_id('ts_'),
2020 'label' => get_string(substr($type, 0, -1), 'form'),
2021 'options' => array_map(function($value) use ($timeunits, $currentdate, $userdatetype) {
2022 return [
2023 'name' => $timeunits[$value],
2024 'value' => $value,
2025 'selected' => $currentdate[$userdatetype] == $value
2026 ];
2027 }, array_keys($timeunits)),
2028 ];
2029
2030 unset($attributes['id']);
2031 unset($attributes['name']);
2032 $data->attributes = array_map(function($name) use ($attributes) {
2033 return [
2034 'name' => $name,
2035 'value' => $attributes[$name]
2036 ];
2037 }, array_keys($attributes));
f83b9b63 2038
14395f97 2039 return $OUTPUT->render_from_template('core/select_time', $data);
f83b9b63
PS
2040 }
2041
5be262b6
PS
2042 /**
2043 * Shortcut for quick making of lists
9678c7b8
SH
2044 *
2045 * Note: 'list' is a reserved keyword ;-)
2046 *
5be262b6 2047 * @param array $items
5be262b6 2048 * @param array $attributes
9678c7b8 2049 * @param string $tag ul or ol
5be262b6
PS
2050 * @return string
2051 */
2052 public static function alist(array $items, array $attributes = null, $tag = 'ul') {
b69a3f1c 2053 $output = html_writer::start_tag($tag, $attributes)."\n";
5be262b6 2054 foreach ($items as $item) {
b69a3f1c 2055 $output .= html_writer::tag('li', $item)."\n";
5be262b6 2056 }
b69a3f1c
SH
2057 $output .= html_writer::end_tag($tag);
2058 return $output;
5be262b6
PS
2059 }
2060
6ea66ff3
PS
2061 /**
2062 * Returns hidden input fields created from url parameters.
9678c7b8 2063 *
6ea66ff3
PS
2064 * @param moodle_url $url
2065 * @param array $exclude list of excluded parameters
2066 * @return string HTML fragment
2067 */
2068 public static function input_hidden_params(moodle_url $url, array $exclude = null) {
2069 $exclude = (array)$exclude;
2070 $params = $url->params();
2071 foreach ($exclude as $key) {
2072 unset($params[$key]);
2073 }
2074
2075 $output = '';
bde156b3 2076 foreach ($params as $key => $value) {
6ea66ff3
PS
2077 $attributes = array('type'=>'hidden', 'name'=>$key, 'value'=>$value);
2078 $output .= self::empty_tag('input', $attributes)."\n";
2079 }
2080 return $output;
2081 }
77774f6a
PS
2082
2083 /**
2084 * Generate a script tag containing the the specified code.
2085 *
f8129210
SH
2086 * @param string $jscode the JavaScript code
2087 * @param moodle_url|string $url optional url of the external script, $code ignored if specified
77774f6a
PS
2088 * @return string HTML, the code wrapped in <script> tags.
2089 */
e50b4c89 2090 public static function script($jscode, $url=null) {
77774f6a 2091 if ($jscode) {
1a94ddd3 2092 return self::tag('script', "\n//<![CDATA[\n$jscode\n//]]>\n") . "\n";
e50b4c89
PS
2093
2094 } else if ($url) {
1a94ddd3 2095 return self::tag('script', '', ['src' => $url]) . "\n";
a9967cf5 2096
77774f6a
PS
2097 } else {
2098 return '';
2099 }
2100 }
16be8974
DM
2101
2102 /**
2103 * Renders HTML table
2104 *
2105 * This method may modify the passed instance by adding some default properties if they are not set yet.
2106 * If this is not what you want, you should make a full clone of your data before passing them to this
2107 * method. In most cases this is not an issue at all so we do not clone by default for performance
2108 * and memory consumption reasons.
2109 *
2110 * @param html_table $table data to be rendered
2111 * @return string HTML code
2112 */
2113 public static function table(html_table $table) {
2114 // prepare table data and populate missing properties with reasonable defaults
2115 if (!empty($table->align)) {
2116 foreach ($table->align as $key => $aa) {
2117 if ($aa) {
2118 $table->align[$key] = 'text-align:'. fix_align_rtl($aa) .';'; // Fix for RTL languages
2119 } else {
2120 $table->align[$key] = null;
2121 }
2122 }
2123 }
2124 if (!empty($table->size)) {
2125 foreach ($table->size as $key => $ss) {
2126 if ($ss) {
2127 $table->size[$key] = 'width:'. $ss .';';
2128 } else {
2129 $table->size[$key] = null;
2130 }
2131 }
2132 }
2133 if (!empty($table->wrap)) {
2134 foreach ($table->wrap as $key => $ww) {
2135 if ($ww) {
2136 $table->wrap[$key] = 'white-space:nowrap;';
2137 } else {
2138 $table->wrap[$key] = '';
2139 }
2140 }
2141 }
2142 if (!empty($table->head)) {
2143 foreach ($table->head as $key => $val) {
2144 if (!isset($table->align[$key])) {
2145 $table->align[$key] = null;
2146 }
2147 if (!isset($table->size[$key])) {
2148 $table->size[$key] = null;
2149 }
2150 if (!isset($table->wrap[$key])) {
2151 $table->wrap[$key] = null;
2152 }
2153
2154 }
2155 }
2156 if (empty($table->attributes['class'])) {
2157 $table->attributes['class'] = 'generaltable';
2158 }
2159 if (!empty($table->tablealign)) {
2160 $table->attributes['class'] .= ' boxalign' . $table->tablealign;
2161 }
2162
2163 // explicitly assigned properties override those defined via $table->attributes
e126c0cc 2164 $table->attributes['class'] = trim($table->attributes['class']);
16be8974
DM
2165 $attributes = array_merge($table->attributes, array(
2166 'id' => $table->id,
2167 'width' => $table->width,
2168 'summary' => $table->summary,
2169 'cellpadding' => $table->cellpadding,
2170 'cellspacing' => $table->cellspacing,
2171 ));
2172 $output = html_writer::start_tag('table', $attributes) . "\n";
2173
2174 $countcols = 0;
2175
3451a51c
JC
2176 // Output a caption if present.
2177 if (!empty($table->caption)) {
2178 $captionattributes = array();
2179 if ($table->captionhide) {
2180 $captionattributes['class'] = 'accesshide';
2181 }
2182 $output .= html_writer::tag(
2183 'caption',
2184 $table->caption,
2185 $captionattributes
2186 );
2187 }
2188
16be8974
DM
2189 if (!empty($table->head)) {
2190 $countcols = count($table->head);
5174f3c5 2191
16be8974
DM
2192 $output .= html_writer::start_tag('thead', array()) . "\n";
2193 $output .= html_writer::start_tag('tr', array()) . "\n";
2194 $keys = array_keys($table->head);
2195 $lastkey = end($keys);
2196
2197 foreach ($table->head as $key => $heading) {
2198 // Convert plain string headings into html_table_cell objects
2199 if (!($heading instanceof html_table_cell)) {
2200 $headingtext = $heading;
2201 $heading = new html_table_cell();
2202 $heading->text = $headingtext;
2203 $heading->header = true;
2204 }
2205
2206 if ($heading->header !== false) {
2207 $heading->header = true;
2208 }
2209
d28b4ec3
SR
2210 $tagtype = 'td';
2211 if ($heading->header && (string)$heading->text != '') {
2212 $tagtype = 'th';
e126c0cc
DM
2213 }
2214
16be8974
DM
2215 $heading->attributes['class'] .= ' header c' . $key;
2216 if (isset($table->headspan[$key]) && $table->headspan[$key] > 1) {
2217 $heading->colspan = $table->headspan[$key];
2218 $countcols += $table->headspan[$key] - 1;
2219 }
2220
2221 if ($key == $lastkey) {
2222 $heading->attributes['class'] .= ' lastcol';
2223 }
2224 if (isset($table->colclasses[$key])) {
2225 $heading->attributes['class'] .= ' ' . $table->colclasses[$key];
2226 }
e126c0cc 2227 $heading->attributes['class'] = trim($heading->attributes['class']);
d28b4ec3
SR
2228 $attributes = array_merge($heading->attributes, [
2229 'style' => $table->align[$key] . $table->size[$key] . $heading->style,
2230 'colspan' => $heading->colspan,
2231 ]);
16be8974 2232
d28b4ec3
SR
2233 if ($tagtype == 'th') {
2234 $attributes['scope'] = !empty($heading->scope) ? $heading->scope : 'col';
16be8974 2235 }
d28b4ec3 2236
16be8974
DM
2237 $output .= html_writer::tag($tagtype, $heading->text, $attributes) . "\n";
2238 }
2239 $output .= html_writer::end_tag('tr') . "\n";
2240 $output .= html_writer::end_tag('thead') . "\n";
2241
2242 if (empty($table->data)) {
2243 // For valid XHTML strict every table must contain either a valid tr
2244 // or a valid tbody... both of which must contain a valid td
2245 $output .= html_writer::start_tag('tbody', array('class' => 'empty'));
2246 $output .= html_writer::tag('tr', html_writer::tag('td', '', array('colspan'=>count($table->head))));
2247 $output .= html_writer::end_tag('tbody');
2248 }
2249 }
2250
2251 if (!empty($table->data)) {
16be8974
DM
2252 $keys = array_keys($table->data);
2253 $lastrowkey = end($keys);
2254 $output .= html_writer::start_tag('tbody', array());
2255
2256 foreach ($table->data as $key => $row) {
2257 if (($row === 'hr') && ($countcols)) {
2258 $output .= html_writer::tag('td', html_writer::tag('div', '', array('class' => 'tabledivider')), array('colspan' => $countcols));
2259 } else {
2260 // Convert array rows to html_table_rows and cell strings to html_table_cell objects
2261 if (!($row instanceof html_table_row)) {
2262 $newrow = new html_table_row();
2263
4954d6ed
FM
2264 foreach ($row as $cell) {
2265 if (!($cell instanceof html_table_cell)) {
2266 $cell = new html_table_cell($cell);
2267 }
16be8974
DM
2268 $newrow->cells[] = $cell;
2269 }
2270 $row = $newrow;
2271 }
2272
16be8974
DM
2273 if (isset($table->rowclasses[$key])) {
2274 $row->attributes['class'] .= ' ' . $table->rowclasses[$key];
2275 }
2276
16be8974
DM
2277 if ($key == $lastrowkey) {
2278 $row->attributes['class'] .= ' lastrow';
2279 }
2280
7dfaa3ae
AN
2281 // Explicitly assigned properties should override those defined in the attributes.
2282 $row->attributes['class'] = trim($row->attributes['class']);
2283 $trattributes = array_merge($row->attributes, array(
2284 'id' => $row->id,
2285 'style' => $row->style,
2286 ));
2287 $output .= html_writer::start_tag('tr', $trattributes) . "\n";
16be8974
DM
2288 $keys2 = array_keys($row->cells);
2289 $lastkey = end($keys2);
2290
5174f3c5 2291 $gotlastkey = false; //flag for sanity checking
16be8974 2292 foreach ($row->cells as $key => $cell) {
5174f3c5
AD
2293 if ($gotlastkey) {
2294 //This should never happen. Why do we have a cell after the last cell?
2295 mtrace("A cell with key ($key) was found after the last key ($lastkey)");
2296 }
2297
16be8974
DM
2298 if (!($cell instanceof html_table_cell)) {
2299 $mycell = new html_table_cell();
2300 $mycell->text = $cell;
2301 $cell = $mycell;
2302 }
2303
e126c0cc
DM
2304 if (($cell->header === true) && empty($cell->scope)) {
2305 $cell->scope = 'row';
2306 }
2307
16be8974
DM
2308 if (isset($table->colclasses[$key])) {
2309 $cell->attributes['class'] .= ' ' . $table->colclasses[$key];
2310 }
2311
2312 $cell->attributes['class'] .= ' cell c' . $key;
2313 if ($key == $lastkey) {
2314 $cell->attributes['class'] .= ' lastcol';
5174f3c5 2315 $gotlastkey = true;
16be8974
DM
2316 }
2317 $tdstyle = '';
2318 $tdstyle .= isset($table->align[$key]) ? $table->align[$key] : '';
2319 $tdstyle .= isset($table->size[$key]) ? $table->size[$key] : '';
2320 $tdstyle .= isset($table->wrap[$key]) ? $table->wrap[$key] : '';
e126c0cc 2321 $cell->attributes['class'] = trim($cell->attributes['class']);
16be8974
DM
2322 $tdattributes = array_merge($cell->attributes, array(
2323 'style' => $tdstyle . $cell->style,
2324 'colspan' => $cell->colspan,
2325 'rowspan' => $cell->rowspan,
2326 'id' => $cell->id,
2327 'abbr' => $cell->abbr,
2328 'scope' => $cell->scope,
2329 ));
2330 $tagtype = 'td';
2331 if ($cell->header === true) {
2332 $tagtype = 'th';
2333 }
2334 $output .= html_writer::tag($tagtype, $cell->text, $tdattributes) . "\n";
2335 }
2336 }
2337 $output .= html_writer::end_tag('tr') . "\n";
2338 }
2339 $output .= html_writer::end_tag('tbody') . "\n";
2340 }
2341 $output .= html_writer::end_tag('table') . "\n";
2342
2343 return $output;
2344 }
2345
995f2d51
DM
2346 /**
2347 * Renders form element label
2348 *
2349 * By default, the label is suffixed with a label separator defined in the
2350 * current language pack (colon by default in the English lang pack).
2351 * Adding the colon can be explicitly disabled if needed. Label separators
2352 * are put outside the label tag itself so they are not read by
2353 * screenreaders (accessibility).
2354 *
2355 * Parameter $for explicitly associates the label with a form control. When
2356 * set, the value of this attribute must be the same as the value of
2357 * the id attribute of the form control in the same document. When null,
2358 * the label being defined is associated with the control inside the label
2359 * element.
2360 *
2361 * @param string $text content of the label tag
2362 * @param string|null $for id of the element this label is associated with, null for no association
2363 * @param bool $colonize add label separator (colon) to the label text, if it is not there yet
2364 * @param array $attributes to be inserted in the tab, for example array('accesskey' => 'a')
2365 * @return string HTML of the label element
2366 */
9678c7b8 2367 public static function label($text, $for, $colonize = true, array $attributes=array()) {
995f2d51
DM
2368 if (!is_null($for)) {
2369 $attributes = array_merge($attributes, array('for' => $for));
2370 }
2371 $text = trim($text);
2372 $label = self::tag('label', $text, $attributes);
2373
9678c7b8
SH
2374 // TODO MDL-12192 $colonize disabled for now yet
2375 // if (!empty($text) and $colonize) {
2376 // // the $text may end with the colon already, though it is bad string definition style
2377 // $colon = get_string('labelsep', 'langconfig');
2378 // if (!empty($colon)) {
2379 // $trimmed = trim($colon);
2380 // if ((substr($text, -strlen($trimmed)) == $trimmed) or (substr($text, -1) == ':')) {
2381 // //debugging('The label text should not end with colon or other label separator,
2382 // // please fix the string definition.', DEBUG_DEVELOPER);
2383 // } else {
2384 // $label .= $colon;
2385 // }
2386 // }
2387 // }
995f2d51
DM
2388
2389 return $label;
2390 }
4c4e5876 2391
2392 /**
2393 * Combines a class parameter with other attributes. Aids in code reduction
2394 * because the class parameter is very frequently used.
2395 *
2396 * If the class attribute is specified both in the attributes and in the
2397 * class parameter, the two values are combined with a space between.
2398 *
2399 * @param string $class Optional CSS class (or classes as space-separated list)
2400 * @param array $attributes Optional other attributes as array
2401 * @return array Attributes (or null if still none)
2402 */
2403 private static function add_class($class = '', array $attributes = null) {
2404 if ($class !== '') {
2405 $classattribute = array('class' => $class);
2406 if ($attributes) {
2407 if (array_key_exists('class', $attributes)) {
2408 $attributes['class'] = trim($attributes['class'] . ' ' . $class);
2409 } else {
2410 $attributes = $classattribute + $attributes;
2411 }
2412 } else {
2413 $attributes = $classattribute;
2414 }
2415 }
2416 return $attributes;
2417 }
2418
2419 /**
2420 * Creates a <div> tag. (Shortcut function.)
2421 *
2422 * @param string $content HTML content of tag
2423 * @param string $class Optional CSS class (or classes as space-separated list)
2424 * @param array $attributes Optional other attributes as array
2425 * @return string HTML code for div
2426 */
2427 public static function div($content, $class = '', array $attributes = null) {
2428 return self::tag('div', $content, self::add_class($class, $attributes));
2429 }
2430
2431 /**
2432 * Starts a <div> tag. (Shortcut function.)
2433 *
2434 * @param string $class Optional CSS class (or classes as space-separated list)
2435 * @param array $attributes Optional other attributes as array
2436 * @return string HTML code for open div tag
2437 */
2438 public static function start_div($class = '', array $attributes = null) {
2439 return self::start_tag('div', self::add_class($class, $attributes));
2440 }
2441
2442 /**
2443 * Ends a <div> tag. (Shortcut function.)
2444 *
2445 * @return string HTML code for close div tag
2446 */
2447 public static function end_div() {
2448 return self::end_tag('div');
2449 }
2450
2451 /**
2452 * Creates a <span> tag. (Shortcut function.)
2453 *
2454 * @param string $content HTML content of tag
2455 * @param string $class Optional CSS class (or classes as space-separated list)
2456 * @param array $attributes Optional other attributes as array
2457 * @return string HTML code for span
2458 */
2459 public static function span($content, $class = '', array $attributes = null) {
2460 return self::tag('span', $content, self::add_class($class, $attributes));
2461 }
2462
2463 /**
2464 * Starts a <span> tag. (Shortcut function.)
2465 *
2466 * @param string $class Optional CSS class (or classes as space-separated list)
2467 * @param array $attributes Optional other attributes as array
2468 * @return string HTML code for open span tag
2469 */
2470 public static function start_span($class = '', array $attributes = null) {
2471 return self::start_tag('span', self::add_class($class, $attributes));
2472 }
2473
2474 /**
2475 * Ends a <span> tag. (Shortcut function.)
2476 *
2477 * @return string HTML code for close span tag
2478 */
2479 public static function end_span() {
2480 return self::end_tag('span');
2481 }
5d0c95a5
PS
2482}
2483
227255b8
PS
2484/**
2485 * Simple javascript output class
9678c7b8 2486 *
227255b8 2487 * @copyright 2010 Petr Skoda
9678c7b8
SH
2488 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2489 * @since Moodle 2.0
f8129210 2490 * @package core
76be40cc 2491 * @category output
227255b8
PS
2492 */
2493class js_writer {
9678c7b8 2494
227255b8
PS
2495 /**
2496 * Returns javascript code calling the function
9678c7b8 2497 *
1a10840e 2498 * @param string $function function name, can be complex like Y.Event.purgeElement
227255b8
PS
2499 * @param array $arguments parameters
2500 * @param int $delay execution delay in seconds
2501 * @return string JS code fragment
2502 */
e839dce1 2503 public static function function_call($function, array $arguments = null, $delay=0) {
1b4e41af 2504 if ($arguments) {
b2cb00c2 2505 $arguments = array_map('json_encode', convert_to_array($arguments));
1b4e41af
PS
2506 $arguments = implode(', ', $arguments);
2507 } else {
2508 $arguments = '';
2509 }
227255b8
PS
2510 $js = "$function($arguments);";
2511
2512 if ($delay) {
2513 $delay = $delay * 1000; // in miliseconds
2514 $js = "setTimeout(function() { $js }, $delay);";
2515 }
2516 return $js . "\n";
2517 }
2518
3b01539c 2519 /**
3d3fae72 2520 * Special function which adds Y as first argument of function call.
9678c7b8 2521 *
3d3fae72
SH
2522 * @param string $function The function to call
2523 * @param array $extraarguments Any arguments to pass to it
2524 * @return string Some JS code
3b01539c 2525 */
e839dce1 2526 public static function function_call_with_Y($function, array $extraarguments = null) {
3b01539c 2527 if ($extraarguments) {
b2cb00c2 2528 $extraarguments = array_map('json_encode', convert_to_array($extraarguments));
3b01539c
PS
2529 $arguments = 'Y, ' . implode(', ', $extraarguments);
2530 } else {
2531 $arguments = 'Y';
2532 }
2533 return "$function($arguments);\n";
2534 }
2535
1ce15fda
SH
2536 /**
2537 * Returns JavaScript code to initialise a new object
9678c7b8 2538 *
3d3fae72
SH
2539 * @param string $var If it is null then no var is assigned the new object.
2540 * @param string $class The class to initialise an object for.
2541 * @param array $arguments An array of args to pass to the init method.
2542 * @param array $requirements Any modules required for this class.
2543 * @param int $delay The delay before initialisation. 0 = no delay.
2544 * @return string Some JS code
1ce15fda 2545 */
e839dce1 2546 public static function object_init($var, $class, array $arguments = null, array $requirements = null, $delay=0) {
1ce15fda 2547 if (is_array($arguments)) {
b2cb00c2 2548 $arguments = array_map('json_encode', convert_to_array($arguments));
1ce15fda
SH
2549 $arguments = implode(', ', $arguments);
2550 }
2551
2552 if ($var === null) {
53fc3e70 2553 $js = "new $class(Y, $arguments);";
1ce15fda 2554 } else if (strpos($var, '.')!==false) {
53fc3e70 2555 $js = "$var = new $class(Y, $arguments);";
1ce15fda 2556 } else {
53fc3e70 2557 $js = "var $var = new $class(Y, $arguments);";
1ce15fda
SH
2558 }
2559
2560 if ($delay) {
2561 $delay = $delay * 1000; // in miliseconds
2562 $js = "setTimeout(function() { $js }, $delay);";
2563 }
2564
2565 if (count($requirements) > 0) {
2566 $requirements = implode("', '", $requirements);
53fc3e70 2567 $js = "Y.use('$requirements', function(Y){ $js });";
1ce15fda
SH
2568 }
2569 return $js."\n";
2570 }
2571
227255b8
PS
2572 /**
2573 * Returns code setting value to variable
9678c7b8 2574 *
227255b8
PS
2575 * @param string $name
2576 * @param mixed $value json serialised value
2577 * @param bool $usevar add var definition, ignored for nested properties
2578 * @return string JS code fragment
2579 */
9678c7b8 2580 public static function set_variable($name, $value, $usevar = true) {
227255b8
PS
2581 $output = '';
2582
2583 if ($usevar) {
2584 if (strpos($name, '.')) {
2585 $output .= '';
2586 } else {
2587 $output .= 'var ';
2588 }
2589 }
2590
2591 $output .= "$name = ".json_encode($value).";";
2592
2593 return $output;
2594 }
2595
2596 /**
2597 * Writes event handler attaching code
9678c7b8 2598 *
f8129210
SH
2599 * @param array|string $selector standard YUI selector for elements, may be
2600 * array or string, element id is in the form "#idvalue"
227255b8
PS
2601 * @param string $event A valid DOM event (click, mousedown, change etc.)
2602 * @param string $function The name of the function to call
9678c7b8 2603 * @param array $arguments An optional array of argument parameters to pass to the function
227255b8
PS
2604 * @return string JS code fragment
2605 */
e839dce1 2606 public static function event_handler($selector, $event, $function, array $arguments = null) {
227255b8
PS
2607 $selector = json_encode($selector);
2608 $output = "Y.on('$event', $function, $selector, null";
2609 if (!empty($arguments)) {
2610 $output .= ', ' . json_encode($arguments);
2611 }
2612 return $output . ");\n";
2613 }
2614}
2615
d9c8f425 2616/**
f8129210 2617 * Holds all the information required to render a <table> by {@link core_renderer::table()}
d9c8f425 2618 *
16be8974
DM
2619 * Example of usage:
2620 * $t = new html_table();
2621 * ... // set various properties of the object $t as described below
2622 * echo html_writer::table($t);
d9c8f425 2623 *
16be8974 2624 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
9678c7b8
SH
2625 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2626 * @since Moodle 2.0
f8129210 2627 * @package core
76be40cc 2628 * @category output
d9c8f425 2629 */
16be8974 2630class html_table {
9678c7b8 2631
d9c8f425 2632 /**
76be40cc 2633 * @var string Value to use for the id attribute of the table
d9c8f425 2634 */
97c10099 2635 public $id = null;
9678c7b8 2636
d9c8f425 2637 /**
76be40cc 2638 * @var array Attributes of HTML attributes for the <table> element
d9c8f425 2639 */
16be8974 2640 public $attributes = array();
9678c7b8 2641
7b1f2c82 2642 /**
76be40cc 2643 * @var array An array of headings. The n-th array item is used as a heading of the n-th column.
a0ead5eb 2644 * For more control over the rendering of the headers, an array of html_table_cell objects
54a007e8 2645 * can be passed instead of an array of strings.
7b1f2c82 2646 *
2647 * Example of usage:
2648 * $t->head = array('Student', 'Grade');
2649 */
2650 public $head;
9678c7b8 2651
7b1f2c82 2652 /**
76be40cc 2653 * @var array An array that can be used to make a heading span multiple columns.
f8129210
SH
2654 * In this example, {@link html_table:$data} is supposed to have three columns. For the first two columns,
2655 * the same heading is used. Therefore, {@link html_table::$head} should consist of two items.
7b1f2c82 2656 *
2657 * Example of usage:
2658 * $t->headspan = array(2,1);
7b1f2c82 2659 */
2660 public $headspan;
9678c7b8 2661
7b1f2c82 2662 /**
76be40cc 2663 * @var array An array of column alignments.
9678c7b8 2664 * The value is used as CSS 'text-align' property. Therefore, possible
7b1f2c82 2665 * values are 'left', 'right', 'center' and 'justify'. Specify 'right' or 'left' from the perspective
2666 * of a left-to-right (LTR) language. For RTL, the values are flipped automatically.
2667 *
beb56299 2668 * Examples of usage:
2669 * $t->align = array(null, 'right');
2670 * or
2671 * $t->align[1] = 'right';
d9c8f425 2672 */
beb56299 2673 public $align;
9678c7b8 2674
d9c8f425 2675 /**
76be40cc 2676 * @var array The value is used as CSS 'size' property.
beb56299 2677 *
2678 * Examples of usage:
2679 * $t->size = array('50%', '50%');
2680 * or
2681 * $t->size[1] = '120px';
d9c8f425 2682 */
beb56299 2683 public $size;
9678c7b8 2684
d9c8f425 2685 /**
76be40cc 2686 * @var array An array of wrapping information.
9678c7b8 2687 * The only possible value is 'nowrap' that sets the
beb56299 2688 * CSS property 'white-space' to the value 'nowrap' in the given column.
2689 *
2690 * Example of usage:
2691 * $t->wrap = array(null, 'nowrap');
d9c8f425 2692 */
beb56299 2693 public $wrap;
9678c7b8 2694
d9c8f425 2695 /**
76be40cc 2696 * @var array Array of arrays or html_table_row objects containing the data. Alternatively, if you have
beb56299 2697 * $head specified, the string 'hr' (for horizontal ruler) can be used
2698 * instead of an array of cells data resulting in a divider rendered.
d9c8f425 2699 *
beb56299 2700 * Example of usage with array of arrays:
2701 * $row1 = array('Harry Potter', '76 %');
2702 * $row2 = array('Hermione Granger', '100 %');
2703 * $t->data = array($row1, $row2);
d9c8f425 2704 *
beb56299 2705 * Example with array of html_table_row objects: (used for more fine-grained control)
2706 * $cell1 = new html_table_cell();
2707 * $cell1->text = 'Harry Potter';
2708 * $cell1->colspan = 2;
2709 * $row1 = new html_table_row();
2710 * $row1->cells[] = $cell1;
2711 * $cell2 = new html_table_cell();
2712 * $cell2->text = 'Hermione Granger';
2713 * $cell3 = new html_table_cell();
2714 * $cell3->text = '100 %';
2715 * $row2 = new html_table_row();
2716 * $row2->cells = array($cell2, $cell3);
2717 * $t->data = array($row1, $row2);
2718 */
78da366b 2719 public $data = [];
9678c7b8 2720
beb56299 2721 /**
beb56299 2722 * @deprecated since Moodle 2.0. Styling should be in the CSS.
76be40cc 2723 * @var string Width of the table, percentage of the page preferred.
beb56299 2724 */
2725 public $width = null;
9678c7b8 2726
beb56299 2727 /**
beb56299 2728 * @deprecated since Moodle 2.0. Styling should be in the CSS.
76be40cc 2729 * @var string Alignment for the whole table. Can be 'right', 'left' or 'center' (default).
beb56299 2730 */
2731 public $tablealign = null;
9678c7b8 2732
beb56299 2733 /**
beb56299 2734 * @deprecated since Moodle 2.0. Styling should be in the CSS.
76be40cc 2735 * @var int Padding on each cell, in pixels
beb56299 2736 */
2737 public $cellpadding = null;
9678c7b8 2738
beb56299 2739 /**
76be40cc 2740 * @var int Spacing between cells, in pixels
beb56299 2741 * @deprecated since Moodle 2.0. Styling should be in the CSS.
2742 */
2743 public $cellspacing = null;
9678c7b8 2744
beb56299 2745 /**
76be40cc 2746 * @var array Array of classes to add to particular rows, space-separated string.
ba55be91 2747 * Class 'lastrow' is added automatically for the last row in the table.
d9c8f425 2748 *
beb56299 2749 * Example of usage:
2750 * $t->rowclasses[9] = 'tenth'
2751 */
2752 public $rowclasses;
9678c7b8 2753
beb56299 2754 /**
76be40cc 2755 * @var array An array of classes to add to every cell in a particular column,
beb56299 2756 * space-separated string. Class 'cell' is added automatically by the renderer.
2757 * Classes 'c0' or 'c1' are added automatically for every odd or even column,
2758 * respectively. Class 'lastcol' is added automatically for all last cells
2759 * in a row.
d9c8f425 2760 *
beb56299 2761 * Example of usage:
2762 * $t->colclasses = array(null, 'grade');
d9c8f425 2763 */
beb56299 2764 public $colclasses;
9678c7b8 2765
beb56299 2766 /**
76be40cc 2767 * @var string Description of the contents for screen readers.
beb56299 2768 */
2769 public $summary;
d9c8f425 2770
3451a51c
JC
2771 /**
2772 * @var string Caption for the table, typically a title.
2773 *
2774 * Example of usage:
2775 * $t->caption = "TV Guide";
2776 */
2777 public $caption;
2778
2779 /**
2780 * @var bool Whether to hide the table's caption from sighted users.
2781 *
2782 * Example of usage:
2783 * $t->caption = "TV Guide";
2784 * $t->captionhide = true;
2785 */
2786 public $captionhide = false;
2787
d9c8f425 2788 /**
16be8974 2789 * Constructor
d9c8f425 2790 */
16be8974
DM
2791 public function __construct() {
2792 $this->attributes['class'] = '';
d9c8f425 2793 }
d9c8f425 2794}
2795
2796/**
7b1f2c82 2797 * Component representing a table row.
d9c8f425 2798 *
2799 * @copyright 2009 Nicolas Connault
9678c7b8
SH
2800 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2801 * @since Moodle 2.0
f8129210 2802 * @package core
76be40cc 2803 * @category output
d9c8f425 2804 */
16be8974 2805class html_table_row {
9678c7b8 2806
16be8974 2807 /**
76be40cc 2808 * @var string Value to use for the id attribute of the row.
16be8974
DM
2809 */
2810 public $id = null;
9678c7b8 2811
d9c8f425 2812 /**
76be40cc 2813 * @var array Array of html_table_cell objects
d9c8f425 2814 */
7b1f2c82 2815 public $cells = array();
9678c7b8 2816
beb56299 2817 /**
76be40cc 2818 * @var string Value to use for the style attribute of the table row
beb56299 2819 */
16be8974 2820 public $style = null;
9678c7b8 2821
16be8974 2822 /**
76be40cc 2823 * @var array Attributes of additional HTML attributes for the <tr> element
16be8974
DM
2824 */
2825 public $attributes = array();
a0ead5eb 2826
54a007e8 2827 /**
8cea545e 2828 * Constructor
54a007e8 2829 * @param array $cells
8cea545e
PS
2830 */
2831 public function __construct(array $cells=null) {
16be8974 2832 $this->attributes['class'] = '';
8cea545e
PS
2833 $cells = (array)$cells;
2834 foreach ($cells as $cell) {
2835 if ($cell instanceof html_table_cell) {
2836 $this->cells[] = $cell;
a019627a 2837 } else {
8cea545e 2838 $this->cells[] = new html_table_cell($cell);
a019627a 2839 }
2840 }
54a007e8 2841 }
d9c8f425 2842}
2843
2844/**
7b1f2c82 2845 * Component representing a table cell.
d9c8f425 2846 *
2847 * @copyright 2009 Nicolas Connault
9678c7b8
SH
2848 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2849 * @since Moodle 2.0
f8129210 2850 * @package core
76be40cc 2851 * @category output
d9c8f425 2852 */
16be8974 2853class html_table_cell {
9678c7b8 2854
16be8974 2855 /**
76be40cc 2856 * @var string Value to use for the id attribute of the cell.
16be8974
DM
2857 */
2858 public $id = null;
9678c7b8 2859
d9c8f425 2860 /**
76be40cc 2861 * @var string The contents of the cell.
d9c8f425 2862 */
7b1f2c82 2863 public $text;
9678c7b8 2864
d9c8f425 2865 /**
76be40cc 2866 * @var string Abbreviated version of the contents of the cell.
d9c8f425 2867 */
97c10099 2868 public $abbr = null;
9678c7b8 2869
d9c8f425 2870 /**
76be40cc 2871 * @var int Number of columns this cell should span.
d9c8f425 2872 */
97c10099 2873 public $colspan = null;
9678c7b8 2874
d9c8f425 2875 /**
76be40cc 2876 * @var int Number of rows this cell should span.
d9c8f425 2877 */
97c10099 2878 public $rowspan = null;
9678c7b8 2879
d9c8f425 2880 /**
76be40cc 2881 * @var string Defines a way to associate header cells and data cells in a table.
d9c8f425 2882 */
97c10099 2883 public $scope = null;
9678c7b8 2884
1ae3767a 2885 /**
3d3fae72 2886 * @var bool Whether or not this cell is a header cell.
1ae3767a 2887 */
a4998d01 2888 public $header = null;
9678c7b8 2889
16be8974 2890 /**
76be40cc 2891 * @var string Value to use for the style attribute of the table cell
16be8974
DM
2892 */
2893 public $style = null;
9678c7b8 2894
16be8974 2895 /**
76be40cc 2896 * @var array Attributes of additional HTML attributes for the <td> element
16be8974
DM
2897 */
2898 public $attributes = array();
d9c8f425 2899
9678c7b8
SH
2900 /**
2901 * Constructs a table cell
2902 *
2903 * @param string $text
2904 */
8cea545e
PS
2905 public function __construct($text = null) {
2906 $this->text = $text;
16be8974 2907 $this->attributes['class'] = '';
d9c8f425 2908 }
2909}
2910
d9c8f425 2911/**
beb56299 2912 * Component representing a paging bar.
d9c8f425 2913 *
2914 * @copyright 2009 Nicolas Connault
9678c7b8
SH
2915 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2916 * @since Moodle 2.0
f8129210 2917 * @package core
76be40cc 2918 * @category output
d9c8f425 2919 */
b0da86e0 2920class paging_bar implements renderable, templatable {
9678c7b8 2921
d9c8f425 2922 /**
76be40cc 2923 * @var int The maximum number of pagelinks to display.
d9c8f425 2924 */
beb56299 2925 public $maxdisplay = 18;
9678c7b8 2926
d9c8f425 2927 /**
76be40cc 2928 * @var int The total number of entries to be pages through..
d9c8f425 2929 */
beb56299 2930 public $totalcount;
9678c7b8 2931
d9c8f425 2932 /**
76be40cc 2933 * @var int The page you are currently viewing.
d9c8f425 2934 */
929d7a83 2935 public $page;
9678c7b8 2936
d9c8f425 2937 /**
76be40cc 2938 * @var int The number of entries that should be shown per page.
d9c8f425 2939 */
beb56299 2940 public $perpage;
9678c7b8 2941
d9c8f425 2942 /**
76be40cc 2943 * @var string|moodle_url If this is a string then it is the url which will be appended with $pagevar,
9678c7b8
SH
2944 * an equals sign and the page number.
2945 * If this is a moodle_url object then the pagevar param will be replaced by
2946 * the page no, for each page.
d9c8f425 2947 */
beb56299 2948 public $baseurl;
9678c7b8 2949
d9c8f425 2950 /**
76be40cc 2951 * @var string This is the variable name that you use for the pagenumber in your
9678c7b8 2952 * code (ie. 'tablepage', 'blogpage', etc)
d9c8f425 2953 */
929d7a83 2954 public $pagevar;
9678c7b8 2955
beb56299 2956 /**
76be40cc 2957 * @var string A HTML link representing the "previous" page.
beb56299 2958 */
2959 public $previouslink = null;
9678c7b8 2960
beb56299 2961 /**
76be40cc 2962 * @var string A HTML link representing the "next" page.
beb56299 2963 */
2964 public $nextlink = null;
9678c7b8 2965
beb56299 2966 /**
76be40cc 2967 * @var string A HTML link representing the first page.
beb56299 2968 */
2969 public $firstlink = null;
9678c7b8 2970
beb56299 2971 /**
76be40cc 2972 * @var string A HTML link representing the last page.
beb56299 2973 */
2974 public $lastlink = null;
9678c7b8 2975
beb56299 2976 /**
76be40cc 2977 * @var array An array of strings. One of them is just a string: the current page
beb56299 2978 */
2979 public $pagelinks = array();
d9c8f425 2980
929d7a83
PS
2981 /**
2982 * Constructor paging_bar with only the required params.
2983 *
1a10840e 2984 * @param int $totalcount The total number of entries available to be paged through
929d7a83
PS
2985 * @param int $page The page you are currently viewing
2986 * @param int $perpage The number of entries that should be shown per page
2987 * @param string|moodle_url $baseurl url of the current page, the $pagevar parameter is added
2988 * @param string $pagevar name of page parameter that holds the page number
2989 */
2990 public function __construct($totalcount, $page, $perpage, $baseurl, $pagevar = 'page') {
2991 $this->totalcount = $totalcount;
2992 $this->page = $page;
2993 $this->perpage = $perpage;
2994 $this->baseurl = $baseurl;
2995 $this->pagevar = $pagevar;
2996 }
2997
d9c8f425 2998 /**
9678c7b8
SH
2999 * Prepares the paging bar for output.
3000 *
3001 * This method validates the arguments set up for the paging bar and then
3002 * produces fragments of HTML to assist display later on.
f8129210
SH
3003 *
3004 * @param renderer_base $output
3005 * @param moodle_page $page
3006 * @param string $target
3007 * @throws coding_exception
d9c8f425 3008 */
34059565 3009 public function prepare(renderer_base $output, moodle_page $page, $target) {
1c1f64a2 3010 if (!isset($this->totalcount) || is_null($this->totalcount)) {
929d7a83 3011 throw new coding_exception('paging_bar requires a totalcount value.');
beb56299 3012 }
3013 if (!isset($this->page) || is_null($this->page)) {
929d7a83 3014 throw new coding_exception('paging_bar requires a page value.');
beb56299 3015 }
3016 if (empty($this->perpage)) {
929d7a83 3017 throw new coding_exception('paging_bar requires a perpage value.');
beb56299 3018 }
3019 if (empty($this->baseurl)) {
929d7a83 3020 throw new coding_exception('paging_bar requires a baseurl value.');
beb56299 3021 }
d9c8f425 3022
beb56299 3023 if ($this->totalcount > $this->perpage) {
3024 $pagenum = $this->page - 1;
d9c8f425 3025
beb56299 3026 if ($this->page > 0) {
929d7a83 3027 $this->previouslink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('previous'), array('class'=>'previous'));
beb56299 3028 }
d9c8f425 3029
beb56299 3030 if ($this->perpage > 0) {
3031 $lastpage = ceil($this->totalcount / $this->perpage);
3032 } else {
3033 $lastpage = 1;
3034 }
3035
0232fa7b
JF
3036 if ($this->page > round(($this->maxdisplay/3)*2)) {
3037 $currpage = $this->page - round($this->maxdisplay/3);
beb56299 3038
929d7a83 3039 $this->firstlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>0)), '1', array('class'=>'first'));
beb56299 3040 } else {
0232fa7b 3041 $currpage = 0;
beb56299 3042 }
3043
beb56299 3044 $displaycount = $displaypage = 0;
3045
3046 while ($displaycount < $this->maxdisplay and $currpage < $lastpage) {
3047 $displaypage = $currpage + 1;
3048
f43cdceb 3049 if ($this->page == $currpage) {
1c77e2aa 3050 $this->pagelinks[] = html_writer::span($displaypage, 'current-page');
beb56299 3051 } else {
56ddb719 3052 $pagelink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$currpage)), $displaypage);
beb56299 3053 $this->pagelinks[] = $pagelink;
3054 }
3055
3056 $displaycount++;
3057 $currpage++;
3058 }
3059
3060 if ($currpage < $lastpage) {
3061 $lastpageactual = $lastpage - 1;
abdac127 3062 $this->lastlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$lastpageactual)), $lastpage, array('class'=>'last'));
beb56299 3063 }
3064
3065 $pagenum = $this->page + 1;
3066
b31bbcf8 3067 if ($pagenum != $lastpage) {
abdac127 3068 $this->nextlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('next'), array('class'=>'next'));
beb56299 3069 }
d9c8f425 3070 }
3071 }
b0da86e0
FM
3072
3073 /**
3074 * Export for template.
3075 *
3076 * @param renderer_base $output The renderer.
3077 * @return stdClass
3078 */
3079 public function export_for_template(renderer_base $output) {
3080 $data = new stdClass();
3081 $data->previous = null;
3082 $data->next = null;
3083 $data->first = null;
3084 $data->last = null;
3085 $data->label = get_string('page');
3086 $data->pages = [];
3087 $data->haspages = $this->totalcount > $this->perpage;
f7b84afe 3088 $data->pagesize = $this->perpage;
b0da86e0
FM
3089
3090 if (!$data->haspages) {
3091 return $data;
3092 }
3093
3094 if ($this->page > 0) {
3095 $data->previous = [
3e5a8acc 3096 'page' => $this->page,
b0da86e0
FM
3097 'url' => (new moodle_url($this->baseurl, [$this->pagevar => $this->page - 1]))->out(false)
3098 ];
3099 }
3100
3101 $currpage = 0;
3102 if ($this->page > round(($this->maxdisplay / 3) * 2)) {
3103 $currpage = $this->page - round($this->maxdisplay / 3);
3104 $data->first = [
3105 'page' => 1,
3106 'url' => (new moodle_url($this->baseurl, [$this->pagevar => 0]))->out(false)
3107 ];
3108 }
3109
3110 $lastpage = 1;
3111 if ($this->perpage > 0) {
3112 $lastpage = ceil($this->totalcount / $this->perpage);
3113 }
3114
3115 $displaycount = 0;
3116 $displaypage = 0;
3117 while ($displaycount < $this->maxdisplay and $currpage < $lastpage) {
3118 $displaypage = $currpage + 1;
3119
3120 $iscurrent = $this->page == $currpage;
3121 $link = new moodle_url($this->baseurl, [$this->pagevar => $currpage]);
3122
3123 $data->pages[] = [
3124 'page' => $displaypage,
3125 'active' => $iscurrent,
3126 'url' => $iscurrent ? null : $link->out(false)
3127 ];
3128
3129 $displaycount++;
3130 $currpage++;
3131 }
3132
3133 if ($currpage < $lastpage) {
3134 $data->last = [
3135 'page' => $lastpage,
3136 'url' => (new moodle_url($this->baseurl, [$this->pagevar => $lastpage - 1]))->out(false)
3137 ];
3138 }
3139
3140 if ($this->page + 1 != $lastpage) {
3141 $data->next = [
3e5a8acc 3142 'page' => $this->page + 2,
b0da86e0
FM
3143 'url' => (new moodle_url($this->baseurl, [$this->pagevar => $this->page + 1]))->out(false)
3144 ];
3145 }
3146
3147 return $data;
3148 }
d9c8f425 3149}
3150
8b844f70
IT
3151/**
3152 * Component representing initials bar.
3153 *
3154 * @copyright 2017 Ilya Tregubov
3155 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3156 * @since Moodle 3.3
3157 * @package core
3158 * @category output
3159 */
3160class initials_bar implements renderable, templatable {
3161
3162 /**
3163 * @var string Currently selected letter.
3164 */
3165 public $current;
3166
3167 /**
3168 * @var string Class name to add to this initial bar.
3169 */
3170 public $class;
3171
3172 /**
3173 * @var string The name to put in front of this initial bar.
3174 */
3175 public $title;
3176
3177 /**
3178 * @var string URL parameter name for this initial.
3179 */
3180 public $urlvar;
3181
3182 /**
3183 * @var string URL object.
3184 */
3185 public $url;
3186
3187 /**
3188 * @var array An array of letters in the alphabet.
3189 */
3190 public $alpha;
3191
3192 /**
3193 * Constructor initials_bar with only the required params.
3194 *
3195 * @param string $current the currently selected letter.
3196 * @param string $class class name to add to this initial bar.
3197 * @param string $title the name to put in front of this initial bar.
3198 * @param string $urlvar URL parameter name for this initial.
3199 * @param string $url URL object.
3200 * @param array $alpha of letters in the alphabet.
3201 */
3202 public function __construct($current, $class, $title, $urlvar, $url, $alpha = null) {
3203 $this->current = $current;
3204 $this->class = $class;
3205 $this->title = $title;
3206 $this->urlvar = $urlvar;
3207 $this->url = $url;
3208 $this->alpha = $alpha;
3209 }
3210
3211 /**
3212 * Export for template.
3213 *