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