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