moodle_page: MDL-12212 ->bodyclasses and initialise_standard_body_classes

This commit is contained in:
tjhunt
2009-05-06 08:42:13 +00:00
parent e88462a055
commit 753debd2ab
3 changed files with 89 additions and 35 deletions
+68 -12
View 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;
+20
View 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);
}
}
/**
+1 -23
View File
@@ -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');