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
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.
$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;
$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.
*/
}
}
+/// 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'.
}
}
-/// 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.
*/
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;
$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');