]> git.mjollnir.org Git - moodle.git/commitdiff
moodle_page: MDL-12212 ->bodyclasses and initialise_standard_body_classes
authortjhunt <tjhunt>
Wed, 6 May 2009 08:42:13 +0000 (08:42 +0000)
committertjhunt <tjhunt>
Wed, 6 May 2009 08:42:13 +0000 (08:42 +0000)
lib/pagelib.php
lib/simpletest/testpagelib_moodlepage.php
lib/weblib.php

index 05bf0e1e3d50b32ea9cfa828cbc6540c2cdcb9ad..b74b6eadc3fe9c8c78660d1a31ceabdfedf3c5df 100644 (file)
@@ -48,16 +48,24 @@ class moodle_page {
     const STATE_DONE = 4;
     /**#@-*/
 
+/// Field declarations =========================================================
+
     protected $_state = self::STATE_BEFORE_HEADER;
 
     protected $_course = null;
 
     protected $_context = null;
 
+    protected $_bodyclasses = array();
+
     protected $_pagetype = null;
 
     protected $_legacyclass = null;
 
+/// Getter methods =============================================================
+/// Due to the __get magic below, you normally do not call these as get_x methods,
+/// but instead use the $PAGE->x syntax.
+
     /**
      * @return integer one of the STATE_... constants. You should not normally need
      * to use this in your code. It is indended for internal use by this class
@@ -110,6 +118,15 @@ class moodle_page {
         return $this->_pagetype;
     }
 
+    /**
+     * @return string the class names to put on the body element in the HTML.
+     */
+    public function get_bodyclasses() {
+        return implode(' ', array_keys($this->_bodyclasses));
+    }
+
+/// Setter methods =============================================================
+
     /**
      * Set the state. The state must be one of that STATE_... constants, and
      * the state is only allowed to advance one step at a time.
@@ -121,9 +138,13 @@ class moodle_page {
                     $this->_state . ' and state ' . $state . ' was requestsed.');
         }
 
-        if ($state == self::STATE_PRINTING_HEADER && !$this->_course) {
-            global $SITE;
-            $this->set_course($SITE);
+        if ($state == self::STATE_PRINTING_HEADER) {
+            if (!$this->_course) {
+                global $SITE;
+                $this->set_course($SITE);
+            }
+
+            $this->initialise_standard_body_classes();
         }
 
         $this->_state = $state;
@@ -183,6 +204,16 @@ class moodle_page {
         $this->_pagetype = $pagetype;
     }
 
+    /**
+     * @param string $class add this class name ot the class attribute on the body tag.
+     */
+    public function add_body_class($class) {
+        if ($this->_state > self::STATE_BEFORE_HEADER) {
+            throw new coding_exception('Cannot call moodle_page::add_body_class after output has been started.');
+        }
+        $this->_bodyclasses[$class] = 1;
+    }
+
     /**
      * PHP overloading magic to make the $PAGE->course syntax work.
      */
@@ -195,6 +226,9 @@ class moodle_page {
         }
     }
 
+/// Initialisation methods =====================================================
+/// These set various things up in a default way.
+
     /**
      * Sets ->pagetype from the script name. For example, if the script that was
      * run is mod/quiz/view.php, ->pagetype will be set to 'mod-quiz-view'.
@@ -233,7 +267,36 @@ class moodle_page {
         }
     }
 
-/// Deperecated fields and methods for backwards compatibility =================
+    protected function initialise_standard_body_classes() {
+        $pagetype = $this->pagetype;
+        if ($pagetype == 'site-index') {
+            $this->_legacyclass = 'course';
+        } else if (substr($pagetype, 0, 6) == 'admin-') {
+            $this->_legacyclass = 'admin';
+        } else {
+            $this->_legacyclass = substr($pagetype, 0, strrpos($pagetype, '-'));
+        }
+        $this->add_body_class($this->_legacyclass);
+
+        $this->add_body_class('course-' . $this->_course->id);
+        $this->add_body_class(get_browser_version_classes());
+        $this->add_body_class('dir-' . get_string('thisdirection'));
+        $this->add_body_class('lang-' . current_language());
+
+        if (!isloggedin()) {
+            $this->add_body_class('notloggedin');
+        }
+
+        if (!empty($USER->editing)) {
+            $this->add_body_class('editing');
+        }
+
+        if (!empty($CFG->blocksdrag)) {
+            $this->add_body_class('drag');
+        }
+    }
+
+/// Deprecated fields and methods for backwards compatibility ==================
 
     /**
      * @deprecated since Moodle 2.0 - use $PAGE->pagetype instead.
@@ -267,14 +330,7 @@ class moodle_page {
      */
     public function get_legacyclass() {
         if (is_null($this->_legacyclass)) {
-            $pagetype = $this->pagetype;
-            if ($pagetype == 'site-index') {
-                $this->_legacyclass = 'course';
-            } else if (substr($pagetype, 0, 6) == 'admin-') {
-                $this->_legacyclass = 'admin';
-            } else {
-                $this->_legacyclass = substr($pagetype, 0, strrpos($pagetype, '-'));
-            }
+            $this->initialise_standard_body_classes();
         }
         debugging('Call to deprecated method moodle_page::get_legacyclass.');
         return $this->_legacyclass;
index 17f26367692edb84f60a5cf2e7808c9eb22419c7..34e5259f5383c3a5445d633883674c8fe51f3f82 100644 (file)
@@ -198,6 +198,26 @@ class moodle_page_test extends UnitTestCase {
         // Validate
         $this->assertEqual('site-index', $this->testpage->pagetype);
     }
+
+    public function test_get_body_classes_empty() {
+        // Validate
+        $this->assertEqual('', $this->testpage->bodyclasses);
+    }
+
+    public function test_get_body_classes_single() {
+        // Exercise SUT
+        $this->testpage->add_body_class('aclassname');
+        // Validate
+        $this->assertEqual('aclassname', $this->testpage->bodyclasses);
+    }
+
+    public function test_get_body_classes_double() {
+        // Exercise SUT
+        $this->testpage->add_body_class('aclassname');
+        $this->testpage->add_body_class('anotherclassname');
+        // Validate
+        $this->assertEqual('aclassname anotherclassname', $this->testpage->bodyclasses);
+    }
 }
 
 /**
index cac8859079919c6bd00da88dc22cbb75e115395f..adfb9095ec90bbb5d0e5be5409a9edd30fff8bc7 100644 (file)
@@ -2326,30 +2326,8 @@ function print_header ($title='', $heading='', $navigation='', $focus='',
     $title = str_replace('"', '&quot;', $title);
 
     // Create class and id for this page
-
     $pageid = $PAGE->pagetype;
-    $pageclass = $PAGE->legacyclass;
-
-    $pageclass .= ' course-'.$COURSE->id;
-
-    if (!isloggedin()) {
-        $pageclass .= ' notloggedin';
-    }
-
-    if (!empty($USER->editing)) {
-        $pageclass .= ' editing';
-    }
-
-    if (!empty($CFG->blocksdrag)) {
-        $pageclass .= ' drag';
-    }
-
-    $pageclass .= ' ' . get_browser_version_classes();
-
-    $pageclass .= ' dir-'.get_string('thisdirection');
-
-    $pageclass .= ' lang-'.$currentlanguage;
-
+    $pageclass = $PAGE->bodyclasses;
     $bodytags .= ' class="'.$pageclass.'" id="'.$pageid.'"';
 
     require_once($CFG->libdir .'/editor/htmlEditor.class.php');