Skip to content

Commit

Permalink
themes: MDL-19077 change how the theme is initialised and CSS is served.
Browse files Browse the repository at this point in the history
This is part of http://docs.moodle.org/en/Development:Theme_engines_for_Moodle%3F

$THEME is now initialised at the same time as $OUTPUT. Old functions like
theme_setup are deprecated in favour of methods on $PAGE. There is a new
theme_config class in outputlib.php that deals with loading the theme config.php file.

CSS used to be served by themes styles.php files calling a function in weblib.php.
Now it works by each theme's styles.php file doing
$themename = basename(dirname(__FILE__));
require_once(dirname(__FILE__) . '/../../theme/styles.php');
which is less code to be copied into each theme. (Old-style styles.php files still
work thanks to some code in deprecatedlib.php.)

Admin UI for choosing a theme cleaned up.

A couple of theme-specific hard-coded hacks like $THEME->cssconstants and
$THEME->CSSEdit have been replaced by a more generic $THEME->customcssoutputfunction
hook. See examples at the end of outputlib.php

Also:
* Fix setting the theme in the URL, which seems to have been broken since 1.9.
* Fix up errors on a few pages caused by the new initialisation order.
* MDL-19097 moodle_page::set_course should not set $COURSE unless it is $PAGE.
* httpsrequired() from moodlelib.php moved to $PAGE->https_required().
* Move has_started() method to the renderer base class.
* Further fixes to display of early errors.
* Remove print_header/footer_old from weblib. I did not mean to commit them before.
  • Loading branch information
tjhunt committed Jul 1, 2009
1 parent 0456fc1 commit b700947
Show file tree
Hide file tree
Showing 37 changed files with 1,651 additions and 1,536 deletions.
1 change: 1 addition & 0 deletions admin/settings/appearance.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
$temp->add(new admin_setting_configcheckbox('allowuserthemes', get_string('allowuserthemes', 'admin'), get_string('configallowuserthemes', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('allowcoursethemes', get_string('allowcoursethemes', 'admin'), get_string('configallowcoursethemes', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('allowcategorythemes', get_string('allowcategorythemes', 'admin'), get_string('configallowcategorythemes', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('allowthemechangeonurl', get_string('allowthemechangeonurl', 'admin'), get_string('configallowthemechangeonurl', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('allowuserblockhiding', get_string('allowuserblockhiding', 'admin'), get_string('configallowuserblockhiding', 'admin'), 1));
$temp->add(new admin_setting_configcheckbox('showblocksonmodpages', get_string('showblocksonmodpages', 'admin'), get_string('configshowblocksonmodpages', 'admin'), 0));
$temp->add(new admin_setting_configselect('hideactivitytypenavlink', get_string('hideactivitytypenavlink', 'admin'), get_string('confighideactivitytypenavlink', 'admin'), 0,
Expand Down
4 changes: 2 additions & 2 deletions config-dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@
// Set the priority of themes from highest to lowest. This is useful (for
// example) in sites where the user theme should override all other theme
// settings for accessibility reasons. You can also disable types of themes
// by removing them from the array. The default setting is:
// $CFG->themeorder = array('page', 'course', 'category', 'session', 'user', 'site');
// (other than site) by removing them from the array. The default setting is:
// $CFG->themeorder = array('course', 'category', 'session', 'user', 'site');
// NOTE: course, category, session, user themes still require the
// respective settings to be enabled
//
Expand Down
5 changes: 0 additions & 5 deletions course/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@
}
}

if(!empty($CFG->allowcategorythemes) && isset($category->theme)) {
// specifying theme here saves us some dbqs
theme_setup($category->theme);
}

/// Print headings
$numcategories = $DB->count_records('course_categories');

Expand Down
2 changes: 2 additions & 0 deletions lang/en_utf8/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
$string['allowemailaddresses'] = 'Allowed email domains';
$string['allowobjectembed'] = 'Allow EMBED and OBJECT tags';
$string['allowrenames'] = 'Allow renames';
$string['allowthemechangeonurl'] = 'Allow theme changes in the URL';
$string['allowuserblockhiding'] = 'Allow users to hide blocks';
$string['allowusermailcharset'] = 'Allow user to select character set';
$string['allowuserswitchrolestheycantassign'] = 'Allow users without the assign roles capability to switch roles';
Expand Down Expand Up @@ -82,6 +83,7 @@
$string['configallowoverride'] = 'You can allow people with the roles on the left side to override some of the column roles';
$string['configallowoverride2'] = 'Select which role(s) can be overridden by each role in the left column.<br />Note that these settings only apply to users who have either the capability moodle/role:override or the capability moodle/role:safeoverride allowed.';
$string['configallowswitch'] = 'Select which roles a user may switch to, based on which roles they already have. In addition to an entry in this table, a user must also have the moodle/role:switchroles capability to be able to switch.<br />Note that it is only possible to switch to roles that have the moodle/course:view capability, and that do not have the moodle/site:doanything capability, so some columns in this table are disabled.';
$string['configallowthemechangeonurl'] = 'If you turn this setting on, then the theme can by changed by adding theme={themename}&amp;sesskey={sesskey} to any Moodle URL.';
$string['configallowunenroll'] = 'If this is set \'Yes\', then students are allowed to unenrol themselves from courses whenever they like. Otherwise they are not allowed, and this process will be solely controlled by the teachers and administrators.';
$string['configallowuserblockhiding'] = 'Do you want to allow users to hide/show side blocks throughout this site? This feature uses Javascript and cookies to remember the state of each collapsible block, and only affects the user\'s own view.';
$string['configallowusermailcharset'] = 'Enabling this, every user in the site will be able to specify his own charset for email.';
Expand Down
14 changes: 7 additions & 7 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5205,9 +5205,6 @@ function admin_get_root($reload=false, $requirefulltree=true) {
$ADMIN->purge_children($requirefulltree);
}

// Some parts of the tree require $CFG->pixpath.
$OUTPUT->initialise_deprecated_cfg_pixpath();

if (!$ADMIN->loaded) {
// we process this file first to create categories first and in correct order
require($CFG->dirroot.'/'.$CFG->admin.'/settings/top.php');
Expand Down Expand Up @@ -5275,7 +5272,7 @@ function admin_apply_default_settings($node=NULL, $unconditional=true) {
* @return int number of changed settings
*/
function admin_write_settings($formdata) {
global $CFG, $SITE, $PAGE, $DB;
global $CFG, $SITE, $DB;

$olddbsessions = !empty($CFG->dbsessions);
$formdata = (array)$formdata;
Expand Down Expand Up @@ -5314,9 +5311,12 @@ function admin_write_settings($formdata) {
require_logout();
}

// now update $SITE - it might have been changed
$SITE = $DB->get_record('course', array('id'=>$SITE->id));
$PAGE->set_course($SITE);
// Now update $SITE - just update the fields, in case other people have a
// a reference to it (e.g. $PAGE, $COURSE).
$newsite = $DB->get_record('course', array('id'=>$SITE->id));
foreach (get_object_vars($newsite) as $field => $value) {
$SITE->$field = $value;
}

// now reload all settings - some of them might depend on the changed
admin_get_root(true);
Expand Down
4 changes: 4 additions & 0 deletions lib/blocklib.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ public function load_blocks($includeinvisible = NULL) {
return;
}

if (!isset($this->defaultregion)) {
$this->page->initialise_theme_and_output();
}

if (is_null($includeinvisible)) {
$includeinvisible = $this->page->user_is_editing();
}
Expand Down
102 changes: 0 additions & 102 deletions lib/cssconstants.php

This file was deleted.

49 changes: 49 additions & 0 deletions lib/deprecatedlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,55 @@ public function __construct($theme, $page) {
}


/**
* Used to be used for setting up the theme. No longer used by core code, and
* should not have been used elsewhere.
*
* The theme is now automatically initialised before it is first used. If you really need
* to force this to happen, just reference $PAGE->theme.
*
* To force a particular theme on a particular page, you can use $PAGE->force_theme(...).
* However, I can't think of any valid reason to do that outside the theme selector UI.
*
* @deprecated
* @param string $theme The theme to use defaults to current theme
* @param array $params An array of parameters to use
*/
function theme_setup($theme = '', $params=NULL) {
throw new coding_exception('The function theme_setup is no longer required, and should no longer be used. ' .
'The current theme gets initialised automatically before it is first used.');
}

/**
* @deprecated use $PAGE->theme->name instead.
* @return string the name of the current theme.
*/
function current_theme() {
global $PAGE;
// TODO, uncomment this once we have eliminated all references to current_theme in core code.
// debugging('current_theme is deprecated, use $PAGE->theme->name instead', DEBUG_DEVELOPER);
return $PAGE->theme->name;
}

/**
* This used to be the thing that theme styles.php files used to do all the work.
* This is now handled differently. You should copy theme/standard/styes.php
* into your theme.
*
* @deprecated
* @param int $lastmodified Always gets set to now
* @param int $lifetime The max-age header setting (seconds) defaults to 300
* @param string $themename The name of the theme to use (optional) defaults to current theme
* @param string $forceconfig Force a particular theme config (optional)
* @param string $lang Load styles for the specified language (optional)
*/
function style_sheet_setup($lastmodified=0, $lifetime=300, $themename='', $forceconfig='', $lang='') {
global $CFG, $PAGE, $THEME, $showdeprecatedstylesheetsetupwarning;
$showdeprecatedstylesheetsetupwarning = true;
include($CFG->dirroot . '/theme/styles.php');
exit;
}

/**
* Prints some red text using echo
*
Expand Down
20 changes: 2 additions & 18 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -8208,26 +8208,10 @@ function address_in_subnet($addr, $subnetstr) {
*
* By using this function properly, we can ensure 100% https-ized pages
* at our entire discretion (login, forgot_password, change_password)
*
* @global object
* @global bool
*/
function httpsrequired() {

global $CFG, $HTTPSPAGEREQUIRED;

if (!empty($CFG->loginhttps)) {
$HTTPSPAGEREQUIRED = true;
$CFG->httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
$CFG->httpsthemewww = str_replace('http:', 'https:', $CFG->themewww);

// change theme URLs to https
theme_setup();

} else {
$CFG->httpswwwroot = $CFG->wwwroot;
$CFG->httpsthemewww = $CFG->themewww;
}
global $PAGE;
$PAGE->https_required();
}

/**
Expand Down
Loading

0 comments on commit b700947

Please sign in to comment.