Commit | Line | Data |
---|---|---|
d9c8f425 | 1 | <?php |
d9c8f425 | 2 | // This file is part of Moodle - http://moodle.org/ |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | /** | |
18 | * Classes representing HTML elements, used by $OUTPUT methods | |
19 | * | |
20 | * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML | |
21 | * for an overview. | |
22 | * | |
f8129210 | 23 | * @package core |
76be40cc | 24 | * @category output |
9678c7b8 SH |
25 | * @copyright 2009 Tim Hunt |
26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
d9c8f425 | 27 | */ |
28 | ||
78bfb562 | 29 | defined('MOODLE_INTERNAL') || die(); |
5d0c95a5 PS |
30 | |
31 | /** | |
32 | * Interface marking other classes as suitable for renderer_base::render() | |
3d3fae72 | 33 | * |
9678c7b8 | 34 | * @copyright 2010 Petr Skoda (skodak) info@skodak.org |
f8129210 | 35 | * @package core |
76be40cc | 36 | * @category output |
5d0c95a5 PS |
37 | */ |
38 | interface renderable { | |
39 | // intentionally empty | |
40 | } | |
41 | ||
bb496de7 DC |
42 | /** |
43 | * Data structure representing a file picker. | |
44 | * | |
45 | * @copyright 2010 Dongsheng Cai | |
9678c7b8 SH |
46 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
47 | * @since Moodle 2.0 | |
f8129210 | 48 | * @package core |
76be40cc | 49 | * @category output |
bb496de7 DC |
50 | */ |
51 | class file_picker implements renderable { | |
9678c7b8 SH |
52 | |
53 | /** | |
76be40cc | 54 | * @var stdClass An object containing options for the file picker |
9678c7b8 | 55 | */ |
bb496de7 | 56 | public $options; |
9678c7b8 SH |
57 | |
58 | /** | |
59 | * Constructs a file picker object. | |
60 | * | |
61 | * The following are possible options for the filepicker: | |
62 | * - accepted_types (*) | |
63 | * - return_types (FILE_INTERNAL) | |
64 | * - env (filepicker) | |
65 | * - client_id (uniqid) | |
66 | * - itemid (0) | |
67 | * - maxbytes (-1) | |
68 | * - maxfiles (1) | |
69 | * - buttonname (false) | |
70 | * | |
71 | * @param stdClass $options An object containing options for the file picker. | |
72 | */ | |
bb496de7 DC |
73 | public function __construct(stdClass $options) { |
74 | global $CFG, $USER, $PAGE; | |
75 | require_once($CFG->dirroot. '/repository/lib.php'); | |
76 | $defaults = array( | |
77 | 'accepted_types'=>'*', | |
bb496de7 DC |
78 | 'return_types'=>FILE_INTERNAL, |
79 | 'env' => 'filepicker', | |
80 | 'client_id' => uniqid(), | |
81 | 'itemid' => 0, | |
82 | 'maxbytes'=>-1, | |
83 | 'maxfiles'=>1, | |
f50a61fb | 84 | 'buttonname'=>false |
bb496de7 DC |
85 | ); |
86 | foreach ($defaults as $key=>$value) { | |
87 | if (empty($options->$key)) { | |
88 | $options->$key = $value; | |
89 | } | |
90 | } | |
91 | ||
92 | $options->currentfile = ''; | |
93 | if (!empty($options->itemid)) { | |
94 | $fs = get_file_storage(); | |
b0c6dc1c | 95 | $usercontext = context_user::instance($USER->id); |
e4256380 | 96 | if (empty($options->filename)) { |
64f93798 | 97 | if ($files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id DESC', false)) { |
e4256380 DC |
98 | $file = reset($files); |
99 | } | |
100 | } else { | |
64f93798 | 101 | $file = $fs->get_file($usercontext->id, 'user', 'draft', $options->itemid, $options->filepath, $options->filename); |
e4256380 DC |
102 | } |
103 | if (!empty($file)) { | |
ee9a4962 | 104 | $options->currentfile = html_writer::link(moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename()), $file->get_filename()); |
bb496de7 DC |
105 | } |
106 | } | |
107 | ||
bb496de7 DC |
108 | // initilise options, getting files in root path |
109 | $this->options = initialise_filepicker($options); | |
110 | ||
111 | // copying other options | |
112 | foreach ($options as $name=>$value) { | |
98e7ae63 DC |
113 | if (!isset($this->options->$name)) { |
114 | $this->options->$name = $value; | |
115 | } | |
bb496de7 DC |
116 | } |
117 | } | |
118 | } | |
119 | ||
5d0c95a5 | 120 | /** |
bf11293a | 121 | * Data structure representing a user picture. |
5d0c95a5 PS |
122 | * |
123 | * @copyright 2009 Nicolas Connault, 2010 Petr Skoda | |
9678c7b8 SH |
124 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
125 | * @since Modle 2.0 | |
f8129210 | 126 | * @package core |
76be40cc | 127 | * @category output |
5d0c95a5 PS |
128 | */ |
129 | class user_picture implements renderable { | |
130 | /** | |
76be40cc SH |
131 | * @var array List of mandatory fields in user record here. (do not include |
132 | * TEXT columns because it would break SELECT DISTINCT in MSSQL and ORACLE) | |
5d0c95a5 | 133 | */ |
76be40cc | 134 | protected static $fields = array('id', 'picture', 'firstname', 'lastname', 'imagealt', 'email'); |
5d0c95a5 PS |
135 | |
136 | /** | |
76be40cc SH |
137 | * @var stdClass A user object with at least fields all columns specified |
138 | * in $fields array constant set. | |
5d0c95a5 PS |
139 | */ |
140 | public $user; | |
9678c7b8 | 141 | |
5d0c95a5 | 142 | /** |
76be40cc SH |
143 | * @var int The course id. Used when constructing the link to the user's |
144 | * profile, page course id used if not specified. | |
5d0c95a5 PS |
145 | */ |
146 | public $courseid; | |
9678c7b8 | 147 | |
5d0c95a5 | 148 | /** |
76be40cc | 149 | * @var bool Add course profile link to image |
5d0c95a5 PS |
150 | */ |
151 | public $link = true; | |
9678c7b8 | 152 | |
5d0c95a5 | 153 | /** |
76be40cc SH |
154 | * @var int Size in pixels. Special values are (true/1 = 100px) and |
155 | * (false/0 = 35px) | |
9678c7b8 | 156 | * for backward compatibility. |
5d0c95a5 PS |
157 | */ |
158 | public $size = 35; | |
9678c7b8 | 159 | |
5d0c95a5 | 160 | /** |
3d3fae72 | 161 | * @var bool Add non-blank alt-text to the image. |
5d0c95a5 PS |
162 | * Default true, set to false when image alt just duplicates text in screenreaders. |
163 | */ | |
164 | public $alttext = true; | |
9678c7b8 | 165 | |
5d0c95a5 | 166 | /** |
3d3fae72 | 167 | * @var bool Whether or not to open the link in a popup window. |
5d0c95a5 PS |
168 | */ |
169 | public $popup = false; | |
9678c7b8 | 170 | |
5d0c95a5 | 171 | /** |
76be40cc | 172 | * @var string Image class attribute |
5d0c95a5 PS |
173 | */ |
174 | public $class = 'userpicture'; | |
175 | ||
176 | /** | |
177 | * User picture constructor. | |
178 | * | |
9678c7b8 | 179 | * @param stdClass $user user record with at least id, picture, imagealt, firstname and lastname set. |
faa328c3 | 180 | * It is recommended to add also contextid of the user for performance reasons. |
5d0c95a5 PS |
181 | */ |
182 | public function __construct(stdClass $user) { | |
183 | global $DB; | |
184 | ||
5d0c95a5 PS |
185 | if (empty($user->id)) { |
186 | throw new coding_exception('User id is required when printing user avatar image.'); | |
187 | } | |
188 | ||
189 | // only touch the DB if we are missing data and complain loudly... | |
190 | $needrec = false; | |
3a11c09f | 191 | foreach (self::$fields as $field) { |
5d0c95a5 PS |
192 | if (!array_key_exists($field, $user)) { |
193 | $needrec = true; | |
194 | debugging('Missing '.$field.' property in $user object, this is a performance problem that needs to be fixed by a developer. ' | |
195 | .'Please use user_picture::fields() to get the full list of required fields.', DEBUG_DEVELOPER); | |
196 | break; | |
197 | } | |
198 | } | |
199 | ||
200 | if ($needrec) { | |
3a11c09f | 201 | $this->user = $DB->get_record('user', array('id'=>$user->id), self::fields(), MUST_EXIST); |
5d0c95a5 PS |
202 | } else { |
203 | $this->user = clone($user); | |
204 | } | |
205 | } | |
206 | ||
207 | /** | |
1a10840e | 208 | * Returns a list of required user fields, useful when fetching required user info from db. |
f3afba4e PS |
209 | * |
210 | * In some cases we have to fetch the user data together with some other information, | |
211 | * the idalias is useful there because the id would otherwise override the main | |
212 | * id of the result record. Please note it has to be converted back to id before rendering. | |
213 | * | |
5d0c95a5 | 214 | * @param string $tableprefix name of database table prefix in query |
3a11c09f | 215 | * @param array $extrafields extra fields to be included in result (do not include TEXT columns because it would break SELECT DISTINCT in MSSQL and ORACLE) |
f3afba4e | 216 | * @param string $idalias alias of id field |
9958e561 | 217 | * @param string $fieldprefix prefix to add to all columns in their aliases, does not apply to 'id' |
5d0c95a5 PS |
218 | * @return string |
219 | */ | |
9958e561 | 220 | public static function fields($tableprefix = '', array $extrafields = NULL, $idalias = 'id', $fieldprefix = '') { |
3a11c09f PS |
221 | if (!$tableprefix and !$extrafields and !$idalias) { |
222 | return implode(',', self::$fields); | |
5d0c95a5 | 223 | } |
3a11c09f PS |
224 | if ($tableprefix) { |
225 | $tableprefix .= '.'; | |
226 | } | |
227 | $fields = array(); | |
228 | foreach (self::$fields as $field) { | |
229 | if ($field === 'id' and $idalias and $idalias !== 'id') { | |
6f7b89e2 | 230 | $fields[$field] = "$tableprefix$field AS $idalias"; |
3a11c09f | 231 | } else { |
9958e561 DM |
232 | if ($fieldprefix and $field !== 'id') { |
233 | $fields[$field] = "$tableprefix$field AS $fieldprefix$field"; | |
234 | } else { | |
235 | $fields[$field] = "$tableprefix$field"; | |
236 | } | |
3a11c09f PS |
237 | } |
238 | } | |
239 | // add extra fields if not already there | |
240 | if ($extrafields) { | |
241 | foreach ($extrafields as $e) { | |
242 | if ($e === 'id' or isset($fields[$e])) { | |
243 | continue; | |
244 | } | |
5c0d03ea DM |
245 | if ($fieldprefix) { |
246 | $fields[$e] = "$tableprefix$e AS $fieldprefix$e"; | |
247 | } else { | |
248 | $fields[$e] = "$tableprefix$e"; | |
249 | } | |
f3afba4e | 250 | } |
f3afba4e PS |
251 | } |
252 | return implode(',', $fields); | |
5d0c95a5 | 253 | } |
5d0c95a5 | 254 | |
5c0d03ea DM |
255 | /** |
256 | * Extract the aliased user fields from a given record | |
257 | * | |
258 | * Given a record that was previously obtained using {@link self::fields()} with aliases, | |
259 | * this method extracts user related unaliased fields. | |
260 | * | |
261 | * @param stdClass $record containing user picture fields | |
262 | * @param array $extrafields extra fields included in the $record | |
263 | * @param string $idalias alias of the id field | |
264 | * @param string $fieldprefix prefix added to all columns in their aliases, does not apply to 'id' | |
265 | * @return stdClass object with unaliased user fields | |
266 | */ | |
9678c7b8 | 267 | public static function unalias(stdClass $record, array $extrafields = null, $idalias = 'id', $fieldprefix = '') { |
5c0d03ea DM |
268 | |
269 | if (empty($idalias)) { | |
270 | $idalias = 'id'; | |
271 | } | |
272 | ||
273 | $return = new stdClass(); | |
274 | ||
275 | foreach (self::$fields as $field) { | |
276 | if ($field === 'id') { | |
9ecbf801 | 277 | if (property_exists($record, $idalias)) { |
5c0d03ea DM |
278 | $return->id = $record->{$idalias}; |
279 | } | |
280 | } else { | |
9ecbf801 | 281 | if (property_exists($record, $fieldprefix.$field)) { |
5c0d03ea DM |
282 | $return->{$field} = $record->{$fieldprefix.$field}; |
283 | } | |
284 | } | |
285 | } | |
286 | // add extra fields if not already there | |
287 | if ($extrafields) { | |
288 | foreach ($extrafields as $e) { | |
9ecbf801 | 289 | if ($e === 'id' or property_exists($return, $e)) { |
5c0d03ea DM |
290 | continue; |
291 | } | |
292 | $return->{$e} = $record->{$fieldprefix.$e}; | |
293 | } | |
294 | } | |
295 | ||
296 | return $return; | |
871a3ec5 SH |
297 | } |
298 | ||
299 | /** | |
300 | * Works out the URL for the users picture. | |
301 | * | |
302 | * This method is recommended as it avoids costly redirects of user pictures | |
303 | * if requests are made for non-existent files etc. | |
304 | * | |
f8129210 | 305 | * @param moodle_page $page |
871a3ec5 SH |
306 | * @param renderer_base $renderer |
307 | * @return moodle_url | |
308 | */ | |
309 | public function get_url(moodle_page $page, renderer_base $renderer = null) { | |
33dca156 | 310 | global $CFG; |
871a3ec5 SH |
311 | |
312 | if (is_null($renderer)) { | |
313 | $renderer = $page->get_renderer('core'); | |
314 | } | |
315 | ||
871a3ec5 SH |
316 | // Sort out the filename and size. Size is only required for the gravatar |
317 | // implementation presently. | |
318 | if (empty($this->size)) { | |
319 | $filename = 'f2'; | |
320 | $size = 35; | |
321 | } else if ($this->size === true or $this->size == 1) { | |
322 | $filename = 'f1'; | |
323 | $size = 100; | |
33d9f44b PS |
324 | } else if ($this->size > 100) { |
325 | $filename = 'f3'; | |
326 | $size = (int)$this->size; | |
871a3ec5 SH |
327 | } else if ($this->size >= 50) { |
328 | $filename = 'f1'; | |
329 | $size = (int)$this->size; | |
330 | } else { | |
331 | $filename = 'f2'; | |
332 | $size = (int)$this->size; | |
333 | } | |
334 | ||
faa328c3 PS |
335 | $defaulturl = $renderer->pix_url('u/'.$filename); // default image |
336 | ||
337 | if ((!empty($CFG->forcelogin) and !isloggedin()) || | |
338 | (!empty($CFG->forceloginforprofileimage) && (!isloggedin() || isguestuser()))) { | |
339 | // Protect images if login required and not logged in; | |
340 | // also if login is required for profile images and is not logged in or guest | |
341 | // do not use require_login() because it is expensive and not suitable here anyway. | |
342 | return $defaulturl; | |
03636668 | 343 | } |
4125bdc1 | 344 | |
faa328c3 PS |
345 | // First try to detect deleted users - but do not read from database for performance reasons! |
346 | if (!empty($this->user->deleted) or strpos($this->user->email, '@') === false) { | |
347 | // All deleted users should have email replaced by md5 hash, | |
348 | // all active users are expected to have valid email. | |
349 | return $defaulturl; | |
350 | } | |
351 | ||
352 | // Did the user upload a picture? | |
4d254790 | 353 | if ($this->user->picture > 0) { |
faa328c3 PS |
354 | if (!empty($this->user->contextid)) { |
355 | $contextid = $this->user->contextid; | |
356 | } else { | |
357 | $context = context_user::instance($this->user->id, IGNORE_MISSING); | |
358 | if (!$context) { | |
359 | // This must be an incorrectly deleted user, all other users have context. | |
360 | return $defaulturl; | |
361 | } | |
362 | $contextid = $context->id; | |
363 | } | |
364 | ||
871a3ec5 SH |
365 | $path = '/'; |
366 | if (clean_param($page->theme->name, PARAM_THEME) == $page->theme->name) { | |
367 | // We append the theme name to the file path if we have it so that | |
368 | // in the circumstance that the profile picture is not available | |
369 | // when the user actually requests it they still get the profile | |
370 | // picture for the correct theme. | |
371 | $path .= $page->theme->name.'/'; | |
372 | } | |
faa328c3 | 373 | // Set the image URL to the URL for the uploaded file and return. |
4d254790 PS |
374 | $url = moodle_url::make_pluginfile_url($contextid, 'user', 'icon', NULL, $path, $filename); |
375 | $url->param('rev', $this->user->picture); | |
376 | return $url; | |
faa328c3 PS |
377 | } |
378 | ||
4d254790 | 379 | if ($this->user->picture == 0 and !empty($CFG->enablegravatar)) { |
4125bdc1 SH |
380 | // Normalise the size variable to acceptable bounds |
381 | if ($size < 1 || $size > 512) { | |
871a3ec5 SH |
382 | $size = 35; |
383 | } | |
4125bdc1 | 384 | // Hash the users email address |
871a3ec5 | 385 | $md5 = md5(strtolower(trim($this->user->email))); |
4125bdc1 | 386 | // Build a gravatar URL with what we know. |
f127b1a5 MA |
387 | |
388 | // Find the best default image URL we can (MDL-35669) | |
389 | if (empty($CFG->gravatardefaulturl)) { | |
390 | $absoluteimagepath = $page->theme->resolve_image_location('u/'.$filename, 'core'); | |
391 | if (strpos($absoluteimagepath, $CFG->dirroot) === 0) { | |
392 | $gravatardefault = $CFG->wwwroot . substr($absoluteimagepath, strlen($CFG->dirroot)); | |
393 | } else { | |
394 | $gravatardefault = $CFG->wwwroot . '/pix/u/' . $filename . '.png'; | |
395 | } | |
396 | } else { | |
397 | $gravatardefault = $CFG->gravatardefaulturl; | |
398 | } | |
399 | ||
4125bdc1 SH |
400 | // If the currently requested page is https then we'll return an |
401 | // https gravatar page. | |
33dca156 | 402 | if (strpos($CFG->httpswwwroot, 'https:') === 0) { |
b96a15cc | 403 | $gravatardefault = str_replace($CFG->wwwroot, $CFG->httpswwwroot, $gravatardefault); // Replace by secure url. |
f127b1a5 | 404 | return new moodle_url("https://secure.gravatar.com/avatar/{$md5}", array('s' => $size, 'd' => $gravatardefault)); |
4125bdc1 | 405 | } else { |
f127b1a5 | 406 | return new moodle_url("http://www.gravatar.com/avatar/{$md5}", array('s' => $size, 'd' => $gravatardefault)); |
4125bdc1 | 407 | } |
871a3ec5 SH |
408 | } |
409 | ||
faa328c3 | 410 | return $defaulturl; |
5c0d03ea DM |
411 | } |
412 | } | |
bf11293a PS |
413 | |
414 | /** | |
415 | * Data structure representing a help icon. | |
416 | * | |
417 | * @copyright 2009 Nicolas Connault, 2010 Petr Skoda | |
9678c7b8 SH |
418 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
419 | * @since Moodle 2.0 | |
f8129210 | 420 | * @package core |
76be40cc | 421 | * @category output |
bf11293a | 422 | */ |
596509e4 | 423 | class old_help_icon implements renderable { |
9678c7b8 | 424 | |
bf11293a | 425 | /** |
76be40cc | 426 | * @var string Lang pack identifier |
bf11293a | 427 | */ |
53a78cef | 428 | public $helpidentifier; |
9678c7b8 | 429 | |
bf11293a | 430 | /** |
76be40cc | 431 | * @var string A descriptive text for title tooltip |
bf11293a | 432 | */ |
97c10099 | 433 | public $title = null; |
9678c7b8 | 434 | |
bf11293a | 435 | /** |
76be40cc | 436 | * @var string Component name, the same as in get_string() |
bf11293a PS |
437 | */ |
438 | public $component = 'moodle'; | |
9678c7b8 | 439 | |
bf11293a | 440 | /** |
76be40cc | 441 | * @var string Extra descriptive text next to the icon |
bf11293a | 442 | */ |
97c10099 | 443 | public $linktext = null; |
bf11293a PS |
444 | |
445 | /** | |
446 | * Constructor: sets up the other components in case they are needed | |
9678c7b8 | 447 | * |
53a78cef | 448 | * @param string $helpidentifier The keyword that defines a help page |
1a10840e | 449 | * @param string $title A descriptive text for accessibility only |
bf11293a | 450 | * @param string $component |
bf11293a | 451 | */ |
53a78cef | 452 | public function __construct($helpidentifier, $title, $component = 'moodle') { |
bf11293a PS |
453 | if (empty($title)) { |
454 | throw new coding_exception('A help_icon object requires a $text parameter'); | |
455 | } | |
53a78cef PS |
456 | if (empty($helpidentifier)) { |
457 | throw new coding_exception('A help_icon object requires a $helpidentifier parameter'); | |
bf11293a PS |
458 | } |
459 | ||
53a78cef PS |
460 | $this->helpidentifier = $helpidentifier; |
461 | $this->title = $title; | |
462 | $this->component = $component; | |
bf11293a PS |
463 | } |
464 | } | |
465 | ||
49f0d481 PS |
466 | /** |
467 | * Data structure representing a help icon. | |
468 | * | |
469 | * @copyright 2010 Petr Skoda (info@skodak.org) | |
9678c7b8 SH |
470 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
471 | * @since Moodle 2.0 | |
f8129210 | 472 | * @package core |
76be40cc | 473 | * @category output |
49f0d481 PS |
474 | */ |
475 | class help_icon implements renderable { | |
9678c7b8 | 476 | |
49f0d481 | 477 | /** |
76be40cc | 478 | * @var string lang pack identifier (without the "_help" suffix), |
9678c7b8 SH |
479 | * both get_string($identifier, $component) and get_string($identifier.'_help', $component) |
480 | * must exist. | |
49f0d481 PS |
481 | */ |
482 | public $identifier; | |
9678c7b8 | 483 | |
49f0d481 | 484 | /** |
76be40cc | 485 | * @var string Component name, the same as in get_string() |
49f0d481 PS |
486 | */ |
487 | public $component; | |
9678c7b8 | 488 | |
49f0d481 | 489 | /** |
76be40cc | 490 | * @var string Extra descriptive text next to the icon |
49f0d481 PS |
491 | */ |
492 | public $linktext = null; | |
493 | ||
494 | /** | |
495 | * Constructor | |
9678c7b8 SH |
496 | * |
497 | * @param string $identifier string for help page title, | |
5435c9dc MD |
498 | * string with _help suffix is used for the actual help text. |
499 | * string with _link suffix is used to create a link to further info (if it exists) | |
49f0d481 PS |
500 | * @param string $component |
501 | */ | |
259c165d PS |
502 | public function __construct($identifier, $component) { |
503 | $this->identifier = $identifier; | |
49f0d481 PS |
504 | $this->component = $component; |
505 | } | |
259c165d PS |
506 | |
507 | /** | |
508 | * Verifies that both help strings exists, shows debug warnings if not | |
509 | */ | |
510 | public function diag_strings() { | |
511 | $sm = get_string_manager(); | |
512 | if (!$sm->string_exists($this->identifier, $this->component)) { | |
513 | debugging("Help title string does not exist: [$this->identifier, $this->component]"); | |
514 | } | |
5435c9dc | 515 | if (!$sm->string_exists($this->identifier.'_help', $this->component)) { |
876521ac | 516 | debugging("Help contents string does not exist: [{$this->identifier}_help, $this->component]"); |
259c165d PS |
517 | } |
518 | } | |
49f0d481 PS |
519 | } |
520 | ||
bf11293a | 521 | |
000c278c PS |
522 | /** |
523 | * Data structure representing an icon. | |
524 | * | |
525 | * @copyright 2010 Petr Skoda | |
9678c7b8 SH |
526 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
527 | * @since Moodle 2.0 | |
f8129210 | 528 | * @package core |
76be40cc | 529 | * @category output |
000c278c PS |
530 | */ |
531 | class pix_icon implements renderable { | |
9678c7b8 SH |
532 | |
533 | /** | |
76be40cc | 534 | * @var string The icon name |
9678c7b8 | 535 | */ |
000c278c | 536 | var $pix; |
9678c7b8 SH |
537 | |
538 | /** | |
76be40cc | 539 | * @var string The component the icon belongs to. |
9678c7b8 | 540 | */ |
000c278c | 541 | var $component; |
9678c7b8 SH |
542 | |
543 | /** | |
76be40cc | 544 | * @var array An array of attributes to use on the icon |
9678c7b8 | 545 | */ |
000c278c PS |
546 | var $attributes = array(); |
547 | ||
548 | /** | |
549 | * Constructor | |
9678c7b8 | 550 | * |
000c278c | 551 | * @param string $pix short icon name |
7749e187 | 552 | * @param string $alt The alt text to use for the icon |
000c278c PS |
553 | * @param string $component component name |
554 | * @param array $attributes html attributes | |
555 | */ | |
556 | public function __construct($pix, $alt, $component='moodle', array $attributes = null) { | |
c80877aa PS |
557 | $this->pix = $pix; |
558 | $this->component = $component; | |
000c278c PS |
559 | $this->attributes = (array)$attributes; |
560 | ||
561 | $this->attributes['alt'] = $alt; | |
562 | if (empty($this->attributes['class'])) { | |
563 | $this->attributes['class'] = 'smallicon'; | |
564 | } | |
565 | if (!isset($this->attributes['title'])) { | |
566 | $this->attributes['title'] = $this->attributes['alt']; | |
aad982aa FM |
567 | } else if (empty($this->attributes['title'])) { |
568 | // Remove the title attribute if empty, we probably want to use the parent node's title | |
569 | // and some browsers might overwrite it with an empty title. | |
570 | unset($this->attributes['title']); | |
000c278c PS |
571 | } |
572 | } | |
573 | } | |
574 | ||
d63c5073 DM |
575 | /** |
576 | * Data structure representing an emoticon image | |
577 | * | |
9678c7b8 SH |
578 | * @copyright 2010 David Mudrak |
579 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
580 | * @since Moodle 2.0 | |
f8129210 | 581 | * @package core |
76be40cc | 582 | * @category output |
d63c5073 DM |
583 | */ |
584 | class pix_emoticon extends pix_icon implements renderable { | |
585 | ||
586 | /** | |
587 | * Constructor | |
588 | * @param string $pix short icon name | |
b9fadae7 DM |
589 | * @param string $alt alternative text |
590 | * @param string $component emoticon image provider | |
591 | * @param array $attributes explicit HTML attributes | |
d63c5073 | 592 | */ |
b9fadae7 DM |
593 | public function __construct($pix, $alt, $component = 'moodle', array $attributes = array()) { |
594 | if (empty($attributes['class'])) { | |
595 | $attributes['class'] = 'emoticon'; | |
596 | } | |
d63c5073 DM |
597 | parent::__construct($pix, $alt, $component, $attributes); |
598 | } | |
599 | } | |
000c278c | 600 | |
3ba60ee1 PS |
601 | /** |
602 | * Data structure representing a simple form with only one button. | |
603 | * | |
604 | * @copyright 2009 Petr Skoda | |
9678c7b8 SH |
605 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
606 | * @since Moodle 2.0 | |
f8129210 | 607 | * @package core |
76be40cc | 608 | * @category output |
3ba60ee1 PS |
609 | */ |
610 | class single_button implements renderable { | |
9678c7b8 | 611 | |
574fbea4 | 612 | /** |
76be40cc | 613 | * @var moodle_url Target url |
574fbea4 | 614 | */ |
3ba60ee1 | 615 | var $url; |
9678c7b8 | 616 | |
574fbea4 | 617 | /** |
76be40cc | 618 | * @var string Button label |
574fbea4 | 619 | */ |
3ba60ee1 | 620 | var $label; |
9678c7b8 | 621 | |
574fbea4 | 622 | /** |
76be40cc | 623 | * @var string Form submit method post or get |
574fbea4 | 624 | */ |
3ba60ee1 | 625 | var $method = 'post'; |
9678c7b8 | 626 | |
574fbea4 | 627 | /** |
76be40cc | 628 | * @var string Wrapping div class |
9678c7b8 | 629 | */ |
3ba60ee1 | 630 | var $class = 'singlebutton'; |
9678c7b8 | 631 | |
574fbea4 | 632 | /** |
3d3fae72 | 633 | * @var bool True if button disabled, false if normal |
574fbea4 | 634 | */ |
3ba60ee1 | 635 | var $disabled = false; |
9678c7b8 | 636 | |
574fbea4 | 637 | /** |
76be40cc | 638 | * @var string Button tooltip |
574fbea4 | 639 | */ |
97c10099 | 640 | var $tooltip = null; |
9678c7b8 | 641 | |
574fbea4 | 642 | /** |
76be40cc | 643 | * @var string Form id |
574fbea4 | 644 | */ |
3ba60ee1 | 645 | var $formid; |
9678c7b8 | 646 | |
574fbea4 | 647 | /** |
76be40cc | 648 | * @var array List of attached actions |
574fbea4 | 649 | */ |
3ba60ee1 PS |
650 | var $actions = array(); |
651 | ||
652 | /** | |
653 | * Constructor | |
9678c7b8 | 654 | * @param moodle_url $url |
3ba60ee1 PS |
655 | * @param string $label button text |
656 | * @param string $method get or post submit method | |
3ba60ee1 PS |
657 | */ |
658 | public function __construct(moodle_url $url, $label, $method='post') { | |
659 | $this->url = clone($url); | |
660 | $this->label = $label; | |
661 | $this->method = $method; | |
662 | } | |
663 | ||
664 | /** | |
574fbea4 | 665 | * Shortcut for adding a JS confirm dialog when the button is clicked. |
3ba60ee1 | 666 | * The message must be a yes/no question. |
9678c7b8 | 667 | * |
f8129210 | 668 | * @param string $confirmmessage The yes/no confirmation question. If "Yes" is clicked, the original action will occur. |
3ba60ee1 PS |
669 | */ |
670 | public function add_confirm_action($confirmmessage) { | |
8f78cd5a | 671 | $this->add_action(new confirm_action($confirmmessage)); |
3ba60ee1 PS |
672 | } |
673 | ||
574fbea4 PS |
674 | /** |
675 | * Add action to the button. | |
676 | * @param component_action $action | |
574fbea4 | 677 | */ |
3ba60ee1 PS |
678 | public function add_action(component_action $action) { |
679 | $this->actions[] = $action; | |
680 | } | |
681 | } | |
682 | ||
683 | ||
a9967cf5 PS |
684 | /** |
685 | * Simple form with just one select field that gets submitted automatically. | |
f8129210 | 686 | * |
a9967cf5 PS |
687 | * If JS not enabled small go button is printed too. |
688 | * | |
689 | * @copyright 2009 Petr Skoda | |
9678c7b8 SH |
690 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
691 | * @since Moodle 2.0 | |
f8129210 | 692 | * @package core |
76be40cc | 693 | * @category output |
a9967cf5 PS |
694 | */ |
695 | class single_select implements renderable { | |
9678c7b8 | 696 | |
a9967cf5 | 697 | /** |
76be40cc | 698 | * @var moodle_url Target url - includes hidden fields |
a9967cf5 PS |
699 | */ |
700 | var $url; | |
9678c7b8 | 701 | |
a9967cf5 | 702 | /** |
76be40cc | 703 | * @var string Name of the select element. |
a9967cf5 PS |
704 | */ |
705 | var $name; | |
9678c7b8 | 706 | |
a9967cf5 | 707 | /** |
9678c7b8 SH |
708 | * @var array $options associative array value=>label ex.: array(1=>'One, 2=>Two) |
709 | * it is also possible to specify optgroup as complex label array ex.: | |
710 | * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two'))) | |
711 | * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three'))) | |
a9967cf5 PS |
712 | */ |
713 | var $options; | |
9678c7b8 | 714 | |
a9967cf5 | 715 | /** |
76be40cc | 716 | * @var string Selected option |
a9967cf5 PS |
717 | */ |
718 | var $selected; | |
9678c7b8 | 719 | |
a9967cf5 | 720 | /** |
76be40cc | 721 | * @var array Nothing selected |
a9967cf5 PS |
722 | */ |
723 | var $nothing; | |
9678c7b8 | 724 | |
a9967cf5 | 725 | /** |
76be40cc | 726 | * @var array Extra select field attributes |
a9967cf5 PS |
727 | */ |
728 | var $attributes = array(); | |
9678c7b8 | 729 | |
a9967cf5 | 730 | /** |
76be40cc | 731 | * @var string Button label |
a9967cf5 PS |
732 | */ |
733 | var $label = ''; | |
9678c7b8 | 734 | |
ecc5cc31 | 735 | /** |
de7a570c | 736 | * @var array Button label's attributes |
ecc5cc31 RW |
737 | */ |
738 | var $labelattributes = array(); | |
739 | ||
a9967cf5 | 740 | /** |
76be40cc | 741 | * @var string Form submit method post or get |
a9967cf5 PS |
742 | */ |
743 | var $method = 'get'; | |
9678c7b8 | 744 | |
a9967cf5 | 745 | /** |
76be40cc SH |
746 | * @var string Wrapping div class |
747 | */ | |
a9967cf5 | 748 | var $class = 'singleselect'; |
9678c7b8 | 749 | |
a9967cf5 | 750 | /** |
3d3fae72 | 751 | * @var bool True if button disabled, false if normal |
a9967cf5 PS |
752 | */ |
753 | var $disabled = false; | |
9678c7b8 | 754 | |
a9967cf5 | 755 | /** |
76be40cc | 756 | * @var string Button tooltip |
a9967cf5 PS |
757 | */ |
758 | var $tooltip = null; | |
9678c7b8 | 759 | |
a9967cf5 | 760 | /** |
76be40cc | 761 | * @var string Form id |
a9967cf5 PS |
762 | */ |
763 | var $formid = null; | |
9678c7b8 | 764 | |
a9967cf5 | 765 | /** |
76be40cc | 766 | * @var array List of attached actions |
a9967cf5 PS |
767 | */ |
768 | var $helpicon = null; | |
9678c7b8 | 769 | |
a9967cf5 PS |
770 | /** |
771 | * Constructor | |
772 | * @param moodle_url $url form action target, includes hidden fields | |
773 | * @param string $name name of selection field - the changing parameter in url | |
774 | * @param array $options list of options | |
775 | * @param string $selected selected element | |
776 | * @param array $nothing | |
f8dab966 | 777 | * @param string $formid |
a9967cf5 | 778 | */ |
9678c7b8 | 779 | public function __construct(moodle_url $url, $name, array $options, $selected = '', $nothing = array('' => 'choosedots'), $formid = null) { |
a9967cf5 PS |
780 | $this->url = $url; |
781 | $this->name = $name; | |
782 | $this->options = $options; | |
783 | $this->selected = $selected; | |
784 | $this->nothing = $nothing; | |
f8dab966 | 785 | $this->formid = $formid; |
a9967cf5 PS |
786 | } |
787 | ||
788 | /** | |
789 | * Shortcut for adding a JS confirm dialog when the button is clicked. | |
790 | * The message must be a yes/no question. | |
9678c7b8 | 791 | * |
f8129210 | 792 | * @param string $confirmmessage The yes/no confirmation question. If "Yes" is clicked, the original action will occur. |
a9967cf5 PS |
793 | */ |
794 | public function add_confirm_action($confirmmessage) { | |
795 | $this->add_action(new component_action('submit', 'M.util.show_confirm_dialog', array('message' => $confirmmessage))); | |
796 | } | |
797 | ||
798 | /** | |
799 | * Add action to the button. | |
9678c7b8 | 800 | * |
a9967cf5 | 801 | * @param component_action $action |
a9967cf5 PS |
802 | */ |
803 | public function add_action(component_action $action) { | |
804 | $this->actions[] = $action; | |
805 | } | |
f8dab966 PS |
806 | |
807 | /** | |
259c165d | 808 | * Adds help icon. |
9678c7b8 | 809 | * |
f8129210 | 810 | * @param string $helppage The keyword that defines a help page |
1a10840e | 811 | * @param string $title A descriptive text for accessibility only |
f8dab966 | 812 | * @param string $component |
f8dab966 | 813 | */ |
596509e4 PS |
814 | public function set_old_help_icon($helppage, $title, $component = 'moodle') { |
815 | $this->helpicon = new old_help_icon($helppage, $title, $component); | |
f8dab966 PS |
816 | } |
817 | ||
259c165d PS |
818 | /** |
819 | * Adds help icon. | |
9678c7b8 | 820 | * |
259c165d PS |
821 | * @param string $identifier The keyword that defines a help page |
822 | * @param string $component | |
259c165d PS |
823 | */ |
824 | public function set_help_icon($identifier, $component = 'moodle') { | |
9c7b24bf | 825 | $this->helpicon = new help_icon($identifier, $component); |
259c165d PS |
826 | } |
827 | ||
f8dab966 | 828 | /** |
995f2d51 | 829 | * Sets select's label |
9678c7b8 | 830 | * |
f8dab966 | 831 | * @param string $label |
ecc5cc31 | 832 | * @param array $attributes (optional) |
f8dab966 | 833 | */ |
ecc5cc31 | 834 | public function set_label($label, $attributes = array()) { |
f8dab966 | 835 | $this->label = $label; |
ecc5cc31 RW |
836 | $this->labelattributes = $attributes; |
837 | ||
f8dab966 | 838 | } |
a9967cf5 PS |
839 | } |
840 | ||
4d10e579 PS |
841 | /** |
842 | * Simple URL selection widget description. | |
9678c7b8 | 843 | * |
4d10e579 | 844 | * @copyright 2009 Petr Skoda |
9678c7b8 SH |
845 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
846 | * @since Moodle 2.0 | |
f8129210 | 847 | * @package core |
76be40cc | 848 | * @category output |
4d10e579 PS |
849 | */ |
850 | class url_select implements renderable { | |
851 | /** | |
9678c7b8 SH |
852 | * @var array $urls associative array value=>label ex.: array(1=>'One, 2=>Two) |
853 | * it is also possible to specify optgroup as complex label array ex.: | |
854 | * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two'))) | |
855 | * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three'))) | |
4d10e579 PS |
856 | */ |
857 | var $urls; | |
9678c7b8 | 858 | |
4d10e579 | 859 | /** |
76be40cc | 860 | * @var string Selected option |
4d10e579 PS |
861 | */ |
862 | var $selected; | |
9678c7b8 | 863 | |
4d10e579 | 864 | /** |
76be40cc | 865 | * @var array Nothing selected |
4d10e579 PS |
866 | */ |
867 | var $nothing; | |
9678c7b8 | 868 | |
4d10e579 | 869 | /** |
76be40cc | 870 | * @var array Extra select field attributes |
4d10e579 PS |
871 | */ |
872 | var $attributes = array(); | |
9678c7b8 | 873 | |
4d10e579 | 874 | /** |
76be40cc | 875 | * @var string Button label |
4d10e579 PS |
876 | */ |
877 | var $label = ''; | |
9678c7b8 | 878 | |
ecc5cc31 RW |
879 | /** |
880 | * @var array Button label's attributes | |
881 | */ | |
882 | var $labelattributes = array(); | |
883 | ||
4d10e579 | 884 | /** |
76be40cc SH |
885 | * @var string Wrapping div class |
886 | */ | |
4d10e579 | 887 | var $class = 'urlselect'; |
9678c7b8 | 888 | |
4d10e579 | 889 | /** |
3d3fae72 | 890 | * @var bool True if button disabled, false if normal |
4d10e579 PS |
891 | */ |
892 | var $disabled = false; | |
9678c7b8 | 893 | |
4d10e579 | 894 | /** |
76be40cc | 895 | * @var string Button tooltip |
4d10e579 PS |
896 | */ |
897 | var $tooltip = null; | |
9678c7b8 | 898 | |
4d10e579 | 899 | /** |
76be40cc | 900 | * @var string Form id |
4d10e579 PS |
901 | */ |
902 | var $formid = null; | |
9678c7b8 | 903 | |
4d10e579 | 904 | /** |
76be40cc | 905 | * @var array List of attached actions |
4d10e579 PS |
906 | */ |
907 | var $helpicon = null; | |
9678c7b8 | 908 | |
15e48a1a SM |
909 | /** |
910 | * @var string If set, makes button visible with given name for button | |
911 | */ | |
912 | var $showbutton = null; | |
9678c7b8 | 913 | |
4d10e579 PS |
914 | /** |
915 | * Constructor | |
916 | * @param array $urls list of options | |
917 | * @param string $selected selected element | |
918 | * @param array $nothing | |
919 | * @param string $formid | |
15e48a1a SM |
920 | * @param string $showbutton Set to text of button if it should be visible |
921 | * or null if it should be hidden (hidden version always has text 'go') | |
922 | */ | |
9678c7b8 | 923 | public function __construct(array $urls, $selected = '', $nothing = array('' => 'choosedots'), $formid = null, $showbutton = null) { |
15e48a1a SM |
924 | $this->urls = $urls; |
925 | $this->selected = $selected; | |
926 | $this->nothing = $nothing; | |
927 | $this->formid = $formid; | |
928 | $this->showbutton = $showbutton; | |
4d10e579 PS |
929 | } |
930 | ||
931 | /** | |
259c165d | 932 | * Adds help icon. |
9678c7b8 | 933 | * |
f8129210 | 934 | * @param string $helppage The keyword that defines a help page |
1a10840e | 935 | * @param string $title A descriptive text for accessibility only |
4d10e579 | 936 | * @param string $component |
4d10e579 | 937 | */ |
596509e4 PS |
938 | public function set_old_help_icon($helppage, $title, $component = 'moodle') { |
939 | $this->helpicon = new old_help_icon($helppage, $title, $component); | |
4d10e579 PS |
940 | } |
941 | ||
259c165d PS |
942 | /** |
943 | * Adds help icon. | |
9678c7b8 | 944 | * |
259c165d PS |
945 | * @param string $identifier The keyword that defines a help page |
946 | * @param string $component | |
259c165d PS |
947 | */ |
948 | public function set_help_icon($identifier, $component = 'moodle') { | |
9c7b24bf | 949 | $this->helpicon = new help_icon($identifier, $component); |
259c165d PS |
950 | } |
951 | ||
4d10e579 | 952 | /** |
995f2d51 | 953 | * Sets select's label |
9678c7b8 | 954 | * |
4d10e579 | 955 | * @param string $label |
ecc5cc31 | 956 | * @param array $attributes (optional) |
4d10e579 | 957 | */ |
ecc5cc31 | 958 | public function set_label($label, $attributes = array()) { |
4d10e579 | 959 | $this->label = $label; |
ecc5cc31 | 960 | $this->labelattributes = $attributes; |
4d10e579 PS |
961 | } |
962 | } | |
963 | ||
574fbea4 PS |
964 | /** |
965 | * Data structure describing html link with special action attached. | |
9678c7b8 | 966 | * |
574fbea4 | 967 | * @copyright 2010 Petr Skoda |
9678c7b8 SH |
968 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
969 | * @since Moodle 2.0 | |
f8129210 | 970 | * @package core |
76be40cc | 971 | * @category output |
574fbea4 PS |
972 | */ |
973 | class action_link implements renderable { | |
9678c7b8 | 974 | |
574fbea4 | 975 | /** |
76be40cc | 976 | * @var moodle_url Href url |
574fbea4 PS |
977 | */ |
978 | var $url; | |
9678c7b8 | 979 | |
574fbea4 | 980 | /** |
76be40cc | 981 | * @var string Link text HTML fragment |
574fbea4 PS |
982 | */ |
983 | var $text; | |
9678c7b8 | 984 | |
574fbea4 | 985 | /** |
76be40cc | 986 | * @var array HTML attributes |
574fbea4 PS |
987 | */ |
988 | var $attributes; | |
9678c7b8 | 989 | |
574fbea4 | 990 | /** |
76be40cc | 991 | * @var array List of actions attached to link |
574fbea4 PS |
992 | */ |
993 | var $actions; | |
994 | ||
995 | /** | |
996 | * Constructor | |
9678c7b8 | 997 | * @param moodle_url $url |
574fbea4 PS |
998 | * @param string $text HTML fragment |
999 | * @param component_action $action | |
11820bac | 1000 | * @param array $attributes associative array of html link attributes + disabled |
574fbea4 | 1001 | */ |
9678c7b8 SH |
1002 | public function __construct(moodle_url $url, $text, component_action $action = null, array $attributes = null) { |
1003 | $this->url = clone($url); | |
1004 | $this->text = $text; | |
b0fef57b | 1005 | $this->attributes = (array)$attributes; |
f14b641b | 1006 | if ($action) { |
574fbea4 PS |
1007 | $this->add_action($action); |
1008 | } | |
1009 | } | |
1010 | ||
1011 | /** | |
1012 | * Add action to the link. | |
9678c7b8 | 1013 | * |
574fbea4 | 1014 | * @param component_action $action |
574fbea4 PS |
1015 | */ |
1016 | public function add_action(component_action $action) { | |
1017 | $this->actions[] = $action; | |
1018 | } | |
c63923bd | 1019 | |
9678c7b8 SH |
1020 | /** |
1021 | * Adds a CSS class to this action link object | |
1022 | * @param string $class | |
1023 | */ | |
c63923bd | 1024 | public function add_class($class) { |
67da0bf7 DM |
1025 | if (empty($this->attributes['class'])) { |
1026 | $this->attributes['class'] = $class; | |
c63923bd | 1027 | } else { |
67da0bf7 | 1028 | $this->attributes['class'] .= ' ' . $class; |
c63923bd PS |
1029 | } |
1030 | } | |
574fbea4 | 1031 | } |
3ba60ee1 | 1032 | |
5d0c95a5 PS |
1033 | /** |
1034 | * Simple html output class | |
9678c7b8 | 1035 | * |
5d0c95a5 | 1036 | * @copyright 2009 Tim Hunt, 2010 Petr Skoda |
9678c7b8 SH |
1037 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
1038 | * @since Moodle 2.0 | |
f8129210 | 1039 | * @package core |
76be40cc | 1040 | * @category output |
5d0c95a5 PS |
1041 | */ |
1042 | class html_writer { | |
9678c7b8 | 1043 | |
5d0c95a5 PS |
1044 | /** |
1045 | * Outputs a tag with attributes and contents | |
9678c7b8 | 1046 | * |
5d0c95a5 | 1047 | * @param string $tagname The name of tag ('a', 'img', 'span' etc.) |
5d0c95a5 | 1048 | * @param string $contents What goes between the opening and closing tags |
26acc814 | 1049 | * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.) |
5d0c95a5 PS |
1050 | * @return string HTML fragment |
1051 | */ | |
26acc814 | 1052 | public static function tag($tagname, $contents, array $attributes = null) { |
5d0c95a5 PS |
1053 | return self::start_tag($tagname, $attributes) . $contents . self::end_tag($tagname); |
1054 | } | |
1055 | ||
1056 | /** | |
1057 | * Outputs an opening tag with attributes | |
9678c7b8 | 1058 | * |
5d0c95a5 PS |
1059 | * @param string $tagname The name of tag ('a', 'img', 'span' etc.) |
1060 | * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.) | |
1061 | * @return string HTML fragment | |
1062 | */ | |
1063 | public static function start_tag($tagname, array $attributes = null) { | |
1064 | return '<' . $tagname . self::attributes($attributes) . '>'; | |
1065 | } | |
1066 | ||
1067 | /** | |
1068 | * Outputs a closing tag | |
9678c7b8 | 1069 | * |
5d0c95a5 PS |
1070 | * @param string $tagname The name of tag ('a', 'img', 'span' etc.) |
1071 | * @return string HTML fragment | |
1072 | */ | |
1073 | public static function end_tag($tagname) { | |
1074 | return '</' . $tagname . '>'; | |
1075 | } | |
1076 | ||
1077 | /** | |
1078 | * Outputs an empty tag with attributes | |
9678c7b8 | 1079 | * |
5d0c95a5 PS |
1080 | * @param string $tagname The name of tag ('input', 'img', 'br' etc.) |
1081 | * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.) | |
1082 | * @return string HTML fragment | |
1083 | */ | |
1084 | public static function empty_tag($tagname, array $attributes = null) { | |
1085 | return '<' . $tagname . self::attributes($attributes) . ' />'; | |
1086 | } | |
1087 | ||
836c47d7 TH |
1088 | /** |
1089 | * Outputs a tag, but only if the contents are not empty | |
9678c7b8 | 1090 | * |
836c47d7 TH |
1091 | * @param string $tagname The name of tag ('a', 'img', 'span' etc.) |
1092 | * @param string $contents What goes between the opening and closing tags | |
1093 | * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.) | |
1094 | * @return string HTML fragment | |
1095 | */ | |
1096 | public static function nonempty_tag($tagname, $contents, array $attributes = null) { | |
1097 | if ($contents === '' || is_null($contents)) { | |
1098 | return ''; | |
1099 | } | |
1100 | return self::tag($tagname, $contents, $attributes); | |
1101 | } | |
1102 | ||
5d0c95a5 PS |
1103 | /** |
1104 | * Outputs a HTML attribute and value | |
9678c7b8 | 1105 | * |
5d0c95a5 PS |
1106 | * @param string $name The name of the attribute ('src', 'href', 'class' etc.) |
1107 | * @param string $value The value of the attribute. The value will be escaped with {@link s()} | |
1108 | * @return string HTML fragment | |
1109 | */ | |
1110 | public static function attribute($name, $value) { | |
1111 | if (is_array($value)) { | |
1112 | debugging("Passed an array for the HTML attribute $name", DEBUG_DEVELOPER); | |
1113 | } | |
bf11293a PS |
1114 | if ($value instanceof moodle_url) { |
1115 | return ' ' . $name . '="' . $value->out() . '"'; | |
1116 | } | |
97c10099 PS |
1117 | |
1118 | // special case, we do not want these in output | |
1119 | if ($value === null) { | |
1120 | return ''; | |
5d0c95a5 | 1121 | } |
97c10099 PS |
1122 | |
1123 | // no sloppy trimming here! | |
1124 | return ' ' . $name . '="' . s($value) . '"'; | |
5d0c95a5 PS |
1125 | } |
1126 | ||
1127 | /** | |
1128 | * Outputs a list of HTML attributes and values | |
9678c7b8 | 1129 | * |
5d0c95a5 PS |
1130 | * @param array $attributes The tag attributes (array('src' => $url, 'class' => 'class1') etc.) |
1131 | * The values will be escaped with {@link s()} | |
1132 | * @return string HTML fragment | |
1133 | */ | |
1134 | public static function attributes(array $attributes = null) { | |
1135 | $attributes = (array)$attributes; | |
1136 | $output = ''; | |
1137 | foreach ($attributes as $name => $value) { | |
1138 | $output .= self::attribute($name, $value); | |
1139 | } | |
1140 | return $output; | |
1141 | } | |
1142 | ||
1143 | /** | |
1144 | * Generates random html element id. | |
9678c7b8 SH |
1145 | * |
1146 | * @staticvar int $counter | |
1147 | * @staticvar type $uniq | |
3d3fae72 SH |
1148 | * @param string $base A string fragment that will be included in the random ID. |
1149 | * @return string A unique ID | |
5d0c95a5 PS |
1150 | */ |
1151 | public static function random_id($base='random') { | |
2c479c8f PS |
1152 | static $counter = 0; |
1153 | static $uniq; | |
1154 | ||
1155 | if (!isset($uniq)) { | |
1156 | $uniq = uniqid(); | |
1157 | } | |
1158 | ||
1159 | $counter++; | |
1160 | return $base.$uniq.$counter; | |
5d0c95a5 | 1161 | } |
0f4c64b7 PS |
1162 | |
1163 | /** | |
1164 | * Generates a simple html link | |
9678c7b8 | 1165 | * |
3d3fae72 SH |
1166 | * @param string|moodle_url $url The URL |
1167 | * @param string $text The text | |
1168 | * @param array $attributes HTML attributes | |
0f4c64b7 PS |
1169 | * @return string HTML fragment |
1170 | */ | |
1171 | public static function link($url, $text, array $attributes = null) { | |
1172 | $attributes = (array)$attributes; | |
1173 | $attributes['href'] = $url; | |
26acc814 | 1174 | return self::tag('a', $text, $attributes); |
0f4c64b7 | 1175 | } |
3ff163c5 | 1176 | |
14dce022 | 1177 | /** |
9678c7b8 SH |
1178 | * Generates a simple checkbox with optional label |
1179 | * | |
3d3fae72 SH |
1180 | * @param string $name The name of the checkbox |
1181 | * @param string $value The value of the checkbox | |
1182 | * @param bool $checked Whether the checkbox is checked | |
1183 | * @param string $label The label for the checkbox | |
1184 | * @param array $attributes Any attributes to apply to the checkbox | |
14dce022 PS |
1185 | * @return string html fragment |
1186 | */ | |
1187 | public static function checkbox($name, $value, $checked = true, $label = '', array $attributes = null) { | |
1188 | $attributes = (array)$attributes; | |
1189 | $output = ''; | |
1190 | ||
1191 | if ($label !== '' and !is_null($label)) { | |
1192 | if (empty($attributes['id'])) { | |
1193 | $attributes['id'] = self::random_id('checkbox_'); | |
1194 | } | |
1195 | } | |
53868425 PS |
1196 | $attributes['type'] = 'checkbox'; |
1197 | $attributes['value'] = $value; | |
1198 | $attributes['name'] = $name; | |
621b4d08 | 1199 | $attributes['checked'] = $checked ? 'checked' : null; |
53868425 | 1200 | |
14dce022 PS |
1201 | $output .= self::empty_tag('input', $attributes); |
1202 | ||
1203 | if ($label !== '' and !is_null($label)) { | |
26acc814 | 1204 | $output .= self::tag('label', $label, array('for'=>$attributes['id'])); |
14dce022 PS |
1205 | } |
1206 | ||
1207 | return $output; | |
1208 | } | |
1209 | ||
78bdac64 PS |
1210 | /** |
1211 | * Generates a simple select yes/no form field | |
9678c7b8 | 1212 | * |
78bdac64 PS |
1213 | * @param string $name name of select element |
1214 | * @param bool $selected | |
1215 | * @param array $attributes - html select element attributes | |
9678c7b8 | 1216 | * @return string HTML fragment |
78bdac64 | 1217 | */ |
19f3bbb2 | 1218 | public static function select_yes_no($name, $selected=true, array $attributes = null) { |
78bdac64 PS |
1219 | $options = array('1'=>get_string('yes'), '0'=>get_string('no')); |
1220 | return self::select($options, $name, $selected, null, $attributes); | |
1221 | } | |
1222 | ||
3ff163c5 PS |
1223 | /** |
1224 | * Generates a simple select form field | |
9678c7b8 | 1225 | * |
6770330d PS |
1226 | * @param array $options associative array value=>label ex.: |
1227 | * array(1=>'One, 2=>Two) | |
1228 | * it is also possible to specify optgroup as complex label array ex.: | |
bde156b3 | 1229 | * array(array('Odd'=>array(1=>'One', 3=>'Three)), array('Even'=>array(2=>'Two'))) |
6770330d | 1230 | * array(1=>'One', '--1uniquekey'=>array('More'=>array(2=>'Two', 3=>'Three'))) |
3ff163c5 | 1231 | * @param string $name name of select element |
1a10840e | 1232 | * @param string|array $selected value or array of values depending on multiple attribute |
f8129210 SH |
1233 | * @param array|bool $nothing add nothing selected option, or false of not added |
1234 | * @param array $attributes html select element attributes | |
78bdac64 | 1235 | * @return string HTML fragment |
3ff163c5 | 1236 | */ |
9678c7b8 | 1237 | public static function select(array $options, $name, $selected = '', $nothing = array('' => 'choosedots'), array $attributes = null) { |
3ff163c5 PS |
1238 | $attributes = (array)$attributes; |
1239 | if (is_array($nothing)) { | |
1240 | foreach ($nothing as $k=>$v) { | |
4b9210f3 | 1241 | if ($v === 'choose' or $v === 'choosedots') { |
3ff163c5 PS |
1242 | $nothing[$k] = get_string('choosedots'); |
1243 | } | |
1244 | } | |
1245 | $options = $nothing + $options; // keep keys, do not override | |
3750c3bd PS |
1246 | |
1247 | } else if (is_string($nothing) and $nothing !== '') { | |
1248 | // BC | |
1249 | $options = array(''=>$nothing) + $options; | |
bde156b3 | 1250 | } |
3ff163c5 PS |
1251 | |
1252 | // we may accept more values if multiple attribute specified | |
1253 | $selected = (array)$selected; | |
1254 | foreach ($selected as $k=>$v) { | |
1255 | $selected[$k] = (string)$v; | |
1256 | } | |
1257 | ||
1258 | if (!isset($attributes['id'])) { | |
1259 | $id = 'menu'.$name; | |
1260 | // name may contaion [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading | |
1261 | $id = str_replace('[', '', $id); | |
1262 | $id = str_replace(']', '', $id); | |
1263 | $attributes['id'] = $id; | |
1264 | } | |
1265 | ||
1266 | if (!isset($attributes['class'])) { | |
1267 | $class = 'menu'.$name; | |
1268 | // name may contaion [], which would make an invalid class. e.g. numeric question type editing form, assignment quickgrading | |
1269 | $class = str_replace('[', '', $class); | |
1270 | $class = str_replace(']', '', $class); | |
1271 | $attributes['class'] = $class; | |
1272 | } | |
f8129210 | 1273 | $attributes['class'] = 'select ' . $attributes['class']; // Add 'select' selector always |
3ff163c5 PS |
1274 | |
1275 | $attributes['name'] = $name; | |
1276 | ||
1a09fa6d TH |
1277 | if (!empty($attributes['disabled'])) { |
1278 | $attributes['disabled'] = 'disabled'; | |
1279 | } else { | |
1280 | unset($attributes['disabled']); | |
1281 | } | |
1282 | ||
3ff163c5 PS |
1283 | $output = ''; |
1284 | foreach ($options as $value=>$label) { | |
6770330d PS |
1285 | if (is_array($label)) { |
1286 | // ignore key, it just has to be unique | |
1287 | $output .= self::select_optgroup(key($label), current($label), $selected); | |
1288 | } else { | |
1289 | $output .= self::select_option($label, $value, $selected); | |
3ff163c5 | 1290 | } |
3ff163c5 | 1291 | } |
26acc814 | 1292 | return self::tag('select', $output, $attributes); |
3ff163c5 | 1293 | } |
6770330d | 1294 | |
9678c7b8 SH |
1295 | /** |
1296 | * Returns HTML to display a select box option. | |
1297 | * | |
1298 | * @param string $label The label to display as the option. | |
1299 | * @param string|int $value The value the option represents | |
1300 | * @param array $selected An array of selected options | |
1301 | * @return string HTML fragment | |
1302 | */ | |
6770330d PS |
1303 | private static function select_option($label, $value, array $selected) { |
1304 | $attributes = array(); | |
1305 | $value = (string)$value; | |
1306 | if (in_array($value, $selected, true)) { | |
1307 | $attributes['selected'] = 'selected'; | |
1308 | } | |
1309 | $attributes['value'] = $value; | |
26acc814 | 1310 | return self::tag('option', $label, $attributes); |
6770330d PS |
1311 | } |
1312 | ||
9678c7b8 SH |
1313 | /** |
1314 | * Returns HTML to display a select box option group. | |
1315 | * | |
1316 | * @param string $groupname The label to use for the group | |
1317 | * @param array $options The options in the group | |
1318 | * @param array $selected An array of selected values. | |
1319 | * @return string HTML fragment. | |
1320 | */ | |
6770330d PS |
1321 | private static function select_optgroup($groupname, $options, array $selected) { |
1322 | if (empty($options)) { | |
1323 | return ''; | |
1324 | } | |
1325 | $attributes = array('label'=>$groupname); | |
1326 | $output = ''; | |
1327 | foreach ($options as $value=>$label) { | |
1328 | $output .= self::select_option($label, $value, $selected); | |
1329 | } | |
26acc814 | 1330 | return self::tag('optgroup', $output, $attributes); |
6770330d | 1331 | } |
6ea66ff3 | 1332 | |
f83b9b63 PS |
1333 | /** |
1334 | * This is a shortcut for making an hour selector menu. | |
9678c7b8 | 1335 | * |
f83b9b63 PS |
1336 | * @param string $type The type of selector (years, months, days, hours, minutes) |
1337 | * @param string $name fieldname | |
1338 | * @param int $currenttime A default timestamp in GMT | |
1339 | * @param int $step minute spacing | |
1340 | * @param array $attributes - html select element attributes | |
1341 | * @return HTML fragment | |
1342 | */ | |
9678c7b8 | 1343 | public static function select_time($type, $name, $currenttime = 0, $step = 5, array $attributes = null) { |
f83b9b63 PS |
1344 | if (!$currenttime) { |
1345 | $currenttime = time(); | |
1346 | } | |
1347 | $currentdate = usergetdate($currenttime); | |
1348 | $userdatetype = $type; | |
1349 | $timeunits = array(); | |
1350 | ||
1351 | switch ($type) { | |
1352 | case 'years': | |
1353 | for ($i=1970; $i<=2020; $i++) { | |
1354 | $timeunits[$i] = $i; | |
1355 | } | |
1356 | $userdatetype = 'year'; | |
1357 | break; | |
1358 | case 'months': | |
1359 | for ($i=1; $i<=12; $i++) { | |
1360 | $timeunits[$i] = userdate(gmmktime(12,0,0,$i,15,2000), "%B"); | |
1361 | } | |
1362 | $userdatetype = 'month'; | |
0387e003 | 1363 | $currentdate['month'] = (int)$currentdate['mon']; |
f83b9b63 PS |
1364 | break; |
1365 | case 'days': | |
1366 | for ($i=1; $i<=31; $i++) { | |
1367 | $timeunits[$i] = $i; | |
1368 | } | |
1369 | $userdatetype = 'mday'; | |
1370 | break; | |
1371 | case 'hours': | |
1372 | for ($i=0; $i<=23; $i++) { | |
1373 | $timeunits[$i] = sprintf("%02d",$i); | |
1374 | } | |
1375 | break; | |
1376 | case 'minutes': | |
1377 | if ($step != 1) { | |
1378 | $currentdate['minutes'] = ceil($currentdate['minutes']/$step)*$step; | |
1379 | } | |
1380 | ||
1381 | for ($i=0; $i<=59; $i+=$step) { | |
1382 | $timeunits[$i] = sprintf("%02d",$i); | |
1383 | } | |
1384 | break; | |
1385 | default: | |
1386 | throw new coding_exception("Time type $type is not supported by html_writer::select_time()."); | |
1387 | } | |
1388 | ||
1389 | if (empty($attributes['id'])) { | |
1390 | $attributes['id'] = self::random_id('ts_'); | |
1391 | } | |
1392 | $timerselector = self::select($timeunits, $name, $currentdate[$userdatetype], null, array('id'=>$attributes['id'])); | |
26acc814 | 1393 | $label = self::tag('label', get_string(substr($type, 0, -1), 'form'), array('for'=>$attributes['id'], 'class'=>'accesshide')); |
f83b9b63 PS |
1394 | |
1395 | return $label.$timerselector; | |
1396 | } | |
1397 | ||
5be262b6 PS |
1398 | /** |
1399 | * Shortcut for quick making of lists | |
9678c7b8 SH |
1400 | * |
1401 | * Note: 'list' is a reserved keyword ;-) | |
1402 | * | |
5be262b6 | 1403 | * @param array $items |
5be262b6 | 1404 | * @param array $attributes |
9678c7b8 | 1405 | * @param string $tag ul or ol |
5be262b6 PS |
1406 | * @return string |
1407 | */ | |
1408 | public static function alist(array $items, array $attributes = null, $tag = 'ul') { | |
5be262b6 PS |
1409 | $output = ''; |
1410 | ||
1411 | foreach ($items as $item) { | |
1412 | $output .= html_writer::start_tag('li') . "\n"; | |
1413 | $output .= $item . "\n"; | |
1414 | $output .= html_writer::end_tag('li') . "\n"; | |
1415 | } | |
1416 | ||
26acc814 | 1417 | return html_writer::tag($tag, $output, $attributes); |
5be262b6 PS |
1418 | } |
1419 | ||
6ea66ff3 PS |
1420 | /** |
1421 | * Returns hidden input fields created from url parameters. | |
9678c7b8 | 1422 | * |
6ea66ff3 PS |
1423 | * @param moodle_url $url |
1424 | * @param array $exclude list of excluded parameters | |
1425 | * @return string HTML fragment | |
1426 | */ | |
1427 | public static function input_hidden_params(moodle_url $url, array $exclude = null) { | |
1428 | $exclude = (array)$exclude; | |
1429 | $params = $url->params(); | |
1430 | foreach ($exclude as $key) { | |
1431 | unset($params[$key]); | |
1432 | } | |
1433 | ||
1434 | $output = ''; | |
bde156b3 | 1435 | foreach ($params as $key => $value) { |
6ea66ff3 PS |
1436 | $attributes = array('type'=>'hidden', 'name'=>$key, 'value'=>$value); |
1437 | $output .= self::empty_tag('input', $attributes)."\n"; | |
1438 | } | |
1439 | return $output; | |
1440 | } | |
77774f6a PS |
1441 | |
1442 | /** | |
1443 | * Generate a script tag containing the the specified code. | |
1444 | * | |
f8129210 SH |
1445 | * @param string $jscode the JavaScript code |
1446 | * @param moodle_url|string $url optional url of the external script, $code ignored if specified | |
77774f6a PS |
1447 | * @return string HTML, the code wrapped in <script> tags. |
1448 | */ | |
e50b4c89 | 1449 | public static function script($jscode, $url=null) { |
77774f6a | 1450 | if ($jscode) { |
e50b4c89 | 1451 | $attributes = array('type'=>'text/javascript'); |
26acc814 | 1452 | return self::tag('script', "\n//<![CDATA[\n$jscode\n//]]>\n", $attributes) . "\n"; |
e50b4c89 PS |
1453 | |
1454 | } else if ($url) { | |
1455 | $attributes = array('type'=>'text/javascript', 'src'=>$url); | |
26acc814 | 1456 | return self::tag('script', '', $attributes) . "\n"; |
a9967cf5 | 1457 | |
77774f6a PS |
1458 | } else { |
1459 | return ''; | |
1460 | } | |
1461 | } | |
16be8974 DM |
1462 | |
1463 | /** | |
1464 | * Renders HTML table | |
1465 | * | |
1466 | * This method may modify the passed instance by adding some default properties if they are not set yet. | |
1467 | * If this is not what you want, you should make a full clone of your data before passing them to this | |
1468 | * method. In most cases this is not an issue at all so we do not clone by default for performance | |
1469 | * and memory consumption reasons. | |
1470 | * | |
1471 | * @param html_table $table data to be rendered | |
1472 | * @return string HTML code | |
1473 | */ | |
1474 | public static function table(html_table $table) { | |
1475 | // prepare table data and populate missing properties with reasonable defaults | |
1476 | if (!empty($table->align)) { | |
1477 | foreach ($table->align as $key => $aa) { | |
1478 | if ($aa) { | |
1479 | $table->align[$key] = 'text-align:'. fix_align_rtl($aa) .';'; // Fix for RTL languages | |
1480 | } else { | |
1481 | $table->align[$key] = null; | |
1482 | } | |
1483 | } | |
1484 | } | |
1485 | if (!empty($table->size)) { | |
1486 | foreach ($table->size as $key => $ss) { | |
1487 | if ($ss) { | |
1488 | $table->size[$key] = 'width:'. $ss .';'; | |
1489 | } else { | |
1490 | $table->size[$key] = null; | |
1491 | } | |
1492 | } | |
1493 | } | |
1494 | if (!empty($table->wrap)) { | |
1495 | foreach ($table->wrap as $key => $ww) { | |
1496 | if ($ww) { | |
1497 | $table->wrap[$key] = 'white-space:nowrap;'; | |
1498 | } else { | |
1499 | $table->wrap[$key] = ''; | |
1500 | } | |
1501 | } | |
1502 | } | |
1503 | if (!empty($table->head)) { | |
1504 | foreach ($table->head as $key => $val) { | |
1505 | if (!isset($table->align[$key])) { | |
1506 | $table->align[$key] = null; | |
1507 | } | |
1508 | if (!isset($table->size[$key])) { | |
1509 | $table->size[$key] = null; | |
1510 | } | |
1511 | if (!isset($table->wrap[$key])) { | |
1512 | $table->wrap[$key] = null; | |
1513 | } | |
1514 | ||
1515 | } | |
1516 | } | |
1517 | if (empty($table->attributes['class'])) { | |
1518 | $table->attributes['class'] = 'generaltable'; | |
1519 | } | |
1520 | if (!empty($table->tablealign)) { | |
1521 | $table->attributes['class'] .= ' boxalign' . $table->tablealign; | |
1522 | } | |
1523 | ||
1524 | // explicitly assigned properties override those defined via $table->attributes | |
e126c0cc | 1525 | $table->attributes['class'] = trim($table->attributes['class']); |
16be8974 DM |
1526 | $attributes = array_merge($table->attributes, array( |
1527 | 'id' => $table->id, | |
1528 | 'width' => $table->width, | |
1529 | 'summary' => $table->summary, | |
1530 | 'cellpadding' => $table->cellpadding, | |
1531 | 'cellspacing' => $table->cellspacing, | |
1532 | )); | |
1533 | $output = html_writer::start_tag('table', $attributes) . "\n"; | |
1534 | ||
1535 | $countcols = 0; | |
1536 | ||
1537 | if (!empty($table->head)) { | |
1538 | $countcols = count($table->head); | |
5174f3c5 | 1539 | |
16be8974 DM |
1540 | $output .= html_writer::start_tag('thead', array()) . "\n"; |
1541 | $output .= html_writer::start_tag('tr', array()) . "\n"; | |
1542 | $keys = array_keys($table->head); | |
1543 | $lastkey = end($keys); | |
1544 | ||
1545 | foreach ($table->head as $key => $heading) { | |
1546 | // Convert plain string headings into html_table_cell objects | |
1547 | if (!($heading instanceof html_table_cell)) { | |
1548 | $headingtext = $heading; | |
1549 | $heading = new html_table_cell(); | |
1550 | $heading->text = $headingtext; | |
1551 | $heading->header = true; | |
1552 | } | |
1553 | ||
1554 | if ($heading->header !== false) { | |
1555 | $heading->header = true; | |
1556 | } | |
1557 | ||
e126c0cc DM |
1558 | if ($heading->header && empty($heading->scope)) { |
1559 | $heading->scope = 'col'; | |
1560 | } | |
1561 | ||
16be8974 DM |
1562 | $heading->attributes['class'] .= ' header c' . $key; |
1563 | if (isset($table->headspan[$key]) && $table->headspan[$key] > 1) { | |
1564 | $heading->colspan = $table->headspan[$key]; | |
1565 | $countcols += $table->headspan[$key] - 1; | |
1566 | } | |
1567 | ||
1568 | if ($key == $lastkey) { | |
1569 | $heading->attributes['class'] .= ' lastcol'; | |
1570 | } | |
1571 | if (isset($table->colclasses[$key])) { | |
1572 | $heading->attributes['class'] .= ' ' . $table->colclasses[$key]; | |
1573 | } | |
e126c0cc | 1574 | $heading->attributes['class'] = trim($heading->attributes['class']); |
16be8974 DM |
1575 | $attributes = array_merge($heading->attributes, array( |
1576 | 'style' => $table->align[$key] . $table->size[$key] . $heading->style, | |
1577 | 'scope' => $heading->scope, | |
1578 | 'colspan' => $heading->colspan, | |
1579 | )); | |
1580 | ||
1581 | $tagtype = 'td'; | |
1582 | if ($heading->header === true) { | |
1583 | $tagtype = 'th'; | |
1584 | } | |
1585 | $output .= html_writer::tag($tagtype, $heading->text, $attributes) . "\n"; | |
1586 | } | |
1587 | $output .= html_writer::end_tag('tr') . "\n"; | |
1588 | $output .= html_writer::end_tag('thead') . "\n"; | |
1589 | ||
1590 | if (empty($table->data)) { | |
1591 | // For valid XHTML strict every table must contain either a valid tr | |
1592 | // or a valid tbody... both of which must contain a valid td | |
1593 | $output .= html_writer::start_tag('tbody', array('class' => 'empty')); | |
1594 | $output .= html_writer::tag('tr', html_writer::tag('td', '', array('colspan'=>count($table->head)))); | |
1595 | $output .= html_writer::end_tag('tbody'); | |
1596 | } | |
1597 | } | |
1598 | ||
1599 | if (!empty($table->data)) { | |
1600 | $oddeven = 1; | |
1601 | $keys = array_keys($table->data); | |
1602 | $lastrowkey = end($keys); | |
1603 | $output .= html_writer::start_tag('tbody', array()); | |
1604 | ||
1605 | foreach ($table->data as $key => $row) { | |
1606 | if (($row === 'hr') && ($countcols)) { | |
1607 | $output .= html_writer::tag('td', html_writer::tag('div', '', array('class' => 'tabledivider')), array('colspan' => $countcols)); | |
1608 | } else { | |
1609 | // Convert array rows to html_table_rows and cell strings to html_table_cell objects | |
1610 | if (!($row instanceof html_table_row)) { | |
1611 | $newrow = new html_table_row(); | |
1612 | ||
4954d6ed FM |
1613 | foreach ($row as $cell) { |
1614 | if (!($cell instanceof html_table_cell)) { | |
1615 | $cell = new html_table_cell($cell); | |
1616 | } | |
16be8974 DM |
1617 | $newrow->cells[] = $cell; |
1618 | } | |
1619 | $row = $newrow; | |
1620 | } | |
1621 | ||
1622 | $oddeven = $oddeven ? 0 : 1; | |
1623 | if (isset($table->rowclasses[$key])) { | |
1624 | $row->attributes['class'] .= ' ' . $table->rowclasses[$key]; | |
1625 | } | |
1626 | ||
1627 | $row->attributes['class'] .= ' r' . $oddeven; | |
1628 | if ($key == $lastrowkey) { | |
1629 | $row->attributes['class'] .= ' lastrow'; | |
1630 | } | |
1631 | ||
e126c0cc | 1632 | $output .= html_writer::start_tag('tr', array('class' => trim($row->attributes['class']), 'style' => $row->style, 'id' => $row->id)) . "\n"; |
16be8974 DM |
1633 | $keys2 = array_keys($row->cells); |
1634 | $lastkey = end($keys2); | |
1635 | ||
5174f3c5 | 1636 | $gotlastkey = false; //flag for sanity checking |
16be8974 | 1637 | foreach ($row->cells as $key => $cell) { |
5174f3c5 AD |
1638 | if ($gotlastkey) { |
1639 | //This should never happen. Why do we have a cell after the last cell? | |
1640 | mtrace("A cell with key ($key) was found after the last key ($lastkey)"); | |
1641 | } | |
1642 | ||
16be8974 DM |
1643 | if (!($cell instanceof html_table_cell)) { |
1644 | $mycell = new html_table_cell(); | |
1645 | $mycell->text = $cell; | |
1646 | $cell = $mycell; | |
1647 | } | |
1648 | ||
e126c0cc DM |
1649 | if (($cell->header === true) && empty($cell->scope)) { |
1650 | $cell->scope = 'row'; | |
1651 | } | |
1652 | ||
16be8974 DM |
1653 | if (isset($table->colclasses[$key])) { |
1654 | $cell->attributes['class'] .= ' ' . $table->colclasses[$key]; | |
1655 | } | |
1656 | ||
1657 | $cell->attributes['class'] .= ' cell c' . $key; | |
1658 | if ($key == $lastkey) { | |
1659 | $cell->attributes['class'] .= ' lastcol'; | |
5174f3c5 | 1660 | $gotlastkey = true; |
16be8974 DM |
1661 | } |
1662 | $tdstyle = ''; | |
1663 | $tdstyle .= isset($table->align[$key]) ? $table->align[$key] : ''; | |
1664 | $tdstyle .= isset($table->size[$key]) ? $table->size[$key] : ''; | |
1665 | $tdstyle .= isset($table->wrap[$key]) ? $table->wrap[$key] : ''; | |
e126c0cc | 1666 | $cell->attributes['class'] = trim($cell->attributes['class']); |
16be8974 DM |
1667 | $tdattributes = array_merge($cell->attributes, array( |
1668 | 'style' => $tdstyle . $cell->style, | |
1669 | 'colspan' => $cell->colspan, | |
1670 | 'rowspan' => $cell->rowspan, | |
1671 | 'id' => $cell->id, | |
1672 | 'abbr' => $cell->abbr, | |
1673 | 'scope' => $cell->scope, | |
1674 | )); | |
1675 | $tagtype = 'td'; | |
1676 | if ($cell->header === true) { | |
1677 | $tagtype = 'th'; | |
1678 | } | |
1679 | $output .= html_writer::tag($tagtype, $cell->text, $tdattributes) . "\n"; | |
1680 | } | |
1681 | } | |
1682 | $output .= html_writer::end_tag('tr') . "\n"; | |
1683 | } | |
1684 | $output .= html_writer::end_tag('tbody') . "\n"; | |
1685 | } | |
1686 | $output .= html_writer::end_tag('table') . "\n"; | |
1687 | ||
1688 | return $output; | |
1689 | } | |
1690 | ||
995f2d51 DM |
1691 | /** |
1692 | * Renders form element label | |
1693 | * | |
1694 | * By default, the label is suffixed with a label separator defined in the | |
1695 | * current language pack (colon by default in the English lang pack). | |
1696 | * Adding the colon can be explicitly disabled if needed. Label separators | |
1697 | * are put outside the label tag itself so they are not read by | |
1698 | * screenreaders (accessibility). | |
1699 | * | |
1700 | * Parameter $for explicitly associates the label with a form control. When | |
1701 | * set, the value of this attribute must be the same as the value of | |
1702 | * the id attribute of the form control in the same document. When null, | |
1703 | * the label being defined is associated with the control inside the label | |
1704 | * element. | |
1705 | * | |
1706 | * @param string $text content of the label tag | |
1707 | * @param string|null $for id of the element this label is associated with, null for no association | |
1708 | * @param bool $colonize add label separator (colon) to the label text, if it is not there yet | |
1709 | * @param array $attributes to be inserted in the tab, for example array('accesskey' => 'a') | |
1710 | * @return string HTML of the label element | |
1711 | */ | |
9678c7b8 | 1712 | public static function label($text, $for, $colonize = true, array $attributes=array()) { |
995f2d51 DM |
1713 | if (!is_null($for)) { |
1714 | $attributes = array_merge($attributes, array('for' => $for)); | |
1715 | } | |
1716 | $text = trim($text); | |
1717 | $label = self::tag('label', $text, $attributes); | |
1718 | ||
9678c7b8 SH |
1719 | // TODO MDL-12192 $colonize disabled for now yet |
1720 | // if (!empty($text) and $colonize) { | |
1721 | // // the $text may end with the colon already, though it is bad string definition style | |
1722 | // $colon = get_string('labelsep', 'langconfig'); | |
1723 | // if (!empty($colon)) { | |
1724 | // $trimmed = trim($colon); | |
1725 | // if ((substr($text, -strlen($trimmed)) == $trimmed) or (substr($text, -1) == ':')) { | |
1726 | // //debugging('The label text should not end with colon or other label separator, | |
1727 | // // please fix the string definition.', DEBUG_DEVELOPER); | |
1728 | // } else { | |
1729 | // $label .= $colon; | |
1730 | // } | |
1731 | // } | |
1732 | // } | |
995f2d51 DM |
1733 | |
1734 | return $label; | |
1735 | } | |
5d0c95a5 PS |
1736 | } |
1737 | ||
227255b8 PS |
1738 | /** |
1739 | * Simple javascript output class | |
9678c7b8 | 1740 | * |
227255b8 | 1741 | * @copyright 2010 Petr Skoda |
9678c7b8 SH |
1742 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
1743 | * @since Moodle 2.0 | |
f8129210 | 1744 | * @package core |
76be40cc | 1745 | * @category output |
227255b8 PS |
1746 | */ |
1747 | class js_writer { | |
9678c7b8 | 1748 | |
227255b8 PS |
1749 | /** |
1750 | * Returns javascript code calling the function | |
9678c7b8 | 1751 | * |
1a10840e | 1752 | * @param string $function function name, can be complex like Y.Event.purgeElement |
227255b8 PS |
1753 | * @param array $arguments parameters |
1754 | * @param int $delay execution delay in seconds | |
1755 | * @return string JS code fragment | |
1756 | */ | |
e839dce1 | 1757 | public static function function_call($function, array $arguments = null, $delay=0) { |
1b4e41af | 1758 | if ($arguments) { |
b2cb00c2 | 1759 | $arguments = array_map('json_encode', convert_to_array($arguments)); |
1b4e41af PS |
1760 | $arguments = implode(', ', $arguments); |
1761 | } else { | |
1762 | $arguments = ''; | |
1763 | } | |
227255b8 PS |
1764 | $js = "$function($arguments);"; |
1765 | ||
1766 | if ($delay) { | |
1767 | $delay = $delay * 1000; // in miliseconds | |
1768 | $js = "setTimeout(function() { $js }, $delay);"; | |
1769 | } | |
1770 | return $js . "\n"; | |
1771 | } | |
1772 | ||
3b01539c | 1773 | /** |
3d3fae72 | 1774 | * Special function which adds Y as first argument of function call. |
9678c7b8 | 1775 | * |
3d3fae72 SH |
1776 | * @param string $function The function to call |
1777 | * @param array $extraarguments Any arguments to pass to it | |
1778 | * @return string Some JS code | |
3b01539c | 1779 | */ |
e839dce1 | 1780 | public static function function_call_with_Y($function, array $extraarguments = null) { |
3b01539c | 1781 | if ($extraarguments) { |
b2cb00c2 | 1782 | $extraarguments = array_map('json_encode', convert_to_array($extraarguments)); |
3b01539c PS |
1783 | $arguments = 'Y, ' . implode(', ', $extraarguments); |
1784 | } else { | |
1785 | $arguments = 'Y'; | |
1786 | } | |
1787 | return "$function($arguments);\n"; | |
1788 | } | |
1789 | ||
1ce15fda SH |
1790 | /** |
1791 | * Returns JavaScript code to initialise a new object | |
9678c7b8 | 1792 | * |
3d3fae72 SH |
1793 | * @param string $var If it is null then no var is assigned the new object. |
1794 | * @param string $class The class to initialise an object for. | |
1795 | * @param array $arguments An array of args to pass to the init method. | |
1796 | * @param array $requirements Any modules required for this class. | |
1797 | * @param int $delay The delay before initialisation. 0 = no delay. | |
1798 | * @return string Some JS code | |
1ce15fda | 1799 | */ |
e839dce1 | 1800 | public static function object_init($var, $class, array $arguments = null, array $requirements = null, $delay=0) { |
1ce15fda | 1801 | if (is_array($arguments)) { |
b2cb00c2 | 1802 | $arguments = array_map('json_encode', convert_to_array($arguments)); |
1ce15fda SH |
1803 | $arguments = implode(', ', $arguments); |
1804 | } | |
1805 | ||
1806 | if ($var === null) { | |
53fc3e70 | 1807 | $js = "new $class(Y, $arguments);"; |
1ce15fda | 1808 | } else if (strpos($var, '.')!==false) { |
53fc3e70 | 1809 | $js = "$var = new $class(Y, $arguments);"; |
1ce15fda | 1810 | } else { |
53fc3e70 | 1811 | $js = "var $var = new $class(Y, $arguments);"; |
1ce15fda SH |
1812 | } |
1813 | ||
1814 | if ($delay) { | |
1815 | $delay = $delay * 1000; // in miliseconds | |
1816 | $js = "setTimeout(function() { $js }, $delay);"; | |
1817 | } | |
1818 | ||
1819 | if (count($requirements) > 0) { | |
1820 | $requirements = implode("', '", $requirements); | |
53fc3e70 | 1821 | $js = "Y.use('$requirements', function(Y){ $js });"; |
1ce15fda SH |
1822 | } |
1823 | return $js."\n"; | |
1824 | } | |
1825 | ||
227255b8 PS |
1826 | /** |
1827 | * Returns code setting value to variable | |
9678c7b8 | 1828 | * |
227255b8 PS |
1829 | * @param string $name |
1830 | * @param mixed $value json serialised value | |
1831 | * @param bool $usevar add var definition, ignored for nested properties | |
1832 | * @return string JS code fragment | |
1833 | */ | |
9678c7b8 | 1834 | public static function set_variable($name, $value, $usevar = true) { |
227255b8 PS |
1835 | $output = ''; |
1836 | ||
1837 | if ($usevar) { | |
1838 | if (strpos($name, '.')) { | |
1839 | $output .= ''; | |
1840 | } else { | |
1841 | $output .= 'var '; | |
1842 | } | |
1843 | } | |
1844 | ||
1845 | $output .= "$name = ".json_encode($value).";"; | |
1846 | ||
1847 | return $output; | |
1848 | } | |
1849 | ||
1850 | /** | |
1851 | * Writes event handler attaching code | |
9678c7b8 | 1852 | * |
f8129210 SH |
1853 | * @param array|string $selector standard YUI selector for elements, may be |
1854 | * array or string, element id is in the form "#idvalue" | |
227255b8 PS |
1855 | * @param string $event A valid DOM event (click, mousedown, change etc.) |
1856 | * @param string $function The name of the function to call | |
9678c7b8 | 1857 | * @param array $arguments An optional array of argument parameters to pass to the function |
227255b8 PS |
1858 | * @return string JS code fragment |
1859 | */ | |
e839dce1 | 1860 | public static function event_handler($selector, $event, $function, array $arguments = null) { |
227255b8 PS |
1861 | $selector = json_encode($selector); |
1862 | $output = "Y.on('$event', $function, $selector, null"; | |
1863 | if (!empty($arguments)) { | |
1864 | $output .= ', ' . json_encode($arguments); | |
1865 | } | |
1866 | return $output . ");\n"; | |
1867 | } | |
1868 | } | |
1869 | ||
d9c8f425 | 1870 | /** |
f8129210 | 1871 | * Holds all the information required to render a <table> by {@link core_renderer::table()} |
d9c8f425 | 1872 | * |
16be8974 DM |
1873 | * Example of usage: |
1874 | * $t = new html_table(); | |
1875 | * ... // set various properties of the object $t as described below | |
1876 | * echo html_writer::table($t); | |
d9c8f425 | 1877 | * |
16be8974 | 1878 | * @copyright 2009 David Mudrak <david.mudrak@gmail.com> |
9678c7b8 SH |
1879 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
1880 | * @since Moodle 2.0 | |
f8129210 | 1881 | * @package core |
76be40cc | 1882 | * @category output |
d9c8f425 | 1883 | */ |
16be8974 | 1884 | class html_table { |
9678c7b8 | 1885 | |
d9c8f425 | 1886 | /** |
76be40cc | 1887 | * @var string Value to use for the id attribute of the table |
d9c8f425 | 1888 | */ |
97c10099 | 1889 | public $id = null; |
9678c7b8 | 1890 | |
d9c8f425 | 1891 | /** |
76be40cc | 1892 | * @var array Attributes of HTML attributes for the <table> element |
d9c8f425 | 1893 | */ |
16be8974 | 1894 | public $attributes = array(); |
9678c7b8 | 1895 | |
7b1f2c82 | 1896 | /** |
76be40cc | 1897 | * @var array An array of headings. The n-th array item is used as a heading of the n-th column. |
a0ead5eb | 1898 | * For more control over the rendering of the headers, an array of html_table_cell objects |
54a007e8 | 1899 | * can be passed instead of an array of strings. |
7b1f2c82 | 1900 | * |
1901 | * Example of usage: | |
1902 | * $t->head = array('Student', 'Grade'); | |
1903 | */ | |
1904 | public $head; | |
9678c7b8 | 1905 | |
7b1f2c82 | 1906 | /** |
76be40cc | 1907 | * @var array An array that can be used to make a heading span multiple columns. |
f8129210 SH |
1908 | * In this example, {@link html_table:$data} is supposed to have three columns. For the first two columns, |
1909 | * the same heading is used. Therefore, {@link html_table::$head} should consist of two items. | |
7b1f2c82 | 1910 | * |
1911 | * Example of usage: | |
1912 | * $t->headspan = array(2,1); | |
7b1f2c82 | 1913 | */ |
1914 | public $headspan; | |
9678c7b8 | 1915 | |
7b1f2c82 | 1916 | /** |
76be40cc | 1917 | * @var array An array of column alignments. |
9678c7b8 | 1918 | * The value is used as CSS 'text-align' property. Therefore, possible |
7b1f2c82 | 1919 | * values are 'left', 'right', 'center' and 'justify'. Specify 'right' or 'left' from the perspective |
1920 | * of a left-to-right (LTR) language. For RTL, the values are flipped automatically. | |
1921 | * | |
beb56299 | 1922 | * Examples of usage: |
1923 | * $t->align = array(null, 'right'); | |
1924 | * or | |
1925 | * $t->align[1] = 'right'; | |
d9c8f425 | 1926 | */ |
beb56299 | 1927 | public $align; |
9678c7b8 | 1928 | |
d9c8f425 | 1929 | /** |
76be40cc | 1930 | * @var array The value is used as CSS 'size' property. |
beb56299 | 1931 | * |
1932 | * Examples of usage: | |
1933 | * $t->size = array('50%', '50%'); | |
1934 | * or | |
1935 | * $t->size[1] = '120px'; | |
d9c8f425 | 1936 | */ |
beb56299 | 1937 | public $size; |
9678c7b8 | 1938 | |
d9c8f425 | 1939 | /** |
76be40cc | 1940 | * @var array An array of wrapping information. |
9678c7b8 | 1941 | * The only possible value is 'nowrap' that sets the |
beb56299 | 1942 | * CSS property 'white-space' to the value 'nowrap' in the given column. |
1943 | * | |
1944 | * Example of usage: | |
1945 | * $t->wrap = array(null, 'nowrap'); | |
d9c8f425 | 1946 | */ |
beb56299 | 1947 | public $wrap; |
9678c7b8 | 1948 | |
d9c8f425 | 1949 | /** |
76be40cc | 1950 | * @var array Array of arrays or html_table_row objects containing the data. Alternatively, if you have |
beb56299 | 1951 | * $head specified, the string 'hr' (for horizontal ruler) can be used |
1952 | * instead of an array of cells data resulting in a divider rendered. | |
d9c8f425 | 1953 | * |
beb56299 | 1954 | * Example of usage with array of arrays: |
1955 | * $row1 = array('Harry Potter', '76 %'); | |
1956 | * $row2 = array('Hermione Granger', '100 %'); | |
1957 | * $t->data = array($row1, $row2); | |
d9c8f425 | 1958 | * |
beb56299 | 1959 | * Example with array of html_table_row objects: (used for more fine-grained control) |
1960 | * $cell1 = new html_table_cell(); | |
1961 | * $cell1->text = 'Harry Potter'; | |
1962 | * $cell1->colspan = 2; | |
1963 | * $row1 = new html_table_row(); | |
1964 | * $row1->cells[] = $cell1; | |
1965 | * $cell2 = new html_table_cell(); | |
1966 | * $cell2->text = 'Hermione Granger'; | |
1967 | * $cell3 = new html_table_cell(); | |
1968 | * $cell3->text = '100 %'; | |
1969 | * $row2 = new html_table_row(); | |
1970 | * $row2->cells = array($cell2, $cell3); | |
1971 | * $t->data = array($row1, $row2); | |
1972 | */ | |
1973 | public $data; | |
9678c7b8 | 1974 | |
beb56299 | 1975 | /** |
beb56299 | 1976 | * @deprecated since Moodle 2.0. Styling should be in the CSS. |
76be40cc | 1977 | * @var string Width of the table, percentage of the page preferred. |
beb56299 | 1978 | */ |
1979 | public $width = null; | |
9678c7b8 | 1980 | |
beb56299 | 1981 | /** |
beb56299 | 1982 | * @deprecated since Moodle 2.0. Styling should be in the CSS. |
76be40cc | 1983 | * @var string Alignment for the whole table. Can be 'right', 'left' or 'center' (default). |
beb56299 | 1984 | */ |
1985 | public $tablealign = null; | |
9678c7b8 | 1986 | |
beb56299 | 1987 | /** |
beb56299 | 1988 | * @deprecated since Moodle 2.0. Styling should be in the CSS. |
76be40cc | 1989 | * @var int Padding on each cell, in pixels |
beb56299 | 1990 | */ |
1991 | public $cellpadding = null; | |
9678c7b8 | 1992 | |
beb56299 | 1993 | /** |
76be40cc | 1994 | * @var int Spacing between cells, in pixels |
beb56299 | 1995 | * @deprecated since Moodle 2.0. Styling should be in the CSS. |
1996 | */ | |
1997 | public $cellspacing = null; | |
9678c7b8 | 1998 | |
beb56299 | 1999 | /** |
76be40cc | 2000 | * @var array Array of classes to add to particular rows, space-separated string. |
beb56299 | 2001 | * Classes 'r0' or 'r1' are added automatically for every odd or even row, |
2002 | * respectively. Class 'lastrow' is added automatically for the last row | |
2003 | * in the table. | |
d9c8f425 | 2004 | * |
beb56299 | 2005 | * Example of usage: |
2006 | * $t->rowclasses[9] = 'tenth' | |
2007 | */ | |
2008 | public $rowclasses; | |
9678c7b8 | 2009 | |
beb56299 | 2010 | /** |
76be40cc | 2011 | * @var array An array of classes to add to every cell in a particular column, |
beb56299 | 2012 | * space-separated string. Class 'cell' is added automatically by the renderer. |
2013 | * Classes 'c0' or 'c1' are added automatically for every odd or even column, | |
2014 | * respectively. Class 'lastcol' is added automatically for all last cells | |
2015 | * in a row. | |
d9c8f425 | 2016 | * |
beb56299 | 2017 | * Example of usage: |
2018 | * $t->colclasses = array(null, 'grade'); | |
d9c8f425 | 2019 | */ |
beb56299 | 2020 | public $colclasses; |
9678c7b8 | 2021 | |
beb56299 | 2022 | /** |
76be40cc | 2023 | * @var string Description of the contents for screen readers. |
beb56299 | 2024 | */ |
2025 | public $summary; | |
d9c8f425 | 2026 | |
2027 | /** | |
16be8974 | 2028 | * Constructor |
d9c8f425 | 2029 | */ |
16be8974 DM |
2030 | public function __construct() { |
2031 | $this->attributes['class'] = ''; | |
d9c8f425 | 2032 | } |
d9c8f425 | 2033 | } |
2034 | ||
2035 | /** | |
7b1f2c82 | 2036 | * Component representing a table row. |
d9c8f425 | 2037 | * |
2038 | * @copyright 2009 Nicolas Connault | |
9678c7b8 SH |
2039 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
2040 | * @since Moodle 2.0 | |
f8129210 | 2041 | * @package core |
76be40cc | 2042 | * @category output |
d9c8f425 | 2043 | */ |
16be8974 | 2044 | class html_table_row { |
9678c7b8 | 2045 | |
16be8974 | 2046 | /** |
76be40cc | 2047 | * @var string Value to use for the id attribute of the row. |
16be8974 DM |
2048 | */ |
2049 | public $id = null; | |
9678c7b8 | 2050 | |
d9c8f425 | 2051 | /** |
76be40cc | 2052 | * @var array Array of html_table_cell objects |
d9c8f425 | 2053 | */ |
7b1f2c82 | 2054 | public $cells = array(); |
9678c7b8 | 2055 | |
beb56299 | 2056 | /** |
76be40cc | 2057 | * @var string Value to use for the style attribute of the table row |
beb56299 | 2058 | */ |
16be8974 | 2059 | public $style = null; |
9678c7b8 | 2060 | |
16be8974 | 2061 | /** |
76be40cc | 2062 | * @var array Attributes of additional HTML attributes for the <tr> element |
16be8974 DM |
2063 | */ |
2064 | public $attributes = array(); | |
a0ead5eb | 2065 | |
54a007e8 | 2066 | /** |
8cea545e | 2067 | * Constructor |
54a007e8 | 2068 | * @param array $cells |
8cea545e PS |
2069 | */ |
2070 | public function __construct(array $cells=null) { | |
16be8974 | 2071 | $this->attributes['class'] = ''; |
8cea545e PS |
2072 | $cells = (array)$cells; |
2073 | foreach ($cells as $cell) { | |
2074 | if ($cell instanceof html_table_cell) { | |
2075 | $this->cells[] = $cell; | |
a019627a | 2076 | } else { |
8cea545e | 2077 | $this->cells[] = new html_table_cell($cell); |
a019627a | 2078 | } |
2079 | } | |
54a007e8 | 2080 | } |
d9c8f425 | 2081 | } |
2082 | ||
2083 | /** | |
7b1f2c82 | 2084 | * Component representing a table cell. |
d9c8f425 | 2085 | * |
2086 | * @copyright 2009 Nicolas Connault | |
9678c7b8 SH |
2087 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
2088 | * @since Moodle 2.0 | |
f8129210 | 2089 | * @package core |
76be40cc | 2090 | * @category output |
d9c8f425 | 2091 | */ |
16be8974 | 2092 | class html_table_cell { |
9678c7b8 | 2093 | |
16be8974 | 2094 | /** |
76be40cc | 2095 | * @var string Value to use for the id attribute of the cell. |
16be8974 DM |
2096 | */ |
2097 | public $id = null; | |
9678c7b8 | 2098 | |
d9c8f425 | 2099 | /** |
76be40cc | 2100 | * @var string The contents of the cell. |
d9c8f425 | 2101 | */ |
7b1f2c82 | 2102 | public $text; |
9678c7b8 | 2103 | |
d9c8f425 | 2104 | /** |
76be40cc | 2105 | * @var string Abbreviated version of the contents of the cell. |
d9c8f425 | 2106 | */ |
97c10099 | 2107 | public $abbr = null; |
9678c7b8 | 2108 | |
d9c8f425 | 2109 | /** |
76be40cc | 2110 | * @var int Number of columns this cell should span. |
d9c8f425 | 2111 | */ |
97c10099 | 2112 | public $colspan = null; |
9678c7b8 | 2113 | |
d9c8f425 | 2114 | /** |
76be40cc | 2115 | * @var int Number of rows this cell should span. |
d9c8f425 | 2116 | */ |
97c10099 | 2117 | public $rowspan = null; |
9678c7b8 | 2118 | |
d9c8f425 | 2119 | /** |
76be40cc | 2120 | * @var string Defines a way to associate header cells and data cells in a table. |
d9c8f425 | 2121 | */ |
97c10099 | 2122 | public $scope = null; |
9678c7b8 | 2123 | |
1ae3767a | 2124 | /** |
3d3fae72 | 2125 | * @var bool Whether or not this cell is a header cell. |
1ae3767a | 2126 | */ |
a4998d01 | 2127 | public $header = null; |
9678c7b8 | 2128 | |
16be8974 | 2129 | /** |
76be40cc | 2130 | * @var string Value to use for the style attribute of the table cell |
16be8974 DM |
2131 | */ |
2132 | public $style = null; | |
9678c7b8 | 2133 | |
16be8974 | 2134 | /** |
76be40cc | 2135 | * @var array Attributes of additional HTML attributes for the <td> element |
16be8974 DM |
2136 | */ |
2137 | public $attributes = array(); | |
d9c8f425 | 2138 | |
9678c7b8 SH |
2139 | /** |
2140 | * Constructs a table cell | |
2141 | * | |
2142 | * @param string $text | |
2143 | */ | |
8cea545e PS |
2144 | public function __construct($text = null) { |
2145 | $this->text = $text; | |
16be8974 | 2146 | $this->attributes['class'] = ''; |
d9c8f425 | 2147 | } |
2148 | } | |
2149 | ||
d9c8f425 | 2150 | /** |
beb56299 | 2151 | * Component representing a paging bar. |
d9c8f425 | 2152 | * |
2153 | * @copyright 2009 Nicolas Connault | |
9678c7b8 SH |
2154 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
2155 | * @since Moodle 2.0 | |
f8129210 | 2156 | * @package core |
76be40cc | 2157 | * @category output |
d9c8f425 | 2158 | */ |
929d7a83 | 2159 | class paging_bar implements renderable { |
9678c7b8 | 2160 | |
d9c8f425 | 2161 | /** |
76be40cc | 2162 | * @var int The maximum number of pagelinks to display. |
d9c8f425 | 2163 | */ |
beb56299 | 2164 | public $maxdisplay = 18; |
9678c7b8 | 2165 | |
d9c8f425 | 2166 | /** |
76be40cc | 2167 | * @var int The total number of entries to be pages through.. |
d9c8f425 | 2168 | */ |
beb56299 | 2169 | public $totalcount; |
9678c7b8 | 2170 | |
d9c8f425 | 2171 | /** |
76be40cc | 2172 | * @var int The page you are currently viewing. |
d9c8f425 | 2173 | */ |
929d7a83 | 2174 | public $page; |
9678c7b8 | 2175 | |
d9c8f425 | 2176 | /** |
76be40cc | 2177 | * @var int The number of entries that should be shown per page. |
d9c8f425 | 2178 | */ |
beb56299 | 2179 | public $perpage; |
9678c7b8 | 2180 | |
d9c8f425 | 2181 | /** |
76be40cc | 2182 | * @var string|moodle_url If this is a string then it is the url which will be appended with $pagevar, |
9678c7b8 SH |
2183 | * an equals sign and the page number. |
2184 | * If this is a moodle_url object then the pagevar param will be replaced by | |
2185 | * the page no, for each page. | |
d9c8f425 | 2186 | */ |
beb56299 | 2187 | public $baseurl; |
9678c7b8 | 2188 | |
d9c8f425 | 2189 | /** |
76be40cc | 2190 | * @var string This is the variable name that you use for the pagenumber in your |
9678c7b8 | 2191 | * code (ie. 'tablepage', 'blogpage', etc) |
d9c8f425 | 2192 | */ |
929d7a83 | 2193 | public $pagevar; |
9678c7b8 | 2194 | |
beb56299 | 2195 | /** |
76be40cc | 2196 | * @var string A HTML link representing the "previous" page. |
beb56299 | 2197 | */ |
2198 | public $previouslink = null; | |
9678c7b8 | 2199 | |
beb56299 | 2200 | /** |
76be40cc | 2201 | * @var string A HTML link representing the "next" page. |
beb56299 | 2202 | */ |
2203 | public $nextlink = null; | |
9678c7b8 | 2204 | |
beb56299 | 2205 | /** |
76be40cc | 2206 | * @var string A HTML link representing the first page. |
beb56299 | 2207 | */ |
2208 | public $firstlink = null; | |
9678c7b8 | 2209 | |
beb56299 | 2210 | /** |
76be40cc | 2211 | * @var string A HTML link representing the last page. |
beb56299 | 2212 | */ |
2213 | public $lastlink = null; | |
9678c7b8 | 2214 | |
beb56299 | 2215 | /** |
76be40cc | 2216 | * @var array An array of strings. One of them is just a string: the current page |
beb56299 | 2217 | */ |
2218 | public $pagelinks = array(); | |
d9c8f425 | 2219 | |
929d7a83 PS |
2220 | /** |
2221 | * Constructor paging_bar with only the required params. | |
2222 | * | |
1a10840e | 2223 | * @param int $totalcount The total number of entries available to be paged through |
929d7a83 PS |
2224 | * @param int $page The page you are currently viewing |
2225 | * @param int $perpage The number of entries that should be shown per page | |
2226 | * @param string|moodle_url $baseurl url of the current page, the $pagevar parameter is added | |
2227 | * @param string $pagevar name of page parameter that holds the page number | |
2228 | */ | |
2229 | public function __construct($totalcount, $page, $perpage, $baseurl, $pagevar = 'page') { | |
2230 | $this->totalcount = $totalcount; | |
2231 | $this->page = $page; | |
2232 | $this->perpage = $perpage; | |
2233 | $this->baseurl = $baseurl; | |
2234 | $this->pagevar = $pagevar; | |
2235 | } | |
2236 | ||
d9c8f425 | 2237 | /** |
9678c7b8 SH |
2238 | * Prepares the paging bar for output. |
2239 | * | |
2240 | * This method validates the arguments set up for the paging bar and then | |
2241 | * produces fragments of HTML to assist display later on. | |
f8129210 SH |
2242 | * |
2243 | * @param renderer_base $output | |
2244 | * @param moodle_page $page | |
2245 | * @param string $target | |
2246 | * @throws coding_exception | |
d9c8f425 | 2247 | */ |
34059565 | 2248 | public function prepare(renderer_base $output, moodle_page $page, $target) { |
1c1f64a2 | 2249 | if (!isset($this->totalcount) || is_null($this->totalcount)) { |
929d7a83 | 2250 | throw new coding_exception('paging_bar requires a totalcount value.'); |
beb56299 | 2251 | } |
2252 | if (!isset($this->page) || is_null($this->page)) { | |
929d7a83 | 2253 | throw new coding_exception('paging_bar requires a page value.'); |
beb56299 | 2254 | } |
2255 | if (empty($this->perpage)) { | |
929d7a83 | 2256 | throw new coding_exception('paging_bar requires a perpage value.'); |
beb56299 | 2257 | } |
2258 | if (empty($this->baseurl)) { | |
929d7a83 | 2259 | throw new coding_exception('paging_bar requires a baseurl value.'); |
beb56299 | 2260 | } |
d9c8f425 | 2261 | |
beb56299 | 2262 | if ($this->totalcount > $this->perpage) { |
2263 | $pagenum = $this->page - 1; | |
d9c8f425 | 2264 | |
beb56299 | 2265 | if ($this->page > 0) { |
929d7a83 | 2266 | $this->previouslink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('previous'), array('class'=>'previous')); |
beb56299 | 2267 | } |
d9c8f425 | 2268 | |
beb56299 | 2269 | if ($this->perpage > 0) { |
2270 | $lastpage = ceil($this->totalcount / $this->perpage); | |
2271 | } else { | |
2272 | $lastpage = 1; | |
2273 | } | |
2274 | ||
0232fa7b JF |
2275 | if ($this->page > round(($this->maxdisplay/3)*2)) { |
2276 | $currpage = $this->page - round($this->maxdisplay/3); | |
beb56299 | 2277 | |
929d7a83 | 2278 | $this->firstlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>0)), '1', array('class'=>'first')); |
beb56299 | 2279 | } else { |
0232fa7b | 2280 | $currpage = 0; |
beb56299 | 2281 | } |
2282 | ||
beb56299 | 2283 | $displaycount = $displaypage = 0; |
2284 | ||
2285 | while ($displaycount < $this->maxdisplay and $currpage < $lastpage) { | |
2286 | $displaypage = $currpage + 1; | |
2287 | ||
f43cdceb | 2288 | if ($this->page == $currpage) { |
beb56299 | 2289 | $this->pagelinks[] = $displaypage; |
2290 | } else { | |
56ddb719 | 2291 | $pagelink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$currpage)), $displaypage); |
beb56299 | 2292 | $this->pagelinks[] = $pagelink; |
2293 | } | |
2294 | ||
2295 | $displaycount++; | |
2296 | $currpage++; | |
2297 | } | |
2298 | ||
2299 | if ($currpage < $lastpage) { | |
2300 | $lastpageactual = $lastpage - 1; | |
abdac127 | 2301 | $this->lastlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$lastpageactual)), $lastpage, array('class'=>'last')); |
beb56299 | 2302 | } |
2303 | ||
2304 | $pagenum = $this->page + 1; | |
2305 | ||
2306 | if ($pagenum != $displaypage) { | |
abdac127 | 2307 | $this->nextlink = html_writer::link(new moodle_url($this->baseurl, array($this->pagevar=>$pagenum)), get_string('next'), array('class'=>'next')); |
beb56299 | 2308 | } |
d9c8f425 | 2309 | } |
2310 | } | |
d9c8f425 | 2311 | } |
2312 | ||
d9c8f425 | 2313 | /** |
beb56299 | 2314 | * This class represents how a block appears on a page. |
d9c8f425 | 2315 | * |
beb56299 | 2316 | * During output, each block instance is asked to return a block_contents object, |
2317 | * those are then passed to the $OUTPUT->block function for display. | |
2318 | * | |
f8129210 | 2319 | * contents should probably be generated using a moodle_block_..._renderer. |
beb56299 | 2320 | * |
2321 | * Other block-like things that need to appear on the page, for example the | |
2322 | * add new block UI, are also represented as block_contents objects. | |
2323 | * | |
2324 | * @copyright 2009 Tim Hunt | |
9678c7b8 SH |
2325 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
2326 | * @since Moodle 2.0 | |
f8129210 | 2327 | * @package core |
76be40cc | 2328 | * @category output |
d9c8f425 | 2329 | */ |
dd72b308 | 2330 | class block_contents { |
beb56299 | 2331 | |
76be40cc | 2332 | /** Used when the block cannot be collapsed **/ |
beb56299 | 2333 | const NOT_HIDEABLE = 0; |
76be40cc SH |
2334 | |
2335 | /** Used when the block can be collapsed but currently is not **/ | |
beb56299 | 2336 | const VISIBLE = 1; |
76be40cc SH |
2337 | |
2338 | /** Used when the block has been collapsed **/ | |
beb56299 | 2339 | const HIDDEN = 2; |
2340 | ||
d9c8f425 | 2341 | /** |
76be40cc | 2342 | * @var int Used to set $skipid. |
9678c7b8 SH |
2343 | */ |
2344 | protected static $idcounter = 1; | |
2345 | ||
2346 | /** | |
3d3fae72 | 2347 | * @var int All the blocks (or things that look like blocks) printed on |
76be40cc | 2348 | * a page are given a unique number that can be used to construct id="" attributes. |
9678c7b8 | 2349 | * This is set automatically be the {@link prepare()} method. |
beb56299 | 2350 | * Do not try to set it manually. |
d9c8f425 | 2351 | */ |
beb56299 | 2352 | public $skipid; |
d9c8f425 | 2353 | |
2354 | /** | |
3d3fae72 | 2355 | * @var int If this is the contents of a real block, this should be set |
76be40cc | 2356 | * to the block_instance.id. Otherwise this should be set to 0. |
beb56299 | 2357 | */ |
2358 | public $blockinstanceid = 0; | |
2359 | ||
2360 | /** | |
3d3fae72 | 2361 | * @var int If this is a real block instance, and there is a corresponding |
beb56299 | 2362 | * block_position.id for the block on this page, this should be set to that id. |
2363 | * Otherwise it should be 0. | |
2364 | */ | |
2365 | public $blockpositionid = 0; | |
2366 | ||
2367 | /** | |
76be40cc | 2368 | * @var array An array of attribute => value pairs that are put on the outer div of this |
9678c7b8 | 2369 | * block. {@link $id} and {@link $classes} attributes should be set separately. |
beb56299 | 2370 | */ |
dd72b308 | 2371 | public $attributes; |
beb56299 | 2372 | |
2373 | /** | |
76be40cc | 2374 | * @var string The title of this block. If this came from user input, it should already |
9678c7b8 SH |
2375 | * have had format_string() processing done on it. This will be output inside |
2376 | * <h2> tags. Please do not cause invalid XHTML. | |
beb56299 | 2377 | */ |
2378 | public $title = ''; | |
2379 | ||
91d941c3 SH |
2380 | /** |
2381 | * @var string The label to use when the block does not, or will not have a visible title. | |
2382 | * You should never set this as well as title... it will just be ignored. | |
2383 | */ | |
2384 | public $arialabel = ''; | |
2385 | ||
beb56299 | 2386 | /** |
76be40cc | 2387 | * @var string HTML for the content |
beb56299 | 2388 | */ |
2389 | public $content = ''; | |
2390 | ||
2391 | /** | |
76be40cc | 2392 | * @var array An alternative to $content, it you want a list of things with optional icons. |
beb56299 | 2393 | */ |
2394 | public $footer = ''; | |
2395 | ||
2396 | /** | |
76be40cc SH |
2397 | * @var string Any small print that should appear under the block to explain |
2398 | * to the teacher about the block, for example 'This is a sticky block that was | |
beb56299 | 2399 | * added in the system context.' |
beb56299 | 2400 | */ |
2401 | public $annotation = ''; | |
2402 | ||
2403 | /** | |
3d3fae72 | 2404 | * @var int One of the constants NOT_HIDEABLE, VISIBLE, HIDDEN. Whether |
beb56299 | 2405 | * the user can toggle whether this block is visible. |
2406 | */ | |
2407 | public $collapsible = self::NOT_HIDEABLE; | |
2408 | ||
2409 | /** | |
76be40cc SH |
2410 | * @var array A (possibly empty) array of editing controls. Each element of |
2411 | * this array should be an array('url' => $url, 'icon' => $icon, 'caption' => $caption). | |
b5d0cafc | 2412 | * $icon is the icon name. Fed to $OUTPUT->pix_url. |
beb56299 | 2413 | */ |
2414 | public $controls = array(); | |
2415 | ||
dd72b308 | 2416 | |
beb56299 | 2417 | /** |
dd72b308 PS |
2418 | * Create new instance of block content |
2419 | * @param array $attributes | |
d9c8f425 | 2420 | */ |
9678c7b8 | 2421 | public function __construct(array $attributes = null) { |
beb56299 | 2422 | $this->skipid = self::$idcounter; |
2423 | self::$idcounter += 1; | |
dd72b308 PS |
2424 | |
2425 | if ($attributes) { | |
2426 | // standard block | |
2427 | $this->attributes = $attributes; | |
2428 | } else { | |
2429 | // simple "fake" blocks used in some modules and "Add new block" block | |
6605ff8c | 2430 | $this->attributes = array('class'=>'block'); |
beb56299 | 2431 | } |
dd72b308 PS |
2432 | } |
2433 | ||
2434 | /** | |
2435 | * Add html class to block | |
9678c7b8 | 2436 | * |
dd72b308 | 2437 | * @param string $class |
dd72b308 PS |
2438 | */ |
2439 | public function add_class($class) { | |
2440 | $this->attributes['class'] .= ' '.$class; | |
d9c8f425 | 2441 | } |
2442 | } | |
beb56299 | 2443 | |
34059565 | 2444 | |
beb56299 | 2445 | /** |
2446 | * This class represents a target for where a block can go when it is being moved. | |
2447 | * | |
2448 | * This needs to be rendered as a form with the given hidden from fields, and | |
2449 | * clicking anywhere in the form should submit it. The form action should be | |
2450 | * $PAGE->url. | |
2451 | * | |
2452 | * @copyright 2009 Tim Hunt | |
9678c7b8 SH |
2453 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
2454 | * @since Moodle 2.0 | |
f8129210 | 2455 | * @package core |
76be40cc | 2456 | * @category output |
beb56299 | 2457 | */ |
dd72b308 | 2458 | class block_move_target { |
9678c7b8 | 2459 | |
beb56299 | 2460 | /** |
76be40cc | 2461 | * @var moodle_url Move url |
beb56299 | 2462 | */ |
dd72b308 | 2463 | public $url; |
9678c7b8 | 2464 | |
beb56299 | 2465 | /** |
76be40cc | 2466 | * @var string label |
beb56299 | 2467 | */ |
dd72b308 PS |
2468 | public $text; |
2469 | ||
2470 | /** | |
1a10840e | 2471 | * Constructor |
dd72b308 PS |
2472 | * @param string $text |
2473 | * @param moodle_url $url | |
2474 | */ | |
2475 | public function __construct($text, moodle_url $url) { | |
2476 | $this->text = $text; | |
2477 | $this->url = $url; | |
2478 | } | |
beb56299 | 2479 | } |
d2dbd0c0 SH |
2480 | |
2481 | /** | |
2482 | * Custom menu item | |
2483 | * | |
2484 | * This class is used to represent one item within a custom menu that may or may | |
2485 | * not have children. | |
2486 | * | |
2487 | * @copyright 2010 Sam Hemelryk | |
9678c7b8 SH |
2488 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
2489 | * @since Moodle 2.0 | |
f8129210 | 2490 | * @package core |
76be40cc | 2491 | * @category output |
d2dbd0c0 SH |
2492 | */ |
2493 | class custom_menu_item implements renderable { | |
9678c7b8 | 2494 | |
d2dbd0c0 | 2495 | /** |
76be40cc | 2496 | * @var string The text to show for the item |
d2dbd0c0 SH |
2497 | */ |
2498 | protected $text; | |
9678c7b8 | 2499 | |
d2dbd0c0 | 2500 | /** |
76be40cc | 2501 | * @var moodle_url The link to give the icon if it has no children |
d2dbd0c0 SH |
2502 | */ |
2503 | protected $url; | |
9678c7b8 | 2504 | |
d2dbd0c0 | 2505 | /** |
76be40cc | 2506 | * @var string A title to apply to the item. By default the text |
d2dbd0c0 SH |
2507 | */ |
2508 | protected $title; | |
9678c7b8 | 2509 | |
d2dbd0c0 | 2510 | /** |
76be40cc SH |
2511 | * @var int A sort order for the item, not necessary if you order things in |
2512 | * the CFG var. | |
d2dbd0c0 SH |
2513 | */ |
2514 | protected $sort; | |
9678c7b8 | 2515 | |
d2dbd0c0 | 2516 | /** |
76be40cc SH |
2517 | * @var custom_menu_item A reference to the parent for this item or NULL if |
2518 | * it is a top level item | |
d2dbd0c0 SH |
2519 | */ |
2520 | protected $parent; | |
9678c7b8 | 2521 | |
d2dbd0c0 | 2522 | /** |
76be40cc | 2523 | * @var array A array in which to store children this item has. |
d2dbd0c0 SH |
2524 | */ |
2525 | protected $children = array(); | |
9678c7b8 | 2526 | |
d2dbd0c0 | 2527 | /** |
76be40cc | 2528 | * @var int A reference to the sort var of the last child that was added |
d2dbd0c0 SH |
2529 | */ |
2530 | protected $lastsort = 0; | |
9678c7b8 | 2531 | |
d2dbd0c0 SH |
2532 | /** |
2533 | * Constructs the new custom menu item | |
2534 | * | |
2535 | * @param string $text | |
2536 | * @param moodle_url $url A moodle url to apply as the link for this item [Optional] | |
2537 | * @param string $title A title to apply to this item [Optional] | |
2538 | * @param int $sort A sort or to use if we need to sort differently [Optional] | |
2539 | * @param custom_menu_item $parent A reference to the parent custom_menu_item this child | |
2540 | * belongs to, only if the child has a parent. [Optional] | |
2541 | */ | |
9678c7b8 | 2542 | public function __construct($text, moodle_url $url=null, $title=null, $sort = null, custom_menu_item $parent = null) { |
d2dbd0c0 SH |
2543 | $this->text = $text; |
2544 | $this->url = $url; | |
2545 | $this->title = $title; | |
2546 | $this->sort = (int)$sort; | |
2547 | $this->parent = $parent; | |
2548 | } | |
2549 | ||
2550 | /** | |
2551 | * Adds a custom menu item as a child of this node given its properties. | |
2552 | * | |
2553 | * @param string $text | |
2554 | * @param moodle_url $url | |
2555 | * @param string $title | |
2556 | * @param int $sort | |
2557 | * @return custom_menu_item | |
2558 | */ | |
9678c7b8 | 2559 | public function add($text, moodle_url $url = null, $title = null, $sort = null) { |
d2dbd0c0 SH |
2560 | $key = count($this->children); |
2561 | if (empty($sort)) { | |
2562 | $sort = $this->lastsort + 1; | |
2563 | } | |
2564 | $this->children[$key] = new custom_menu_item($text, $url, $title, $sort, $this); | |
2565 | $this->lastsort = (int)$sort; | |
2566 | return $this->children[$key]; | |
2567 | } | |
9678c7b8 | 2568 | |
d2dbd0c0 SH |
2569 | /** |
2570 | * Returns the text for this item | |
2571 | * @return string | |
2572 | */ | |
2573 | public function get_text() { | |
2574 | return $this->text; | |
2575 | } | |
9678c7b8 | 2576 | |
d2dbd0c0 SH |
2577 | /** |
2578 | * Returns the url for this item | |
2579 | * @return moodle_url | |
2580 | */ | |
2581 | public function get_url() { | |
2582 | return $this->url; | |
2583 | } | |
9678c7b8 | 2584 | |
d2dbd0c0 SH |
2585 | /** |
2586 | * Returns the title for this item | |
2587 | * @return string | |
2588 | */ | |
2589 | public function get_title() { | |
2590 | return $this->title; | |
2591 | } | |
9678c7b8 | 2592 | |
d2dbd0c0 SH |
2593 | /** |
2594 | * Sorts and returns the children for this item | |
2595 | * @return array | |
2596 | */ | |
2597 | public function get_children() { | |
2598 | $this->sort(); | |
2599 | return $this->children; | |
2600 | } | |
9678c7b8 | 2601 | |
d2dbd0c0 SH |
2602 | /** |
2603 | * Gets the sort order for this child | |
2604 | * @return int | |
2605 | */ | |
2606 | public function get_sort_order() { | |
2607 | return $this->sort; | |
2608 | } | |
9678c7b8 | 2609 | |
d2dbd0c0 SH |
2610 | /** |
2611 | * Gets the parent this child belong to | |
2612 | * @return custom_menu_item | |
2613 | */ | |
2614 | public function get_parent() { | |
2615 | return $this->parent; | |
2616 | } | |
9678c7b8 | 2617 | |
d2dbd0c0 SH |
2618 | /** |
2619 | * Sorts the children this item has | |
2620 | */ | |
2621 | public function sort() { | |
2622 | usort($this->children, array('custom_menu','sort_custom_menu_items')); | |
2623 | } | |
9678c7b8 | 2624 | |
d2dbd0c0 SH |
2625 | /** |
2626 | * Returns true if this item has any children | |
2627 | * @return bool | |
2628 | */ | |
2629 | public function has_children() { | |
2630 | return (count($this->children) > 0); | |
2631 | } | |
f3827323 SH |
2632 | |
2633 | /** | |
2634 | * Sets the text for the node | |
2635 | * @param string $text | |
2636 | */ | |
2637 | public function set_text($text) { | |
2638 | $this->text = (string)$text; | |
2639 | } | |
2640 | ||
2641 | /** | |
2642 | * Sets the title for the node | |
2643 | * @param string $title | |
2644 | */ | |
2645 | public function set_title($title) { | |
2646 | $this->title = (string)$title; | |
2647 | } | |
2648 | ||
2649 | /** | |
2650 | * Sets the url for the node | |
2651 | * @param moodle_url $url | |
2652 | */ | |
2653 | public function set_url(moodle_url $url) { | |
2654 | $this->url = $url; | |
2655 | } | |
d2dbd0c0 SH |
2656 | } |
2657 | ||
2658 | /** | |
2659 | * Custom menu class | |
2660 | * | |
2661 | * This class is used to operate a custom menu that can be rendered for the page. | |
2662 | * The custom menu is built using $CFG->custommenuitems and is a structured collection | |
2663 | * of custom_menu_item nodes that can be rendered by the core renderer. | |
2664 | * | |
2665 | * To configure the custom menu: | |
2666 | * Settings: Administration > Appearance > Themes > Theme settings | |
2667 | * | |
2668 | * @copyright 2010 Sam Hemelryk | |
9678c7b8 SH |
2669 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
2670 | * @since Moodle 2.0 | |
f8129210 | 2671 | * @package core |
76be40cc | 2672 | * @category output |
d2dbd0c0 SH |
2673 | */ |
2674 | class custom_menu extends custom_menu_item { | |
155fffe6 | 2675 | |
9678c7b8 | 2676 | /** |
76be40cc | 2677 | * @var string The language we should render for, null disables multilang support. |
9678c7b8 | 2678 | */ |
4564d58f DM |
2679 | protected $currentlanguage = null; |
2680 | ||
d2dbd0c0 SH |
2681 | /** |
2682 | * Creates the custom menu | |
155fffe6 DM |
2683 | * |
2684 | * @param string $definition the menu items definition in syntax required by {@link convert_text_to_menu_nodes()} | |
f8129210 | 2685 | * @param string $currentlanguage the current language code, null disables multilang support |
d2dbd0c0 | 2686 | */ |
4564d58f | 2687 | public function __construct($definition = '', $currentlanguage = null) { |
4564d58f | 2688 | $this->currentlanguage = $currentlanguage; |
155fffe6 DM |
2689 | parent::__construct('root'); // create virtual root element of the menu |
2690 | if (!empty($definition)) { | |
4564d58f | 2691 | $this->override_children(self::convert_text_to_menu_nodes($definition, $currentlanguage)); |
12cc75ae | 2692 | } |
d2dbd0c0 SH |
2693 | } |
2694 | ||
2695 | /** | |
2696 | * Overrides the children of this custom menu. Useful when getting children | |
2697 | * from $CFG->custommenuitems | |
9678c7b8 SH |
2698 | * |
2699 | * @param array $children | |
d2dbd0c0 SH |
2700 | */ |
2701 | public function override_children(array $children) { | |
2702 | $this->children = array(); | |
2703 | foreach ($children as $child) { | |
2704 | if ($child instanceof custom_menu_item) { | |
2705 | $this->children[] = $child; | |
2706 | } | |
2707 | } | |
2708 | } | |
2709 | ||
2710 | /** | |
2711 | * Converts a string into a structured array of custom_menu_items which can | |
2712 | * then be added to a custom menu. | |
2713 | * | |
2714 | * Structure: | |
4564d58f DM |
2715 | * text|url|title|langs |
2716 | * The number of hyphens at the start determines the depth of the item. The | |
2717 | * languages are optional, comma separated list of languages the line is for. | |
d2dbd0c0 SH |
2718 | * |
2719 | * Example structure: | |
2720 | * First level first item|http://www.moodle.com/ | |
2721 | * -Second level first item|http://www.moodle.com/partners/ | |
2722 | * -Second level second item|http://www.moodle.com/hq/ | |
2723 | * --Third level first item|http://www.moodle.com/jobs/ | |
2724 | * -Second level third item|http://www.moodle.com/development/ | |
2725 | * First level second item|http://www.moodle.com/feedback/ | |
2726 | * First level third item | |
4564d58f DM |
2727 | * English only|http://moodle.com|English only item|en |
2728 | * German only|http://moodle.de|Deutsch|de,de_du,de_kids | |
2729 | * | |
4d2ee4c2 | 2730 | * |
d2dbd0c0 | 2731 | * @static |
4564d58f DM |
2732 | * @param string $text the menu items definition |
2733 | * @param string $language the language code, null disables multilang support | |
d2dbd0c0 SH |
2734 | * @return array |
2735 | */ | |
4564d58f | 2736 | public static function convert_text_to_menu_nodes($text, $language = null) { |
d2dbd0c0 SH |
2737 | $lines = explode("\n", $text); |
2738 | $children = array(); | |
2739 | $lastchild = null; | |
2740 | $lastdepth = null; | |
2741 | $lastsort = 0; | |
2742 | foreach ($lines as $line) { | |
2743 | $line = trim($line); | |
4564d58f DM |
2744 | $bits = explode('|', $line, 4); // name|url|title|langs |
2745 | if (!array_key_exists(0, $bits) or empty($bits[0])) { | |
d2dbd0c0 SH |
2746 | // Every item must have a name to be valid |
2747 | continue; | |
2748 | } else { | |
2749 | $bits[0] = ltrim($bits[0],'-'); | |
2750 | } | |
4564d58f | 2751 | if (!array_key_exists(1, $bits) or empty($bits[1])) { |
d2dbd0c0 SH |
2752 | // Set the url to null |
2753 | $bits[1] = null; | |
2754 | } else { | |
2755 | // Make sure the url is a moodle url | |
a26f25ae | 2756 | $bits[1] = new moodle_url(trim($bits[1])); |
d2dbd0c0 | 2757 | } |
4564d58f | 2758 | if (!array_key_exists(2, $bits) or empty($bits[2])) { |
d2dbd0c0 SH |
2759 | // Set the title to null seeing as there isn't one |
2760 | $bits[2] = $bits[0]; | |
2761 | } | |
4564d58f DM |
2762 | if (!array_key_exists(3, $bits) or empty($bits[3])) { |
2763 | // The item is valid for all languages | |
2764 | $itemlangs = null; | |
2765 | } else { | |
2766 | $itemlangs = array_map('trim', explode(',', $bits[3])); | |
2767 | } | |
2768 | if (!empty($language) and !empty($itemlangs)) { | |
2769 | // check that the item is intended for the current language | |
2770 | if (!in_array($language, $itemlangs)) { | |
2771 | continue; | |
2772 | } | |
2773 | } | |
d2dbd0c0 | 2774 | // Set an incremental sort order to keep it simple. |
4564d58f | 2775 | $lastsort++; |
d2dbd0c0 SH |
2776 | if (preg_match('/^(\-*)/', $line, $match) && $lastchild != null && $lastdepth !== null) { |
2777 | $depth = strlen($match[1]); | |
2778 | if ($depth < $lastdepth) { | |
57bedaee SH |
2779 | $difference = $lastdepth - $depth; |
2780 | if ($lastdepth > 1 && $lastdepth != $difference) { | |
2781 | $tempchild = $lastchild->get_parent(); | |
2782 | for ($i =0; $i < $difference; $i++) { | |
2783 | $tempchild = $tempchild->get_parent(); | |
2784 | } | |
4564d58f | 2785 | $lastchild = $tempchild->add($bits[0], $bits[1], $bits[2], $lastsort); |
d2dbd0c0 SH |
2786 | } else { |
2787 | $depth = 0; | |
4564d58f | 2788 | $lastchild = new custom_menu_item($bits[0], $bits[1], $bits[2], $lastsort); |
d2dbd0c0 SH |
2789 | $children[] = $lastchild; |
2790 | } | |
2791 | } else if ($depth > $lastdepth) { | |
2792 | $depth = $lastdepth + 1; | |
4564d58f | 2793 | $lastchild = $lastchild->add($bits[0], $bits[1], $bits[2], $lastsort); |
d2dbd0c0 SH |
2794 | } else { |
2795 | if ($depth == 0) { | |
4564d58f | 2796 | $lastchild = new custom_menu_item($bits[0], $bits[1], $bits[2], $lastsort); |
d2dbd0c0 SH |
2797 | $children[] = $lastchild; |
2798 | } else { | |
4564d58f | 2799 | $lastchild = $lastchild->get_parent()->add($bits[0], $bits[1], $bits[2], $lastsort); |
d2dbd0c0 SH |
2800 | } |
2801 | } | |
2802 | } else { | |
2803 | $depth = 0; | |
4564d58f | 2804 | $lastchild = new custom_menu_item($bits[0], $bits[1], $bits[2], $lastsort); |
d2dbd0c0 SH |
2805 | $children[] = $lastchild; |
2806 | } | |
2807 | $lastdepth = $depth; | |
2808 | } | |
2809 | return $children; | |
2810 | } | |
2811 | ||
2812 | /** | |
2813 | * Sorts two custom menu items | |
2814 | * | |
2815 | * This function is designed to be used with the usort method | |
2816 | * usort($this->children, array('custom_menu','sort_custom_menu_items')); | |
2817 | * | |
9678c7b8 | 2818 | * @static |
d2dbd0c0 SH |
2819 | * @param custom_menu_item $itema |
2820 | * @param custom_menu_item $itemb | |
2821 | * @return int | |
2822 | */ | |
2823 | public static function sort_custom_menu_items(custom_menu_item $itema, custom_menu_item $itemb) { | |
2824 | $itema = $itema->get_sort_order(); | |
2825 | $itemb = $itemb->get_sort_order(); | |
2826 | if ($itema == $itemb) { | |
2827 | return 0; | |
2828 | } | |
2829 | return ($itema > $itemb) ? +1 : -1; | |
2830 | } | |
de7a570c | 2831 | } |