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
25 * @copyright 2009 Tim Hunt
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 * Interface marking other classes as suitable for renderer_base::render()
32 * @author 2010 Petr Skoda (skodak) info@skodak.org
34 interface renderable {
35 // intentionally empty
40 * Data structure representing a user picture.
42 * @copyright 2009 Nicolas Connault, 2010 Petr Skoda
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
46 class user_picture implements renderable {
48 * List of mandatory fields in user record here.
51 const FIELDS = 'id,picture,firstname,lastname,imagealt';
54 * @var object $user A user object with at least fields id, picture, imagealt, firstname and lastname set.
58 * @var int $courseid The course id. Used when constructing the link to the user's profile,
59 * page course id used if not specified.
63 * @var bool $link add course profile link to image
67 * @var int $size Size in pixels. Special values are (true/1 = 100px) and (false/0 = 35px) for backward compatibility
71 * @var boolean $alttext add non-blank alt-text to the image.
72 * Default true, set to false when image alt just duplicates text in screenreaders.
74 public $alttext = true;
76 * @var boolean $popup Whether or not to open the link in a popup window.
78 public $popup = false;
80 * @var string Image class attribute
82 public $class = 'userpicture';
85 * User picture constructor.
87 * @param object $user user record with at least id, picture, imagealt, firstname and lastname set.
88 * @param array $options such as link, size, link, ...
90 public function __construct(stdClass $user) {
93 static $fields = null;
94 if (is_null($fields)) {
95 $fields = explode(',', self::FIELDS);
98 if (empty($user->id)) {
99 throw new coding_exception('User id is required when printing user avatar image.');
102 // only touch the DB if we are missing data and complain loudly...
104 foreach ($fields as $field) {
105 if (!array_key_exists($field, $user)) {
107 debugging('Missing '.$field.' property in $user object, this is a performance problem that needs to be fixed by a developer. '
108 .'Please use user_picture::fields() to get the full list of required fields.', DEBUG_DEVELOPER);
114 $this->user = $DB->get_record('user', array('id'=>$user->id), self::FIELDS, MUST_EXIST);
116 $this->user = clone($user);
121 * Returns a list of required user fields, usefull when fetching required user info from db.
123 * In some cases we have to fetch the user data together with some other information,
124 * the idalias is useful there because the id would otherwise override the main
125 * id of the result record. Please note it has to be converted back to id before rendering.
127 * @param string $tableprefix name of database table prefix in query
128 * @param string $idalias alias of id field
131 public static function fields($tableprefix = '', $idalias = '') {
132 if ($tableprefix === '' and $idalias === '') {
135 $fields = explode(',', self::FIELDS);
136 foreach ($fields as $key=>$field) {
137 if ($field === 'id' and $idalias !== '') {
138 $field = "$field AS $idalias";
140 $fields[$key] = "$tableprefix.$field";
142 return implode(',', $fields);
148 * Data structure representing a help icon.
150 * @copyright 2009 Nicolas Connault, 2010 Petr Skoda
151 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
154 class old_help_icon implements renderable {
156 * @var string $helpidentifier lang pack identifier (should inlcude the "_hlp" suffix)
158 public $helpidentifier;
160 * @var string $title A descriptive text for title tooltip
162 public $title = null;
164 * @var string $component Component name, the same as in get_string()
166 public $component = 'moodle';
168 * @var string $linktext Extra descriptive text next to the icon
170 public $linktext = null;
173 * Constructor: sets up the other components in case they are needed
174 * @param string $helpidentifier The keyword that defines a help page
175 * @param string $title A descriptive text for accesibility only
176 * @param string $component
177 * @param bool $linktext add extra text to icon
180 public function __construct($helpidentifier, $title, $component = 'moodle') {
182 throw new coding_exception('A help_icon object requires a $text parameter');
184 if (empty($helpidentifier)) {
185 throw new coding_exception('A help_icon object requires a $helpidentifier parameter');
188 $this->helpidentifier = $helpidentifier;
189 $this->title = $title;
190 $this->component = $component;
196 * Data structure representing an icon.
198 * @copyright 2010 Petr Skoda
199 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
202 class pix_icon implements renderable {
205 var $attributes = array();
209 * @param string $pix short icon name
210 * @param string $component component name
211 * @param array $attributes html attributes
213 public function __construct($pix, $alt, $component='moodle', array $attributes = null) {
215 $this->component = $component;
216 $this->attributes = (array)$attributes;
218 $this->attributes['alt'] = $alt;
219 if (empty($this->attributes['class'])) {
220 $this->attributes['class'] = 'smallicon';
222 if (!isset($this->attributes['title'])) {
223 $this->attributes['title'] = $this->attributes['alt'];
230 * Data structure representing a simple form with only one button.
232 * @copyright 2009 Petr Skoda
233 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
236 class single_button implements renderable {
249 * @var string post or get
251 var $method = 'post';
256 var $class = 'singlebutton';
258 * True if button disabled, false if normal
261 var $disabled = false;
273 * List of attached actions
274 * @var array of component_action
276 var $actions = array();
280 * @param string|moodle_url $url
281 * @param string $label button text
282 * @param string $method get or post submit method
284 public function __construct(moodle_url $url, $label, $method='post') {
285 $this->url = clone($url);
286 $this->label = $label;
287 $this->method = $method;
291 * Shortcut for adding a JS confirm dialog when the button is clicked.
292 * The message must be a yes/no question.
293 * @param string $message The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
296 public function add_confirm_action($confirmmessage) {
297 $this->add_action(new component_action('click', 'M.util.show_confirm_dialog', array('message' => $confirmmessage)));
301 * Add action to the button.
302 * @param component_action $action
305 public function add_action(component_action $action) {
306 $this->actions[] = $action;
312 * Simple form with just one select field that gets submitted automatically.
313 * If JS not enabled small go button is printed too.
315 * @copyright 2009 Petr Skoda
316 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
319 class single_select implements renderable {
321 * Target url - includes hidden fields
326 * Name of the select element.
331 * @var array $options associative array value=>label ex.:
332 * array(1=>'One, 2=>Two)
333 * it is also possible to specify optgroup as complex label array ex.:
334 * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
335 * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
349 * Extra select field attributes
352 var $attributes = array();
360 * @var string post or get
367 var $class = 'singleselect';
369 * True if button disabled, false if normal
372 var $disabled = false;
384 * List of attached actions
385 * @var array of component_action
387 var $helpicon = null;
390 * @param moodle_url $url form action target, includes hidden fields
391 * @param string $name name of selection field - the changing parameter in url
392 * @param array $options list of options
393 * @param string $selected selected element
394 * @param array $nothing
395 * @param string $formid
397 public function __construct(moodle_url $url, $name, array $options, $selected='', $nothing=array(''=>'choosedots'), $formid=null) {
400 $this->options = $options;
401 $this->selected = $selected;
402 $this->nothing = $nothing;
403 $this->formid = $formid;
407 * Shortcut for adding a JS confirm dialog when the button is clicked.
408 * The message must be a yes/no question.
409 * @param string $message The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
412 public function add_confirm_action($confirmmessage) {
413 $this->add_action(new component_action('submit', 'M.util.show_confirm_dialog', array('message' => $confirmmessage)));
417 * Add action to the button.
418 * @param component_action $action
421 public function add_action(component_action $action) {
422 $this->actions[] = $action;
426 * Constructor: sets up the other components in case they are needed
427 * @param string $page The keyword that defines a help page
428 * @param string $title A descriptive text for accesibility only
429 * @param string $component
430 * @param bool $linktext add extra text to icon
433 public function set_old_help_icon($helppage, $title, $component = 'moodle') {
434 $this->helpicon = new old_help_icon($helppage, $title, $component);
439 * @param string $label
442 public function set_label($label) {
443 $this->label = $label;
449 * Simple URL selection widget description.
450 * @copyright 2009 Petr Skoda
451 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
454 class url_select implements renderable {
456 * @var array $urls associative array value=>label ex.:
457 * array(1=>'One, 2=>Two)
458 * it is also possible to specify optgroup as complex label array ex.:
459 * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
460 * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
474 * Extra select field attributes
477 var $attributes = array();
487 var $class = 'urlselect';
489 * True if button disabled, false if normal
492 var $disabled = false;
504 * List of attached actions
505 * @var array of component_action
507 var $helpicon = null;
510 * @param array $urls list of options
511 * @param string $selected selected element
512 * @param array $nothing
513 * @param string $formid
515 public function __construct(array $urls, $selected='', $nothing=array(''=>'choosedots'), $formid=null) {
517 $this->selected = $selected;
518 $this->nothing = $nothing;
519 $this->formid = $formid;
523 * Constructor: sets up the other components in case they are needed
524 * @param string $page The keyword that defines a help page
525 * @param string $title A descriptive text for accesibility only
526 * @param string $component
527 * @param bool $linktext add extra text to icon
530 public function set_old_help_icon($helppage, $title, $component = 'moodle') {
531 $this->helpicon = new old_help_icon($helppage, $title, $component);
536 * @param string $label
539 public function set_label($label) {
540 $this->label = $label;
546 * Data structure describing html link with special action attached.
547 * @copyright 2010 Petr Skoda
548 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
551 class action_link implements renderable {
559 * @var string HTML fragment
568 * List of actions attached to link
569 * @var array of component_action
575 * @param string|moodle_url $url
576 * @param string $text HTML fragment
577 * @param component_action $action
578 * @param array $attributes associative array of html link attributes + disabled
580 public function __construct(moodle_url $url, $text, component_action $action=null, array $attributes=null) {
581 $this->url = clone($url);
583 $this->attributes = (array)$attributes;
585 $this->add_action($action);
590 * Add action to the link.
591 * @param component_action $action
594 public function add_action(component_action $action) {
595 $this->actions[] = $action;
598 public function add_class($class) {
599 if (empty($this->attributes['class'])) {
600 $this->attributes['class'] = $class;
602 $this->attributes['class'] .= ' ' . $class;
607 // ==== HTML writer and helper classes, will be probably moved elsewhere ======
610 * Simple html output class
611 * @copyright 2009 Tim Hunt, 2010 Petr Skoda
615 * Outputs a tag with attributes and contents
616 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
617 * @param string $contents What goes between the opening and closing tags
618 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
619 * @return string HTML fragment
621 public static function tag($tagname, $contents, array $attributes = null) {
622 return self::start_tag($tagname, $attributes) . $contents . self::end_tag($tagname);
626 * Outputs an opening tag with attributes
627 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
628 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
629 * @return string HTML fragment
631 public static function start_tag($tagname, array $attributes = null) {
632 return '<' . $tagname . self::attributes($attributes) . '>';
636 * Outputs a closing tag
637 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
638 * @return string HTML fragment
640 public static function end_tag($tagname) {
641 return '</' . $tagname . '>';
645 * Outputs an empty tag with attributes
646 * @param string $tagname The name of tag ('input', 'img', 'br' etc.)
647 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
648 * @return string HTML fragment
650 public static function empty_tag($tagname, array $attributes = null) {
651 return '<' . $tagname . self::attributes($attributes) . ' />';
655 * Outputs a HTML attribute and value
656 * @param string $name The name of the attribute ('src', 'href', 'class' etc.)
657 * @param string $value The value of the attribute. The value will be escaped with {@link s()}
658 * @return string HTML fragment
660 public static function attribute($name, $value) {
661 if (is_array($value)) {
662 debugging("Passed an array for the HTML attribute $name", DEBUG_DEVELOPER);
664 if ($value instanceof moodle_url) {
665 return ' ' . $name . '="' . $value->out() . '"';
668 // special case, we do not want these in output
669 if ($value === null) {
673 // no sloppy trimming here!
674 return ' ' . $name . '="' . s($value) . '"';
678 * Outputs a list of HTML attributes and values
679 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
680 * The values will be escaped with {@link s()}
681 * @return string HTML fragment
683 public static function attributes(array $attributes = null) {
684 $attributes = (array)$attributes;
686 foreach ($attributes as $name => $value) {
687 $output .= self::attribute($name, $value);
693 * Generates random html element id.
694 * @param string $base
697 public static function random_id($base='random') {
698 return uniqid($base);
702 * Generates a simple html link
703 * @param string|moodle_url $url
704 * @param string $text link txt
705 * @param array $attributes extra html attributes
706 * @return string HTML fragment
708 public static function link($url, $text, array $attributes = null) {
709 $attributes = (array)$attributes;
710 $attributes['href'] = $url;
711 return self::tag('a', $text, $attributes);
715 * generates a simple checkbox with optional label
716 * @param string $name
717 * @param string $value
718 * @param bool $checked
719 * @param string $label
720 * @param array $attributes
721 * @return string html fragment
723 public static function checkbox($name, $value, $checked = true, $label = '', array $attributes = null) {
724 $attributes = (array)$attributes;
727 if ($label !== '' and !is_null($label)) {
728 if (empty($attributes['id'])) {
729 $attributes['id'] = self::random_id('checkbox_');
732 $attributes['type'] = 'checkbox';
733 $attributes['value'] = $value;
734 $attributes['name'] = $name;
735 $attributes['checked'] = $checked ? 'selected' : null;
737 $output .= self::empty_tag('input', $attributes);
739 if ($label !== '' and !is_null($label)) {
740 $output .= self::tag('label', $label, array('for'=>$attributes['id']));
747 * Generates a simple select yes/no form field
748 * @param string $name name of select element
749 * @param bool $selected
750 * @param array $attributes - html select element attributes
751 * @return string HRML fragment
753 public static function select_yes_no($name, $selected=true, array $attributes = null) {
754 $options = array('1'=>get_string('yes'), '0'=>get_string('no'));
755 return self::select($options, $name, $selected, null, $attributes);
759 * Generates a simple select form field
760 * @param array $options associative array value=>label ex.:
761 * array(1=>'One, 2=>Two)
762 * it is also possible to specify optgroup as complex label array ex.:
763 * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
764 * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
765 * @param string $name name of select element
766 * @param string|array $selected value or arary of values depending on multiple attribute
767 * @param array|bool $nothing, add nothing selected option, or false of not added
768 * @param array $attributes - html select element attributes
769 * @return string HTML fragment
771 public static function select(array $options, $name, $selected = '', $nothing = array(''=>'choosedots'), array $attributes = null) {
772 $attributes = (array)$attributes;
773 if (is_array($nothing)) {
774 foreach ($nothing as $k=>$v) {
775 if ($v === 'choose' or $v === 'choosedots') {
776 $nothing[$k] = get_string('choosedots');
779 $options = $nothing + $options; // keep keys, do not override
781 } else if (is_string($nothing) and $nothing !== '') {
783 $options = array(''=>$nothing) + $options;
786 // we may accept more values if multiple attribute specified
787 $selected = (array)$selected;
788 foreach ($selected as $k=>$v) {
789 $selected[$k] = (string)$v;
792 if (!isset($attributes['id'])) {
794 // name may contaion [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading
795 $id = str_replace('[', '', $id);
796 $id = str_replace(']', '', $id);
797 $attributes['id'] = $id;
800 if (!isset($attributes['class'])) {
801 $class = 'menu'.$name;
802 // name may contaion [], which would make an invalid class. e.g. numeric question type editing form, assignment quickgrading
803 $class = str_replace('[', '', $class);
804 $class = str_replace(']', '', $class);
805 $attributes['class'] = $class;
807 $attributes['class'] = 'select ' . $attributes['class']; /// Add 'select' selector always
809 $attributes['name'] = $name;
812 foreach ($options as $value=>$label) {
813 if (is_array($label)) {
814 // ignore key, it just has to be unique
815 $output .= self::select_optgroup(key($label), current($label), $selected);
817 $output .= self::select_option($label, $value, $selected);
820 return self::tag('select', $output, $attributes);
823 private static function select_option($label, $value, array $selected) {
824 $attributes = array();
825 $value = (string)$value;
826 if (in_array($value, $selected, true)) {
827 $attributes['selected'] = 'selected';
829 $attributes['value'] = $value;
830 return self::tag('option', $label, $attributes);
833 private static function select_optgroup($groupname, $options, array $selected) {
834 if (empty($options)) {
837 $attributes = array('label'=>$groupname);
839 foreach ($options as $value=>$label) {
840 $output .= self::select_option($label, $value, $selected);
842 return self::tag('optgroup', $output, $attributes);
846 * This is a shortcut for making an hour selector menu.
847 * @param string $type The type of selector (years, months, days, hours, minutes)
848 * @param string $name fieldname
849 * @param int $currenttime A default timestamp in GMT
850 * @param int $step minute spacing
851 * @param array $attributes - html select element attributes
852 * @return HTML fragment
854 public static function select_time($type, $name, $currenttime=0, $step=5, array $attributes=null) {
856 $currenttime = time();
858 $currentdate = usergetdate($currenttime);
859 $userdatetype = $type;
860 $timeunits = array();
864 for ($i=1970; $i<=2020; $i++) {
867 $userdatetype = 'year';
870 for ($i=1; $i<=12; $i++) {
871 $timeunits[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
873 $userdatetype = 'month';
874 $currentdate['month'] = $currentdate['mon'];
877 for ($i=1; $i<=31; $i++) {
880 $userdatetype = 'mday';
883 for ($i=0; $i<=23; $i++) {
884 $timeunits[$i] = sprintf("%02d",$i);
889 $currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step;
892 for ($i=0; $i<=59; $i+=$step) {
893 $timeunits[$i] = sprintf("%02d",$i);
897 throw new coding_exception("Time type $type is not supported by html_writer::select_time().");
900 if (empty($attributes['id'])) {
901 $attributes['id'] = self::random_id('ts_');
903 $timerselector = self::select($timeunits, $name, $currentdate[$userdatetype], null, array('id'=>$attributes['id']));
904 $label = self::tag('label', get_string(substr($type, 0, -1), 'form'), array('for'=>$attributes['id'], 'class'=>'accesshide'));
906 return $label.$timerselector;
910 * Shortcut for quick making of lists
911 * @param array $items
912 * @param string $tag ul or ol
913 * @param array $attributes
916 public static function alist(array $items, array $attributes = null, $tag = 'ul') {
917 //note: 'list' is a reserved keyword ;-)
921 foreach ($items as $item) {
922 $output .= html_writer::start_tag('li') . "\n";
923 $output .= $item . "\n";
924 $output .= html_writer::end_tag('li') . "\n";
927 return html_writer::tag($tag, $output, $attributes);
931 * Returns hidden input fields created from url parameters.
932 * @param moodle_url $url
933 * @param array $exclude list of excluded parameters
934 * @return string HTML fragment
936 public static function input_hidden_params(moodle_url $url, array $exclude = null) {
937 $exclude = (array)$exclude;
938 $params = $url->params();
939 foreach ($exclude as $key) {
940 unset($params[$key]);
944 foreach ($params as $key => $value) {
945 $attributes = array('type'=>'hidden', 'name'=>$key, 'value'=>$value);
946 $output .= self::empty_tag('input', $attributes)."\n";
952 * Generate a script tag containing the the specified code.
954 * @param string $js the JavaScript code
955 * @param moodle_url|string optional url of the external script, $code ignored if specified
956 * @return string HTML, the code wrapped in <script> tags.
958 public static function script($jscode, $url=null) {
960 $attributes = array('type'=>'text/javascript');
961 return self::tag('script', "\n//<![CDATA[\n$jscode\n//]]>\n", $attributes) . "\n";
964 $attributes = array('type'=>'text/javascript', 'src'=>$url);
965 return self::tag('script', '', $attributes) . "\n";
975 * This method may modify the passed instance by adding some default properties if they are not set yet.
976 * If this is not what you want, you should make a full clone of your data before passing them to this
977 * method. In most cases this is not an issue at all so we do not clone by default for performance
978 * and memory consumption reasons.
980 * @param html_table $table data to be rendered
981 * @return string HTML code
983 public static function table(html_table $table) {
984 // prepare table data and populate missing properties with reasonable defaults
985 if (!empty($table->align)) {
986 foreach ($table->align as $key => $aa) {
988 $table->align[$key] = 'text-align:'. fix_align_rtl($aa) .';'; // Fix for RTL languages
990 $table->align[$key] = null;
994 if (!empty($table->size)) {
995 foreach ($table->size as $key => $ss) {
997 $table->size[$key] = 'width:'. $ss .';';
999 $table->size[$key] = null;
1003 if (!empty($table->wrap)) {
1004 foreach ($table->wrap as $key => $ww) {
1006 $table->wrap[$key] = 'white-space:nowrap;';
1008 $table->wrap[$key] = '';
1012 if (!empty($table->head)) {
1013 foreach ($table->head as $key => $val) {
1014 if (!isset($table->align[$key])) {
1015 $table->align[$key] = null;
1017 if (!isset($table->size[$key])) {
1018 $table->size[$key] = null;
1020 if (!isset($table->wrap[$key])) {
1021 $table->wrap[$key] = null;
1026 if (empty($table->attributes['class'])) {
1027 $table->attributes['class'] = 'generaltable';
1029 if (!empty($table->tablealign)) {
1030 $table->attributes['class'] .= ' boxalign' . $table->tablealign;
1033 // explicitly assigned properties override those defined via $table->attributes
1034 $table->attributes['class'] = trim($table->attributes['class']);
1035 $attributes = array_merge($table->attributes, array(
1037 'width' => $table->width,
1038 'summary' => $table->summary,
1039 'cellpadding' => $table->cellpadding,
1040 'cellspacing' => $table->cellspacing,
1042 $output = html_writer::start_tag('table', $attributes) . "\n";
1046 if (!empty($table->head)) {
1047 $countcols = count($table->head);
1048 $output .= html_writer::start_tag('thead', array()) . "\n";
1049 $output .= html_writer::start_tag('tr', array()) . "\n";
1050 $keys = array_keys($table->head);
1051 $lastkey = end($keys);
1053 foreach ($table->head as $key => $heading) {
1054 // Convert plain string headings into html_table_cell objects
1055 if (!($heading instanceof html_table_cell)) {
1056 $headingtext = $heading;
1057 $heading = new html_table_cell();
1058 $heading->text = $headingtext;
1059 $heading->header = true;
1062 if ($heading->header !== false) {
1063 $heading->header = true;
1066 if ($heading->header && empty($heading->scope)) {
1067 $heading->scope = 'col';
1070 $heading->attributes['class'] .= ' header c' . $key;
1071 if (isset($table->headspan[$key]) && $table->headspan[$key] > 1) {
1072 $heading->colspan = $table->headspan[$key];
1073 $countcols += $table->headspan[$key] - 1;
1076 if ($key == $lastkey) {
1077 $heading->attributes['class'] .= ' lastcol';
1079 if (isset($table->colclasses[$key])) {
1080 $heading->attributes['class'] .= ' ' . $table->colclasses[$key];
1082 $heading->attributes['class'] = trim($heading->attributes['class']);
1083 $attributes = array_merge($heading->attributes, array(
1084 'style' => $table->align[$key] . $table->size[$key] . $heading->style,
1085 'scope' => $heading->scope,
1086 'colspan' => $heading->colspan,
1090 if ($heading->header === true) {
1093 $output .= html_writer::tag($tagtype, $heading->text, $attributes) . "\n";
1095 $output .= html_writer::end_tag('tr') . "\n";
1096 $output .= html_writer::end_tag('thead') . "\n";
1098 if (empty($table->data)) {
1099 // For valid XHTML strict every table must contain either a valid tr
1100 // or a valid tbody... both of which must contain a valid td
1101 $output .= html_writer::start_tag('tbody', array('class' => 'empty'));
1102 $output .= html_writer::tag('tr', html_writer::tag('td', '', array('colspan'=>count($table->head))));
1103 $output .= html_writer::end_tag('tbody');
1107 if (!empty($table->data)) {
1109 $keys = array_keys($table->data);
1110 $lastrowkey = end($keys);
1111 $output .= html_writer::start_tag('tbody', array());
1113 foreach ($table->data as $key => $row) {
1114 if (($row === 'hr') && ($countcols)) {
1115 $output .= html_writer::tag('td', html_writer::tag('div', '', array('class' => 'tabledivider')), array('colspan' => $countcols));
1117 // Convert array rows to html_table_rows and cell strings to html_table_cell objects
1118 if (!($row instanceof html_table_row)) {
1119 $newrow = new html_table_row();
1121 foreach ($row as $item) {
1122 $cell = new html_table_cell();
1123 $cell->text = $item;
1124 $newrow->cells[] = $cell;
1129 $oddeven = $oddeven ? 0 : 1;
1130 if (isset($table->rowclasses[$key])) {
1131 $row->attributes['class'] .= ' ' . $table->rowclasses[$key];
1134 $row->attributes['class'] .= ' r' . $oddeven;
1135 if ($key == $lastrowkey) {
1136 $row->attributes['class'] .= ' lastrow';
1139 $output .= html_writer::start_tag('tr', array('class' => trim($row->attributes['class']), 'style' => $row->style, 'id' => $row->id)) . "\n";
1140 $keys2 = array_keys($row->cells);
1141 $lastkey = end($keys2);
1143 foreach ($row->cells as $key => $cell) {
1144 if (!($cell instanceof html_table_cell)) {
1145 $mycell = new html_table_cell();
1146 $mycell->text = $cell;
1150 if (($cell->header === true) && empty($cell->scope)) {
1151 $cell->scope = 'row';
1154 if (isset($table->colclasses[$key])) {
1155 $cell->attributes['class'] .= ' ' . $table->colclasses[$key];
1158 $cell->attributes['class'] .= ' cell c' . $key;
1159 if ($key == $lastkey) {
1160 $cell->attributes['class'] .= ' lastcol';
1163 $tdstyle .= isset($table->align[$key]) ? $table->align[$key] : '';
1164 $tdstyle .= isset($table->size[$key]) ? $table->size[$key] : '';
1165 $tdstyle .= isset($table->wrap[$key]) ? $table->wrap[$key] : '';
1166 $cell->attributes['class'] = trim($cell->attributes['class']);
1167 $tdattributes = array_merge($cell->attributes, array(
1168 'style' => $tdstyle . $cell->style,
1169 'colspan' => $cell->colspan,
1170 'rowspan' => $cell->rowspan,
1172 'abbr' => $cell->abbr,
1173 'scope' => $cell->scope,
1176 if ($cell->header === true) {
1179 $output .= html_writer::tag($tagtype, $cell->text, $tdattributes) . "\n";
1182 $output .= html_writer::end_tag('tr') . "\n";
1184 $output .= html_writer::end_tag('tbody') . "\n";
1186 $output .= html_writer::end_tag('table') . "\n";
1193 // ==== JS writer and helper classes, will be probably moved elsewhere ======
1196 * Simple javascript output class
1197 * @copyright 2010 Petr Skoda
1201 * Returns javascript code calling the function
1202 * @param string $function function name, can be complex lin Y.Event.purgeElement
1203 * @param array $arguments parameters
1204 * @param int $delay execution delay in seconds
1205 * @return string JS code fragment
1207 public function function_call($function, array $arguments = null, $delay=0) {
1209 $arguments = array_map('json_encode', $arguments);
1210 $arguments = implode(', ', $arguments);
1214 $js = "$function($arguments);";
1217 $delay = $delay * 1000; // in miliseconds
1218 $js = "setTimeout(function() { $js }, $delay);";
1224 * Special function which adds Y as first argument of fucntion call.
1225 * @param string $function
1226 * @param array $extraarguments
1229 public function function_call_with_Y($function, array $extraarguments = null) {
1230 if ($extraarguments) {
1231 $extraarguments = array_map('json_encode', $extraarguments);
1232 $arguments = 'Y, ' . implode(', ', $extraarguments);
1236 return "$function($arguments);\n";
1240 * Returns JavaScript code to initialise a new object
1241 * @param string|null $var If it is null then no var is assigned the new object
1242 * @param string $class
1243 * @param array $arguments
1244 * @param array $requirements
1248 public function object_init($var, $class, array $arguments = null, array $requirements = null, $delay=0) {
1249 if (is_array($arguments)) {
1250 $arguments = array_map('json_encode', $arguments);
1251 $arguments = implode(', ', $arguments);
1254 if ($var === null) {
1255 $js = "new $class(Y, $arguments);";
1256 } else if (strpos($var, '.')!==false) {
1257 $js = "$var = new $class(Y, $arguments);";
1259 $js = "var $var = new $class(Y, $arguments);";
1263 $delay = $delay * 1000; // in miliseconds
1264 $js = "setTimeout(function() { $js }, $delay);";
1267 if (count($requirements) > 0) {
1268 $requirements = implode("', '", $requirements);
1269 $js = "Y.use('$requirements', function(Y){ $js });";
1275 * Returns code setting value to variable
1276 * @param string $name
1277 * @param mixed $value json serialised value
1278 * @param bool $usevar add var definition, ignored for nested properties
1279 * @return string JS code fragment
1281 public function set_variable($name, $value, $usevar=true) {
1285 if (strpos($name, '.')) {
1292 $output .= "$name = ".json_encode($value).";";
1298 * Writes event handler attaching code
1299 * @param mixed $selector standard YUI selector for elemnts, may be array or string, element id is in the form "#idvalue"
1300 * @param string $event A valid DOM event (click, mousedown, change etc.)
1301 * @param string $function The name of the function to call
1302 * @param array $arguments An optional array of argument parameters to pass to the function
1303 * @return string JS code fragment
1305 public function event_handler($selector, $event, $function, array $arguments = null) {
1306 $selector = json_encode($selector);
1307 $output = "Y.on('$event', $function, $selector, null";
1308 if (!empty($arguments)) {
1309 $output .= ', ' . json_encode($arguments);
1311 return $output . ");\n";
1316 * Holds all the information required to render a <table> by {@see core_renderer::table()}
1319 * $t = new html_table();
1320 * ... // set various properties of the object $t as described below
1321 * echo html_writer::table($t);
1323 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
1324 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1329 * @var string value to use for the id attribute of the table
1333 * @var array attributes of HTML attributes for the <table> element
1335 public $attributes = array();
1337 * For more control over the rendering of the headers, an array of html_table_cell objects
1338 * can be passed instead of an array of strings.
1339 * @var array of headings. The n-th array item is used as a heading of the n-th column.
1342 * $t->head = array('Student', 'Grade');
1346 * @var array can be used to make a heading span multiple columns
1349 * $t->headspan = array(2,1);
1351 * In this example, {@see html_table:$data} is supposed to have three columns. For the first two columns,
1352 * the same heading is used. Therefore, {@see html_table::$head} should consist of two items.
1356 * @var array of column alignments. The value is used as CSS 'text-align' property. Therefore, possible
1357 * values are 'left', 'right', 'center' and 'justify'. Specify 'right' or 'left' from the perspective
1358 * of a left-to-right (LTR) language. For RTL, the values are flipped automatically.
1360 * Examples of usage:
1361 * $t->align = array(null, 'right');
1363 * $t->align[1] = 'right';
1368 * @var array of column sizes. The value is used as CSS 'size' property.
1370 * Examples of usage:
1371 * $t->size = array('50%', '50%');
1373 * $t->size[1] = '120px';
1377 * @var array of wrapping information. The only possible value is 'nowrap' that sets the
1378 * CSS property 'white-space' to the value 'nowrap' in the given column.
1381 * $t->wrap = array(null, 'nowrap');
1385 * @var array of arrays or html_table_row objects containing the data. Alternatively, if you have
1386 * $head specified, the string 'hr' (for horizontal ruler) can be used
1387 * instead of an array of cells data resulting in a divider rendered.
1389 * Example of usage with array of arrays:
1390 * $row1 = array('Harry Potter', '76 %');
1391 * $row2 = array('Hermione Granger', '100 %');
1392 * $t->data = array($row1, $row2);
1394 * Example with array of html_table_row objects: (used for more fine-grained control)
1395 * $cell1 = new html_table_cell();
1396 * $cell1->text = 'Harry Potter';
1397 * $cell1->colspan = 2;
1398 * $row1 = new html_table_row();
1399 * $row1->cells[] = $cell1;
1400 * $cell2 = new html_table_cell();
1401 * $cell2->text = 'Hermione Granger';
1402 * $cell3 = new html_table_cell();
1403 * $cell3->text = '100 %';
1404 * $row2 = new html_table_row();
1405 * $row2->cells = array($cell2, $cell3);
1406 * $t->data = array($row1, $row2);
1410 * @var string width of the table, percentage of the page preferred. Defaults to 80%
1411 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1413 public $width = null;
1415 * @var string alignment the whole table. Can be 'right', 'left' or 'center' (default).
1416 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1418 public $tablealign = null;
1420 * @var int padding on each cell, in pixels
1421 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1423 public $cellpadding = null;
1425 * @var int spacing between cells, in pixels
1426 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1428 public $cellspacing = null;
1430 * @var array classes to add to particular rows, space-separated string.
1431 * Classes 'r0' or 'r1' are added automatically for every odd or even row,
1432 * respectively. Class 'lastrow' is added automatically for the last row
1436 * $t->rowclasses[9] = 'tenth'
1440 * @var array classes to add to every cell in a particular column,
1441 * space-separated string. Class 'cell' is added automatically by the renderer.
1442 * Classes 'c0' or 'c1' are added automatically for every odd or even column,
1443 * respectively. Class 'lastcol' is added automatically for all last cells
1447 * $t->colclasses = array(null, 'grade');
1451 * @var string description of the contents for screen readers.
1458 public function __construct() {
1459 $this->attributes['class'] = '';
1465 * Component representing a table row.
1467 * @copyright 2009 Nicolas Connault
1468 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1471 class html_table_row {
1473 * @var string value to use for the id attribute of the row
1477 * @var array $cells Array of html_table_cell objects
1479 public $cells = array();
1481 * @var string $style value to use for the style attribute of the table row
1483 public $style = null;
1485 * @var array attributes of additional HTML attributes for the <tr> element
1487 public $attributes = array();
1491 * @param array $cells
1493 public function __construct(array $cells=null) {
1494 $this->attributes['class'] = '';
1495 $cells = (array)$cells;
1496 foreach ($cells as $cell) {
1497 if ($cell instanceof html_table_cell) {
1498 $this->cells[] = $cell;
1500 $this->cells[] = new html_table_cell($cell);
1508 * Component representing a table cell.
1510 * @copyright 2009 Nicolas Connault
1511 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1514 class html_table_cell {
1516 * @var string value to use for the id attribute of the cell
1520 * @var string $text The contents of the cell
1524 * @var string $abbr Abbreviated version of the contents of the cell
1526 public $abbr = null;
1528 * @var int $colspan Number of columns this cell should span
1530 public $colspan = null;
1532 * @var int $rowspan Number of rows this cell should span
1534 public $rowspan = null;
1536 * @var string $scope Defines a way to associate header cells and data cells in a table
1538 public $scope = null;
1540 * @var boolean $header Whether or not this cell is a header cell
1542 public $header = null;
1544 * @var string $style value to use for the style attribute of the table cell
1546 public $style = null;
1548 * @var array attributes of additional HTML attributes for the <tr> element
1550 public $attributes = array();
1552 public function __construct($text = null) {
1553 $this->text = $text;
1554 $this->attributes['class'] = '';
1559 /// Complex components aggregating simpler components
1563 * Component representing a paging bar.
1565 * @copyright 2009 Nicolas Connault
1566 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1569 class paging_bar implements renderable {
1571 * @var int $maxdisplay The maximum number of pagelinks to display
1573 public $maxdisplay = 18;
1575 * @var int $totalcount post or get
1579 * @var int $page The page you are currently viewing
1583 * @var int $perpage The number of entries that should be shown per page
1587 * @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.
1588 * If this is a moodle_url object then the pagevar param will be replaced by the page no, for each page.
1592 * @var string $pagevar This is the variable name that you use for the page number in your code (ie. 'tablepage', 'blogpage', etc)
1596 * @var string $previouslink A HTML link representing the "previous" page
1598 public $previouslink = null;
1600 * @var tring $nextlink A HTML link representing the "next" page
1602 public $nextlink = null;
1604 * @var tring $firstlink A HTML link representing the first page
1606 public $firstlink = null;
1608 * @var tring $lastlink A HTML link representing the last page
1610 public $lastlink = null;
1612 * @var array $pagelinks An array of strings. One of them is just a string: the current page
1614 public $pagelinks = array();
1617 * Constructor paging_bar with only the required params.
1619 * @param int $totalcount Thetotal number of entries available to be paged through
1620 * @param int $page The page you are currently viewing
1621 * @param int $perpage The number of entries that should be shown per page
1622 * @param string|moodle_url $baseurl url of the current page, the $pagevar parameter is added
1623 * @param string $pagevar name of page parameter that holds the page number
1625 public function __construct($totalcount, $page, $perpage, $baseurl, $pagevar = 'page') {
1626 $this->totalcount = $totalcount;
1627 $this->page = $page;
1628 $this->perpage = $perpage;
1629 $this->baseurl = $baseurl;
1630 $this->pagevar = $pagevar;
1636 public function prepare(renderer_base $output, moodle_page $page, $target) {
1637 if (!isset($this->totalcount) || is_null($this->totalcount)) {
1638 throw new coding_exception('paging_bar requires a totalcount value.');
1640 if (!isset($this->page) || is_null($this->page)) {
1641 throw new coding_exception('paging_bar requires a page value.');
1643 if (empty($this->perpage)) {
1644 throw new coding_exception('paging_bar requires a perpage value.');
1646 if (empty($this->baseurl)) {
1647 throw new coding_exception('paging_bar requires a baseurl value.');
1650 if ($this->totalcount > $this->perpage) {
1651 $pagenum = $this->page - 1;
1653 if ($this->page > 0) {
1654 $this->previouslink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('previous'), array('class'=>'previous'));
1657 if ($this->perpage > 0) {
1658 $lastpage = ceil($this->totalcount / $this->perpage);
1663 if ($this->page > 15) {
1664 $startpage = $this->page - 10;
1666 $this->firstlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>0)), '1', array('class'=>'first'));
1671 $currpage = $startpage;
1672 $displaycount = $displaypage = 0;
1674 while ($displaycount < $this->maxdisplay and $currpage < $lastpage) {
1675 $displaypage = $currpage + 1;
1677 if ($this->page == $currpage) {
1678 $this->pagelinks[] = $displaypage;
1680 $pagelink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$currpage)), $displaypage);
1681 $this->pagelinks[] = $pagelink;
1688 if ($currpage < $lastpage) {
1689 $lastpageactual = $lastpage - 1;
1690 $this->lastlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$lastpageactual)), $lastpage, array('class'=>'last'));
1693 $pagenum = $this->page + 1;
1695 if ($pagenum != $displaypage) {
1696 $this->nextlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('next'), array('class'=>'next'));
1704 * This class represents how a block appears on a page.
1706 * During output, each block instance is asked to return a block_contents object,
1707 * those are then passed to the $OUTPUT->block function for display.
1709 * {@link $contents} should probably be generated using a moodle_block_..._renderer.
1711 * Other block-like things that need to appear on the page, for example the
1712 * add new block UI, are also represented as block_contents objects.
1714 * @copyright 2009 Tim Hunt
1715 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1718 class block_contents {
1719 /** @var int used to set $skipid. */
1720 protected static $idcounter = 1;
1722 const NOT_HIDEABLE = 0;
1727 * @var integer $skipid All the blocks (or things that look like blocks)
1728 * printed on a page are given a unique number that can be used to construct
1729 * id="" attributes. This is set automatically be the {@link prepare()} method.
1730 * Do not try to set it manually.
1735 * @var integer If this is the contents of a real block, this should be set to
1736 * the block_instance.id. Otherwise this should be set to 0.
1738 public $blockinstanceid = 0;
1741 * @var integer if this is a real block instance, and there is a corresponding
1742 * block_position.id for the block on this page, this should be set to that id.
1743 * Otherwise it should be 0.
1745 public $blockpositionid = 0;
1748 * @param array $attributes an array of attribute => value pairs that are put on the
1749 * outer div of this block. {@link $id} and {@link $classes} attributes should be set separately.
1754 * @param string $title The title of this block. If this came from user input,
1755 * it should already have had format_string() processing done on it. This will
1756 * be output inside <h2> tags. Please do not cause invalid XHTML.
1761 * @param string $content HTML for the content
1763 public $content = '';
1766 * @param array $list an alternative to $content, it you want a list of things with optional icons.
1768 public $footer = '';
1771 * Any small print that should appear under the block to explain to the
1772 * teacher about the block, for example 'This is a sticky block that was
1773 * added in the system context.'
1776 public $annotation = '';
1779 * @var integer one of the constants NOT_HIDEABLE, VISIBLE, HIDDEN. Whether
1780 * the user can toggle whether this block is visible.
1782 public $collapsible = self::NOT_HIDEABLE;
1785 * A (possibly empty) array of editing controls. Each element of this array
1786 * should be an array('url' => $url, 'icon' => $icon, 'caption' => $caption).
1787 * $icon is the icon name. Fed to $OUTPUT->pix_url.
1790 public $controls = array();
1794 * Create new instance of block content
1795 * @param array $attributes
1797 public function __construct(array $attributes=null) {
1798 $this->skipid = self::$idcounter;
1799 self::$idcounter += 1;
1803 $this->attributes = $attributes;
1805 // simple "fake" blocks used in some modules and "Add new block" block
1806 $this->attributes = array('class'=>'sideblock');
1811 * Add html class to block
1812 * @param string $class
1815 public function add_class($class) {
1816 $this->attributes['class'] .= ' '.$class;
1822 * This class represents a target for where a block can go when it is being moved.
1824 * This needs to be rendered as a form with the given hidden from fields, and
1825 * clicking anywhere in the form should submit it. The form action should be
1828 * @copyright 2009 Tim Hunt
1829 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1832 class block_move_target {
1846 * @param string $text
1847 * @param moodle_url $url
1849 public function __construct($text, moodle_url $url) {
1850 $this->text = $text;