MDL-21233 moodle_url improvemewnts, code simplification, more diagnostics; fixed...
[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 * This constant is used for html attributes which need to have an empty
31 * value and still be output by the renderers (e.g. alt="");
32 *
33 * @constant @EMPTY@
34 */
35define('HTML_ATTR_EMPTY', '@EMPTY@');
36
37
38/**
39 * Interface marking other classes as suitable for renderer_base::render()
40 * @author 2010 Petr Skoda (skodak) info@skodak.org
41 */
42interface renderable {
43 // intentionally empty
44}
45
46
47/**
bf11293a 48 * Data structure representing a user picture.
5d0c95a5
PS
49 *
50 * @copyright 2009 Nicolas Connault, 2010 Petr Skoda
51 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
52 * @since Moodle 2.0
53 */
54class user_picture implements renderable {
55 /**
56 * List of mandatory fields in user record here.
57 * @var string
58 */
59 const FIELDS = 'id,picture,firstname,lastname,imagealt';
60
61 /**
62 * @var object $user A user object with at least fields id, picture, imagealt, firstname and lastname set.
63 */
64 public $user;
65 /**
66 * @var int $courseid The course id. Used when constructing the link to the user's profile,
67 * page course id used if not specified.
68 */
69 public $courseid;
70 /**
71 * @var bool $link add course profile link to image
72 */
73 public $link = true;
74 /**
75 * @var int $size Size in pixels. Special values are (true/1 = 100px) and (false/0 = 35px) for backward compatibility
76 */
77 public $size = 35;
78 /**
79 * @var boolean $alttext add non-blank alt-text to the image.
80 * Default true, set to false when image alt just duplicates text in screenreaders.
81 */
82 public $alttext = true;
83 /**
84 * @var boolean $popup Whether or not to open the link in a popup window.
85 */
86 public $popup = false;
87 /**
88 * @var string Image class attribute
89 */
90 public $class = 'userpicture';
91
92 /**
93 * User picture constructor.
94 *
95 * @param object $user user record with at least id, picture, imagealt, firstname and lastname set.
96 * @param array $options such as link, size, link, ...
97 */
98 public function __construct(stdClass $user) {
99 global $DB;
100
101 static $fields = null;
102 if (is_null($fields)) {
103 $fields = explode(',', self::FIELDS);
104 }
105
106 if (empty($user->id)) {
107 throw new coding_exception('User id is required when printing user avatar image.');
108 }
109
110 // only touch the DB if we are missing data and complain loudly...
111 $needrec = false;
112 foreach ($fields as $field) {
113 if (!array_key_exists($field, $user)) {
114 $needrec = true;
115 debugging('Missing '.$field.' property in $user object, this is a performance problem that needs to be fixed by a developer. '
116 .'Please use user_picture::fields() to get the full list of required fields.', DEBUG_DEVELOPER);
117 break;
118 }
119 }
120
121 if ($needrec) {
122 $this->user = $DB->get_record('user', array('id'=>$user->id), self::FIELDS, MUST_EXIST);
123 } else {
124 $this->user = clone($user);
125 }
126 }
127
128 /**
129 * Returns a list of required user fields, usefull when fetching required user info from db.
130 * @param string $tableprefix name of database table prefix in query
131 * @return string
132 */
133 public static function fields($tableprefix = '') {
134 if ($tableprefix === '') {
135 return self::FIELDS;
136 } else {
137 return "$tableprefix." . str_replace(',', ",$tableprefix.", self::FIELDS);
138 }
139 }
140}
141
bf11293a
PS
142
143/**
144 * Data structure representing a help icon.
145 *
146 * @copyright 2009 Nicolas Connault, 2010 Petr Skoda
147 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
148 * @since Moodle 2.0
149 */
150class help_icon implements renderable {
151 /**
152 * @var string $page name of help page
153 */
154 public $helppage;
155 /**
156 * @var string $title A descriptive text for title tooltip
157 */
158 public $title = '';
159 /**
160 * @var string $component Component name, the same as in get_string()
161 */
162 public $component = 'moodle';
163 /**
164 * @var string $linktext Extra descriptive text next to the icon
165 */
166 public $linktext = '';
167
168 /**
169 * Constructor: sets up the other components in case they are needed
170 * @param string $page The keyword that defines a help page
171 * @param string $title A descriptive text for accesibility only
172 * @param string $component
173 * @param bool $linktext add extra text to icon
174 * @return void
175 */
176 public function __construct($helppage, $title, $component = 'moodle') {
177 if (empty($title)) {
178 throw new coding_exception('A help_icon object requires a $text parameter');
179 }
180 if (empty($helppage)) {
181 throw new coding_exception('A help_icon object requires a $helppage parameter');
182 }
183
184 $this->helppage = $helppage;
185 $this->title = $title;
186 $this->component = $component;
187 }
188}
189
190
3ba60ee1
PS
191/**
192 * Data structure representing a simple form with only one button.
193 *
194 * @copyright 2009 Petr Skoda
195 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
196 * @since Moodle 2.0
197 */
198class single_button implements renderable {
574fbea4
PS
199 /**
200 * Target url
201 * @var moodle_url
202 */
3ba60ee1 203 var $url;
574fbea4
PS
204 /**
205 * Button label
206 * @var string
207 */
3ba60ee1 208 var $label;
574fbea4
PS
209 /**
210 * Form submit method
211 * @var string post or get
212 */
3ba60ee1 213 var $method = 'post';
574fbea4
PS
214 /**
215 * Wrapping div class
216 * @var string
217 * */
3ba60ee1 218 var $class = 'singlebutton';
574fbea4
PS
219 /**
220 * True if button disabled, false if normal
221 * @var boolean
222 */
3ba60ee1 223 var $disabled = false;
574fbea4
PS
224 /**
225 * Button tooltip
226 * @var string
227 */
3ba60ee1 228 var $tooltip = '';
574fbea4
PS
229 /**
230 * Form id
231 * @var string
232 */
3ba60ee1 233 var $formid;
574fbea4
PS
234 /**
235 * List of attached actions
236 * @var array of component_action
237 */
3ba60ee1
PS
238 var $actions = array();
239
240 /**
241 * Constructor
574fbea4 242 * @param string|moodle_url $url
3ba60ee1
PS
243 * @param string $label button text
244 * @param string $method get or post submit method
3ba60ee1
PS
245 */
246 public function __construct(moodle_url $url, $label, $method='post') {
247 $this->url = clone($url);
248 $this->label = $label;
249 $this->method = $method;
250 }
251
252 /**
574fbea4 253 * Shortcut for adding a JS confirm dialog when the button is clicked.
3ba60ee1
PS
254 * The message must be a yes/no question.
255 * @param string $message The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
256 * @return void
257 */
258 public function add_confirm_action($confirmmessage) {
259 $this->add_action(new component_action('click', 'confirm_dialog', array('message' => $confirmmessage)));
260 }
261
574fbea4
PS
262 /**
263 * Add action to the button.
264 * @param component_action $action
265 * @return void
266 */
3ba60ee1
PS
267 public function add_action(component_action $action) {
268 $this->actions[] = $action;
269 }
270}
271
272
574fbea4
PS
273/**
274 * Data structure describing html link with special action attached.
275 * @copyright 2010 Petr Skoda
276 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
277 * @since Moodle 2.0
278 */
279class action_link implements renderable {
280 /**
281 * Href url
282 * @var moodle_url
283 */
284 var $url;
285 /**
286 * Link text
287 * @var string HTML fragment
288 */
289 var $text;
290 /**
291 * HTML attributes
292 * @var array
293 */
294 var $attributes;
295 /**
296 * List of actions attached to link
297 * @var array of component_action
298 */
299 var $actions;
300
301 /**
302 * Constructor
303 * @param string|moodle_url $url
304 * @param string $text HTML fragment
305 * @param component_action $action
11820bac 306 * @param array $attributes associative array of html link attributes + disabled
574fbea4
PS
307 */
308 public function __construct(moodle_url $url, $text, component_action $action=null, array $attributes=null) {
309 $this->url = clone($url);
310 $this->text = $text;
f14b641b 311 if ($action) {
574fbea4
PS
312 $this->add_action($action);
313 }
314 }
315
316 /**
317 * Add action to the link.
318 * @param component_action $action
319 * @return void
320 */
321 public function add_action(component_action $action) {
322 $this->actions[] = $action;
323 }
324}
3ba60ee1 325
5d0c95a5
PS
326// ==== HTML writer and helper classes, will be probably moved elsewhere ======
327
328
329/**
330 * Simple html output class
331 * @copyright 2009 Tim Hunt, 2010 Petr Skoda
332 */
333class html_writer {
334 /**
335 * Outputs a tag with attributes and contents
336 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
337 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
338 * @param string $contents What goes between the opening and closing tags
339 * @return string HTML fragment
340 */
341 public static function tag($tagname, array $attributes = null, $contents) {
342 return self::start_tag($tagname, $attributes) . $contents . self::end_tag($tagname);
343 }
344
345 /**
346 * Outputs an opening tag with attributes
347 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
348 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
349 * @return string HTML fragment
350 */
351 public static function start_tag($tagname, array $attributes = null) {
352 return '<' . $tagname . self::attributes($attributes) . '>';
353 }
354
355 /**
356 * Outputs a closing tag
357 * @param string $tagname The name of tag ('a', 'img', 'span' etc.)
358 * @return string HTML fragment
359 */
360 public static function end_tag($tagname) {
361 return '</' . $tagname . '>';
362 }
363
364 /**
365 * Outputs an empty tag with attributes
366 * @param string $tagname The name of tag ('input', 'img', 'br' etc.)
367 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
368 * @return string HTML fragment
369 */
370 public static function empty_tag($tagname, array $attributes = null) {
371 return '<' . $tagname . self::attributes($attributes) . ' />';
372 }
373
374 /**
375 * Outputs a HTML attribute and value
376 * @param string $name The name of the attribute ('src', 'href', 'class' etc.)
377 * @param string $value The value of the attribute. The value will be escaped with {@link s()}
378 * @return string HTML fragment
379 */
380 public static function attribute($name, $value) {
381 if (is_array($value)) {
382 debugging("Passed an array for the HTML attribute $name", DEBUG_DEVELOPER);
383 }
bf11293a
PS
384 if ($value instanceof moodle_url) {
385 return ' ' . $name . '="' . $value->out() . '"';
386 }
5d0c95a5
PS
387 $value = trim($value);
388 if ($value == HTML_ATTR_EMPTY) {
389 return ' ' . $name . '=""';
390 } else if ($value || is_numeric($value)) { // We want 0 to be output.
391 return ' ' . $name . '="' . s($value) . '"';
392 }
393 }
394
395 /**
396 * Outputs a list of HTML attributes and values
397 * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.)
398 * The values will be escaped with {@link s()}
399 * @return string HTML fragment
400 */
401 public static function attributes(array $attributes = null) {
402 $attributes = (array)$attributes;
403 $output = '';
404 foreach ($attributes as $name => $value) {
405 $output .= self::attribute($name, $value);
406 }
407 return $output;
408 }
409
410 /**
411 * Generates random html element id.
412 * @param string $base
413 * @return string
414 */
415 public static function random_id($base='random') {
416 return uniqid($base);
417 }
0f4c64b7
PS
418
419 /**
420 * Generates a simple html link
421 * @param string|moodle_url $url
422 * @param string $text link txt
423 * @param array $attributes extra html attributes
424 * @return string HTML fragment
425 */
426 public static function link($url, $text, array $attributes = null) {
427 $attributes = (array)$attributes;
428 $attributes['href'] = $url;
429 return self::tag('a', $attributes, $text);
430 }
5d0c95a5
PS
431}
432
433
434// ===============================================================================================
435// TODO: Following components will be refactored soon
436
d9c8f425 437/**
7b1f2c82 438 * Base class for classes representing HTML elements, like html_select.
d9c8f425 439 *
440 * Handles the id and class attributes.
441 *
442 * @copyright 2009 Tim Hunt
443 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
444 * @since Moodle 2.0
445 */
6dd7d7f0 446class html_component {
d9c8f425 447 /**
448 * @var string value to use for the id attribute of this HTML tag.
449 */
450 public $id = '';
451 /**
452 * @var string $alt value to use for the alt attribute of this HTML tag.
453 */
454 public $alt = '';
455 /**
456 * @var string $style value to use for the style attribute of this HTML tag.
457 */
458 public $style = '';
459 /**
460 * @var array class names to add to this HTML element.
461 */
462 public $classes = array();
463 /**
464 * @var string $title The title attributes applicable to any XHTML element
465 */
466 public $title = '';
467 /**
468 * An optional array of component_action objects handling the action part of this component.
469 * @var array $actions
470 */
471 protected $actions = array();
d9c8f425 472
8fa16366
PS
473 /**
474 * Compoment constructor.
475 * @param array $options image attributes such as title, id, alt, style, class
476 */
477 public function __construct(array $options = null) {
478 // not implemented in this class because we want to set only public properties of this component
479 renderer_base::apply_component_options($this, $options);
480 }
481
d9c8f425 482 /**
483 * Ensure some class names are an array.
484 * @param mixed $classes either an array of class names or a space-separated
485 * string containing class names.
486 * @return array the class names as an array.
487 */
488 public static function clean_classes($classes) {
642816a6 489 if (empty($classes)) {
3bd6b994 490 return array();
642816a6 491 } else if (is_array($classes)) {
d9c8f425 492 return $classes;
493 } else {
494 return explode(' ', trim($classes));
495 }
496 }
497
498 /**
499 * Set the class name array.
500 * @param mixed $classes either an array of class names or a space-separated
501 * string containing class names.
502 * @return void
503 */
504 public function set_classes($classes) {
505 $this->classes = self::clean_classes($classes);
506 }
507
508 /**
509 * Add a class name to the class names array.
510 * @param string $class the new class name to add.
511 * @return void
512 */
513 public function add_class($class) {
514 $this->classes[] = $class;
515 }
516
517 /**
518 * Add a whole lot of class names to the class names array.
519 * @param mixed $classes either an array of class names or a space-separated
520 * string containing class names.
521 * @return void
522 */
523 public function add_classes($classes) {
eeecf5a7 524 $this->classes = array_merge($this->classes, self::clean_classes($classes));
d9c8f425 525 }
526
527 /**
528 * Get the class names as a string.
529 * @return string the class names as a space-separated string. Ready to be put in the class="" attribute.
530 */
531 public function get_classes_string() {
532 return implode(' ', $this->classes);
533 }
534
535 /**
536 * Perform any cleanup or final processing that should be done before an
34059565
PS
537 * instance of this class is output. This method is supposed to be called
538 * only from renderers.
539 *
540 * @param renderer_base $output output renderer
541 * @param moodle_page $page
542 * @param string $target rendering target
d9c8f425 543 * @return void
544 */
34059565 545 public function prepare(renderer_base $output, moodle_page $page, $target) {
d9c8f425 546 $this->classes = array_unique(self::clean_classes($this->classes));
547 }
548
549 /**
550 * This checks developer do not try to assign a property directly
551 * if we have a setter for it. Otherwise, the property is set as expected.
552 * @param string $name The name of the variable to set
553 * @param mixed $value The value to assign to the variable
554 * @return void
555 */
556 public function __set($name, $value) {
557 if ($name == 'class') {
558 debugging('this way of setting css class has been deprecated. use set_classes() method instead.');
559 $this->set_classes($value);
560 } else {
561 $this->{$name} = $value;
562 }
563 }
564
565 /**
566 * Adds a JS action to this component.
567 * Note: the JS function you write must have only two arguments: (string)event and (object|array)args
568 * If you want to add an instantiated component_action (or one of its subclasses), give the object as the only parameter
569 *
570 * @param mixed $event a DOM event (click, mouseover etc.) or a component_action object
571 * @param string $jsfunction The name of the JS function to call. required if argument 1 is a string (event)
572 * @param array $jsfunctionargs An optional array of JS arguments to pass to the function
573 */
574 public function add_action($event, $jsfunction=null, $jsfunctionargs=array()) {
575 if (empty($this->id)) {
576 $this->generate_id();
577 }
578
579 if ($event instanceof component_action) {
580 $this->actions[] = $event;
581 } else {
582 if (empty($jsfunction)) {
6dd7d7f0 583 throw new coding_exception('html_component::add_action requires a JS function argument if the first argument is a string event');
d9c8f425 584 }
585 $this->actions[] = new component_action($event, $jsfunction, $jsfunctionargs);
586 }
587 }
588
589 /**
590 * Internal method for generating a unique ID for the purpose of event handlers.
591 */
592 protected function generate_id() {
0c868b08 593 $this->id = uniqid(get_class($this));
d9c8f425 594 }
595
596 /**
597 * Returns the array of component_actions.
598 * @return array Component actions
599 */
600 public function get_actions() {
601 return $this->actions;
602 }
603
7a5c78e0 604 /**
605 * Shortcut for adding a JS confirm dialog when the component is clicked.
606 * The message must be a yes/no question.
607 * @param string $message The yes/no confirmation question. If "Yes" is clicked, the original action will occur.
a0ead5eb 608 * @param string $callback The name of a JS function whose scope will be set to the simpleDialog object and have this
5529f787 609 * function's arguments set as this.args.
7a5c78e0 610 * @return void
611 */
5529f787 612 public function add_confirm_action($message, $callback=null) {
613 $this->add_action(new component_action('click', 'confirm_dialog', array('message' => $message, 'callback' => $callback)));
7a5c78e0 614 }
db49be13 615
616 /**
617 * Returns true if this component has an action of the requested type (component_action by default).
618 * @param string $class The class of the action we are looking for
619 * @return boolean True if action is found
620 */
621 public function has_action($class='component_action') {
622 foreach ($this->actions as $action) {
623 if (get_class($action) == $class) {
624 return true;
625 }
626 }
627 return false;
628 }
7a5c78e0 629}
630
34059565 631
6dd7d7f0 632class labelled_html_component extends html_component {
7a5c78e0 633 /**
634 * @var mixed $label The label for that component. String or html_label object
635 */
636 public $label;
637
8fa16366
PS
638 /**
639 * Compoment constructor.
640 * @param array $options image attributes such as title, id, alt, style, class
641 */
642 public function __construct(array $options = null) {
643 parent::__construct($options);
644 }
645
d9c8f425 646 /**
647 * Adds a descriptive label to the component.
648 *
649 * This can be used in two ways:
650 *
651 * <pre>
652 * $component->set_label($elementlabel, $elementid);
653 * // OR
654 * $label = new html_label();
655 * $label->for = $elementid;
656 * $label->text = $elementlabel;
657 * $component->set_label($label);
658 * </pre>
659 *
660 * Use the second form when you need to add additional HTML attributes
661 * to the label and/or JS actions.
662 *
663 * @param mixed $text Either the text of the label or a html_label object
664 * @param text $for The value of the "for" attribute (the associated element's id)
665 * @return void
666 */
667 public function set_label($text, $for=null) {
668 if ($text instanceof html_label) {
669 $this->label = $text;
670 } else if (!empty($text)) {
671 $this->label = new html_label();
672 $this->label->for = $for;
1ae3767a 673 if (empty($for)) {
3cc457db 674 if (empty($this->id)) {
675 $this->generate_id();
676 }
d9c8f425 677 $this->label->for = $this->id;
678 }
679 $this->label->text = $text;
680 }
681 }
682}
683
beb56299 684/// Components representing HTML elements
d9c8f425 685
686/**
beb56299 687 * This class represents a label element
d9c8f425 688 *
beb56299 689 * @copyright 2009 Nicolas Connault
d9c8f425 690 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
691 * @since Moodle 2.0
692 */
6dd7d7f0 693class html_label extends html_component {
d9c8f425 694 /**
beb56299 695 * @var string $text The text to display in the label
d9c8f425 696 */
beb56299 697 public $text;
d9c8f425 698 /**
beb56299 699 * @var string $for The name of the form field this label is associated with
d9c8f425 700 */
beb56299 701 public $for;
702
d9c8f425 703 /**
6dd7d7f0 704 * @see html_component::prepare()
beb56299 705 * @return void
d9c8f425 706 */
34059565 707 public function prepare(renderer_base $output, moodle_page $page, $target) {
beb56299 708 if (empty($this->text)) {
709 throw new coding_exception('html_label must have a $text value.');
710 }
34059565 711 parent::prepare($output, $page, $target);
beb56299 712 }
713}
714
34059565 715
beb56299 716/**
7b1f2c82 717 * This class hold all the information required to describe a <select> menu that
78946b9b 718 * will be printed by {@link core_renderer::select()}. (Or by an overridden
7b1f2c82 719 * version of that method in a subclass.)
beb56299 720 *
7b1f2c82 721 * This component can also hold enough metadata to be used as a popup form. It just
722 * needs a bit more setting up than for a simple menu. See the shortcut methods for
723 * developer-friendly usage.
724 *
725 * All the fields that are not set by the constructor have sensible defaults, so
726 * you only need to set the properties where you want non-default behaviour.
727 *
728 * @copyright 2009 Tim Hunt
beb56299 729 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
730 * @since Moodle 2.0
731 */
7a5c78e0 732class html_select extends labelled_html_component {
d9c8f425 733 /**
7b1f2c82 734 * The html_select object parses an array of options into component objects
735 * @see nested attribute
736 * @var mixed $options the choices to show in the menu. An array $value => $display, of html_select_option or of html_select_optgroup objects.
d9c8f425 737 */
7b1f2c82 738 public $options;
d9c8f425 739 /**
7b1f2c82 740 * @var string $name the name of this form control. That is, the name of the GET/POST
741 * variable that will be set if this select is submitted as part of a form.
d9c8f425 742 */
7b1f2c82 743 public $name;
d9c8f425 744 /**
7b1f2c82 745 * @var string $selectedvalue the option to select initially. Should match one
746 * of the $options array keys. Default none.
d9c8f425 747 */
7b1f2c82 748 public $selectedvalue;
d9c8f425 749 /**
7b1f2c82 750 * Defaults to get_string('choosedots').
751 * Set this to '' if you do not want a 'nothing is selected' option.
752 * This is ignored if the rendertype is 'radio' or 'checkbox'
753 * @var string The label for the 'nothing is selected' option.
d9c8f425 754 */
7b1f2c82 755 public $nothinglabel = null;
d9c8f425 756 /**
7b1f2c82 757 * @var string The value returned by the 'nothing is selected' option. Defaults to 0.
d9c8f425 758 */
7b1f2c82 759 public $nothingvalue = 0;
d9c8f425 760 /**
7b1f2c82 761 * @var boolean set this to true if you want the control to appear disabled.
d9c8f425 762 */
7b1f2c82 763 public $disabled = false;
d9c8f425 764 /**
7b1f2c82 765 * @var integer if non-zero, sets the tabindex attribute on the <select> element. Default 0.
d9c8f425 766 */
7b1f2c82 767 public $tabindex = 0;
d9c8f425 768 /**
7b1f2c82 769 * @var mixed Defaults to false, which means display the select as a dropdown menu.
770 * If true, display this select as a list box whose size is chosen automatically.
771 * If an integer, display as list box of that size.
d9c8f425 772 */
7b1f2c82 773 public $listbox = false;
d9c8f425 774 /**
7b1f2c82 775 * @var integer if you are using $listbox === true to get an automatically
776 * sized list box, the size of the list box will be the number of options,
777 * or this number, whichever is smaller.
d9c8f425 778 */
7b1f2c82 779 public $maxautosize = 10;
d9c8f425 780 /**
7b1f2c82 781 * @var boolean if true, allow multiple selection. Only used if $listbox is true, or if
782 * the select is to be output as checkboxes.
d9c8f425 783 */
7b1f2c82 784 public $multiple = false;
beb56299 785 /**
7b1f2c82 786 * Another way to use nested menu is to prefix optgroup labels with -- and end the optgroup with --
787 * Leave this setting to false if you are using the latter method.
788 * @var boolean $nested if true, uses $options' keys as option headings (optgroup)
beb56299 789 */
7b1f2c82 790 public $nested = false;
791 /**
792 * @var html_form $form An optional html_form component
793 */
794 public $form;
795 /**
4bcc5118 796 * @var help_icon $array help icon params
7b1f2c82 797 */
798 public $helpicon;
799 /**
800 * @var boolean $rendertype How the select element should be rendered: menu or radio (checkbox is just radio + multiple)
801 */
802 public $rendertype = 'menu';
d9c8f425 803
804 /**
6dd7d7f0 805 * @see html_component::prepare()
d9c8f425 806 * @return void
807 */
34059565 808 public function prepare(renderer_base $output, moodle_page $page, $target) {
7b1f2c82 809 global $CFG;
810
811 // name may contain [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading
beb56299 812 if (empty($this->id)) {
7b1f2c82 813 $this->id = 'menu' . str_replace(array('[', ']'), '', $this->name);
814 }
815
816 if (empty($this->classes)) {
817 $this->set_classes(array('menu' . str_replace(array('[', ']'), '', $this->name)));
818 }
819
820 if (is_null($this->nothinglabel)) {
821 $this->nothinglabel = get_string('choosedots');
822 }
823
824 if (!empty($this->label) && !($this->label instanceof html_label)) {
825 $label = new html_label();
826 $label->text = $this->label;
827 $label->for = $this->name;
828 $this->label = $label;
d9c8f425 829 }
7b1f2c82 830
831 $this->add_class('select');
832
833 $this->initialise_options();
34059565 834 parent::prepare($output, $page, $target);
d9c8f425 835 }
836
837 /**
7b1f2c82 838 * This is a shortcut for making a simple select menu. It lets you specify
839 * the options, name and selected option in one line of code.
840 * @param array $options used to initialise {@link $options}.
841 * @param string $name used to initialise {@link $name}.
842 * @param string $selected used to initialise {@link $selected}.
54b16692 843 * @param string $nothinglabel The label for the 'nothing is selected' option. Defaults to "Choose..."
7b1f2c82 844 * @return html_select A html_select object with the three common fields initialised.
d9c8f425 845 */
54b16692 846 public static function make($options, $name, $selected = '', $nothinglabel='choosedots') {
7b1f2c82 847 $menu = new html_select();
848 $menu->options = $options;
849 $menu->name = $name;
850 $menu->selectedvalue = $selected;
851 return $menu;
d9c8f425 852 }
853
854 /**
7b1f2c82 855 * This is a shortcut for making a yes/no select menu.
856 * @param string $name used to initialise {@link $name}.
857 * @param string $selected used to initialise {@link $selected}.
858 * @return html_select A menu initialised with yes/no options.
beb56299 859 */
7b1f2c82 860 public static function make_yes_no($name, $selected) {
861 return self::make(array(0 => get_string('no'), 1 => get_string('yes')), $name, $selected);
862 }
863
d9c8f425 864 /**
7b1f2c82 865 * This is a shortcut for making an hour selector menu.
866 * @param string $type The type of selector (years, months, days, hours, minutes)
867 * @param string $name fieldname
868 * @param int $currenttime A default timestamp in GMT
869 * @param int $step minute spacing
870 * @return html_select A menu initialised with hour options.
d9c8f425 871 */
7b1f2c82 872 public static function make_time_selector($type, $name, $currenttime=0, $step=5) {
873
874 if (!$currenttime) {
875 $currenttime = time();
876 }
877 $currentdate = usergetdate($currenttime);
878 $userdatetype = $type;
879
880 switch ($type) {
881 case 'years':
882 for ($i=1970; $i<=2020; $i++) {
883 $timeunits[$i] = $i;
884 }
885 $userdatetype = 'year';
886 break;
887 case 'months':
888 for ($i=1; $i<=12; $i++) {
889 $timeunits[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B");
890 }
891 $userdatetype = 'month';
1ae3767a 892 $currentdate['month'] = $currentdate['mon'];
7b1f2c82 893 break;
894 case 'days':
895 for ($i=1; $i<=31; $i++) {
896 $timeunits[$i] = $i;
897 }
898 $userdatetype = 'mday';
899 break;
900 case 'hours':
901 for ($i=0; $i<=23; $i++) {
902 $timeunits[$i] = sprintf("%02d",$i);
903 }
904 break;
905 case 'minutes':
906 if ($step != 1) {
907 $currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step;
908 }
909
910 for ($i=0; $i<=59; $i+=$step) {
911 $timeunits[$i] = sprintf("%02d",$i);
912 }
913 break;
914 default:
915 throw new coding_exception("Time type $type is not supported by html_select::make_time_selector().");
916 }
917
918 $timerselector = self::make($timeunits, $name, $currentdate[$userdatetype]);
919 $timerselector->label = new html_label();
1ae3767a 920
921 $timerselector->label->text = get_string(substr($type, 0, -1), 'form');
7b1f2c82 922 $timerselector->label->for = "menu$timerselector->name";
923 $timerselector->label->add_class('accesshide');
924 $timerselector->nothinglabel = '';
925
926 return $timerselector;
927 }
928
929 /**
930 * Given an associative array of type => fieldname and an optional timestamp,
931 * returns an array of html_select components representing date/time selectors.
932 * @param array $selectors Arrays of type => fieldname. Selectors will be returned in the order of the types given
933 * @param int $currenttime A UNIX timestamp
934 * @param int $step minute spacing
935 * @return array Instantiated date/time selectors
936 */
8fa16366 937 public static function make_time_selectors($selectors, $currenttime=0, $step=5) {
7b1f2c82 938 $selects = array();
939 foreach ($selectors as $type => $name) {
940 $selects[] = html_select::make_time_selector($type, $name, $currenttime, $step);
941 }
942 return $selects;
943 }
944
945 /**
946 * This is a shortcut for making a select popup form.
947 * @param mixed $baseurl The target URL, string or moodle_url
948 * @param string $name The variable which this select's options are changing in the URL
949 * @param array $options A list of value-label pairs for the popup list
950 * @param string $formid id for the control. Must be unique on the page. Used in the HTML.
951 * @param string $selected The option that is initially selected
952 * @return html_select A menu initialised as a popup form.
953 */
8fa16366 954 public static function make_popup_form($baseurl, $name, $options, $formid, $selected=null) {
7b1f2c82 955 global $CFG;
956
957 $selectedurl = null;
958
959 if (!($baseurl instanceof moodle_url)) {
960 $baseurl = new moodle_url($baseurl);
961 }
962
963 if (!empty($selected)) {
964 $selectedurl = $baseurl->out(false, array($name => $selected), false);
965 }
966
7b1f2c82 967 // Replace real value by formatted URLs
968 foreach ($options as $value => $label) {
969 $options[$baseurl->out(false, array($name => $value), false)] = $label;
970 unset($options[$value]);
971 }
972
973 $select = self::make($options, 'jump', $selectedurl);
974
975 $select->form = new html_form();
976 $select->form->id = $formid;
977 $select->form->method = 'get';
b65bfc3e 978 $select->form->jssubmitaction = true;
7b1f2c82 979 $select->form->add_class('popupform');
a6855934 980 $select->form->url = new moodle_url('/course/jumpto.php', array('sesskey' => sesskey()));
7b1f2c82 981 $select->form->button->text = get_string('go');
982
983 $select->id = $formid . '_jump';
984
985 $select->add_action('change', 'submit_form_by_id', array('id' => $formid, 'selectid' => $select->id));
986
987 return $select;
988 }
989
990 /**
991 * Override the URLs of the default popup_form, which only supports one base URL
992 * @param array $options value=>label pairs representing select options
993 * @return void
994 */
995 public function override_option_values($options) {
5acf9cd3
PS
996 // TODO: this is ugly hack because components shoudl never touch global $PAGE with the exception in prepare(),
997 // in any case this methods needs to be revisited because it does not make much sense to use the same $menu in
998 // html_select::make_popup_form() and then again in this method!
999 global $PAGE; //TODO: remove
1000
e4c5abdc 1001 $originalcount = count($options);
7b1f2c82 1002 $this->initialise_options();
e4c5abdc 1003 $newcount = count($this->options);
1004 $first = true;
7b1f2c82 1005
1006 reset($options);
1007
1008 foreach ($this->options as $optkey => $optgroup) {
1009 if ($optgroup instanceof html_select_optgroup) {
1010 foreach ($optgroup->options as $key => $option) {
1011 next($options);
1012 $this->options[$optkey]->options[$key]->value = key($options);
1013
1014 $optionurl = new moodle_url(key($options));
1015
5acf9cd3 1016 if ($optionurl->compare($PAGE->url, URL_MATCH_PARAMS)) {
7b1f2c82 1017 $this->options[$optkey]->options[$key]->selected = 'selected';
1018 }
1019 }
1020 next($options);
e4c5abdc 1021 } else if ($optgroup instanceof html_select_option && !($first && $originalcount < $newcount)) {
7b1f2c82 1022 $this->options[$optkey]->value = key($options);
1023 $optionurl = new moodle_url(key($options));
1024
5acf9cd3 1025 if ($optionurl->compare($PAGE->url, URL_MATCH_PARAMS)) {
7b1f2c82 1026 $this->options[$optkey]->selected = 'selected';
1027 }
e4c5abdc 1028 next($options);
7b1f2c82 1029 }
e4c5abdc 1030 $first = false;
7b1f2c82 1031 }
1032 }
1033
1034 /**
1035 * Adds a help icon next to the select menu.
1036 *
7b1f2c82 1037 * <pre>
4bcc5118 1038 * $select->set_help_icon($page, $text, $component);
7b1f2c82 1039 * </pre>
1040 *
4bcc5118 1041 * @param string $helppage Either the keyword that defines a help page or a help_icon object
7b1f2c82 1042 * @param text $text The text of the help icon
4bcc5118 1043 * @param component $component
7b1f2c82 1044 * @param boolean $linktext Whether or not to show text next to the icon
1045 * @return void
1046 */
4bcc5118
PS
1047 public function set_help_icon($helppage='', $text='', $component='moodle') {
1048 if ($helppage) {
1049 $this->helpicon = array('helppage'=>$helppage, 'text'=>$text, 'component'=>$component);
1050 } else {
1051 $this->helpicon = null;
7b1f2c82 1052 }
1053 }
1054
1055 /**
1056 * Parses the $options array and instantiates html_select_option objects in
1057 * the place of the original value => label pairs. This is useful for when you
1058 * need to setup extra html attributes and actions on individual options before
1059 * the component is sent to the renderer
1060 * @return void;
1061 */
1062 public function initialise_options() {
1063 // If options are already instantiated objects, stop here
1064 $firstoption = reset($this->options);
1065 if ($firstoption instanceof html_select_option || $firstoption instanceof html_select_optgroup) {
1066 return;
1067 }
1068
1069 if ($this->rendertype == 'radio' && $this->multiple) {
1070 $this->rendertype = 'checkbox';
1071 }
1072
1073 // If nested is on, or if radio/checkbox rendertype is set, remove the default Choose option
1074 if ($this->nested || $this->rendertype == 'radio' || $this->rendertype == 'checkbox') {
1075 $this->nothinglabel = '';
1076 }
1077
1078 $options = $this->options;
1079
1080 $this->options = array();
1081
1082 if ($this->nested && $this->rendertype != 'menu') {
1083 throw new coding_exception('html_select cannot render nested options as radio buttons or checkboxes.');
1084 } else if ($this->nested) {
1085 foreach ($options as $section => $values) {
1086 $optgroup = new html_select_optgroup();
1087 $optgroup->text = $section;
1088
1089 foreach ($values as $value => $display) {
1090 $option = new html_select_option();
1091 $option->value = s($value);
1092 $option->text = $display;
1093 if ($display === '') {
1094 $option->text = $value;
1095 }
1096
1097 if ((string) $value == (string) $this->selectedvalue ||
1098 (is_array($this->selectedvalue) && in_array($value, $this->selectedvalue))) {
1099 $option->selected = 'selected';
1100 }
1101
1102 $optgroup->options[] = $option;
1103 }
1104
1105 $this->options[] = $optgroup;
1106 }
1107 } else {
1108 $inoptgroup = false;
1109 $optgroup = false;
1110
1111 foreach ($options as $value => $display) {
1112 if ($display == '--') { /// we are ending previous optgroup
1113 // $this->options[] = $optgroup;
1114 $inoptgroup = false;
1115 continue;
1116 } else if (substr($display,0,2) == '--') { /// we are starting a new optgroup
1117 if (!empty($optgroup->options)) {
1118 $this->options[] = $optgroup;
1119 }
1120
1121 $optgroup = new html_select_optgroup();
1122 $optgroup->text = substr($display,2); // stripping the --
1123
1124 $inoptgroup = true; /// everything following will be in an optgroup
1125 continue;
1126
1127 } else {
1128 // Add $nothing option if there are not optgroups
1129 if ($this->nothinglabel && empty($this->options[0]) && !$inoptgroup) {
1130 $nothingoption = new html_select_option();
1131 $nothingoption->value = 0;
1132 if (!empty($this->nothingvalue)) {
1133 $nothingoption->value = $this->nothingvalue;
1134 }
1135 $nothingoption->text = $this->nothinglabel;
1136 $this->options = array($nothingoption) + $this->options;
1137 }
1138
1139 $option = new html_select_option();
1140 $option->text = $display;
1141
1142 if ($display === '') {
1143 $option->text = $value;
1144 }
1145
1146 if ((string) $value == (string) $this->selectedvalue ||
1147 (is_array($this->selectedvalue) && in_array($value, $this->selectedvalue))) {
1148 $option->selected = 'selected';
1149 }
1150
1151 $option->value = s($value);
1152
1153 if ($inoptgroup) {
1154 $optgroup->options[] = $option;
1155 } else {
1156 $this->options[] = $option;
1157 }
1158 }
1159 }
1160
1161 if ($optgroup) {
1162 $this->options[] = $optgroup;
1163 }
1164 }
1165 }
1166}
1167
34059565 1168
7b1f2c82 1169/**
1170 * This class represents a select option element
1171 *
1172 * @copyright 2009 Nicolas Connault
1173 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1174 * @since Moodle 2.0
1175 */
7a5c78e0 1176class html_select_option extends labelled_html_component {
7b1f2c82 1177 /**
1178 * @var string $value The value of this option (will be sent with form)
1179 */
1180 public $value;
1181 /**
1182 * @var string $text The display value of the option
1183 */
1184 public $text;
1185 /**
1186 * @var boolean $selected Whether or not this option is selected
1187 */
1188 public $selected = false;
a4998d01 1189 /**
1190 * @var boolean $disabled Whether or not this option is disabled
1191 */
1192 public $disabled = false;
7b1f2c82 1193
1194 public function __construct() {
1195 $this->label = new html_label();
1196 }
1197
1198 /**
6dd7d7f0 1199 * @see html_component::prepare()
7b1f2c82 1200 * @return void
1201 */
34059565 1202 public function prepare(renderer_base $output, moodle_page $page, $target) {
c7e4c7b7 1203 if (empty($this->text) && (string)$this->text!=='0') {
7b1f2c82 1204 throw new coding_exception('html_select_option requires a $text value.');
1205 }
1206
1207 if (empty($this->label->text)) {
1208 $this->set_label($this->text);
1209 } else if (!($this->label instanceof html_label)) {
1210 $this->set_label($this->label);
1211 }
1212 if (empty($this->id)) {
1213 $this->generate_id();
1214 }
1215
34059565 1216 parent::prepare($output, $page, $target);
7b1f2c82 1217 }
1218
1219 /**
1220 * Shortcut for making a checkbox-ready option
1221 * @param string $value The value of the checkbox
1222 * @param boolean $checked
1223 * @param string $label
1224 * @param string $alt
1225 * @return html_select_option A component ready for $OUTPUT->checkbox()
1226 */
8fa16366 1227 public static function make_checkbox($value, $checked, $label, $alt='') {
7b1f2c82 1228 $checkbox = new html_select_option();
1229 $checkbox->value = $value;
1230 $checkbox->selected = $checked;
1231 $checkbox->text = $label;
1232 $checkbox->label->text = $label;
1233 $checkbox->alt = $alt;
1234 return $checkbox;
1235 }
1236}
1237
34059565 1238
7b1f2c82 1239/**
1240 * This class represents a select optgroup element
1241 *
1242 * @copyright 2009 Nicolas Connault
1243 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1244 * @since Moodle 2.0
1245 */
6dd7d7f0 1246class html_select_optgroup extends html_component {
7b1f2c82 1247 /**
1248 * @var string $text The display value of the optgroup
1249 */
1250 public $text;
1251 /**
1252 * @var array $options An array of html_select_option objects
1253 */
1254 public $options = array();
1255
34059565 1256 public function prepare(renderer_base $output, moodle_page $page, $target) {
7b1f2c82 1257 if (empty($this->text)) {
1258 throw new coding_exception('html_select_optgroup requires a $text value.');
1259 }
1260 if (empty($this->options)) {
1261 throw new coding_exception('html_select_optgroup requires at least one html_select_option object');
1262 }
34059565 1263 parent::prepare($output, $page, $target);
7b1f2c82 1264 }
1265}
1266
34059565 1267
7b1f2c82 1268/**
1269 * This class represents an input field
1270 *
1271 * @copyright 2009 Nicolas Connault
1272 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1273 * @since Moodle 2.0
1274 */
7a5c78e0 1275class html_field extends labelled_html_component {
7b1f2c82 1276 /**
1277 * @var string $name The name attribute of the field
1278 */
1279 public $name;
1280 /**
1281 * @var string $value The value attribute of the field
1282 */
1283 public $value;
1284 /**
1285 * @var string $type The type attribute of the field (text, submit, checkbox etc)
1286 */
1287 public $type;
1288 /**
1289 * @var string $maxlength The maxlength attribute of the field (only applies to text type)
1290 */
1291 public $maxlength;
5fc6d585 1292 /**
1293 * @var boolean $disabled Whether or not this field is disabled
1294 */
1295 public $disabled = false;
7b1f2c82 1296
1297 public function __construct() {
1298 $this->label = new html_label();
1299 }
1300
1301 /**
6dd7d7f0 1302 * @see html_component::prepare()
7b1f2c82 1303 * @return void
1304 */
34059565 1305 public function prepare(renderer_base $output, moodle_page $page, $target) {
7b1f2c82 1306 if (empty($this->style)) {
1307 $this->style = 'width: 4em;';
1308 }
1309 if (empty($this->id)) {
1310 $this->generate_id();
1311 }
34059565 1312 parent::prepare($output, $page, $target);
7b1f2c82 1313 }
1314
1315 /**
1316 * Shortcut for creating a text input component.
1317 * @param string $name The name of the text field
1318 * @param string $value The value of the text field
1319 * @param string $alt The info to be inserted in the alt tag
1320 * @param int $maxlength Sets the maxlength attribute of the field. Not set by default
1321 * @return html_field The field component
1322 */
a0ead5eb 1323 public static function make_text($name='unnamed', $value='', $alt='', $maxlength=0) {
7b1f2c82 1324 $field = new html_field();
1325 if (empty($alt)) {
a0ead5eb 1326 $alt = $name;
7b1f2c82 1327 }
1328 $field->type = 'text';
1329 $field->name = $name;
1330 $field->value = $value;
1331 $field->alt = $alt;
1332 $field->maxlength = $maxlength;
1333 return $field;
1334 }
1335}
1336
34059565 1337
7b1f2c82 1338/**
1339 * Holds all the information required to render a <table> by
78946b9b 1340 * {@see core_renderer::table()} or by an overridden version of that
7b1f2c82 1341 * method in a subclass.
1342 *
1343 * Example of usage:
1344 * $t = new html_table();
1345 * ... // set various properties of the object $t as described below
1346 * echo $OUTPUT->table($t);
1347 *
1348 * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
1349 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1350 * @since Moodle 2.0
1351 */
7a5c78e0 1352class html_table extends labelled_html_component {
7b1f2c82 1353 /**
a0ead5eb 1354 * For more control over the rendering of the headers, an array of html_table_cell objects
54a007e8 1355 * can be passed instead of an array of strings.
7b1f2c82 1356 * @var array of headings. The n-th array item is used as a heading of the n-th column.
1357 *
1358 * Example of usage:
1359 * $t->head = array('Student', 'Grade');
1360 */
1361 public $head;
1362 /**
1363 * @var array can be used to make a heading span multiple columns
1364 *
1365 * Example of usage:
1366 * $t->headspan = array(2,1);
1367 *
1368 * In this example, {@see html_table:$data} is supposed to have three columns. For the first two columns,
1369 * the same heading is used. Therefore, {@see html_table::$head} should consist of two items.
1370 */
1371 public $headspan;
1372 /**
1373 * @var array of column alignments. The value is used as CSS 'text-align' property. Therefore, possible
1374 * values are 'left', 'right', 'center' and 'justify'. Specify 'right' or 'left' from the perspective
1375 * of a left-to-right (LTR) language. For RTL, the values are flipped automatically.
1376 *
beb56299 1377 * Examples of usage:
1378 * $t->align = array(null, 'right');
1379 * or
1380 * $t->align[1] = 'right';
1381 *
d9c8f425 1382 */
beb56299 1383 public $align;
d9c8f425 1384 /**
beb56299 1385 * @var array of column sizes. The value is used as CSS 'size' property.
1386 *
1387 * Examples of usage:
1388 * $t->size = array('50%', '50%');
1389 * or
1390 * $t->size[1] = '120px';
d9c8f425 1391 */
beb56299 1392 public $size;
d9c8f425 1393 /**
beb56299 1394 * @var array of wrapping information. The only possible value is 'nowrap' that sets the
1395 * CSS property 'white-space' to the value 'nowrap' in the given column.
1396 *
1397 * Example of usage:
1398 * $t->wrap = array(null, 'nowrap');
d9c8f425 1399 */
beb56299 1400 public $wrap;
d9c8f425 1401 /**
beb56299 1402 * @var array of arrays or html_table_row objects containing the data. Alternatively, if you have
1403 * $head specified, the string 'hr' (for horizontal ruler) can be used
1404 * instead of an array of cells data resulting in a divider rendered.
d9c8f425 1405 *
beb56299 1406 * Example of usage with array of arrays:
1407 * $row1 = array('Harry Potter', '76 %');
1408 * $row2 = array('Hermione Granger', '100 %');
1409 * $t->data = array($row1, $row2);
d9c8f425 1410 *
beb56299 1411 * Example with array of html_table_row objects: (used for more fine-grained control)
1412 * $cell1 = new html_table_cell();
1413 * $cell1->text = 'Harry Potter';
1414 * $cell1->colspan = 2;
1415 * $row1 = new html_table_row();
1416 * $row1->cells[] = $cell1;
1417 * $cell2 = new html_table_cell();
1418 * $cell2->text = 'Hermione Granger';
1419 * $cell3 = new html_table_cell();
1420 * $cell3->text = '100 %';
1421 * $row2 = new html_table_row();
1422 * $row2->cells = array($cell2, $cell3);
1423 * $t->data = array($row1, $row2);
1424 */
1425 public $data;
1426 /**
1427 * @var string width of the table, percentage of the page preferred. Defaults to 80% of the page width.
1428 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1429 */
1430 public $width = null;
1431 /**
1432 * @var string alignment the whole table. Can be 'right', 'left' or 'center' (default).
1433 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1434 */
1435 public $tablealign = null;
1436 /**
1437 * @var int padding on each cell, in pixels
1438 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1439 */
1440 public $cellpadding = null;
1441 /**
1442 * @var int spacing between cells, in pixels
1443 * @deprecated since Moodle 2.0. Styling should be in the CSS.
1444 */
1445 public $cellspacing = null;
1446 /**
1447 * @var array classes to add to particular rows, space-separated string.
1448 * Classes 'r0' or 'r1' are added automatically for every odd or even row,
1449 * respectively. Class 'lastrow' is added automatically for the last row
1450 * in the table.
d9c8f425 1451 *
beb56299 1452 * Example of usage:
1453 * $t->rowclasses[9] = 'tenth'
1454 */
1455 public $rowclasses;
1456 /**
1457 * @var array classes to add to every cell in a particular column,
1458 * space-separated string. Class 'cell' is added automatically by the renderer.
1459 * Classes 'c0' or 'c1' are added automatically for every odd or even column,
1460 * respectively. Class 'lastcol' is added automatically for all last cells
1461 * in a row.
d9c8f425 1462 *
beb56299 1463 * Example of usage:
1464 * $t->colclasses = array(null, 'grade');
d9c8f425 1465 */
beb56299 1466 public $colclasses;
1467 /**
1468 * @var string description of the contents for screen readers.
1469 */
1470 public $summary;
1471 /**
1472 * @var bool true causes the contents of the heading cells to be rotated 90 degrees.
1473 */
1474 public $rotateheaders = false;
319770d7 1475 /**
1476 * @var array $headclasses Array of CSS classes to apply to the table's thead.
1477 */
1478 public $headclasses = array();
1479 /**
1480 * @var array $bodyclasses Array of CSS classes to apply to the table's tbody.
1481 */
1482 public $bodyclasses = array();
1483 /**
1484 * @var array $footclasses Array of CSS classes to apply to the table's tfoot.
1485 */
1486 public $footclasses = array();
a0ead5eb 1487
d9c8f425 1488
1489 /**
6dd7d7f0 1490 * @see html_component::prepare()
beb56299 1491 * @return void
d9c8f425 1492 */
34059565 1493 public function prepare(renderer_base $output, moodle_page $page, $target) {
beb56299 1494 if (!empty($this->align)) {
1495 foreach ($this->align as $key => $aa) {
1496 if ($aa) {
1497 $this->align[$key] = 'text-align:'. fix_align_rtl($aa) .';'; // Fix for RTL languages
1498 } else {
1499 $this->align[$key] = '';
1500 }
1501 }
d9c8f425 1502 }
beb56299 1503 if (!empty($this->size)) {
1504 foreach ($this->size as $key => $ss) {
1505 if ($ss) {
1506 $this->size[$key] = 'width:'. $ss .';';
1507 } else {
1508 $this->size[$key] = '';
1509 }
1510 }
d9c8f425 1511 }
beb56299 1512 if (!empty($this->wrap)) {
1513 foreach ($this->wrap as $key => $ww) {
1514 if ($ww) {
1515 $this->wrap[$key] = 'white-space:nowrap;';
1516 } else {
1517 $this->wrap[$key] = '';
1518 }
1519 }
d9c8f425 1520 }
beb56299 1521 if (!empty($this->head)) {
1522 foreach ($this->head as $key => $val) {
1523 if (!isset($this->align[$key])) {
1524 $this->align[$key] = '';
1525 }
1526 if (!isset($this->size[$key])) {
1527 $this->size[$key] = '';
1528 }
1529 if (!isset($this->wrap[$key])) {
1530 $this->wrap[$key] = '';
d9c8f425 1531 }
1532
d9c8f425 1533 }
beb56299 1534 }
1535 if (empty($this->classes)) { // must be done before align
1536 $this->set_classes(array('generaltable'));
1537 }
1538 if (!empty($this->tablealign)) {
1539 $this->add_class('boxalign' . $this->tablealign);
1540 }
1541 if (!empty($this->rotateheaders)) {
1542 $this->add_class('rotateheaders');
d9c8f425 1543 } else {
beb56299 1544 $this->rotateheaders = false; // Makes life easier later.
1545 }
34059565 1546 parent::prepare($output, $page, $target);
beb56299 1547 }
1548 /**
1549 * @param string $name The name of the variable to set
1550 * @param mixed $value The value to assign to the variable
1551 * @return void
1552 */
1553 public function __set($name, $value) {
1554 if ($name == 'rowclass') {
1555 debugging('rowclass[] has been deprecated for html_table ' .
7b1f2c82 1556 'and should be replaced with rowclasses[]. please fix the code.');
1557 $this->rowclasses = $value;
1558 } else {
1559 parent::__set($name, $value);
d9c8f425 1560 }
d9c8f425 1561 }
d9c8f425 1562}
1563
34059565 1564
d9c8f425 1565/**
7b1f2c82 1566 * Component representing a table row.
d9c8f425 1567 *
1568 * @copyright 2009 Nicolas Connault
1569 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1570 * @since Moodle 2.0
1571 */
6dd7d7f0 1572class html_table_row extends html_component {
d9c8f425 1573 /**
7b1f2c82 1574 * @var array $cells Array of html_table_cell objects
d9c8f425 1575 */
7b1f2c82 1576 public $cells = array();
d9c8f425 1577
beb56299 1578 /**
6dd7d7f0 1579 * @see lib/html_component#prepare()
beb56299 1580 * @return void
1581 */
34059565
PS
1582 public function prepare(renderer_base $output, moodle_page $page, $target) {
1583 parent::prepare($output, $page, $target);
d9c8f425 1584 }
a0ead5eb 1585
54a007e8 1586 /**
a019627a 1587 * Shortcut method for creating a row with an array of cells. Converts cells to html_table_cell objects.
54a007e8 1588 * @param array $cells
1589 * @return html_table_row
1590 */
8fa16366 1591 public static function make($cells=array()) {
54a007e8 1592 $row = new html_table_row();
a019627a 1593 foreach ($cells as $celltext) {
1594 if (!($celltext instanceof html_table_cell)) {
1595 $cell = new html_table_cell();
1596 $cell->text = $celltext;
1597 $row->cells[] = $cell;
1598 } else {
1599 $row->cells[] = $celltext;
1600 }
1601 }
54a007e8 1602 return $row;
1603 }
d9c8f425 1604}
1605
34059565 1606
d9c8f425 1607/**
7b1f2c82 1608 * Component representing a table cell.
d9c8f425 1609 *
1610 * @copyright 2009 Nicolas Connault
1611 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1612 * @since Moodle 2.0
1613 */
6dd7d7f0 1614class html_table_cell extends html_component {
d9c8f425 1615 /**
7b1f2c82 1616 * @var string $text The contents of the cell
d9c8f425 1617 */
7b1f2c82 1618 public $text;
d9c8f425 1619 /**
7b1f2c82 1620 * @var string $abbr Abbreviated version of the contents of the cell
d9c8f425 1621 */
7b1f2c82 1622 public $abbr = '';
d9c8f425 1623 /**
7b1f2c82 1624 * @var int $colspan Number of columns this cell should span
d9c8f425 1625 */
7b1f2c82 1626 public $colspan = '';
d9c8f425 1627 /**
7b1f2c82 1628 * @var int $rowspan Number of rows this cell should span
d9c8f425 1629 */
7b1f2c82 1630 public $rowspan = '';
d9c8f425 1631 /**
7b1f2c82 1632 * @var string $scope Defines a way to associate header cells and data cells in a table
d9c8f425 1633 */
7b1f2c82 1634 public $scope = '';
1ae3767a 1635 /**
1636 * @var boolean $header Whether or not this cell is a header cell
1637 */
a4998d01 1638 public $header = null;
d9c8f425 1639
1640 /**
6dd7d7f0 1641 * @see lib/html_component#prepare()
d9c8f425 1642 * @return void
1643 */
34059565 1644 public function prepare(renderer_base $output, moodle_page $page, $target) {
54a007e8 1645 if ($this->header && empty($this->scope)) {
1646 $this->scope = 'col';
1647 }
34059565 1648 parent::prepare($output, $page, $target);
d9c8f425 1649 }
1650}
1651
34059565 1652
d9c8f425 1653/**
7b1f2c82 1654 * Component representing a XHTML link.
d9c8f425 1655 *
beb56299 1656 * @copyright 2009 Nicolas Connault
d9c8f425 1657 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1658 * @since Moodle 2.0
1659 */
6dd7d7f0 1660class html_link extends html_component {
d9c8f425 1661 /**
7b1f2c82 1662 * URL can be simple text or a moodle_url object
1663 * @var mixed $url
d9c8f425 1664 */
beb56299 1665 public $url;
d9c8f425 1666
1667 /**
5d77fc1d 1668 * @var string $text The HTML text that will appear between the link tags
d9c8f425 1669 */
5d77fc1d 1670 public $text = '';
d9c8f425 1671
1672 /**
a0ead5eb 1673 * @var boolean $disabled Whether or not this link is disabled (will be rendered as plain text)
1674 */
1675 public $disabled = false;
1676
284943fc 1677 /**
1678 * @var boolean $disableifcurrent Whether or not this link should be disabled if it the same as the current page
1679 */
1680 public $disableifcurrent = false;
1681
5d77fc1d
PS
1682 /**
1683 * New link constructor.
1684 *
1685 * @param moodle_url|string $url url of the image
1686 * @param array $options link attributes such as title, id, disabled, disableifcurrent, etc.
1687 */
1688 public function __construct($url = null, $text = '', array $options = null) {
1689 parent::__construct($options);
1690
1691 if (is_null($url)) {
1692 // to be filled later
1693
1694 } else if ($url instanceof moodle_url) {
4bcc5118 1695 $this->url = clone($url);
5d77fc1d
PS
1696
1697 } else if (is_string($url)) {
4bcc5118 1698 $this->url = new moodle_url($url);
5d77fc1d
PS
1699
1700 } else {
1701 throw new coding_style_exception('Image can be constructed only from moodle_url or string url.');
1702 }
1703
1704 $this->text = $text;
1705 }
1706
a0ead5eb 1707 /**
6dd7d7f0 1708 * @see lib/html_component#prepare() Disables the link if it links to the current page.
beb56299 1709 * @return void
d9c8f425 1710 */
34059565 1711 public function prepare(renderer_base $output, moodle_page $page, $target) {
7b1f2c82 1712 // We can't accept an empty text value
5d77fc1d 1713 if ($this->text === '' or is_null($this->text)) { // 0 is valid value, do not use empty()
7b1f2c82 1714 throw new coding_exception('A html_link must have a descriptive text value!');
beb56299 1715 }
1716
a0ead5eb 1717 if (!($this->url instanceof moodle_url)) {
1718 $this->url = new moodle_url($this->url);
1719 }
1720
5d77fc1d 1721 if ($this->disableifcurrent and $this->url->compare($page->url, URL_MATCH_PARAMS)) {
a0ead5eb 1722 $this->disabled = true;
1723 }
5d77fc1d 1724
34059565 1725 parent::prepare($output, $page, $target);
beb56299 1726 }
d9c8f425 1727
1728 /**
7b1f2c82 1729 * Shortcut for creating a link component.
1730 * @param mixed $url String or moodle_url
1731 * @param string $text The text of the link
1732 * @return html_link The link component
d9c8f425 1733 */
8fa16366 1734 public static function make($url, $text) {
5d77fc1d 1735 return new html_link($url, $text);
d9c8f425 1736 }
1737}
1738
34059565 1739
d9c8f425 1740/**
7b1f2c82 1741 * Component representing a XHTML button (input of type 'button').
1742 * The renderer will either output it as a button with an onclick event,
1743 * or as a form with hidden inputs.
d9c8f425 1744 *
beb56299 1745 * @copyright 2009 Nicolas Connault
d9c8f425 1746 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1747 * @since Moodle 2.0
1748 */
7a5c78e0 1749class html_button extends labelled_html_component {
d9c8f425 1750 /**
7b1f2c82 1751 * @var string $text
d9c8f425 1752 */
7b1f2c82 1753 public $text;
d9c8f425 1754
d9c8f425 1755 /**
7b1f2c82 1756 * @var boolean $disabled Whether or not this button is disabled
d9c8f425 1757 */
beb56299 1758 public $disabled = false;
7b1f2c82 1759
d9c8f425 1760 /**
6dd7d7f0 1761 * @see lib/html_component#prepare()
7b1f2c82 1762 * @return void
d9c8f425 1763 */
34059565 1764 public function prepare(renderer_base $output, moodle_page $page, $target) {
7b1f2c82 1765 $this->add_class('singlebutton');
1766
1767 if (empty($this->text)) {
b65bfc3e 1768 $this->text = get_string('submit');
7b1f2c82 1769 }
1770
1771 if ($this->disabled) {
1772 $this->disabled = 'disabled';
1773 }
1774
34059565 1775 parent::prepare($output, $page, $target);
7b1f2c82 1776 }
1777}
1778
1779/**
1780 * Component representing an image.
1781 *
1782 * @copyright 2009 Nicolas Connault
1783 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1784 * @since Moodle 2.0
1785 */
7a5c78e0 1786class html_image extends labelled_html_component {
d9c8f425 1787 /**
7b1f2c82 1788 * @var string $src The path to the image being used
d9c8f425 1789 */
7b1f2c82 1790 public $src;
1ba862ec
PS
1791 /**
1792 * @var int $width of image
1793 */
1794 public $width;
1795 /**
1796 * @var int $height of image
1797 */
1798 public $height;
7b1f2c82 1799
8fa16366
PS
1800 /**
1801 * New image constructor.
1802 *
1803 * @param moodle_url|string $url url of the image
1804 * @param array $options image attributes such as title, id, alt, widht, height
1805 */
4bcc5118 1806 public function __construct($src = null, array $options = null) {
8fa16366
PS
1807 parent::__construct($options);
1808
4bcc5118 1809 if (is_null($src)) {
8fa16366
PS
1810 // to be filled later
1811
4bcc5118
PS
1812 } else if ($src instanceof moodle_url) {
1813 $this->src = clone($src);
8fa16366 1814
4bcc5118
PS
1815 } else if (is_string($src)) {
1816 $this->src = new moodle_url($src);
8fa16366
PS
1817
1818 } else {
1819 throw new coding_style_exception('Image can be constructed only from moodle_url or string url.');
1820 }
1821 }
1822
d9c8f425 1823 /**
6dd7d7f0 1824 * @see lib/html_component#prepare()
7b1f2c82 1825 * @return void
d9c8f425 1826 */
34059565 1827 public function prepare(renderer_base $output, moodle_page $page, $target) {
b65bfc3e 1828 if (empty($this->src)) {
1829 throw new coding_exception('html_image requires a $src value (moodle_url).');
1830 }
1831
1ba862ec 1832 // no general class here, use custom class instead or img element directly in css selectors
34059565 1833 parent::prepare($output, $page, $target);
8fa16366
PS
1834
1835 if ($this->alt === '') {
1836 // needs to be set for accessibility reasons
1837 $this->alt = HTML_ATTR_EMPTY;
1838 }
7b1f2c82 1839 }
1840}
1841
34059565 1842
7b1f2c82 1843/**
1844 * Component representing a textarea.
1845 *
1846 * @copyright 2009 Nicolas Connault
1847 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1848 * @since Moodle 2.0
1849 */
6dd7d7f0 1850class html_textarea extends html_component {
d9c8f425 1851 /**
7b1f2c82 1852 * @param string $name Name to use for the textarea element.
d9c8f425 1853 */
7b1f2c82 1854 public $name;
d9c8f425 1855 /**
7b1f2c82 1856 * @param string $value Initial content to display in the textarea.
d9c8f425 1857 */
7b1f2c82 1858 public $value;
d9c8f425 1859 /**
7b1f2c82 1860 * @param int $rows Number of rows to display (minimum of 10 when $height is non-null)
d9c8f425 1861 */
7b1f2c82 1862 public $rows;
beb56299 1863 /**
7b1f2c82 1864 * @param int $cols Number of columns to display (minimum of 65 when $width is non-null)
beb56299 1865 */
7b1f2c82 1866 public $cols;
1867 /**
1868 * @param bool $usehtmleditor Enables the use of the htmleditor for this field.
1869 */
1870 public $usehtmleditor;
d9c8f425 1871
1872 /**
6dd7d7f0 1873 * @see lib/html_component#prepare()
d9c8f425 1874 * @return void
1875 */
34059565 1876 public function prepare(renderer_base $output, moodle_page $page, $target) {
7b1f2c82 1877 $this->add_class('form-textarea');
d9c8f425 1878
beb56299 1879 if (empty($this->id)) {
7b1f2c82 1880 $this->id = "edit-$this->name";
d9c8f425 1881 }
beb56299 1882
7b1f2c82 1883 if ($this->usehtmleditor) {
1884 editors_head_setup();
1885 $editor = get_preferred_texteditor(FORMAT_HTML);
1886 $editor->use_editor($this->id, array('legacy'=>true));
1887 $this->value = htmlspecialchars($value);
d9c8f425 1888 }
beb56299 1889
34059565 1890 parent::prepare($output, $page, $target);
d9c8f425 1891 }
7b1f2c82 1892}
beb56299 1893
34059565 1894
7b1f2c82 1895/**
1896 * Component representing a simple form wrapper. Its purpose is mainly to enclose
1897 * a submit input with the appropriate action and hidden inputs.
1898 *
1899 * @copyright 2009 Nicolas Connault
1900 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1901 * @since Moodle 2.0
1902 */
6dd7d7f0 1903class html_form extends html_component {
d9c8f425 1904 /**
7b1f2c82 1905 * @var string $method post or get
d9c8f425 1906 */
7b1f2c82 1907 public $method = 'post';
d9c8f425 1908 /**
7b1f2c82 1909 * If a string is given, it will be converted to a moodle_url during prepare()
1910 * @var mixed $url A moodle_url including params or a string
d9c8f425 1911 */
7b1f2c82 1912 public $url;
7b1f2c82 1913 /**
1914 * @var boolean $showbutton If true, the submit button will always be shown even if JavaScript is available
1915 */
1916 public $showbutton = false;
1917 /**
1918 * @var string $targetwindow The name of the target page to open the linked page in.
1919 */
1920 public $targetwindow = 'self';
1921 /**
1922 * @var html_button $button A submit button
1923 */
1924 public $button;
b65bfc3e 1925 /**
1926 * @var boolean $jssubmitaction If true, the submit button will be hidden when JS is enabled
1927 */
1928 public $jssubmitaction = false;
d9c8f425 1929 /**
7b1f2c82 1930 * Constructor: sets up the other components in case they are needed
1931 * @return void
d9c8f425 1932 */
3cd5305f
PS
1933 public function __construct(array $options = null) {
1934 parent::__construct($options);
7b1f2c82 1935 $this->button = new html_button();
d894edd4 1936 $this->button->text = get_string('go');
beb56299 1937 }
1938
d9c8f425 1939 /**
6dd7d7f0 1940 * @see lib/html_component#prepare()
7b1f2c82 1941 * @return void
d9c8f425 1942 */
34059565 1943 public function prepare(renderer_base $output, moodle_page $page, $target) {
beb56299 1944
7b1f2c82 1945 if (empty($this->url)) {
1946 throw new coding_exception('A html_form must have a $url value (string or moodle_url).');
beb56299 1947 }
1948
d894edd4
PS
1949 if (is_string($this->url)) {
1950 $this->url = new moodle_url($this->url);
beb56299 1951 }
1952
7b1f2c82 1953 if ($this->method == 'post') {
d894edd4 1954 // automatic CSRF protection
7b1f2c82 1955 $this->url->param('sesskey', sesskey());
beb56299 1956 }
d9c8f425 1957
34059565 1958 parent::prepare($output, $page, $target);
7b1f2c82 1959 }
d894edd4
PS
1960}
1961
1962
7b1f2c82 1963/**
1964 * Component representing a list.
1965 *
1966 * The advantage of using this object instead of a flat array is that you can load it
1967 * with metadata (CSS classes, event handlers etc.) which can be used by the renderers.
1968 *
1969 * @copyright 2009 Nicolas Connault
1970 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1971 * @since Moodle 2.0
1972 */
6dd7d7f0 1973class html_list extends html_component {
d9c8f425 1974
7b1f2c82 1975 /**
1976 * @var array $items An array of html_list_item or html_list objects
1977 */
1978 public $items = array();
d9c8f425 1979
7b1f2c82 1980 /**
1981 * @var string $type The type of list (ordered|unordered), definition type not yet supported
1982 */
1983 public $type = 'unordered';
d9c8f425 1984
b65bfc3e 1985 /**
1986 * @var string $text An optional descriptive text for the list. Will be output as a list item before opening the new list
1987 */
1988 public $text = false;
1989
7b1f2c82 1990 /**
6dd7d7f0 1991 * @see lib/html_component#prepare()
7b1f2c82 1992 * @return void
1993 */
34059565
PS
1994 public function prepare(renderer_base $output, moodle_page $page, $target) {
1995 parent::prepare($output, $page, $target);
d9c8f425 1996 }
1997
1998 /**
7b1f2c82 1999 * This function takes a nested array of data and maps it into this list's $items array
2000 * as proper html_list_item and html_list objects, with appropriate metadata.
2001 *
2002 * @param array $tree A nested array (array keys are ignored);
2003 * @param int $row Used in identifying the iteration level and in ul classes
beb56299 2004 * @return void
d9c8f425 2005 */
7b1f2c82 2006 public function load_data($tree, $level=0) {
beb56299 2007
7b1f2c82 2008 $this->add_class("list-$level");
beb56299 2009
b65bfc3e 2010 $i = 1;
7b1f2c82 2011 foreach ($tree as $key => $element) {
2012 if (is_array($element)) {
2013 $newhtmllist = new html_list();
b65bfc3e 2014 $newhtmllist->type = $this->type;
7b1f2c82 2015 $newhtmllist->load_data($element, $level + 1);
b65bfc3e 2016 $newhtmllist->text = $key;
7b1f2c82 2017 $this->items[] = $newhtmllist;
2018 } else {
2019 $listitem = new html_list_item();
2020 $listitem->value = $element;
b65bfc3e 2021 $listitem->add_class("list-item-$level-$i");
7b1f2c82 2022 $this->items[] = $listitem;
beb56299 2023 }
b65bfc3e 2024 $i++;
beb56299 2025 }
2026 }
d9c8f425 2027
2028 /**
7b1f2c82 2029 * Adds a html_list_item or html_list to this list.
2030 * If the param is a string, a html_list_item will be added.
2031 * @param mixed $item String, html_list or html_list_item object
d9c8f425 2032 * @return void
2033 */
7b1f2c82 2034 public function add_item($item) {
2035 if ($item instanceof html_list_item || $item instanceof html_list) {
2036 $this->items[] = $item;
2037 } else {
2038 $listitem = new html_list_item();
2039 $listitem->value = $item;
2040 $this->items[] = $item;
beb56299 2041 }
d9c8f425 2042 }
7b1f2c82 2043}
d9c8f425 2044
34059565 2045
7b1f2c82 2046/**
2047 * Component representing a list item.
2048 *
2049 * @copyright 2009 Nicolas Connault
2050 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2051 * @since Moodle 2.0
2052 */
6dd7d7f0 2053class html_list_item extends html_component {
d9c8f425 2054 /**
7b1f2c82 2055 * @var string $value The value of the list item
d9c8f425 2056 */
7b1f2c82 2057 public $value;
d9c8f425 2058
7b1f2c82 2059 /**
6dd7d7f0 2060 * @see lib/html_component#prepare()
7b1f2c82 2061 * @return void
2062 */
34059565
PS
2063 public function prepare(renderer_base $output, moodle_page $page, $target) {
2064 parent::prepare($output, $page, $target);
d9c8f425 2065 }
2066}
2067
34059565 2068
54a007e8 2069/**
a0ead5eb 2070 * Component representing a span element. It has no special attributes, so
54a007e8 2071 * it is very low-level and can be used for styling and JS actions.
2072 *
2073 * @copyright 2009 Nicolas Connault
2074 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2075 * @since Moodle 2.0
2076 */
6dd7d7f0 2077class html_span extends html_component {
54a007e8 2078 /**
2079 * @var string $text The contents of the span
2080 */
2081 public $contents;
2082 /**
6dd7d7f0 2083 * @see lib/html_component#prepare()
54a007e8 2084 * @return void
2085 */
34059565
PS
2086 public function prepare(renderer_base $output, moodle_page $page, $target) {
2087 parent::prepare($output, $page, $target);
54a007e8 2088 }
2089}
2090
7b1f2c82 2091/// Complex components aggregating simpler components
2092
34059565 2093
d9c8f425 2094/**
beb56299 2095 * Component representing a paging bar.
d9c8f425 2096 *
2097 * @copyright 2009 Nicolas Connault
2098 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2099 * @since Moodle 2.0
2100 */
6dd7d7f0 2101class moodle_paging_bar extends html_component {
d9c8f425 2102 /**
beb56299 2103 * @var int $maxdisplay The maximum number of pagelinks to display
d9c8f425 2104 */
beb56299 2105 public $maxdisplay = 18;
d9c8f425 2106 /**
beb56299 2107 * @var int $totalcount post or get
d9c8f425 2108 */
beb56299 2109 public $totalcount;
d9c8f425 2110 /**
beb56299 2111 * @var int $page The page you are currently viewing
d9c8f425 2112 */
beb56299 2113 public $page = 0;
d9c8f425 2114 /**
beb56299 2115 * @var int $perpage The number of entries that should be shown per page
d9c8f425 2116 */
beb56299 2117 public $perpage;
d9c8f425 2118 /**
beb56299 2119 * @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.
2120 * If this is a moodle_url object then the pagevar param will be replaced by the page no, for each page.
d9c8f425 2121 */
beb56299 2122 public $baseurl;
d9c8f425 2123 /**
beb56299 2124 * @var string $pagevar This is the variable name that you use for the page number in your code (ie. 'tablepage', 'blogpage', etc)
d9c8f425 2125 */
beb56299 2126 public $pagevar = 'page';
beb56299 2127 /**
2128 * @var html_link $previouslink A HTML link representing the "previous" page
2129 */
2130 public $previouslink = null;
2131 /**
2132 * @var html_link $nextlink A HTML link representing the "next" page
2133 */
2134 public $nextlink = null;
2135 /**
2136 * @var html_link $firstlink A HTML link representing the first page
2137 */
2138 public $firstlink = null;
2139 /**
2140 * @var html_link $lastlink A HTML link representing the last page
2141 */
2142 public $lastlink = null;
2143 /**
2144 * @var array $pagelinks An array of html_links. One of them is just a string: the current page
2145 */
2146 public $pagelinks = array();
d9c8f425 2147
2148 /**
6dd7d7f0 2149 * @see lib/html_component#prepare()
d9c8f425 2150 * @return void
2151 */
34059565 2152 public function prepare(renderer_base $output, moodle_page $page, $target) {
1c1f64a2 2153 if (!isset($this->totalcount) || is_null($this->totalcount)) {
beb56299 2154 throw new coding_exception('moodle_paging_bar requires a totalcount value.');
2155 }
2156 if (!isset($this->page) || is_null($this->page)) {
2157 throw new coding_exception('moodle_paging_bar requires a page value.');
2158 }
2159 if (empty($this->perpage)) {
2160 throw new coding_exception('moodle_paging_bar requires a perpage value.');
2161 }
2162 if (empty($this->baseurl)) {
2163 throw new coding_exception('moodle_paging_bar requires a baseurl value.');
2164 }
2165 if (!($this->baseurl instanceof moodle_url)) {
2166 $this->baseurl = new moodle_url($this->baseurl);
2167 }
d9c8f425 2168
beb56299 2169 if ($this->totalcount > $this->perpage) {
2170 $pagenum = $this->page - 1;
d9c8f425 2171
beb56299 2172 if ($this->page > 0) {
2173 $this->previouslink = new html_link();
2174 $this->previouslink->add_class('previous');
2175 $this->previouslink->url = clone($this->baseurl);
2176 $this->previouslink->url->param($this->pagevar, $pagenum);
2177 $this->previouslink->text = get_string('previous');
2178 }
d9c8f425 2179
beb56299 2180 if ($this->perpage > 0) {
2181 $lastpage = ceil($this->totalcount / $this->perpage);
2182 } else {
2183 $lastpage = 1;
2184 }
2185
2186 if ($this->page > 15) {
2187 $startpage = $this->page - 10;
2188
2189 $this->firstlink = new html_link();
2190 $this->firstlink->url = clone($this->baseurl);
2191 $this->firstlink->url->param($this->pagevar, 0);
2192 $this->firstlink->text = 1;
2193 $this->firstlink->add_class('first');
2194 } else {
2195 $startpage = 0;
2196 }
2197
2198 $currpage = $startpage;
2199 $displaycount = $displaypage = 0;
2200
2201 while ($displaycount < $this->maxdisplay and $currpage < $lastpage) {
2202 $displaypage = $currpage + 1;
2203
f43cdceb 2204 if ($this->page == $currpage) {
beb56299 2205 $this->pagelinks[] = $displaypage;
2206 } else {
2207 $pagelink = new html_link();
2208 $pagelink->url = clone($this->baseurl);
2209 $pagelink->url->param($this->pagevar, $currpage);
2210 $pagelink->text = $displaypage;
2211 $this->pagelinks[] = $pagelink;
2212 }
2213
2214 $displaycount++;
2215 $currpage++;
2216 }
2217
2218 if ($currpage < $lastpage) {
2219 $lastpageactual = $lastpage - 1;
2220 $this->lastlink = new html_link();
2221 $this->lastlink->url = clone($this->baseurl);
2222 $this->lastlink->url->param($this->pagevar, $lastpageactual);
2223 $this->lastlink->text = $lastpage;
2224 $this->lastlink->add_class('last');
2225 }
2226
2227 $pagenum = $this->page + 1;
2228
2229 if ($pagenum != $displaypage) {
2230 $this->nextlink = new html_link();
2231 $this->nextlink->url = clone($this->baseurl);
2232 $this->nextlink->url->param($this->pagevar, $pagenum);
2233 $this->nextlink->text = get_string('next');
2234 $this->nextlink->add_class('next');
2235 }
d9c8f425 2236 }
2237 }
d9c8f425 2238
2239 /**
beb56299 2240 * Shortcut for initialising a moodle_paging_bar with only the required params.
2241 *
2242 * @param int $totalcount Thetotal number of entries available to be paged through
2243 * @param int $page The page you are currently viewing
2244 * @param int $perpage The number of entries that should be shown per page
2245 * @param mixed $baseurl If this is a string then it is the url which will be appended with $pagevar, an equals sign and the page number.
2246 * If this is a moodle_url object then the pagevar param will be replaced by the page no, for each page.
d9c8f425 2247 */
8fa16366 2248 public static function make($totalcount, $page, $perpage, $baseurl) {
beb56299 2249 $pagingbar = new moodle_paging_bar();
2250 $pagingbar->totalcount = $totalcount;
2251 $pagingbar->page = $page;
2252 $pagingbar->perpage = $perpage;
2253 $pagingbar->baseurl = $baseurl;
2254 return $pagingbar;
d9c8f425 2255 }
2256}
2257
34059565 2258
d9c8f425 2259/**
beb56299 2260 * This class represents how a block appears on a page.
d9c8f425 2261 *
beb56299 2262 * During output, each block instance is asked to return a block_contents object,
2263 * those are then passed to the $OUTPUT->block function for display.
2264 *
2265 * {@link $contents} should probably be generated using a moodle_block_..._renderer.
2266 *
2267 * Other block-like things that need to appear on the page, for example the
2268 * add new block UI, are also represented as block_contents objects.
2269 *
2270 * @copyright 2009 Tim Hunt
d9c8f425 2271 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2272 * @since Moodle 2.0
2273 */
6dd7d7f0 2274class block_contents extends html_component {
beb56299 2275 /** @var int used to set $skipid. */
2276 protected static $idcounter = 1;
2277
2278 const NOT_HIDEABLE = 0;
2279 const VISIBLE = 1;
2280 const HIDDEN = 2;
2281
d9c8f425 2282 /**
beb56299 2283 * @param integer $skipid All the blocks (or things that look like blocks)
2284 * printed on a page are given a unique number that can be used to construct
2285 * id="" attributes. This is set automatically be the {@link prepare()} method.
2286 * Do not try to set it manually.
d9c8f425 2287 */
beb56299 2288 public $skipid;
d9c8f425 2289
2290 /**
beb56299 2291 * @var integer If this is the contents of a real block, this should be set to
2292 * the block_instance.id. Otherwise this should be set to 0.
2293 */
2294 public $blockinstanceid = 0;
2295
2296 /**
2297 * @var integer if this is a real block instance, and there is a corresponding
2298 * block_position.id for the block on this page, this should be set to that id.
2299 * Otherwise it should be 0.
2300 */
2301 public $blockpositionid = 0;
2302
2303 /**
2304 * @param array $attributes an array of attribute => value pairs that are put on the
2305 * outer div of this block. {@link $id} and {@link $classes} attributes should be set separately.
2306 */
2307 public $attributes = array();
2308
2309 /**
2310 * @param string $title The title of this block. If this came from user input,
2311 * it should already have had format_string() processing done on it. This will
2312 * be output inside <h2> tags. Please do not cause invalid XHTML.
2313 */
2314 public $title = '';
2315
2316 /**
2317 * @param string $content HTML for the content
2318 */
2319 public $content = '';
2320
2321 /**
2322 * @param array $list an alternative to $content, it you want a list of things with optional icons.
2323 */
2324 public $footer = '';
2325
2326 /**
2327 * Any small print that should appear under the block to explain to the
2328 * teacher about the block, for example 'This is a sticky block that was
2329 * added in the system context.'
2330 * @var string
2331 */
2332 public $annotation = '';
2333
2334 /**
2335 * @var integer one of the constants NOT_HIDEABLE, VISIBLE, HIDDEN. Whether
2336 * the user can toggle whether this block is visible.
2337 */
2338 public $collapsible = self::NOT_HIDEABLE;
2339
2340 /**
2341 * A (possibly empty) array of editing controls. Each element of this array
2342 * should be an array('url' => $url, 'icon' => $icon, 'caption' => $caption).
b5d0cafc 2343 * $icon is the icon name. Fed to $OUTPUT->pix_url.
beb56299 2344 * @var array
2345 */
2346 public $controls = array();
2347
2348 /**
6dd7d7f0 2349 * @see html_component::prepare()
d9c8f425 2350 * @return void
2351 */
34059565 2352 public function prepare(renderer_base $output, moodle_page $page, $target) {
beb56299 2353 $this->skipid = self::$idcounter;
2354 self::$idcounter += 1;
2355 $this->add_class('sideblock');
2356 if (empty($this->blockinstanceid) || !strip_tags($this->title)) {
2357 $this->collapsible = self::NOT_HIDEABLE;
2358 }
2359 if ($this->collapsible == self::HIDDEN) {
2360 $this->add_class('hidden');
2361 }
2362 if (!empty($this->controls)) {
2363 $this->add_class('block_with_controls');
2364 }
34059565 2365 parent::prepare($output, $page, $target);
d9c8f425 2366 }
2367}
beb56299 2368
34059565 2369
beb56299 2370/**
2371 * This class represents a target for where a block can go when it is being moved.
2372 *
2373 * This needs to be rendered as a form with the given hidden from fields, and
2374 * clicking anywhere in the form should submit it. The form action should be
2375 * $PAGE->url.
2376 *
2377 * @copyright 2009 Tim Hunt
2378 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2379 * @since Moodle 2.0
2380 */
6dd7d7f0 2381class block_move_target extends html_component {
beb56299 2382 /**
2383 * List of hidden form fields.
2384 * @var array
2385 */
2386 public $url = array();
2387 /**
2388 * List of hidden form fields.
2389 * @var array
2390 */
2391 public $text = '';
2392}