From 8f8210b3e83a3e0e24f5206d830612dbd4b0f0e2 Mon Sep 17 00:00:00 2001 From: ikawhero Date: Wed, 2 May 2007 09:35:35 +0000 Subject: [PATCH] 1. Changes to allow different priorities in the theme selection based on CFG->themeorder setting 2. Changes to allow category themes. Theme can now be set for a category which will apply to all sub-categories (unless they specifically have a theme set) and to child courses (course theme and $CFG->themeorder settings permitting). New config setting $CFG->allowcategorythemes must be set. Off by default as it means extra database calls. There is an ugly hack to find out if we are viewing course/category.php - anyone welcome to tidy this up if they have a better idea. GUI for setting category theme coming. --- lib/weblib.php | 110 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 97 insertions(+), 13 deletions(-) diff --git a/lib/weblib.php b/lib/weblib.php index da4924c891..4288634ccd 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2502,31 +2502,115 @@ function print_footer($course=NULL, $usercourse=NULL, $return=false) { * Returns the name of the current theme * * @uses $CFG - * @param $USER - * @param $SESSION + * @uses $USER + * @uses $SESSION + * @uses $COURSE + * @uses $FULLME * @return string */ function current_theme() { - global $CFG, $USER, $SESSION, $COURSE; + global $CFG, $USER, $SESSION, $COURSE, $FULLME; - if (!empty($CFG->pagetheme)) { // Page theme is for special page-only themes set by code - return $CFG->pagetheme; + if (empty($CFG->themeorder)) { + $themeorder = array('page', 'course', 'category', 'session', 'user', 'site'); + } else { + $themeorder = $CFG->themeorder; + } - } else if (!empty($CFG->allowcoursethemes) and !empty($COURSE->theme)) { // Course themes override others - return $COURSE->theme; + $theme = ''; + foreach ($themeorder as $themetype) { - } else if (!empty($SESSION->theme)) { // Session theme can override other settings - return $SESSION->theme; + if (!empty($theme)) continue; + + switch ($themetype) { + case 'page': // Page theme is for special page-only themes set by code + if (!empty($CFG->pagetheme)) { + $theme = $CFG->pagetheme; + } + break; + case 'course': + if (!empty($CFG->allowcoursethemes) and !empty($COURSE->theme)) { + $theme = $COURSE->theme; + } + break; + case 'category': + if (!empty($CFG->allowcategorythemes)) { + /// Nasty hack to check if we're in a category page + if (stripos($FULLME, 'course/category.php') !== false) { + global $id; + if (!empty($id)) { + $theme = current_category_theme($id); + } + /// Otherwise check if we're in a course that has a category theme set + } else if (!empty($COURSE->category)) { + $theme = current_category_theme($COURSE->category); + } + } + break; + case 'session': + if (!empty($SESSION->theme)) { + $theme = $SESSION->theme; + } + break; + case 'user': + if (!empty($CFG->allowuserthemes) and !empty($USER->theme)) { + $theme = $USER->theme; + } + break; + case 'site': + $theme = $CFG->theme; + break; + default: + /// do nothing + } + } - } else if (!empty($CFG->allowuserthemes) and !empty($USER->theme)) { // User theme can override site theme - return $USER->theme; +/// A final check in case 'site' was not included in $CFG->themeorder + if (empty($theme)) { + $theme = $CFG->theme; + } + return $theme; +} + +/** + * Retrieves the category theme if one exists, otherwise checks the parent categories. + * Recursive function. + * + * @uses $COURSE + * @param integer $categoryid id of the category to check + * @return string theme name + */ +function current_category_theme($categoryid=0) { + global $COURSE; + +/// Use the COURSE global if the categoryid not set + if (empty($categoryid)) { + if (!empty($COURSE->category)) { + $categoryid = $COURSE->category; + } else { + return false; + } + } + +/// Retrieve the current category + if ($category = get_record('course_categories', 'id', $categoryid)) { + + /// Return the category theme if it exists + if (!empty($category->theme)) { + return $category->theme; + + /// Otherwise try the parent category if one exists + } else if (!empty($category->parent)) { + return current_category_theme($category->parent); + } + +/// Return false if we can't find the category record } else { - return $CFG->theme; + return false; } } - /** * This function is called by stylesheets to set up the header * approriately as well as the current path -- 2.39.5