Revert "MDL-39871 output: improved block manipulation inheritance"
[moodle.git] / lib / outputlib.php
CommitLineData
571fa828 1<?php
571fa828 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
78946b9b
PS
17/**
18 * Functions for generating the HTML that Moodle should output.
19 *
20 * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
21 * for an overview.
22 *
473dd811
SH
23 * @copyright 2009 Tim Hunt
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
f8129210 25 * @package core
76be40cc 26 * @category output
78946b9b
PS
27 */
28
78bfb562
PS
29defined('MOODLE_INTERNAL') || die();
30
d9c8f425 31require_once($CFG->libdir.'/outputcomponents.php');
32require_once($CFG->libdir.'/outputactions.php');
33require_once($CFG->libdir.'/outputfactories.php');
d9c8f425 34require_once($CFG->libdir.'/outputrenderers.php');
0bb38e8c 35require_once($CFG->libdir.'/outputrequirementslib.php');
d9c8f425 36
571fa828 37/**
78946b9b 38 * Invalidate all server and client side caches.
473dd811 39 *
f11db1a6 40 * This method deletes the physical directory that is used to cache the theme
473dd811 41 * files used for serving.
f11db1a6 42 * Because it deletes the main theme cache directory all themes are reset by
473dd811 43 * this function.
78946b9b
PS
44 */
45function theme_reset_all_caches() {
fe7b75f8 46 global $CFG, $PAGE;
78946b9b 47
f11db1a6
PS
48 $next = time();
49 if (isset($CFG->themerev) and $next <= $CFG->themerev and $CFG->themerev - $next < 60*60) {
50 // This resolves problems when reset is requested repeatedly within 1s,
51 // the < 1h condition prevents accidental switching to future dates
52 // because we might not recover from it.
53 $next = $CFG->themerev+1;
54 }
55
56 set_config('themerev', $next); // time is unique even when you reset/switch database
fe7b75f8
PS
57
58 if ($PAGE) {
59 $PAGE->reload_theme();
60 }
78946b9b
PS
61}
62
63/**
64 * Enable or disable theme designer mode.
473dd811 65 *
78946b9b 66 * @param bool $state
78946b9b
PS
67 */
68function theme_set_designer_mod($state) {
69 theme_reset_all_caches();
70 set_config('themedesignermode', (int)!empty($state));
71}
72
73/**
74 * Returns current theme revision number.
473dd811 75 *
78946b9b 76 * @return int
571fa828 77 */
78946b9b
PS
78function theme_get_revision() {
79 global $CFG;
80
81 if (empty($CFG->themedesignermode)) {
82 if (empty($CFG->themerev)) {
83 return -1;
84 } else {
85 return $CFG->themerev;
86 }
87
88 } else {
89 return -1;
90 }
91}
92
571fa828 93
571fa828 94/**
fdeb7fa1 95 * This class represents the configuration variables of a Moodle theme.
96 *
97 * All the variables with access: public below (with a few exceptions that are marked)
3d3fae72 98 * are the properties you can set in your themes config.php file.
fdeb7fa1 99 *
100 * There are also some methods and protected variables that are part of the inner
3d3fae72 101 * workings of Moodle's themes system. If you are just editing a themes config.php
fa1afe32 102 * file, you can just ignore those, and the following information for developers.
ebebf55c 103 *
104 * Normally, to create an instance of this class, you should use the
105 * {@link theme_config::load()} factory method to load a themes config.php file.
fa1afe32 106 * However, normally you don't need to bother, because moodle_page (that is, $PAGE)
fdeb7fa1 107 * will create one for you, accessible as $PAGE->theme.
571fa828 108 *
109 * @copyright 2009 Tim Hunt
473dd811
SH
110 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
111 * @since Moodle 2.0
f8129210 112 * @package core
76be40cc 113 * @category output
571fa828 114 */
ebebf55c 115class theme_config {
473dd811 116
5f0baa43 117 /**
76be40cc 118 * @var string Default theme, used when requested theme not found.
5f0baa43 119 */
90723839 120 const DEFAULT_THEME = 'standard';
5f0baa43 121
ebebf55c 122 /**
76be40cc 123 * @var array You can base your theme on other themes by linking to the other theme as
78946b9b 124 * parents. This lets you use the CSS and layouts from the other themes
f8129210 125 * (see {@link theme_config::$layouts}).
fdeb7fa1 126 * That makes it easy to create a new theme that is similar to another one
3d3fae72 127 * but with a few changes. In this themes CSS you only need to override
fdeb7fa1 128 * those rules you want to change.
fdeb7fa1 129 */
78946b9b 130 public $parents;
571fa828 131
fdeb7fa1 132 /**
76be40cc 133 * @var array The names of all the stylesheets from this theme that you would
78946b9b 134 * like included, in order. Give the names of the files without .css.
fdeb7fa1 135 */
78946b9b 136 public $sheets = array();
571fa828 137
fdeb7fa1 138 /**
76be40cc 139 * @var array The names of all the stylesheets from parents that should be excluded.
78946b9b
PS
140 * true value may be used to specify all parents or all themes from one parent.
141 * If no value specified value from parent theme used.
fdeb7fa1 142 */
78946b9b 143 public $parents_exclude_sheets = null;
571fa828 144
fdeb7fa1 145 /**
76be40cc 146 * @var array List of plugin sheets to be excluded.
78946b9b 147 * If no value specified value from parent theme used.
fdeb7fa1 148 */
78946b9b 149 public $plugins_exclude_sheets = null;
571fa828 150
fdeb7fa1 151 /**
76be40cc 152 * @var array List of style sheets that are included in the text editor bodies.
78946b9b 153 * Sheets from parent themes are used automatically and can not be excluded.
fdeb7fa1 154 */
78946b9b 155 public $editor_sheets = array();
571fa828 156
38aafea2 157 /**
76be40cc 158 * @var array The names of all the javascript files this theme that you would
04c01408 159 * like included from head, in order. Give the names of the files without .js.
38aafea2
PS
160 */
161 public $javascripts = array();
162
04c01408 163 /**
76be40cc 164 * @var array The names of all the javascript files this theme that you would
04c01408 165 * like included from footer, in order. Give the names of the files without .js.
04c01408
PS
166 */
167 public $javascripts_footer = array();
4399b9d5 168
38aafea2 169 /**
76be40cc
SH
170 * @var array The names of all the javascript files from parents that should
171 * be excluded. true value may be used to specify all parents or all themes
172 * from one parent.
38aafea2 173 * If no value specified value from parent theme used.
38aafea2
PS
174 */
175 public $parents_exclude_javascripts = null;
176
fdeb7fa1 177 /**
76be40cc 178 * @var array Which file to use for each page layout.
fdeb7fa1 179 *
78946b9b
PS
180 * This is an array of arrays. The keys of the outer array are the different layouts.
181 * Pages in Moodle are using several different layouts like 'normal', 'course', 'home',
182 * 'popup', 'form', .... The most reliable way to get a complete list is to look at
183 * {@link http://cvs.moodle.org/moodle/theme/base/config.php?view=markup the base theme config.php file}.
d4a03c00 184 * That file also has a good example of how to set this setting.
fdeb7fa1 185 *
78946b9b 186 * For each layout, the value in the outer array is an array that describes
d4a03c00 187 * how you want that type of page to look. For example
188 * <pre>
189 * $THEME->layouts = array(
39df78bf 190 * // Most pages - if we encounter an unknown or a missing page type, this one is used.
191b267b 191 * 'standard' => array(
78946b9b
PS
192 * 'theme' = 'mytheme',
193 * 'file' => 'normal.php',
d4a03c00 194 * 'regions' => array('side-pre', 'side-post'),
195 * 'defaultregion' => 'side-post'
196 * ),
197 * // The site home page.
198 * 'home' => array(
78946b9b
PS
199 * 'theme' = 'mytheme',
200 * 'file' => 'home.php',
d4a03c00 201 * 'regions' => array('side-pre', 'side-post'),
202 * 'defaultregion' => 'side-post'
203 * ),
204 * // ...
205 * );
206 * </pre>
fdeb7fa1 207 *
78946b9b
PS
208 * 'theme' name of the theme where is the layout located
209 * 'file' is the layout file to use for this type of page.
210 * layout files are stored in layout subfolder
d4a03c00 211 * 'regions' This lists the regions on the page where blocks may appear. For
212 * each region you list here, your layout file must include a call to
213 * <pre>
214 * echo $OUTPUT->blocks_for_region($regionname);
215 * </pre>
216 * or equivalent so that the blocks are actually visible.
fdeb7fa1 217 *
d4a03c00 218 * 'defaultregion' If the list of regions is non-empty, then you must pick
219 * one of the one of them as 'default'. This has two meanings. First, this is
220 * where new blocks are added. Second, if there are any blocks associated with
fa1afe32 221 * the page, but in non-existent regions, they appear here. (Imaging, for example,
d4a03c00 222 * that someone added blocks using a different theme that used different region
223 * names, and then switched to this theme.)
fdeb7fa1 224 */
d4a03c00 225 public $layouts = array();
571fa828 226
fdeb7fa1 227 /**
76be40cc
SH
228 * @var string Name of the renderer factory class to use. Must implement the
229 * {@link renderer_factory} interface.
fdeb7fa1 230 *
231 * This is an advanced feature. Moodle output is generated by 'renderers',
232 * you can customise the HTML that is output by writing custom renderers,
233 * and then you need to specify 'renderer factory' so that Moodle can find
234 * your renderers.
235 *
236 * There are some renderer factories supplied with Moodle. Please follow these
237 * links to see what they do.
238 * <ul>
239 * <li>{@link standard_renderer_factory} - the default.</li>
240 * <li>{@link theme_overridden_renderer_factory} - use this if you want to write
78946b9b 241 * your own custom renderers in a lib.php file in this theme (or the parent theme).</li>
fdeb7fa1 242 * </ul>
fdeb7fa1 243 */
ebebf55c 244 public $rendererfactory = 'standard_renderer_factory';
ebebf55c 245
fdeb7fa1 246 /**
76be40cc 247 * @var string Function to do custom CSS post-processing.
fdeb7fa1 248 *
78946b9b
PS
249 * This is an advanced feature. If you want to do custom post-processing on the
250 * CSS before it is output (for example, to replace certain variable names
251 * with particular values) you can give the name of a function here.
fdeb7fa1 252 */
78946b9b 253 public $csspostprocess = null;
571fa828 254
255 /**
76be40cc 256 * @var string Accessibility: Right arrow-like character is
78946b9b
PS
257 * used in the breadcrumb trail, course navigation menu
258 * (previous/next activity), calendar, and search forum block.
259 * If the theme does not set characters, appropriate defaults
260 * are set automatically. Please DO NOT
261 * use &lt; &gt; &raquo; - these are confusing for blind users.
78946b9b
PS
262 */
263 public $rarrow = null;
264
265 /**
76be40cc 266 * @var string Accessibility: Right arrow-like character is
78946b9b
PS
267 * used in the breadcrumb trail, course navigation menu
268 * (previous/next activity), calendar, and search forum block.
269 * If the theme does not set characters, appropriate defaults
270 * are set automatically. Please DO NOT
271 * use &lt; &gt; &raquo; - these are confusing for blind users.
571fa828 272 */
78946b9b
PS
273 public $larrow = null;
274
03276af6 275 /**
76be40cc 276 * @var bool Some themes may want to disable ajax course editing.
03276af6
DM
277 */
278 public $enablecourseajax = true;
78946b9b 279
13725b37
PS
280 /**
281 * @var string Determines served document types
282 * - 'html5' the only officially supported doctype in Moodle
283 * - 'xhtml5' may be used in development for validation (not intended for production servers!)
284 * - 'xhtml' XHTML 1.0 Strict for legacy themes only
285 */
286 public $doctype = 'html5';
287
78946b9b 288 //==Following properties are not configurable from theme config.php==
571fa828 289
fdeb7fa1 290 /**
76be40cc 291 * @var string The name of this theme. Set automatically when this theme is
78946b9b 292 * loaded. This can not be set in theme config.php
fdeb7fa1 293 */
78946b9b 294 public $name;
fdeb7fa1 295
296 /**
76be40cc 297 * @var string The folder where this themes files are stored. This is set
78946b9b 298 * automatically. This can not be set in theme config.php
fdeb7fa1 299 */
78946b9b 300 public $dir;
fdeb7fa1 301
302 /**
76be40cc 303 * @var stdClass Theme settings stored in config_plugins table.
78946b9b 304 * This can not be set in theme config.php
78946b9b
PS
305 */
306 public $setting = null;
307
d2c394f3 308 /**
76be40cc 309 * @var bool If set to true and the theme enables the dock then blocks will be able
d2c394f3 310 * to be moved to the special dock
d2c394f3
SH
311 */
312 public $enable_dock = false;
313
4d56ee95 314 /**
76be40cc 315 * @var bool If set to true then this theme will not be shown in the theme selector unless
4d56ee95 316 * theme designer mode is turned on.
4d56ee95
SH
317 */
318 public $hidefromselector = false;
319
97dbc8f2
PS
320 /**
321 * @var array list of YUI CSS modules to be included on each page. This may be used
322 * to remove cssreset and use cssnormalise module instead.
323 */
324 public $yuicssmodules = array('cssreset', 'cssfonts', 'cssgrids', 'cssbase');
325
36b77e3a
SH
326 /**
327 * An associative array of block manipulations that should be made if the user is using an rtl language.
328 * The key is the original block region, and the value is the block region to change to.
329 * This is used when displaying blocks for regions only.
330 * @var array
331 */
e2ad11d8 332 public $blockrtlmanipulations = array();
36b77e3a 333
78946b9b 334 /**
76be40cc 335 * @var renderer_factory Instance of the renderer_factory implementation
fdeb7fa1 336 * we are using. Implementation detail.
337 */
338 protected $rf = null;
339
340 /**
76be40cc 341 * @var array List of parent config objects.
78946b9b
PS
342 **/
343 protected $parent_configs = array();
fdeb7fa1 344
b305d623
SH
345 /**
346 * @var bool If set to true then the theme is safe to run through the optimiser (if it is enabled)
347 * If set to false then we know either the theme has already been optimised and the CSS optimiser is not needed
348 * or the theme is not compatible with the CSS optimiser. In both cases even if enabled the CSS optimiser will not
349 * be used with this theme if set to false.
350 */
351 public $supportscssoptimisation = true;
352
436dbeec
SH
353 /**
354 * Used to determine whether we can serve SVG images or not.
355 * @var bool
356 */
357 private $usesvg = null;
358
571fa828 359 /**
ebebf55c 360 * Load the config.php file for a particular theme, and return an instance
361 * of this class. (That is, this is a factory method.)
362 *
363 * @param string $themename the name of the theme.
364 * @return theme_config an instance of this class.
571fa828 365 */
ebebf55c 366 public static function load($themename) {
367 global $CFG;
571fa828 368
78946b9b
PS
369 // load theme settings from db
370 try {
371 $settings = get_config('theme_'.$themename);
372 } catch (dml_exception $e) {
da0c0e25 373 // most probably moodle tables not created yet
365a5941 374 $settings = new stdClass();
78946b9b 375 }
ebebf55c 376
78946b9b
PS
377 if ($config = theme_config::find_theme_config($themename, $settings)) {
378 return new theme_config($config);
5f0baa43
PS
379
380 } else if ($themename == theme_config::DEFAULT_THEME) {
381 throw new coding_exception('Default theme '.theme_config::DEFAULT_THEME.' not available or broken!');
382
2211f504
MA
383 } else if ($config = theme_config::find_theme_config($CFG->theme, $settings)) {
384 return new theme_config($config);
385
78946b9b
PS
386 } else {
387 // bad luck, the requested theme has some problems - admin see details in theme config
5f0baa43 388 return new theme_config(theme_config::find_theme_config(theme_config::DEFAULT_THEME, $settings));
78946b9b
PS
389 }
390 }
ebebf55c 391
78946b9b
PS
392 /**
393 * Theme diagnostic code. It is very problematic to send debug output
394 * to the actual CSS file, instead this functions is supposed to
ae5b6801 395 * diagnose given theme and highlights all potential problems.
78946b9b
PS
396 * This information should be available from the theme selection page
397 * or some other debug page for theme designers.
398 *
399 * @param string $themename
400 * @return array description of problems
401 */
402 public static function diagnose($themename) {
5ef719e7 403 //TODO: MDL-21108
78946b9b
PS
404 return array();
405 }
406
407 /**
408 * Private constructor, can be called only from the factory method.
409 * @param stdClass $config
410 */
411 private function __construct($config) {
412 global $CFG; //needed for included lib.php files
413
414 $this->settings = $config->settings;
415 $this->name = $config->name;
416 $this->dir = $config->dir;
417
418 if ($this->name != 'base') {
419 $baseconfig = theme_config::find_theme_config('base', $this->settings);
420 } else {
421 $baseconfig = $config;
571fa828 422 }
ebebf55c 423
fef27578 424 $configurable = array('parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'javascripts', 'javascripts_footer',
b305d623 425 'parents_exclude_javascripts', 'layouts', 'enable_dock', 'enablecourseajax', 'supportscssoptimisation',
97dbc8f2 426 'rendererfactory', 'csspostprocess', 'editor_sheets', 'rarrow', 'larrow', 'hidefromselector', 'doctype',
36b77e3a 427 'yuicssmodules', 'blockrtlmanipulations');
ebebf55c 428
78946b9b
PS
429 foreach ($config as $key=>$value) {
430 if (in_array($key, $configurable)) {
431 $this->$key = $value;
432 }
433 }
434
435 // verify all parents and load configs and renderers
436 foreach ($this->parents as $parent) {
437 if ($parent == 'base') {
438 $parent_config = $baseconfig;
439 } else if (!$parent_config = theme_config::find_theme_config($parent, $this->settings)) {
440 // this is not good - better exclude faulty parents
441 continue;
442 }
443 $libfile = $parent_config->dir.'/lib.php';
444 if (is_readable($libfile)) {
445 // theme may store various function here
446 include_once($libfile);
447 }
448 $renderersfile = $parent_config->dir.'/renderers.php';
449 if (is_readable($renderersfile)) {
450 // may contain core and plugin renderers and renderer factory
451 include_once($renderersfile);
452 }
453 $this->parent_configs[$parent] = $parent_config;
454 $rendererfile = $parent_config->dir.'/renderers.php';
455 if (is_readable($rendererfile)) {
456 // may contain core and plugin renderers and renderer factory
457 include_once($rendererfile);
458 }
459 }
460 $libfile = $this->dir.'/lib.php';
461 if (is_readable($libfile)) {
462 // theme may store various function here
463 include_once($libfile);
464 }
465 $rendererfile = $this->dir.'/renderers.php';
466 if (is_readable($rendererfile)) {
467 // may contain core and plugin renderers and renderer factory
468 include_once($rendererfile);
6fc267a0
BJ
469 } else {
470 // check if renderers.php file is missnamed renderer.php
471 if (is_readable($this->dir.'/renderer.php')) {
472 debugging('Developer hint: '.$this->dir.'/renderer.php should be renamed to ' . $this->dir."/renderers.php.
11c0ecce 473 See: http://docs.moodle.org/dev/Output_renderers#Theme_renderers.", DEBUG_DEVELOPER);
6fc267a0 474 }
78946b9b 475 }
4399b9d5 476
78946b9b
PS
477 // cascade all layouts properly
478 foreach ($baseconfig->layouts as $layout=>$value) {
479 if (!isset($this->layouts[$layout])) {
480 foreach ($this->parent_configs as $parent_config) {
481 if (isset($parent_config->layouts[$layout])) {
482 $this->layouts[$layout] = $parent_config->layouts[$layout];
483 continue 2;
484 }
485 }
486 $this->layouts[$layout] = $value;
487 }
488 }
489
e89dfdbc 490 // Use parent theme's "blockrtlmanipulations" if it was defined and if it was not defined on current theme.
e2ad11d8
SH
491 if (empty($this->blockrtlmanipulations) and !empty($parent_config->blockrtlmanipulations)) {
492 $this->blockrtlmanipulations = $parent_config->blockrtlmanipulations;
e89dfdbc
NK
493 }
494
78946b9b
PS
495 //fix arrows if needed
496 $this->check_theme_arrows();
571fa828 497 }
498
63c88397
PS
499 /**
500 * Let the theme initialise the page object (usually $PAGE).
501 *
502 * This may be used for example to request jQuery in add-ons.
503 *
504 * @param moodle_page $page
505 */
506 public function init_page(moodle_page $page) {
507 $themeinitfunction = 'theme_'.$this->name.'_page_init';
508 if (function_exists($themeinitfunction)) {
509 $themeinitfunction($page);
510 }
511 }
512
f8129210 513 /**
78946b9b
PS
514 * Checks if arrows $THEME->rarrow, $THEME->larrow have been set (theme/-/config.php).
515 * If not it applies sensible defaults.
516 *
517 * Accessibility: right and left arrow Unicode characters for breadcrumb, calendar,
518 * search forum block, etc. Important: these are 'silent' in a screen-reader
519 * (unlike &gt; &raquo;), and must be accompanied by text.
34a2777c 520 */
78946b9b
PS
521 private function check_theme_arrows() {
522 if (!isset($this->rarrow) and !isset($this->larrow)) {
523 // Default, looks good in Win XP/IE 6, Win/Firefox 1.5, Win/Netscape 8...
524 // Also OK in Win 9x/2K/IE 5.x
525 $this->rarrow = '&#x25BA;';
526 $this->larrow = '&#x25C4;';
527 if (empty($_SERVER['HTTP_USER_AGENT'])) {
528 $uagent = '';
ebebf55c 529 } else {
78946b9b
PS
530 $uagent = $_SERVER['HTTP_USER_AGENT'];
531 }
532 if (false !== strpos($uagent, 'Opera')
533 || false !== strpos($uagent, 'Mac')) {
534 // Looks good in Win XP/Mac/Opera 8/9, Mac/Firefox 2, Camino, Safari.
535 // Not broken in Mac/IE 5, Mac/Netscape 7 (?).
536 $this->rarrow = '&#x25B6;';
537 $this->larrow = '&#x25C0;';
538 }
f04721e2 539 elseif ((false !== strpos($uagent, 'Konqueror'))
0179d6be
JF
540 || (false !== strpos($uagent, 'Android'))) {
541 // The fonts on Android don't include the characters required for this to work as expected.
542 // So we use the same ones Konqueror uses.
78946b9b
PS
543 $this->rarrow = '&rarr;';
544 $this->larrow = '&larr;';
545 }
546 elseif (isset($_SERVER['HTTP_ACCEPT_CHARSET'])
547 && false === stripos($_SERVER['HTTP_ACCEPT_CHARSET'], 'utf-8')) {
548 // (Win/IE 5 doesn't set ACCEPT_CHARSET, but handles Unicode.)
549 // To be safe, non-Unicode browsers!
550 $this->rarrow = '&gt;';
551 $this->larrow = '&lt;';
552 }
553
f8129210 554 // RTL support - in RTL languages, swap r and l arrows
78946b9b
PS
555 if (right_to_left()) {
556 $t = $this->rarrow;
557 $this->rarrow = $this->larrow;
558 $this->larrow = $t;
ebebf55c 559 }
ebebf55c 560 }
78946b9b 561 }
ebebf55c 562
78946b9b
PS
563 /**
564 * Returns output renderer prefixes, these are used when looking
71c03ac1 565 * for the overridden renderers in themes.
473dd811 566 *
78946b9b
PS
567 * @return array
568 */
569 public function renderer_prefixes() {
570 global $CFG; // just in case the included files need it
571
596af93a 572 $prefixes = array('theme_'.$this->name);
78946b9b
PS
573
574 foreach ($this->parent_configs as $parent) {
575 $prefixes[] = 'theme_'.$parent->name;
576 }
577
578 return $prefixes;
34a2777c 579 }
580
571fa828 581 /**
78946b9b 582 * Returns the stylesheet URL of this editor content
473dd811 583 *
78946b9b 584 * @param bool $encoded false means use & and true use &amp; in URLs
1eb2b517 585 * @return moodle_url
571fa828 586 */
78946b9b
PS
587 public function editor_css_url($encoded=true) {
588 global $CFG;
78946b9b 589 $rev = theme_get_revision();
78946b9b 590 if ($rev > -1) {
1eb2b517 591 $url = new moodle_url("$CFG->httpswwwroot/theme/styles.php");
7070b7f2 592 if (!empty($CFG->slasharguments)) {
7070b7f2 593 $url->set_slashargument('/'.$this->name.'/'.$rev.'/editor', 'noparam', true);
7070b7f2 594 } else {
1eb2b517 595 $url->params(array('theme'=>$this->name,'rev'=>$rev, 'type'=>'editor'));
7070b7f2 596 }
78946b9b
PS
597 } else {
598 $params = array('theme'=>$this->name, 'type'=>'editor');
1eb2b517 599 $url = new moodle_url($CFG->httpswwwroot.'/theme/styles_debug.php', $params);
571fa828 600 }
1eb2b517 601 return $url;
571fa828 602 }
603
604 /**
78946b9b 605 * Returns the content of the CSS to be used in editor content
473dd811 606 *
78946b9b 607 * @return string
571fa828 608 */
71be124d 609 public function editor_css_files() {
78946b9b
PS
610 global $CFG;
611
71be124d 612 $files = array();
78946b9b
PS
613
614 // first editor plugins
bd3b3bba 615 $plugins = core_component::get_plugin_list('editor');
78946b9b
PS
616 foreach ($plugins as $plugin=>$fulldir) {
617 $sheetfile = "$fulldir/editor_styles.css";
618 if (is_readable($sheetfile)) {
71be124d 619 $files['plugin_'.$plugin] = $sheetfile;
78946b9b
PS
620 }
621 }
622 // then parent themes
623 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
624 if (empty($parent_config->editor_sheets)) {
625 continue;
626 }
627 foreach ($parent_config->editor_sheets as $sheet) {
ca194849 628 $sheetfile = "$parent_config->dir/style/$sheet.css";
78946b9b 629 if (is_readable($sheetfile)) {
71be124d 630 $files['parent_'.$parent_config->name.'_'.$sheet] = $sheetfile;
78946b9b
PS
631 }
632 }
633 }
634 // finally this theme
635 if (!empty($this->editor_sheets)) {
636 foreach ($this->editor_sheets as $sheet) {
71be124d 637 $sheetfile = "$this->dir/style/$sheet.css";
78946b9b 638 if (is_readable($sheetfile)) {
71be124d 639 $files['theme_'.$sheet] = $sheetfile;
78946b9b
PS
640 }
641 }
642 }
643
71be124d 644 return $files;
571fa828 645 }
646
647 /**
78946b9b 648 * Get the stylesheet URL of this theme
473dd811
SH
649 *
650 * @param moodle_page $page Not used... deprecated?
38aafea2 651 * @return array of moodle_url
571fa828 652 */
efaa4c08 653 public function css_urls(moodle_page $page) {
78946b9b
PS
654 global $CFG;
655
656 $rev = theme_get_revision();
657
efaa4c08 658 $urls = array();
efaa4c08 659
383b89a1
SH
660 $svg = $this->use_svg_icons();
661
78946b9b 662 if ($rev > -1) {
383b89a1 663 $url = new moodle_url("$CFG->httpswwwroot/theme/styles.php");
378b3eac 664 $separate = (core_useragent::is_ie() && !core_useragent::check_ie_version('10'));
1eb2b517
SH
665 if (!empty($CFG->slasharguments)) {
666 $slashargs = '';
667 if (!$svg) {
668 // We add a simple /_s to the start of the path.
669 // The underscore is used to ensure that it isn't a valid theme name.
670 $slashargs .= '/_s'.$slashargs;
671 }
672 $slashargs .= '/'.$this->name.'/'.$rev.'/all';
673 if ($separate) {
674 $slashargs .= '/chunk0';
675 }
676 $url->set_slashargument($slashargs, 'noparam', true);
537740cb 677 } else {
1eb2b517
SH
678 $params = array('theme' => $this->name,'rev' => $rev, 'type' => 'all');
679 if (!$svg) {
680 // We add an SVG param so that we know not to serve SVG images.
681 // We do this because all modern browsers support SVG and this param will one day be removed.
682 $params['svg'] = '0';
7070b7f2 683 }
1eb2b517
SH
684 if ($separate) {
685 $params['chunk'] = '0';
686 }
687 $url->params($params);
78946b9b 688 }
1eb2b517
SH
689 $urls[] = $url;
690
78946b9b 691 } else {
7829cf79
PS
692 // find out the current CSS and cache it now for 5 seconds
693 // the point is to construct the CSS only once and pass it through the
694 // dataroot to the script that actually serves the sheets
d6b64d15
PS
695 if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
696 define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php
697 }
bcf7f9f3 698 $candidatedir = "$CFG->cachedir/theme/$this->name";
383b89a1
SH
699 if ($svg) {
700 $candidatesheet = "$candidatedir/designer.ser";
701 } else {
702 $candidatesheet = "$candidatedir/designer_nosvg.ser";
703 }
bcf7f9f3
704 $rebuild = true;
705 if (file_exists($candidatesheet) and filemtime($candidatesheet) > time() - THEME_DESIGNER_CACHE_LIFETIME) {
7829cf79
PS
706 if ($css = file_get_contents($candidatesheet)) {
707 $css = unserialize($css);
bcf7f9f3
708 if (is_array($css)) {
709 $rebuild = false;
710 }
711 }
712 }
713 if ($rebuild) {
714 // Prepare the CSS optimiser if it is to be used,
715 // please note that it may be very slow and is therefore strongly discouraged in theme designer mode.
716 $optimiser = null;
717 if (!empty($CFG->enablecssoptimiser) && $this->supportscssoptimisation) {
718 require_once($CFG->dirroot.'/lib/csslib.php');
719 $optimiser = new css_optimiser;
7829cf79 720 }
b305d623 721 $css = $this->css_content($optimiser);
bcf7f9f3
722
723 // We do not want any errors here because this may fail easily because of the concurrent access.
724 $prevabort = ignore_user_abort(true);
725 check_dir_exists($candidatedir);
726 $tempfile = tempnam($candidatedir, 'tmpdesigner');
727 file_put_contents($tempfile, serialize($css));
728 $reporting = error_reporting(0);
729 chmod($tempfile, $CFG->filepermissions);
730 unlink($candidatesheet); // Do not rely on rename() deleting original, they may decide to change it at any time as usually.
731 rename($tempfile, $candidatesheet);
732 error_reporting($reporting);
733 ignore_user_abort($prevabort);
7829cf79
PS
734 }
735
383b89a1
SH
736 $baseurl = new moodle_url($CFG->httpswwwroot.'/theme/styles_debug.php');
737 if (!$svg) {
738 // We add an SVG param so that we know not to serve SVG images.
739 // We do this because all modern browsers support SVG and this param will one day be removed.
740 $baseurl->param('svg', '0');
741 }
378b3eac 742 if (core_useragent::is_ie()) {
c09e1a2c 743 // lalala, IE does not allow more than 31 linked CSS files from main document
71be124d 744 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name, 'type'=>'ie', 'subtype'=>'plugins'));
a6338a13
SH
745 foreach ($css['parents'] as $parent=>$sheets) {
746 // We need to serve parents individually otherwise we may easily exceed the style limit IE imposes (4096)
71be124d 747 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'ie', 'subtype'=>'parents', 'sheet'=>$parent));
a6338a13 748 }
71be124d 749 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name, 'type'=>'ie', 'subtype'=>'theme'));
c09e1a2c
PS
750
751 } else {
752 foreach ($css['plugins'] as $plugin=>$unused) {
753 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'plugin', 'subtype'=>$plugin));
754 }
755 foreach ($css['parents'] as $parent=>$sheets) {
756 foreach ($sheets as $sheet=>$unused2) {
757 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'parent', 'subtype'=>$parent, 'sheet'=>$sheet));
758 }
759 }
760 foreach ($css['theme'] as $sheet=>$unused) {
761 $urls[] = new moodle_url($baseurl, array('sheet'=>$sheet, 'theme'=>$this->name, 'type'=>'theme')); // sheet first in order to make long urls easier to read
78946b9b 762 }
78946b9b 763 }
78946b9b 764 }
efaa4c08
PS
765
766 return $urls;
571fa828 767 }
34a2777c 768
ebebf55c 769 /**
045f492c 770 * Returns an array of organised CSS files required for this output
473dd811 771 *
045f492c 772 * @return array
ebebf55c 773 */
045f492c
SH
774 public function css_files() {
775 $cssfiles = array('plugins'=>array(), 'parents'=>array(), 'theme'=>array());
78946b9b 776
8c96c3cb 777 // get all plugin sheets
045f492c 778 $excludes = $this->resolve_excludes('plugins_exclude_sheets');
78946b9b 779 if ($excludes !== true) {
46f6f7f2 780 foreach (core_component::get_plugin_types() as $type=>$unused) {
045f492c 781 if ($type === 'theme' || (!empty($excludes[$type]) and $excludes[$type] === true)) {
78946b9b
PS
782 continue;
783 }
bd3b3bba 784 $plugins = core_component::get_plugin_list($type);
78946b9b
PS
785 foreach ($plugins as $plugin=>$fulldir) {
786 if (!empty($excludes[$type]) and is_array($excludes[$type])
787 and in_array($plugin, $excludes[$type])) {
788 continue;
789 }
f8bb9666
SH
790
791 $plugincontent = '';
78946b9b
PS
792 $sheetfile = "$fulldir/styles.css";
793 if (is_readable($sheetfile)) {
045f492c 794 $cssfiles['plugins'][$type.'_'.$plugin] = $sheetfile;
f8bb9666
SH
795 }
796 $sheetthemefile = "$fulldir/styles_{$this->name}.css";
797 if (is_readable($sheetthemefile)) {
045f492c 798 $cssfiles['plugins'][$type.'_'.$plugin.'_'.$this->name] = $sheetthemefile;
f8bb9666 799 }
78946b9b
PS
800 }
801 }
802 }
34a2777c 803
78946b9b 804 // find out wanted parent sheets
045f492c 805 $excludes = $this->resolve_excludes('parents_exclude_sheets');
78946b9b
PS
806 if ($excludes !== true) {
807 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
808 $parent = $parent_config->name;
045f492c 809 if (empty($parent_config->sheets) || (!empty($excludes[$parent]) and $excludes[$parent] === true)) {
78946b9b
PS
810 continue;
811 }
812 foreach ($parent_config->sheets as $sheet) {
813 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
814 and in_array($sheet, $excludes[$parent])) {
815 continue;
816 }
817 $sheetfile = "$parent_config->dir/style/$sheet.css";
818 if (is_readable($sheetfile)) {
045f492c 819 $cssfiles['parents'][$parent][$sheet] = $sheetfile;
78946b9b
PS
820 }
821 }
822 }
823 }
34a2777c 824
78946b9b
PS
825 // current theme sheets
826 if (is_array($this->sheets)) {
827 foreach ($this->sheets as $sheet) {
828 $sheetfile = "$this->dir/style/$sheet.css";
829 if (is_readable($sheetfile)) {
045f492c 830 $cssfiles['theme'][$sheet] = $sheetfile;
78946b9b 831 }
ebebf55c 832 }
78946b9b
PS
833 }
834
045f492c
SH
835 return $cssfiles;
836 }
837
838 /**
839 * Returns the content of the one huge CSS merged from all style sheets.
473dd811 840 *
b305d623 841 * @param css_optimiser|null $optimiser A CSS optimiser to use during on the content. Null = don't optimise
045f492c
SH
842 * @return string
843 */
b305d623 844 public function css_content(css_optimiser $optimiser = null) {
71be124d 845 $files = array_merge($this->css_files(), array('editor'=>$this->editor_css_files()));
b305d623 846 $css = $this->css_files_get_contents($files, array(), $optimiser);
78946b9b
PS
847 return $css;
848 }
34a2777c 849
045f492c
SH
850 /**
851 * Given an array of file paths or a single file path loads the contents of
852 * the CSS file, processes it then returns it in the same structure it was given.
853 *
f8129210 854 * Can be used recursively on the results of {@link css_files}
045f492c
SH
855 *
856 * @param array|string $file An array of file paths or a single file path
71c03ac1 857 * @param array $keys An array of previous array keys [recursive addition]
b305d623 858 * @param css_optimiser|null $optimiser A CSS optimiser to use during on the content. Null = don't optimise
045f492c
SH
859 * @return The converted array or the contents of the single file ($file type)
860 */
b305d623
SH
861 protected function css_files_get_contents($file, array $keys, css_optimiser $optimiser = null) {
862 global $CFG;
045f492c 863 if (is_array($file)) {
04cfa387
SH
864 // We use a separate array to keep everything in the exact same order.
865 $return = array();
045f492c 866 foreach ($file as $key=>$f) {
04cfa387 867 $return[clean_param($key, PARAM_SAFEDIR)] = $this->css_files_get_contents($f, array_merge($keys, array($key)), $optimiser);
045f492c 868 }
04cfa387 869 return $return;
045f492c 870 } else {
b305d623
SH
871 $contents = file_get_contents($file);
872 $contents = $this->post_process($contents);
045f492c 873 $comment = '/** Path: '.implode(' ', $keys).' **/'."\n";
b305d623
SH
874 $stats = '';
875 if (!is_null($optimiser)) {
876 $contents = $optimiser->process($contents);
877 if (!empty($CFG->cssoptimiserstats)) {
878 $stats = $optimiser->output_stats_css();
879 }
880 }
881 return $comment.$stats.$contents;
045f492c
SH
882 }
883 }
884
e68c5f36
PS
885
886 /**
473dd811
SH
887 * Generate a URL to the file that serves theme JavaScript files.
888 *
d7656956
ARN
889 * If we determine that the theme has no relevant files, then we return
890 * early with a null value.
891 *
71c03ac1 892 * @param bool $inhead true means head url, false means footer
d7656956 893 * @return moodle_url|null
e68c5f36 894 */
baeb20bb 895 public function javascript_url($inhead) {
e68c5f36
PS
896 global $CFG;
897
898 $rev = theme_get_revision();
e68c5f36 899 $params = array('theme'=>$this->name,'rev'=>$rev);
baeb20bb
PS
900 $params['type'] = $inhead ? 'head' : 'footer';
901
d7656956
ARN
902 // Return early if there are no files to serve
903 if (count($this->javascript_files($params['type'])) === 0) {
904 return null;
905 }
906
ecbad2ad
PS
907 if (!empty($CFG->slasharguments) and $rev > 0) {
908 $url = new moodle_url("$CFG->httpswwwroot/theme/javascript.php");
909 $url->set_slashargument('/'.$this->name.'/'.$rev.'/'.$params['type'], 'noparam', true);
910 return $url;
911 } else {
912 return new moodle_url($CFG->httpswwwroot.'/theme/javascript.php', $params);
913 }
e68c5f36
PS
914 }
915
473dd811
SH
916 /**
917 * Get the URL's for the JavaScript files used by this theme.
918 * They won't be served directly, instead they'll be mediated through
919 * theme/javascript.php.
920 *
921 * @param string $type Either javascripts_footer, or javascripts
922 * @return array
923 */
045f492c
SH
924 public function javascript_files($type) {
925 if ($type === 'footer') {
926 $type = 'javascripts_footer';
927 } else {
f856106b 928 $type = 'javascripts';
045f492c 929 }
04c01408 930
358c13cb
PS
931 $js = array();
932 // find out wanted parent javascripts
045f492c 933 $excludes = $this->resolve_excludes('parents_exclude_javascripts');
358c13cb
PS
934 if ($excludes !== true) {
935 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
936 $parent = $parent_config->name;
04c01408 937 if (empty($parent_config->$type)) {
358c13cb
PS
938 continue;
939 }
940 if (!empty($excludes[$parent]) and $excludes[$parent] === true) {
941 continue;
942 }
04c01408 943 foreach ($parent_config->$type as $javascript) {
358c13cb
PS
944 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
945 and in_array($javascript, $excludes[$parent])) {
946 continue;
947 }
948 $javascriptfile = "$parent_config->dir/javascript/$javascript.js";
949 if (is_readable($javascriptfile)) {
045f492c 950 $js[] = $javascriptfile;
358c13cb
PS
951 }
952 }
953 }
954 }
955
956 // current theme javascripts
04c01408
PS
957 if (is_array($this->$type)) {
958 foreach ($this->$type as $javascript) {
358c13cb
PS
959 $javascriptfile = "$this->dir/javascript/$javascript.js";
960 if (is_readable($javascriptfile)) {
045f492c
SH
961 $js[] = $javascriptfile;
962 }
963 }
964 }
045f492c
SH
965 return $js;
966 }
967
968 /**
3d3fae72 969 * Resolves an exclude setting to the themes setting is applicable or the
045f492c
SH
970 * setting of its closest parent.
971 *
972 * @param string $variable The name of the setting the exclude setting to resolve
f8129210 973 * @param string $default
045f492c
SH
974 * @return mixed
975 */
f8129210 976 protected function resolve_excludes($variable, $default = null) {
045f492c
SH
977 $setting = $default;
978 if (is_array($this->{$variable}) or $this->{$variable} === true) {
979 $setting = $this->{$variable};
980 } else {
981 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
982 if (!isset($parent_config->{$variable})) {
983 continue;
984 }
985 if (is_array($parent_config->{$variable}) or $parent_config->{$variable} === true) {
986 $setting = $parent_config->{$variable};
987 break;
358c13cb
PS
988 }
989 }
990 }
045f492c
SH
991 return $setting;
992 }
358c13cb 993
045f492c
SH
994 /**
995 * Returns the content of the one huge javascript file merged from all theme javascript files.
473dd811 996 *
f8129210 997 * @param bool $type
045f492c
SH
998 * @return string
999 */
1000 public function javascript_content($type) {
1001 $jsfiles = $this->javascript_files($type);
1002 $js = '';
1003 foreach ($jsfiles as $jsfile) {
1004 $js .= file_get_contents($jsfile)."\n";
1005 }
1006 return $js;
e68c5f36
PS
1007 }
1008
473dd811
SH
1009 /**
1010 * Post processes CSS.
1011 *
1012 * This method post processes all of the CSS before it is served for this theme.
1013 * This is done so that things such as image URL's can be swapped in and to
1014 * run any specific CSS post process method the theme has requested.
3d3fae72 1015 * This allows themes to use CSS settings.
473dd811
SH
1016 *
1017 * @param string $css The CSS to process.
1018 * @return string The processed CSS.
1019 */
045f492c 1020 public function post_process($css) {
78946b9b 1021 // now resolve all image locations
37824e73 1022 if (preg_match_all('/\[\[pix:([a-z0-9_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) {
78946b9b
PS
1023 $replaced = array();
1024 foreach ($matches as $match) {
1025 if (isset($replaced[$match[0]])) {
1026 continue;
1027 }
1028 $replaced[$match[0]] = true;
1029 $imagename = $match[2];
1030 $component = rtrim($match[1], '|');
b9bc2019 1031 $imageurl = $this->pix_url($imagename, $component)->out(false);
df06c1c4 1032 // we do not need full url because the image.php is always in the same dir
9d473266 1033 $imageurl = preg_replace('|^http.?://[^/]+|', '', $imageurl);
df06c1c4 1034 $css = str_replace($match[0], $imageurl, $css);
ebebf55c 1035 }
34a2777c 1036 }
17a6649b 1037
9ba6076c 1038 // Now resolve all font locations.
37824e73 1039 if (preg_match_all('/\[\[font:([a-z0-9_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) {
9ba6076c
PS
1040 $replaced = array();
1041 foreach ($matches as $match) {
1042 if (isset($replaced[$match[0]])) {
1043 continue;
1044 }
1045 $replaced[$match[0]] = true;
1046 $fontname = $match[2];
1047 $component = rtrim($match[1], '|');
1048 $fonturl = $this->font_url($fontname, $component)->out(false);
1049 // We do not need full url because the font.php is always in the same dir.
1050 $fonturl = preg_replace('|^http.?://[^/]+|', '', $fonturl);
1051 $css = str_replace($match[0], $fonturl, $css);
1052 }
1053 }
1054
78946b9b
PS
1055 // now resolve all theme settings or do any other postprocessing
1056 $csspostprocess = $this->csspostprocess;
1057 if (function_exists($csspostprocess)) {
1058 $css = $csspostprocess($css, $this);
fdeb7fa1 1059 }
1060
78946b9b 1061 return $css;
ebebf55c 1062 }
17a6649b 1063
ebebf55c 1064 /**
78946b9b
PS
1065 * Return the URL for an image
1066 *
1067 * @param string $imagename the name of the icon.
f8129210 1068 * @param string $component specification of one plugin like in get_string()
78946b9b 1069 * @return moodle_url
fdeb7fa1 1070 */
c39e5ba2 1071 public function pix_url($imagename, $component) {
fdeb7fa1 1072 global $CFG;
78946b9b 1073
9d473266 1074 $params = array('theme'=>$this->name);
436dbeec 1075 $svg = $this->use_svg_icons();
9d473266
PS
1076
1077 if (empty($component) or $component === 'moodle' or $component === 'core') {
1078 $params['component'] = 'core';
1079 } else {
1080 $params['component'] = $component;
1081 }
78946b9b
PS
1082
1083 $rev = theme_get_revision();
1084 if ($rev != -1) {
1085 $params['rev'] = $rev;
1086 }
9d473266
PS
1087
1088 $params['image'] = $imagename;
1089
436dbeec 1090 $url = new moodle_url("$CFG->httpswwwroot/theme/image.php");
9d473266 1091 if (!empty($CFG->slasharguments) and $rev > 0) {
436dbeec
SH
1092 $path = '/'.$params['theme'].'/'.$params['component'].'/'.$params['rev'].'/'.$params['image'];
1093 if (!$svg) {
1094 // We add a simple /_s to the start of the path.
1095 // The underscore is used to ensure that it isn't a valid theme name.
1096 $path = '/_s'.$path;
1097 }
1098 $url->set_slashargument($path, 'noparam', true);
9d473266 1099 } else {
436dbeec
SH
1100 if (!$svg) {
1101 // We add an SVG param so that we know not to serve SVG images.
1102 // We do this because all modern browsers support SVG and this param will one day be removed.
1103 $params['svg'] = '0';
1104 }
1105 $url->params($params);
fdeb7fa1 1106 }
78946b9b 1107
9d473266 1108 return $url;
fdeb7fa1 1109 }
1110
9ba6076c
PS
1111 /**
1112 * Return the URL for a font
1113 *
1114 * @param string $font the name of the font (including extension).
1115 * @param string $component specification of one plugin like in get_string()
1116 * @return moodle_url
1117 */
1118 public function font_url($font, $component) {
1119 global $CFG;
1120
1121 $params = array('theme'=>$this->name);
1122
1123 if (empty($component) or $component === 'moodle' or $component === 'core') {
1124 $params['component'] = 'core';
1125 } else {
1126 $params['component'] = $component;
1127 }
1128
1129 $rev = theme_get_revision();
1130 if ($rev != -1) {
1131 $params['rev'] = $rev;
1132 }
1133
1134 $params['font'] = $font;
1135
1136 $url = new moodle_url("$CFG->httpswwwroot/theme/font.php");
1137 if (!empty($CFG->slasharguments) and $rev > 0) {
1138 $path = '/'.$params['theme'].'/'.$params['component'].'/'.$params['rev'].'/'.$params['font'];
1139 $url->set_slashargument($path, 'noparam', true);
1140 } else {
1141 $url->params($params);
1142 }
1143
1144 return $url;
1145 }
1146
fe7b75f8
PS
1147 /**
1148 * Returns URL to the stored file via pluginfile.php.
1149 *
1150 * Note the theme must also implement pluginfile.php handler,
1151 * theme revision is used instead of the itemid.
1152 *
1153 * @param string $setting
1154 * @param string $filearea
1155 * @return string protocol relative URL or null if not present
1156 */
1157 public function setting_file_url($setting, $filearea) {
1158 global $CFG;
1159
1160 if (empty($this->settings->$setting)) {
1161 return null;
1162 }
1163
1164 $component = 'theme_'.$this->name;
1165 $itemid = theme_get_revision();
1166 $filepath = $this->settings->$setting;
1167 $syscontext = context_system::instance();
1168
1169 $url = moodle_url::make_file_url("$CFG->wwwroot/pluginfile.php", "/$syscontext->id/$component/$filearea/$itemid".$filepath);
1170
1171 // Now this is tricky because the we can not hardcode http or https here, lets use the relative link.
1172 // Note: unfortunately moodle_url does not support //urls yet.
1173
1174 $url = preg_replace('|^https?://|i', '//', $url->out(false));
1175
1176 return $url;
1177 }
1178
1179 /**
1180 * Serve the theme setting file.
1181 *
1182 * @param string $filearea
1183 * @param array $args
1184 * @param bool $forcedownload
1185 * @param array $options
1186 * @return bool may terminate if file not found or donotdie not specified
1187 */
1188 public function setting_file_serve($filearea, $args, $forcedownload, $options) {
1189 global $CFG;
1190 require_once("$CFG->libdir/filelib.php");
1191
1192 $syscontext = context_system::instance();
1193 $component = 'theme_'.$this->name;
1194
1195 $revision = array_shift($args);
1196 if ($revision < 0) {
1197 $lifetime = 0;
1198 } else {
1199 $lifetime = 60*60*24*60;
1200 }
1201
1202 $fs = get_file_storage();
1203 $relativepath = implode('/', $args);
1204
1205 $fullpath = "/{$syscontext->id}/{$component}/{$filearea}/0/{$relativepath}";
1206 $fullpath = rtrim($fullpath, '/');
1207 if ($file = $fs->get_file_by_hash(sha1($fullpath))) {
1208 send_stored_file($file, $lifetime, 0, $forcedownload, $options);
1209 return true;
1210 } else {
1211 send_file_not_found();
1212 }
1213 }
1214
fdeb7fa1 1215 /**
78946b9b 1216 * Resolves the real image location.
436dbeec
SH
1217 *
1218 * $svg was introduced as an arg in 2.4. It is important because not all supported browsers support the use of SVG
1219 * and we need a way in which to turn it off.
1220 * By default SVG won't be used unless asked for. This is done for two reasons:
1221 * 1. It ensures that we don't serve svg images unless we really want to. The admin has selected to force them, of the users
1222 * browser supports SVG.
1223 * 2. We only serve SVG images from locations we trust. This must NOT include any areas where the image may have been uploaded
1224 * by the user due to security concerns.
1225 *
78946b9b
PS
1226 * @param string $image name of image, may contain relative path
1227 * @param string $component
436dbeec 1228 * @param bool $svg If set to true SVG images will also be looked for.
78946b9b 1229 * @return string full file path
fdeb7fa1 1230 */
436dbeec 1231 public function resolve_image_location($image, $component, $svg = false) {
78946b9b
PS
1232 global $CFG;
1233
436dbeec
SH
1234 if (!is_bool($svg)) {
1235 // If $svg isn't a bool then we need to decide for ourselves.
1236 $svg = $this->use_svg_icons();
1237 }
1238
78946b9b 1239 if ($component === 'moodle' or $component === 'core' or empty($component)) {
436dbeec 1240 if ($imagefile = $this->image_exists("$this->dir/pix_core/$image", $svg)) {
78946b9b
PS
1241 return $imagefile;
1242 }
1243 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
436dbeec 1244 if ($imagefile = $this->image_exists("$parent_config->dir/pix_core/$image", $svg)) {
78946b9b
PS
1245 return $imagefile;
1246 }
1247 }
436dbeec 1248 if ($imagefile = $this->image_exists("$CFG->dataroot/pix/$image", $svg)) {
f930cf6b
EM
1249 return $imagefile;
1250 }
436dbeec 1251 if ($imagefile = $this->image_exists("$CFG->dirroot/pix/$image", $svg)) {
78946b9b
PS
1252 return $imagefile;
1253 }
1254 return null;
1255
1256 } else if ($component === 'theme') { //exception
1257 if ($image === 'favicon') {
1258 return "$this->dir/pix/favicon.ico";
1259 }
436dbeec 1260 if ($imagefile = $this->image_exists("$this->dir/pix/$image", $svg)) {
78946b9b
PS
1261 return $imagefile;
1262 }
1263 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
436dbeec 1264 if ($imagefile = $this->image_exists("$parent_config->dir/pix/$image", $svg)) {
78946b9b
PS
1265 return $imagefile;
1266 }
1267 }
1268 return null;
1269
78946b9b
PS
1270 } else {
1271 if (strpos($component, '_') === false) {
1272 $component = 'mod_'.$component;
1273 }
1274 list($type, $plugin) = explode('_', $component, 2);
1275
436dbeec 1276 if ($imagefile = $this->image_exists("$this->dir/pix_plugins/$type/$plugin/$image", $svg)) {
78946b9b
PS
1277 return $imagefile;
1278 }
1279 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
436dbeec 1280 if ($imagefile = $this->image_exists("$parent_config->dir/pix_plugins/$type/$plugin/$image", $svg)) {
78946b9b
PS
1281 return $imagefile;
1282 }
1283 }
436dbeec 1284 if ($imagefile = $this->image_exists("$CFG->dataroot/pix_plugins/$type/$plugin/$image", $svg)) {
f930cf6b
EM
1285 return $imagefile;
1286 }
1c74b260 1287 $dir = core_component::get_plugin_directory($type, $plugin);
436dbeec 1288 if ($imagefile = $this->image_exists("$dir/pix/$image", $svg)) {
78946b9b
PS
1289 return $imagefile;
1290 }
1291 return null;
fdeb7fa1 1292 }
fdeb7fa1 1293 }
1294
9ba6076c
PS
1295 /**
1296 * Resolves the real font location.
1297 *
1298 * @param string $font name of font file
1299 * @param string $component
1300 * @return string full file path
1301 */
1302 public function resolve_font_location($font, $component) {
1303 global $CFG;
1304
1305 if ($component === 'moodle' or $component === 'core' or empty($component)) {
1306 if (file_exists("$this->dir/fonts_core/$font")) {
1307 return "$this->dir/fonts_core/$font";
1308 }
1309 foreach (array_reverse($this->parent_configs) as $parent_config) { // Base first, the immediate parent last.
1310 if (file_exists("$parent_config->dir/fonts_core/$font")) {
1311 return "$parent_config->dir/fonts_core/$font";
1312 }
1313 }
1314 if (file_exists("$CFG->dataroot/fonts/$font")) {
1315 return "$CFG->dataroot/fonts/$font";
1316 }
1317 if (file_exists("$CFG->dirroot/lib/fonts/$font")) {
1318 return "$CFG->dirroot/lib/fonts/$font";
1319 }
1320 return null;
1321
1322 } else if ($component === 'theme') { // Exception.
1323 if (file_exists("$this->dir/fonts/$font")) {
1324 return "$this->dir/fonts/$font";
1325 }
1326 foreach (array_reverse($this->parent_configs) as $parent_config) { // Base first, the immediate parent last.
1327 if (file_exists("$parent_config->dir/fonts/$font")) {
1328 return "$parent_config->dir/fonts/$font";
1329 }
1330 }
1331 return null;
1332
1333 } else {
1334 if (strpos($component, '_') === false) {
1335 $component = 'mod_'.$component;
1336 }
1337 list($type, $plugin) = explode('_', $component, 2);
1338
1339 if (file_exists("$this->dir/fonts_plugins/$type/$plugin/$font")) {
1340 return "$this->dir/fonts_plugins/$type/$plugin/$font";
1341 }
1342 foreach (array_reverse($this->parent_configs) as $parent_config) { // Base first, the immediate parent last.
1343 if (file_exists("$parent_config->dir/fonts_plugins/$type/$plugin/$font")) {
1344 return "$parent_config->dir/fonts_plugins/$type/$plugin/$font";
1345 }
1346 }
1347 if (file_exists("$CFG->dataroot/fonts_plugins/$type/$plugin/$font")) {
1348 return "$CFG->dataroot/fonts_plugins/$type/$plugin/$font";
1349 }
1350 $dir = core_component::get_plugin_directory($type, $plugin);
1351 if (file_exists("$dir/fonts/$font")) {
1352 return "$dir/fonts/$font";
1353 }
1354 return null;
1355 }
1356 }
1357
436dbeec
SH
1358 /**
1359 * Return true if we should look for SVG images as well.
1360 *
1361 * @staticvar bool $svg
1362 * @return bool
1363 */
1364 public function use_svg_icons() {
1365 global $CFG;
1366 if ($this->usesvg === null) {
c3d2fbf9 1367
436dbeec 1368 if (!isset($CFG->svgicons) || !is_bool($CFG->svgicons)) {
c3d2fbf9 1369 $this->usesvg = core_useragent::supports_svg();
436dbeec
SH
1370 } else {
1371 // Force them on/off depending upon the setting.
1372 $this->usesvg = $CFG->svgicons;
1373 }
1374 }
1375 return $this->usesvg;
1376 }
1377
f7d6a556
SH
1378 /**
1379 * Forces the usesvg setting to either true or false, avoiding any decision making.
1380 *
1381 * This function should only ever be used when absolutely required, and before any generation of image URL's has occurred.
383b89a1 1382 * DO NOT ABUSE THIS FUNCTION... not that you'd want to right ;)
f7d6a556
SH
1383 *
1384 * @param bool $setting True to force the use of svg when available, null otherwise.
1385 */
383b89a1 1386 public function force_svg_use($setting) {
f7d6a556
SH
1387 $this->usesvg = (bool)$setting;
1388 }
1389
d4a03c00 1390 /**
78946b9b 1391 * Checks if file with any image extension exists.
473dd811 1392 *
436dbeec
SH
1393 * The order to these images was adjusted prior to the release of 2.4
1394 * At that point the were the following image counts in Moodle core:
1395 *
1396 * - png = 667 in pix dirs (1499 total)
1397 * - gif = 385 in pix dirs (606 total)
1398 * - jpg = 62 in pix dirs (74 total)
1399 * - jpeg = 0 in pix dirs (1 total)
1400 *
1401 * There is work in progress to move towards SVG presently hence that has been prioritiesed.
1402 *
78946b9b 1403 * @param string $filepath
436dbeec 1404 * @param bool $svg If set to true SVG images will also be looked for.
78946b9b 1405 * @return string image name with extension
d4a03c00 1406 */
436dbeec
SH
1407 private static function image_exists($filepath, $svg = false) {
1408 if ($svg && file_exists("$filepath.svg")) {
1409 return "$filepath.svg";
78946b9b
PS
1410 } else if (file_exists("$filepath.png")) {
1411 return "$filepath.png";
436dbeec
SH
1412 } else if (file_exists("$filepath.gif")) {
1413 return "$filepath.gif";
78946b9b
PS
1414 } else if (file_exists("$filepath.jpg")) {
1415 return "$filepath.jpg";
1416 } else if (file_exists("$filepath.jpeg")) {
1417 return "$filepath.jpeg";
d4a03c00 1418 } else {
78946b9b 1419 return false;
d4a03c00 1420 }
1421 }
1422
fdeb7fa1 1423 /**
78946b9b 1424 * Loads the theme config from config.php file.
473dd811 1425 *
78946b9b 1426 * @param string $themename
473dd811
SH
1427 * @param stdClass $settings from config_plugins table
1428 * @return stdClass The theme configuration
fdeb7fa1 1429 */
78946b9b
PS
1430 private static function find_theme_config($themename, $settings) {
1431 // We have to use the variable name $THEME (upper case) because that
1432 // is what is used in theme config.php files.
fdeb7fa1 1433
78946b9b
PS
1434 if (!$dir = theme_config::find_theme_location($themename)) {
1435 return null;
fdeb7fa1 1436 }
2f67a9b3 1437
365a5941 1438 $THEME = new stdClass();
78946b9b
PS
1439 $THEME->name = $themename;
1440 $THEME->dir = $dir;
1441 $THEME->settings = $settings;
1442
1443 global $CFG; // just in case somebody tries to use $CFG in theme config
1444 include("$THEME->dir/config.php");
1445
1446 // verify the theme configuration is OK
1447 if (!is_array($THEME->parents)) {
1448 // parents option is mandatory now
1449 return null;
fdeb7fa1 1450 }
1451
78946b9b 1452 return $THEME;
fdeb7fa1 1453 }
1454
d4a03c00 1455 /**
78946b9b
PS
1456 * Finds the theme location and verifies the theme has all needed files
1457 * and is not obsoleted.
473dd811 1458 *
78946b9b
PS
1459 * @param string $themename
1460 * @return string full dir path or null if not found
d4a03c00 1461 */
78946b9b
PS
1462 private static function find_theme_location($themename) {
1463 global $CFG;
1464
1465 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
1466 $dir = "$CFG->dirroot/theme/$themename";
d4a03c00 1467
3dd1d7e7
DM
1468 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
1469 $dir = "$CFG->themedir/$themename";
1470
7d875874 1471 } else {
78946b9b 1472 return null;
d4a03c00 1473 }
78946b9b
PS
1474
1475 if (file_exists("$dir/styles.php")) {
1476 //legacy theme - needs to be upgraded - upgrade info is displayed on the admin settings page
1477 return null;
1478 }
2f67a9b3 1479
78946b9b 1480 return $dir;
d4a03c00 1481 }
1482
1d13c75c 1483 /**
78946b9b 1484 * Get the renderer for a part of Moodle for this theme.
473dd811 1485 *
78946b9b 1486 * @param moodle_page $page the page we are rendering
f8129210 1487 * @param string $component the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
78946b9b 1488 * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
c927e35c 1489 * @param string $target one of rendering target constants
78946b9b 1490 * @return renderer_base the requested renderer.
1d13c75c 1491 */
c927e35c 1492 public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
78946b9b 1493 if (is_null($this->rf)) {
c927e35c 1494 $classname = $this->rendererfactory;
78946b9b 1495 $this->rf = new $classname($this);
1d13c75c 1496 }
78946b9b 1497
c927e35c 1498 return $this->rf->get_renderer($page, $component, $subtype, $target);
1d13c75c 1499 }
1500
fdeb7fa1 1501 /**
78946b9b 1502 * Get the information from {@link $layouts} for this type of page.
473dd811 1503 *
78946b9b
PS
1504 * @param string $pagelayout the the page layout name.
1505 * @return array the appropriate part of {@link $layouts}.
fdeb7fa1 1506 */
78946b9b
PS
1507 protected function layout_info_for_page($pagelayout) {
1508 if (array_key_exists($pagelayout, $this->layouts)) {
1509 return $this->layouts[$pagelayout];
1510 } else {
191b267b
PS
1511 debugging('Invalid page layout specified: ' . $pagelayout);
1512 return $this->layouts['standard'];
fdeb7fa1 1513 }
1514 }
1515
1516 /**
78946b9b
PS
1517 * Given the settings of this theme, and the page pagelayout, return the
1518 * full path of the page layout file to use.
1519 *
1520 * Used by {@link core_renderer::header()}.
1521 *
1522 * @param string $pagelayout the the page layout name.
1523 * @return string Full path to the lyout file to use
ebebf55c 1524 */
78946b9b 1525 public function layout_file($pagelayout) {
ebebf55c 1526 global $CFG;
fdeb7fa1 1527
78946b9b
PS
1528 $layoutinfo = $this->layout_info_for_page($pagelayout);
1529 $layoutfile = $layoutinfo['file'];
fdeb7fa1 1530
84db3ea2
SH
1531 if (array_key_exists('theme', $layoutinfo)) {
1532 $themes = array($layoutinfo['theme']);
1533 } else {
1534 $themes = array_merge(array($this->name),$this->parents);
1535 }
4399b9d5 1536
84db3ea2
SH
1537 foreach ($themes as $theme) {
1538 if ($dir = $this->find_theme_location($theme)) {
1539 $path = "$dir/layout/$layoutfile";
1540
1541 // Check the template exists, return general base theme template if not.
1542 if (is_readable($path)) {
1543 return $path;
1544 }
78946b9b 1545 }
34a2777c 1546 }
1547
191b267b 1548 debugging('Can not find layout file for: ' . $pagelayout);
78946b9b
PS
1549 // fallback to standard normal layout
1550 return "$CFG->dirroot/theme/base/layout/general.php";
1551 }
ebebf55c 1552
78946b9b
PS
1553 /**
1554 * Returns auxiliary page layout options specified in layout configuration array.
473dd811 1555 *
78946b9b
PS
1556 * @param string $pagelayout
1557 * @return array
1558 */
1559 public function pagelayout_options($pagelayout) {
1560 $info = $this->layout_info_for_page($pagelayout);
1561 if (!empty($info['options'])) {
1562 return $info['options'];
34a2777c 1563 }
78946b9b
PS
1564 return array();
1565 }
34a2777c 1566
78946b9b
PS
1567 /**
1568 * Inform a block_manager about the block regions this theme wants on this
1569 * page layout.
473dd811 1570 *
78946b9b
PS
1571 * @param string $pagelayout the general type of the page.
1572 * @param block_manager $blockmanager the block_manger to set up.
78946b9b
PS
1573 */
1574 public function setup_blocks($pagelayout, $blockmanager) {
1575 $layoutinfo = $this->layout_info_for_page($pagelayout);
1576 if (!empty($layoutinfo['regions'])) {
1577 $blockmanager->add_regions($layoutinfo['regions']);
1578 $blockmanager->set_default_region($layoutinfo['defaultregion']);
ebebf55c 1579 }
34a2777c 1580 }
1581
473dd811
SH
1582 /**
1583 * Gets the visible name for the requested block region.
1584 *
1585 * @param string $region The region name to get
1586 * @param string $theme The theme the region belongs to (may come from the parent theme)
1587 * @return string
1588 */
cadc0d28
TH
1589 protected function get_region_name($region, $theme) {
1590 $regionstring = get_string('region-' . $region, 'theme_' . $theme);
1591 // A name exists in this theme, so use it
1592 if (substr($regionstring, 0, 1) != '[') {
1593 return $regionstring;
1594 }
1595
1596 // Otherwise, try to find one elsewhere
1597 // Check parents, if any
1598 foreach ($this->parents as $parentthemename) {
1599 $regionstring = get_string('region-' . $region, 'theme_' . $parentthemename);
1600 if (substr($regionstring, 0, 1) != '[') {
1601 return $regionstring;
1602 }
1603 }
1604
1605 // Last resort, try the base theme for names
1606 return get_string('region-' . $region, 'theme_base');
1607 }
1608
ebebf55c 1609 /**
78946b9b 1610 * Get the list of all block regions known to this theme in all templates.
473dd811 1611 *
78946b9b 1612 * @return array internal region name => human readable name.
ebebf55c 1613 */
78946b9b
PS
1614 public function get_all_block_regions() {
1615 $regions = array();
78946b9b
PS
1616 foreach ($this->layouts as $layoutinfo) {
1617 foreach ($layoutinfo['regions'] as $region) {
a9535f79 1618 $regions[$region] = $this->get_region_name($region, $this->name);
ebebf55c 1619 }
34a2777c 1620 }
78946b9b 1621 return $regions;
34a2777c 1622 }
d609d962
SH
1623
1624 /**
1625 * Returns the human readable name of the theme
1626 *
1627 * @return string
1628 */
1629 public function get_theme_name() {
1630 return get_string('pluginname', 'theme_'.$this->name);
1631 }
ebebf55c 1632}
34a2777c 1633
ebebf55c 1634/**
d9c8f425 1635 * This class keeps track of which HTML tags are currently open.
1636 *
1637 * This makes it much easier to always generate well formed XHTML output, even
1638 * if execution terminates abruptly. Any time you output some opening HTML
1639 * without the matching closing HTML, you should push the necessary close tags
1640 * onto the stack.
ebebf55c 1641 *
1642 * @copyright 2009 Tim Hunt
473dd811
SH
1643 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1644 * @since Moodle 2.0
f8129210 1645 * @package core
76be40cc 1646 * @category output
ebebf55c 1647 */
d9c8f425 1648class xhtml_container_stack {
473dd811
SH
1649
1650 /**
76be40cc 1651 * @var array Stores the list of open containers.
473dd811 1652 */
d9c8f425 1653 protected $opencontainers = array();
473dd811 1654
fa1afe32 1655 /**
76be40cc 1656 * @var array In developer debug mode, stores a stack trace of all opens and
d9c8f425 1657 * closes, so we can output helpful error messages when there is a mismatch.
fa1afe32 1658 */
d9c8f425 1659 protected $log = array();
473dd811 1660
fa1afe32 1661 /**
76be40cc
SH
1662 * @var boolean Store whether we are developer debug mode. We need this in
1663 * several places including in the destructor where we may not have access to $CFG.
fa1afe32 1664 */
d9c8f425 1665 protected $isdebugging;
34a2777c 1666
473dd811
SH
1667 /**
1668 * Constructor
1669 */
d9c8f425 1670 public function __construct() {
96f81ea3
PS
1671 global $CFG;
1672 $this->isdebugging = $CFG->debugdeveloper;
ebebf55c 1673 }
34a2777c 1674
fa1afe32 1675 /**
d9c8f425 1676 * Push the close HTML for a recently opened container onto the stack.
473dd811 1677 *
d9c8f425 1678 * @param string $type The type of container. This is checked when {@link pop()}
1679 * is called and must match, otherwise a developer debug warning is output.
1680 * @param string $closehtml The HTML required to close the container.
fa1afe32 1681 */
d9c8f425 1682 public function push($type, $closehtml) {
1683 $container = new stdClass;
1684 $container->type = $type;
1685 $container->closehtml = $closehtml;
1686 if ($this->isdebugging) {
1687 $this->log('Open', $type);
3aaa27f4 1688 }
d9c8f425 1689 array_push($this->opencontainers, $container);
ebebf55c 1690 }
34a2777c 1691
fa1afe32 1692 /**
d9c8f425 1693 * Pop the HTML for the next closing container from the stack. The $type
1694 * must match the type passed when the container was opened, otherwise a
1695 * warning will be output.
473dd811 1696 *
d9c8f425 1697 * @param string $type The type of container.
1698 * @return string the HTML required to close the container.
fa1afe32 1699 */
d9c8f425 1700 public function pop($type) {
1701 if (empty($this->opencontainers)) {
1702 debugging('<p>There are no more open containers. This suggests there is a nesting problem.</p>' .
1703 $this->output_log(), DEBUG_DEVELOPER);
1704 return;
3aaa27f4 1705 }
ebebf55c 1706
d9c8f425 1707 $container = array_pop($this->opencontainers);
1708 if ($container->type != $type) {
1709 debugging('<p>The type of container to be closed (' . $container->type .
1710 ') does not match the type of the next open container (' . $type .
1711 '). This suggests there is a nesting problem.</p>' .
1712 $this->output_log(), DEBUG_DEVELOPER);
ebebf55c 1713 }
d9c8f425 1714 if ($this->isdebugging) {
1715 $this->log('Close', $type);
e8775320 1716 }
d9c8f425 1717 return $container->closehtml;
ebebf55c 1718 }
e8775320 1719
fa1afe32 1720 /**
d9c8f425 1721 * Close all but the last open container. This is useful in places like error
1722 * handling, where you want to close all the open containers (apart from <body>)
1723 * before outputting the error message.
473dd811 1724 *
d9c8f425 1725 * @param bool $shouldbenone assert that the stack should be empty now - causes a
1726 * developer debug warning if it isn't.
1727 * @return string the HTML required to close any open containers inside <body>.
fa1afe32 1728 */
d9c8f425 1729 public function pop_all_but_last($shouldbenone = false) {
1730 if ($shouldbenone && count($this->opencontainers) != 1) {
1731 debugging('<p>Some HTML tags were opened in the body of the page but not closed.</p>' .
1732 $this->output_log(), DEBUG_DEVELOPER);
1733 }
1734 $output = '';
1735 while (count($this->opencontainers) > 1) {
1736 $container = array_pop($this->opencontainers);
1737 $output .= $container->closehtml;
e8775320 1738 }
d9c8f425 1739 return $output;
e8775320 1740 }
34a2777c 1741
ebebf55c 1742 /**
d9c8f425 1743 * You can call this function if you want to throw away an instance of this
1744 * class without properly emptying the stack (for example, in a unit test).
1745 * Calling this method stops the destruct method from outputting a developer
1746 * debug warning. After calling this method, the instance can no longer be used.
ebebf55c 1747 */
d9c8f425 1748 public function discard() {
1749 $this->opencontainers = null;
ebebf55c 1750 }
d9c8f425 1751
fa1afe32 1752 /**
d9c8f425 1753 * Adds an entry to the log.
473dd811 1754 *
d9c8f425 1755 * @param string $action The name of the action
1756 * @param string $type The type of action
fa1afe32 1757 */
d9c8f425 1758 protected function log($action, $type) {
1759 $this->log[] = '<li>' . $action . ' ' . $type . ' at:' .
1760 format_backtrace(debug_backtrace()) . '</li>';
ebebf55c 1761 }
34a2777c 1762
fa1afe32 1763 /**
d9c8f425 1764 * Outputs the log's contents as a HTML list.
473dd811 1765 *
d9c8f425 1766 * @return string HTML list of the log
fa1afe32 1767 */
d9c8f425 1768 protected function output_log() {
1769 return '<ul>' . implode("\n", $this->log) . '</ul>';
34a2777c 1770 }
6fc267a0 1771}