theme-anomaly MDL-20175 Fixed regressions after recent block changes
[moodle.git] / lib / outputlib.php
CommitLineData
571fa828 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
78946b9b
PS
18/**
19 * Functions for generating the HTML that Moodle should output.
20 *
21 * Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
22 * for an overview.
23 *
24 * @package moodlecore
25 * @copyright 2009 Tim Hunt
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
d9c8f425 29require_once($CFG->libdir.'/outputcomponents.php');
30require_once($CFG->libdir.'/outputactions.php');
31require_once($CFG->libdir.'/outputfactories.php');
d9c8f425 32require_once($CFG->libdir.'/outputrenderers.php');
0bb38e8c 33require_once($CFG->libdir.'/outputrequirementslib.php');
d9c8f425 34
571fa828 35/**
78946b9b
PS
36 * Invalidate all server and client side caches.
37 * @return void
38 */
39function theme_reset_all_caches() {
40 global $CFG;
41 require_once("$CFG->libdir/filelib.php");
42
43 set_config('themerev', empty($CFG->themerev) ? 1 : $CFG->themerev+1);
44 fulldelete("$CFG->dataroot/cache/theme");
45}
46
47/**
48 * Enable or disable theme designer mode.
49 * @param bool $state
50 * @return void
51 */
52function theme_set_designer_mod($state) {
53 theme_reset_all_caches();
54 set_config('themedesignermode', (int)!empty($state));
55}
56
57/**
58 * Returns current theme revision number.
59 * @return int
571fa828 60 */
78946b9b
PS
61function theme_get_revision() {
62 global $CFG;
63
64 if (empty($CFG->themedesignermode)) {
65 if (empty($CFG->themerev)) {
66 return -1;
67 } else {
68 return $CFG->themerev;
69 }
70
71 } else {
72 return -1;
73 }
74}
75
571fa828 76
571fa828 77/**
fdeb7fa1 78 * This class represents the configuration variables of a Moodle theme.
79 *
80 * All the variables with access: public below (with a few exceptions that are marked)
81 * are the properties you can set in your theme's config.php file.
82 *
83 * There are also some methods and protected variables that are part of the inner
84 * workings of Moodle's themes system. If you are just editing a theme's config.php
fa1afe32 85 * file, you can just ignore those, and the following information for developers.
ebebf55c 86 *
87 * Normally, to create an instance of this class, you should use the
88 * {@link theme_config::load()} factory method to load a themes config.php file.
fa1afe32 89 * However, normally you don't need to bother, because moodle_page (that is, $PAGE)
fdeb7fa1 90 * will create one for you, accessible as $PAGE->theme.
571fa828 91 *
92 * @copyright 2009 Tim Hunt
93 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
94 * @since Moodle 2.0
95 */
ebebf55c 96class theme_config {
5f0baa43
PS
97 /**
98 * @var string default theme, used when requested theme not found
99 * TODO: MDL-21149 replace with some new default theme when implemented
100 */
fef27578 101 const DEFAULT_THEME = 'standardold';
5f0baa43 102
ebebf55c 103 /**
78946b9b
PS
104 * You can base your theme on other themes by linking to the other theme as
105 * parents. This lets you use the CSS and layouts from the other themes
106 * (see {@link $layouts}).
fdeb7fa1 107 * That makes it easy to create a new theme that is similar to another one
108 * but with a few changes. In this theme's CSS you only need to override
109 * those rules you want to change.
fdeb7fa1 110 *
78946b9b 111 * @var array
fdeb7fa1 112 */
78946b9b 113 public $parents;
571fa828 114
fdeb7fa1 115 /**
78946b9b
PS
116 * The names of all the stylesheets from this theme that you would
117 * like included, in order. Give the names of the files without .css.
fdeb7fa1 118 *
78946b9b 119 * @var array
fdeb7fa1 120 */
78946b9b 121 public $sheets = array();
571fa828 122
fdeb7fa1 123 /**
2e7dbbd7 124 * The names of all the stylesheets from parents that should be excluded.
78946b9b
PS
125 * true value may be used to specify all parents or all themes from one parent.
126 * If no value specified value from parent theme used.
fdeb7fa1 127 *
78946b9b 128 * @var array or arrays, true means all, null means use value from parent
fdeb7fa1 129 */
78946b9b 130 public $parents_exclude_sheets = null;
571fa828 131
fdeb7fa1 132 /**
78946b9b
PS
133 * List of plugin sheets to be excluded.
134 * If no value specified value from parent theme used.
135 *
136 * @var array of full plugin names, null means use value from parent
fdeb7fa1 137 */
78946b9b 138 public $plugins_exclude_sheets = null;
571fa828 139
fdeb7fa1 140 /**
78946b9b
PS
141 * List of style sheets that are included in the text editor bodies.
142 * Sheets from parent themes are used automatically and can not be excluded.
fdeb7fa1 143 *
78946b9b 144 * @var array
fdeb7fa1 145 */
78946b9b 146 public $editor_sheets = array();
571fa828 147
38aafea2
PS
148 /**
149 * The names of all the javascript files this theme that you would
04c01408 150 * like included from head, in order. Give the names of the files without .js.
38aafea2
PS
151 *
152 * @var array
153 */
154 public $javascripts = array();
155
04c01408
PS
156 /**
157 * The names of all the javascript files this theme that you would
158 * like included from footer, in order. Give the names of the files without .js.
159 *
160 * @var array
161 */
162 public $javascripts_footer = array();
163
38aafea2 164 /**
b2ae9661 165 * The names of all the javascript files from parents that should be excluded.
38aafea2
PS
166 * true value may be used to specify all parents or all themes from one parent.
167 * If no value specified value from parent theme used.
168 *
169 * @var array or arrays, true means all, null means use value from parent
170 */
171 public $parents_exclude_javascripts = null;
172
fdeb7fa1 173 /**
78946b9b 174 * Which file to use for each page layout.
fdeb7fa1 175 *
78946b9b
PS
176 * This is an array of arrays. The keys of the outer array are the different layouts.
177 * Pages in Moodle are using several different layouts like 'normal', 'course', 'home',
178 * 'popup', 'form', .... The most reliable way to get a complete list is to look at
179 * {@link http://cvs.moodle.org/moodle/theme/base/config.php?view=markup the base theme config.php file}.
d4a03c00 180 * That file also has a good example of how to set this setting.
fdeb7fa1 181 *
78946b9b 182 * For each layout, the value in the outer array is an array that describes
d4a03c00 183 * how you want that type of page to look. For example
184 * <pre>
185 * $THEME->layouts = array(
39df78bf 186 * // Most pages - if we encounter an unknown or a missing page type, this one is used.
191b267b 187 * 'standard' => array(
78946b9b
PS
188 * 'theme' = 'mytheme',
189 * 'file' => 'normal.php',
d4a03c00 190 * 'regions' => array('side-pre', 'side-post'),
191 * 'defaultregion' => 'side-post'
192 * ),
193 * // The site home page.
194 * 'home' => array(
78946b9b
PS
195 * 'theme' = 'mytheme',
196 * 'file' => 'home.php',
d4a03c00 197 * 'regions' => array('side-pre', 'side-post'),
198 * 'defaultregion' => 'side-post'
199 * ),
200 * // ...
201 * );
202 * </pre>
fdeb7fa1 203 *
78946b9b
PS
204 * 'theme' name of the theme where is the layout located
205 * 'file' is the layout file to use for this type of page.
206 * layout files are stored in layout subfolder
d4a03c00 207 * 'regions' This lists the regions on the page where blocks may appear. For
208 * each region you list here, your layout file must include a call to
209 * <pre>
210 * echo $OUTPUT->blocks_for_region($regionname);
211 * </pre>
212 * or equivalent so that the blocks are actually visible.
fdeb7fa1 213 *
d4a03c00 214 * 'defaultregion' If the list of regions is non-empty, then you must pick
215 * one of the one of them as 'default'. This has two meanings. First, this is
216 * where new blocks are added. Second, if there are any blocks associated with
fa1afe32 217 * the page, but in non-existent regions, they appear here. (Imaging, for example,
d4a03c00 218 * that someone added blocks using a different theme that used different region
219 * names, and then switched to this theme.)
fdeb7fa1 220 *
221 * @var array
222 */
d4a03c00 223 public $layouts = array();
571fa828 224
fdeb7fa1 225 /**
226 * With this you can control the colours of the big MP3 player
227 * that is used for MP3 resources.
228 *
229 * @var string
230 */
ebebf55c 231 public $resource_mp3player_colors = 'bgColour=000000&btnColour=ffffff&btnBorderColour=cccccc&iconColour=000000&iconOverColour=00cc00&trackColour=cccccc&handleColour=ffffff&loaderColour=ffffff&font=Arial&fontColour=3333FF&buffer=10&waitForPlay=no&autoPlay=yes';
571fa828 232
fdeb7fa1 233 /**
78946b9b
PS
234 * With this you can control the colours of the small MP3 player
235 * that is used elsewhere.
236 *
fdeb7fa1 237 * @var string
238 */
ebebf55c 239 public $filter_mediaplugin_colors = 'bgColour=000000&btnColour=ffffff&btnBorderColour=cccccc&iconColour=000000&iconOverColour=00cc00&trackColour=cccccc&handleColour=ffffff&loaderColour=ffffff&waitForPlay=yes';
240
fdeb7fa1 241 /**
242 * Name of the renderer factory class to use.
243 *
244 * This is an advanced feature. Moodle output is generated by 'renderers',
245 * you can customise the HTML that is output by writing custom renderers,
246 * and then you need to specify 'renderer factory' so that Moodle can find
247 * your renderers.
248 *
249 * There are some renderer factories supplied with Moodle. Please follow these
250 * links to see what they do.
251 * <ul>
252 * <li>{@link standard_renderer_factory} - the default.</li>
253 * <li>{@link theme_overridden_renderer_factory} - use this if you want to write
78946b9b 254 * your own custom renderers in a lib.php file in this theme (or the parent theme).</li>
fdeb7fa1 255 * </ul>
256 *
257 * @var string name of a class implementing the {@link renderer_factory} interface.
258 */
ebebf55c 259 public $rendererfactory = 'standard_renderer_factory';
ebebf55c 260
fdeb7fa1 261 /**
78946b9b 262 * Function to do custom CSS post-processing.
fdeb7fa1 263 *
78946b9b
PS
264 * This is an advanced feature. If you want to do custom post-processing on the
265 * CSS before it is output (for example, to replace certain variable names
266 * with particular values) you can give the name of a function here.
fdeb7fa1 267 *
78946b9b 268 * @var string the name of a function.
fdeb7fa1 269 */
78946b9b 270 public $csspostprocess = null;
571fa828 271
272 /**
78946b9b
PS
273 * Accessibility: Right arrow-like character is
274 * used in the breadcrumb trail, course navigation menu
275 * (previous/next activity), calendar, and search forum block.
276 * If the theme does not set characters, appropriate defaults
277 * are set automatically. Please DO NOT
278 * use &lt; &gt; &raquo; - these are confusing for blind users.
fdeb7fa1 279 *
78946b9b
PS
280 * @var string
281 */
282 public $rarrow = null;
283
284 /**
285 * Accessibility: Right arrow-like character is
286 * used in the breadcrumb trail, course navigation menu
287 * (previous/next activity), calendar, and search forum block.
288 * If the theme does not set characters, appropriate defaults
289 * are set automatically. Please DO NOT
290 * use &lt; &gt; &raquo; - these are confusing for blind users.
ebebf55c 291 *
78946b9b 292 * @var string
571fa828 293 */
78946b9b
PS
294 public $larrow = null;
295
296
297 //==Following properties are not configurable from theme config.php==
571fa828 298
fdeb7fa1 299 /**
78946b9b
PS
300 * The name of this theme. Set automatically when this theme is
301 * loaded. This can not be set in theme config.php
302 * @var string
fdeb7fa1 303 */
78946b9b 304 public $name;
fdeb7fa1 305
306 /**
78946b9b
PS
307 * the folder where this themes files are stored. This is set
308 * automatically. This can not be set in theme config.php
309 * @var string
fdeb7fa1 310 */
78946b9b 311 public $dir;
fdeb7fa1 312
313 /**
78946b9b
PS
314 * Theme settings stored in config_plugins table.
315 * This can not be set in theme config.php
316 * @var object
317 */
318 public $setting = null;
319
d2c394f3
SH
320 /**
321 * If set to true and the theme enables the dock then blocks will be able
322 * to be moved to the special dock
323 * @var bool
324 */
325 public $enable_dock = false;
326
78946b9b
PS
327 /**
328 * Instance of the renderer_factory implementation
fdeb7fa1 329 * we are using. Implementation detail.
78946b9b 330 * @var renderer_factory
fdeb7fa1 331 */
332 protected $rf = null;
333
334 /**
78946b9b
PS
335 * List of parent config objects.
336 * @var array list of parent configs
337 **/
338 protected $parent_configs = array();
fdeb7fa1 339
571fa828 340 /**
ebebf55c 341 * Load the config.php file for a particular theme, and return an instance
342 * of this class. (That is, this is a factory method.)
343 *
344 * @param string $themename the name of the theme.
345 * @return theme_config an instance of this class.
571fa828 346 */
ebebf55c 347 public static function load($themename) {
348 global $CFG;
571fa828 349
78946b9b
PS
350 // load theme settings from db
351 try {
352 $settings = get_config('theme_'.$themename);
353 } catch (dml_exception $e) {
da0c0e25 354 // most probably moodle tables not created yet
78946b9b
PS
355 $settings = new object();
356 }
ebebf55c 357
78946b9b
PS
358 if ($config = theme_config::find_theme_config($themename, $settings)) {
359 return new theme_config($config);
5f0baa43
PS
360
361 } else if ($themename == theme_config::DEFAULT_THEME) {
362 throw new coding_exception('Default theme '.theme_config::DEFAULT_THEME.' not available or broken!');
363
78946b9b
PS
364 } else {
365 // bad luck, the requested theme has some problems - admin see details in theme config
5f0baa43 366 return new theme_config(theme_config::find_theme_config(theme_config::DEFAULT_THEME, $settings));
78946b9b
PS
367 }
368 }
ebebf55c 369
78946b9b
PS
370 /**
371 * Theme diagnostic code. It is very problematic to send debug output
372 * to the actual CSS file, instead this functions is supposed to
ae5b6801 373 * diagnose given theme and highlights all potential problems.
78946b9b
PS
374 * This information should be available from the theme selection page
375 * or some other debug page for theme designers.
376 *
377 * @param string $themename
378 * @return array description of problems
379 */
380 public static function diagnose($themename) {
5ef719e7 381 //TODO: MDL-21108
78946b9b
PS
382 return array();
383 }
384
385 /**
386 * Private constructor, can be called only from the factory method.
387 * @param stdClass $config
388 */
389 private function __construct($config) {
390 global $CFG; //needed for included lib.php files
391
392 $this->settings = $config->settings;
393 $this->name = $config->name;
394 $this->dir = $config->dir;
395
396 if ($this->name != 'base') {
397 $baseconfig = theme_config::find_theme_config('base', $this->settings);
398 } else {
399 $baseconfig = $config;
571fa828 400 }
ebebf55c 401
fef27578 402 $configurable = array('parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'javascripts', 'javascripts_footer',
d2c394f3 403 'parents_exclude_javascripts', 'layouts', 'resource_mp3player_colors', 'enable_dock',
78946b9b 404 'filter_mediaplugin_colors', 'rendererfactory', 'csspostprocess', 'editor_sheets', 'rarrow', 'larrow');
ebebf55c 405
78946b9b
PS
406 foreach ($config as $key=>$value) {
407 if (in_array($key, $configurable)) {
408 $this->$key = $value;
409 }
410 }
411
412 // verify all parents and load configs and renderers
413 foreach ($this->parents as $parent) {
414 if ($parent == 'base') {
415 $parent_config = $baseconfig;
416 } else if (!$parent_config = theme_config::find_theme_config($parent, $this->settings)) {
417 // this is not good - better exclude faulty parents
418 continue;
419 }
420 $libfile = $parent_config->dir.'/lib.php';
421 if (is_readable($libfile)) {
422 // theme may store various function here
423 include_once($libfile);
424 }
425 $renderersfile = $parent_config->dir.'/renderers.php';
426 if (is_readable($renderersfile)) {
427 // may contain core and plugin renderers and renderer factory
428 include_once($renderersfile);
429 }
430 $this->parent_configs[$parent] = $parent_config;
431 $rendererfile = $parent_config->dir.'/renderers.php';
432 if (is_readable($rendererfile)) {
433 // may contain core and plugin renderers and renderer factory
434 include_once($rendererfile);
435 }
436 }
437 $libfile = $this->dir.'/lib.php';
438 if (is_readable($libfile)) {
439 // theme may store various function here
440 include_once($libfile);
441 }
442 $rendererfile = $this->dir.'/renderers.php';
443 if (is_readable($rendererfile)) {
444 // may contain core and plugin renderers and renderer factory
445 include_once($rendererfile);
446 }
447
448 // cascade all layouts properly
449 foreach ($baseconfig->layouts as $layout=>$value) {
450 if (!isset($this->layouts[$layout])) {
451 foreach ($this->parent_configs as $parent_config) {
452 if (isset($parent_config->layouts[$layout])) {
453 $this->layouts[$layout] = $parent_config->layouts[$layout];
454 continue 2;
455 }
456 }
457 $this->layouts[$layout] = $value;
458 }
459 }
460
461 //fix arrows if needed
462 $this->check_theme_arrows();
571fa828 463 }
464
78946b9b
PS
465 /*
466 * Checks if arrows $THEME->rarrow, $THEME->larrow have been set (theme/-/config.php).
467 * If not it applies sensible defaults.
468 *
469 * Accessibility: right and left arrow Unicode characters for breadcrumb, calendar,
470 * search forum block, etc. Important: these are 'silent' in a screen-reader
471 * (unlike &gt; &raquo;), and must be accompanied by text.
34a2777c 472 */
78946b9b
PS
473 private function check_theme_arrows() {
474 if (!isset($this->rarrow) and !isset($this->larrow)) {
475 // Default, looks good in Win XP/IE 6, Win/Firefox 1.5, Win/Netscape 8...
476 // Also OK in Win 9x/2K/IE 5.x
477 $this->rarrow = '&#x25BA;';
478 $this->larrow = '&#x25C4;';
479 if (empty($_SERVER['HTTP_USER_AGENT'])) {
480 $uagent = '';
ebebf55c 481 } else {
78946b9b
PS
482 $uagent = $_SERVER['HTTP_USER_AGENT'];
483 }
484 if (false !== strpos($uagent, 'Opera')
485 || false !== strpos($uagent, 'Mac')) {
486 // Looks good in Win XP/Mac/Opera 8/9, Mac/Firefox 2, Camino, Safari.
487 // Not broken in Mac/IE 5, Mac/Netscape 7 (?).
488 $this->rarrow = '&#x25B6;';
489 $this->larrow = '&#x25C0;';
490 }
491 elseif (false !== strpos($uagent, 'Konqueror')) {
492 $this->rarrow = '&rarr;';
493 $this->larrow = '&larr;';
494 }
495 elseif (isset($_SERVER['HTTP_ACCEPT_CHARSET'])
496 && false === stripos($_SERVER['HTTP_ACCEPT_CHARSET'], 'utf-8')) {
497 // (Win/IE 5 doesn't set ACCEPT_CHARSET, but handles Unicode.)
498 // To be safe, non-Unicode browsers!
499 $this->rarrow = '&gt;';
500 $this->larrow = '&lt;';
501 }
502
503 /// RTL support - in RTL languages, swap r and l arrows
504 if (right_to_left()) {
505 $t = $this->rarrow;
506 $this->rarrow = $this->larrow;
507 $this->larrow = $t;
ebebf55c 508 }
ebebf55c 509 }
78946b9b 510 }
ebebf55c 511
78946b9b
PS
512 /**
513 * Returns output renderer prefixes, these are used when looking
514 * for the overriden renderers in themes.
515 * @return array
516 */
517 public function renderer_prefixes() {
518 global $CFG; // just in case the included files need it
519
596af93a 520 $prefixes = array('theme_'.$this->name);
78946b9b
PS
521
522 foreach ($this->parent_configs as $parent) {
523 $prefixes[] = 'theme_'.$parent->name;
524 }
525
526 return $prefixes;
34a2777c 527 }
528
571fa828 529 /**
78946b9b
PS
530 * Returns the stylesheet URL of this editor content
531 * @param bool $encoded false means use & and true use &amp; in URLs
532 * @return string
571fa828 533 */
78946b9b
PS
534 public function editor_css_url($encoded=true) {
535 global $CFG;
536
537 $rev = theme_get_revision();
538
539 if ($rev > -1) {
540 $params = array('theme'=>$this->name,'rev'=>$rev, 'type'=>'editor');
541 return new moodle_url($CFG->httpswwwroot.'/theme/styles.php', $params);
542 } else {
543 $params = array('theme'=>$this->name, 'type'=>'editor');
544 return new moodle_url($CFG->httpswwwroot.'/theme/styles_debug.php', $params);
571fa828 545 }
571fa828 546 }
547
548 /**
78946b9b
PS
549 * Returns the content of the CSS to be used in editor content
550 * @return string
571fa828 551 */
78946b9b
PS
552 public function editor_css_content() {
553 global $CFG;
554
555 $css = '';
556
557 // first editor plugins
558 $plugins = get_plugin_list('editor');
559 foreach ($plugins as $plugin=>$fulldir) {
560 $sheetfile = "$fulldir/editor_styles.css";
561 if (is_readable($sheetfile)) {
562 $css .= "/*** Editor $plugin content CSS ***/\n\n" . file_get_contents($sheetfile) . "\n\n";
563 }
564 }
565 // then parent themes
566 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
567 if (empty($parent_config->editor_sheets)) {
568 continue;
569 }
570 foreach ($parent_config->editor_sheets as $sheet) {
571 $sheetfile = "$parent_config->dir/$sheet.css";
572 if (is_readable($sheetfile)) {
573 $css .= "/*** Parent theme $parent/$sheet ***/\n\n" . file_get_contents($sheetfile) . "\n\n";
574 }
575 }
576 }
577 // finally this theme
578 if (!empty($this->editor_sheets)) {
579 foreach ($this->editor_sheets as $sheet) {
580 $sheetfile = "$this->dir/$sheet.css";
581 if (is_readable($sheetfile)) {
582 $css .= "/*** Theme $sheet ***/\n\n" . file_get_contents($sheetfile) . "\n\n";
583 }
584 }
585 }
586
587 return $this->post_process($css);
571fa828 588 }
589
590 /**
78946b9b
PS
591 * Get the stylesheet URL of this theme
592 * @param bool $encoded false means use & and true use &amp; in URLs
38aafea2 593 * @return array of moodle_url
571fa828 594 */
efaa4c08 595 public function css_urls(moodle_page $page) {
78946b9b
PS
596 global $CFG;
597
598 $rev = theme_get_revision();
599
efaa4c08 600 $urls = array();
efaa4c08 601
78946b9b
PS
602 if ($rev > -1) {
603 $params = array('theme'=>$this->name,'rev'=>$rev);
604 if (check_browser_version('MSIE', 5) and !check_browser_version('MSIE', 8)) {
605 $params['type'] = 'ie';
606 }
efaa4c08 607 $urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles.php', $params);
78946b9b
PS
608
609 } else {
7829cf79
PS
610 // find out the current CSS and cache it now for 5 seconds
611 // the point is to construct the CSS only once and pass it through the
612 // dataroot to the script that actually serves the sheets
d6b64d15
PS
613 if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
614 define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php
615 }
7829cf79
PS
616 $candidatesheet = "$CFG->dataroot/cache/theme/$this->name/designer.ser";
617 if (!file_exists($candidatesheet)) {
618 $css = $this->css_content();
619 check_dir_exists(dirname($candidatesheet), true, true);
620 file_put_contents($candidatesheet, serialize($css));
621
d6b64d15 622 } else if (filemtime($candidatesheet) > time() - THEME_DESIGNER_CACHE_LIFETIME) {
7829cf79
PS
623 if ($css = file_get_contents($candidatesheet)) {
624 $css = unserialize($css);
625 } else {
626 unlink($candidatesheet);
627 $css = $this->css_content();
628 }
629
630 } else {
631 unlink($candidatesheet);
632 $css = $this->css_content();
633 file_put_contents($candidatesheet, serialize($css));
634 }
635
c724f835 636 $baseurl = $CFG->httpswwwroot.'/theme/styles_debug.php';
c09e1a2c
PS
637
638 if (check_browser_version('MSIE', 5)) {
639 // lalala, IE does not allow more than 31 linked CSS files from main document
640 $urls[] = new moodle_url($CFG->httpswwwroot.'/theme/styles_debug.php', array('theme'=>$this->name, 'type'=>'ie'));
641
642 } else {
643 foreach ($css['plugins'] as $plugin=>$unused) {
644 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'plugin', 'subtype'=>$plugin));
645 }
646 foreach ($css['parents'] as $parent=>$sheets) {
647 foreach ($sheets as $sheet=>$unused2) {
648 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'parent', 'subtype'=>$parent, 'sheet'=>$sheet));
649 }
650 }
651 foreach ($css['theme'] as $sheet=>$unused) {
652 $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 653 }
78946b9b 654 }
78946b9b 655 }
efaa4c08
PS
656
657 return $urls;
571fa828 658 }
34a2777c 659
ebebf55c 660 /**
78946b9b
PS
661 * Returns the content of the one huge CSS merged from all style sheets.
662 * @return string
ebebf55c 663 */
78946b9b 664 public function css_content() {
34a2777c 665 global $CFG;
666
c724f835 667 $css = array('plugins'=>array(), 'parents'=>array(), 'theme'=>array());
78946b9b 668
8c96c3cb 669 // get all plugin sheets
78946b9b
PS
670 $excludes = null;
671 if (is_array($this->plugins_exclude_sheets) or $this->plugins_exclude_sheets === true) {
672 $excludes = $this->plugins_exclude_sheets;
673 } else {
674 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
675 if (!isset($parent_config->plugins_exclude_sheets)) {
676 continue;
677 }
678 if (is_array($parent_config->plugins_exclude_sheets) or $parent_config->plugins_exclude_sheets === true) {
679 $excludes = $parent_config->plugins_exclude_sheets;
680 break;
681 }
682 }
ebebf55c 683 }
78946b9b
PS
684 if ($excludes !== true) {
685 foreach (get_plugin_types() as $type=>$unused) {
686 if ($type === 'theme') {
687 continue;
688 }
689 if (!empty($excludes[$type]) and $excludes[$type] === true) {
690 continue;
691 }
692 $plugins = get_plugin_list($type);
693 foreach ($plugins as $plugin=>$fulldir) {
694 if (!empty($excludes[$type]) and is_array($excludes[$type])
695 and in_array($plugin, $excludes[$type])) {
696 continue;
697 }
698 $sheetfile = "$fulldir/styles.css";
699 if (is_readable($sheetfile)) {
700 $css['plugins'][$type.'_'.$plugin] = $this->post_process("/*** Standard plugin $type/$plugin ***/\n\n" . file_get_contents($sheetfile));
701 }
702 }
703 }
34a2777c 704 }
705
78946b9b
PS
706 // find out wanted parent sheets
707 $excludes = null;
708 if (is_array($this->parents_exclude_sheets) or $this->parents_exclude_sheets === true) {
709 $excludes = $this->parents_exclude_sheets;
710 } else {
711 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
712 if (!isset($parent_config->parents_exclude_sheets)) {
713 continue;
714 }
715 if (is_array($parent_config->parents_exclude_sheets) or $parent_config->parents_exclude_sheets === true) {
716 $excludes = $parent_config->parents_exclude_sheets;
717 break;
718 }
719 }
720 }
721 if ($excludes !== true) {
722 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
723 $parent = $parent_config->name;
724 if (empty($parent_config->sheets)) {
725 continue;
726 }
727 if (!empty($excludes[$parent]) and $excludes[$parent] === true) {
728 continue;
729 }
730 foreach ($parent_config->sheets as $sheet) {
731 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
732 and in_array($sheet, $excludes[$parent])) {
733 continue;
734 }
735 $sheetfile = "$parent_config->dir/style/$sheet.css";
736 if (is_readable($sheetfile)) {
358c13cb 737 $css['parents'][$parent][$sheet] = $this->post_process("/*** Parent theme $parent/style/$sheet.css ***/\n\n" . file_get_contents($sheetfile));
78946b9b
PS
738 }
739 }
740 }
741 }
34a2777c 742
78946b9b
PS
743 // current theme sheets
744 if (is_array($this->sheets)) {
745 foreach ($this->sheets as $sheet) {
746 $sheetfile = "$this->dir/style/$sheet.css";
747 if (is_readable($sheetfile)) {
358c13cb 748 $css['theme'][$sheet] = $this->post_process("/*** This theme $this->name/style/$sheet ***/\n\n" . file_get_contents($sheetfile));
78946b9b 749 }
ebebf55c 750 }
78946b9b
PS
751 }
752
753 return $css;
754 }
34a2777c 755
e68c5f36
PS
756
757 /**
758 * Get the javascript URL of this theme
baeb20bb 759 * @param bool $inhead true means haed url, false means footer
e68c5f36
PS
760 * @return moodle_url
761 */
baeb20bb 762 public function javascript_url($inhead) {
e68c5f36
PS
763 global $CFG;
764
765 $rev = theme_get_revision();
e68c5f36 766 $params = array('theme'=>$this->name,'rev'=>$rev);
baeb20bb
PS
767 $params['type'] = $inhead ? 'head' : 'footer';
768
769 return new moodle_url($CFG->httpswwwroot.'/theme/javascript.php', $params);
e68c5f36
PS
770 }
771
772 /**
773 * Returns the content of the one huge javascript file merged from all theme javascript files.
baeb20bb 774 * @param bool $inhead
e68c5f36
PS
775 * @return string
776 */
baeb20bb
PS
777 public function javascript_content($type) {
778 $type = ($type === 'footer') ? 'javascripts_footer' : 'javascripts';
04c01408 779
358c13cb
PS
780 $js = array();
781 // find out wanted parent javascripts
782 $excludes = null;
783 if (is_array($this->parents_exclude_javascripts) or $this->parents_exclude_javascripts === true) {
784 $excludes = $this->parents_exclude_javascripts;
785 } else {
786 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
787 if (!isset($parent_config->parents_exclude_javascripts)) {
788 continue;
789 }
790 if (is_array($parent_config->parents_exclude_javascripts) or $parent_config->parents_exclude_javascripts === true) {
791 $excludes = $parent_config->parents_exclude_javascripts;
792 break;
793 }
794 }
795 }
796 if ($excludes !== true) {
797 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
798 $parent = $parent_config->name;
04c01408 799 if (empty($parent_config->$type)) {
358c13cb
PS
800 continue;
801 }
802 if (!empty($excludes[$parent]) and $excludes[$parent] === true) {
803 continue;
804 }
04c01408 805 foreach ($parent_config->$type as $javascript) {
358c13cb
PS
806 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
807 and in_array($javascript, $excludes[$parent])) {
808 continue;
809 }
810 $javascriptfile = "$parent_config->dir/javascript/$javascript.js";
811 if (is_readable($javascriptfile)) {
812 $js[] = "/*** Parent theme $parent/javascript/$javascript.js ***/\n\n" . file_get_contents($javascriptfile);
813 }
814 }
815 }
816 }
817
818 // current theme javascripts
04c01408
PS
819 if (is_array($this->$type)) {
820 foreach ($this->$type as $javascript) {
358c13cb
PS
821 $javascriptfile = "$this->dir/javascript/$javascript.js";
822 if (is_readable($javascriptfile)) {
823 $js[] = "/*** This theme $this->name/javascript/$javascript.js ***/\n\n" . file_get_contents($javascriptfile);
824 }
825 }
826 }
827
828 return implode("\n\n", $js);
e68c5f36
PS
829 }
830
78946b9b 831 protected function post_process($css) {
df06c1c4
PS
832 global $CFG;
833
78946b9b
PS
834 // now resolve all image locations
835 if (preg_match_all('/\[\[pix:([a-z_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) {
836 $replaced = array();
837 foreach ($matches as $match) {
838 if (isset($replaced[$match[0]])) {
839 continue;
840 }
841 $replaced[$match[0]] = true;
842 $imagename = $match[2];
843 $component = rtrim($match[1], '|');
b9bc2019 844 $imageurl = $this->pix_url($imagename, $component)->out(false);
df06c1c4
PS
845 // we do not need full url because the image.php is always in the same dir
846 $imageurl = str_replace("$CFG->httpswwwroot/theme/", '', $imageurl);
847 $css = str_replace($match[0], $imageurl, $css);
ebebf55c 848 }
34a2777c 849 }
17a6649b 850
78946b9b
PS
851 // now resolve all theme settings or do any other postprocessing
852 $csspostprocess = $this->csspostprocess;
853 if (function_exists($csspostprocess)) {
854 $css = $csspostprocess($css, $this);
fdeb7fa1 855 }
856
78946b9b 857 return $css;
ebebf55c 858 }
17a6649b 859
ebebf55c 860 /**
78946b9b
PS
861 * Return the URL for an image
862 *
863 * @param string $imagename the name of the icon.
864 * @param string $component, specification of one plugin like in get_string()
865 * @return moodle_url
fdeb7fa1 866 */
c39e5ba2 867 public function pix_url($imagename, $component) {
fdeb7fa1 868 global $CFG;
78946b9b
PS
869
870 $params = array('theme'=>$this->name, 'image'=>$imagename);
871
872 $rev = theme_get_revision();
873 if ($rev != -1) {
874 $params['rev'] = $rev;
875 }
876 if (!empty($component) and $component !== 'moodle'and $component !== 'core') {
877 $params['component'] = $component;
fdeb7fa1 878 }
78946b9b
PS
879
880 return new moodle_url("$CFG->httpswwwroot/theme/image.php", $params);
fdeb7fa1 881 }
882
883 /**
78946b9b
PS
884 * Resolves the real image location.
885 * @param string $image name of image, may contain relative path
886 * @param string $component
887 * @return string full file path
fdeb7fa1 888 */
78946b9b
PS
889 public function resolve_image_location($image, $component) {
890 global $CFG;
891
892 if ($component === 'moodle' or $component === 'core' or empty($component)) {
893 if ($imagefile = $this->image_exists("$this->dir/pix_core/$image")) {
894 return $imagefile;
895 }
896 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
897 if ($imagefile = $this->image_exists("$parent_config->dir/pix_core/$image")) {
898 return $imagefile;
899 }
900 }
901 if ($imagefile = $this->image_exists("$CFG->dirroot/pix/$image")) {
902 return $imagefile;
903 }
904 return null;
905
906 } else if ($component === 'theme') { //exception
907 if ($image === 'favicon') {
908 return "$this->dir/pix/favicon.ico";
909 }
910 if ($imagefile = $this->image_exists("$this->dir/pix/$image")) {
911 return $imagefile;
912 }
913 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
914 if ($imagefile = $this->image_exists("$parent_config->dir/pix/$image")) {
915 return $imagefile;
916 }
917 }
918 return null;
919
78946b9b
PS
920 } else {
921 if (strpos($component, '_') === false) {
922 $component = 'mod_'.$component;
923 }
924 list($type, $plugin) = explode('_', $component, 2);
925
926 if ($imagefile = $this->image_exists("$this->dir/pix_plugins/$type/$plugin/$image")) {
927 return $imagefile;
928 }
929 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
930 if ($imagefile = $this->image_exists("$parent_config->dir/pix_plugins/$type/$plugin/$image")) {
931 return $imagefile;
932 }
933 }
934 $dir = get_plugin_directory($type, $plugin);
935 if ($imagefile = $this->image_exists("$dir/pix/$image")) {
936 return $imagefile;
937 }
938 return null;
fdeb7fa1 939 }
fdeb7fa1 940 }
941
d4a03c00 942 /**
78946b9b
PS
943 * Checks if file with any image extension exists.
944 * @param string $filepath
945 * @return string image name with extension
d4a03c00 946 */
78946b9b
PS
947 private static function image_exists($filepath) {
948 if (file_exists("$filepath.gif")) {
949 return "$filepath.gif";
950 } else if (file_exists("$filepath.png")) {
951 return "$filepath.png";
952 } else if (file_exists("$filepath.jpg")) {
953 return "$filepath.jpg";
954 } else if (file_exists("$filepath.jpeg")) {
955 return "$filepath.jpeg";
d4a03c00 956 } else {
78946b9b 957 return false;
d4a03c00 958 }
959 }
960
fdeb7fa1 961 /**
78946b9b
PS
962 * Loads the theme config from config.php file.
963 * @param string $themename
964 * @param object $settings from config_plugins table
965 * @return object
fdeb7fa1 966 */
78946b9b
PS
967 private static function find_theme_config($themename, $settings) {
968 // We have to use the variable name $THEME (upper case) because that
969 // is what is used in theme config.php files.
fdeb7fa1 970
78946b9b
PS
971 if (!$dir = theme_config::find_theme_location($themename)) {
972 return null;
fdeb7fa1 973 }
2f67a9b3 974
78946b9b
PS
975 $THEME = new object();
976 $THEME->name = $themename;
977 $THEME->dir = $dir;
978 $THEME->settings = $settings;
979
980 global $CFG; // just in case somebody tries to use $CFG in theme config
981 include("$THEME->dir/config.php");
982
983 // verify the theme configuration is OK
984 if (!is_array($THEME->parents)) {
985 // parents option is mandatory now
986 return null;
fdeb7fa1 987 }
988
78946b9b 989 return $THEME;
fdeb7fa1 990 }
991
d4a03c00 992 /**
78946b9b
PS
993 * Finds the theme location and verifies the theme has all needed files
994 * and is not obsoleted.
995 * @param string $themename
996 * @return string full dir path or null if not found
d4a03c00 997 */
78946b9b
PS
998 private static function find_theme_location($themename) {
999 global $CFG;
1000
1001 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
1002 $dir = "$CFG->dirroot/theme/$themename";
d4a03c00 1003
7d875874 1004 } else {
78946b9b 1005 return null;
d4a03c00 1006 }
78946b9b
PS
1007
1008 if (file_exists("$dir/styles.php")) {
1009 //legacy theme - needs to be upgraded - upgrade info is displayed on the admin settings page
1010 return null;
1011 }
2f67a9b3 1012
78946b9b 1013 return $dir;
d4a03c00 1014 }
1015
1d13c75c 1016 /**
78946b9b 1017 * Get the renderer for a part of Moodle for this theme.
78946b9b 1018 * @param moodle_page $page the page we are rendering
56cbc53b 1019 * @param string $module the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
78946b9b 1020 * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
c927e35c 1021 * @param string $target one of rendering target constants
78946b9b 1022 * @return renderer_base the requested renderer.
1d13c75c 1023 */
c927e35c 1024 public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
78946b9b 1025 if (is_null($this->rf)) {
c927e35c 1026 $classname = $this->rendererfactory;
78946b9b 1027 $this->rf = new $classname($this);
1d13c75c 1028 }
78946b9b 1029
c927e35c 1030 return $this->rf->get_renderer($page, $component, $subtype, $target);
1d13c75c 1031 }
1032
fdeb7fa1 1033 /**
78946b9b
PS
1034 * Get the information from {@link $layouts} for this type of page.
1035 * @param string $pagelayout the the page layout name.
1036 * @return array the appropriate part of {@link $layouts}.
fdeb7fa1 1037 */
78946b9b
PS
1038 protected function layout_info_for_page($pagelayout) {
1039 if (array_key_exists($pagelayout, $this->layouts)) {
1040 return $this->layouts[$pagelayout];
1041 } else {
191b267b
PS
1042 debugging('Invalid page layout specified: ' . $pagelayout);
1043 return $this->layouts['standard'];
fdeb7fa1 1044 }
1045 }
1046
1047 /**
78946b9b
PS
1048 * Given the settings of this theme, and the page pagelayout, return the
1049 * full path of the page layout file to use.
1050 *
1051 * Used by {@link core_renderer::header()}.
1052 *
1053 * @param string $pagelayout the the page layout name.
1054 * @return string Full path to the lyout file to use
ebebf55c 1055 */
78946b9b 1056 public function layout_file($pagelayout) {
ebebf55c 1057 global $CFG;
fdeb7fa1 1058
78946b9b
PS
1059 $layoutinfo = $this->layout_info_for_page($pagelayout);
1060 $layoutfile = $layoutinfo['file'];
1061 $theme = $layoutinfo['theme'];
fdeb7fa1 1062
78946b9b
PS
1063 if ($dir = $this->find_theme_location($theme)) {
1064 $path = "$dir/layout/$layoutfile";
fdeb7fa1 1065
78946b9b
PS
1066 // Check the template exists, return general base theme template if not.
1067 if (is_readable($path)) {
1068 return $path;
1069 }
34a2777c 1070 }
1071
191b267b 1072 debugging('Can not find layout file for: ' . $pagelayout);
78946b9b
PS
1073 // fallback to standard normal layout
1074 return "$CFG->dirroot/theme/base/layout/general.php";
1075 }
ebebf55c 1076
78946b9b
PS
1077 /**
1078 * Returns auxiliary page layout options specified in layout configuration array.
1079 * @param string $pagelayout
1080 * @return array
1081 */
1082 public function pagelayout_options($pagelayout) {
1083 $info = $this->layout_info_for_page($pagelayout);
1084 if (!empty($info['options'])) {
1085 return $info['options'];
34a2777c 1086 }
78946b9b
PS
1087 return array();
1088 }
34a2777c 1089
78946b9b
PS
1090 /**
1091 * Inform a block_manager about the block regions this theme wants on this
1092 * page layout.
1093 * @param string $pagelayout the general type of the page.
1094 * @param block_manager $blockmanager the block_manger to set up.
1095 * @return void
1096 */
1097 public function setup_blocks($pagelayout, $blockmanager) {
1098 $layoutinfo = $this->layout_info_for_page($pagelayout);
1099 if (!empty($layoutinfo['regions'])) {
1100 $blockmanager->add_regions($layoutinfo['regions']);
1101 $blockmanager->set_default_region($layoutinfo['defaultregion']);
ebebf55c 1102 }
34a2777c 1103 }
1104
cadc0d28
TH
1105 protected function get_region_name($region, $theme) {
1106 $regionstring = get_string('region-' . $region, 'theme_' . $theme);
1107 // A name exists in this theme, so use it
1108 if (substr($regionstring, 0, 1) != '[') {
1109 return $regionstring;
1110 }
1111
1112 // Otherwise, try to find one elsewhere
1113 // Check parents, if any
1114 foreach ($this->parents as $parentthemename) {
1115 $regionstring = get_string('region-' . $region, 'theme_' . $parentthemename);
1116 if (substr($regionstring, 0, 1) != '[') {
1117 return $regionstring;
1118 }
1119 }
1120
1121 // Last resort, try the base theme for names
1122 return get_string('region-' . $region, 'theme_base');
1123 }
1124
ebebf55c 1125 /**
78946b9b
PS
1126 * Get the list of all block regions known to this theme in all templates.
1127 * @return array internal region name => human readable name.
ebebf55c 1128 */
78946b9b
PS
1129 public function get_all_block_regions() {
1130 $regions = array();
78946b9b
PS
1131 foreach ($this->layouts as $layoutinfo) {
1132 foreach ($layoutinfo['regions'] as $region) {
cadc0d28 1133 $regions[$region] = $this->get_region_name($region, $layoutinfo['theme']);
ebebf55c 1134 }
34a2777c 1135 }
78946b9b 1136 return $regions;
34a2777c 1137 }
ebebf55c 1138}
34a2777c 1139
78946b9b 1140
ebebf55c 1141/**
d9c8f425 1142 * This class keeps track of which HTML tags are currently open.
1143 *
1144 * This makes it much easier to always generate well formed XHTML output, even
1145 * if execution terminates abruptly. Any time you output some opening HTML
1146 * without the matching closing HTML, you should push the necessary close tags
1147 * onto the stack.
ebebf55c 1148 *
1149 * @copyright 2009 Tim Hunt
1150 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1151 * @since Moodle 2.0
1152 */
d9c8f425 1153class xhtml_container_stack {
1154 /** @var array stores the list of open containers. */
1155 protected $opencontainers = array();
fa1afe32 1156 /**
d9c8f425 1157 * @var array in developer debug mode, stores a stack trace of all opens and
1158 * closes, so we can output helpful error messages when there is a mismatch.
fa1afe32 1159 */
d9c8f425 1160 protected $log = array();
fa1afe32 1161 /**
d9c8f425 1162 * Store whether we are developer debug mode. We need this in several places
1163 * including in the destructor where we may no thave access to $CFG.
1164 * @var boolean
fa1afe32 1165 */
d9c8f425 1166 protected $isdebugging;
34a2777c 1167
d9c8f425 1168 public function __construct() {
1169 $this->isdebugging = debugging('', DEBUG_DEVELOPER);
ebebf55c 1170 }
34a2777c 1171
fa1afe32 1172 /**
d9c8f425 1173 * Push the close HTML for a recently opened container onto the stack.
1174 * @param string $type The type of container. This is checked when {@link pop()}
1175 * is called and must match, otherwise a developer debug warning is output.
1176 * @param string $closehtml The HTML required to close the container.
1177 * @return void
fa1afe32 1178 */
d9c8f425 1179 public function push($type, $closehtml) {
1180 $container = new stdClass;
1181 $container->type = $type;
1182 $container->closehtml = $closehtml;
1183 if ($this->isdebugging) {
1184 $this->log('Open', $type);
3aaa27f4 1185 }
d9c8f425 1186 array_push($this->opencontainers, $container);
ebebf55c 1187 }
34a2777c 1188
fa1afe32 1189 /**
d9c8f425 1190 * Pop the HTML for the next closing container from the stack. The $type
1191 * must match the type passed when the container was opened, otherwise a
1192 * warning will be output.
1193 * @param string $type The type of container.
1194 * @return string the HTML required to close the container.
fa1afe32 1195 */
d9c8f425 1196 public function pop($type) {
1197 if (empty($this->opencontainers)) {
1198 debugging('<p>There are no more open containers. This suggests there is a nesting problem.</p>' .
1199 $this->output_log(), DEBUG_DEVELOPER);
1200 return;
3aaa27f4 1201 }
ebebf55c 1202
d9c8f425 1203 $container = array_pop($this->opencontainers);
1204 if ($container->type != $type) {
1205 debugging('<p>The type of container to be closed (' . $container->type .
1206 ') does not match the type of the next open container (' . $type .
1207 '). This suggests there is a nesting problem.</p>' .
1208 $this->output_log(), DEBUG_DEVELOPER);
ebebf55c 1209 }
d9c8f425 1210 if ($this->isdebugging) {
1211 $this->log('Close', $type);
e8775320 1212 }
d9c8f425 1213 return $container->closehtml;
ebebf55c 1214 }
e8775320 1215
fa1afe32 1216 /**
d9c8f425 1217 * Close all but the last open container. This is useful in places like error
1218 * handling, where you want to close all the open containers (apart from <body>)
1219 * before outputting the error message.
1220 * @param bool $shouldbenone assert that the stack should be empty now - causes a
1221 * developer debug warning if it isn't.
1222 * @return string the HTML required to close any open containers inside <body>.
fa1afe32 1223 */
d9c8f425 1224 public function pop_all_but_last($shouldbenone = false) {
1225 if ($shouldbenone && count($this->opencontainers) != 1) {
1226 debugging('<p>Some HTML tags were opened in the body of the page but not closed.</p>' .
1227 $this->output_log(), DEBUG_DEVELOPER);
1228 }
1229 $output = '';
1230 while (count($this->opencontainers) > 1) {
1231 $container = array_pop($this->opencontainers);
1232 $output .= $container->closehtml;
e8775320 1233 }
d9c8f425 1234 return $output;
e8775320 1235 }
34a2777c 1236
ebebf55c 1237 /**
d9c8f425 1238 * You can call this function if you want to throw away an instance of this
1239 * class without properly emptying the stack (for example, in a unit test).
1240 * Calling this method stops the destruct method from outputting a developer
1241 * debug warning. After calling this method, the instance can no longer be used.
1242 * @return void
ebebf55c 1243 */
d9c8f425 1244 public function discard() {
1245 $this->opencontainers = null;
ebebf55c 1246 }
d9c8f425 1247
fa1afe32 1248 /**
d9c8f425 1249 * Adds an entry to the log.
1250 * @param string $action The name of the action
1251 * @param string $type The type of action
1252 * @return void
fa1afe32 1253 */
d9c8f425 1254 protected function log($action, $type) {
1255 $this->log[] = '<li>' . $action . ' ' . $type . ' at:' .
1256 format_backtrace(debug_backtrace()) . '</li>';
ebebf55c 1257 }
34a2777c 1258
fa1afe32 1259 /**
d9c8f425 1260 * Outputs the log's contents as a HTML list.
1261 * @return string HTML list of the log
fa1afe32 1262 */
d9c8f425 1263 protected function output_log() {
1264 return '<ul>' . implode("\n", $this->log) . '</ul>';
34a2777c 1265 }
1266}