MDL-20601 added missing covnersion of url placeholders
[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';
78946b9b 637 foreach ($css['plugins'] as $plugin=>$unused) {
c724f835 638 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'plugin', 'subtype'=>$plugin));
78946b9b
PS
639 }
640 foreach ($css['parents'] as $parent=>$sheets) {
641 foreach ($sheets as $sheet=>$unused2) {
c71c725e 642 $urls[] = new moodle_url($baseurl, array('theme'=>$this->name,'type'=>'parent', 'subtype'=>$parent, 'sheet'=>$sheet));
78946b9b
PS
643 }
644 }
645 foreach ($css['theme'] as $sheet=>$unused) {
c724f835 646 $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 647 }
78946b9b 648 }
efaa4c08
PS
649
650 return $urls;
571fa828 651 }
34a2777c 652
ebebf55c 653 /**
78946b9b
PS
654 * Returns the content of the one huge CSS merged from all style sheets.
655 * @return string
ebebf55c 656 */
78946b9b 657 public function css_content() {
34a2777c 658 global $CFG;
659
c724f835 660 $css = array('plugins'=>array(), 'parents'=>array(), 'theme'=>array());
78946b9b 661
8c96c3cb 662 // get all plugin sheets
78946b9b
PS
663 $excludes = null;
664 if (is_array($this->plugins_exclude_sheets) or $this->plugins_exclude_sheets === true) {
665 $excludes = $this->plugins_exclude_sheets;
666 } else {
667 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
668 if (!isset($parent_config->plugins_exclude_sheets)) {
669 continue;
670 }
671 if (is_array($parent_config->plugins_exclude_sheets) or $parent_config->plugins_exclude_sheets === true) {
672 $excludes = $parent_config->plugins_exclude_sheets;
673 break;
674 }
675 }
ebebf55c 676 }
78946b9b
PS
677 if ($excludes !== true) {
678 foreach (get_plugin_types() as $type=>$unused) {
679 if ($type === 'theme') {
680 continue;
681 }
682 if (!empty($excludes[$type]) and $excludes[$type] === true) {
683 continue;
684 }
685 $plugins = get_plugin_list($type);
686 foreach ($plugins as $plugin=>$fulldir) {
687 if (!empty($excludes[$type]) and is_array($excludes[$type])
688 and in_array($plugin, $excludes[$type])) {
689 continue;
690 }
691 $sheetfile = "$fulldir/styles.css";
692 if (is_readable($sheetfile)) {
693 $css['plugins'][$type.'_'.$plugin] = $this->post_process("/*** Standard plugin $type/$plugin ***/\n\n" . file_get_contents($sheetfile));
694 }
695 }
696 }
34a2777c 697 }
698
78946b9b
PS
699 // find out wanted parent sheets
700 $excludes = null;
701 if (is_array($this->parents_exclude_sheets) or $this->parents_exclude_sheets === true) {
702 $excludes = $this->parents_exclude_sheets;
703 } else {
704 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
705 if (!isset($parent_config->parents_exclude_sheets)) {
706 continue;
707 }
708 if (is_array($parent_config->parents_exclude_sheets) or $parent_config->parents_exclude_sheets === true) {
709 $excludes = $parent_config->parents_exclude_sheets;
710 break;
711 }
712 }
713 }
714 if ($excludes !== true) {
715 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
716 $parent = $parent_config->name;
717 if (empty($parent_config->sheets)) {
718 continue;
719 }
720 if (!empty($excludes[$parent]) and $excludes[$parent] === true) {
721 continue;
722 }
723 foreach ($parent_config->sheets as $sheet) {
724 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
725 and in_array($sheet, $excludes[$parent])) {
726 continue;
727 }
728 $sheetfile = "$parent_config->dir/style/$sheet.css";
729 if (is_readable($sheetfile)) {
358c13cb 730 $css['parents'][$parent][$sheet] = $this->post_process("/*** Parent theme $parent/style/$sheet.css ***/\n\n" . file_get_contents($sheetfile));
78946b9b
PS
731 }
732 }
733 }
734 }
34a2777c 735
78946b9b
PS
736 // current theme sheets
737 if (is_array($this->sheets)) {
738 foreach ($this->sheets as $sheet) {
739 $sheetfile = "$this->dir/style/$sheet.css";
740 if (is_readable($sheetfile)) {
358c13cb 741 $css['theme'][$sheet] = $this->post_process("/*** This theme $this->name/style/$sheet ***/\n\n" . file_get_contents($sheetfile));
78946b9b 742 }
ebebf55c 743 }
78946b9b
PS
744 }
745
746 return $css;
747 }
34a2777c 748
e68c5f36
PS
749
750 /**
751 * Get the javascript URL of this theme
04c01408 752 * @param bool $footer true measn footer url, false means head
e68c5f36
PS
753 * @return moodle_url
754 */
04c01408 755 public function javascript_url($footer=false) {
e68c5f36
PS
756 global $CFG;
757
758 $rev = theme_get_revision();
759
760 $params = array('theme'=>$this->name,'rev'=>$rev);
04c01408
PS
761 if ($footer) {
762 $params['type'] = 'footer';
763 }
e68c5f36
PS
764 return new moodle_url($CFG->httpswwwroot.'/theme/javascripts.php', $params);
765 }
766
767 /**
768 * Returns the content of the one huge javascript file merged from all theme javascript files.
04c01408 769 * @param bool $footer true measn footer url, false means head
e68c5f36
PS
770 * @return string
771 */
04c01408
PS
772 public function javascript_content($footer=false) {
773 $type = $footer ? 'javascripts_footer' : 'javascripts';
774
358c13cb
PS
775 $js = array();
776 // find out wanted parent javascripts
777 $excludes = null;
778 if (is_array($this->parents_exclude_javascripts) or $this->parents_exclude_javascripts === true) {
779 $excludes = $this->parents_exclude_javascripts;
780 } else {
781 foreach ($this->parent_configs as $parent_config) { // the immediate parent first, base last
782 if (!isset($parent_config->parents_exclude_javascripts)) {
783 continue;
784 }
785 if (is_array($parent_config->parents_exclude_javascripts) or $parent_config->parents_exclude_javascripts === true) {
786 $excludes = $parent_config->parents_exclude_javascripts;
787 break;
788 }
789 }
790 }
791 if ($excludes !== true) {
792 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
793 $parent = $parent_config->name;
04c01408 794 if (empty($parent_config->$type)) {
358c13cb
PS
795 continue;
796 }
797 if (!empty($excludes[$parent]) and $excludes[$parent] === true) {
798 continue;
799 }
04c01408 800 foreach ($parent_config->$type as $javascript) {
358c13cb
PS
801 if (!empty($excludes[$parent]) and is_array($excludes[$parent])
802 and in_array($javascript, $excludes[$parent])) {
803 continue;
804 }
805 $javascriptfile = "$parent_config->dir/javascript/$javascript.js";
806 if (is_readable($javascriptfile)) {
807 $js[] = "/*** Parent theme $parent/javascript/$javascript.js ***/\n\n" . file_get_contents($javascriptfile);
808 }
809 }
810 }
811 }
812
813 // current theme javascripts
04c01408
PS
814 if (is_array($this->$type)) {
815 foreach ($this->$type as $javascript) {
358c13cb
PS
816 $javascriptfile = "$this->dir/javascript/$javascript.js";
817 if (is_readable($javascriptfile)) {
818 $js[] = "/*** This theme $this->name/javascript/$javascript.js ***/\n\n" . file_get_contents($javascriptfile);
819 }
820 }
821 }
822
823 return implode("\n\n", $js);
e68c5f36
PS
824 }
825
78946b9b 826 protected function post_process($css) {
df06c1c4
PS
827 global $CFG;
828
78946b9b
PS
829 // now resolve all image locations
830 if (preg_match_all('/\[\[pix:([a-z_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) {
831 $replaced = array();
832 foreach ($matches as $match) {
833 if (isset($replaced[$match[0]])) {
834 continue;
835 }
836 $replaced[$match[0]] = true;
837 $imagename = $match[2];
838 $component = rtrim($match[1], '|');
b9bc2019 839 $imageurl = $this->pix_url($imagename, $component)->out(false);
df06c1c4
PS
840 // we do not need full url because the image.php is always in the same dir
841 $imageurl = str_replace("$CFG->httpswwwroot/theme/", '', $imageurl);
842 $css = str_replace($match[0], $imageurl, $css);
ebebf55c 843 }
34a2777c 844 }
17a6649b 845
78946b9b
PS
846 // now resolve all theme settings or do any other postprocessing
847 $csspostprocess = $this->csspostprocess;
848 if (function_exists($csspostprocess)) {
849 $css = $csspostprocess($css, $this);
fdeb7fa1 850 }
851
78946b9b 852 return $css;
ebebf55c 853 }
17a6649b 854
ebebf55c 855 /**
78946b9b
PS
856 * Return the URL for an image
857 *
858 * @param string $imagename the name of the icon.
859 * @param string $component, specification of one plugin like in get_string()
860 * @return moodle_url
fdeb7fa1 861 */
c39e5ba2 862 public function pix_url($imagename, $component) {
fdeb7fa1 863 global $CFG;
78946b9b
PS
864
865 $params = array('theme'=>$this->name, 'image'=>$imagename);
866
867 $rev = theme_get_revision();
868 if ($rev != -1) {
869 $params['rev'] = $rev;
870 }
871 if (!empty($component) and $component !== 'moodle'and $component !== 'core') {
872 $params['component'] = $component;
fdeb7fa1 873 }
78946b9b
PS
874
875 return new moodle_url("$CFG->httpswwwroot/theme/image.php", $params);
fdeb7fa1 876 }
877
878 /**
78946b9b
PS
879 * Resolves the real image location.
880 * @param string $image name of image, may contain relative path
881 * @param string $component
882 * @return string full file path
fdeb7fa1 883 */
78946b9b
PS
884 public function resolve_image_location($image, $component) {
885 global $CFG;
886
887 if ($component === 'moodle' or $component === 'core' or empty($component)) {
888 if ($imagefile = $this->image_exists("$this->dir/pix_core/$image")) {
889 return $imagefile;
890 }
891 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
892 if ($imagefile = $this->image_exists("$parent_config->dir/pix_core/$image")) {
893 return $imagefile;
894 }
895 }
896 if ($imagefile = $this->image_exists("$CFG->dirroot/pix/$image")) {
897 return $imagefile;
898 }
899 return null;
900
901 } else if ($component === 'theme') { //exception
902 if ($image === 'favicon') {
903 return "$this->dir/pix/favicon.ico";
904 }
905 if ($imagefile = $this->image_exists("$this->dir/pix/$image")) {
906 return $imagefile;
907 }
908 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
909 if ($imagefile = $this->image_exists("$parent_config->dir/pix/$image")) {
910 return $imagefile;
911 }
912 }
913 return null;
914
78946b9b
PS
915 } else {
916 if (strpos($component, '_') === false) {
917 $component = 'mod_'.$component;
918 }
919 list($type, $plugin) = explode('_', $component, 2);
920
921 if ($imagefile = $this->image_exists("$this->dir/pix_plugins/$type/$plugin/$image")) {
922 return $imagefile;
923 }
924 foreach (array_reverse($this->parent_configs) as $parent_config) { // base first, the immediate parent last
925 if ($imagefile = $this->image_exists("$parent_config->dir/pix_plugins/$type/$plugin/$image")) {
926 return $imagefile;
927 }
928 }
929 $dir = get_plugin_directory($type, $plugin);
930 if ($imagefile = $this->image_exists("$dir/pix/$image")) {
931 return $imagefile;
932 }
933 return null;
fdeb7fa1 934 }
fdeb7fa1 935 }
936
d4a03c00 937 /**
78946b9b
PS
938 * Checks if file with any image extension exists.
939 * @param string $filepath
940 * @return string image name with extension
d4a03c00 941 */
78946b9b
PS
942 private static function image_exists($filepath) {
943 if (file_exists("$filepath.gif")) {
944 return "$filepath.gif";
945 } else if (file_exists("$filepath.png")) {
946 return "$filepath.png";
947 } else if (file_exists("$filepath.jpg")) {
948 return "$filepath.jpg";
949 } else if (file_exists("$filepath.jpeg")) {
950 return "$filepath.jpeg";
d4a03c00 951 } else {
78946b9b 952 return false;
d4a03c00 953 }
954 }
955
fdeb7fa1 956 /**
78946b9b
PS
957 * Loads the theme config from config.php file.
958 * @param string $themename
959 * @param object $settings from config_plugins table
960 * @return object
fdeb7fa1 961 */
78946b9b
PS
962 private static function find_theme_config($themename, $settings) {
963 // We have to use the variable name $THEME (upper case) because that
964 // is what is used in theme config.php files.
fdeb7fa1 965
78946b9b
PS
966 if (!$dir = theme_config::find_theme_location($themename)) {
967 return null;
fdeb7fa1 968 }
2f67a9b3 969
78946b9b
PS
970 $THEME = new object();
971 $THEME->name = $themename;
972 $THEME->dir = $dir;
973 $THEME->settings = $settings;
974
975 global $CFG; // just in case somebody tries to use $CFG in theme config
976 include("$THEME->dir/config.php");
977
978 // verify the theme configuration is OK
979 if (!is_array($THEME->parents)) {
980 // parents option is mandatory now
981 return null;
fdeb7fa1 982 }
983
78946b9b 984 return $THEME;
fdeb7fa1 985 }
986
d4a03c00 987 /**
78946b9b
PS
988 * Finds the theme location and verifies the theme has all needed files
989 * and is not obsoleted.
990 * @param string $themename
991 * @return string full dir path or null if not found
d4a03c00 992 */
78946b9b
PS
993 private static function find_theme_location($themename) {
994 global $CFG;
995
996 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
997 $dir = "$CFG->dirroot/theme/$themename";
d4a03c00 998
7d875874 999 } else {
78946b9b 1000 return null;
d4a03c00 1001 }
78946b9b
PS
1002
1003 if (file_exists("$dir/styles.php")) {
1004 //legacy theme - needs to be upgraded - upgrade info is displayed on the admin settings page
1005 return null;
1006 }
2f67a9b3 1007
78946b9b 1008 return $dir;
d4a03c00 1009 }
1010
1d13c75c 1011 /**
78946b9b 1012 * Get the renderer for a part of Moodle for this theme.
78946b9b 1013 * @param moodle_page $page the page we are rendering
56cbc53b 1014 * @param string $module the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
78946b9b 1015 * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
c927e35c 1016 * @param string $target one of rendering target constants
78946b9b 1017 * @return renderer_base the requested renderer.
1d13c75c 1018 */
c927e35c 1019 public function get_renderer(moodle_page $page, $component, $subtype = null, $target = null) {
78946b9b 1020 if (is_null($this->rf)) {
c927e35c 1021 $classname = $this->rendererfactory;
78946b9b 1022 $this->rf = new $classname($this);
1d13c75c 1023 }
78946b9b 1024
c927e35c 1025 return $this->rf->get_renderer($page, $component, $subtype, $target);
1d13c75c 1026 }
1027
fdeb7fa1 1028 /**
78946b9b
PS
1029 * Get the information from {@link $layouts} for this type of page.
1030 * @param string $pagelayout the the page layout name.
1031 * @return array the appropriate part of {@link $layouts}.
fdeb7fa1 1032 */
78946b9b
PS
1033 protected function layout_info_for_page($pagelayout) {
1034 if (array_key_exists($pagelayout, $this->layouts)) {
1035 return $this->layouts[$pagelayout];
1036 } else {
191b267b
PS
1037 debugging('Invalid page layout specified: ' . $pagelayout);
1038 return $this->layouts['standard'];
fdeb7fa1 1039 }
1040 }
1041
1042 /**
78946b9b
PS
1043 * Given the settings of this theme, and the page pagelayout, return the
1044 * full path of the page layout file to use.
1045 *
1046 * Used by {@link core_renderer::header()}.
1047 *
1048 * @param string $pagelayout the the page layout name.
1049 * @return string Full path to the lyout file to use
ebebf55c 1050 */
78946b9b 1051 public function layout_file($pagelayout) {
ebebf55c 1052 global $CFG;
fdeb7fa1 1053
78946b9b
PS
1054 $layoutinfo = $this->layout_info_for_page($pagelayout);
1055 $layoutfile = $layoutinfo['file'];
1056 $theme = $layoutinfo['theme'];
fdeb7fa1 1057
78946b9b
PS
1058 if ($dir = $this->find_theme_location($theme)) {
1059 $path = "$dir/layout/$layoutfile";
fdeb7fa1 1060
78946b9b
PS
1061 // Check the template exists, return general base theme template if not.
1062 if (is_readable($path)) {
1063 return $path;
1064 }
34a2777c 1065 }
1066
191b267b 1067 debugging('Can not find layout file for: ' . $pagelayout);
78946b9b
PS
1068 // fallback to standard normal layout
1069 return "$CFG->dirroot/theme/base/layout/general.php";
1070 }
ebebf55c 1071
78946b9b
PS
1072 /**
1073 * Returns auxiliary page layout options specified in layout configuration array.
1074 * @param string $pagelayout
1075 * @return array
1076 */
1077 public function pagelayout_options($pagelayout) {
1078 $info = $this->layout_info_for_page($pagelayout);
1079 if (!empty($info['options'])) {
1080 return $info['options'];
34a2777c 1081 }
78946b9b
PS
1082 return array();
1083 }
34a2777c 1084
78946b9b
PS
1085 /**
1086 * Inform a block_manager about the block regions this theme wants on this
1087 * page layout.
1088 * @param string $pagelayout the general type of the page.
1089 * @param block_manager $blockmanager the block_manger to set up.
1090 * @return void
1091 */
1092 public function setup_blocks($pagelayout, $blockmanager) {
1093 $layoutinfo = $this->layout_info_for_page($pagelayout);
1094 if (!empty($layoutinfo['regions'])) {
1095 $blockmanager->add_regions($layoutinfo['regions']);
1096 $blockmanager->set_default_region($layoutinfo['defaultregion']);
ebebf55c 1097 }
34a2777c 1098 }
1099
cadc0d28
TH
1100 protected function get_region_name($region, $theme) {
1101 $regionstring = get_string('region-' . $region, 'theme_' . $theme);
1102 // A name exists in this theme, so use it
1103 if (substr($regionstring, 0, 1) != '[') {
1104 return $regionstring;
1105 }
1106
1107 // Otherwise, try to find one elsewhere
1108 // Check parents, if any
1109 foreach ($this->parents as $parentthemename) {
1110 $regionstring = get_string('region-' . $region, 'theme_' . $parentthemename);
1111 if (substr($regionstring, 0, 1) != '[') {
1112 return $regionstring;
1113 }
1114 }
1115
1116 // Last resort, try the base theme for names
1117 return get_string('region-' . $region, 'theme_base');
1118 }
1119
ebebf55c 1120 /**
78946b9b
PS
1121 * Get the list of all block regions known to this theme in all templates.
1122 * @return array internal region name => human readable name.
ebebf55c 1123 */
78946b9b
PS
1124 public function get_all_block_regions() {
1125 $regions = array();
78946b9b
PS
1126 foreach ($this->layouts as $layoutinfo) {
1127 foreach ($layoutinfo['regions'] as $region) {
cadc0d28 1128 $regions[$region] = $this->get_region_name($region, $layoutinfo['theme']);
ebebf55c 1129 }
34a2777c 1130 }
78946b9b 1131 return $regions;
34a2777c 1132 }
ebebf55c 1133}
34a2777c 1134
78946b9b 1135
ebebf55c 1136/**
d9c8f425 1137 * This class keeps track of which HTML tags are currently open.
1138 *
1139 * This makes it much easier to always generate well formed XHTML output, even
1140 * if execution terminates abruptly. Any time you output some opening HTML
1141 * without the matching closing HTML, you should push the necessary close tags
1142 * onto the stack.
ebebf55c 1143 *
1144 * @copyright 2009 Tim Hunt
1145 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1146 * @since Moodle 2.0
1147 */
d9c8f425 1148class xhtml_container_stack {
1149 /** @var array stores the list of open containers. */
1150 protected $opencontainers = array();
fa1afe32 1151 /**
d9c8f425 1152 * @var array in developer debug mode, stores a stack trace of all opens and
1153 * closes, so we can output helpful error messages when there is a mismatch.
fa1afe32 1154 */
d9c8f425 1155 protected $log = array();
fa1afe32 1156 /**
d9c8f425 1157 * Store whether we are developer debug mode. We need this in several places
1158 * including in the destructor where we may no thave access to $CFG.
1159 * @var boolean
fa1afe32 1160 */
d9c8f425 1161 protected $isdebugging;
34a2777c 1162
d9c8f425 1163 public function __construct() {
1164 $this->isdebugging = debugging('', DEBUG_DEVELOPER);
ebebf55c 1165 }
34a2777c 1166
fa1afe32 1167 /**
d9c8f425 1168 * Push the close HTML for a recently opened container onto the stack.
1169 * @param string $type The type of container. This is checked when {@link pop()}
1170 * is called and must match, otherwise a developer debug warning is output.
1171 * @param string $closehtml The HTML required to close the container.
1172 * @return void
fa1afe32 1173 */
d9c8f425 1174 public function push($type, $closehtml) {
1175 $container = new stdClass;
1176 $container->type = $type;
1177 $container->closehtml = $closehtml;
1178 if ($this->isdebugging) {
1179 $this->log('Open', $type);
3aaa27f4 1180 }
d9c8f425 1181 array_push($this->opencontainers, $container);
ebebf55c 1182 }
34a2777c 1183
fa1afe32 1184 /**
d9c8f425 1185 * Pop the HTML for the next closing container from the stack. The $type
1186 * must match the type passed when the container was opened, otherwise a
1187 * warning will be output.
1188 * @param string $type The type of container.
1189 * @return string the HTML required to close the container.
fa1afe32 1190 */
d9c8f425 1191 public function pop($type) {
1192 if (empty($this->opencontainers)) {
1193 debugging('<p>There are no more open containers. This suggests there is a nesting problem.</p>' .
1194 $this->output_log(), DEBUG_DEVELOPER);
1195 return;
3aaa27f4 1196 }
ebebf55c 1197
d9c8f425 1198 $container = array_pop($this->opencontainers);
1199 if ($container->type != $type) {
1200 debugging('<p>The type of container to be closed (' . $container->type .
1201 ') does not match the type of the next open container (' . $type .
1202 '). This suggests there is a nesting problem.</p>' .
1203 $this->output_log(), DEBUG_DEVELOPER);
ebebf55c 1204 }
d9c8f425 1205 if ($this->isdebugging) {
1206 $this->log('Close', $type);
e8775320 1207 }
d9c8f425 1208 return $container->closehtml;
ebebf55c 1209 }
e8775320 1210
fa1afe32 1211 /**
d9c8f425 1212 * Close all but the last open container. This is useful in places like error
1213 * handling, where you want to close all the open containers (apart from <body>)
1214 * before outputting the error message.
1215 * @param bool $shouldbenone assert that the stack should be empty now - causes a
1216 * developer debug warning if it isn't.
1217 * @return string the HTML required to close any open containers inside <body>.
fa1afe32 1218 */
d9c8f425 1219 public function pop_all_but_last($shouldbenone = false) {
1220 if ($shouldbenone && count($this->opencontainers) != 1) {
1221 debugging('<p>Some HTML tags were opened in the body of the page but not closed.</p>' .
1222 $this->output_log(), DEBUG_DEVELOPER);
1223 }
1224 $output = '';
1225 while (count($this->opencontainers) > 1) {
1226 $container = array_pop($this->opencontainers);
1227 $output .= $container->closehtml;
e8775320 1228 }
d9c8f425 1229 return $output;
e8775320 1230 }
34a2777c 1231
ebebf55c 1232 /**
d9c8f425 1233 * You can call this function if you want to throw away an instance of this
1234 * class without properly emptying the stack (for example, in a unit test).
1235 * Calling this method stops the destruct method from outputting a developer
1236 * debug warning. After calling this method, the instance can no longer be used.
1237 * @return void
ebebf55c 1238 */
d9c8f425 1239 public function discard() {
1240 $this->opencontainers = null;
ebebf55c 1241 }
d9c8f425 1242
fa1afe32 1243 /**
d9c8f425 1244 * Adds an entry to the log.
1245 * @param string $action The name of the action
1246 * @param string $type The type of action
1247 * @return void
fa1afe32 1248 */
d9c8f425 1249 protected function log($action, $type) {
1250 $this->log[] = '<li>' . $action . ' ' . $type . ' at:' .
1251 format_backtrace(debug_backtrace()) . '</li>';
ebebf55c 1252 }
34a2777c 1253
fa1afe32 1254 /**
d9c8f425 1255 * Outputs the log's contents as a HTML list.
1256 * @return string HTML list of the log
fa1afe32 1257 */
d9c8f425 1258 protected function output_log() {
1259 return '<ul>' . implode("\n", $this->log) . '</ul>';
34a2777c 1260 }
1261}