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