moodle_page: MDL-14306 (part of MDL-12212) class name on body with ->category
authortjhunt <tjhunt>
Wed, 6 May 2009 08:46:48 +0000 (08:46 +0000)
committertjhunt <tjhunt>
Wed, 6 May 2009 08:46:48 +0000 (08:46 +0000)
If ->allowcategorythemes is set, includes all parent categories too

admin/settings.php
lib/adminlib.php
lib/pagelib.php
lib/simpletest/testpagelib_moodlepage.php

index 22d0c5b097382fd1f64b6c6c93f9645c9882597e..ae1aefde5942e1b2ab786ebdceff9f5f8bf8afd4 100644 (file)
@@ -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;
index 59fb555809d71858b762ac884d69cbb6bb201d2f..8b3523fbe438b614e8a242903fc3d0db41730ac8 100644 (file)
@@ -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);
index 4995bb326044998b55180464bf1e39072d4267db..35a5c480620cc59580ca39eda6cc714fd0532e44 100644 (file)
@@ -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;
 }
index cc798a0362a691f99d1927c8640a4d0a1d31d240..92581cc78585a15f0606ece6c24a395efe8ac564 100644 (file)
@@ -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);