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