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