3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Classes representing HTML elements, used by $OUTPUT methods
21 * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
26 * @copyright 2009 Tim Hunt
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 defined('MOODLE_INTERNAL') || die();
33 * Interface marking other classes as suitable for renderer_base::render()
34 * @author 2010 Petr Skoda (skodak) info@skodak.org
36 interface renderable {
37 // intentionally empty
41 * Data structure representing a file picker.
43 * @copyright 2010 Dongsheng Cai
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
47 class file_picker implements renderable {
49 public function __construct(stdClass $options) {
50 global $CFG, $USER, $PAGE;
51 require_once($CFG->dirroot. '/repository/lib.php');
53 'accepted_types'=>'*',
54 'return_types'=>FILE_INTERNAL,
55 'env' => 'filepicker',
56 'client_id' => uniqid(),
61 foreach ($defaults as $key=>$value) {
62 if (empty($options->$key)) {
63 $options->$key = $value;
67 $options->currentfile = '';
68 if (!empty($options->itemid)) {
69 $fs = get_file_storage();
70 $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
71 if (empty($options->filename)) {
72 if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id DESC', false)) {
73 $file = reset($files);
76 $file = $fs->get_file($usercontext->id, 'user', 'draft', $options->itemid, $options->filepath, $options->filename);
79 $options->currentfile = html_writer::link(moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()), $file->get_filename());
83 // initilise options, getting files in root path
84 $this->options = initialise_filepicker($options);
86 // copying other options
87 foreach ($options as $name=>$value) {
88 if (!isset($this->options->$name)) {
89 $this->options->$name = $value;
96 * Data structure representing a user picture.
98 * @copyright 2009 Nicolas Connault, 2010 Petr Skoda
99 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
102 class user_picture implements renderable {
104 * @var array List of mandatory fields in user record here. (do not include TEXT columns because it would break SELECT DISTINCT in MSSQL and ORACLE)
106 protected static $fields = array('id', 'picture', 'firstname', 'lastname', 'imagealt', 'email');
109 * @var object $user A user object with at least fields all columns specified in $fields array constant set.
113 * @var int $courseid The course id. Used when constructing the link to the user's profile,
114 * page course id used if not specified.
118 * @var bool $link add course profile link to image
122 * @var int $size Size in pixels. Special values are (true/1 = 100px) and (false/0 = 35px) for backward compatibility
126 * @var boolean $alttext add non-blank alt-text to the image.
127 * Default true, set to false when image alt just duplicates text in screenreaders.
129 public $alttext = true;
131 * @var boolean $popup Whether or not to open the link in a popup window.
133 public $popup = false;
135 * @var string Image class attribute
137 public $class = 'userpicture';
140 * User picture constructor.
142 * @param object $user user record with at least id, picture, imagealt, firstname and lastname set.
143 * @param array $options such as link, size, link, ...
145 public function __construct(stdClass $user) {
148 if (empty($user->id)) {
149 throw new coding_exception('User id is required when printing user avatar image.');
152 // only touch the DB if we are missing data and complain loudly...
154 foreach (self::$fields as $field) {
155 if (!array_key_exists($field, $user)) {
157 debugging('Missing '.$field.' property in $user object, this is a performance problem that needs to be fixed by a developer. '
158 .'Please use user_picture::fields() to get the full list of required fields.', DEBUG_DEVELOPER);
164 $this->user = $DB->get_record('user', array('id'=>$user->id), self::fields(), MUST_EXIST);
166 $this->user = clone($user);
171 * Returns a list of required user fields, useful when fetching required user info from db.
173 * In some cases we have to fetch the user data together with some other information,
174 * the idalias is useful there because the id would otherwise override the main
175 * id of the result record. Please note it has to be converted back to id before rendering.
177 * @param string $tableprefix name of database table prefix in query
178 * @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)
179 * @param string $idalias alias of id field
182 public static function fields($tableprefix = '', array $extrafields = NULL, $idalias = 'id') {
183 if (!$tableprefix and !$extrafields and !$idalias) {
184 return implode(',', self::$fields);
190 foreach (self::$fields as $field) {
191 if ($field === 'id' and $idalias and $idalias !== 'id') {
192 $fields[$field] = "$tableprefix$field AS $idalias";
194 $fields[$field] = $tableprefix.$field;
197 // add extra fields if not already there
199 foreach ($extrafields as $e) {
200 if ($e === 'id' or isset($fields[$e])) {
203 $fields[$e] = $tableprefix.$e;
206 return implode(',', $fields);
212 * Data structure representing a help icon.
214 * @copyright 2009 Nicolas Connault, 2010 Petr Skoda
215 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
218 class old_help_icon implements renderable {
220 * @var string $helpidentifier lang pack identifier
222 public $helpidentifier;
224 * @var string $title A descriptive text for title tooltip
226 public $title = null;
228 * @var string $component Component name, the same as in get_string()
230 public $component = 'moodle';
232 * @var string $linktext Extra descriptive text next to the icon
234 public $linktext = null;
237 * Constructor: sets up the other components in case they are needed
238 * @param string $helpidentifier The keyword that defines a help page
239 * @param string $title A descriptive text for accessibility only
240 * @param string $component
241 * @param bool $linktext add extra text to icon
244 public function __construct($helpidentifier, $title, $component = 'moodle') {
246 throw new coding_exception('A help_icon object requires a $text parameter');
248 if (empty($helpidentifier)) {
249 throw new coding_exception('A help_icon object requires a $helpidentifier parameter');
252 $this->helpidentifier = $helpidentifier;
253 $this->title = $title;
254 $this->component = $component;
259 * Data structure representing a help icon.
261 * @copyright 2010 Petr Skoda (info@skodak.org)
262 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
265 class help_icon implements renderable {
267 * @var string $identifier lang pack identifier (without the "_help" suffix),
268 * both get_string($identifier, $component) and get_string($identifier.'_help', $component)
273 * @var string $component Component name, the same as in get_string()
277 * @var string $linktext Extra descriptive text next to the icon
279 public $linktext = null;
283 * @param string $identifier string for help page title,
284 * string with _help suffix is used for the actual help text.
285 * string with _link suffix is used to create a link to further info (if it exists)
286 * @param string $component
288 public function __construct($identifier, $component) {
289 $this->identifier = $identifier;
290 $this->component = $component;
294 * Verifies that both help strings exists, shows debug warnings if not
296 public function diag_strings() {
297 $sm = get_string_manager();
298 if (!$sm->string_exists($this->identifier, $this->component)) {
299 debugging("Help title string does not exist: [$this->identifier, $this->component]");
301 if (!$sm->string_exists($this->identifier.'_help', $this->component)) {
302 debugging("Help contents string does not exist: [{$this->identifier}_help, $this->component]");
309 * Data structure representing an icon.
311 * @copyright 2010 Petr Skoda
312 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
315 class pix_icon implements renderable {
318 var $attributes = array();
322 * @param string $pix short icon name
323 * @param string $component component name
324 * @param array $attributes html attributes
326 public function __construct($pix, $alt, $component='moodle', array $attributes = null) {
328 $this->component = $component;
329 $this->attributes = (array)$attributes;
331 $this->attributes['alt'] = $alt;
332 if (empty($this->attributes['class'])) {
333 $this->attributes['class'] = 'smallicon';
335 if (!isset($this->attributes['title'])) {
336 $this->attributes['title'] = $this->attributes['alt'];
342 * Data structure representing an emoticon image
346 class pix_emoticon extends pix_icon implements renderable {
350 * @param string $pix short icon name
351 * @param string $alt alternative text
352 * @param string $component emoticon image provider
353 * @param array $attributes explicit HTML attributes
355 public function __construct($pix, $alt, $component = 'moodle', array $attributes = array()) {
356 if (empty($attributes['class'])) {
357 $attributes['class'] = 'emoticon';
359 parent::__construct($pix, $alt, $component, $attributes);
364 * Data structure representing a simple form with only one button.
366 * @copyright 2009 Petr Skoda
367 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
370 class single_button implements renderable {
383 * @var string post or get
385 var $method = 'post';
390 var $class = 'singlebutton';
392 * True if button disabled, false if normal
395 var $disabled = false;
407 * List of attached actions
408 * @var array of component_action
410 var $actions = array();
414 * @param string|moodle_url $url
415 * @param string $label button text
416 * @param string $method get or post submit method
418 public function __construct(moodle_url $url, $label, $method='post') {
419 $this->url = clone($url);
420 $this->label = $label;
421 $this->method = $method;
425 * Shortcut for adding a JS confirm dialog when the button is clicked.
426 * The message must be a yes/no question.
427 * @param string $message The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
430 public function add_confirm_action($confirmmessage) {
431 $this->add_action(new component_action('click', 'M.util.show_confirm_dialog', array('message' => $confirmmessage)));
435 * Add action to the button.
436 * @param component_action $action
439 public function add_action(component_action $action) {
440 $this->actions[] = $action;
446 * Simple form with just one select field that gets submitted automatically.
447 * If JS not enabled small go button is printed too.
449 * @copyright 2009 Petr Skoda
450 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
453 class single_select implements renderable {
455 * Target url - includes hidden fields
460 * Name of the select element.
465 * @var array $options associative array value=>label ex.:
466 * array(1=>'One, 2=>Two)
467 * it is also possible to specify optgroup as complex label array ex.:
468 * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
469 * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
483 * Extra select field attributes
486 var $attributes = array();
494 * @var string post or get
501 var $class = 'singleselect';
503 * True if button disabled, false if normal
506 var $disabled = false;
518 * List of attached actions
519 * @var array of component_action
521 var $helpicon = null;
524 * @param moodle_url $url form action target, includes hidden fields
525 * @param string $name name of selection field - the changing parameter in url
526 * @param array $options list of options
527 * @param string $selected selected element
528 * @param array $nothing
529 * @param string $formid
531 public function __construct(moodle_url $url, $name, array $options, $selected='', $nothing=array(''=>'choosedots'), $formid=null) {
534 $this->options = $options;
535 $this->selected = $selected;
536 $this->nothing = $nothing;
537 $this->formid = $formid;
541 * Shortcut for adding a JS confirm dialog when the button is clicked.
542 * The message must be a yes/no question.
543 * @param string $message The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
546 public function add_confirm_action($confirmmessage) {
547 $this->add_action(new component_action('submit', 'M.util.show_confirm_dialog', array('message' => $confirmmessage)));
551 * Add action to the button.
552 * @param component_action $action
555 public function add_action(component_action $action) {
556 $this->actions[] = $action;
561 * @param string $page The keyword that defines a help page
562 * @param string $title A descriptive text for accessibility only
563 * @param string $component
564 * @param bool $linktext add extra text to icon
567 public function set_old_help_icon($helppage, $title, $component = 'moodle') {
568 $this->helpicon = new old_help_icon($helppage, $title, $component);
573 * @param string $identifier The keyword that defines a help page
574 * @param string $component
575 * @param bool $linktext add extra text to icon
578 public function set_help_icon($identifier, $component = 'moodle') {
579 $this->helpicon = new help_icon($identifier, $component);
583 * Sets select's label
584 * @param string $label
587 public function set_label($label) {
588 $this->label = $label;
594 * Simple URL selection widget description.
595 * @copyright 2009 Petr Skoda
596 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
599 class url_select implements renderable {
601 * @var array $urls associative array value=>label ex.:
602 * array(1=>'One, 2=>Two)
603 * it is also possible to specify optgroup as complex label array ex.:
604 * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
605 * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
619 * Extra select field attributes
622 var $attributes = array();
632 var $class = 'urlselect';
634 * True if button disabled, false if normal
637 var $disabled = false;
649 * List of attached actions
650 * @var array of component_action
652 var $helpicon = null;
655 * @param array $urls list of options
656 * @param string $selected selected element
657 * @param array $nothing
658 * @param string $formid
660 public function __construct(array $urls, $selected='', $nothing=array(''=>'choosedots'), $formid=null) {
662 $this->selected = $selected;
663 $this->nothing = $nothing;
664 $this->formid = $formid;
669 * @param string $page The keyword that defines a help page
670 * @param string $title A descriptive text for accessibility only
671 * @param string $component
672 * @param bool $linktext add extra text to icon
675 public function set_old_help_icon($helppage, $title, $component = 'moodle') {
676 $this->helpicon = new old_help_icon($helppage, $title, $component);
681 * @param string $identifier The keyword that defines a help page
682 * @param string $component
683 * @param bool $linktext add extra text to icon
686 public function set_help_icon($identifier, $component = 'moodle') {
687 $this->helpicon = new help_icon($identifier, $component);
691 * Sets select's label
692 * @param string $label
695 public function set_label($label) {
696 $this->label = $label;
702 * Data structure describing html link with special action attached.
703 * @copyright 2010 Petr Skoda
704 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
707 class action_link implements renderable {
715 * @var string HTML fragment
724 * List of actions attached to link
725 * @var array of component_action
731 * @param string|moodle_url $url
732 * @param string $text HTML fragment
733 * @param component_action $action
734 * @param array $attributes associative array of html link attributes + disabled
736 public function __construct(moodle_url $url, $text, component_action $action=null, array $attributes=null) {
737 $this->url = clone($url);
739 $this->attributes = (array)$attributes;
741 $this->add_action($action);
746 * Add action to the link.
747 * @param component_action $action
750 public function add_action(component_action $action) {
751 $this->actions[] = $action;
754 public function add_class($class) {
755 if (empty($this->attributes['class'])) {
756 $this->attributes['class'] = $class;
758 $this->attributes['class'] .= ' ' . $class;
763 // ==== HTML writer and helper classes, will be probably moved elsewhere ======
766 * Simple html output class
767 * @copyright 2009 Tim Hunt, 2010 Petr Skoda
771 * Outputs a tag with attributes and contents
772 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
773 * @param string $contents What goes between the opening and closing tags
774 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
775 * @return string HTML fragment
777 public static function tag($tagname, $contents, array $attributes = null) {
778 return self::start_tag($tagname, $attributes) . $contents . self::end_tag($tagname);
782 * Outputs an opening tag with attributes
783 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
784 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
785 * @return string HTML fragment
787 public static function start_tag($tagname, array $attributes = null) {
788 return '<' . $tagname . self::attributes($attributes) . '>';
792 * Outputs a closing tag
793 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
794 * @return string HTML fragment
796 public static function end_tag($tagname) {
797 return '</' . $tagname . '>';
801 * Outputs an empty tag with attributes
802 * @param string $tagname The name of tag ('input', 'img', 'br' etc.)
803 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
804 * @return string HTML fragment
806 public static function empty_tag($tagname, array $attributes = null) {
807 return '<' . $tagname . self::attributes($attributes) . ' />';
811 * Outputs a tag, but only if the contents are not empty
812 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
813 * @param string $contents What goes between the opening and closing tags
814 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
815 * @return string HTML fragment
817 public static function nonempty_tag($tagname, $contents, array $attributes = null) {
818 if ($contents === '' || is_null($contents)) {
821 return self::tag($tagname, $contents, $attributes);
825 * Outputs a HTML attribute and value
826 * @param string $name The name of the attribute ('src', 'href', 'class' etc.)
827 * @param string $value The value of the attribute. The value will be escaped with {@link s()}
828 * @return string HTML fragment
830 public static function attribute($name, $value) {
831 if (is_array($value)) {
832 debugging("Passed an array for the HTML attribute $name", DEBUG_DEVELOPER);
834 if ($value instanceof moodle_url) {
835 return ' ' . $name . '="' . $value->out() . '"';
838 // special case, we do not want these in output
839 if ($value === null) {
843 // no sloppy trimming here!
844 return ' ' . $name . '="' . s($value) . '"';
848 * Outputs a list of HTML attributes and values
849 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
850 * The values will be escaped with {@link s()}
851 * @return string HTML fragment
853 public static function attributes(array $attributes = null) {
854 $attributes = (array)$attributes;
856 foreach ($attributes as $name => $value) {
857 $output .= self::attribute($name, $value);
863 * Generates random html element id.
864 * @param string $base
867 public static function random_id($base='random') {
868 return uniqid($base);
872 * Generates a simple html link
873 * @param string|moodle_url $url
874 * @param string $text link txt
875 * @param array $attributes extra html attributes
876 * @return string HTML fragment
878 public static function link($url, $text, array $attributes = null) {
879 $attributes = (array)$attributes;
880 $attributes['href'] = $url;
881 return self::tag('a', $text, $attributes);
885 * generates a simple checkbox with optional label
886 * @param string $name
887 * @param string $value
888 * @param bool $checked
889 * @param string $label
890 * @param array $attributes
891 * @return string html fragment
893 public static function checkbox($name, $value, $checked = true, $label = '', array $attributes = null) {
894 $attributes = (array)$attributes;
897 if ($label !== '' and !is_null($label)) {
898 if (empty($attributes['id'])) {
899 $attributes['id'] = self::random_id('checkbox_');
902 $attributes['type'] = 'checkbox';
903 $attributes['value'] = $value;
904 $attributes['name'] = $name;
905 $attributes['checked'] = $checked ? 'checked' : null;
907 $output .= self::empty_tag('input', $attributes);
909 if ($label !== '' and !is_null($label)) {
910 $output .= self::tag('label', $label, array('for'=>$attributes['id']));
917 * Generates a simple select yes/no form field
918 * @param string $name name of select element
919 * @param bool $selected
920 * @param array $attributes - html select element attributes
921 * @return string HRML fragment
923 public static function select_yes_no($name, $selected=true, array $attributes = null) {
924 $options = array('1'=>get_string('yes'), '0'=>get_string('no'));
925 return self::select($options, $name, $selected, null, $attributes);
929 * Generates a simple select form field
930 * @param array $options associative array value=>label ex.:
931 * array(1=>'One, 2=>Two)
932 * it is also possible to specify optgroup as complex label array ex.:
933 * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
934 * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
935 * @param string $name name of select element
936 * @param string|array $selected value or array of values depending on multiple attribute
937 * @param array|bool $nothing, add nothing selected option, or false of not added
938 * @param array $attributes - html select element attributes
939 * @return string HTML fragment
941 public static function select(array $options, $name, $selected = '', $nothing = array(''=>'choosedots'), array $attributes = null) {
942 $attributes = (array)$attributes;
943 if (is_array($nothing)) {
944 foreach ($nothing as $k=>$v) {
945 if ($v === 'choose' or $v === 'choosedots') {
946 $nothing[$k] = get_string('choosedots');
949 $options = $nothing + $options; // keep keys, do not override
951 } else if (is_string($nothing) and $nothing !== '') {
953 $options = array(''=>$nothing) + $options;
956 // we may accept more values if multiple attribute specified
957 $selected = (array)$selected;
958 foreach ($selected as $k=>$v) {
959 $selected[$k] = (string)$v;
962 if (!isset($attributes['id'])) {
964 // name may contaion [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading
965 $id = str_replace('[', '', $id);
966 $id = str_replace(']', '', $id);
967 $attributes['id'] = $id;
970 if (!isset($attributes['class'])) {
971 $class = 'menu'.$name;
972 // name may contaion [], which would make an invalid class. e.g. numeric question type editing form, assignment quickgrading
973 $class = str_replace('[', '', $class);
974 $class = str_replace(']', '', $class);
975 $attributes['class'] = $class;
977 $attributes['class'] = 'select ' . $attributes['class']; /// Add 'select' selector always
979 $attributes['name'] = $name;
982 foreach ($options as $value=>$label) {
983 if (is_array($label)) {
984 // ignore key, it just has to be unique
985 $output .= self::select_optgroup(key($label), current($label), $selected);
987 $output .= self::select_option($label, $value, $selected);
990 return self::tag('select', $output, $attributes);
993 private static function select_option($label, $value, array $selected) {
994 $attributes = array();
995 $value = (string)$value;
996 if (in_array($value, $selected, true)) {
997 $attributes['selected'] = 'selected';
999 $attributes['value'] = $value;
1000 return self::tag('option', $label, $attributes);
1003 private static function select_optgroup($groupname, $options, array $selected) {
1004 if (empty($options)) {
1007 $attributes = array('label'=>$groupname);
1009 foreach ($options as $value=>$label) {
1010 $output .= self::select_option($label, $value, $selected);
1012 return self::tag('optgroup', $output, $attributes);
1016 * This is a shortcut for making an hour selector menu.
1017 * @param string $type The type of selector (years, months, days, hours, minutes)
1018 * @param string $name fieldname
1019 * @param int $currenttime A default timestamp in GMT
1020 * @param int $step minute spacing
1021 * @param array $attributes - html select element attributes
1022 * @return HTML fragment
1024 public static function select_time($type, $name, $currenttime=0, $step=5, array $attributes=null) {
1025 if (!$currenttime) {
1026 $currenttime = time();
1028 $currentdate = usergetdate($currenttime);
1029 $userdatetype = $type;
1030 $timeunits = array();
1034 for ($i=1970; $i<=2020; $i++) {
1035 $timeunits[$i] = $i;
1037 $userdatetype = 'year';
1040 for ($i=1; $i<=12; $i++) {
1041 $timeunits[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
1043 $userdatetype = 'month';
1044 $currentdate['month'] = $currentdate['mon'];
1047 for ($i=1; $i<=31; $i++) {
1048 $timeunits[$i] = $i;
1050 $userdatetype = 'mday';
1053 for ($i=0; $i<=23; $i++) {
1054 $timeunits[$i] = sprintf("%02d",$i);
1059 $currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step;
1062 for ($i=0; $i<=59; $i+=$step) {
1063 $timeunits[$i] = sprintf("%02d",$i);
1067 throw new coding_exception("Time type $type is not supported by html_writer::select_time().");
1070 if (empty($attributes['id'])) {
1071 $attributes['id'] = self::random_id('ts_');
1073 $timerselector = self::select($timeunits, $name, $currentdate[$userdatetype], null, array('id'=>$attributes['id']));
1074 $label = self::tag('label', get_string(substr($type, 0, -1), 'form'), array('for'=>$attributes['id'], 'class'=>'accesshide'));
1076 return $label.$timerselector;
1080 * Shortcut for quick making of lists
1081 * @param array $items
1082 * @param string $tag ul or ol
1083 * @param array $attributes
1086 public static function alist(array $items, array $attributes = null, $tag = 'ul') {
1087 //note: 'list' is a reserved keyword ;-)
1091 foreach ($items as $item) {
1092 $output .= html_writer::start_tag('li') . "\n";
1093 $output .= $item . "\n";
1094 $output .= html_writer::end_tag('li') . "\n";
1097 return html_writer::tag($tag, $output, $attributes);
1101 * Returns hidden input fields created from url parameters.
1102 * @param moodle_url $url
1103 * @param array $exclude list of excluded parameters
1104 * @return string HTML fragment
1106 public static function input_hidden_params(moodle_url $url, array $exclude = null) {
1107 $exclude = (array)$exclude;
1108 $params = $url->params();
1109 foreach ($exclude as $key) {
1110 unset($params[$key]);
1114 foreach ($params as $key => $value) {
1115 $attributes = array('type'=>'hidden', 'name'=>$key, 'value'=>$value);
1116 $output .= self::empty_tag('input', $attributes)."\n";
1122 * Generate a script tag containing the the specified code.
1124 * @param string $js the JavaScript code
1125 * @param moodle_url|string optional url of the external script, $code ignored if specified
1126 * @return string HTML, the code wrapped in <script> tags.
1128 public static function script($jscode, $url=null) {
1130 $attributes = array('type'=>'text/javascript');
1131 return self::tag('script', "\n//<![CDATA[\n$jscode\n//]]>\n", $attributes) . "\n";
1134 $attributes = array('type'=>'text/javascript', 'src'=>$url);
1135 return self::tag('script', '', $attributes) . "\n";
1143 * Renders HTML table
1145 * This method may modify the passed instance by adding some default properties if they are not set yet.
1146 * If this is not what you want, you should make a full clone of your data before passing them to this
1147 * method. In most cases this is not an issue at all so we do not clone by default for performance
1148 * and memory consumption reasons.
1150 * @param html_table $table data to be rendered
1151 * @return string HTML code
1153 public static function table(html_table $table) {
1154 // prepare table data and populate missing properties with reasonable defaults
1155 if (!empty($table->align)) {
1156 foreach ($table->align as $key => $aa) {
1158 $table->align[$key] = 'text-align:'. fix_align_rtl($aa) .';'; // Fix for RTL languages
1160 $table->align[$key] = null;
1164 if (!empty($table->size)) {
1165 foreach ($table->size as $key => $ss) {
1167 $table->size[$key] = 'width:'. $ss .';';
1169 $table->size[$key] = null;
1173 if (!empty($table->wrap)) {
1174 foreach ($table->wrap as $key => $ww) {
1176 $table->wrap[$key] = 'white-space:nowrap;';
1178 $table->wrap[$key] = '';
1182 if (!empty($table->head)) {
1183 foreach ($table->head as $key => $val) {
1184 if (!isset($table->align[$key])) {
1185 $table->align[$key] = null;
1187 if (!isset($table->size[$key])) {
1188 $table->size[$key] = null;
1190 if (!isset($table->wrap[$key])) {
1191 $table->wrap[$key] = null;
1196 if (empty($table->attributes['class'])) {
1197 $table->attributes['class'] = 'generaltable';
1199 if (!empty($table->tablealign)) {
1200 $table->attributes['class'] .= ' boxalign' . $table->tablealign;
1203 // explicitly assigned properties override those defined via $table->attributes
1204 $table->attributes['class'] = trim($table->attributes['class']);
1205 $attributes = array_merge($table->attributes, array(
1207 'width' => $table->width,
1208 'summary' => $table->summary,
1209 'cellpadding' => $table->cellpadding,
1210 'cellspacing' => $table->cellspacing,
1212 $output = html_writer::start_tag('table', $attributes) . "\n";
1216 if (!empty($table->head)) {
1217 $countcols = count($table->head);
1219 $output .= html_writer::start_tag('thead', array()) . "\n";
1220 $output .= html_writer::start_tag('tr', array()) . "\n";
1221 $keys = array_keys($table->head);
1222 $lastkey = end($keys);
1224 foreach ($table->head as $key => $heading) {
1225 // Convert plain string headings into html_table_cell objects
1226 if (!($heading instanceof html_table_cell)) {
1227 $headingtext = $heading;
1228 $heading = new html_table_cell();
1229 $heading->text = $headingtext;
1230 $heading->header = true;
1233 if ($heading->header !== false) {
1234 $heading->header = true;
1237 if ($heading->header && empty($heading->scope)) {
1238 $heading->scope = 'col';
1241 $heading->attributes['class'] .= ' header c' . $key;
1242 if (isset($table->headspan[$key]) && $table->headspan[$key] > 1) {
1243 $heading->colspan = $table->headspan[$key];
1244 $countcols += $table->headspan[$key] - 1;
1247 if ($key == $lastkey) {
1248 $heading->attributes['class'] .= ' lastcol';
1250 if (isset($table->colclasses[$key])) {
1251 $heading->attributes['class'] .= ' ' . $table->colclasses[$key];
1253 $heading->attributes['class'] = trim($heading->attributes['class']);
1254 $attributes = array_merge($heading->attributes, array(
1255 'style' => $table->align[$key] . $table->size[$key] . $heading->style,
1256 'scope' => $heading->scope,
1257 'colspan' => $heading->colspan,
1261 if ($heading->header === true) {
1264 $output .= html_writer::tag($tagtype, $heading->text, $attributes) . "\n";
1266 $output .= html_writer::end_tag('tr') . "\n";
1267 $output .= html_writer::end_tag('thead') . "\n";
1269 if (empty($table->data)) {
1270 // For valid XHTML strict every table must contain either a valid tr
1271 // or a valid tbody... both of which must contain a valid td
1272 $output .= html_writer::start_tag('tbody', array('class' => 'empty'));
1273 $output .= html_writer::tag('tr', html_writer::tag('td', '', array('colspan'=>count($table->head))));
1274 $output .= html_writer::end_tag('tbody');
1278 if (!empty($table->data)) {
1280 $keys = array_keys($table->data);
1281 $lastrowkey = end($keys);
1282 $output .= html_writer::start_tag('tbody', array());
1284 foreach ($table->data as $key => $row) {
1285 if (($row === 'hr') && ($countcols)) {
1286 $output .= html_writer::tag('td', html_writer::tag('div', '', array('class' => 'tabledivider')), array('colspan' => $countcols));
1288 // Convert array rows to html_table_rows and cell strings to html_table_cell objects
1289 if (!($row instanceof html_table_row)) {
1290 $newrow = new html_table_row();
1292 foreach ($row as $item) {
1293 $cell = new html_table_cell();
1294 $cell->text = $item;
1295 $newrow->cells[] = $cell;
1300 $oddeven = $oddeven ? 0 : 1;
1301 if (isset($table->rowclasses[$key])) {
1302 $row->attributes['class'] .= ' ' . $table->rowclasses[$key];
1305 $row->attributes['class'] .= ' r' . $oddeven;
1306 if ($key == $lastrowkey) {
1307 $row->attributes['class'] .= ' lastrow';
1310 $output .= html_writer::start_tag('tr', array('class' => trim($row->attributes['class']), 'style' => $row->style, 'id' => $row->id)) . "\n";
1311 $keys2 = array_keys($row->cells);
1312 $lastkey = end($keys2);
1314 $gotlastkey = false; //flag for sanity checking
1315 foreach ($row->cells as $key => $cell) {
1317 //This should never happen. Why do we have a cell after the last cell?
1318 mtrace("A cell with key ($key) was found after the last key ($lastkey)");
1321 if (!($cell instanceof html_table_cell)) {
1322 $mycell = new html_table_cell();
1323 $mycell->text = $cell;
1327 if (($cell->header === true) && empty($cell->scope)) {
1328 $cell->scope = 'row';
1331 if (isset($table->colclasses[$key])) {
1332 $cell->attributes['class'] .= ' ' . $table->colclasses[$key];
1335 $cell->attributes['class'] .= ' cell c' . $key;
1336 if ($key == $lastkey) {
1337 $cell->attributes['class'] .= ' lastcol';
1341 $tdstyle .= isset($table->align[$key]) ? $table->align[$key] : '';
1342 $tdstyle .= isset($table->size[$key]) ? $table->size[$key] : '';
1343 $tdstyle .= isset($table->wrap[$key]) ? $table->wrap[$key] : '';
1344 $cell->attributes['class'] = trim($cell->attributes['class']);
1345 $tdattributes = array_merge($cell->attributes, array(
1346 'style' => $tdstyle . $cell->style,
1347 'colspan' => $cell->colspan,
1348 'rowspan' => $cell->rowspan,
1350 'abbr' => $cell->abbr,
1351 'scope' => $cell->scope,
1354 if ($cell->header === true) {
1357 $output .= html_writer::tag($tagtype, $cell->text, $tdattributes) . "\n";
1360 $output .= html_writer::end_tag('tr') . "\n";
1362 $output .= html_writer::end_tag('tbody') . "\n";
1364 $output .= html_writer::end_tag('table') . "\n";
1370 * Renders form element label
1372 * By default, the label is suffixed with a label separator defined in the
1373 * current language pack (colon by default in the English lang pack).
1374 * Adding the colon can be explicitly disabled if needed. Label separators
1375 * are put outside the label tag itself so they are not read by
1376 * screenreaders (accessibility).
1378 * Parameter $for explicitly associates the label with a form control. When
1379 * set, the value of this attribute must be the same as the value of
1380 * the id attribute of the form control in the same document. When null,
1381 * the label being defined is associated with the control inside the label
1384 * @param string $text content of the label tag
1385 * @param string|null $for id of the element this label is associated with, null for no association
1386 * @param bool $colonize add label separator (colon) to the label text, if it is not there yet
1387 * @param array $attributes to be inserted in the tab, for example array('accesskey' => 'a')
1388 * @return string HTML of the label element
1390 public static function label($text, $for, $colonize=true, array $attributes=array()) {
1391 if (!is_null($for)) {
1392 $attributes = array_merge($attributes, array('for' => $for));
1394 $text = trim($text);
1395 $label = self::tag('label', $text, $attributes);
1398 // TODO $colonize disabled for now yet - see MDL-12192 for details
1399 if (!empty($text) and $colonize) {
1400 // the $text may end with the colon already, though it is bad string definition style
1401 $colon = get_string('labelsep', 'langconfig');
1402 if (!empty($colon)) {
1403 $trimmed = trim($colon);
1404 if ((substr($text, -strlen($trimmed)) == $trimmed) or (substr($text, -1) == ':')) {
1405 //debugging('The label text should not end with colon or other label separator,
1406 // please fix the string definition.', DEBUG_DEVELOPER);
1418 // ==== JS writer and helper classes, will be probably moved elsewhere ======
1421 * Simple javascript output class
1422 * @copyright 2010 Petr Skoda
1426 * Returns javascript code calling the function
1427 * @param string $function function name, can be complex like Y.Event.purgeElement
1428 * @param array $arguments parameters
1429 * @param int $delay execution delay in seconds
1430 * @return string JS code fragment
1432 public static function function_call($function, array $arguments = null, $delay=0) {
1434 $arguments = array_map('json_encode', $arguments);
1435 $arguments = implode(', ', $arguments);
1439 $js = "$function($arguments);";
1442 $delay = $delay * 1000; // in miliseconds
1443 $js = "setTimeout(function() { $js }, $delay);";
1449 * Special function which adds Y as first argument of fucntion call.
1450 * @param string $function
1451 * @param array $extraarguments
1454 public static function function_call_with_Y($function, array $extraarguments = null) {
1455 if ($extraarguments) {
1456 $extraarguments = array_map('json_encode', $extraarguments);
1457 $arguments = 'Y, ' . implode(', ', $extraarguments);
1461 return "$function($arguments);\n";
1465 * Returns JavaScript code to initialise a new object
1466 * @param string|null $var If it is null then no var is assigned the new object
1467 * @param string $class
1468 * @param array $arguments
1469 * @param array $requirements
1473 public static function object_init($var, $class, array $arguments = null, array $requirements = null, $delay=0) {
1474 if (is_array($arguments)) {
1475 $arguments = array_map('json_encode', $arguments);
1476 $arguments = implode(', ', $arguments);
1479 if ($var === null) {
1480 $js = "new $class(Y, $arguments);";
1481 } else if (strpos($var, '.')!==false) {
1482 $js = "$var = new $class(Y, $arguments);";
1484 $js = "var $var = new $class(Y, $arguments);";
1488 $delay = $delay * 1000; // in miliseconds
1489 $js = "setTimeout(function() { $js }, $delay);";
1492 if (count($requirements) > 0) {
1493 $requirements = implode("', '", $requirements);
1494 $js = "Y.use('$requirements', function(Y){ $js });";
1500 * Returns code setting value to variable
1501 * @param string $name
1502 * @param mixed $value json serialised value
1503 * @param bool $usevar add var definition, ignored for nested properties
1504 * @return string JS code fragment
1506 public static function set_variable($name, $value, $usevar=true) {
1510 if (strpos($name, '.')) {
1517 $output .= "$name = ".json_encode($value).";";
1523 * Writes event handler attaching code
1524 * @param mixed $selector standard YUI selector for elements, may be array or string, element id is in the form "#idvalue"
1525 * @param string $event A valid DOM event (click, mousedown, change etc.)
1526 * @param string $function The name of the function to call
1527 * @param array $arguments An optional array of argument parameters to pass to the function
1528 * @return string JS code fragment
1530 public static function event_handler($selector, $event, $function, array $arguments = null) {
1531 $selector = json_encode($selector);
1532 $output = "Y.on('$event', $function, $selector, null";
1533 if (!empty($arguments)) {
1534 $output .= ', ' . json_encode($arguments);
1536 return $output . ");\n";
1541 * Holds all the information required to render a <table> by {@see core_renderer::table()}
1544 * $t = new html_table();
1545 * ... // set various properties of the object $t as described below
1546 * echo html_writer::table($t);
1548 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
1549 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1554 * @var string value to use for the id attribute of the table
1558 * @var array attributes of HTML attributes for the <table> element
1560 public $attributes = array();
1562 * For more control over the rendering of the headers, an array of html_table_cell objects
1563 * can be passed instead of an array of strings.
1564 * @var array of headings. The n-th array item is used as a heading of the n-th column.
1567 * $t->head = array('Student', 'Grade');
1571 * @var array can be used to make a heading span multiple columns
1574 * $t->headspan = array(2,1);
1576 * In this example, {@see html_table:$data} is supposed to have three columns. For the first two columns,
1577 * the same heading is used. Therefore, {@see html_table::$head} should consist of two items.
1581 * @var array of column alignments. The value is used as CSS 'text-align' property. Therefore, possible
1582 * values are 'left', 'right', 'center' and 'justify'. Specify 'right' or 'left' from the perspective
1583 * of a left-to-right (LTR) language. For RTL, the values are flipped automatically.
1585 * Examples of usage:
1586 * $t->align = array(null, 'right');
1588 * $t->align[1] = 'right';
1593 * @var array of column sizes. The value is used as CSS 'size' property.
1595 * Examples of usage:
1596 * $t->size = array('50%', '50%');
1598 * $t->size[1] = '120px';
1602 * @var array of wrapping information. The only possible value is 'nowrap' that sets the
1603 * CSS property 'white-space' to the value 'nowrap' in the given column.
1606 * $t->wrap = array(null, 'nowrap');
1610 * @var array of arrays or html_table_row objects containing the data. Alternatively, if you have
1611 * $head specified, the string 'hr' (for horizontal ruler) can be used
1612 * instead of an array of cells data resulting in a divider rendered.
1614 * Example of usage with array of arrays:
1615 * $row1 = array('Harry Potter', '76 %');
1616 * $row2 = array('Hermione Granger', '100 %');
1617 * $t->data = array($row1, $row2);
1619 * Example with array of html_table_row objects: (used for more fine-grained control)
1620 * $cell1 = new html_table_cell();
1621 * $cell1->text = 'Harry Potter';
1622 * $cell1->colspan = 2;
1623 * $row1 = new html_table_row();
1624 * $row1->cells[] = $cell1;
1625 * $cell2 = new html_table_cell();
1626 * $cell2->text = 'Hermione Granger';
1627 * $cell3 = new html_table_cell();
1628 * $cell3->text = '100 %';
1629 * $row2 = new html_table_row();
1630 * $row2->cells = array($cell2, $cell3);
1631 * $t->data = array($row1, $row2);
1635 * @var string width of the table, percentage of the page preferred. Defaults to 80%
1636 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1638 public $width = null;
1640 * @var string alignment the whole table. Can be 'right', 'left' or 'center' (default).
1641 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1643 public $tablealign = null;
1645 * @var int padding on each cell, in pixels
1646 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1648 public $cellpadding = null;
1650 * @var int spacing between cells, in pixels
1651 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1653 public $cellspacing = null;
1655 * @var array classes to add to particular rows, space-separated string.
1656 * Classes 'r0' or 'r1' are added automatically for every odd or even row,
1657 * respectively. Class 'lastrow' is added automatically for the last row
1661 * $t->rowclasses[9] = 'tenth'
1665 * @var array classes to add to every cell in a particular column,
1666 * space-separated string. Class 'cell' is added automatically by the renderer.
1667 * Classes 'c0' or 'c1' are added automatically for every odd or even column,
1668 * respectively. Class 'lastcol' is added automatically for all last cells
1672 * $t->colclasses = array(null, 'grade');
1676 * @var string description of the contents for screen readers.
1683 public function __construct() {
1684 $this->attributes['class'] = '';
1690 * Component representing a table row.
1692 * @copyright 2009 Nicolas Connault
1693 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1696 class html_table_row {
1698 * @var string value to use for the id attribute of the row
1702 * @var array $cells Array of html_table_cell objects
1704 public $cells = array();
1706 * @var string $style value to use for the style attribute of the table row
1708 public $style = null;
1710 * @var array attributes of additional HTML attributes for the <tr> element
1712 public $attributes = array();
1716 * @param array $cells
1718 public function __construct(array $cells=null) {
1719 $this->attributes['class'] = '';
1720 $cells = (array)$cells;
1721 foreach ($cells as $cell) {
1722 if ($cell instanceof html_table_cell) {
1723 $this->cells[] = $cell;
1725 $this->cells[] = new html_table_cell($cell);
1733 * Component representing a table cell.
1735 * @copyright 2009 Nicolas Connault
1736 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1739 class html_table_cell {
1741 * @var string value to use for the id attribute of the cell
1745 * @var string $text The contents of the cell
1749 * @var string $abbr Abbreviated version of the contents of the cell
1751 public $abbr = null;
1753 * @var int $colspan Number of columns this cell should span
1755 public $colspan = null;
1757 * @var int $rowspan Number of rows this cell should span
1759 public $rowspan = null;
1761 * @var string $scope Defines a way to associate header cells and data cells in a table
1763 public $scope = null;
1765 * @var boolean $header Whether or not this cell is a header cell
1767 public $header = null;
1769 * @var string $style value to use for the style attribute of the table cell
1771 public $style = null;
1773 * @var array attributes of additional HTML attributes for the <td> element
1775 public $attributes = array();
1777 public function __construct($text = null) {
1778 $this->text = $text;
1779 $this->attributes['class'] = '';
1784 /// Complex components aggregating simpler components
1788 * Component representing a paging bar.
1790 * @copyright 2009 Nicolas Connault
1791 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1794 class paging_bar implements renderable {
1796 * @var int $maxdisplay The maximum number of pagelinks to display
1798 public $maxdisplay = 18;
1800 * @var int $totalcount post or get
1804 * @var int $page The page you are currently viewing
1808 * @var int $perpage The number of entries that should be shown per page
1812 * @var string $baseurl If this is a string then it is the url which will be appended with $pagevar, an equals sign and the page number.
1813 * If this is a moodle_url object then the pagevar param will be replaced by the page no, for each page.
1817 * @var string $pagevar This is the variable name that you use for the page number in your code (ie. 'tablepage', 'blogpage', etc)
1821 * @var string $previouslink A HTML link representing the "previous" page
1823 public $previouslink = null;
1825 * @var tring $nextlink A HTML link representing the "next" page
1827 public $nextlink = null;
1829 * @var tring $firstlink A HTML link representing the first page
1831 public $firstlink = null;
1833 * @var tring $lastlink A HTML link representing the last page
1835 public $lastlink = null;
1837 * @var array $pagelinks An array of strings. One of them is just a string: the current page
1839 public $pagelinks = array();
1842 * Constructor paging_bar with only the required params.
1844 * @param int $totalcount The total number of entries available to be paged through
1845 * @param int $page The page you are currently viewing
1846 * @param int $perpage The number of entries that should be shown per page
1847 * @param string|moodle_url $baseurl url of the current page, the $pagevar parameter is added
1848 * @param string $pagevar name of page parameter that holds the page number
1850 public function __construct($totalcount, $page, $perpage, $baseurl, $pagevar = 'page') {
1851 $this->totalcount = $totalcount;
1852 $this->page = $page;
1853 $this->perpage = $perpage;
1854 $this->baseurl = $baseurl;
1855 $this->pagevar = $pagevar;
1861 public function prepare(renderer_base $output, moodle_page $page, $target) {
1862 if (!isset($this->totalcount) || is_null($this->totalcount)) {
1863 throw new coding_exception('paging_bar requires a totalcount value.');
1865 if (!isset($this->page) || is_null($this->page)) {
1866 throw new coding_exception('paging_bar requires a page value.');
1868 if (empty($this->perpage)) {
1869 throw new coding_exception('paging_bar requires a perpage value.');
1871 if (empty($this->baseurl)) {
1872 throw new coding_exception('paging_bar requires a baseurl value.');
1875 if ($this->totalcount > $this->perpage) {
1876 $pagenum = $this->page - 1;
1878 if ($this->page > 0) {
1879 $this->previouslink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('previous'), array('class'=>'previous'));
1882 if ($this->perpage > 0) {
1883 $lastpage = ceil($this->totalcount / $this->perpage);
1888 if ($this->page > 15) {
1889 $startpage = $this->page - 10;
1891 $this->firstlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>0)), '1', array('class'=>'first'));
1896 $currpage = $startpage;
1897 $displaycount = $displaypage = 0;
1899 while ($displaycount < $this->maxdisplay and $currpage < $lastpage) {
1900 $displaypage = $currpage + 1;
1902 if ($this->page == $currpage) {
1903 $this->pagelinks[] = $displaypage;
1905 $pagelink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$currpage)), $displaypage);
1906 $this->pagelinks[] = $pagelink;
1913 if ($currpage < $lastpage) {
1914 $lastpageactual = $lastpage - 1;
1915 $this->lastlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$lastpageactual)), $lastpage, array('class'=>'last'));
1918 $pagenum = $this->page + 1;
1920 if ($pagenum != $displaypage) {
1921 $this->nextlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('next'), array('class'=>'next'));
1929 * This class represents how a block appears on a page.
1931 * During output, each block instance is asked to return a block_contents object,
1932 * those are then passed to the $OUTPUT->block function for display.
1934 * {@link $contents} should probably be generated using a moodle_block_..._renderer.
1936 * Other block-like things that need to appear on the page, for example the
1937 * add new block UI, are also represented as block_contents objects.
1939 * @copyright 2009 Tim Hunt
1940 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1943 class block_contents {
1944 /** @var int used to set $skipid. */
1945 protected static $idcounter = 1;
1947 const NOT_HIDEABLE = 0;
1952 * @var integer $skipid All the blocks (or things that look like blocks)
1953 * printed on a page are given a unique number that can be used to construct
1954 * id="" attributes. This is set automatically be the {@link prepare()} method.
1955 * Do not try to set it manually.
1960 * @var integer If this is the contents of a real block, this should be set to
1961 * the block_instance.id. Otherwise this should be set to 0.
1963 public $blockinstanceid = 0;
1966 * @var integer if this is a real block instance, and there is a corresponding
1967 * block_position.id for the block on this page, this should be set to that id.
1968 * Otherwise it should be 0.
1970 public $blockpositionid = 0;
1973 * @param array $attributes an array of attribute => value pairs that are put on the
1974 * outer div of this block. {@link $id} and {@link $classes} attributes should be set separately.
1979 * @param string $title The title of this block. If this came from user input,
1980 * it should already have had format_string() processing done on it. This will
1981 * be output inside <h2> tags. Please do not cause invalid XHTML.
1986 * @param string $content HTML for the content
1988 public $content = '';
1991 * @param array $list an alternative to $content, it you want a list of things with optional icons.
1993 public $footer = '';
1996 * Any small print that should appear under the block to explain to the
1997 * teacher about the block, for example 'This is a sticky block that was
1998 * added in the system context.'
2001 public $annotation = '';
2004 * @var integer one of the constants NOT_HIDEABLE, VISIBLE, HIDDEN. Whether
2005 * the user can toggle whether this block is visible.
2007 public $collapsible = self::NOT_HIDEABLE;
2010 * A (possibly empty) array of editing controls. Each element of this array
2011 * should be an array('url' => $url, 'icon' => $icon, 'caption' => $caption).
2012 * $icon is the icon name. Fed to $OUTPUT->pix_url.
2015 public $controls = array();
2019 * Create new instance of block content
2020 * @param array $attributes
2022 public function __construct(array $attributes=null) {
2023 $this->skipid = self::$idcounter;
2024 self::$idcounter += 1;
2028 $this->attributes = $attributes;
2030 // simple "fake" blocks used in some modules and "Add new block" block
2031 $this->attributes = array('class'=>'block');
2036 * Add html class to block
2037 * @param string $class
2040 public function add_class($class) {
2041 $this->attributes['class'] .= ' '.$class;
2047 * This class represents a target for where a block can go when it is being moved.
2049 * This needs to be rendered as a form with the given hidden from fields, and
2050 * clicking anywhere in the form should submit it. The form action should be
2053 * @copyright 2009 Tim Hunt
2054 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2057 class block_move_target {
2071 * @param string $text
2072 * @param moodle_url $url
2074 public function __construct($text, moodle_url $url) {
2075 $this->text = $text;
2083 * This class is used to represent one item within a custom menu that may or may
2084 * not have children.
2086 * @copyright 2010 Sam Hemelryk
2087 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2090 class custom_menu_item implements renderable {
2092 * The text to show for the item
2097 * The link to give the icon if it has no children
2102 * A title to apply to the item. By default the text
2107 * A sort order for the item, not necessary if you order things in the CFG var
2112 * A reference to the parent for this item or NULL if it is a top level item
2113 * @var custom_menu_item
2117 * A array in which to store children this item has.
2120 protected $children = array();
2122 * A reference to the sort var of the last child that was added
2125 protected $lastsort = 0;
2127 * Constructs the new custom menu item
2129 * @param string $text
2130 * @param moodle_url $url A moodle url to apply as the link for this item [Optional]
2131 * @param string $title A title to apply to this item [Optional]
2132 * @param int $sort A sort or to use if we need to sort differently [Optional]
2133 * @param custom_menu_item $parent A reference to the parent custom_menu_item this child
2134 * belongs to, only if the child has a parent. [Optional]
2136 public function __construct($text, moodle_url $url=null, $title=null, $sort = null, custom_menu_item $parent=null) {
2137 $this->text = $text;
2139 $this->title = $title;
2140 $this->sort = (int)$sort;
2141 $this->parent = $parent;
2145 * Adds a custom menu item as a child of this node given its properties.
2147 * @param string $text
2148 * @param moodle_url $url
2149 * @param string $title
2151 * @return custom_menu_item
2153 public function add($text, moodle_url $url=null, $title=null, $sort = null) {
2154 $key = count($this->children);
2156 $sort = $this->lastsort + 1;
2158 $this->children[$key] = new custom_menu_item($text, $url, $title, $sort, $this);
2159 $this->lastsort = (int)$sort;
2160 return $this->children[$key];
2163 * Returns the text for this item
2166 public function get_text() {
2170 * Returns the url for this item
2171 * @return moodle_url
2173 public function get_url() {
2177 * Returns the title for this item
2180 public function get_title() {
2181 return $this->title;
2184 * Sorts and returns the children for this item
2187 public function get_children() {
2189 return $this->children;
2192 * Gets the sort order for this child
2195 public function get_sort_order() {
2199 * Gets the parent this child belong to
2200 * @return custom_menu_item
2202 public function get_parent() {
2203 return $this->parent;
2206 * Sorts the children this item has
2208 public function sort() {
2209 usort($this->children, array('custom_menu','sort_custom_menu_items'));
2212 * Returns true if this item has any children
2215 public function has_children() {
2216 return (count($this->children) > 0);
2220 * Sets the text for the node
2221 * @param string $text
2223 public function set_text($text) {
2224 $this->text = (string)$text;
2228 * Sets the title for the node
2229 * @param string $title
2231 public function set_title($title) {
2232 $this->title = (string)$title;
2236 * Sets the url for the node
2237 * @param moodle_url $url
2239 public function set_url(moodle_url $url) {
2247 * This class is used to operate a custom menu that can be rendered for the page.
2248 * The custom menu is built using $CFG->custommenuitems and is a structured collection
2249 * of custom_menu_item nodes that can be rendered by the core renderer.
2251 * To configure the custom menu:
2252 * Settings: Administration > Appearance > Themes > Theme settings
2254 * @copyright 2010 Sam Hemelryk
2255 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2258 class custom_menu extends custom_menu_item {
2260 * Creates the custom menu
2261 * @param string $text Sets the text for this custom menu, never gets used and is optional
2263 public function __construct($text='base') {
2265 parent::__construct($text);
2266 if (!empty($CFG->custommenuitems)) {
2267 $this->override_children(self::convert_text_to_menu_nodes($CFG->custommenuitems));
2272 * Overrides the children of this custom menu. Useful when getting children
2273 * from $CFG->custommenuitems
2275 public function override_children(array $children) {
2276 $this->children = array();
2277 foreach ($children as $child) {
2278 if ($child instanceof custom_menu_item) {
2279 $this->children[] = $child;
2285 * Converts a string into a structured array of custom_menu_items which can
2286 * then be added to a custom menu.
2290 * The number of hyphens at the start determines the depth of the item
2292 * Example structure:
2293 * First level first item|http://www.moodle.com/
2294 * -Second level first item|http://www.moodle.com/partners/
2295 * -Second level second item|http://www.moodle.com/hq/
2296 * --Third level first item|http://www.moodle.com/jobs/
2297 * -Second level third item|http://www.moodle.com/development/
2298 * First level second item|http://www.moodle.com/feedback/
2299 * First level third item
2302 * @param string $text
2305 public static function convert_text_to_menu_nodes($text) {
2306 $lines = explode("\n", $text);
2307 $children = array();
2311 foreach ($lines as $line) {
2312 $line = trim($line);
2313 $bits = explode('|', $line ,4); // name|url|title|sort
2314 if (!array_key_exists(0, $bits) || empty($bits[0])) {
2315 // Every item must have a name to be valid
2318 $bits[0] = ltrim($bits[0],'-');
2320 if (!array_key_exists(1, $bits)) {
2321 // Set the url to null
2324 // Make sure the url is a moodle url
2325 $bits[1] = new moodle_url(trim($bits[1]));
2327 if (!array_key_exists(2, $bits)) {
2328 // Set the title to null seeing as there isn't one
2329 $bits[2] = $bits[0];
2331 // Set an incremental sort order to keep it simple.
2332 $bits[3] = $lastsort;
2333 $lastsort = $bits[3]+1;
2334 if (preg_match('/^(\-*)/', $line, $match) && $lastchild != null && $lastdepth !== null) {
2335 $depth = strlen($match[1]);
2336 if ($depth < $lastdepth) {
2337 if ($lastdepth > 1) {
2338 $depth = $lastdepth - 1;
2339 $lastchild = $lastchild->get_parent()->get_parent()->add($bits[0], $bits[1], $bits[2], $bits[3]);
2342 $lastchild = new custom_menu_item($bits[0], $bits[1], $bits[2], $bits[3]);
2343 $children[] = $lastchild;
2345 } else if ($depth > $lastdepth) {
2346 $depth = $lastdepth + 1;
2347 $lastchild = $lastchild->add($bits[0], $bits[1], $bits[2], $bits[3]);
2350 $lastchild = new custom_menu_item($bits[0], $bits[1], $bits[2], $bits[3]);
2351 $children[] = $lastchild;
2353 $lastchild = $lastchild->get_parent()->add($bits[0], $bits[1], $bits[2], $bits[3]);
2358 $lastchild = new custom_menu_item($bits[0], $bits[1], $bits[2], $bits[3]);
2359 $children[] = $lastchild;
2361 $lastdepth = $depth;
2367 * Sorts two custom menu items
2369 * This function is designed to be used with the usort method
2370 * usort($this->children, array('custom_menu','sort_custom_menu_items'));
2372 * @param custom_menu_item $itema
2373 * @param custom_menu_item $itemb
2376 public static function sort_custom_menu_items(custom_menu_item $itema, custom_menu_item $itemb) {
2377 $itema = $itema->get_sort_order();
2378 $itemb = $itemb->get_sort_order();
2379 if ($itema == $itemb) {
2382 return ($itema > $itemb) ? +1 : -1;