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