From d7ab887938e5996bbe6960c1ea54382904abcd11 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Wed, 6 May 2009 08:46:48 +0000 Subject: [PATCH] moodle_page: MDL-14306 (part of MDL-12212) class name on body with ->category If ->allowcategorythemes is set, includes all parent categories too --- admin/settings.php | 2 +- lib/adminlib.php | 2 +- lib/pagelib.php | 44 ++++++++++++++++++++--- lib/simpletest/testpagelib_moodlepage.php | 10 ++++++ 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/admin/settings.php b/admin/settings.php index 22d0c5b097..ae1aefde59 100644 --- a/admin/settings.php +++ b/admin/settings.php @@ -54,8 +54,8 @@ if ($data = data_submitted() and confirm_sesskey()) { /// very hacky page setup page_map_class(PAGE_ADMIN, 'page_admin'); $PAGE = page_create_object(PAGE_ADMIN, 0); // there must be any constant id number -$PAGE->init_extra($section); $PAGE->set_pagetype('admin-setting-' . $section); +$PAGE->init_extra($section); if (!isset($USER->adminediting)) { $USER->adminediting = false; diff --git a/lib/adminlib.php b/lib/adminlib.php index 59fb555809..8b3523fbe4 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -3906,10 +3906,10 @@ function admin_externalpage_setup($section, $extrabutton='', $extraurlparams=arr page_map_class(PAGE_ADMIN, 'page_admin'); $PAGE = page_create_object(PAGE_ADMIN, 0); // there must be any constant id number + $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); $PAGE->init_extra($section); // hack alert! $PAGE->set_extra_button($extrabutton); $PAGE->set_extra_url_params($extraurlparams, $actualurl); - $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); $adminroot = admin_get_root(false, false); // settings not required for external pages $extpage = $adminroot->locate($section); diff --git a/lib/pagelib.php b/lib/pagelib.php index 4995bb3260..35a5c48062 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -220,6 +220,10 @@ class moodle_page { throw new coding_exception('Cannot call moodle_page::set_course after output has been started.'); } + if (!empty($this->_course->id) && $this->_course->id != $course->id) { + $this->_categories = null; + } + $this->_course = clone($course); $COURSE = $this->_course; @@ -227,8 +231,6 @@ class moodle_page { $this->set_context(get_context_instance(CONTEXT_COURSE, $this->_course->id)); } - $this->_categories = null; - moodle_setlocale(); theme_setup(); } @@ -354,6 +356,24 @@ class moodle_page { $this->add_body_class($this->url_to_class_name($CFG->wwwroot)); + if ($CFG->allowcategorythemes) { + $this->ensure_category_loaded(); + foreach ($this->_categories as $catid => $notused) { + $this->add_body_class('category-' . $catid); + } + } else { + $catid = 0; + if (is_array($this->_categories)) { + $catids = array_keys($this->_categories); + $catid = reset($catids); + } else if (!empty($this->_course->category)) { + $catid = $this->_course->category; + } + if ($catid) { + $this->add_body_class('category-' . $catid); + } + } + if (!isloggedin()) { $this->add_body_class('notloggedin'); } @@ -490,7 +510,7 @@ function page_create_instance($instance) { * its numeric ID. Returns a fully constructed page_base subclass you can work with. */ function page_create_object($type, $id = NULL) { - global $CFG, $PAGE; + global $CFG, $PAGE, $SITE; $data = new stdClass; $data->pagetype = $type; @@ -498,9 +518,23 @@ function page_create_object($type, $id = NULL) { $classname = page_map_class($type); $object = new $classname; - $object->init_quick($data); - $object->set_course($PAGE->course); + $course = $PAGE->course; + if ($course->id != $SITE->id) { + $object->set_course($course); + } else { + try { + $category = $PAGE->category; + } catch (coding_exception $e) { + // Was not set before, so no need to try to set it again. + $category = false; + } + if ($category) { + $object->set_category_by_id($category->id); + } else { + $object->set_course($SITE); + } + } //$object->set_pagetype($type); return $object; } diff --git a/lib/simpletest/testpagelib_moodlepage.php b/lib/simpletest/testpagelib_moodlepage.php index cc798a0362..92581cc785 100644 --- a/lib/simpletest/testpagelib_moodlepage.php +++ b/lib/simpletest/testpagelib_moodlepage.php @@ -144,6 +144,16 @@ class moodle_page_test extends UnitTestCase { $this->testpage->set_category_by_id(123); } + public function test_categories_array_empty_for_front_page() { + // Setup fixture + $course = $this->create_a_course(); + $course->category = 0; + $this->testpage->set_context(new stdClass); // Avoid trying to set the context. + $this->testpage->set_course($course); + // Exercise SUT and validate. + $this->assertEqual(array(), $this->testpage->categories); + } + public function test_set_state_normal_path() { $this->assertEqual(moodle_page::STATE_BEFORE_HEADER, $this->testpage->state); -- 2.39.5