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