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