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