Merge branch 'MDL-49360-28' of git://github.com/lameze/moodle into MOODLE_28_STABLE
[moodle.git] / lib / outputcomponents.php
blob4752d7f9c2d21b1fbb691ed019ddf7b278a4c71a
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
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/>.
17 /**
18 * Classes representing HTML elements, used by $OUTPUT methods
20 * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
21 * for an overview.
23 * @package core
24 * @category output
25 * @copyright 2009 Tim Hunt
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
31 /**
32 * Interface marking other classes as suitable for renderer_base::render()
34 * @copyright 2010 Petr Skoda (skodak) info@skodak.org
35 * @package core
36 * @category output
38 interface renderable {
39 // intentionally empty
42 /**
43 * Data structure representing a file picker.
45 * @copyright 2010 Dongsheng Cai
46 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47 * @since Moodle 2.0
48 * @package core
49 * @category output
51 class file_picker implements renderable {
53 /**
54 * @var stdClass An object containing options for the file picker
56 public $options;
58 /**
59 * Constructs a file picker object.
61 * The following are possible options for the filepicker:
62 * - accepted_types (*)
63 * - return_types (FILE_INTERNAL)
64 * - env (filepicker)
65 * - client_id (uniqid)
66 * - itemid (0)
67 * - maxbytes (-1)
68 * - maxfiles (1)
69 * - buttonname (false)
71 * @param stdClass $options An object containing options for the file picker.
73 public function __construct(stdClass $options) {
74 global $CFG, $USER, $PAGE;
75 require_once($CFG->dirroot. '/repository/lib.php');
76 $defaults = array(
77 'accepted_types'=>'*',
78 'return_types'=>FILE_INTERNAL,
79 'env' => 'filepicker',
80 'client_id' => uniqid(),
81 'itemid' => 0,
82 'maxbytes'=>-1,
83 'maxfiles'=>1,
84 'buttonname'=>false
86 foreach ($defaults as $key=>$value) {
87 if (empty($options->$key)) {
88 $options->$key = $value;
92 $options->currentfile = '';
93 if (!empty($options->itemid)) {
94 $fs = get_file_storage();
95 $usercontext = context_user::instance($USER->id);
96 if (empty($options->filename)) {
97 if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id DESC', false)) {
98 $file = reset($files);
100 } else {
101 $file = $fs->get_file($usercontext->id, 'user', 'draft', $options->itemid, $options->filepath, $options->filename);
103 if (!empty($file)) {
104 $options->currentfile = html_writer::link(moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()), $file->get_filename());
108 // initilise options, getting files in root path
109 $this->options = initialise_filepicker($options);
111 // copying other options
112 foreach ($options as $name=>$value) {
113 if (!isset($this->options->$name)) {
114 $this->options->$name = $value;
121 * Data structure representing a user picture.
123 * @copyright 2009 Nicolas Connault, 2010 Petr Skoda
124 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
125 * @since Modle 2.0
126 * @package core
127 * @category output
129 class user_picture implements renderable {
131 * @var array List of mandatory fields in user record here. (do not include
132 * TEXT columns because it would break SELECT DISTINCT in MSSQL and ORACLE)
134 protected static $fields = array('id', 'picture', 'firstname', 'lastname', 'firstnamephonetic', 'lastnamephonetic',
135 'middlename', 'alternatename', 'imagealt', 'email');
138 * @var stdClass A user object with at least fields all columns specified
139 * in $fields array constant set.
141 public $user;
144 * @var int The course id. Used when constructing the link to the user's
145 * profile, page course id used if not specified.
147 public $courseid;
150 * @var bool Add course profile link to image
152 public $link = true;
155 * @var int Size in pixels. Special values are (true/1 = 100px) and
156 * (false/0 = 35px)
157 * for backward compatibility.
159 public $size = 35;
162 * @var bool Add non-blank alt-text to the image.
163 * Default true, set to false when image alt just duplicates text in screenreaders.
165 public $alttext = true;
168 * @var bool Whether or not to open the link in a popup window.
170 public $popup = false;
173 * @var string Image class attribute
175 public $class = 'userpicture';
178 * @var bool Whether to be visible to screen readers.
180 public $visibletoscreenreaders = true;
183 * User picture constructor.
185 * @param stdClass $user user record with at least id, picture, imagealt, firstname and lastname set.
186 * It is recommended to add also contextid of the user for performance reasons.
188 public function __construct(stdClass $user) {
189 global $DB;
191 if (empty($user->id)) {
192 throw new coding_exception('User id is required when printing user avatar image.');
195 // only touch the DB if we are missing data and complain loudly...
196 $needrec = false;
197 foreach (self::$fields as $field) {
198 if (!array_key_exists($field, $user)) {
199 $needrec = true;
200 debugging('Missing '.$field.' property in $user object, this is a performance problem that needs to be fixed by a developer. '
201 .'Please use user_picture::fields() to get the full list of required fields.', DEBUG_DEVELOPER);
202 break;
206 if ($needrec) {
207 $this->user = $DB->get_record('user', array('id'=>$user->id), self::fields(), MUST_EXIST);
208 } else {
209 $this->user = clone($user);
214 * Returns a list of required user fields, useful when fetching required user info from db.
216 * In some cases we have to fetch the user data together with some other information,
217 * the idalias is useful there because the id would otherwise override the main
218 * id of the result record. Please note it has to be converted back to id before rendering.
220 * @param string $tableprefix name of database table prefix in query
221 * @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)
222 * @param string $idalias alias of id field
223 * @param string $fieldprefix prefix to add to all columns in their aliases, does not apply to 'id'
224 * @return string
226 public static function fields($tableprefix = '', array $extrafields = NULL, $idalias = 'id', $fieldprefix = '') {
227 if (!$tableprefix and !$extrafields and !$idalias) {
228 return implode(',', self::$fields);
230 if ($tableprefix) {
231 $tableprefix .= '.';
233 foreach (self::$fields as $field) {
234 if ($field === 'id' and $idalias and $idalias !== 'id') {
235 $fields[$field] = "$tableprefix$field AS $idalias";
236 } else {
237 if ($fieldprefix and $field !== 'id') {
238 $fields[$field] = "$tableprefix$field AS $fieldprefix$field";
239 } else {
240 $fields[$field] = "$tableprefix$field";
244 // add extra fields if not already there
245 if ($extrafields) {
246 foreach ($extrafields as $e) {
247 if ($e === 'id' or isset($fields[$e])) {
248 continue;
250 if ($fieldprefix) {
251 $fields[$e] = "$tableprefix$e AS $fieldprefix$e";
252 } else {
253 $fields[$e] = "$tableprefix$e";
257 return implode(',', $fields);
261 * Extract the aliased user fields from a given record
263 * Given a record that was previously obtained using {@link self::fields()} with aliases,
264 * this method extracts user related unaliased fields.
266 * @param stdClass $record containing user picture fields
267 * @param array $extrafields extra fields included in the $record
268 * @param string $idalias alias of the id field
269 * @param string $fieldprefix prefix added to all columns in their aliases, does not apply to 'id'
270 * @return stdClass object with unaliased user fields
272 public static function unalias(stdClass $record, array $extrafields = null, $idalias = 'id', $fieldprefix = '') {
274 if (empty($idalias)) {
275 $idalias = 'id';
278 $return = new stdClass();
280 foreach (self::$fields as $field) {
281 if ($field === 'id') {
282 if (property_exists($record, $idalias)) {
283 $return->id = $record->{$idalias};
285 } else {
286 if (property_exists($record, $fieldprefix.$field)) {
287 $return->{$field} = $record->{$fieldprefix.$field};
291 // add extra fields if not already there
292 if ($extrafields) {
293 foreach ($extrafields as $e) {
294 if ($e === 'id' or property_exists($return, $e)) {
295 continue;
297 $return->{$e} = $record->{$fieldprefix.$e};
301 return $return;
305 * Works out the URL for the users picture.
307 * This method is recommended as it avoids costly redirects of user pictures
308 * if requests are made for non-existent files etc.
310 * @param moodle_page $page
311 * @param renderer_base $renderer
312 * @return moodle_url
314 public function get_url(moodle_page $page, renderer_base $renderer = null) {
315 global $CFG;
317 if (is_null($renderer)) {
318 $renderer = $page->get_renderer('core');
321 // Sort out the filename and size. Size is only required for the gravatar
322 // implementation presently.
323 if (empty($this->size)) {
324 $filename = 'f2';
325 $size = 35;
326 } else if ($this->size === true or $this->size == 1) {
327 $filename = 'f1';
328 $size = 100;
329 } else if ($this->size > 100) {
330 $filename = 'f3';
331 $size = (int)$this->size;
332 } else if ($this->size >= 50) {
333 $filename = 'f1';
334 $size = (int)$this->size;
335 } else {
336 $filename = 'f2';
337 $size = (int)$this->size;
340 $defaulturl = $renderer->pix_url('u/'.$filename); // default image
342 if ((!empty($CFG->forcelogin) and !isloggedin()) ||
343 (!empty($CFG->forceloginforprofileimage) && (!isloggedin() || isguestuser()))) {
344 // Protect images if login required and not logged in;
345 // also if login is required for profile images and is not logged in or guest
346 // do not use require_login() because it is expensive and not suitable here anyway.
347 return $defaulturl;
350 // First try to detect deleted users - but do not read from database for performance reasons!
351 if (!empty($this->user->deleted) or strpos($this->user->email, '@') === false) {
352 // All deleted users should have email replaced by md5 hash,
353 // all active users are expected to have valid email.
354 return $defaulturl;
357 // Did the user upload a picture?
358 if ($this->user->picture > 0) {
359 if (!empty($this->user->contextid)) {
360 $contextid = $this->user->contextid;
361 } else {
362 $context = context_user::instance($this->user->id, IGNORE_MISSING);
363 if (!$context) {
364 // This must be an incorrectly deleted user, all other users have context.
365 return $defaulturl;
367 $contextid = $context->id;
370 $path = '/';
371 if (clean_param($page->theme->name, PARAM_THEME) == $page->theme->name) {
372 // We append the theme name to the file path if we have it so that
373 // in the circumstance that the profile picture is not available
374 // when the user actually requests it they still get the profile
375 // picture for the correct theme.
376 $path .= $page->theme->name.'/';
378 // Set the image URL to the URL for the uploaded file and return.
379 $url = moodle_url::make_pluginfile_url($contextid, 'user', 'icon', NULL, $path, $filename);
380 $url->param('rev', $this->user->picture);
381 return $url;
384 if ($this->user->picture == 0 and !empty($CFG->enablegravatar)) {
385 // Normalise the size variable to acceptable bounds
386 if ($size < 1 || $size > 512) {
387 $size = 35;
389 // Hash the users email address
390 $md5 = md5(strtolower(trim($this->user->email)));
391 // Build a gravatar URL with what we know.
393 // Find the best default image URL we can (MDL-35669)
394 if (empty($CFG->gravatardefaulturl)) {
395 $absoluteimagepath = $page->theme->resolve_image_location('u/'.$filename, 'core');
396 if (strpos($absoluteimagepath, $CFG->dirroot) === 0) {
397 $gravatardefault = $CFG->wwwroot . substr($absoluteimagepath, strlen($CFG->dirroot));
398 } else {
399 $gravatardefault = $CFG->wwwroot . '/pix/u/' . $filename . '.png';
401 } else {
402 $gravatardefault = $CFG->gravatardefaulturl;
405 // If the currently requested page is https then we'll return an
406 // https gravatar page.
407 if (is_https()) {
408 $gravatardefault = str_replace($CFG->wwwroot, $CFG->httpswwwroot, $gravatardefault); // Replace by secure url.
409 return new moodle_url("https://secure.gravatar.com/avatar/{$md5}", array('s' => $size, 'd' => $gravatardefault));
410 } else {
411 return new moodle_url("http://www.gravatar.com/avatar/{$md5}", array('s' => $size, 'd' => $gravatardefault));
415 return $defaulturl;
420 * Data structure representing a help icon.
422 * @copyright 2010 Petr Skoda (info@skodak.org)
423 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
424 * @since Moodle 2.0
425 * @package core
426 * @category output
428 class help_icon implements renderable {
431 * @var string lang pack identifier (without the "_help" suffix),
432 * both get_string($identifier, $component) and get_string($identifier.'_help', $component)
433 * must exist.
435 public $identifier;
438 * @var string Component name, the same as in get_string()
440 public $component;
443 * @var string Extra descriptive text next to the icon
445 public $linktext = null;
448 * Constructor
450 * @param string $identifier string for help page title,
451 * string with _help suffix is used for the actual help text.
452 * string with _link suffix is used to create a link to further info (if it exists)
453 * @param string $component
455 public function __construct($identifier, $component) {
456 $this->identifier = $identifier;
457 $this->component = $component;
461 * Verifies that both help strings exists, shows debug warnings if not
463 public function diag_strings() {
464 $sm = get_string_manager();
465 if (!$sm->string_exists($this->identifier, $this->component)) {
466 debugging("Help title string does not exist: [$this->identifier, $this->component]");
468 if (!$sm->string_exists($this->identifier.'_help', $this->component)) {
469 debugging("Help contents string does not exist: [{$this->identifier}_help, $this->component]");
476 * Data structure representing an icon.
478 * @copyright 2010 Petr Skoda
479 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
480 * @since Moodle 2.0
481 * @package core
482 * @category output
484 class pix_icon implements renderable {
487 * @var string The icon name
489 var $pix;
492 * @var string The component the icon belongs to.
494 var $component;
497 * @var array An array of attributes to use on the icon
499 var $attributes = array();
502 * Constructor
504 * @param string $pix short icon name
505 * @param string $alt The alt text to use for the icon
506 * @param string $component component name
507 * @param array $attributes html attributes
509 public function __construct($pix, $alt, $component='moodle', array $attributes = null) {
510 $this->pix = $pix;
511 $this->component = $component;
512 $this->attributes = (array)$attributes;
514 $this->attributes['alt'] = $alt;
515 if (empty($this->attributes['class'])) {
516 $this->attributes['class'] = 'smallicon';
518 if (!isset($this->attributes['title'])) {
519 $this->attributes['title'] = $this->attributes['alt'];
520 } else if (empty($this->attributes['title'])) {
521 // Remove the title attribute if empty, we probably want to use the parent node's title
522 // and some browsers might overwrite it with an empty title.
523 unset($this->attributes['title']);
529 * Data structure representing an emoticon image
531 * @copyright 2010 David Mudrak
532 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
533 * @since Moodle 2.0
534 * @package core
535 * @category output
537 class pix_emoticon extends pix_icon implements renderable {
540 * Constructor
541 * @param string $pix short icon name
542 * @param string $alt alternative text
543 * @param string $component emoticon image provider
544 * @param array $attributes explicit HTML attributes
546 public function __construct($pix, $alt, $component = 'moodle', array $attributes = array()) {
547 if (empty($attributes['class'])) {
548 $attributes['class'] = 'emoticon';
550 parent::__construct($pix, $alt, $component, $attributes);
555 * Data structure representing a simple form with only one button.
557 * @copyright 2009 Petr Skoda
558 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
559 * @since Moodle 2.0
560 * @package core
561 * @category output
563 class single_button implements renderable {
566 * @var moodle_url Target url
568 var $url;
571 * @var string Button label
573 var $label;
576 * @var string Form submit method post or get
578 var $method = 'post';
581 * @var string Wrapping div class
583 var $class = 'singlebutton';
586 * @var bool True if button disabled, false if normal
588 var $disabled = false;
591 * @var string Button tooltip
593 var $tooltip = null;
596 * @var string Form id
598 var $formid;
601 * @var array List of attached actions
603 var $actions = array();
606 * Constructor
607 * @param moodle_url $url
608 * @param string $label button text
609 * @param string $method get or post submit method
611 public function __construct(moodle_url $url, $label, $method='post') {
612 $this->url = clone($url);
613 $this->label = $label;
614 $this->method = $method;
618 * Shortcut for adding a JS confirm dialog when the button is clicked.
619 * The message must be a yes/no question.
621 * @param string $confirmmessage The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
623 public function add_confirm_action($confirmmessage) {
624 $this->add_action(new confirm_action($confirmmessage));
628 * Add action to the button.
629 * @param component_action $action
631 public function add_action(component_action $action) {
632 $this->actions[] = $action;
638 * Simple form with just one select field that gets submitted automatically.
640 * If JS not enabled small go button is printed too.
642 * @copyright 2009 Petr Skoda
643 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
644 * @since Moodle 2.0
645 * @package core
646 * @category output
648 class single_select implements renderable {
651 * @var moodle_url Target url - includes hidden fields
653 var $url;
656 * @var string Name of the select element.
658 var $name;
661 * @var array $options associative array value=>label ex.: array(1=>'One, 2=>Two)
662 * it is also possible to specify optgroup as complex label array ex.:
663 * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
664 * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
666 var $options;
669 * @var string Selected option
671 var $selected;
674 * @var array Nothing selected
676 var $nothing;
679 * @var array Extra select field attributes
681 var $attributes = array();
684 * @var string Button label
686 var $label = '';
689 * @var array Button label's attributes
691 var $labelattributes = array();
694 * @var string Form submit method post or get
696 var $method = 'get';
699 * @var string Wrapping div class
701 var $class = 'singleselect';
704 * @var bool True if button disabled, false if normal
706 var $disabled = false;
709 * @var string Button tooltip
711 var $tooltip = null;
714 * @var string Form id
716 var $formid = null;
719 * @var array List of attached actions
721 var $helpicon = null;
724 * Constructor
725 * @param moodle_url $url form action target, includes hidden fields
726 * @param string $name name of selection field - the changing parameter in url
727 * @param array $options list of options
728 * @param string $selected selected element
729 * @param array $nothing
730 * @param string $formid
732 public function __construct(moodle_url $url, $name, array $options, $selected = '', $nothing = array('' => 'choosedots'), $formid = null) {
733 $this->url = $url;
734 $this->name = $name;
735 $this->options = $options;
736 $this->selected = $selected;
737 $this->nothing = $nothing;
738 $this->formid = $formid;
742 * Shortcut for adding a JS confirm dialog when the button is clicked.
743 * The message must be a yes/no question.
745 * @param string $confirmmessage The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
747 public function add_confirm_action($confirmmessage) {
748 $this->add_action(new component_action('submit', 'M.util.show_confirm_dialog', array('message' => $confirmmessage)));
752 * Add action to the button.
754 * @param component_action $action
756 public function add_action(component_action $action) {
757 $this->actions[] = $action;
761 * Adds help icon.
763 * @deprecated since Moodle 2.0
765 public function set_old_help_icon($helppage, $title, $component = 'moodle') {
766 throw new coding_exception('set_old_help_icon() can not be used any more, please see set_help_icon().');
770 * Adds help icon.
772 * @param string $identifier The keyword that defines a help page
773 * @param string $component
775 public function set_help_icon($identifier, $component = 'moodle') {
776 $this->helpicon = new help_icon($identifier, $component);
780 * Sets select's label
782 * @param string $label
783 * @param array $attributes (optional)
785 public function set_label($label, $attributes = array()) {
786 $this->label = $label;
787 $this->labelattributes = $attributes;
793 * Simple URL selection widget description.
795 * @copyright 2009 Petr Skoda
796 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
797 * @since Moodle 2.0
798 * @package core
799 * @category output
801 class url_select implements renderable {
803 * @var array $urls associative array value=>label ex.: array(1=>'One, 2=>Two)
804 * it is also possible to specify optgroup as complex label array ex.:
805 * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
806 * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
808 var $urls;
811 * @var string Selected option
813 var $selected;
816 * @var array Nothing selected
818 var $nothing;
821 * @var array Extra select field attributes
823 var $attributes = array();
826 * @var string Button label
828 var $label = '';
831 * @var array Button label's attributes
833 var $labelattributes = array();
836 * @var string Wrapping div class
838 var $class = 'urlselect';
841 * @var bool True if button disabled, false if normal
843 var $disabled = false;
846 * @var string Button tooltip
848 var $tooltip = null;
851 * @var string Form id
853 var $formid = null;
856 * @var array List of attached actions
858 var $helpicon = null;
861 * @var string If set, makes button visible with given name for button
863 var $showbutton = null;
866 * Constructor
867 * @param array $urls list of options
868 * @param string $selected selected element
869 * @param array $nothing
870 * @param string $formid
871 * @param string $showbutton Set to text of button if it should be visible
872 * or null if it should be hidden (hidden version always has text 'go')
874 public function __construct(array $urls, $selected = '', $nothing = array('' => 'choosedots'), $formid = null, $showbutton = null) {
875 $this->urls = $urls;
876 $this->selected = $selected;
877 $this->nothing = $nothing;
878 $this->formid = $formid;
879 $this->showbutton = $showbutton;
883 * Adds help icon.
885 * @deprecated since Moodle 2.0
887 public function set_old_help_icon($helppage, $title, $component = 'moodle') {
888 throw new coding_exception('set_old_help_icon() can not be used any more, please see set_help_icon().');
892 * Adds help icon.
894 * @param string $identifier The keyword that defines a help page
895 * @param string $component
897 public function set_help_icon($identifier, $component = 'moodle') {
898 $this->helpicon = new help_icon($identifier, $component);
902 * Sets select's label
904 * @param string $label
905 * @param array $attributes (optional)
907 public function set_label($label, $attributes = array()) {
908 $this->label = $label;
909 $this->labelattributes = $attributes;
914 * Data structure describing html link with special action attached.
916 * @copyright 2010 Petr Skoda
917 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
918 * @since Moodle 2.0
919 * @package core
920 * @category output
922 class action_link implements renderable {
925 * @var moodle_url Href url
927 public $url;
930 * @var string Link text HTML fragment
932 public $text;
935 * @var array HTML attributes
937 public $attributes;
940 * @var array List of actions attached to link
942 public $actions;
945 * @var pix_icon Optional pix icon to render with the link
947 public $icon;
950 * Constructor
951 * @param moodle_url $url
952 * @param string $text HTML fragment
953 * @param component_action $action
954 * @param array $attributes associative array of html link attributes + disabled
955 * @param pix_icon $icon optional pix_icon to render with the link text
957 public function __construct(moodle_url $url,
958 $text,
959 component_action $action=null,
960 array $attributes=null,
961 pix_icon $icon=null) {
962 $this->url = clone($url);
963 $this->text = $text;
964 $this->attributes = (array)$attributes;
965 if ($action) {
966 $this->add_action($action);
968 $this->icon = $icon;
972 * Add action to the link.
974 * @param component_action $action
976 public function add_action(component_action $action) {
977 $this->actions[] = $action;
981 * Adds a CSS class to this action link object
982 * @param string $class
984 public function add_class($class) {
985 if (empty($this->attributes['class'])) {
986 $this->attributes['class'] = $class;
987 } else {
988 $this->attributes['class'] .= ' ' . $class;
993 * Returns true if the specified class has been added to this link.
994 * @param string $class
995 * @return bool
997 public function has_class($class) {
998 return strpos(' ' . $this->attributes['class'] . ' ', ' ' . $class . ' ') !== false;
1003 * Simple html output class
1005 * @copyright 2009 Tim Hunt, 2010 Petr Skoda
1006 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1007 * @since Moodle 2.0
1008 * @package core
1009 * @category output
1011 class html_writer {
1014 * Outputs a tag with attributes and contents
1016 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
1017 * @param string $contents What goes between the opening and closing tags
1018 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
1019 * @return string HTML fragment
1021 public static function tag($tagname, $contents, array $attributes = null) {
1022 return self::start_tag($tagname, $attributes) . $contents . self::end_tag($tagname);
1026 * Outputs an opening tag with attributes
1028 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
1029 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
1030 * @return string HTML fragment
1032 public static function start_tag($tagname, array $attributes = null) {
1033 return '<' . $tagname . self::attributes($attributes) . '>';
1037 * Outputs a closing tag
1039 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
1040 * @return string HTML fragment
1042 public static function end_tag($tagname) {
1043 return '</' . $tagname . '>';
1047 * Outputs an empty tag with attributes
1049 * @param string $tagname The name of tag ('input', 'img', 'br' etc.)
1050 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
1051 * @return string HTML fragment
1053 public static function empty_tag($tagname, array $attributes = null) {
1054 return '<' . $tagname . self::attributes($attributes) . ' />';
1058 * Outputs a tag, but only if the contents are not empty
1060 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
1061 * @param string $contents What goes between the opening and closing tags
1062 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
1063 * @return string HTML fragment
1065 public static function nonempty_tag($tagname, $contents, array $attributes = null) {
1066 if ($contents === '' || is_null($contents)) {
1067 return '';
1069 return self::tag($tagname, $contents, $attributes);
1073 * Outputs a HTML attribute and value
1075 * @param string $name The name of the attribute ('src', 'href', 'class' etc.)
1076 * @param string $value The value of the attribute. The value will be escaped with {@link s()}
1077 * @return string HTML fragment
1079 public static function attribute($name, $value) {
1080 if ($value instanceof moodle_url) {
1081 return ' ' . $name . '="' . $value->out() . '"';
1084 // special case, we do not want these in output
1085 if ($value === null) {
1086 return '';
1089 // no sloppy trimming here!
1090 return ' ' . $name . '="' . s($value) . '"';
1094 * Outputs a list of HTML attributes and values
1096 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
1097 * The values will be escaped with {@link s()}
1098 * @return string HTML fragment
1100 public static function attributes(array $attributes = null) {
1101 $attributes = (array)$attributes;
1102 $output = '';
1103 foreach ($attributes as $name => $value) {
1104 $output .= self::attribute($name, $value);
1106 return $output;
1110 * Generates a simple image tag with attributes.
1112 * @param string $src The source of image
1113 * @param string $alt The alternate text for image
1114 * @param array $attributes The tag attributes (array('height' => $max_height, 'class' => 'class1') etc.)
1115 * @return string HTML fragment
1117 public static function img($src, $alt, array $attributes = null) {
1118 $attributes = (array)$attributes;
1119 $attributes['src'] = $src;
1120 $attributes['alt'] = $alt;
1122 return self::empty_tag('img', $attributes);
1126 * Generates random html element id.
1128 * @staticvar int $counter
1129 * @staticvar type $uniq
1130 * @param string $base A string fragment that will be included in the random ID.
1131 * @return string A unique ID
1133 public static function random_id($base='random') {
1134 static $counter = 0;
1135 static $uniq;
1137 if (!isset($uniq)) {
1138 $uniq = uniqid();
1141 $counter++;
1142 return $base.$uniq.$counter;
1146 * Generates a simple html link
1148 * @param string|moodle_url $url The URL
1149 * @param string $text The text
1150 * @param array $attributes HTML attributes
1151 * @return string HTML fragment
1153 public static function link($url, $text, array $attributes = null) {
1154 $attributes = (array)$attributes;
1155 $attributes['href'] = $url;
1156 return self::tag('a', $text, $attributes);
1160 * Generates a simple checkbox with optional label
1162 * @param string $name The name of the checkbox
1163 * @param string $value The value of the checkbox
1164 * @param bool $checked Whether the checkbox is checked
1165 * @param string $label The label for the checkbox
1166 * @param array $attributes Any attributes to apply to the checkbox
1167 * @return string html fragment
1169 public static function checkbox($name, $value, $checked = true, $label = '', array $attributes = null) {
1170 $attributes = (array)$attributes;
1171 $output = '';
1173 if ($label !== '' and !is_null($label)) {
1174 if (empty($attributes['id'])) {
1175 $attributes['id'] = self::random_id('checkbox_');
1178 $attributes['type'] = 'checkbox';
1179 $attributes['value'] = $value;
1180 $attributes['name'] = $name;
1181 $attributes['checked'] = $checked ? 'checked' : null;
1183 $output .= self::empty_tag('input', $attributes);
1185 if ($label !== '' and !is_null($label)) {
1186 $output .= self::tag('label', $label, array('for'=>$attributes['id']));
1189 return $output;
1193 * Generates a simple select yes/no form field
1195 * @param string $name name of select element
1196 * @param bool $selected
1197 * @param array $attributes - html select element attributes
1198 * @return string HTML fragment
1200 public static function select_yes_no($name, $selected=true, array $attributes = null) {
1201 $options = array('1'=>get_string('yes'), '0'=>get_string('no'));
1202 return self::select($options, $name, $selected, null, $attributes);
1206 * Generates a simple select form field
1208 * @param array $options associative array value=>label ex.:
1209 * array(1=>'One, 2=>Two)
1210 * it is also possible to specify optgroup as complex label array ex.:
1211 * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
1212 * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
1213 * @param string $name name of select element
1214 * @param string|array $selected value or array of values depending on multiple attribute
1215 * @param array|bool $nothing add nothing selected option, or false of not added
1216 * @param array $attributes html select element attributes
1217 * @return string HTML fragment
1219 public static function select(array $options, $name, $selected = '', $nothing = array('' => 'choosedots'), array $attributes = null) {
1220 $attributes = (array)$attributes;
1221 if (is_array($nothing)) {
1222 foreach ($nothing as $k=>$v) {
1223 if ($v === 'choose' or $v === 'choosedots') {
1224 $nothing[$k] = get_string('choosedots');
1227 $options = $nothing + $options; // keep keys, do not override
1229 } else if (is_string($nothing) and $nothing !== '') {
1230 // BC
1231 $options = array(''=>$nothing) + $options;
1234 // we may accept more values if multiple attribute specified
1235 $selected = (array)$selected;
1236 foreach ($selected as $k=>$v) {
1237 $selected[$k] = (string)$v;
1240 if (!isset($attributes['id'])) {
1241 $id = 'menu'.$name;
1242 // name may contaion [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading
1243 $id = str_replace('[', '', $id);
1244 $id = str_replace(']', '', $id);
1245 $attributes['id'] = $id;
1248 if (!isset($attributes['class'])) {
1249 $class = 'menu'.$name;
1250 // name may contaion [], which would make an invalid class. e.g. numeric question type editing form, assignment quickgrading
1251 $class = str_replace('[', '', $class);
1252 $class = str_replace(']', '', $class);
1253 $attributes['class'] = $class;
1255 $attributes['class'] = 'select ' . $attributes['class']; // Add 'select' selector always
1257 $attributes['name'] = $name;
1259 if (!empty($attributes['disabled'])) {
1260 $attributes['disabled'] = 'disabled';
1261 } else {
1262 unset($attributes['disabled']);
1265 $output = '';
1266 foreach ($options as $value=>$label) {
1267 if (is_array($label)) {
1268 // ignore key, it just has to be unique
1269 $output .= self::select_optgroup(key($label), current($label), $selected);
1270 } else {
1271 $output .= self::select_option($label, $value, $selected);
1274 return self::tag('select', $output, $attributes);
1278 * Returns HTML to display a select box option.
1280 * @param string $label The label to display as the option.
1281 * @param string|int $value The value the option represents
1282 * @param array $selected An array of selected options
1283 * @return string HTML fragment
1285 private static function select_option($label, $value, array $selected) {
1286 $attributes = array();
1287 $value = (string)$value;
1288 if (in_array($value, $selected, true)) {
1289 $attributes['selected'] = 'selected';
1291 $attributes['value'] = $value;
1292 return self::tag('option', $label, $attributes);
1296 * Returns HTML to display a select box option group.
1298 * @param string $groupname The label to use for the group
1299 * @param array $options The options in the group
1300 * @param array $selected An array of selected values.
1301 * @return string HTML fragment.
1303 private static function select_optgroup($groupname, $options, array $selected) {
1304 if (empty($options)) {
1305 return '';
1307 $attributes = array('label'=>$groupname);
1308 $output = '';
1309 foreach ($options as $value=>$label) {
1310 $output .= self::select_option($label, $value, $selected);
1312 return self::tag('optgroup', $output, $attributes);
1316 * This is a shortcut for making an hour selector menu.
1318 * @param string $type The type of selector (years, months, days, hours, minutes)
1319 * @param string $name fieldname
1320 * @param int $currenttime A default timestamp in GMT
1321 * @param int $step minute spacing
1322 * @param array $attributes - html select element attributes
1323 * @return HTML fragment
1325 public static function select_time($type, $name, $currenttime = 0, $step = 5, array $attributes = null) {
1326 if (!$currenttime) {
1327 $currenttime = time();
1329 $currentdate = usergetdate($currenttime);
1330 $userdatetype = $type;
1331 $timeunits = array();
1333 switch ($type) {
1334 case 'years':
1335 for ($i=1970; $i<=2020; $i++) {
1336 $timeunits[$i] = $i;
1338 $userdatetype = 'year';
1339 break;
1340 case 'months':
1341 for ($i=1; $i<=12; $i++) {
1342 $timeunits[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
1344 $userdatetype = 'month';
1345 $currentdate['month'] = (int)$currentdate['mon'];
1346 break;
1347 case 'days':
1348 for ($i=1; $i<=31; $i++) {
1349 $timeunits[$i] = $i;
1351 $userdatetype = 'mday';
1352 break;
1353 case 'hours':
1354 for ($i=0; $i<=23; $i++) {
1355 $timeunits[$i] = sprintf("%02d",$i);
1357 break;
1358 case 'minutes':
1359 if ($step != 1) {
1360 $currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step;
1363 for ($i=0; $i<=59; $i+=$step) {
1364 $timeunits[$i] = sprintf("%02d",$i);
1366 break;
1367 default:
1368 throw new coding_exception("Time type $type is not supported by html_writer::select_time().");
1371 if (empty($attributes['id'])) {
1372 $attributes['id'] = self::random_id('ts_');
1374 $timerselector = self::select($timeunits, $name, $currentdate[$userdatetype], null, $attributes);
1375 $label = self::tag('label', get_string(substr($type, 0, -1), 'form'), array('for'=>$attributes['id'], 'class'=>'accesshide'));
1377 return $label.$timerselector;
1381 * Shortcut for quick making of lists
1383 * Note: 'list' is a reserved keyword ;-)
1385 * @param array $items
1386 * @param array $attributes
1387 * @param string $tag ul or ol
1388 * @return string
1390 public static function alist(array $items, array $attributes = null, $tag = 'ul') {
1391 $output = html_writer::start_tag($tag, $attributes)."\n";
1392 foreach ($items as $item) {
1393 $output .= html_writer::tag('li', $item)."\n";
1395 $output .= html_writer::end_tag($tag);
1396 return $output;
1400 * Returns hidden input fields created from url parameters.
1402 * @param moodle_url $url
1403 * @param array $exclude list of excluded parameters
1404 * @return string HTML fragment
1406 public static function input_hidden_params(moodle_url $url, array $exclude = null) {
1407 $exclude = (array)$exclude;
1408 $params = $url->params();
1409 foreach ($exclude as $key) {
1410 unset($params[$key]);
1413 $output = '';
1414 foreach ($params as $key => $value) {
1415 $attributes = array('type'=>'hidden', 'name'=>$key, 'value'=>$value);
1416 $output .= self::empty_tag('input', $attributes)."\n";
1418 return $output;
1422 * Generate a script tag containing the the specified code.
1424 * @param string $jscode the JavaScript code
1425 * @param moodle_url|string $url optional url of the external script, $code ignored if specified
1426 * @return string HTML, the code wrapped in <script> tags.
1428 public static function script($jscode, $url=null) {
1429 if ($jscode) {
1430 $attributes = array('type'=>'text/javascript');
1431 return self::tag('script', "\n//<![CDATA[\n$jscode\n//]]>\n", $attributes) . "\n";
1433 } else if ($url) {
1434 $attributes = array('type'=>'text/javascript', 'src'=>$url);
1435 return self::tag('script', '', $attributes) . "\n";
1437 } else {
1438 return '';
1443 * Renders HTML table
1445 * This method may modify the passed instance by adding some default properties if they are not set yet.
1446 * If this is not what you want, you should make a full clone of your data before passing them to this
1447 * method. In most cases this is not an issue at all so we do not clone by default for performance
1448 * and memory consumption reasons.
1450 * Please do not use .r0/.r1 for css, as they will be removed in Moodle 2.9.
1451 * @todo MDL-43902 , remove r0 and r1 from tr classes.
1453 * @param html_table $table data to be rendered
1454 * @return string HTML code
1456 public static function table(html_table $table) {
1457 // prepare table data and populate missing properties with reasonable defaults
1458 if (!empty($table->align)) {
1459 foreach ($table->align as $key => $aa) {
1460 if ($aa) {
1461 $table->align[$key] = 'text-align:'. fix_align_rtl($aa) .';'; // Fix for RTL languages
1462 } else {
1463 $table->align[$key] = null;
1467 if (!empty($table->size)) {
1468 foreach ($table->size as $key => $ss) {
1469 if ($ss) {
1470 $table->size[$key] = 'width:'. $ss .';';
1471 } else {
1472 $table->size[$key] = null;
1476 if (!empty($table->wrap)) {
1477 foreach ($table->wrap as $key => $ww) {
1478 if ($ww) {
1479 $table->wrap[$key] = 'white-space:nowrap;';
1480 } else {
1481 $table->wrap[$key] = '';
1485 if (!empty($table->head)) {
1486 foreach ($table->head as $key => $val) {
1487 if (!isset($table->align[$key])) {
1488 $table->align[$key] = null;
1490 if (!isset($table->size[$key])) {
1491 $table->size[$key] = null;
1493 if (!isset($table->wrap[$key])) {
1494 $table->wrap[$key] = null;
1499 if (empty($table->attributes['class'])) {
1500 $table->attributes['class'] = 'generaltable';
1502 if (!empty($table->tablealign)) {
1503 $table->attributes['class'] .= ' boxalign' . $table->tablealign;
1506 // explicitly assigned properties override those defined via $table->attributes
1507 $table->attributes['class'] = trim($table->attributes['class']);
1508 $attributes = array_merge($table->attributes, array(
1509 'id' => $table->id,
1510 'width' => $table->width,
1511 'summary' => $table->summary,
1512 'cellpadding' => $table->cellpadding,
1513 'cellspacing' => $table->cellspacing,
1515 $output = html_writer::start_tag('table', $attributes) . "\n";
1517 $countcols = 0;
1519 if (!empty($table->head)) {
1520 $countcols = count($table->head);
1522 $output .= html_writer::start_tag('thead', array()) . "\n";
1523 $output .= html_writer::start_tag('tr', array()) . "\n";
1524 $keys = array_keys($table->head);
1525 $lastkey = end($keys);
1527 foreach ($table->head as $key => $heading) {
1528 // Convert plain string headings into html_table_cell objects
1529 if (!($heading instanceof html_table_cell)) {
1530 $headingtext = $heading;
1531 $heading = new html_table_cell();
1532 $heading->text = $headingtext;
1533 $heading->header = true;
1536 if ($heading->header !== false) {
1537 $heading->header = true;
1540 if ($heading->header && empty($heading->scope)) {
1541 $heading->scope = 'col';
1544 $heading->attributes['class'] .= ' header c' . $key;
1545 if (isset($table->headspan[$key]) && $table->headspan[$key] > 1) {
1546 $heading->colspan = $table->headspan[$key];
1547 $countcols += $table->headspan[$key] - 1;
1550 if ($key == $lastkey) {
1551 $heading->attributes['class'] .= ' lastcol';
1553 if (isset($table->colclasses[$key])) {
1554 $heading->attributes['class'] .= ' ' . $table->colclasses[$key];
1556 $heading->attributes['class'] = trim($heading->attributes['class']);
1557 $attributes = array_merge($heading->attributes, array(
1558 'style' => $table->align[$key] . $table->size[$key] . $heading->style,
1559 'scope' => $heading->scope,
1560 'colspan' => $heading->colspan,
1563 $tagtype = 'td';
1564 if ($heading->header === true) {
1565 $tagtype = 'th';
1567 $output .= html_writer::tag($tagtype, $heading->text, $attributes) . "\n";
1569 $output .= html_writer::end_tag('tr') . "\n";
1570 $output .= html_writer::end_tag('thead') . "\n";
1572 if (empty($table->data)) {
1573 // For valid XHTML strict every table must contain either a valid tr
1574 // or a valid tbody... both of which must contain a valid td
1575 $output .= html_writer::start_tag('tbody', array('class' => 'empty'));
1576 $output .= html_writer::tag('tr', html_writer::tag('td', '', array('colspan'=>count($table->head))));
1577 $output .= html_writer::end_tag('tbody');
1581 if (!empty($table->data)) {
1582 $oddeven = 1;
1583 $keys = array_keys($table->data);
1584 $lastrowkey = end($keys);
1585 $output .= html_writer::start_tag('tbody', array());
1587 foreach ($table->data as $key => $row) {
1588 if (($row === 'hr') && ($countcols)) {
1589 $output .= html_writer::tag('td', html_writer::tag('div', '', array('class' => 'tabledivider')), array('colspan' => $countcols));
1590 } else {
1591 // Convert array rows to html_table_rows and cell strings to html_table_cell objects
1592 if (!($row instanceof html_table_row)) {
1593 $newrow = new html_table_row();
1595 foreach ($row as $cell) {
1596 if (!($cell instanceof html_table_cell)) {
1597 $cell = new html_table_cell($cell);
1599 $newrow->cells[] = $cell;
1601 $row = $newrow;
1604 $oddeven = $oddeven ? 0 : 1;
1605 if (isset($table->rowclasses[$key])) {
1606 $row->attributes['class'] .= ' ' . $table->rowclasses[$key];
1609 $row->attributes['class'] .= ' r' . $oddeven;
1610 if ($key == $lastrowkey) {
1611 $row->attributes['class'] .= ' lastrow';
1614 // Explicitly assigned properties should override those defined in the attributes.
1615 $row->attributes['class'] = trim($row->attributes['class']);
1616 $trattributes = array_merge($row->attributes, array(
1617 'id' => $row->id,
1618 'style' => $row->style,
1620 $output .= html_writer::start_tag('tr', $trattributes) . "\n";
1621 $keys2 = array_keys($row->cells);
1622 $lastkey = end($keys2);
1624 $gotlastkey = false; //flag for sanity checking
1625 foreach ($row->cells as $key => $cell) {
1626 if ($gotlastkey) {
1627 //This should never happen. Why do we have a cell after the last cell?
1628 mtrace("A cell with key ($key) was found after the last key ($lastkey)");
1631 if (!($cell instanceof html_table_cell)) {
1632 $mycell = new html_table_cell();
1633 $mycell->text = $cell;
1634 $cell = $mycell;
1637 if (($cell->header === true) && empty($cell->scope)) {
1638 $cell->scope = 'row';
1641 if (isset($table->colclasses[$key])) {
1642 $cell->attributes['class'] .= ' ' . $table->colclasses[$key];
1645 $cell->attributes['class'] .= ' cell c' . $key;
1646 if ($key == $lastkey) {
1647 $cell->attributes['class'] .= ' lastcol';
1648 $gotlastkey = true;
1650 $tdstyle = '';
1651 $tdstyle .= isset($table->align[$key]) ? $table->align[$key] : '';
1652 $tdstyle .= isset($table->size[$key]) ? $table->size[$key] : '';
1653 $tdstyle .= isset($table->wrap[$key]) ? $table->wrap[$key] : '';
1654 $cell->attributes['class'] = trim($cell->attributes['class']);
1655 $tdattributes = array_merge($cell->attributes, array(
1656 'style' => $tdstyle . $cell->style,
1657 'colspan' => $cell->colspan,
1658 'rowspan' => $cell->rowspan,
1659 'id' => $cell->id,
1660 'abbr' => $cell->abbr,
1661 'scope' => $cell->scope,
1663 $tagtype = 'td';
1664 if ($cell->header === true) {
1665 $tagtype = 'th';
1667 $output .= html_writer::tag($tagtype, $cell->text, $tdattributes) . "\n";
1670 $output .= html_writer::end_tag('tr') . "\n";
1672 $output .= html_writer::end_tag('tbody') . "\n";
1674 $output .= html_writer::end_tag('table') . "\n";
1676 return $output;
1680 * Renders form element label
1682 * By default, the label is suffixed with a label separator defined in the
1683 * current language pack (colon by default in the English lang pack).
1684 * Adding the colon can be explicitly disabled if needed. Label separators
1685 * are put outside the label tag itself so they are not read by
1686 * screenreaders (accessibility).
1688 * Parameter $for explicitly associates the label with a form control. When
1689 * set, the value of this attribute must be the same as the value of
1690 * the id attribute of the form control in the same document. When null,
1691 * the label being defined is associated with the control inside the label
1692 * element.
1694 * @param string $text content of the label tag
1695 * @param string|null $for id of the element this label is associated with, null for no association
1696 * @param bool $colonize add label separator (colon) to the label text, if it is not there yet
1697 * @param array $attributes to be inserted in the tab, for example array('accesskey' => 'a')
1698 * @return string HTML of the label element
1700 public static function label($text, $for, $colonize = true, array $attributes=array()) {
1701 if (!is_null($for)) {
1702 $attributes = array_merge($attributes, array('for' => $for));
1704 $text = trim($text);
1705 $label = self::tag('label', $text, $attributes);
1707 // TODO MDL-12192 $colonize disabled for now yet
1708 // if (!empty($text) and $colonize) {
1709 // // the $text may end with the colon already, though it is bad string definition style
1710 // $colon = get_string('labelsep', 'langconfig');
1711 // if (!empty($colon)) {
1712 // $trimmed = trim($colon);
1713 // if ((substr($text, -strlen($trimmed)) == $trimmed) or (substr($text, -1) == ':')) {
1714 // //debugging('The label text should not end with colon or other label separator,
1715 // // please fix the string definition.', DEBUG_DEVELOPER);
1716 // } else {
1717 // $label .= $colon;
1718 // }
1719 // }
1720 // }
1722 return $label;
1726 * Combines a class parameter with other attributes. Aids in code reduction
1727 * because the class parameter is very frequently used.
1729 * If the class attribute is specified both in the attributes and in the
1730 * class parameter, the two values are combined with a space between.
1732 * @param string $class Optional CSS class (or classes as space-separated list)
1733 * @param array $attributes Optional other attributes as array
1734 * @return array Attributes (or null if still none)
1736 private static function add_class($class = '', array $attributes = null) {
1737 if ($class !== '') {
1738 $classattribute = array('class' => $class);
1739 if ($attributes) {
1740 if (array_key_exists('class', $attributes)) {
1741 $attributes['class'] = trim($attributes['class'] . ' ' . $class);
1742 } else {
1743 $attributes = $classattribute + $attributes;
1745 } else {
1746 $attributes = $classattribute;
1749 return $attributes;
1753 * Creates a <div> tag. (Shortcut function.)
1755 * @param string $content HTML content of tag
1756 * @param string $class Optional CSS class (or classes as space-separated list)
1757 * @param array $attributes Optional other attributes as array
1758 * @return string HTML code for div
1760 public static function div($content, $class = '', array $attributes = null) {
1761 return self::tag('div', $content, self::add_class($class, $attributes));
1765 * Starts a <div> tag. (Shortcut function.)
1767 * @param string $class Optional CSS class (or classes as space-separated list)
1768 * @param array $attributes Optional other attributes as array
1769 * @return string HTML code for open div tag
1771 public static function start_div($class = '', array $attributes = null) {
1772 return self::start_tag('div', self::add_class($class, $attributes));
1776 * Ends a <div> tag. (Shortcut function.)
1778 * @return string HTML code for close div tag
1780 public static function end_div() {
1781 return self::end_tag('div');
1785 * Creates a <span> tag. (Shortcut function.)
1787 * @param string $content HTML content of tag
1788 * @param string $class Optional CSS class (or classes as space-separated list)
1789 * @param array $attributes Optional other attributes as array
1790 * @return string HTML code for span
1792 public static function span($content, $class = '', array $attributes = null) {
1793 return self::tag('span', $content, self::add_class($class, $attributes));
1797 * Starts a <span> tag. (Shortcut function.)
1799 * @param string $class Optional CSS class (or classes as space-separated list)
1800 * @param array $attributes Optional other attributes as array
1801 * @return string HTML code for open span tag
1803 public static function start_span($class = '', array $attributes = null) {
1804 return self::start_tag('span', self::add_class($class, $attributes));
1808 * Ends a <span> tag. (Shortcut function.)
1810 * @return string HTML code for close span tag
1812 public static function end_span() {
1813 return self::end_tag('span');
1818 * Simple javascript output class
1820 * @copyright 2010 Petr Skoda
1821 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1822 * @since Moodle 2.0
1823 * @package core
1824 * @category output
1826 class js_writer {
1829 * Returns javascript code calling the function
1831 * @param string $function function name, can be complex like Y.Event.purgeElement
1832 * @param array $arguments parameters
1833 * @param int $delay execution delay in seconds
1834 * @return string JS code fragment
1836 public static function function_call($function, array $arguments = null, $delay=0) {
1837 if ($arguments) {
1838 $arguments = array_map('json_encode', convert_to_array($arguments));
1839 $arguments = implode(', ', $arguments);
1840 } else {
1841 $arguments = '';
1843 $js = "$function($arguments);";
1845 if ($delay) {
1846 $delay = $delay * 1000; // in miliseconds
1847 $js = "setTimeout(function() { $js }, $delay);";
1849 return $js . "\n";
1853 * Special function which adds Y as first argument of function call.
1855 * @param string $function The function to call
1856 * @param array $extraarguments Any arguments to pass to it
1857 * @return string Some JS code
1859 public static function function_call_with_Y($function, array $extraarguments = null) {
1860 if ($extraarguments) {
1861 $extraarguments = array_map('json_encode', convert_to_array($extraarguments));
1862 $arguments = 'Y, ' . implode(', ', $extraarguments);
1863 } else {
1864 $arguments = 'Y';
1866 return "$function($arguments);\n";
1870 * Returns JavaScript code to initialise a new object
1872 * @param string $var If it is null then no var is assigned the new object.
1873 * @param string $class The class to initialise an object for.
1874 * @param array $arguments An array of args to pass to the init method.
1875 * @param array $requirements Any modules required for this class.
1876 * @param int $delay The delay before initialisation. 0 = no delay.
1877 * @return string Some JS code
1879 public static function object_init($var, $class, array $arguments = null, array $requirements = null, $delay=0) {
1880 if (is_array($arguments)) {
1881 $arguments = array_map('json_encode', convert_to_array($arguments));
1882 $arguments = implode(', ', $arguments);
1885 if ($var === null) {
1886 $js = "new $class(Y, $arguments);";
1887 } else if (strpos($var, '.')!==false) {
1888 $js = "$var = new $class(Y, $arguments);";
1889 } else {
1890 $js = "var $var = new $class(Y, $arguments);";
1893 if ($delay) {
1894 $delay = $delay * 1000; // in miliseconds
1895 $js = "setTimeout(function() { $js }, $delay);";
1898 if (count($requirements) > 0) {
1899 $requirements = implode("', '", $requirements);
1900 $js = "Y.use('$requirements', function(Y){ $js });";
1902 return $js."\n";
1906 * Returns code setting value to variable
1908 * @param string $name
1909 * @param mixed $value json serialised value
1910 * @param bool $usevar add var definition, ignored for nested properties
1911 * @return string JS code fragment
1913 public static function set_variable($name, $value, $usevar = true) {
1914 $output = '';
1916 if ($usevar) {
1917 if (strpos($name, '.')) {
1918 $output .= '';
1919 } else {
1920 $output .= 'var ';
1924 $output .= "$name = ".json_encode($value).";";
1926 return $output;
1930 * Writes event handler attaching code
1932 * @param array|string $selector standard YUI selector for elements, may be
1933 * array or string, element id is in the form "#idvalue"
1934 * @param string $event A valid DOM event (click, mousedown, change etc.)
1935 * @param string $function The name of the function to call
1936 * @param array $arguments An optional array of argument parameters to pass to the function
1937 * @return string JS code fragment
1939 public static function event_handler($selector, $event, $function, array $arguments = null) {
1940 $selector = json_encode($selector);
1941 $output = "Y.on('$event', $function, $selector, null";
1942 if (!empty($arguments)) {
1943 $output .= ', ' . json_encode($arguments);
1945 return $output . ");\n";
1950 * Holds all the information required to render a <table> by {@link core_renderer::table()}
1952 * Example of usage:
1953 * $t = new html_table();
1954 * ... // set various properties of the object $t as described below
1955 * echo html_writer::table($t);
1957 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
1958 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1959 * @since Moodle 2.0
1960 * @package core
1961 * @category output
1963 class html_table {
1966 * @var string Value to use for the id attribute of the table
1968 public $id = null;
1971 * @var array Attributes of HTML attributes for the <table> element
1973 public $attributes = array();
1976 * @var array An array of headings. The n-th array item is used as a heading of the n-th column.
1977 * For more control over the rendering of the headers, an array of html_table_cell objects
1978 * can be passed instead of an array of strings.
1980 * Example of usage:
1981 * $t->head = array('Student', 'Grade');
1983 public $head;
1986 * @var array An array that can be used to make a heading span multiple columns.
1987 * In this example, {@link html_table:$data} is supposed to have three columns. For the first two columns,
1988 * the same heading is used. Therefore, {@link html_table::$head} should consist of two items.
1990 * Example of usage:
1991 * $t->headspan = array(2,1);
1993 public $headspan;
1996 * @var array An array of column alignments.
1997 * The value is used as CSS 'text-align' property. Therefore, possible
1998 * values are 'left', 'right', 'center' and 'justify'. Specify 'right' or 'left' from the perspective
1999 * of a left-to-right (LTR) language. For RTL, the values are flipped automatically.
2001 * Examples of usage:
2002 * $t->align = array(null, 'right');
2003 * or
2004 * $t->align[1] = 'right';
2006 public $align;
2009 * @var array The value is used as CSS 'size' property.
2011 * Examples of usage:
2012 * $t->size = array('50%', '50%');
2013 * or
2014 * $t->size[1] = '120px';
2016 public $size;
2019 * @var array An array of wrapping information.
2020 * The only possible value is 'nowrap' that sets the
2021 * CSS property 'white-space' to the value 'nowrap' in the given column.
2023 * Example of usage:
2024 * $t->wrap = array(null, 'nowrap');
2026 public $wrap;
2029 * @var array Array of arrays or html_table_row objects containing the data. Alternatively, if you have
2030 * $head specified, the string 'hr' (for horizontal ruler) can be used
2031 * instead of an array of cells data resulting in a divider rendered.
2033 * Example of usage with array of arrays:
2034 * $row1 = array('Harry Potter', '76 %');
2035 * $row2 = array('Hermione Granger', '100 %');
2036 * $t->data = array($row1, $row2);
2038 * Example with array of html_table_row objects: (used for more fine-grained control)
2039 * $cell1 = new html_table_cell();
2040 * $cell1->text = 'Harry Potter';
2041 * $cell1->colspan = 2;
2042 * $row1 = new html_table_row();
2043 * $row1->cells[] = $cell1;
2044 * $cell2 = new html_table_cell();
2045 * $cell2->text = 'Hermione Granger';
2046 * $cell3 = new html_table_cell();
2047 * $cell3->text = '100 %';
2048 * $row2 = new html_table_row();
2049 * $row2->cells = array($cell2, $cell3);
2050 * $t->data = array($row1, $row2);
2052 public $data;
2055 * @deprecated since Moodle 2.0. Styling should be in the CSS.
2056 * @var string Width of the table, percentage of the page preferred.
2058 public $width = null;
2061 * @deprecated since Moodle 2.0. Styling should be in the CSS.
2062 * @var string Alignment for the whole table. Can be 'right', 'left' or 'center' (default).
2064 public $tablealign = null;
2067 * @deprecated since Moodle 2.0. Styling should be in the CSS.
2068 * @var int Padding on each cell, in pixels
2070 public $cellpadding = null;
2073 * @var int Spacing between cells, in pixels
2074 * @deprecated since Moodle 2.0. Styling should be in the CSS.
2076 public $cellspacing = null;
2079 * @var array Array of classes to add to particular rows, space-separated string.
2080 * Classes 'r0' or 'r1' are added automatically for every odd or even row,
2081 * respectively. Class 'lastrow' is added automatically for the last row
2082 * in the table.
2084 * Example of usage:
2085 * $t->rowclasses[9] = 'tenth'
2087 public $rowclasses;
2090 * @var array An array of classes to add to every cell in a particular column,
2091 * space-separated string. Class 'cell' is added automatically by the renderer.
2092 * Classes 'c0' or 'c1' are added automatically for every odd or even column,
2093 * respectively. Class 'lastcol' is added automatically for all last cells
2094 * in a row.
2096 * Example of usage:
2097 * $t->colclasses = array(null, 'grade');
2099 public $colclasses;
2102 * @var string Description of the contents for screen readers.
2104 public $summary;
2107 * Constructor
2109 public function __construct() {
2110 $this->attributes['class'] = '';
2115 * Component representing a table row.
2117 * @copyright 2009 Nicolas Connault
2118 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2119 * @since Moodle 2.0
2120 * @package core
2121 * @category output
2123 class html_table_row {
2126 * @var string Value to use for the id attribute of the row.
2128 public $id = null;
2131 * @var array Array of html_table_cell objects
2133 public $cells = array();
2136 * @var string Value to use for the style attribute of the table row
2138 public $style = null;
2141 * @var array Attributes of additional HTML attributes for the <tr> element
2143 public $attributes = array();
2146 * Constructor
2147 * @param array $cells
2149 public function __construct(array $cells=null) {
2150 $this->attributes['class'] = '';
2151 $cells = (array)$cells;
2152 foreach ($cells as $cell) {
2153 if ($cell instanceof html_table_cell) {
2154 $this->cells[] = $cell;
2155 } else {
2156 $this->cells[] = new html_table_cell($cell);
2163 * Component representing a table cell.
2165 * @copyright 2009 Nicolas Connault
2166 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2167 * @since Moodle 2.0
2168 * @package core
2169 * @category output
2171 class html_table_cell {
2174 * @var string Value to use for the id attribute of the cell.
2176 public $id = null;
2179 * @var string The contents of the cell.
2181 public $text;
2184 * @var string Abbreviated version of the contents of the cell.
2186 public $abbr = null;
2189 * @var int Number of columns this cell should span.
2191 public $colspan = null;
2194 * @var int Number of rows this cell should span.
2196 public $rowspan = null;
2199 * @var string Defines a way to associate header cells and data cells in a table.
2201 public $scope = null;
2204 * @var bool Whether or not this cell is a header cell.
2206 public $header = null;
2209 * @var string Value to use for the style attribute of the table cell
2211 public $style = null;
2214 * @var array Attributes of additional HTML attributes for the <td> element
2216 public $attributes = array();
2219 * Constructs a table cell
2221 * @param string $text
2223 public function __construct($text = null) {
2224 $this->text = $text;
2225 $this->attributes['class'] = '';
2230 * Component representing a paging bar.
2232 * @copyright 2009 Nicolas Connault
2233 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2234 * @since Moodle 2.0
2235 * @package core
2236 * @category output
2238 class paging_bar implements renderable {
2241 * @var int The maximum number of pagelinks to display.
2243 public $maxdisplay = 18;
2246 * @var int The total number of entries to be pages through..
2248 public $totalcount;
2251 * @var int The page you are currently viewing.
2253 public $page;
2256 * @var int The number of entries that should be shown per page.
2258 public $perpage;
2261 * @var string|moodle_url If this is a string then it is the url which will be appended with $pagevar,
2262 * an equals sign and the page number.
2263 * If this is a moodle_url object then the pagevar param will be replaced by
2264 * the page no, for each page.
2266 public $baseurl;
2269 * @var string This is the variable name that you use for the pagenumber in your
2270 * code (ie. 'tablepage', 'blogpage', etc)
2272 public $pagevar;
2275 * @var string A HTML link representing the "previous" page.
2277 public $previouslink = null;
2280 * @var string A HTML link representing the "next" page.
2282 public $nextlink = null;
2285 * @var string A HTML link representing the first page.
2287 public $firstlink = null;
2290 * @var string A HTML link representing the last page.
2292 public $lastlink = null;
2295 * @var array An array of strings. One of them is just a string: the current page
2297 public $pagelinks = array();
2300 * Constructor paging_bar with only the required params.
2302 * @param int $totalcount The total number of entries available to be paged through
2303 * @param int $page The page you are currently viewing
2304 * @param int $perpage The number of entries that should be shown per page
2305 * @param string|moodle_url $baseurl url of the current page, the $pagevar parameter is added
2306 * @param string $pagevar name of page parameter that holds the page number
2308 public function __construct($totalcount, $page, $perpage, $baseurl, $pagevar = 'page') {
2309 $this->totalcount = $totalcount;
2310 $this->page = $page;
2311 $this->perpage = $perpage;
2312 $this->baseurl = $baseurl;
2313 $this->pagevar = $pagevar;
2317 * Prepares the paging bar for output.
2319 * This method validates the arguments set up for the paging bar and then
2320 * produces fragments of HTML to assist display later on.
2322 * @param renderer_base $output
2323 * @param moodle_page $page
2324 * @param string $target
2325 * @throws coding_exception
2327 public function prepare(renderer_base $output, moodle_page $page, $target) {
2328 if (!isset($this->totalcount) || is_null($this->totalcount)) {
2329 throw new coding_exception('paging_bar requires a totalcount value.');
2331 if (!isset($this->page) || is_null($this->page)) {
2332 throw new coding_exception('paging_bar requires a page value.');
2334 if (empty($this->perpage)) {
2335 throw new coding_exception('paging_bar requires a perpage value.');
2337 if (empty($this->baseurl)) {
2338 throw new coding_exception('paging_bar requires a baseurl value.');
2341 if ($this->totalcount > $this->perpage) {
2342 $pagenum = $this->page - 1;
2344 if ($this->page > 0) {
2345 $this->previouslink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('previous'), array('class'=>'previous'));
2348 if ($this->perpage > 0) {
2349 $lastpage = ceil($this->totalcount / $this->perpage);
2350 } else {
2351 $lastpage = 1;
2354 if ($this->page > round(($this->maxdisplay/3)*2)) {
2355 $currpage = $this->page - round($this->maxdisplay/3);
2357 $this->firstlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>0)), '1', array('class'=>'first'));
2358 } else {
2359 $currpage = 0;
2362 $displaycount = $displaypage = 0;
2364 while ($displaycount < $this->maxdisplay and $currpage < $lastpage) {
2365 $displaypage = $currpage + 1;
2367 if ($this->page == $currpage) {
2368 $this->pagelinks[] = html_writer::span($displaypage, 'current-page');
2369 } else {
2370 $pagelink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$currpage)), $displaypage);
2371 $this->pagelinks[] = $pagelink;
2374 $displaycount++;
2375 $currpage++;
2378 if ($currpage < $lastpage) {
2379 $lastpageactual = $lastpage - 1;
2380 $this->lastlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$lastpageactual)), $lastpage, array('class'=>'last'));
2383 $pagenum = $this->page + 1;
2385 if ($pagenum != $displaypage) {
2386 $this->nextlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('next'), array('class'=>'next'));
2393 * This class represents how a block appears on a page.
2395 * During output, each block instance is asked to return a block_contents object,
2396 * those are then passed to the $OUTPUT->block function for display.
2398 * contents should probably be generated using a moodle_block_..._renderer.
2400 * Other block-like things that need to appear on the page, for example the
2401 * add new block UI, are also represented as block_contents objects.
2403 * @copyright 2009 Tim Hunt
2404 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2405 * @since Moodle 2.0
2406 * @package core
2407 * @category output
2409 class block_contents {
2411 /** Used when the block cannot be collapsed **/
2412 const NOT_HIDEABLE = 0;
2414 /** Used when the block can be collapsed but currently is not **/
2415 const VISIBLE = 1;
2417 /** Used when the block has been collapsed **/
2418 const HIDDEN = 2;
2421 * @var int Used to set $skipid.
2423 protected static $idcounter = 1;
2426 * @var int All the blocks (or things that look like blocks) printed on
2427 * a page are given a unique number that can be used to construct id="" attributes.
2428 * This is set automatically be the {@link prepare()} method.
2429 * Do not try to set it manually.
2431 public $skipid;
2434 * @var int If this is the contents of a real block, this should be set
2435 * to the block_instance.id. Otherwise this should be set to 0.
2437 public $blockinstanceid = 0;
2440 * @var int If this is a real block instance, and there is a corresponding
2441 * block_position.id for the block on this page, this should be set to that id.
2442 * Otherwise it should be 0.
2444 public $blockpositionid = 0;
2447 * @var array An array of attribute => value pairs that are put on the outer div of this
2448 * block. {@link $id} and {@link $classes} attributes should be set separately.
2450 public $attributes;
2453 * @var string The title of this block. If this came from user input, it should already
2454 * have had format_string() processing done on it. This will be output inside
2455 * <h2> tags. Please do not cause invalid XHTML.
2457 public $title = '';
2460 * @var string The label to use when the block does not, or will not have a visible title.
2461 * You should never set this as well as title... it will just be ignored.
2463 public $arialabel = '';
2466 * @var string HTML for the content
2468 public $content = '';
2471 * @var array An alternative to $content, it you want a list of things with optional icons.
2473 public $footer = '';
2476 * @var string Any small print that should appear under the block to explain
2477 * to the teacher about the block, for example 'This is a sticky block that was
2478 * added in the system context.'
2480 public $annotation = '';
2483 * @var int One of the constants NOT_HIDEABLE, VISIBLE, HIDDEN. Whether
2484 * the user can toggle whether this block is visible.
2486 public $collapsible = self::NOT_HIDEABLE;
2489 * Set this to true if the block is dockable.
2490 * @var bool
2492 public $dockable = false;
2495 * @var array A (possibly empty) array of editing controls. Each element of
2496 * this array should be an array('url' => $url, 'icon' => $icon, 'caption' => $caption).
2497 * $icon is the icon name. Fed to $OUTPUT->pix_url.
2499 public $controls = array();
2503 * Create new instance of block content
2504 * @param array $attributes
2506 public function __construct(array $attributes = null) {
2507 $this->skipid = self::$idcounter;
2508 self::$idcounter += 1;
2510 if ($attributes) {
2511 // standard block
2512 $this->attributes = $attributes;
2513 } else {
2514 // simple "fake" blocks used in some modules and "Add new block" block
2515 $this->attributes = array('class'=>'block');
2520 * Add html class to block
2522 * @param string $class
2524 public function add_class($class) {
2525 $this->attributes['class'] .= ' '.$class;
2531 * This class represents a target for where a block can go when it is being moved.
2533 * This needs to be rendered as a form with the given hidden from fields, and
2534 * clicking anywhere in the form should submit it. The form action should be
2535 * $PAGE->url.
2537 * @copyright 2009 Tim Hunt
2538 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2539 * @since Moodle 2.0
2540 * @package core
2541 * @category output
2543 class block_move_target {
2546 * @var moodle_url Move url
2548 public $url;
2551 * Constructor
2552 * @param moodle_url $url
2554 public function __construct(moodle_url $url) {
2555 $this->url = $url;
2560 * Custom menu item
2562 * This class is used to represent one item within a custom menu that may or may
2563 * not have children.
2565 * @copyright 2010 Sam Hemelryk
2566 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2567 * @since Moodle 2.0
2568 * @package core
2569 * @category output
2571 class custom_menu_item implements renderable {
2574 * @var string The text to show for the item
2576 protected $text;
2579 * @var moodle_url The link to give the icon if it has no children
2581 protected $url;
2584 * @var string A title to apply to the item. By default the text
2586 protected $title;
2589 * @var int A sort order for the item, not necessary if you order things in
2590 * the CFG var.
2592 protected $sort;
2595 * @var custom_menu_item A reference to the parent for this item or NULL if
2596 * it is a top level item
2598 protected $parent;
2601 * @var array A array in which to store children this item has.
2603 protected $children = array();
2606 * @var int A reference to the sort var of the last child that was added
2608 protected $lastsort = 0;
2611 * Constructs the new custom menu item
2613 * @param string $text
2614 * @param moodle_url $url A moodle url to apply as the link for this item [Optional]
2615 * @param string $title A title to apply to this item [Optional]
2616 * @param int $sort A sort or to use if we need to sort differently [Optional]
2617 * @param custom_menu_item $parent A reference to the parent custom_menu_item this child
2618 * belongs to, only if the child has a parent. [Optional]
2620 public function __construct($text, moodle_url $url=null, $title=null, $sort = null, custom_menu_item $parent = null) {
2621 $this->text = $text;
2622 $this->url = $url;
2623 $this->title = $title;
2624 $this->sort = (int)$sort;
2625 $this->parent = $parent;
2629 * Adds a custom menu item as a child of this node given its properties.
2631 * @param string $text
2632 * @param moodle_url $url
2633 * @param string $title
2634 * @param int $sort
2635 * @return custom_menu_item
2637 public function add($text, moodle_url $url = null, $title = null, $sort = null) {
2638 $key = count($this->children);
2639 if (empty($sort)) {
2640 $sort = $this->lastsort + 1;
2642 $this->children[$key] = new custom_menu_item($text, $url, $title, $sort, $this);
2643 $this->lastsort = (int)$sort;
2644 return $this->children[$key];
2648 * Removes a custom menu item that is a child or descendant to the current menu.
2650 * Returns true if child was found and removed.
2652 * @param custom_menu_item $menuitem
2653 * @return bool
2655 public function remove_child(custom_menu_item $menuitem) {
2656 $removed = false;
2657 if (($key = array_search($menuitem, $this->children)) !== false) {
2658 unset($this->children[$key]);
2659 $this->children = array_values($this->children);
2660 $removed = true;
2661 } else {
2662 foreach ($this->children as $child) {
2663 if ($removed = $child->remove_child($menuitem)) {
2664 break;
2668 return $removed;
2672 * Returns the text for this item
2673 * @return string
2675 public function get_text() {
2676 return $this->text;
2680 * Returns the url for this item
2681 * @return moodle_url
2683 public function get_url() {
2684 return $this->url;
2688 * Returns the title for this item
2689 * @return string
2691 public function get_title() {
2692 return $this->title;
2696 * Sorts and returns the children for this item
2697 * @return array
2699 public function get_children() {
2700 $this->sort();
2701 return $this->children;
2705 * Gets the sort order for this child
2706 * @return int
2708 public function get_sort_order() {
2709 return $this->sort;
2713 * Gets the parent this child belong to
2714 * @return custom_menu_item
2716 public function get_parent() {
2717 return $this->parent;
2721 * Sorts the children this item has
2723 public function sort() {
2724 usort($this->children, array('custom_menu','sort_custom_menu_items'));
2728 * Returns true if this item has any children
2729 * @return bool
2731 public function has_children() {
2732 return (count($this->children) > 0);
2736 * Sets the text for the node
2737 * @param string $text
2739 public function set_text($text) {
2740 $this->text = (string)$text;
2744 * Sets the title for the node
2745 * @param string $title
2747 public function set_title($title) {
2748 $this->title = (string)$title;
2752 * Sets the url for the node
2753 * @param moodle_url $url
2755 public function set_url(moodle_url $url) {
2756 $this->url = $url;
2761 * Custom menu class
2763 * This class is used to operate a custom menu that can be rendered for the page.
2764 * The custom menu is built using $CFG->custommenuitems and is a structured collection
2765 * of custom_menu_item nodes that can be rendered by the core renderer.
2767 * To configure the custom menu:
2768 * Settings: Administration > Appearance > Themes > Theme settings
2770 * @copyright 2010 Sam Hemelryk
2771 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2772 * @since Moodle 2.0
2773 * @package core
2774 * @category output
2776 class custom_menu extends custom_menu_item {
2779 * @var string The language we should render for, null disables multilang support.
2781 protected $currentlanguage = null;
2784 * Creates the custom menu
2786 * @param string $definition the menu items definition in syntax required by {@link convert_text_to_menu_nodes()}
2787 * @param string $currentlanguage the current language code, null disables multilang support
2789 public function __construct($definition = '', $currentlanguage = null) {
2790 $this->currentlanguage = $currentlanguage;
2791 parent::__construct('root'); // create virtual root element of the menu
2792 if (!empty($definition)) {
2793 $this->override_children(self::convert_text_to_menu_nodes($definition, $currentlanguage));
2798 * Overrides the children of this custom menu. Useful when getting children
2799 * from $CFG->custommenuitems
2801 * @param array $children
2803 public function override_children(array $children) {
2804 $this->children = array();
2805 foreach ($children as $child) {
2806 if ($child instanceof custom_menu_item) {
2807 $this->children[] = $child;
2813 * Converts a string into a structured array of custom_menu_items which can
2814 * then be added to a custom menu.
2816 * Structure:
2817 * text|url|title|langs
2818 * The number of hyphens at the start determines the depth of the item. The
2819 * languages are optional, comma separated list of languages the line is for.
2821 * Example structure:
2822 * First level first item|http://www.moodle.com/
2823 * -Second level first item|http://www.moodle.com/partners/
2824 * -Second level second item|http://www.moodle.com/hq/
2825 * --Third level first item|http://www.moodle.com/jobs/
2826 * -Second level third item|http://www.moodle.com/development/
2827 * First level second item|http://www.moodle.com/feedback/
2828 * First level third item
2829 * English only|http://moodle.com|English only item|en
2830 * German only|http://moodle.de|Deutsch|de,de_du,de_kids
2833 * @static
2834 * @param string $text the menu items definition
2835 * @param string $language the language code, null disables multilang support
2836 * @return array
2838 public static function convert_text_to_menu_nodes($text, $language = null) {
2839 $root = new custom_menu();
2840 $lastitem = $root;
2841 $lastdepth = 0;
2842 $hiddenitems = array();
2843 $lines = explode("\n", $text);
2844 foreach ($lines as $linenumber => $line) {
2845 $line = trim($line);
2846 if (strlen($line) == 0) {
2847 continue;
2849 // Parse item settings.
2850 $itemtext = null;
2851 $itemurl = null;
2852 $itemtitle = null;
2853 $itemvisible = true;
2854 $settings = explode('|', $line);
2855 foreach ($settings as $i => $setting) {
2856 $setting = trim($setting);
2857 if (!empty($setting)) {
2858 switch ($i) {
2859 case 0:
2860 $itemtext = ltrim($setting, '-');
2861 $itemtitle = $itemtext;
2862 break;
2863 case 1:
2864 try {
2865 $itemurl = new moodle_url($setting);
2866 } catch (moodle_exception $exception) {
2867 // We're not actually worried about this, we don't want to mess up the display
2868 // just for a wrongly entered URL.
2869 $itemurl = null;
2871 break;
2872 case 2:
2873 $itemtitle = $setting;
2874 break;
2875 case 3:
2876 if (!empty($language)) {
2877 $itemlanguages = array_map('trim', explode(',', $setting));
2878 $itemvisible &= in_array($language, $itemlanguages);
2880 break;
2884 // Get depth of new item.
2885 preg_match('/^(\-*)/', $line, $match);
2886 $itemdepth = strlen($match[1]) + 1;
2887 // Find parent item for new item.
2888 while (($lastdepth - $itemdepth) >= 0) {
2889 $lastitem = $lastitem->get_parent();
2890 $lastdepth--;
2892 $lastitem = $lastitem->add($itemtext, $itemurl, $itemtitle, $linenumber + 1);
2893 $lastdepth++;
2894 if (!$itemvisible) {
2895 $hiddenitems[] = $lastitem;
2898 foreach ($hiddenitems as $item) {
2899 $item->parent->remove_child($item);
2901 return $root->get_children();
2905 * Sorts two custom menu items
2907 * This function is designed to be used with the usort method
2908 * usort($this->children, array('custom_menu','sort_custom_menu_items'));
2910 * @static
2911 * @param custom_menu_item $itema
2912 * @param custom_menu_item $itemb
2913 * @return int
2915 public static function sort_custom_menu_items(custom_menu_item $itema, custom_menu_item $itemb) {
2916 $itema = $itema->get_sort_order();
2917 $itemb = $itemb->get_sort_order();
2918 if ($itema == $itemb) {
2919 return 0;
2921 return ($itema > $itemb) ? +1 : -1;
2926 * Stores one tab
2928 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2929 * @package core
2931 class tabobject implements renderable {
2932 /** @var string unique id of the tab in this tree, it is used to find selected and/or inactive tabs */
2933 var $id;
2934 /** @var moodle_url|string link */
2935 var $link;
2936 /** @var string text on the tab */
2937 var $text;
2938 /** @var string title under the link, by defaul equals to text */
2939 var $title;
2940 /** @var bool whether to display a link under the tab name when it's selected */
2941 var $linkedwhenselected = false;
2942 /** @var bool whether the tab is inactive */
2943 var $inactive = false;
2944 /** @var bool indicates that this tab's child is selected */
2945 var $activated = false;
2946 /** @var bool indicates that this tab is selected */
2947 var $selected = false;
2948 /** @var array stores children tabobjects */
2949 var $subtree = array();
2950 /** @var int level of tab in the tree, 0 for root (instance of tabtree), 1 for the first row of tabs */
2951 var $level = 1;
2954 * Constructor
2956 * @param string $id unique id of the tab in this tree, it is used to find selected and/or inactive tabs
2957 * @param string|moodle_url $link
2958 * @param string $text text on the tab
2959 * @param string $title title under the link, by defaul equals to text
2960 * @param bool $linkedwhenselected whether to display a link under the tab name when it's selected
2962 public function __construct($id, $link = null, $text = '', $title = '', $linkedwhenselected = false) {
2963 $this->id = $id;
2964 $this->link = $link;
2965 $this->text = $text;
2966 $this->title = $title ? $title : $text;
2967 $this->linkedwhenselected = $linkedwhenselected;
2971 * Travels through tree and finds the tab to mark as selected, all parents are automatically marked as activated
2973 * @param string $selected the id of the selected tab (whatever row it's on),
2974 * if null marks all tabs as unselected
2975 * @return bool whether this tab is selected or contains selected tab in its subtree
2977 protected function set_selected($selected) {
2978 if ((string)$selected === (string)$this->id) {
2979 $this->selected = true;
2980 // This tab is selected. No need to travel through subtree.
2981 return true;
2983 foreach ($this->subtree as $subitem) {
2984 if ($subitem->set_selected($selected)) {
2985 // This tab has child that is selected. Mark it as activated. No need to check other children.
2986 $this->activated = true;
2987 return true;
2990 return false;
2994 * Travels through tree and finds a tab with specified id
2996 * @param string $id
2997 * @return tabtree|null
2999 public function find($id) {
3000 if ((string)$this->id === (string)$id) {
3001 return $this;
3003 foreach ($this->subtree as $tab) {
3004 if ($obj = $tab->find($id)) {
3005 return $obj;
3008 return null;
3012 * Allows to mark each tab's level in the tree before rendering.
3014 * @param int $level
3016 protected function set_level($level) {
3017 $this->level = $level;
3018 foreach ($this->subtree as $tab) {
3019 $tab->set_level($level + 1);
3025 * Stores tabs list
3027 * Example how to print a single line tabs:
3028 * $rows = array(
3029 * new tabobject(...),
3030 * new tabobject(...)
3031 * );
3032 * echo $OUTPUT->tabtree($rows, $selectedid);
3034 * Multiple row tabs may not look good on some devices but if you want to use them
3035 * you can specify ->subtree for the active tabobject.
3037 * @copyright 2013 Marina Glancy
3038 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3039 * @since Moodle 2.5
3040 * @package core
3041 * @category output
3043 class tabtree extends tabobject {
3045 * Constuctor
3047 * It is highly recommended to call constructor when list of tabs is already
3048 * populated, this way you ensure that selected and inactive tabs are located
3049 * and attribute level is set correctly.
3051 * @param array $tabs array of tabs, each of them may have it's own ->subtree
3052 * @param string|null $selected which tab to mark as selected, all parent tabs will
3053 * automatically be marked as activated
3054 * @param array|string|null $inactive list of ids of inactive tabs, regardless of
3055 * their level. Note that you can as weel specify tabobject::$inactive for separate instances
3057 public function __construct($tabs, $selected = null, $inactive = null) {
3058 $this->subtree = $tabs;
3059 if ($selected !== null) {
3060 $this->set_selected($selected);
3062 if ($inactive !== null) {
3063 if (is_array($inactive)) {
3064 foreach ($inactive as $id) {
3065 if ($tab = $this->find($id)) {
3066 $tab->inactive = true;
3069 } else if ($tab = $this->find($inactive)) {
3070 $tab->inactive = true;
3073 $this->set_level(0);
3078 * An action menu.
3080 * This action menu component takes a series of primary and secondary actions.
3081 * The primary actions are displayed permanently and the secondary attributes are displayed within a drop
3082 * down menu.
3084 * @package core
3085 * @category output
3086 * @copyright 2013 Sam Hemelryk
3087 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3089 class action_menu implements renderable {
3092 * Top right alignment.
3094 const TL = 1;
3097 * Top right alignment.
3099 const TR = 2;
3102 * Top right alignment.
3104 const BL = 3;
3107 * Top right alignment.
3109 const BR = 4;
3112 * The instance number. This is unique to this instance of the action menu.
3113 * @var int
3115 protected $instance = 0;
3118 * An array of primary actions. Please use {@link action_menu::add_primary_action()} to add actions.
3119 * @var array
3121 protected $primaryactions = array();
3124 * An array of secondary actions. Please use {@link action_menu::add_secondary_action()} to add actions.
3125 * @var array
3127 protected $secondaryactions = array();
3130 * An array of attributes added to the container of the action menu.
3131 * Initialised with defaults during construction.
3132 * @var array
3134 public $attributes = array();
3136 * An array of attributes added to the container of the primary actions.
3137 * Initialised with defaults during construction.
3138 * @var array
3140 public $attributesprimary = array();
3142 * An array of attributes added to the container of the secondary actions.
3143 * Initialised with defaults during construction.
3144 * @var array
3146 public $attributessecondary = array();
3149 * The string to use next to the icon for the action icon relating to the secondary (dropdown) menu.
3150 * @var array
3152 public $actiontext = null;
3155 * An icon to use for the toggling the secondary menu (dropdown).
3156 * @var actionicon
3158 public $actionicon;
3161 * Any text to use for the toggling the secondary menu (dropdown).
3162 * @var menutrigger
3164 public $menutrigger = '';
3167 * Place the action menu before all other actions.
3168 * @var prioritise
3170 public $prioritise = false;
3173 * Constructs the action menu with the given items.
3175 * @param array $actions An array of actions.
3177 public function __construct(array $actions = array()) {
3178 static $initialised = 0;
3179 $this->instance = $initialised;
3180 $initialised++;
3182 $this->attributes = array(
3183 'id' => 'action-menu-'.$this->instance,
3184 'class' => 'moodle-actionmenu',
3185 'data-enhance' => 'moodle-core-actionmenu'
3187 $this->attributesprimary = array(
3188 'id' => 'action-menu-'.$this->instance.'-menubar',
3189 'class' => 'menubar',
3190 'role' => 'menubar'
3192 $this->attributessecondary = array(
3193 'id' => 'action-menu-'.$this->instance.'-menu',
3194 'class' => 'menu',
3195 'data-rel' => 'menu-content',
3196 'aria-labelledby' => 'action-menu-toggle-'.$this->instance,
3197 'role' => 'menu'
3199 $this->set_alignment(self::TR, self::BR);
3200 foreach ($actions as $action) {
3201 $this->add($action);
3205 public function set_menu_trigger($trigger) {
3206 $this->menutrigger = $trigger;
3210 * Initialises JS required fore the action menu.
3211 * The JS is only required once as it manages all action menu's on the page.
3213 * @param moodle_page $page
3215 public function initialise_js(moodle_page $page) {
3216 static $initialised = false;
3217 if (!$initialised) {
3218 $page->requires->yui_module('moodle-core-actionmenu', 'M.core.actionmenu.init');
3219 $initialised = true;
3224 * Adds an action to this action menu.
3226 * @param action_menu_link|pix_icon|string $action
3228 public function add($action) {
3229 if ($action instanceof action_link) {
3230 if ($action->primary) {
3231 $this->add_primary_action($action);
3232 } else {
3233 $this->add_secondary_action($action);
3235 } else if ($action instanceof pix_icon) {
3236 $this->add_primary_action($action);
3237 } else {
3238 $this->add_secondary_action($action);
3243 * Adds a primary action to the action menu.
3245 * @param action_menu_link|action_link|pix_icon|string $action
3247 public function add_primary_action($action) {
3248 if ($action instanceof action_link || $action instanceof pix_icon) {
3249 $action->attributes['role'] = 'menuitem';
3250 if ($action instanceof action_menu_link) {
3251 $action->actionmenu = $this;
3254 $this->primaryactions[] = $action;
3258 * Adds a secondary action to the action menu.
3260 * @param action_link|pix_icon|string $action
3262 public function add_secondary_action($action) {
3263 if ($action instanceof action_link || $action instanceof pix_icon) {
3264 $action->attributes['role'] = 'menuitem';
3265 if ($action instanceof action_menu_link) {
3266 $action->actionmenu = $this;
3269 $this->secondaryactions[] = $action;
3273 * Returns the primary actions ready to be rendered.
3275 * @param core_renderer $output The renderer to use for getting icons.
3276 * @return array
3278 public function get_primary_actions(core_renderer $output = null) {
3279 global $OUTPUT;
3280 if ($output === null) {
3281 $output = $OUTPUT;
3283 $pixicon = $this->actionicon;
3284 $linkclasses = array('toggle-display');
3286 $title = '';
3287 if (!empty($this->menutrigger)) {
3288 $pixicon = '<b class="caret"></b>';
3289 $linkclasses[] = 'textmenu';
3290 } else {
3291 $title = new lang_string('actions', 'moodle');
3292 $this->actionicon = new pix_icon(
3293 't/edit_menu',
3295 'moodle',
3296 array('class' => 'iconsmall actionmenu', 'title' => '')
3298 $pixicon = $this->actionicon;
3300 if ($pixicon instanceof renderable) {
3301 $pixicon = $output->render($pixicon);
3302 if ($pixicon instanceof pix_icon && isset($pixicon->attributes['alt'])) {
3303 $title = $pixicon->attributes['alt'];
3306 $string = '';
3307 if ($this->actiontext) {
3308 $string = $this->actiontext;
3310 $actions = $this->primaryactions;
3311 $attributes = array(
3312 'class' => implode(' ', $linkclasses),
3313 'title' => $title,
3314 'id' => 'action-menu-toggle-'.$this->instance,
3315 'role' => 'menuitem'
3317 $link = html_writer::link('#', $string . $this->menutrigger . $pixicon, $attributes);
3318 if ($this->prioritise) {
3319 array_unshift($actions, $link);
3320 } else {
3321 $actions[] = $link;
3323 return $actions;
3327 * Returns the secondary actions ready to be rendered.
3328 * @return array
3330 public function get_secondary_actions() {
3331 return $this->secondaryactions;
3335 * Sets the selector that should be used to find the owning node of this menu.
3336 * @param string $selector A CSS/YUI selector to identify the owner of the menu.
3338 public function set_owner_selector($selector) {
3339 $this->attributes['data-owner'] = $selector;
3343 * Sets the alignment of the dialogue in relation to button used to toggle it.
3345 * @param int $dialogue One of action_menu::TL, action_menu::TR, action_menu::BL, action_menu::BR.
3346 * @param int $button One of action_menu::TL, action_menu::TR, action_menu::BL, action_menu::BR.
3348 public function set_alignment($dialogue, $button) {
3349 if (isset($this->attributessecondary['data-align'])) {
3350 // We've already got one set, lets remove the old class so as to avoid troubles.
3351 $class = $this->attributessecondary['class'];
3352 $search = 'align-'.$this->attributessecondary['data-align'];
3353 $this->attributessecondary['class'] = str_replace($search, '', $class);
3355 $align = $this->get_align_string($dialogue) . '-' . $this->get_align_string($button);
3356 $this->attributessecondary['data-align'] = $align;
3357 $this->attributessecondary['class'] .= ' align-'.$align;
3361 * Returns a string to describe the alignment.
3363 * @param int $align One of action_menu::TL, action_menu::TR, action_menu::BL, action_menu::BR.
3364 * @return string
3366 protected function get_align_string($align) {
3367 switch ($align) {
3368 case self::TL :
3369 return 'tl';
3370 case self::TR :
3371 return 'tr';
3372 case self::BL :
3373 return 'bl';
3374 case self::BR :
3375 return 'br';
3376 default :
3377 return 'tl';
3382 * Sets a constraint for the dialogue.
3384 * The constraint is applied when the dialogue is shown and limits the display of the dialogue to within the
3385 * element the constraint identifies.
3387 * @param string $ancestorselector A snippet of CSS used to identify the ancestor to contrain the dialogue to.
3389 public function set_constraint($ancestorselector) {
3390 $this->attributessecondary['data-constraint'] = $ancestorselector;
3394 * If you call this method the action menu will be displayed but will not be enhanced.
3396 * By not displaying the menu enhanced all items will be displayed in a single row.
3398 public function do_not_enhance() {
3399 unset($this->attributes['data-enhance']);
3403 * Returns true if this action menu will be enhanced.
3405 * @return bool
3407 public function will_be_enhanced() {
3408 return isset($this->attributes['data-enhance']);
3412 * Sets nowrap on items. If true menu items should not wrap lines if they are longer than the available space.
3414 * This property can be useful when the action menu is displayed within a parent element that is either floated
3415 * or relatively positioned.
3416 * In that situation the width of the menu is determined by the width of the parent element which may not be large
3417 * enough for the menu items without them wrapping.
3418 * This disables the wrapping so that the menu takes on the width of the longest item.
3420 * @param bool $value If true nowrap gets set, if false it gets removed. Defaults to true.
3422 public function set_nowrap_on_items($value = true) {
3423 $class = 'nowrap-items';
3424 if (!empty($this->attributes['class'])) {
3425 $pos = strpos($this->attributes['class'], $class);
3426 if ($value === true && $pos === false) {
3427 // The value is true and the class has not been set yet. Add it.
3428 $this->attributes['class'] .= ' '.$class;
3429 } else if ($value === false && $pos !== false) {
3430 // The value is false and the class has been set. Remove it.
3431 $this->attributes['class'] = substr($this->attributes['class'], $pos, strlen($class));
3433 } else if ($value) {
3434 // The value is true and the class has not been set yet. Add it.
3435 $this->attributes['class'] = $class;
3441 * An action menu filler
3443 * @package core
3444 * @category output
3445 * @copyright 2013 Andrew Nicols
3446 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3448 class action_menu_filler extends action_link implements renderable {
3451 * True if this is a primary action. False if not.
3452 * @var bool
3454 public $primary = true;
3457 * Constructs the object.
3459 public function __construct() {
3464 * An action menu action
3466 * @package core
3467 * @category output
3468 * @copyright 2013 Sam Hemelryk
3469 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3471 class action_menu_link extends action_link implements renderable {
3474 * True if this is a primary action. False if not.
3475 * @var bool
3477 public $primary = true;
3480 * The action menu this link has been added to.
3481 * @var action_menu
3483 public $actionmenu = null;
3486 * Constructs the object.
3488 * @param moodle_url $url The URL for the action.
3489 * @param pix_icon $icon The icon to represent the action.
3490 * @param string $text The text to represent the action.
3491 * @param bool $primary Whether this is a primary action or not.
3492 * @param array $attributes Any attribtues associated with the action.
3494 public function __construct(moodle_url $url, pix_icon $icon = null, $text, $primary = true, array $attributes = array()) {
3495 parent::__construct($url, $text, null, $attributes, $icon);
3496 $this->primary = (bool)$primary;
3497 $this->add_class('menu-action');
3498 $this->attributes['role'] = 'menuitem';
3503 * A primary action menu action
3505 * @package core
3506 * @category output
3507 * @copyright 2013 Sam Hemelryk
3508 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3510 class action_menu_link_primary extends action_menu_link {
3512 * Constructs the object.
3514 * @param moodle_url $url
3515 * @param pix_icon $icon
3516 * @param string $text
3517 * @param array $attributes
3519 public function __construct(moodle_url $url, pix_icon $icon = null, $text, array $attributes = array()) {
3520 parent::__construct($url, $icon, $text, true, $attributes);
3525 * A secondary action menu action
3527 * @package core
3528 * @category output
3529 * @copyright 2013 Sam Hemelryk
3530 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3532 class action_menu_link_secondary extends action_menu_link {
3534 * Constructs the object.
3536 * @param moodle_url $url
3537 * @param pix_icon $icon
3538 * @param string $text
3539 * @param array $attributes
3541 public function __construct(moodle_url $url, pix_icon $icon = null, $text, array $attributes = array()) {
3542 parent::__construct($url, $icon, $text, false, $attributes);