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 */
eeb08203 332 public $blockrtlmanipulations = null;
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.
eeb08203
SH
491 if (!is_array($this->blockrtlmanipulations)) {
492 $blockrtlmanipulations = array();
493 foreach ($this->parent_configs as $parentconfig) {
494 if (isset($parentconfig->blockrtlmanipulations)) {
495 $blockrtlmanipulations = $parentconfig->blockrtlmanipulations;
496 break;
497 }
498 }
499 $this->blockrtlmanipulations = $blockrtlmanipulations;
e89dfdbc
NK
500 }
501
78946b9b
PS
502 //fix arrows if needed
503 $this->check_theme_arrows();
571fa828 504 }
505
63c88397
PS
506 /**
507 * Let the theme initialise the page object (usually $PAGE).
508 *
509 * This may be used for example to request jQuery in add-ons.
510 *
511 * @param moodle_page $page
512 */
513 public function init_page(moodle_page $page) {
514 $themeinitfunction = 'theme_'.$this->name.'_page_init';
515 if (function_exists($themeinitfunction)) {
516 $themeinitfunction($page);
517 }
518 }
519
f8129210 520 /**
78946b9b
PS
521 * Checks if arrows $THEME->rarrow, $THEME->larrow have been set (theme/-/config.php).
522 * If not it applies sensible defaults.
523 *
524 * Accessibility: right and left arrow Unicode characters for breadcrumb, calendar,
525 * search forum block, etc. Important: these are 'silent' in a screen-reader
526 * (unlike &gt; &raquo;), and must be accompanied by text.
34a2777c 527 */
78946b9b
PS
528 private function check_theme_arrows() {
529 if (!isset($this->rarrow) and !isset($this->larrow)) {
530 // Default, looks good in Win XP/IE 6, Win/Firefox 1.5, Win/Netscape 8...
531 // Also OK in Win 9x/2K/IE 5.x
532 $this->rarrow = '&#x25BA;';
533 $this->larrow = '&#x25C4;';
534 if (empty($_SERVER['HTTP_USER_AGENT'])) {
535 $uagent = '';
ebebf55c 536 } else {
78946b9b
PS
537 $uagent = $_SERVER['HTTP_USER_AGENT'];
538 }
539 if (false !== strpos($uagent, 'Opera')
540 || false !== strpos($uagent, 'Mac')) {
541 // Looks good in Win XP/Mac/Opera 8/9, Mac/Firefox 2, Camino, Safari.
542 // Not broken in Mac/IE 5, Mac/Netscape 7 (?).
543 $this->rarrow = '&#x25B6;';
544 $this->larrow = '&#x25C0;';
545 }
f04721e2 546 elseif ((false !== strpos($uagent, 'Konqueror'))
0179d6be
JF
547 || (false !== strpos($uagent, 'Android'))) {
548 // The fonts on Android don't include the characters required for this to work as expected.
549 // So we use the same ones Konqueror uses.
78946b9b
PS
550 $this->rarrow = '&rarr;';
551 $this->larrow = '&larr;';
552 }
553 elseif (isset($_SERVER['HTTP_ACCEPT_CHARSET'])
554 && false === stripos($_SERVER['HTTP_ACCEPT_CHARSET'], 'utf-8')) {
555 // (Win/IE 5 doesn't set ACCEPT_CHARSET, but handles Unicode.)
556 // To be safe, non-Unicode browsers!
557 $this->rarrow = '&gt;';
558 $this->larrow = '&lt;';
559 }
560
f8129210 561 // RTL support - in RTL languages, swap r and l arrows
78946b9b
PS
562 if (right_to_left()) {
563 $t = $this->rarrow;
564 $this->rarrow = $this->larrow;
565 $this->larrow = $t;
ebebf55c 566 }
ebebf55c 567 }
78946b9b 568 }
ebebf55c 569
78946b9b
PS
570 /**
571 * Returns output renderer prefixes, these are used when looking
71c03ac1 572 * for the overridden renderers in themes.
473dd811 573 *
78946b9b
PS
574 * @return array
575 */
576 public function renderer_prefixes() {
577 global $CFG; // just in case the included files need it
578
596af93a 579 $prefixes = array('theme_'.$this->name);
78946b9b
PS
580
581 foreach ($this->parent_configs as $parent) {
582 $prefixes[] = 'theme_'.$parent->name;
583 }
584
585 return $prefixes;
34a2777c 586 }
587
571fa828 588 /**
78946b9b 589 * Returns the stylesheet URL of this editor content
473dd811 590 *
78946b9b 591 * @param bool $encoded false means use & and true use &amp; in URLs
1eb2b517 592 * @return moodle_url
571fa828 593 */
78946b9b
PS
594 public function editor_css_url($encoded=true) {
595 global $CFG;
78946b9b 596 $rev = theme_get_revision();
78946b9b 597 if ($rev > -1) {
1eb2b517 598 $url = new moodle_url("$CFG->httpswwwroot/theme/styles.php");
7070b7f2 599 if (!empty($CFG->slasharguments)) {
7070b7f2 600 $url->set_slashargument('/'.$this->name.'/'.$rev.'/editor', 'noparam', true);
7070b7f2 601 } else {
1eb2b517 602 $url->params(array('theme'=>$this->name,'rev'=>$rev, 'type'=>'editor'));
7070b7f2 603 }
78946b9b
PS
604 } else {
605 $params = array('theme'=>$this->name, 'type'=>'editor');
1eb2b517 606 $url = new moodle_url($CFG->httpswwwroot.'/theme/styles_debug.php', $params);
571fa828 607 }
1eb2b517 608 return $url;
571fa828 609 }
610
611 /**
78946b9b 612 * Returns the content of the CSS to be used in editor content
473dd811 613 *
78946b9b 614 * @return string
571fa828 615 */
71be124d 616 public function editor_css_files() {
78946b9b
PS
617 global $CFG;
618
71be124d 619 $files = array();
78946b9b
PS
620
621 // first editor plugins
bd3b3bba 622 $plugins = core_component::get_plugin_list('editor');
78946b9b
PS
623 foreach ($plugins as $plugin=>$fulldir) {
624 $sheetfile = "$fulldir/editor_styles.css";
625 if (is_readable($sheetfile)) {
71be124d 626 $files['plugin_'.$plugin] = $sheetfile;
78946b9b
PS
627 }
628 }
629 // then parent themes
630 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
631 if (empty($parent_config->editor_sheets)) {
632 continue;
633 }
634 foreach ($parent_config->editor_sheets as $sheet) {
ca194849 635 $sheetfile = "$parent_config->dir/style/$sheet.css";
78946b9b 636 if (is_readable($sheetfile)) {
71be124d 637 $files['parent_'.$parent_config->name.'_'.$sheet] = $sheetfile;
78946b9b
PS
638 }
639 }
640 }
641 // finally this theme
642 if (!empty($this->editor_sheets)) {
643 foreach ($this->editor_sheets as $sheet) {
71be124d 644 $sheetfile = "$this->dir/style/$sheet.css";
78946b9b 645 if (is_readable($sheetfile)) {
71be124d 646 $files['theme_'.$sheet] = $sheetfile;
78946b9b
PS
647 }
648 }
649 }
650
71be124d 651 return $files;
571fa828 652 }
653
654 /**
78946b9b 655 * Get the stylesheet URL of this theme
473dd811
SH
656 *
657 * @param moodle_page $page Not used... deprecated?
38aafea2 658 * @return array of moodle_url
571fa828 659 */
efaa4c08 660 public function css_urls(moodle_page $page) {
78946b9b
PS
661 global $CFG;
662
663 $rev = theme_get_revision();
664
efaa4c08 665 $urls = array();
efaa4c08 666
383b89a1
SH
667 $svg = $this->use_svg_icons();
668
78946b9b 669 if ($rev > -1) {
383b89a1 670 $url = new moodle_url("$CFG->httpswwwroot/theme/styles.php");
378b3eac 671 $separate = (core_useragent::is_ie() && !core_useragent::check_ie_version('10'));
1eb2b517
SH
672 if (!empty($CFG->slasharguments)) {
673 $slashargs = '';
674 if (!$svg) {
675 // We add a simple /_s to the start of the path.
676 // The underscore is used to ensure that it isn't a valid theme name.
677 $slashargs .= '/_s'.$slashargs;
678 }
679 $slashargs .= '/'.$this->name.'/'.$rev.'/all';
680 if ($separate) {
681 $slashargs .= '/chunk0';
682 }
683 $url->set_slashargument($slashargs, 'noparam', true);
537740cb 684 } else {
1eb2b517
SH
685 $params = array('theme' => $this->name,'rev' => $rev, 'type' => 'all');
686 if (!$svg) {
687 // We add an SVG param so that we know not to serve SVG images.
688 // We do this because all modern browsers support SVG and this param will one day be removed.
689 $params['svg'] = '0';
7070b7f2 690 }
1eb2b517
SH
691 if ($separate) {
692 $params['chunk'] = '0';
693 }
694 $url->params($params);
78946b9b 695 }
1eb2b517
SH
696 $urls[] = $url;
697
78946b9b 698 } else {
7829cf79
PS
699 // find out the current CSS and cache it now for 5 seconds
700 // the point is to construct the CSS only once and pass it through the
701 // dataroot to the script that actually serves the sheets
d6b64d15
PS
702 if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
703 define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php
704 }
bcf7f9f3 705 $candidatedir = "$CFG->cachedir/theme/$this->name";
383b89a1
SH
706 if ($svg) {
707 $candidatesheet = "$candidatedir/designer.ser";
708 } else {
709 $candidatesheet = "$candidatedir/designer_nosvg.ser";
710 }
bcf7f9f3
711 $rebuild = true;
712 if (file_exists($candidatesheet) and filemtime($candidatesheet) > time() - THEME_DESIGNER_CACHE_LIFETIME) {
7829cf79
PS
713 if ($css = file_get_contents($candidatesheet)) {
714 $css = unserialize($css);
bcf7f9f3
715 if (is_array($css)) {
716 $rebuild = false;
717 }
718 }
719 }
720 if ($rebuild) {
721 // Prepare the CSS optimiser if it is to be used,
722 // please note that it may be very slow and is therefore strongly discouraged in theme designer mode.
723 $optimiser = null;
724 if (!empty($CFG->enablecssoptimiser) && $this->supportscssoptimisation) {
725 require_once($CFG->dirroot.'/lib/csslib.php');
726 $optimiser = new css_optimiser;
7829cf79 727 }
b305d623 728 $css = $this->css_content($optimiser);
bcf7f9f3
729
730 // We do not want any errors here because this may fail easily because of the concurrent access.
731 $prevabort = ignore_user_abort(true);
732 check_dir_exists($candidatedir);
733 $tempfile = tempnam($candidatedir, 'tmpdesigner');
734 file_put_contents($tempfile, serialize($css));
735 $reporting = error_reporting(0);
736 chmod($tempfile, $CFG->filepermissions);
737 unlink($candidatesheet); // Do not rely on rename() deleting original, they may decide to change it at any time as usually.
738 rename($tempfile, $candidatesheet);
739 error_reporting($reporting);
740 ignore_user_abort($prevabort);
7829cf79
PS
741 }
742
383b89a1
SH
743 $baseurl = new moodle_url($CFG->httpswwwroot.'/theme/styles_debug.php');
744 if (!$svg) {
745 // We add an SVG param so that we know not to serve SVG images.
746 // We do this because all modern browsers support SVG and this param will one day be removed.
747 $baseurl->param('svg', '0');
748 }
378b3eac 749 if (core_useragent::is_ie()) {
c09e1a2c 750 // lalala, IE does not allow more than 31 linked CSS files from main document
71be124d 751 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name, 'type'=>'ie', 'subtype'=>'plugins'));
a6338a13
SH
752 foreach ($css['parents'] as $parent=>$sheets) {
753 // We need to serve parents individually otherwise we may easily exceed the style limit IE imposes (4096)
71be124d 754 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'ie', 'subtype'=>'parents', 'sheet'=>$parent));
a6338a13 755 }
71be124d 756 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name, 'type'=>'ie', 'subtype'=>'theme'));
c09e1a2c
PS
757
758 } else {
759 foreach ($css['plugins'] as $plugin=>$unused) {
760 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'plugin', 'subtype'=>$plugin));
761 }
762 foreach ($css['parents'] as $parent=>$sheets) {
763 foreach ($sheets as $sheet=>$unused2) {
764 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'parent', 'subtype'=>$parent, 'sheet'=>$sheet));
765 }
766 }
767 foreach ($css['theme'] as $sheet=>$unused) {
768 $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 769 }
78946b9b 770 }
78946b9b 771 }
efaa4c08
PS
772
773 return $urls;
571fa828 774 }
34a2777c 775
ebebf55c 776 /**
045f492c 777 * Returns an array of organised CSS files required for this output
473dd811 778 *
045f492c 779 * @return array
ebebf55c 780 */
045f492c
SH
781 public function css_files() {
782 $cssfiles = array('plugins'=>array(), 'parents'=>array(), 'theme'=>array());
78946b9b 783
8c96c3cb 784 // get all plugin sheets
045f492c 785 $excludes = $this->resolve_excludes('plugins_exclude_sheets');
78946b9b 786 if ($excludes !== true) {
46f6f7f2 787 foreach (core_component::get_plugin_types() as $type=>$unused) {
045f492c 788 if ($type === 'theme' || (!empty($excludes[$type]) and $excludes[$type] === true)) {
78946b9b
PS
789 continue;
790 }
bd3b3bba 791 $plugins = core_component::get_plugin_list($type);
78946b9b
PS
792 foreach ($plugins as $plugin=>$fulldir) {
793 if (!empty($excludes[$type]) and is_array($excludes[$type])
794 and in_array($plugin, $excludes[$type])) {
795 continue;
796 }
f8bb9666
SH
797
798 $plugincontent = '';
78946b9b
PS
799 $sheetfile = "$fulldir/styles.css";
800 if (is_readable($sheetfile)) {
045f492c 801 $cssfiles['plugins'][$type.'_'.$plugin] = $sheetfile;
f8bb9666
SH
802 }
803 $sheetthemefile = "$fulldir/styles_{$this->name}.css";
804 if (is_readable($sheetthemefile)) {
045f492c 805 $cssfiles['plugins'][$type.'_'.$plugin.'_'.$this->name] = $sheetthemefile;
f8bb9666 806 }
78946b9b
PS
807 }
808 }
809 }
34a2777c 810
78946b9b 811 // find out wanted parent sheets
045f492c 812 $excludes = $this->resolve_excludes('parents_exclude_sheets');
78946b9b
PS
813 if ($excludes !== true) {
814 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
815 $parent = $parent_config->name;
045f492c 816 if (empty($parent_config->sheets) || (!empty($excludes[$parent]) and $excludes[$parent] === true)) {
78946b9b
PS
817 continue;
818 }
819 foreach ($parent_config->sheets as $sheet) {
820 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
821 and in_array($sheet, $excludes[$parent])) {
822 continue;
823 }
824 $sheetfile = "$parent_config->dir/style/$sheet.css";
825 if (is_readable($sheetfile)) {
045f492c 826 $cssfiles['parents'][$parent][$sheet] = $sheetfile;
78946b9b
PS
827 }
828 }
829 }
830 }
34a2777c 831
78946b9b
PS
832 // current theme sheets
833 if (is_array($this->sheets)) {
834 foreach ($this->sheets as $sheet) {
835 $sheetfile = "$this->dir/style/$sheet.css";
836 if (is_readable($sheetfile)) {
045f492c 837 $cssfiles['theme'][$sheet] = $sheetfile;
78946b9b 838 }
ebebf55c 839 }
78946b9b
PS
840 }
841
045f492c
SH
842 return $cssfiles;
843 }
844
845 /**
846 * Returns the content of the one huge CSS merged from all style sheets.
473dd811 847 *
b305d623 848 * @param css_optimiser|null $optimiser A CSS optimiser to use during on the content. Null = don't optimise
045f492c
SH
849 * @return string
850 */
b305d623 851 public function css_content(css_optimiser $optimiser = null) {
71be124d 852 $files = array_merge($this->css_files(), array('editor'=>$this->editor_css_files()));
b305d623 853 $css = $this->css_files_get_contents($files, array(), $optimiser);
78946b9b
PS
854 return $css;
855 }
34a2777c 856
045f492c
SH
857 /**
858 * Given an array of file paths or a single file path loads the contents of
859 * the CSS file, processes it then returns it in the same structure it was given.
860 *
f8129210 861 * Can be used recursively on the results of {@link css_files}
045f492c
SH
862 *
863 * @param array|string $file An array of file paths or a single file path
71c03ac1 864 * @param array $keys An array of previous array keys [recursive addition]
b305d623 865 * @param css_optimiser|null $optimiser A CSS optimiser to use during on the content. Null = don't optimise
045f492c
SH
866 * @return The converted array or the contents of the single file ($file type)
867 */
b305d623
SH
868 protected function css_files_get_contents($file, array $keys, css_optimiser $optimiser = null) {
869 global $CFG;
045f492c 870 if (is_array($file)) {
04cfa387
SH
871 // We use a separate array to keep everything in the exact same order.
872 $return = array();
045f492c 873 foreach ($file as $key=>$f) {
04cfa387 874 $return[clean_param($key, PARAM_SAFEDIR)] = $this->css_files_get_contents($f, array_merge($keys, array($key)), $optimiser);
045f492c 875 }
04cfa387 876 return $return;
045f492c 877 } else {
b305d623
SH
878 $contents = file_get_contents($file);
879 $contents = $this->post_process($contents);
045f492c 880 $comment = '/** Path: '.implode(' ', $keys).' **/'."\n";
b305d623
SH
881 $stats = '';
882 if (!is_null($optimiser)) {
883 $contents = $optimiser->process($contents);
884 if (!empty($CFG->cssoptimiserstats)) {
885 $stats = $optimiser->output_stats_css();
886 }
887 }
888 return $comment.$stats.$contents;
045f492c
SH
889 }
890 }
891
e68c5f36
PS
892
893 /**
473dd811
SH
894 * Generate a URL to the file that serves theme JavaScript files.
895 *
d7656956
ARN
896 * If we determine that the theme has no relevant files, then we return
897 * early with a null value.
898 *
71c03ac1 899 * @param bool $inhead true means head url, false means footer
d7656956 900 * @return moodle_url|null
e68c5f36 901 */
baeb20bb 902 public function javascript_url($inhead) {
e68c5f36
PS
903 global $CFG;
904
905 $rev = theme_get_revision();
e68c5f36 906 $params = array('theme'=>$this->name,'rev'=>$rev);
baeb20bb
PS
907 $params['type'] = $inhead ? 'head' : 'footer';
908
d7656956
ARN
909 // Return early if there are no files to serve
910 if (count($this->javascript_files($params['type'])) === 0) {
911 return null;
912 }
913
ecbad2ad
PS
914 if (!empty($CFG->slasharguments) and $rev > 0) {
915 $url = new moodle_url("$CFG->httpswwwroot/theme/javascript.php");
916 $url->set_slashargument('/'.$this->name.'/'.$rev.'/'.$params['type'], 'noparam', true);
917 return $url;
918 } else {
919 return new moodle_url($CFG->httpswwwroot.'/theme/javascript.php', $params);
920 }
e68c5f36
PS
921 }
922
473dd811
SH
923 /**
924 * Get the URL's for the JavaScript files used by this theme.
925 * They won't be served directly, instead they'll be mediated through
926 * theme/javascript.php.
927 *
928 * @param string $type Either javascripts_footer, or javascripts
929 * @return array
930 */
045f492c
SH
931 public function javascript_files($type) {
932 if ($type === 'footer') {
933 $type = 'javascripts_footer';
934 } else {
f856106b 935 $type = 'javascripts';
045f492c 936 }
04c01408 937
358c13cb
PS
938 $js = array();
939 // find out wanted parent javascripts
045f492c 940 $excludes = $this->resolve_excludes('parents_exclude_javascripts');
358c13cb
PS
941 if ($excludes !== true) {
942 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
943 $parent = $parent_config->name;
04c01408 944 if (empty($parent_config->$type)) {
358c13cb
PS
945 continue;
946 }
947 if (!empty($excludes[$parent]) and $excludes[$parent] === true) {
948 continue;
949 }
04c01408 950 foreach ($parent_config->$type as $javascript) {
358c13cb
PS
951 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
952 and in_array($javascript, $excludes[$parent])) {
953 continue;
954 }
955 $javascriptfile = "$parent_config->dir/javascript/$javascript.js";
956 if (is_readable($javascriptfile)) {
045f492c 957 $js[] = $javascriptfile;
358c13cb
PS
958 }
959 }
960 }
961 }
962
963 // current theme javascripts
04c01408
PS
964 if (is_array($this->$type)) {
965 foreach ($this->$type as $javascript) {
358c13cb
PS
966 $javascriptfile = "$this->dir/javascript/$javascript.js";
967 if (is_readable($javascriptfile)) {
045f492c
SH
968 $js[] = $javascriptfile;
969 }
970 }
971 }
045f492c
SH
972 return $js;
973 }
974
975 /**
3d3fae72 976 * Resolves an exclude setting to the themes setting is applicable or the
045f492c
SH
977 * setting of its closest parent.
978 *
979 * @param string $variable The name of the setting the exclude setting to resolve
f8129210 980 * @param string $default
045f492c
SH
981 * @return mixed
982 */
f8129210 983 protected function resolve_excludes($variable, $default = null) {
045f492c
SH
984 $setting = $default;
985 if (is_array($this->{$variable}) or $this->{$variable} === true) {
986 $setting = $this->{$variable};
987 } else {
988 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
989 if (!isset($parent_config->{$variable})) {
990 continue;
991 }
992 if (is_array($parent_config->{$variable}) or $parent_config->{$variable} === true) {
993 $setting = $parent_config->{$variable};
994 break;
358c13cb
PS
995 }
996 }
997 }
045f492c
SH
998 return $setting;
999 }
358c13cb 1000
045f492c
SH
1001 /**
1002 * Returns the content of the one huge javascript file merged from all theme javascript files.
473dd811 1003 *
f8129210 1004 * @param bool $type
045f492c
SH
1005 * @return string
1006 */
1007 public function javascript_content($type) {
1008 $jsfiles = $this->javascript_files($type);
1009 $js = '';
1010 foreach ($jsfiles as $jsfile) {
1011 $js .= file_get_contents($jsfile)."\n";
1012 }
1013 return $js;
e68c5f36
PS
1014 }
1015
473dd811
SH
1016 /**
1017 * Post processes CSS.
1018 *
1019 * This method post processes all of the CSS before it is served for this theme.
1020 * This is done so that things such as image URL's can be swapped in and to
1021 * run any specific CSS post process method the theme has requested.
3d3fae72 1022 * This allows themes to use CSS settings.
473dd811
SH
1023 *
1024 * @param string $css The CSS to process.
1025 * @return string The processed CSS.
1026 */
045f492c 1027 public function post_process($css) {
78946b9b 1028 // now resolve all image locations
37824e73 1029 if (preg_match_all('/\[\[pix:([a-z0-9_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) {
78946b9b
PS
1030 $replaced = array();
1031 foreach ($matches as $match) {
1032 if (isset($replaced[$match[0]])) {
1033 continue;
1034 }
1035 $replaced[$match[0]] = true;
1036 $imagename = $match[2];
1037 $component = rtrim($match[1], '|');
b9bc2019 1038 $imageurl = $this->pix_url($imagename, $component)->out(false);
df06c1c4 1039 // we do not need full url because the image.php is always in the same dir
9d473266 1040 $imageurl = preg_replace('|^http.?://[^/]+|', '', $imageurl);
df06c1c4 1041 $css = str_replace($match[0], $imageurl, $css);
ebebf55c 1042 }
34a2777c 1043 }
17a6649b 1044
9ba6076c 1045 // Now resolve all font locations.
37824e73 1046 if (preg_match_all('/\[\[font:([a-z0-9_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) {
9ba6076c
PS
1047 $replaced = array();
1048 foreach ($matches as $match) {
1049 if (isset($replaced[$match[0]])) {
1050 continue;
1051 }
1052 $replaced[$match[0]] = true;
1053 $fontname = $match[2];
1054 $component = rtrim($match[1], '|');
1055 $fonturl = $this->font_url($fontname, $component)->out(false);
1056 // We do not need full url because the font.php is always in the same dir.
1057 $fonturl = preg_replace('|^http.?://[^/]+|', '', $fonturl);
1058 $css = str_replace($match[0], $fonturl, $css);
1059 }
1060 }
1061
78946b9b
PS
1062 // now resolve all theme settings or do any other postprocessing
1063 $csspostprocess = $this->csspostprocess;
1064 if (function_exists($csspostprocess)) {
1065 $css = $csspostprocess($css, $this);
fdeb7fa1 1066 }
1067
78946b9b 1068 return $css;
ebebf55c 1069 }
17a6649b 1070
ebebf55c 1071 /**
78946b9b
PS
1072 * Return the URL for an image
1073 *
1074 * @param string $imagename the name of the icon.
f8129210 1075 * @param string $component specification of one plugin like in get_string()
78946b9b 1076 * @return moodle_url
fdeb7fa1 1077 */
c39e5ba2 1078 public function pix_url($imagename, $component) {
fdeb7fa1 1079 global $CFG;
78946b9b 1080
9d473266 1081 $params = array('theme'=>$this->name);
436dbeec 1082 $svg = $this->use_svg_icons();
9d473266
PS
1083
1084 if (empty($component) or $component === 'moodle' or $component === 'core') {
1085 $params['component'] = 'core';
1086 } else {
1087 $params['component'] = $component;
1088 }
78946b9b
PS
1089
1090 $rev = theme_get_revision();
1091 if ($rev != -1) {
1092 $params['rev'] = $rev;
1093 }
9d473266
PS
1094
1095 $params['image'] = $imagename;
1096
436dbeec 1097 $url = new moodle_url("$CFG->httpswwwroot/theme/image.php");
9d473266 1098 if (!empty($CFG->slasharguments) and $rev > 0) {
436dbeec
SH
1099 $path = '/'.$params['theme'].'/'.$params['component'].'/'.$params['rev'].'/'.$params['image'];
1100 if (!$svg) {
1101 // We add a simple /_s to the start of the path.
1102 // The underscore is used to ensure that it isn't a valid theme name.
1103 $path = '/_s'.$path;
1104 }
1105 $url->set_slashargument($path, 'noparam', true);
9d473266 1106 } else {
436dbeec
SH
1107 if (!$svg) {
1108 // We add an SVG param so that we know not to serve SVG images.
1109 // We do this because all modern browsers support SVG and this param will one day be removed.
1110 $params['svg'] = '0';
1111 }
1112 $url->params($params);
fdeb7fa1 1113 }
78946b9b 1114
9d473266 1115 return $url;
fdeb7fa1 1116 }
1117
9ba6076c
PS
1118 /**
1119 * Return the URL for a font
1120 *
1121 * @param string $font the name of the font (including extension).
1122 * @param string $component specification of one plugin like in get_string()
1123 * @return moodle_url
1124 */
1125 public function font_url($font, $component) {
1126 global $CFG;
1127
1128 $params = array('theme'=>$this->name);
1129
1130 if (empty($component) or $component === 'moodle' or $component === 'core') {
1131 $params['component'] = 'core';
1132 } else {
1133 $params['component'] = $component;
1134 }
1135
1136 $rev = theme_get_revision();
1137 if ($rev != -1) {
1138 $params['rev'] = $rev;
1139 }
1140
1141 $params['font'] = $font;
1142
1143 $url = new moodle_url("$CFG->httpswwwroot/theme/font.php");
1144 if (!empty($CFG->slasharguments) and $rev > 0) {
1145 $path = '/'.$params['theme'].'/'.$params['component'].'/'.$params['rev'].'/'.$params['font'];
1146 $url->set_slashargument($path, 'noparam', true);
1147 } else {
1148 $url->params($params);
1149 }
1150
1151 return $url;
1152 }
1153
fe7b75f8
PS
1154 /**
1155 * Returns URL to the stored file via pluginfile.php.
1156 *
1157 * Note the theme must also implement pluginfile.php handler,
1158 * theme revision is used instead of the itemid.
1159 *
1160 * @param string $setting
1161 * @param string $filearea
1162 * @return string protocol relative URL or null if not present
1163 */
1164 public function setting_file_url($setting, $filearea) {
1165 global $CFG;
1166
1167 if (empty($this->settings->$setting)) {
1168 return null;
1169 }
1170
1171 $component = 'theme_'.$this->name;
1172 $itemid = theme_get_revision();
1173 $filepath = $this->settings->$setting;
1174 $syscontext = context_system::instance();
1175
1176 $url = moodle_url::make_file_url("$CFG->wwwroot/pluginfile.php", "/$syscontext->id/$component/$filearea/$itemid".$filepath);
1177
1178 // Now this is tricky because the we can not hardcode http or https here, lets use the relative link.
1179 // Note: unfortunately moodle_url does not support //urls yet.
1180
1181 $url = preg_replace('|^https?://|i', '//', $url->out(false));
1182
1183 return $url;
1184 }
1185
1186 /**
1187 * Serve the theme setting file.
1188 *
1189 * @param string $filearea
1190 * @param array $args
1191 * @param bool $forcedownload
1192 * @param array $options
1193 * @return bool may terminate if file not found or donotdie not specified
1194 */
1195 public function setting_file_serve($filearea, $args, $forcedownload, $options) {
1196 global $CFG;
1197 require_once("$CFG->libdir/filelib.php");
1198
1199 $syscontext = context_system::instance();
1200 $component = 'theme_'.$this->name;
1201
1202 $revision = array_shift($args);
1203 if ($revision < 0) {
1204 $lifetime = 0;
1205 } else {
1206 $lifetime = 60*60*24*60;
1207 }
1208
1209 $fs = get_file_storage();
1210 $relativepath = implode('/', $args);
1211
1212 $fullpath = "/{$syscontext->id}/{$component}/{$filearea}/0/{$relativepath}";
1213 $fullpath = rtrim($fullpath, '/');
1214 if ($file = $fs->get_file_by_hash(sha1($fullpath))) {
1215 send_stored_file($file, $lifetime, 0, $forcedownload, $options);
1216 return true;
1217 } else {
1218 send_file_not_found();
1219 }
1220 }
1221
fdeb7fa1 1222 /**
78946b9b 1223 * Resolves the real image location.
436dbeec
SH
1224 *
1225 * $svg was introduced as an arg in 2.4. It is important because not all supported browsers support the use of SVG
1226 * and we need a way in which to turn it off.
1227 * By default SVG won't be used unless asked for. This is done for two reasons:
1228 * 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
1229 * browser supports SVG.
1230 * 2. We only serve SVG images from locations we trust. This must NOT include any areas where the image may have been uploaded
1231 * by the user due to security concerns.
1232 *
78946b9b
PS
1233 * @param string $image name of image, may contain relative path
1234 * @param string $component
436dbeec 1235 * @param bool $svg If set to true SVG images will also be looked for.
78946b9b 1236 * @return string full file path
fdeb7fa1 1237 */
436dbeec 1238 public function resolve_image_location($image, $component, $svg = false) {
78946b9b
PS
1239 global $CFG;
1240
436dbeec
SH
1241 if (!is_bool($svg)) {
1242 // If $svg isn't a bool then we need to decide for ourselves.
1243 $svg = $this->use_svg_icons();
1244 }
1245
78946b9b 1246 if ($component === 'moodle' or $component === 'core' or empty($component)) {
436dbeec 1247 if ($imagefile = $this->image_exists("$this->dir/pix_core/$image", $svg)) {
78946b9b
PS
1248 return $imagefile;
1249 }
1250 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
436dbeec 1251 if ($imagefile = $this->image_exists("$parent_config->dir/pix_core/$image", $svg)) {
78946b9b
PS
1252 return $imagefile;
1253 }
1254 }
436dbeec 1255 if ($imagefile = $this->image_exists("$CFG->dataroot/pix/$image", $svg)) {
f930cf6b
EM
1256 return $imagefile;
1257 }
436dbeec 1258 if ($imagefile = $this->image_exists("$CFG->dirroot/pix/$image", $svg)) {
78946b9b
PS
1259 return $imagefile;
1260 }
1261 return null;
1262
1263 } else if ($component === 'theme') { //exception
1264 if ($image === 'favicon') {
1265 return "$this->dir/pix/favicon.ico";
1266 }
436dbeec 1267 if ($imagefile = $this->image_exists("$this->dir/pix/$image", $svg)) {
78946b9b
PS
1268 return $imagefile;
1269 }
1270 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
436dbeec 1271 if ($imagefile = $this->image_exists("$parent_config->dir/pix/$image", $svg)) {
78946b9b
PS
1272 return $imagefile;
1273 }
1274 }
1275 return null;
1276
78946b9b
PS
1277 } else {
1278 if (strpos($component, '_') === false) {
1279 $component = 'mod_'.$component;
1280 }
1281 list($type, $plugin) = explode('_', $component, 2);
1282
436dbeec 1283 if ($imagefile = $this->image_exists("$this->dir/pix_plugins/$type/$plugin/$image", $svg)) {
78946b9b
PS
1284 return $imagefile;
1285 }
1286 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
436dbeec 1287 if ($imagefile = $this->image_exists("$parent_config->dir/pix_plugins/$type/$plugin/$image", $svg)) {
78946b9b
PS
1288 return $imagefile;
1289 }
1290 }
436dbeec 1291 if ($imagefile = $this->image_exists("$CFG->dataroot/pix_plugins/$type/$plugin/$image", $svg)) {
f930cf6b
EM
1292 return $imagefile;
1293 }
1c74b260 1294 $dir = core_component::get_plugin_directory($type, $plugin);
436dbeec 1295 if ($imagefile = $this->image_exists("$dir/pix/$image", $svg)) {
78946b9b
PS
1296 return $imagefile;
1297 }
1298 return null;
fdeb7fa1 1299 }
fdeb7fa1 1300 }
1301
9ba6076c
PS
1302 /**
1303 * Resolves the real font location.
1304 *
1305 * @param string $font name of font file
1306 * @param string $component
1307 * @return string full file path
1308 */
1309 public function resolve_font_location($font, $component) {
1310 global $CFG;
1311
1312 if ($component === 'moodle' or $component === 'core' or empty($component)) {
1313 if (file_exists("$this->dir/fonts_core/$font")) {
1314 return "$this->dir/fonts_core/$font";
1315 }
1316 foreach (array_reverse($this->parent_configs) as $parent_config) { // Base first, the immediate parent last.
1317 if (file_exists("$parent_config->dir/fonts_core/$font")) {
1318 return "$parent_config->dir/fonts_core/$font";
1319 }
1320 }
1321 if (file_exists("$CFG->dataroot/fonts/$font")) {
1322 return "$CFG->dataroot/fonts/$font";
1323 }
1324 if (file_exists("$CFG->dirroot/lib/fonts/$font")) {
1325 return "$CFG->dirroot/lib/fonts/$font";
1326 }
1327 return null;
1328
1329 } else if ($component === 'theme') { // Exception.
1330 if (file_exists("$this->dir/fonts/$font")) {
1331 return "$this->dir/fonts/$font";
1332 }
1333 foreach (array_reverse($this->parent_configs) as $parent_config) { // Base first, the immediate parent last.
1334 if (file_exists("$parent_config->dir/fonts/$font")) {
1335 return "$parent_config->dir/fonts/$font";
1336 }
1337 }
1338 return null;
1339
1340 } else {
1341 if (strpos($component, '_') === false) {
1342 $component = 'mod_'.$component;
1343 }
1344 list($type, $plugin) = explode('_', $component, 2);
1345
1346 if (file_exists("$this->dir/fonts_plugins/$type/$plugin/$font")) {
1347 return "$this->dir/fonts_plugins/$type/$plugin/$font";
1348 }
1349 foreach (array_reverse($this->parent_configs) as $parent_config) { // Base first, the immediate parent last.
1350 if (file_exists("$parent_config->dir/fonts_plugins/$type/$plugin/$font")) {
1351 return "$parent_config->dir/fonts_plugins/$type/$plugin/$font";
1352 }
1353 }
1354 if (file_exists("$CFG->dataroot/fonts_plugins/$type/$plugin/$font")) {
1355 return "$CFG->dataroot/fonts_plugins/$type/$plugin/$font";
1356 }
1357 $dir = core_component::get_plugin_directory($type, $plugin);
1358 if (file_exists("$dir/fonts/$font")) {
1359 return "$dir/fonts/$font";
1360 }
1361 return null;
1362 }
1363 }
1364
436dbeec
SH
1365 /**
1366 * Return true if we should look for SVG images as well.
1367 *
1368 * @staticvar bool $svg
1369 * @return bool
1370 */
1371 public function use_svg_icons() {
1372 global $CFG;
1373 if ($this->usesvg === null) {
c3d2fbf9 1374
436dbeec 1375 if (!isset($CFG->svgicons) || !is_bool($CFG->svgicons)) {
c3d2fbf9 1376 $this->usesvg = core_useragent::supports_svg();
436dbeec
SH
1377 } else {
1378 // Force them on/off depending upon the setting.
1379 $this->usesvg = $CFG->svgicons;
1380 }
1381 }
1382 return $this->usesvg;
1383 }
1384
f7d6a556
SH
1385 /**
1386 * Forces the usesvg setting to either true or false, avoiding any decision making.
1387 *
1388 * This function should only ever be used when absolutely required, and before any generation of image URL's has occurred.
383b89a1 1389 * DO NOT ABUSE THIS FUNCTION... not that you'd want to right ;)
f7d6a556
SH
1390 *
1391 * @param bool $setting True to force the use of svg when available, null otherwise.
1392 */
383b89a1 1393 public function force_svg_use($setting) {
f7d6a556
SH
1394 $this->usesvg = (bool)$setting;
1395 }
1396
d4a03c00 1397 /**
78946b9b 1398 * Checks if file with any image extension exists.
473dd811 1399 *
436dbeec
SH
1400 * The order to these images was adjusted prior to the release of 2.4
1401 * At that point the were the following image counts in Moodle core:
1402 *
1403 * - png = 667 in pix dirs (1499 total)
1404 * - gif = 385 in pix dirs (606 total)
1405 * - jpg = 62 in pix dirs (74 total)
1406 * - jpeg = 0 in pix dirs (1 total)
1407 *
1408 * There is work in progress to move towards SVG presently hence that has been prioritiesed.
1409 *
78946b9b 1410 * @param string $filepath
436dbeec 1411 * @param bool $svg If set to true SVG images will also be looked for.
78946b9b 1412 * @return string image name with extension
d4a03c00 1413 */
436dbeec
SH
1414 private static function image_exists($filepath, $svg = false) {
1415 if ($svg && file_exists("$filepath.svg")) {
1416 return "$filepath.svg";
78946b9b
PS
1417 } else if (file_exists("$filepath.png")) {
1418 return "$filepath.png";
436dbeec
SH
1419 } else if (file_exists("$filepath.gif")) {
1420 return "$filepath.gif";
78946b9b
PS
1421 } else if (file_exists("$filepath.jpg")) {
1422 return "$filepath.jpg";
1423 } else if (file_exists("$filepath.jpeg")) {
1424 return "$filepath.jpeg";
d4a03c00 1425 } else {
78946b9b 1426 return false;
d4a03c00 1427 }
1428 }
1429
fdeb7fa1 1430 /**
78946b9b 1431 * Loads the theme config from config.php file.
473dd811 1432 *
78946b9b 1433 * @param string $themename
473dd811
SH
1434 * @param stdClass $settings from config_plugins table
1435 * @return stdClass The theme configuration
fdeb7fa1 1436 */
78946b9b
PS
1437 private static function find_theme_config($themename, $settings) {
1438 // We have to use the variable name $THEME (upper case) because that
1439 // is what is used in theme config.php files.
fdeb7fa1 1440
78946b9b
PS
1441 if (!$dir = theme_config::find_theme_location($themename)) {
1442 return null;
fdeb7fa1 1443 }
2f67a9b3 1444
365a5941 1445 $THEME = new stdClass();
78946b9b
PS
1446 $THEME->name = $themename;
1447 $THEME->dir = $dir;
1448 $THEME->settings = $settings;
1449
1450 global $CFG; // just in case somebody tries to use $CFG in theme config
1451 include("$THEME->dir/config.php");
1452
1453 // verify the theme configuration is OK
1454 if (!is_array($THEME->parents)) {
1455 // parents option is mandatory now
1456 return null;
fdeb7fa1 1457 }
1458
78946b9b 1459 return $THEME;
fdeb7fa1 1460 }
1461
d4a03c00 1462 /**
78946b9b
PS
1463 * Finds the theme location and verifies the theme has all needed files
1464 * and is not obsoleted.
473dd811 1465 *
78946b9b
PS
1466 * @param string $themename
1467 * @return string full dir path or null if not found
d4a03c00 1468 */
78946b9b
PS
1469 private static function find_theme_location($themename) {
1470 global $CFG;
1471
1472 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
1473 $dir = "$CFG->dirroot/theme/$themename";
d4a03c00 1474
3dd1d7e7
DM
1475 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
1476 $dir = "$CFG->themedir/$themename";
1477
7d875874 1478 } else {
78946b9b 1479 return null;
d4a03c00 1480 }
78946b9b
PS
1481
1482 if (file_exists("$dir/styles.php")) {
1483 //legacy theme - needs to be upgraded - upgrade info is displayed on the admin settings page
1484 return null;
1485 }
2f67a9b3 1486
78946b9b 1487 return $dir;
d4a03c00 1488 }
1489
1d13c75c 1490 /**
78946b9b 1491 * Get the renderer for a part of Moodle for this theme.
473dd811 1492 *
78946b9b 1493 * @param moodle_page $page the page we are rendering
f8129210 1494 * @param string $component the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
78946b9b 1495 * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
c927e35c 1496 * @param string $target one of rendering target constants
78946b9b 1497 * @return renderer_base the requested renderer.
1d13c75c 1498 */
c927e35c 1499 public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
78946b9b 1500 if (is_null($this->rf)) {
c927e35c 1501 $classname = $this->rendererfactory;
78946b9b 1502 $this->rf = new $classname($this);
1d13c75c 1503 }
78946b9b 1504
c927e35c 1505 return $this->rf->get_renderer($page, $component, $subtype, $target);
1d13c75c 1506 }
1507
fdeb7fa1 1508 /**
78946b9b 1509 * Get the information from {@link $layouts} for this type of page.
473dd811 1510 *
78946b9b
PS
1511 * @param string $pagelayout the the page layout name.
1512 * @return array the appropriate part of {@link $layouts}.
fdeb7fa1 1513 */
78946b9b
PS
1514 protected function layout_info_for_page($pagelayout) {
1515 if (array_key_exists($pagelayout, $this->layouts)) {
1516 return $this->layouts[$pagelayout];
1517 } else {
191b267b
PS
1518 debugging('Invalid page layout specified: ' . $pagelayout);
1519 return $this->layouts['standard'];
fdeb7fa1 1520 }
1521 }
1522
1523 /**
78946b9b
PS
1524 * Given the settings of this theme, and the page pagelayout, return the
1525 * full path of the page layout file to use.
1526 *
1527 * Used by {@link core_renderer::header()}.
1528 *
1529 * @param string $pagelayout the the page layout name.
1530 * @return string Full path to the lyout file to use
ebebf55c 1531 */
78946b9b 1532 public function layout_file($pagelayout) {
ebebf55c 1533 global $CFG;
fdeb7fa1 1534
78946b9b
PS
1535 $layoutinfo = $this->layout_info_for_page($pagelayout);
1536 $layoutfile = $layoutinfo['file'];
fdeb7fa1 1537
84db3ea2
SH
1538 if (array_key_exists('theme', $layoutinfo)) {
1539 $themes = array($layoutinfo['theme']);
1540 } else {
1541 $themes = array_merge(array($this->name),$this->parents);
1542 }
4399b9d5 1543
84db3ea2
SH
1544 foreach ($themes as $theme) {
1545 if ($dir = $this->find_theme_location($theme)) {
1546 $path = "$dir/layout/$layoutfile";
1547
1548 // Check the template exists, return general base theme template if not.
1549 if (is_readable($path)) {
1550 return $path;
1551 }
78946b9b 1552 }
34a2777c 1553 }
1554
191b267b 1555 debugging('Can not find layout file for: ' . $pagelayout);
78946b9b
PS
1556 // fallback to standard normal layout
1557 return "$CFG->dirroot/theme/base/layout/general.php";
1558 }
ebebf55c 1559
78946b9b
PS
1560 /**
1561 * Returns auxiliary page layout options specified in layout configuration array.
473dd811 1562 *
78946b9b
PS
1563 * @param string $pagelayout
1564 * @return array
1565 */
1566 public function pagelayout_options($pagelayout) {
1567 $info = $this->layout_info_for_page($pagelayout);
1568 if (!empty($info['options'])) {
1569 return $info['options'];
34a2777c 1570 }
78946b9b
PS
1571 return array();
1572 }
34a2777c 1573
78946b9b
PS
1574 /**
1575 * Inform a block_manager about the block regions this theme wants on this
1576 * page layout.
473dd811 1577 *
78946b9b
PS
1578 * @param string $pagelayout the general type of the page.
1579 * @param block_manager $blockmanager the block_manger to set up.
78946b9b
PS
1580 */
1581 public function setup_blocks($pagelayout, $blockmanager) {
1582 $layoutinfo = $this->layout_info_for_page($pagelayout);
1583 if (!empty($layoutinfo['regions'])) {
1584 $blockmanager->add_regions($layoutinfo['regions']);
1585 $blockmanager->set_default_region($layoutinfo['defaultregion']);
ebebf55c 1586 }
34a2777c 1587 }
1588
473dd811
SH
1589 /**
1590 * Gets the visible name for the requested block region.
1591 *
1592 * @param string $region The region name to get
1593 * @param string $theme The theme the region belongs to (may come from the parent theme)
1594 * @return string
1595 */
cadc0d28
TH
1596 protected function get_region_name($region, $theme) {
1597 $regionstring = get_string('region-' . $region, 'theme_' . $theme);
1598 // A name exists in this theme, so use it
1599 if (substr($regionstring, 0, 1) != '[') {
1600 return $regionstring;
1601 }
1602
1603 // Otherwise, try to find one elsewhere
1604 // Check parents, if any
1605 foreach ($this->parents as $parentthemename) {
1606 $regionstring = get_string('region-' . $region, 'theme_' . $parentthemename);
1607 if (substr($regionstring, 0, 1) != '[') {
1608 return $regionstring;
1609 }
1610 }
1611
1612 // Last resort, try the base theme for names
1613 return get_string('region-' . $region, 'theme_base');
1614 }
1615
ebebf55c 1616 /**
78946b9b 1617 * Get the list of all block regions known to this theme in all templates.
473dd811 1618 *
78946b9b 1619 * @return array internal region name => human readable name.
ebebf55c 1620 */
78946b9b
PS
1621 public function get_all_block_regions() {
1622 $regions = array();
78946b9b
PS
1623 foreach ($this->layouts as $layoutinfo) {
1624 foreach ($layoutinfo['regions'] as $region) {
a9535f79 1625 $regions[$region] = $this->get_region_name($region, $this->name);
ebebf55c 1626 }
34a2777c 1627 }
78946b9b 1628 return $regions;
34a2777c 1629 }
d609d962
SH
1630
1631 /**
1632 * Returns the human readable name of the theme
1633 *
1634 * @return string
1635 */
1636 public function get_theme_name() {
1637 return get_string('pluginname', 'theme_'.$this->name);
1638 }
ebebf55c 1639}
34a2777c 1640
ebebf55c 1641/**
d9c8f425 1642 * This class keeps track of which HTML tags are currently open.
1643 *
1644 * This makes it much easier to always generate well formed XHTML output, even
1645 * if execution terminates abruptly. Any time you output some opening HTML
1646 * without the matching closing HTML, you should push the necessary close tags
1647 * onto the stack.
ebebf55c 1648 *
1649 * @copyright 2009 Tim Hunt
473dd811
SH
1650 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1651 * @since Moodle 2.0
f8129210 1652 * @package core
76be40cc 1653 * @category output
ebebf55c 1654 */
d9c8f425 1655class xhtml_container_stack {
473dd811
SH
1656
1657 /**
76be40cc 1658 * @var array Stores the list of open containers.
473dd811 1659 */
d9c8f425 1660 protected $opencontainers = array();
473dd811 1661
fa1afe32 1662 /**
76be40cc 1663 * @var array In developer debug mode, stores a stack trace of all opens and
d9c8f425 1664 * closes, so we can output helpful error messages when there is a mismatch.
fa1afe32 1665 */
d9c8f425 1666 protected $log = array();
473dd811 1667
fa1afe32 1668 /**
76be40cc
SH
1669 * @var boolean Store whether we are developer debug mode. We need this in
1670 * several places including in the destructor where we may not have access to $CFG.
fa1afe32 1671 */
d9c8f425 1672 protected $isdebugging;
34a2777c 1673
473dd811
SH
1674 /**
1675 * Constructor
1676 */
d9c8f425 1677 public function __construct() {
96f81ea3
PS
1678 global $CFG;
1679 $this->isdebugging = $CFG->debugdeveloper;
ebebf55c 1680 }
34a2777c 1681
fa1afe32 1682 /**
d9c8f425 1683 * Push the close HTML for a recently opened container onto the stack.
473dd811 1684 *
d9c8f425 1685 * @param string $type The type of container. This is checked when {@link pop()}
1686 * is called and must match, otherwise a developer debug warning is output.
1687 * @param string $closehtml The HTML required to close the container.
fa1afe32 1688 */
d9c8f425 1689 public function push($type, $closehtml) {
1690 $container = new stdClass;
1691 $container->type = $type;
1692 $container->closehtml = $closehtml;
1693 if ($this->isdebugging) {
1694 $this->log('Open', $type);
3aaa27f4 1695 }
d9c8f425 1696 array_push($this->opencontainers, $container);
ebebf55c 1697 }
34a2777c 1698
fa1afe32 1699 /**
d9c8f425 1700 * Pop the HTML for the next closing container from the stack. The $type
1701 * must match the type passed when the container was opened, otherwise a
1702 * warning will be output.
473dd811 1703 *
d9c8f425 1704 * @param string $type The type of container.
1705 * @return string the HTML required to close the container.
fa1afe32 1706 */
d9c8f425 1707 public function pop($type) {
1708 if (empty($this->opencontainers)) {
1709 debugging('<p>There are no more open containers. This suggests there is a nesting problem.</p>' .
1710 $this->output_log(), DEBUG_DEVELOPER);
1711 return;
3aaa27f4 1712 }
ebebf55c 1713
d9c8f425 1714 $container = array_pop($this->opencontainers);
1715 if ($container->type != $type) {
1716 debugging('<p>The type of container to be closed (' . $container->type .
1717 ') does not match the type of the next open container (' . $type .
1718 '). This suggests there is a nesting problem.</p>' .
1719 $this->output_log(), DEBUG_DEVELOPER);
ebebf55c 1720 }
d9c8f425 1721 if ($this->isdebugging) {
1722 $this->log('Close', $type);
e8775320 1723 }
d9c8f425 1724 return $container->closehtml;
ebebf55c 1725 }
e8775320 1726
fa1afe32 1727 /**
d9c8f425 1728 * Close all but the last open container. This is useful in places like error
1729 * handling, where you want to close all the open containers (apart from <body>)
1730 * before outputting the error message.
473dd811 1731 *
d9c8f425 1732 * @param bool $shouldbenone assert that the stack should be empty now - causes a
1733 * developer debug warning if it isn't.
1734 * @return string the HTML required to close any open containers inside <body>.
fa1afe32 1735 */
d9c8f425 1736 public function pop_all_but_last($shouldbenone = false) {
1737 if ($shouldbenone && count($this->opencontainers) != 1) {
1738 debugging('<p>Some HTML tags were opened in the body of the page but not closed.</p>' .
1739 $this->output_log(), DEBUG_DEVELOPER);
1740 }
1741 $output = '';
1742 while (count($this->opencontainers) > 1) {
1743 $container = array_pop($this->opencontainers);
1744 $output .= $container->closehtml;
e8775320 1745 }
d9c8f425 1746 return $output;
e8775320 1747 }
34a2777c 1748
ebebf55c 1749 /**
d9c8f425 1750 * You can call this function if you want to throw away an instance of this
1751 * class without properly emptying the stack (for example, in a unit test).
1752 * Calling this method stops the destruct method from outputting a developer
1753 * debug warning. After calling this method, the instance can no longer be used.
ebebf55c 1754 */
d9c8f425 1755 public function discard() {
1756 $this->opencontainers = null;
ebebf55c 1757 }
d9c8f425 1758
fa1afe32 1759 /**
d9c8f425 1760 * Adds an entry to the log.
473dd811 1761 *
d9c8f425 1762 * @param string $action The name of the action
1763 * @param string $type The type of action
fa1afe32 1764 */
d9c8f425 1765 protected function log($action, $type) {
1766 $this->log[] = '<li>' . $action . ' ' . $type . ' at:' .
1767 format_backtrace(debug_backtrace()) . '</li>';
ebebf55c 1768 }
34a2777c 1769
fa1afe32 1770 /**
d9c8f425 1771 * Outputs the log's contents as a HTML list.
473dd811 1772 *
d9c8f425 1773 * @return string HTML list of the log
fa1afe32 1774 */
d9c8f425 1775 protected function output_log() {
1776 return '<ul>' . implode("\n", $this->log) . '</ul>';
34a2777c 1777 }
6fc267a0 1778}