navigationlib: MDL-21209 - fix incorrect parameters
[moodle.git] / lib / outputcomponents.php
CommitLineData
d9c8f425 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * Classes representing HTML elements, used by $OUTPUT methods
20 *
21 * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
22 * for an overview.
23 *
24 * @package moodlecore
25 * @copyright 2009 Tim Hunt
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
5d0c95a5
PS
29
30/**
31 * Interface marking other classes as suitable for renderer_base::render()
32 * @author 2010 Petr Skoda (skodak) info@skodak.org
33 */
34interface renderable {
35 // intentionally empty
36}
37
38
39/**
bf11293a 40 * Data structure representing a user picture.
5d0c95a5
PS
41 *
42 * @copyright 2009 Nicolas Connault, 2010 Petr Skoda
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 * @since Moodle 2.0
45 */
46class user_picture implements renderable {
47 /**
48 * List of mandatory fields in user record here.
49 * @var string
50 */
51 const FIELDS = 'id,picture,firstname,lastname,imagealt';
52
53 /**
54 * @var object $user A user object with at least fields id, picture, imagealt, firstname and lastname set.
55 */
56 public $user;
57 /**
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.
60 */
61 public $courseid;
62 /**
63 * @var bool $link add course profile link to image
64 */
65 public $link = true;
66 /**
67 * @var int $size Size in pixels. Special values are (true/1 = 100px) and (false/0 = 35px) for backward compatibility
68 */
69 public $size = 35;
70 /**
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.
73 */
74 public $alttext = true;
75 /**
76 * @var boolean $popup Whether or not to open the link in a popup window.
77 */
78 public $popup = false;
79 /**
80 * @var string Image class attribute
81 */
82 public $class = 'userpicture';
83
84 /**
85 * User picture constructor.
86 *
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, ...
89 */
90 public function __construct(stdClass $user) {
91 global $DB;
92
93 static $fields = null;
94 if (is_null($fields)) {
95 $fields = explode(',', self::FIELDS);
96 }
97
98 if (empty($user->id)) {
99 throw new coding_exception('User id is required when printing user avatar image.');
100 }
101
102 // only touch the DB if we are missing data and complain loudly...
103 $needrec = false;
104 foreach ($fields as $field) {
105 if (!array_key_exists($field, $user)) {
106 $needrec = true;
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);
109 break;
110 }
111 }
112
113 if ($needrec) {
114 $this->user = $DB->get_record('user', array('id'=>$user->id), self::FIELDS, MUST_EXIST);
115 } else {
116 $this->user = clone($user);
117 }
118 }
119
120 /**
121 * Returns a list of required user fields, usefull when fetching required user info from db.
f3afba4e
PS
122 *
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.
126 *
5d0c95a5 127 * @param string $tableprefix name of database table prefix in query
f3afba4e 128 * @param string $idalias alias of id field
5d0c95a5
PS
129 * @return string
130 */
f3afba4e
PS
131 public static function fields($tableprefix = '', $idalias = '') {
132 if ($tableprefix === '' and $idalias === '') {
5d0c95a5 133 return self::FIELDS;
5d0c95a5 134 }
f3afba4e
PS
135 $fields = explode(',', self::FIELDS);
136 foreach ($fields as $key=>$field) {
137 if ($field === 'id' and $idalias !== '') {
138 $field = "$field AS $idalias";
139 }
140 $fields[$key] = "$tableprefix.$field";
141 }
142 return implode(',', $fields);
5d0c95a5
PS
143 }
144}
145
bf11293a
PS
146
147/**
148 * Data structure representing a help icon.
149 *
150 * @copyright 2009 Nicolas Connault, 2010 Petr Skoda
151 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
152 * @since Moodle 2.0
153 */
154class help_icon implements renderable {
155 /**
156 * @var string $page name of help page
157 */
158 public $helppage;
159 /**
160 * @var string $title A descriptive text for title tooltip
161 */
97c10099 162 public $title = null;
bf11293a
PS
163 /**
164 * @var string $component Component name, the same as in get_string()
165 */
166 public $component = 'moodle';
167 /**
168 * @var string $linktext Extra descriptive text next to the icon
169 */
97c10099 170 public $linktext = null;
bf11293a
PS
171
172 /**
173 * Constructor: sets up the other components in case they are needed
174 * @param string $page 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
178 * @return void
179 */
180 public function __construct($helppage, $title, $component = 'moodle') {
181 if (empty($title)) {
182 throw new coding_exception('A help_icon object requires a $text parameter');
183 }
184 if (empty($helppage)) {
185 throw new coding_exception('A help_icon object requires a $helppage parameter');
186 }
187
188 $this->helppage = $helppage;
189 $this->title = $title;
190 $this->component = $component;
191 }
192}
193
194
000c278c
PS
195/**
196 * Data structure representing an icon.
197 *
198 * @copyright 2010 Petr Skoda
199 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
200 * @since Moodle 2.0
201 */
202class pix_icon implements renderable {
203 var $pix;
204 var $component;
205 var $attributes = array();
206
207 /**
208 * Constructor
209 * @param string $pix short icon name
210 * @param string $component component name
211 * @param array $attributes html attributes
212 */
213 public function __construct($pix, $alt, $component='moodle', array $attributes = null) {
c80877aa
PS
214 $this->pix = $pix;
215 $this->component = $component;
000c278c
PS
216 $this->attributes = (array)$attributes;
217
218 $this->attributes['alt'] = $alt;
219 if (empty($this->attributes['class'])) {
220 $this->attributes['class'] = 'smallicon';
221 }
222 if (!isset($this->attributes['title'])) {
223 $this->attributes['title'] = $this->attributes['alt'];
224 }
225 }
226}
227
228
3ba60ee1
PS
229/**
230 * Data structure representing a simple form with only one button.
231 *
232 * @copyright 2009 Petr Skoda
233 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
234 * @since Moodle 2.0
235 */
236class single_button implements renderable {
574fbea4
PS
237 /**
238 * Target url
239 * @var moodle_url
240 */
3ba60ee1 241 var $url;
574fbea4
PS
242 /**
243 * Button label
244 * @var string
245 */
3ba60ee1 246 var $label;
574fbea4
PS
247 /**
248 * Form submit method
249 * @var string post or get
250 */
3ba60ee1 251 var $method = 'post';
574fbea4
PS
252 /**
253 * Wrapping div class
254 * @var string
255 * */
3ba60ee1 256 var $class = 'singlebutton';
574fbea4
PS
257 /**
258 * True if button disabled, false if normal
259 * @var boolean
260 */
3ba60ee1 261 var $disabled = false;
574fbea4
PS
262 /**
263 * Button tooltip
264 * @var string
265 */
97c10099 266 var $tooltip = null;
574fbea4
PS
267 /**
268 * Form id
269 * @var string
270 */
3ba60ee1 271 var $formid;
574fbea4
PS
272 /**
273 * List of attached actions
274 * @var array of component_action
275 */
3ba60ee1
PS
276 var $actions = array();
277
278 /**
279 * Constructor
574fbea4 280 * @param string|moodle_url $url
3ba60ee1
PS
281 * @param string $label button text
282 * @param string $method get or post submit method
3ba60ee1
PS
283 */
284 public function __construct(moodle_url $url, $label, $method='post') {
285 $this->url = clone($url);
286 $this->label = $label;
287 $this->method = $method;
288 }
289
290 /**
574fbea4 291 * Shortcut for adding a JS confirm dialog when the button is clicked.
3ba60ee1
PS
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.
294 * @return void
295 */
296 public function add_confirm_action($confirmmessage) {
20fb563e 297 $this->add_action(new component_action('click', 'M.util.show_confirm_dialog', array('message' => $confirmmessage)));
3ba60ee1
PS
298 }
299
574fbea4
PS
300 /**
301 * Add action to the button.
302 * @param component_action $action
303 * @return void
304 */
3ba60ee1
PS
305 public function add_action(component_action $action) {
306 $this->actions[] = $action;
307 }
308}
309
310
a9967cf5
PS
311/**
312 * Simple form with just one select field that gets submitted automatically.
313 * If JS not enabled small go button is printed too.
314 *
315 * @copyright 2009 Petr Skoda
316 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
317 * @since Moodle 2.0
318 */
319class single_select implements renderable {
320 /**
321 * Target url - includes hidden fields
322 * @var moodle_url
323 */
324 var $url;
325 /**
326 * Name of the select element.
327 * @var string
328 */
329 var $name;
330 /**
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')))
336 */
337 var $options;
338 /**
339 * Selected option
340 * @var string
341 */
342 var $selected;
343 /**
344 * Nothing selected
345 * @var array
346 */
347 var $nothing;
348 /**
349 * Extra select field attributes
350 * @var array
351 */
352 var $attributes = array();
353 /**
354 * Button label
355 * @var string
356 */
357 var $label = '';
358 /**
359 * Form submit method
360 * @var string post or get
361 */
362 var $method = 'get';
363 /**
364 * Wrapping div class
365 * @var string
366 * */
367 var $class = 'singleselect';
368 /**
369 * True if button disabled, false if normal
370 * @var boolean
371 */
372 var $disabled = false;
373 /**
374 * Button tooltip
375 * @var string
376 */
377 var $tooltip = null;
378 /**
379 * Form id
380 * @var string
381 */
382 var $formid = null;
383 /**
384 * List of attached actions
385 * @var array of component_action
386 */
387 var $helpicon = null;
388 /**
389 * Constructor
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
f8dab966 395 * @param string $formid
a9967cf5 396 */
f8dab966 397 public function __construct(moodle_url $url, $name, array $options, $selected='', $nothing=array(''=>'choosedots'), $formid=null) {
a9967cf5
PS
398 $this->url = $url;
399 $this->name = $name;
400 $this->options = $options;
401 $this->selected = $selected;
402 $this->nothing = $nothing;
f8dab966 403 $this->formid = $formid;
a9967cf5
PS
404 }
405
406 /**
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.
410 * @return void
411 */
412 public function add_confirm_action($confirmmessage) {
413 $this->add_action(new component_action('submit', 'M.util.show_confirm_dialog', array('message' => $confirmmessage)));
414 }
415
416 /**
417 * Add action to the button.
418 * @param component_action $action
419 * @return void
420 */
421 public function add_action(component_action $action) {
422 $this->actions[] = $action;
423 }
f8dab966
PS
424
425 /**
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
431 * @return void
432 */
433 public function set_help_icon($helppage, $title, $component = 'moodle') {
434 $this->helpicon = new help_icon($helppage, $title, $component);
435 }
436
437 /**
438 * Set's select lable
439 * @param string $label
440 * @return void
441 */
442 public function set_label($label) {
443 $this->label = $label;
444 }
a9967cf5
PS
445}
446
447
4d10e579
PS
448/**
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
452 * @since Moodle 2.0
453 */
454class url_select implements renderable {
455 /**
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')))
461 */
462 var $urls;
463 /**
464 * Selected option
465 * @var string
466 */
467 var $selected;
468 /**
469 * Nothing selected
470 * @var array
471 */
472 var $nothing;
473 /**
474 * Extra select field attributes
475 * @var array
476 */
477 var $attributes = array();
478 /**
479 * Button label
480 * @var string
481 */
482 var $label = '';
483 /**
484 * Wrapping div class
485 * @var string
486 * */
487 var $class = 'urlselect';
488 /**
489 * True if button disabled, false if normal
490 * @var boolean
491 */
492 var $disabled = false;
493 /**
494 * Button tooltip
495 * @var string
496 */
497 var $tooltip = null;
498 /**
499 * Form id
500 * @var string
501 */
502 var $formid = null;
503 /**
504 * List of attached actions
505 * @var array of component_action
506 */
507 var $helpicon = null;
508 /**
509 * Constructor
510 * @param array $urls list of options
511 * @param string $selected selected element
512 * @param array $nothing
513 * @param string $formid
514 */
515 public function __construct(array $urls, $selected='', $nothing=array(''=>'choosedots'), $formid=null) {
516 $this->urls = $urls;
517 $this->selected = $selected;
518 $this->nothing = $nothing;
519 $this->formid = $formid;
520 }
521
522 /**
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
528 * @return void
529 */
530 public function set_help_icon($helppage, $title, $component = 'moodle') {
531 $this->helpicon = new help_icon($helppage, $title, $component);
532 }
533
534 /**
535 * Set's select lable
536 * @param string $label
537 * @return void
538 */
539 public function set_label($label) {
540 $this->label = $label;
541 }
542}
543
544
574fbea4
PS
545/**
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
549 * @since Moodle 2.0
550 */
551class action_link implements renderable {
552 /**
553 * Href url
554 * @var moodle_url
555 */
556 var $url;
557 /**
558 * Link text
559 * @var string HTML fragment
560 */
561 var $text;
562 /**
563 * HTML attributes
564 * @var array
565 */
566 var $attributes;
567 /**
568 * List of actions attached to link
569 * @var array of component_action
570 */
571 var $actions;
572
573 /**
574 * Constructor
575 * @param string|moodle_url $url
576 * @param string $text HTML fragment
577 * @param component_action $action
11820bac 578 * @param array $attributes associative array of html link attributes + disabled
574fbea4
PS
579 */
580 public function __construct(moodle_url $url, $text, component_action $action=null, array $attributes=null) {
581 $this->url = clone($url);
582 $this->text = $text;
b0fef57b 583 $this->attributes = (array)$attributes;
f14b641b 584 if ($action) {
574fbea4
PS
585 $this->add_action($action);
586 }
587 }
588
589 /**
590 * Add action to the link.
591 * @param component_action $action
592 * @return void
593 */
594 public function add_action(component_action $action) {
595 $this->actions[] = $action;
596 }
c63923bd
PS
597
598 public function add_class($class) {
67da0bf7
DM
599 if (empty($this->attributes['class'])) {
600 $this->attributes['class'] = $class;
c63923bd 601 } else {
67da0bf7 602 $this->attributes['class'] .= ' ' . $class;
c63923bd
PS
603 }
604 }
574fbea4 605}
3ba60ee1 606
5d0c95a5 607
227255b8 608// ==== HTML writer and helper classes, will be probably moved elsewhere ======
5d0c95a5
PS
609
610/**
611 * Simple html output class
612 * @copyright 2009 Tim Hunt, 2010 Petr Skoda
613 */
614class html_writer {
615 /**
616 * Outputs a tag with attributes and contents
617 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
5d0c95a5 618 * @param string $contents What goes between the opening and closing tags
26acc814 619 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
5d0c95a5
PS
620 * @return string HTML fragment
621 */
26acc814 622 public static function tag($tagname, $contents, array $attributes = null) {
5d0c95a5
PS
623 return self::start_tag($tagname, $attributes) . $contents . self::end_tag($tagname);
624 }
625
626 /**
627 * Outputs an opening tag with attributes
628 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
629 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
630 * @return string HTML fragment
631 */
632 public static function start_tag($tagname, array $attributes = null) {
633 return '<' . $tagname . self::attributes($attributes) . '>';
634 }
635
636 /**
637 * Outputs a closing tag
638 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
639 * @return string HTML fragment
640 */
641 public static function end_tag($tagname) {
642 return '</' . $tagname . '>';
643 }
644
645 /**
646 * Outputs an empty tag with attributes
647 * @param string $tagname The name of tag ('input', 'img', 'br' etc.)
648 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
649 * @return string HTML fragment
650 */
651 public static function empty_tag($tagname, array $attributes = null) {
652 return '<' . $tagname . self::attributes($attributes) . ' />';
653 }
654
655 /**
656 * Outputs a HTML attribute and value
657 * @param string $name The name of the attribute ('src', 'href', 'class' etc.)
658 * @param string $value The value of the attribute. The value will be escaped with {@link s()}
659 * @return string HTML fragment
660 */
661 public static function attribute($name, $value) {
662 if (is_array($value)) {
663 debugging("Passed an array for the HTML attribute $name", DEBUG_DEVELOPER);
664 }
bf11293a
PS
665 if ($value instanceof moodle_url) {
666 return ' ' . $name . '="' . $value->out() . '"';
667 }
97c10099
PS
668
669 // special case, we do not want these in output
670 if ($value === null) {
671 return '';
5d0c95a5 672 }
97c10099
PS
673
674 // no sloppy trimming here!
675 return ' ' . $name . '="' . s($value) . '"';
5d0c95a5
PS
676 }
677
678 /**
679 * Outputs a list of HTML attributes and values
680 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
681 * The values will be escaped with {@link s()}
682 * @return string HTML fragment
683 */
684 public static function attributes(array $attributes = null) {
685 $attributes = (array)$attributes;
686 $output = '';
687 foreach ($attributes as $name => $value) {
688 $output .= self::attribute($name, $value);
689 }
690 return $output;
691 }
692
693 /**
694 * Generates random html element id.
695 * @param string $base
696 * @return string
697 */
698 public static function random_id($base='random') {
699 return uniqid($base);
700 }
0f4c64b7
PS
701
702 /**
703 * Generates a simple html link
704 * @param string|moodle_url $url
705 * @param string $text link txt
706 * @param array $attributes extra html attributes
707 * @return string HTML fragment
708 */
709 public static function link($url, $text, array $attributes = null) {
710 $attributes = (array)$attributes;
711 $attributes['href'] = $url;
26acc814 712 return self::tag('a', $text, $attributes);
0f4c64b7 713 }
3ff163c5 714
14dce022
PS
715 /**
716 * generates a simple checkbox with optional label
717 * @param string $name
718 * @param string $value
719 * @param bool $checked
720 * @param string $label
721 * @param array $attributes
722 * @return string html fragment
723 */
724 public static function checkbox($name, $value, $checked = true, $label = '', array $attributes = null) {
725 $attributes = (array)$attributes;
726 $output = '';
727
728 if ($label !== '' and !is_null($label)) {
729 if (empty($attributes['id'])) {
730 $attributes['id'] = self::random_id('checkbox_');
731 }
732 }
53868425
PS
733 $attributes['type'] = 'checkbox';
734 $attributes['value'] = $value;
735 $attributes['name'] = $name;
14dce022 736 $attributes['checked'] = $checked ? 'selected' : null;
53868425 737
14dce022
PS
738 $output .= self::empty_tag('input', $attributes);
739
740 if ($label !== '' and !is_null($label)) {
26acc814 741 $output .= self::tag('label', $label, array('for'=>$attributes['id']));
14dce022
PS
742 }
743
744 return $output;
745 }
746
78bdac64
PS
747 /**
748 * Generates a simple select yes/no form field
749 * @param string $name name of select element
750 * @param bool $selected
751 * @param array $attributes - html select element attributes
752 * @return string HRML fragment
753 */
19f3bbb2 754 public static function select_yes_no($name, $selected=true, array $attributes = null) {
78bdac64
PS
755 $options = array('1'=>get_string('yes'), '0'=>get_string('no'));
756 return self::select($options, $name, $selected, null, $attributes);
757 }
758
3ff163c5
PS
759 /**
760 * Generates a simple select form field
6770330d
PS
761 * @param array $options associative array value=>label ex.:
762 * array(1=>'One, 2=>Two)
763 * it is also possible to specify optgroup as complex label array ex.:
bde156b3 764 * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two')))
6770330d 765 * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three')))
3ff163c5
PS
766 * @param string $name name of select element
767 * @param string|array $selected value or arary of values depending on multiple attribute
768 * @param array|bool $nothing, add nothing selected option, or false of not added
769 * @param array $attributes - html select element attributes
78bdac64 770 * @return string HTML fragment
3ff163c5 771 */
aa2dea70 772 public static function select(array $options, $name, $selected = '', $nothing = array(''=>'choosedots'), array $attributes = null) {
3ff163c5
PS
773 $attributes = (array)$attributes;
774 if (is_array($nothing)) {
775 foreach ($nothing as $k=>$v) {
4b9210f3 776 if ($v === 'choose' or $v === 'choosedots') {
3ff163c5
PS
777 $nothing[$k] = get_string('choosedots');
778 }
779 }
780 $options = $nothing + $options; // keep keys, do not override
3750c3bd
PS
781
782 } else if (is_string($nothing) and $nothing !== '') {
783 // BC
784 $options = array(''=>$nothing) + $options;
bde156b3 785 }
3ff163c5
PS
786
787 // we may accept more values if multiple attribute specified
788 $selected = (array)$selected;
789 foreach ($selected as $k=>$v) {
790 $selected[$k] = (string)$v;
791 }
792
793 if (!isset($attributes['id'])) {
794 $id = 'menu'.$name;
795 // name may contaion [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading
796 $id = str_replace('[', '', $id);
797 $id = str_replace(']', '', $id);
798 $attributes['id'] = $id;
799 }
800
801 if (!isset($attributes['class'])) {
802 $class = 'menu'.$name;
803 // name may contaion [], which would make an invalid class. e.g. numeric question type editing form, assignment quickgrading
804 $class = str_replace('[', '', $class);
805 $class = str_replace(']', '', $class);
806 $attributes['class'] = $class;
807 }
808 $attributes['class'] = 'select ' . $attributes['class']; /// Add 'select' selector always
809
810 $attributes['name'] = $name;
811
812 $output = '';
813 foreach ($options as $value=>$label) {
6770330d
PS
814 if (is_array($label)) {
815 // ignore key, it just has to be unique
816 $output .= self::select_optgroup(key($label), current($label), $selected);
817 } else {
818 $output .= self::select_option($label, $value, $selected);
3ff163c5 819 }
3ff163c5 820 }
26acc814 821 return self::tag('select', $output, $attributes);
3ff163c5 822 }
6770330d
PS
823
824 private static function select_option($label, $value, array $selected) {
825 $attributes = array();
826 $value = (string)$value;
827 if (in_array($value, $selected, true)) {
828 $attributes['selected'] = 'selected';
829 }
830 $attributes['value'] = $value;
26acc814 831 return self::tag('option', $label, $attributes);
6770330d
PS
832 }
833
834 private static function select_optgroup($groupname, $options, array $selected) {
835 if (empty($options)) {
836 return '';
837 }
838 $attributes = array('label'=>$groupname);
839 $output = '';
840 foreach ($options as $value=>$label) {
841 $output .= self::select_option($label, $value, $selected);
842 }
26acc814 843 return self::tag('optgroup', $output, $attributes);
6770330d 844 }
6ea66ff3 845
f83b9b63
PS
846 /**
847 * This is a shortcut for making an hour selector menu.
848 * @param string $type The type of selector (years, months, days, hours, minutes)
849 * @param string $name fieldname
850 * @param int $currenttime A default timestamp in GMT
851 * @param int $step minute spacing
852 * @param array $attributes - html select element attributes
853 * @return HTML fragment
854 */
855 public static function select_time($type, $name, $currenttime=0, $step=5, array $attributes=null) {
856 if (!$currenttime) {
857 $currenttime = time();
858 }
859 $currentdate = usergetdate($currenttime);
860 $userdatetype = $type;
861 $timeunits = array();
862
863 switch ($type) {
864 case 'years':
865 for ($i=1970; $i<=2020; $i++) {
866 $timeunits[$i] = $i;
867 }
868 $userdatetype = 'year';
869 break;
870 case 'months':
871 for ($i=1; $i<=12; $i++) {
872 $timeunits[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
873 }
874 $userdatetype = 'month';
875 $currentdate['month'] = $currentdate['mon'];
876 break;
877 case 'days':
878 for ($i=1; $i<=31; $i++) {
879 $timeunits[$i] = $i;
880 }
881 $userdatetype = 'mday';
882 break;
883 case 'hours':
884 for ($i=0; $i<=23; $i++) {
885 $timeunits[$i] = sprintf("%02d",$i);
886 }
887 break;
888 case 'minutes':
889 if ($step != 1) {
890 $currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step;
891 }
892
893 for ($i=0; $i<=59; $i+=$step) {
894 $timeunits[$i] = sprintf("%02d",$i);
895 }
896 break;
897 default:
898 throw new coding_exception("Time type $type is not supported by html_writer::select_time().");
899 }
900
901 if (empty($attributes['id'])) {
902 $attributes['id'] = self::random_id('ts_');
903 }
904 $timerselector = self::select($timeunits, $name, $currentdate[$userdatetype], null, array('id'=>$attributes['id']));
26acc814 905 $label = self::tag('label', get_string(substr($type, 0, -1), 'form'), array('for'=>$attributes['id'], 'class'=>'accesshide'));
f83b9b63
PS
906
907 return $label.$timerselector;
908 }
909
5be262b6
PS
910 /**
911 * Shortcut for quick making of lists
912 * @param array $items
913 * @param string $tag ul or ol
914 * @param array $attributes
915 * @return string
916 */
917 public static function alist(array $items, array $attributes = null, $tag = 'ul') {
918 //note: 'list' is a reserved keyword ;-)
919
920 $output = '';
921
922 foreach ($items as $item) {
923 $output .= html_writer::start_tag('li') . "\n";
924 $output .= $item . "\n";
925 $output .= html_writer::end_tag('li') . "\n";
926 }
927
26acc814 928 return html_writer::tag($tag, $output, $attributes);
5be262b6
PS
929 }
930
6ea66ff3
PS
931 /**
932 * Returns hidden input fields created from url parameters.
933 * @param moodle_url $url
934 * @param array $exclude list of excluded parameters
935 * @return string HTML fragment
936 */
937 public static function input_hidden_params(moodle_url $url, array $exclude = null) {
938 $exclude = (array)$exclude;
939 $params = $url->params();
940 foreach ($exclude as $key) {
941 unset($params[$key]);
942 }
943
944 $output = '';
bde156b3 945 foreach ($params as $key => $value) {
6ea66ff3
PS
946 $attributes = array('type'=>'hidden', 'name'=>$key, 'value'=>$value);
947 $output .= self::empty_tag('input', $attributes)."\n";
948 }
949 return $output;
950 }
77774f6a
PS
951
952 /**
953 * Generate a script tag containing the the specified code.
954 *
955 * @param string $js the JavaScript code
e50b4c89 956 * @param moodle_url|string optional url of the external script, $code ignored if specified
77774f6a
PS
957 * @return string HTML, the code wrapped in <script> tags.
958 */
e50b4c89 959 public static function script($jscode, $url=null) {
77774f6a 960 if ($jscode) {
e50b4c89 961 $attributes = array('type'=>'text/javascript');
26acc814 962 return self::tag('script', "\n//<![CDATA[\n$jscode\n//]]>\n", $attributes) . "\n";
e50b4c89
PS
963
964 } else if ($url) {
965 $attributes = array('type'=>'text/javascript', 'src'=>$url);
26acc814 966 return self::tag('script', '', $attributes) . "\n";
a9967cf5 967
77774f6a
PS
968 } else {
969 return '';
970 }
971 }
5d0c95a5
PS
972}
973
227255b8
PS
974// ==== JS writer and helper classes, will be probably moved elsewhere ======
975
976/**
977 * Simple javascript output class
978 * @copyright 2010 Petr Skoda
979 */
980class js_writer {
981 /**
982 * Returns javascript code calling the function
983 * @param string $function function name, can be complex lin Y.Event.purgeElement
984 * @param array $arguments parameters
985 * @param int $delay execution delay in seconds
986 * @return string JS code fragment
987 */
988 public function function_call($function, array $arguments = null, $delay=0) {
1b4e41af
PS
989 if ($arguments) {
990 $arguments = array_map('json_encode', $arguments);
991 $arguments = implode(', ', $arguments);
992 } else {
993 $arguments = '';
994 }
227255b8
PS
995 $js = "$function($arguments);";
996
997 if ($delay) {
998 $delay = $delay * 1000; // in miliseconds
999 $js = "setTimeout(function() { $js }, $delay);";
1000 }
1001 return $js . "\n";
1002 }
1003
3b01539c
PS
1004 /**
1005 * Special function which adds Y as first argument of fucntion call.
1006 * @param string $function
1007 * @param array $extraarguments
1008 * @return string
1009 */
1010 public function function_call_with_Y($function, array $extraarguments = null) {
1011 if ($extraarguments) {
1012 $extraarguments = array_map('json_encode', $extraarguments);
1013 $arguments = 'Y, ' . implode(', ', $extraarguments);
1014 } else {
1015 $arguments = 'Y';
1016 }
1017 return "$function($arguments);\n";
1018 }
1019
1ce15fda
SH
1020 /**
1021 * Returns JavaScript code to initialise a new object
1022 * @param string|null $var If it is null then no var is assigned the new object
1023 * @param string $class
1024 * @param array $arguments
1025 * @param array $requirements
1026 * @param int $delay
1027 * @return string
1028 */
1029 public function object_init($var, $class, array $arguments = null, array $requirements = null, $delay=0) {
1030 if (is_array($arguments)) {
1031 $arguments = array_map('json_encode', $arguments);
1032 $arguments = implode(', ', $arguments);
1033 }
1034
1035 if ($var === null) {
53fc3e70 1036 $js = "new $class(Y, $arguments);";
1ce15fda 1037 } else if (strpos($var, '.')!==false) {
53fc3e70 1038 $js = "$var = new $class(Y, $arguments);";
1ce15fda 1039 } else {
53fc3e70 1040 $js = "var $var = new $class(Y, $arguments);";
1ce15fda
SH
1041 }
1042
1043 if ($delay) {
1044 $delay = $delay * 1000; // in miliseconds
1045 $js = "setTimeout(function() { $js }, $delay);";
1046 }
1047
1048 if (count($requirements) > 0) {
1049 $requirements = implode("', '", $requirements);
53fc3e70 1050 $js = "Y.use('$requirements', function(Y){ $js });";
1ce15fda
SH
1051 }
1052 return $js."\n";
1053 }
1054
227255b8
PS
1055 /**
1056 * Returns code setting value to variable
1057 * @param string $name
1058 * @param mixed $value json serialised value
1059 * @param bool $usevar add var definition, ignored for nested properties
1060 * @return string JS code fragment
1061 */
1062 public function set_variable($name, $value, $usevar=true) {
1063 $output = '';
1064
1065 if ($usevar) {
1066 if (strpos($name, '.')) {
1067 $output .= '';
1068 } else {
1069 $output .= 'var ';
1070 }
1071 }
1072
1073 $output .= "$name = ".json_encode($value).";";
1074
1075 return $output;
1076 }
1077
1078 /**
1079 * Writes event handler attaching code
1080 * @param mixed $selector standard YUI selector for elemnts, may be array or string, element id is in the form "#idvalue"
1081 * @param string $event A valid DOM event (click, mousedown, change etc.)
1082 * @param string $function The name of the function to call
1083 * @param array $arguments An optional array of argument parameters to pass to the function
1084 * @return string JS code fragment
1085 */
1086 public function event_handler($selector, $event, $function, array $arguments = null) {
1087 $selector = json_encode($selector);
1088 $output = "Y.on('$event', $function, $selector, null";
1089 if (!empty($arguments)) {
1090 $output .= ', ' . json_encode($arguments);
1091 }
1092 return $output . ");\n";
1093 }
1094}
1095
5d0c95a5
PS
1096
1097// ===============================================================================================
8cea545e 1098// TODO: Following HTML components still need some refactoring
5d0c95a5 1099
d9c8f425 1100/**
4ed85790 1101 * Base class for classes representing HTML elements.
d9c8f425 1102 *
1103 * Handles the id and class attributes.
1104 *
1105 * @copyright 2009 Tim Hunt
1106 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1107 * @since Moodle 2.0
1108 */
6dd7d7f0 1109class html_component {
d9c8f425 1110 /**
1111 * @var string value to use for the id attribute of this HTML tag.
1112 */
97c10099 1113 public $id = null;
d9c8f425 1114 /**
1115 * @var string $alt value to use for the alt attribute of this HTML tag.
1116 */
97c10099 1117 public $alt = null;
d9c8f425 1118 /**
1119 * @var string $style value to use for the style attribute of this HTML tag.
1120 */
97c10099 1121 public $style = null;
d9c8f425 1122 /**
1123 * @var array class names to add to this HTML element.
1124 */
1125 public $classes = array();
1126 /**
1127 * @var string $title The title attributes applicable to any XHTML element
1128 */
97c10099 1129 public $title = null;
d9c8f425 1130 /**
1131 * An optional array of component_action objects handling the action part of this component.
1132 * @var array $actions
1133 */
1134 protected $actions = array();
d9c8f425 1135
1136 /**
1137 * Ensure some class names are an array.
1138 * @param mixed $classes either an array of class names or a space-separated
1139 * string containing class names.
1140 * @return array the class names as an array.
1141 */
1142 public static function clean_classes($classes) {
642816a6 1143 if (empty($classes)) {
3bd6b994 1144 return array();
642816a6 1145 } else if (is_array($classes)) {
d9c8f425 1146 return $classes;
1147 } else {
1148 return explode(' ', trim($classes));
1149 }
1150 }
1151
1152 /**
1153 * Set the class name array.
1154 * @param mixed $classes either an array of class names or a space-separated
1155 * string containing class names.
1156 * @return void
1157 */
1158 public function set_classes($classes) {
1159 $this->classes = self::clean_classes($classes);
1160 }
1161
1162 /**
1163 * Add a class name to the class names array.
1164 * @param string $class the new class name to add.
1165 * @return void
1166 */
1167 public function add_class($class) {
1168 $this->classes[] = $class;
1169 }
1170
1171 /**
1172 * Add a whole lot of class names to the class names array.
1173 * @param mixed $classes either an array of class names or a space-separated
1174 * string containing class names.
1175 * @return void
1176 */
1177 public function add_classes($classes) {
eeecf5a7 1178 $this->classes = array_merge($this->classes, self::clean_classes($classes));
d9c8f425 1179 }
1180
1181 /**
1182 * Get the class names as a string.
1183 * @return string the class names as a space-separated string. Ready to be put in the class="" attribute.
1184 */
1185 public function get_classes_string() {
1186 return implode(' ', $this->classes);
1187 }
1188
1189 /**
1190 * Perform any cleanup or final processing that should be done before an
34059565
PS
1191 * instance of this class is output. This method is supposed to be called
1192 * only from renderers.
d9c8f425 1193 * @return void
1194 */
8cea545e 1195 public function prepare() {
d9c8f425 1196 $this->classes = array_unique(self::clean_classes($this->classes));
1197 }
7a5c78e0 1198}
1199
34059565 1200
7b1f2c82 1201/**
1202 * Holds all the information required to render a <table> by
78946b9b 1203 * {@see core_renderer::table()} or by an overridden version of that
7b1f2c82 1204 * method in a subclass.
1205 *
1206 * Example of usage:
1207 * $t = new html_table();
1208 * ... // set various properties of the object $t as described below
1209 * echo $OUTPUT->table($t);
1210 *
1211 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
1212 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1213 * @since Moodle 2.0
1214 */
f7065efe 1215class html_table extends html_component {
7b1f2c82 1216 /**
a0ead5eb 1217 * For more control over the rendering of the headers, an array of html_table_cell objects
54a007e8 1218 * can be passed instead of an array of strings.
7b1f2c82 1219 * @var array of headings. The n-th array item is used as a heading of the n-th column.
1220 *
1221 * Example of usage:
1222 * $t->head = array('Student', 'Grade');
1223 */
1224 public $head;
1225 /**
1226 * @var array can be used to make a heading span multiple columns
1227 *
1228 * Example of usage:
1229 * $t->headspan = array(2,1);
1230 *
1231 * In this example, {@see html_table:$data} is supposed to have three columns. For the first two columns,
1232 * the same heading is used. Therefore, {@see html_table::$head} should consist of two items.
1233 */
1234 public $headspan;
1235 /**
1236 * @var array of column alignments. The value is used as CSS 'text-align' property. Therefore, possible
1237 * values are 'left', 'right', 'center' and 'justify'. Specify 'right' or 'left' from the perspective
1238 * of a left-to-right (LTR) language. For RTL, the values are flipped automatically.
1239 *
beb56299 1240 * Examples of usage:
1241 * $t->align = array(null, 'right');
1242 * or
1243 * $t->align[1] = 'right';
1244 *
d9c8f425 1245 */
beb56299 1246 public $align;
d9c8f425 1247 /**
beb56299 1248 * @var array of column sizes. The value is used as CSS 'size' property.
1249 *
1250 * Examples of usage:
1251 * $t->size = array('50%', '50%');
1252 * or
1253 * $t->size[1] = '120px';
d9c8f425 1254 */
beb56299 1255 public $size;
d9c8f425 1256 /**
beb56299 1257 * @var array of wrapping information. The only possible value is 'nowrap' that sets the
1258 * CSS property 'white-space' to the value 'nowrap' in the given column.
1259 *
1260 * Example of usage:
1261 * $t->wrap = array(null, 'nowrap');
d9c8f425 1262 */
beb56299 1263 public $wrap;
d9c8f425 1264 /**
beb56299 1265 * @var array of arrays or html_table_row objects containing the data. Alternatively, if you have
1266 * $head specified, the string 'hr' (for horizontal ruler) can be used
1267 * instead of an array of cells data resulting in a divider rendered.
d9c8f425 1268 *
beb56299 1269 * Example of usage with array of arrays:
1270 * $row1 = array('Harry Potter', '76 %');
1271 * $row2 = array('Hermione Granger', '100 %');
1272 * $t->data = array($row1, $row2);
d9c8f425 1273 *
beb56299 1274 * Example with array of html_table_row objects: (used for more fine-grained control)
1275 * $cell1 = new html_table_cell();
1276 * $cell1->text = 'Harry Potter';
1277 * $cell1->colspan = 2;
1278 * $row1 = new html_table_row();
1279 * $row1->cells[] = $cell1;
1280 * $cell2 = new html_table_cell();
1281 * $cell2->text = 'Hermione Granger';
1282 * $cell3 = new html_table_cell();
1283 * $cell3->text = '100 %';
1284 * $row2 = new html_table_row();
1285 * $row2->cells = array($cell2, $cell3);
1286 * $t->data = array($row1, $row2);
1287 */
1288 public $data;
1289 /**
1290 * @var string width of the table, percentage of the page preferred. Defaults to 80% of the page width.
1291 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1292 */
1293 public $width = null;
1294 /**
1295 * @var string alignment the whole table. Can be 'right', 'left' or 'center' (default).
1296 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1297 */
1298 public $tablealign = null;
1299 /**
1300 * @var int padding on each cell, in pixels
1301 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1302 */
1303 public $cellpadding = null;
1304 /**
1305 * @var int spacing between cells, in pixels
1306 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1307 */
1308 public $cellspacing = null;
1309 /**
1310 * @var array classes to add to particular rows, space-separated string.
1311 * Classes 'r0' or 'r1' are added automatically for every odd or even row,
1312 * respectively. Class 'lastrow' is added automatically for the last row
1313 * in the table.
d9c8f425 1314 *
beb56299 1315 * Example of usage:
1316 * $t->rowclasses[9] = 'tenth'
1317 */
1318 public $rowclasses;
1319 /**
1320 * @var array classes to add to every cell in a particular column,
1321 * space-separated string. Class 'cell' is added automatically by the renderer.
1322 * Classes 'c0' or 'c1' are added automatically for every odd or even column,
1323 * respectively. Class 'lastcol' is added automatically for all last cells
1324 * in a row.
d9c8f425 1325 *
beb56299 1326 * Example of usage:
1327 * $t->colclasses = array(null, 'grade');
d9c8f425 1328 */
beb56299 1329 public $colclasses;
1330 /**
1331 * @var string description of the contents for screen readers.
1332 */
1333 public $summary;
1334 /**
1335 * @var bool true causes the contents of the heading cells to be rotated 90 degrees.
1336 */
1337 public $rotateheaders = false;
319770d7 1338 /**
1339 * @var array $headclasses Array of CSS classes to apply to the table's thead.
1340 */
1341 public $headclasses = array();
1342 /**
1343 * @var array $bodyclasses Array of CSS classes to apply to the table's tbody.
1344 */
1345 public $bodyclasses = array();
1346 /**
1347 * @var array $footclasses Array of CSS classes to apply to the table's tfoot.
1348 */
1349 public $footclasses = array();
a0ead5eb 1350
d9c8f425 1351
1352 /**
6dd7d7f0 1353 * @see html_component::prepare()
beb56299 1354 * @return void
d9c8f425 1355 */
8cea545e 1356 public function prepare() {
beb56299 1357 if (!empty($this->align)) {
1358 foreach ($this->align as $key => $aa) {
1359 if ($aa) {
1360 $this->align[$key] = 'text-align:'. fix_align_rtl($aa) .';'; // Fix for RTL languages
1361 } else {
97c10099 1362 $this->align[$key] = null;
beb56299 1363 }
1364 }
d9c8f425 1365 }
beb56299 1366 if (!empty($this->size)) {
1367 foreach ($this->size as $key => $ss) {
1368 if ($ss) {
1369 $this->size[$key] = 'width:'. $ss .';';
1370 } else {
97c10099 1371 $this->size[$key] = null;
beb56299 1372 }
1373 }
d9c8f425 1374 }
beb56299 1375 if (!empty($this->wrap)) {
1376 foreach ($this->wrap as $key => $ww) {
1377 if ($ww) {
1378 $this->wrap[$key] = 'white-space:nowrap;';
1379 } else {
1380 $this->wrap[$key] = '';
1381 }
1382 }
d9c8f425 1383 }
beb56299 1384 if (!empty($this->head)) {
1385 foreach ($this->head as $key => $val) {
1386 if (!isset($this->align[$key])) {
97c10099 1387 $this->align[$key] = null;
beb56299 1388 }
1389 if (!isset($this->size[$key])) {
97c10099 1390 $this->size[$key] = null;
beb56299 1391 }
1392 if (!isset($this->wrap[$key])) {
97c10099 1393 $this->wrap[$key] = null;
d9c8f425 1394 }
1395
d9c8f425 1396 }
beb56299 1397 }
1398 if (empty($this->classes)) { // must be done before align
1399 $this->set_classes(array('generaltable'));
1400 }
1401 if (!empty($this->tablealign)) {
1402 $this->add_class('boxalign' . $this->tablealign);
1403 }
1404 if (!empty($this->rotateheaders)) {
1405 $this->add_class('rotateheaders');
d9c8f425 1406 } else {
beb56299 1407 $this->rotateheaders = false; // Makes life easier later.
1408 }
8cea545e 1409 parent::prepare();
d9c8f425 1410 }
d9c8f425 1411}
1412
34059565 1413
d9c8f425 1414/**
7b1f2c82 1415 * Component representing a table row.
d9c8f425 1416 *
1417 * @copyright 2009 Nicolas Connault
1418 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1419 * @since Moodle 2.0
1420 */
6dd7d7f0 1421class html_table_row extends html_component {
d9c8f425 1422 /**
7b1f2c82 1423 * @var array $cells Array of html_table_cell objects
d9c8f425 1424 */
7b1f2c82 1425 public $cells = array();
d9c8f425 1426
beb56299 1427 /**
6dd7d7f0 1428 * @see lib/html_component#prepare()
beb56299 1429 * @return void
1430 */
8cea545e
PS
1431 public function prepare() {
1432 parent::prepare();
d9c8f425 1433 }
a0ead5eb 1434
54a007e8 1435 /**
8cea545e 1436 * Constructor
54a007e8 1437 * @param array $cells
8cea545e
PS
1438 */
1439 public function __construct(array $cells=null) {
1440 $cells = (array)$cells;
1441 foreach ($cells as $cell) {
1442 if ($cell instanceof html_table_cell) {
1443 $this->cells[] = $cell;
a019627a 1444 } else {
8cea545e 1445 $this->cells[] = new html_table_cell($cell);
a019627a 1446 }
1447 }
54a007e8 1448 }
d9c8f425 1449}
1450
34059565 1451
d9c8f425 1452/**
7b1f2c82 1453 * Component representing a table cell.
d9c8f425 1454 *
1455 * @copyright 2009 Nicolas Connault
1456 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1457 * @since Moodle 2.0
1458 */
6dd7d7f0 1459class html_table_cell extends html_component {
d9c8f425 1460 /**
7b1f2c82 1461 * @var string $text The contents of the cell
d9c8f425 1462 */
7b1f2c82 1463 public $text;
d9c8f425 1464 /**
7b1f2c82 1465 * @var string $abbr Abbreviated version of the contents of the cell
d9c8f425 1466 */
97c10099 1467 public $abbr = null;
d9c8f425 1468 /**
7b1f2c82 1469 * @var int $colspan Number of columns this cell should span
d9c8f425 1470 */
97c10099 1471 public $colspan = null;
d9c8f425 1472 /**
7b1f2c82 1473 * @var int $rowspan Number of rows this cell should span
d9c8f425 1474 */
97c10099 1475 public $rowspan = null;
d9c8f425 1476 /**
7b1f2c82 1477 * @var string $scope Defines a way to associate header cells and data cells in a table
d9c8f425 1478 */
97c10099 1479 public $scope = null;
1ae3767a 1480 /**
1481 * @var boolean $header Whether or not this cell is a header cell
1482 */
a4998d01 1483 public $header = null;
d9c8f425 1484
1485 /**
6dd7d7f0 1486 * @see lib/html_component#prepare()
d9c8f425 1487 * @return void
1488 */
8cea545e 1489 public function prepare() {
54a007e8 1490 if ($this->header && empty($this->scope)) {
1491 $this->scope = 'col';
1492 }
8cea545e
PS
1493 parent::prepare();
1494 }
1495
1496 public function __construct($text = null) {
1497 $this->text = $text;
d9c8f425 1498 }
1499}
1500
34059565 1501
7b1f2c82 1502/// Complex components aggregating simpler components
1503
34059565 1504
d9c8f425 1505/**
beb56299 1506 * Component representing a paging bar.
d9c8f425 1507 *
1508 * @copyright 2009 Nicolas Connault
1509 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1510 * @since Moodle 2.0
1511 */
929d7a83 1512class paging_bar implements renderable {
d9c8f425 1513 /**
beb56299 1514 * @var int $maxdisplay The maximum number of pagelinks to display
d9c8f425 1515 */
beb56299 1516 public $maxdisplay = 18;
d9c8f425 1517 /**
beb56299 1518 * @var int $totalcount post or get
d9c8f425 1519 */
beb56299 1520 public $totalcount;
d9c8f425 1521 /**
beb56299 1522 * @var int $page The page you are currently viewing
d9c8f425 1523 */
929d7a83 1524 public $page;
d9c8f425 1525 /**
beb56299 1526 * @var int $perpage The number of entries that should be shown per page
d9c8f425 1527 */
beb56299 1528 public $perpage;
d9c8f425 1529 /**
beb56299 1530 * @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.
1531 * If this is a moodle_url object then the pagevar param will be replaced by the page no, for each page.
d9c8f425 1532 */
beb56299 1533 public $baseurl;
d9c8f425 1534 /**
beb56299 1535 * @var string $pagevar This is the variable name that you use for the page number in your code (ie. 'tablepage', 'blogpage', etc)
d9c8f425 1536 */
929d7a83 1537 public $pagevar;
beb56299 1538 /**
56ddb719 1539 * @var string $previouslink A HTML link representing the "previous" page
beb56299 1540 */
1541 public $previouslink = null;
1542 /**
56ddb719 1543 * @var tring $nextlink A HTML link representing the "next" page
beb56299 1544 */
1545 public $nextlink = null;
1546 /**
56ddb719 1547 * @var tring $firstlink A HTML link representing the first page
beb56299 1548 */
1549 public $firstlink = null;
1550 /**
56ddb719 1551 * @var tring $lastlink A HTML link representing the last page
beb56299 1552 */
1553 public $lastlink = null;
1554 /**
56ddb719 1555 * @var array $pagelinks An array of strings. One of them is just a string: the current page
beb56299 1556 */
1557 public $pagelinks = array();
d9c8f425 1558
929d7a83
PS
1559 /**
1560 * Constructor paging_bar with only the required params.
1561 *
1562 * @param int $totalcount Thetotal number of entries available to be paged through
1563 * @param int $page The page you are currently viewing
1564 * @param int $perpage The number of entries that should be shown per page
1565 * @param string|moodle_url $baseurl url of the current page, the $pagevar parameter is added
1566 * @param string $pagevar name of page parameter that holds the page number
1567 */
1568 public function __construct($totalcount, $page, $perpage, $baseurl, $pagevar = 'page') {
1569 $this->totalcount = $totalcount;
1570 $this->page = $page;
1571 $this->perpage = $perpage;
1572 $this->baseurl = $baseurl;
1573 $this->pagevar = $pagevar;
1574 }
1575
d9c8f425 1576 /**
6dd7d7f0 1577 * @see lib/html_component#prepare()
d9c8f425 1578 * @return void
1579 */
34059565 1580 public function prepare(renderer_base $output, moodle_page $page, $target) {
1c1f64a2 1581 if (!isset($this->totalcount) || is_null($this->totalcount)) {
929d7a83 1582 throw new coding_exception('paging_bar requires a totalcount value.');
beb56299 1583 }
1584 if (!isset($this->page) || is_null($this->page)) {
929d7a83 1585 throw new coding_exception('paging_bar requires a page value.');
beb56299 1586 }
1587 if (empty($this->perpage)) {
929d7a83 1588 throw new coding_exception('paging_bar requires a perpage value.');
beb56299 1589 }
1590 if (empty($this->baseurl)) {
929d7a83 1591 throw new coding_exception('paging_bar requires a baseurl value.');
beb56299 1592 }
d9c8f425 1593
beb56299 1594 if ($this->totalcount > $this->perpage) {
1595 $pagenum = $this->page - 1;
d9c8f425 1596
beb56299 1597 if ($this->page > 0) {
929d7a83 1598 $this->previouslink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('previous'), array('class'=>'previous'));
beb56299 1599 }
d9c8f425 1600
beb56299 1601 if ($this->perpage > 0) {
1602 $lastpage = ceil($this->totalcount / $this->perpage);
1603 } else {
1604 $lastpage = 1;
1605 }
1606
1607 if ($this->page > 15) {
1608 $startpage = $this->page - 10;
1609
929d7a83 1610 $this->firstlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>0)), '1', array('class'=>'first'));
beb56299 1611 } else {
1612 $startpage = 0;
1613 }
1614
1615 $currpage = $startpage;
1616 $displaycount = $displaypage = 0;
1617
1618 while ($displaycount < $this->maxdisplay and $currpage < $lastpage) {
1619 $displaypage = $currpage + 1;
1620
f43cdceb 1621 if ($this->page == $currpage) {
beb56299 1622 $this->pagelinks[] = $displaypage;
1623 } else {
56ddb719 1624 $pagelink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$currpage)), $displaypage);
beb56299 1625 $this->pagelinks[] = $pagelink;
1626 }
1627
1628 $displaycount++;
1629 $currpage++;
1630 }
1631
1632 if ($currpage < $lastpage) {
1633 $lastpageactual = $lastpage - 1;
abdac127 1634 $this->lastlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$lastpageactual)), $lastpage, array('class'=>'last'));
beb56299 1635 }
1636
1637 $pagenum = $this->page + 1;
1638
1639 if ($pagenum != $displaypage) {
abdac127 1640 $this->nextlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('next'), array('class'=>'next'));
beb56299 1641 }
d9c8f425 1642 }
1643 }
d9c8f425 1644}
1645
34059565 1646
d9c8f425 1647/**
beb56299 1648 * This class represents how a block appears on a page.
d9c8f425 1649 *
beb56299 1650 * During output, each block instance is asked to return a block_contents object,
1651 * those are then passed to the $OUTPUT->block function for display.
1652 *
1653 * {@link $contents} should probably be generated using a moodle_block_..._renderer.
1654 *
1655 * Other block-like things that need to appear on the page, for example the
1656 * add new block UI, are also represented as block_contents objects.
1657 *
1658 * @copyright 2009 Tim Hunt
d9c8f425 1659 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1660 * @since Moodle 2.0
1661 */
dd72b308 1662class block_contents {
beb56299 1663 /** @var int used to set $skipid. */
1664 protected static $idcounter = 1;
1665
1666 const NOT_HIDEABLE = 0;
1667 const VISIBLE = 1;
1668 const HIDDEN = 2;
1669
d9c8f425 1670 /**
dd72b308 1671 * @var integer $skipid All the blocks (or things that look like blocks)
beb56299 1672 * printed on a page are given a unique number that can be used to construct
1673 * id="" attributes. This is set automatically be the {@link prepare()} method.
1674 * Do not try to set it manually.
d9c8f425 1675 */
beb56299 1676 public $skipid;
d9c8f425 1677
1678 /**
beb56299 1679 * @var integer If this is the contents of a real block, this should be set to
1680 * the block_instance.id. Otherwise this should be set to 0.
1681 */
1682 public $blockinstanceid = 0;
1683
1684 /**
1685 * @var integer if this is a real block instance, and there is a corresponding
1686 * block_position.id for the block on this page, this should be set to that id.
1687 * Otherwise it should be 0.
1688 */
1689 public $blockpositionid = 0;
1690
1691 /**
1692 * @param array $attributes an array of attribute => value pairs that are put on the
1693 * outer div of this block. {@link $id} and {@link $classes} attributes should be set separately.
1694 */
dd72b308 1695 public $attributes;
beb56299 1696
1697 /**
1698 * @param string $title The title of this block. If this came from user input,
1699 * it should already have had format_string() processing done on it. This will
1700 * be output inside <h2> tags. Please do not cause invalid XHTML.
1701 */
1702 public $title = '';
1703
1704 /**
1705 * @param string $content HTML for the content
1706 */
1707 public $content = '';
1708
1709 /**
1710 * @param array $list an alternative to $content, it you want a list of things with optional icons.
1711 */
1712 public $footer = '';
1713
1714 /**
1715 * Any small print that should appear under the block to explain to the
1716 * teacher about the block, for example 'This is a sticky block that was
1717 * added in the system context.'
1718 * @var string
1719 */
1720 public $annotation = '';
1721
1722 /**
1723 * @var integer one of the constants NOT_HIDEABLE, VISIBLE, HIDDEN. Whether
1724 * the user can toggle whether this block is visible.
1725 */
1726 public $collapsible = self::NOT_HIDEABLE;
1727
1728 /**
1729 * A (possibly empty) array of editing controls. Each element of this array
1730 * should be an array('url' => $url, 'icon' => $icon, 'caption' => $caption).
b5d0cafc 1731 * $icon is the icon name. Fed to $OUTPUT->pix_url.
beb56299 1732 * @var array
1733 */
1734 public $controls = array();
1735
dd72b308 1736
beb56299 1737 /**
dd72b308
PS
1738 * Create new instance of block content
1739 * @param array $attributes
d9c8f425 1740 */
dd72b308 1741 public function __construct(array $attributes=null) {
beb56299 1742 $this->skipid = self::$idcounter;
1743 self::$idcounter += 1;
dd72b308
PS
1744
1745 if ($attributes) {
1746 // standard block
1747 $this->attributes = $attributes;
1748 } else {
1749 // simple "fake" blocks used in some modules and "Add new block" block
1750 $this->attributes = array('class'=>'sideblock');
beb56299 1751 }
dd72b308
PS
1752 }
1753
1754 /**
1755 * Add html class to block
1756 * @param string $class
1757 * @return void
1758 */
1759 public function add_class($class) {
1760 $this->attributes['class'] .= ' '.$class;
d9c8f425 1761 }
1762}
beb56299 1763
34059565 1764
beb56299 1765/**
1766 * This class represents a target for where a block can go when it is being moved.
1767 *
1768 * This needs to be rendered as a form with the given hidden from fields, and
1769 * clicking anywhere in the form should submit it. The form action should be
1770 * $PAGE->url.
1771 *
1772 * @copyright 2009 Tim Hunt
1773 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1774 * @since Moodle 2.0
1775 */
dd72b308 1776class block_move_target {
beb56299 1777 /**
dd72b308
PS
1778 * Move url
1779 * @var moodle_url
beb56299 1780 */
dd72b308 1781 public $url;
beb56299 1782 /**
dd72b308
PS
1783 * label
1784 * @var string
beb56299 1785 */
dd72b308
PS
1786 public $text;
1787
1788 /**
1789 * Cosntructor
1790 * @param string $text
1791 * @param moodle_url $url
1792 */
1793 public function __construct($text, moodle_url $url) {
1794 $this->text = $text;
1795 $this->url = $url;
1796 }
beb56299 1797}