From 753debd2abebf9e4529bd25f932339ff40f39899 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Wed, 6 May 2009 08:42:13 +0000 Subject: [PATCH] moodle_page: MDL-12212 ->bodyclasses and initialise_standard_body_classes --- lib/pagelib.php | 80 +++++++++++++++++++---- lib/simpletest/testpagelib_moodlepage.php | 20 ++++++ lib/weblib.php | 24 +------ 3 files changed, 89 insertions(+), 35 deletions(-) diff --git a/lib/pagelib.php b/lib/pagelib.php index 05bf0e1e3d..b74b6eadc3 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -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; diff --git a/lib/simpletest/testpagelib_moodlepage.php b/lib/simpletest/testpagelib_moodlepage.php index 17f2636769..34e5259f53 100644 --- a/lib/simpletest/testpagelib_moodlepage.php +++ b/lib/simpletest/testpagelib_moodlepage.php @@ -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); + } } /** diff --git a/lib/weblib.php b/lib/weblib.php index cac8859079..adfb9095ec 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2326,30 +2326,8 @@ function print_header ($title='', $heading='', $navigation='', $focus='', $title = str_replace('"', '"', $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'); -- 2.39.5