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