2009-04-15 03:24:25 +00:00
|
|
|
<?php // $Id$
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// //
|
|
|
|
// NOTICE OF COPYRIGHT //
|
|
|
|
// //
|
|
|
|
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
|
|
|
|
// http://moodle.org //
|
|
|
|
// //
|
|
|
|
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// This program is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License for more details: //
|
|
|
|
// //
|
|
|
|
// http://www.gnu.org/copyleft/gpl.html //
|
|
|
|
// //
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2004-11-08 19:36:07 +00:00
|
|
|
|
2004-11-10 16:43:57 +00:00
|
|
|
/**
|
2009-05-06 08:27:12 +00:00
|
|
|
* This file contains the moodle_page class. There is normally a single instance
|
|
|
|
* of this class in the $PAGE global variable. This class is a central reporitory
|
|
|
|
* of information about the page we are building up to send back to the user.
|
2004-11-10 16:43:57 +00:00
|
|
|
*
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
* @package pages
|
|
|
|
*/
|
|
|
|
|
2009-05-06 08:29:22 +00:00
|
|
|
/**
|
|
|
|
* $PAGE is a central store of information about the current page we are
|
|
|
|
* generating in response to the user's request. It does not do very much itself
|
|
|
|
* except keep track of information, however, it serves as the access point to
|
|
|
|
* some more significant components like $PAGE->theme, $PAGE->requires,
|
|
|
|
* $PAGE->blocks, etc.
|
|
|
|
*/
|
|
|
|
class moodle_page {
|
|
|
|
/**#@+ Tracks the where we are in the generation of the page. */
|
|
|
|
const STATE_BEFORE_HEADER = 0;
|
|
|
|
const STATE_PRINTING_HEADER = 1;
|
|
|
|
const STATE_IN_BODY = 2;
|
|
|
|
const STATE_PRINTING_FOOTER = 3;
|
|
|
|
const STATE_DONE = 4;
|
|
|
|
/**#@-*/
|
|
|
|
|
2009-05-06 08:42:13 +00:00
|
|
|
/// Field declarations =========================================================
|
|
|
|
|
2009-05-06 08:29:22 +00:00
|
|
|
protected $_state = self::STATE_BEFORE_HEADER;
|
|
|
|
|
|
|
|
protected $_course = null;
|
|
|
|
|
2009-05-06 08:30:25 +00:00
|
|
|
protected $_context = null;
|
|
|
|
|
2009-05-06 08:46:05 +00:00
|
|
|
/**
|
|
|
|
* This holds any categories that $_course belongs to, starting with the
|
|
|
|
* particular category it belongs to, and working out through any parent
|
|
|
|
* categories to the top level. These are loaded progressively, if neaded.
|
|
|
|
* There are three states. $_categories = null initially when nothing is
|
|
|
|
* loaded; $_categories = array($id => $cat, $parentid => null) when we have
|
|
|
|
* loaded $_course->category, but not any parents; and a complete array once
|
|
|
|
* everything is loaded.
|
|
|
|
*/
|
|
|
|
protected $_categories = null;
|
|
|
|
|
2009-05-06 08:42:13 +00:00
|
|
|
protected $_bodyclasses = array();
|
|
|
|
|
2009-05-06 08:34:32 +00:00
|
|
|
protected $_pagetype = null;
|
|
|
|
|
2009-05-06 08:38:55 +00:00
|
|
|
protected $_legacyclass = null;
|
|
|
|
|
2009-05-06 08:42:13 +00:00
|
|
|
/// Getter methods =============================================================
|
2009-05-06 08:43:51 +00:00
|
|
|
/// Due to the __get magic below, you normally do not call these as $PAGE->get_x
|
|
|
|
/// methods, but instead use the $PAGE->x syntax.
|
2009-05-06 08:42:13 +00:00
|
|
|
|
2009-05-06 08:29:22 +00:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
* and its friends like print_header, to check that everything is working as
|
|
|
|
* expected. Also accessible as $PAGE->state.
|
|
|
|
*/
|
|
|
|
public function get_state() {
|
|
|
|
return $this->_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean has the header already been printed? Also accessible as
|
|
|
|
* $PAGE->headerprinted.
|
|
|
|
*/
|
|
|
|
public function get_headerprinted() {
|
|
|
|
return $this->_state >= self::STATE_IN_BODY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return object the current course that we are inside - a row from the
|
|
|
|
* course table. (Also available as $COURSE global.) If we are not inside
|
|
|
|
* an actual course, this will be the site course. You can also access this
|
|
|
|
* as $PAGE->course.
|
|
|
|
*/
|
|
|
|
public function get_course() {
|
|
|
|
global $SITE;
|
|
|
|
if (is_null($this->_course)) {
|
|
|
|
return $SITE;
|
|
|
|
}
|
|
|
|
return $this->_course;
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:46:05 +00:00
|
|
|
/**
|
|
|
|
* @return mixed the category that the page course belongs to. If there isn't one
|
|
|
|
* (that is, if this is the front page course) returns null.
|
|
|
|
*/
|
|
|
|
public function get_category() {
|
|
|
|
$this->ensure_category_loaded();
|
|
|
|
if (!empty($this->_categories)) {
|
|
|
|
return reset($this->_categories);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array an array of all the categories the page course belongs to,
|
|
|
|
* starting with the immediately containing category, and working out to
|
|
|
|
* the top-level category. This may be the empty array if we are in the
|
|
|
|
* front page course.
|
|
|
|
*/
|
|
|
|
public function get_categories() {
|
|
|
|
$this->ensure_categories_loaded();
|
|
|
|
return $this->_categories;
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:30:25 +00:00
|
|
|
/**
|
|
|
|
* @return object the main context to which this page belongs.
|
|
|
|
*/
|
|
|
|
public function get_context() {
|
|
|
|
if (is_null($this->_context)) {
|
|
|
|
throw new coding_exception('$PAGE->context accessed before it was known.');
|
|
|
|
}
|
|
|
|
return $this->_context;
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:34:32 +00:00
|
|
|
/**
|
|
|
|
* @return string e.g. 'my-index' or 'mod-quiz-attempt'. Same as the id attribute on <body>.
|
|
|
|
*/
|
|
|
|
public function get_pagetype() {
|
2009-05-06 08:38:55 +00:00
|
|
|
if (is_null($this->_pagetype) || isset($CFG->pagepath)) {
|
|
|
|
$this->initialise_default_pagetype();
|
2009-05-06 08:34:32 +00:00
|
|
|
}
|
|
|
|
return $this->_pagetype;
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:42:13 +00:00
|
|
|
/**
|
|
|
|
* @return string the class names to put on the body element in the HTML.
|
|
|
|
*/
|
|
|
|
public function get_bodyclasses() {
|
|
|
|
return implode(' ', array_keys($this->_bodyclasses));
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:44:58 +00:00
|
|
|
/**
|
|
|
|
* PHP overloading magic to make the $PAGE->course syntax work.
|
|
|
|
*/
|
|
|
|
public function __get($field) {
|
|
|
|
$getmethod = 'get_' . $field;
|
|
|
|
if (method_exists($this, $getmethod)) {
|
|
|
|
return $this->$getmethod();
|
|
|
|
} else {
|
|
|
|
throw new coding_exception('Unknown field ' . $field . ' of $PAGE.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:42:13 +00:00
|
|
|
/// Setter methods =============================================================
|
|
|
|
|
2009-05-06 08:29:22 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @param integer $state the new state.
|
|
|
|
*/
|
|
|
|
public function set_state($state) {
|
|
|
|
if ($state != $this->_state + 1 || $state > self::STATE_DONE) {
|
|
|
|
throw new coding_exception('Invalid state passed to moodle_page::set_state. We are in state ' .
|
|
|
|
$this->_state . ' and state ' . $state . ' was requestsed.');
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:42:13 +00:00
|
|
|
if ($state == self::STATE_PRINTING_HEADER) {
|
|
|
|
if (!$this->_course) {
|
|
|
|
global $SITE;
|
|
|
|
$this->set_course($SITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->initialise_standard_body_classes();
|
2009-05-06 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_state = $state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current course. This sets both $PAGE->course and $COURSE. It also
|
|
|
|
* sets the right theme and locale.
|
|
|
|
*
|
|
|
|
* Normally you don't need to call this function yourself, require_login will
|
|
|
|
* call it for you if you pass a $course to it. You can use this function
|
|
|
|
* on pages that do need to call require_login().
|
|
|
|
*
|
2009-05-06 08:30:25 +00:00
|
|
|
* Sets $PAGE->context to the course context, if it is not already set.
|
|
|
|
*
|
2009-05-06 08:29:22 +00:00
|
|
|
* @param object the course to set as the global course.
|
|
|
|
*/
|
|
|
|
public function set_course($course) {
|
2009-05-06 08:46:05 +00:00
|
|
|
global $COURSE;
|
2009-05-06 08:29:22 +00:00
|
|
|
|
|
|
|
if (empty($course->id)) {
|
|
|
|
throw new coding_exception('$course passed to moodle_page::set_course does not look like a proper course object.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->_state > self::STATE_BEFORE_HEADER) {
|
|
|
|
throw new coding_exception('Cannot call moodle_page::set_course after output has been started.');
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:46:48 +00:00
|
|
|
if (!empty($this->_course->id) && $this->_course->id != $course->id) {
|
|
|
|
$this->_categories = null;
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:29:22 +00:00
|
|
|
$this->_course = clone($course);
|
|
|
|
$COURSE = $this->_course;
|
|
|
|
|
2009-05-06 08:30:25 +00:00
|
|
|
if (!$this->_context) {
|
|
|
|
$this->set_context(get_context_instance(CONTEXT_COURSE, $this->_course->id));
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:29:22 +00:00
|
|
|
moodle_setlocale();
|
|
|
|
theme_setup();
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:30:25 +00:00
|
|
|
/**
|
|
|
|
* Set the main context to which this page belongs.
|
|
|
|
* @param object $context a context object, normally obtained with get_context_instance.
|
|
|
|
*/
|
|
|
|
public function set_context($context) {
|
|
|
|
$this->_context = $context;
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:34:32 +00:00
|
|
|
/**
|
|
|
|
* @param string $pagetype e.g. 'my-index' or 'mod-quiz-attempt'. Normally
|
|
|
|
* you do not need to set this manually, it is automatically created from the
|
|
|
|
* script name. However, on some pages this is overridden. For example, the
|
|
|
|
* page type for coures/view.php includes the course format, for example
|
|
|
|
* 'coures-view-weeks'. This gets used as the id attribute on <body> and
|
|
|
|
* also for determining which blocks are displayed.
|
|
|
|
*/
|
|
|
|
public function set_pagetype($pagetype) {
|
|
|
|
$this->_pagetype = $pagetype;
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:42:13 +00:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:29:22 +00:00
|
|
|
/**
|
2009-05-06 08:44:58 +00:00
|
|
|
* @param array $classes this utility method calls add_body_class for each array element.
|
2009-05-06 08:29:22 +00:00
|
|
|
*/
|
2009-05-06 08:44:58 +00:00
|
|
|
public function add_body_classes($classes) {
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
$this->add_body_class($class);
|
2009-05-06 08:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-06 08:34:32 +00:00
|
|
|
|
2009-05-06 08:46:05 +00:00
|
|
|
/**
|
|
|
|
* Set the course category this page belongs to manually. This automatically
|
|
|
|
* sets $PAGE->course to be the site coures. You cannot use this method if
|
|
|
|
* you have already set $PAGE->course - in that case, the category must be
|
|
|
|
* the one that the course belongs to. This also automatically sets the
|
|
|
|
* page context to the category context.
|
|
|
|
* @param integer $categoryid The id of the category to set.
|
|
|
|
*/
|
|
|
|
public function set_category_by_id($categoryid) {
|
|
|
|
global $SITE, $DB;
|
|
|
|
if (!is_null($this->_course)) {
|
|
|
|
throw new coding_exception('Attempt to manually set the course category when the course has been set. This is not allowed.');
|
|
|
|
}
|
|
|
|
if (is_array($this->_categories)) {
|
|
|
|
throw new coding_exception('Course category has already been set. You are not allowed to change it.');
|
|
|
|
}
|
|
|
|
$this->set_course($SITE);
|
|
|
|
$this->load_category($categoryid);
|
|
|
|
$this->set_context(get_context_instance(CONTEXT_COURSECAT, $categoryid));
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:42:13 +00:00
|
|
|
/// Initialisation methods =====================================================
|
|
|
|
/// These set various things up in a default way.
|
|
|
|
|
2009-05-06 08:38:55 +00:00
|
|
|
/**
|
|
|
|
* 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'.
|
|
|
|
* @param string $script the path to the script that should be used to
|
|
|
|
* initialise ->pagetype. If not passed the $SCRIPT global will be used.
|
|
|
|
* If legacy code has set $CFG->pagepath that will be used instead, and a
|
|
|
|
* developer warning issued.
|
|
|
|
*/
|
|
|
|
protected function initialise_default_pagetype($script = '') {
|
|
|
|
global $CFG, $SCRIPT;
|
|
|
|
|
|
|
|
if (isset($CFG->pagepath)) {
|
|
|
|
debugging('Some code appears to have set $CFG->pagepath. That was a horrible deprecated thing. ' .
|
|
|
|
'Don\'t do it! Try calling $PAGE->set_pagetype() instead.');
|
|
|
|
$script = $CFG->pagepath;
|
|
|
|
unset($CFG->pagepath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($script)) {
|
|
|
|
$script = ltrim($SCRIPT, '/');
|
|
|
|
$len = strlen($CFG->admin);
|
|
|
|
if (substr($script, 0, $len) == $CFG->admin) {
|
|
|
|
$script = 'admin' . substr($script, $len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$path = str_replace('.php', '', $script);
|
|
|
|
if (substr($path, -1) == '/') {
|
|
|
|
$path .= 'index';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($path) || $path == 'index') {
|
|
|
|
$this->_pagetype = 'site-index';
|
|
|
|
} else {
|
|
|
|
$this->_pagetype = str_replace('/', '-', $path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:42:13 +00:00
|
|
|
protected function initialise_standard_body_classes() {
|
2009-05-06 08:44:58 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2009-05-06 08:42:13 +00:00
|
|
|
$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);
|
2009-05-06 08:44:58 +00:00
|
|
|
$this->add_body_classes(get_browser_version_classes());
|
2009-05-06 08:42:13 +00:00
|
|
|
$this->add_body_class('dir-' . get_string('thisdirection'));
|
|
|
|
$this->add_body_class('lang-' . current_language());
|
|
|
|
|
2009-05-06 08:44:58 +00:00
|
|
|
$this->add_body_class($this->url_to_class_name($CFG->wwwroot));
|
|
|
|
|
2009-05-06 08:46:48 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:42:13 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:46:05 +00:00
|
|
|
protected function ensure_category_loaded() {
|
|
|
|
if (is_array($this->_categories)) {
|
|
|
|
return; // Already done.
|
|
|
|
}
|
|
|
|
if (is_null($this->_course)) {
|
|
|
|
throw new coding_exception('Attempt to get the course category for this page before the course was set.');
|
|
|
|
}
|
|
|
|
if ($this->_course->category == 0) {
|
|
|
|
$this->_categories = array();
|
|
|
|
} else {
|
|
|
|
$this->load_category($this->_course->category);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function load_category($categoryid) {
|
|
|
|
global $DB;
|
|
|
|
$category = $DB->get_record('course_categories', array('id' => $categoryid));
|
|
|
|
if (!$category) {
|
|
|
|
throw new moodle_exception('unknowncategory');
|
|
|
|
}
|
|
|
|
$this->_categories[$category->id] = $category;
|
|
|
|
$parentcategoryids = explode('/', trim($category->path, '/'));
|
|
|
|
array_pop($parentcategoryids);
|
|
|
|
foreach (array_reverse($parentcategoryids) as $catid) {
|
|
|
|
$this->_categories[$catid] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function ensure_categories_loaded() {
|
|
|
|
global $DB;
|
|
|
|
$this->ensure_category_loaded();
|
|
|
|
if (!is_null(end($this->_categories))) {
|
|
|
|
return; // Already done.
|
|
|
|
}
|
|
|
|
$idstoload = array_keys($this->_categories);
|
|
|
|
array_shift($idstoload);
|
|
|
|
$categories = $DB->get_records_list('course_categories', 'id', $idstoload);
|
|
|
|
foreach ($idstoload as $catid) {
|
|
|
|
$this->_categories[$catid] = $categories[$catid];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:44:58 +00:00
|
|
|
protected function url_to_class_name($url) {
|
|
|
|
$bits = parse_url($url);
|
|
|
|
$class = str_replace('.', '-', $bits['host']);
|
|
|
|
if (!empty($bits['port'])) {
|
|
|
|
$class .= '--' . $bits['port'];
|
|
|
|
}
|
|
|
|
if (!empty($bits['path'])) {
|
|
|
|
$path = trim($bits['path'], '/');
|
|
|
|
if ($path) {
|
|
|
|
$class .= '--' . str_replace('/', '-', $path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:42:13 +00:00
|
|
|
/// Deprecated fields and methods for backwards compatibility ==================
|
2009-05-06 08:38:55 +00:00
|
|
|
|
2009-05-06 08:34:32 +00:00
|
|
|
/**
|
2009-05-06 08:35:39 +00:00
|
|
|
* @deprecated since Moodle 2.0 - use $PAGE->pagetype instead.
|
2009-05-06 08:34:32 +00:00
|
|
|
* @return string page type.
|
|
|
|
*/
|
|
|
|
public function get_type() {
|
|
|
|
debugging('Call to deprecated method moodle_page::get_type. Please use $PAGE->pagetype instead.');
|
|
|
|
return $this->get_pagetype();
|
|
|
|
}
|
2009-05-06 08:38:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated since Moodle 2.0 - use $PAGE->pagetype instead.
|
|
|
|
* @return string this is what page_id_and_class used to return via the $getclass parameter.
|
|
|
|
*/
|
|
|
|
function get_format_name() {
|
|
|
|
return $this->get_pagetype();
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:35:39 +00:00
|
|
|
/**
|
|
|
|
* @deprecated since Moodle 2.0 - use $PAGE->course instead.
|
|
|
|
* @return object course.
|
|
|
|
*/
|
|
|
|
public function get_courserecord() {
|
|
|
|
debugging('Call to deprecated method moodle_page::get_courserecord. Please use $PAGE->course instead.');
|
|
|
|
return $this->get_course();
|
|
|
|
}
|
2009-05-06 08:38:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated since Moodle 2.0
|
|
|
|
* @return string this is what page_id_and_class used to return via the $getclass parameter.
|
|
|
|
*/
|
|
|
|
public function get_legacyclass() {
|
|
|
|
if (is_null($this->_legacyclass)) {
|
2009-05-06 08:42:13 +00:00
|
|
|
$this->initialise_standard_body_classes();
|
2009-05-06 08:38:55 +00:00
|
|
|
}
|
|
|
|
debugging('Call to deprecated method moodle_page::get_legacyclass.');
|
|
|
|
return $this->_legacyclass;
|
|
|
|
}
|
2009-05-06 08:29:22 +00:00
|
|
|
}
|
|
|
|
|
2009-05-06 08:27:12 +00:00
|
|
|
/**
|
|
|
|
* @deprecated since Moodle 2.0
|
|
|
|
* Load any page_base subclasses from the pagelib.php library in a particular folder.
|
|
|
|
* @param $path the folder path
|
|
|
|
* @return array an array of page types.
|
|
|
|
*/
|
2005-02-28 04:01:30 +00:00
|
|
|
function page_import_types($path) {
|
|
|
|
global $CFG;
|
2009-05-06 08:27:12 +00:00
|
|
|
debugging('Call to deprecated function page_import_types.', DEBUG_DEVELOPER);
|
2005-02-28 04:01:30 +00:00
|
|
|
}
|
|
|
|
|
2005-02-02 02:32:40 +00:00
|
|
|
/**
|
2009-05-06 08:38:55 +00:00
|
|
|
* @deprecated since Moodle 2.0
|
|
|
|
* @param integer $instance legacy page instance id.
|
|
|
|
* @return the global $PAGE object.
|
2005-02-02 02:32:40 +00:00
|
|
|
*/
|
|
|
|
function page_create_instance($instance) {
|
2009-05-06 08:38:55 +00:00
|
|
|
return page_create_object($PAGE->pagetype, $instance);
|
2005-02-02 02:32:40 +00:00
|
|
|
}
|
|
|
|
|
2004-11-29 04:19:05 +00:00
|
|
|
/**
|
|
|
|
* Factory function page_create_object(). Called with a pagetype identifier and possibly with
|
|
|
|
* its numeric ID. Returns a fully constructed page_base subclass you can work with.
|
|
|
|
*/
|
|
|
|
function page_create_object($type, $id = NULL) {
|
2009-05-06 08:46:48 +00:00
|
|
|
global $CFG, $PAGE, $SITE;
|
2005-01-28 17:03:57 +00:00
|
|
|
|
2004-11-29 04:19:05 +00:00
|
|
|
$data = new stdClass;
|
|
|
|
$data->pagetype = $type;
|
|
|
|
$data->pageid = $id;
|
|
|
|
|
|
|
|
$classname = page_map_class($type);
|
2008-01-25 08:39:14 +00:00
|
|
|
$object = new $classname;
|
2004-11-29 04:19:05 +00:00
|
|
|
$object->init_quick($data);
|
2009-05-06 08:46:48 +00:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|
2009-05-06 08:41:02 +00:00
|
|
|
//$object->set_pagetype($type);
|
2004-11-29 04:19:05 +00:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-11-29 04:38:07 +00:00
|
|
|
* Function page_map_class() is the way for your code to define its own page subclasses and let Moodle recognize them.
|
2004-11-29 04:19:05 +00:00
|
|
|
* Use it to associate the textual identifier of your Page with the actual class name that has to be instantiated.
|
|
|
|
*/
|
|
|
|
function page_map_class($type, $classname = NULL) {
|
2005-01-28 17:03:57 +00:00
|
|
|
global $CFG;
|
|
|
|
|
2005-01-28 03:52:56 +00:00
|
|
|
static $mappings = NULL;
|
2009-05-06 08:27:12 +00:00
|
|
|
|
2005-01-28 17:03:57 +00:00
|
|
|
if ($mappings === NULL) {
|
2005-01-28 03:52:56 +00:00
|
|
|
$mappings = array(
|
2005-02-10 01:59:11 +00:00
|
|
|
PAGE_COURSE_VIEW => 'page_course'
|
2005-01-28 03:52:56 +00:00
|
|
|
);
|
|
|
|
}
|
2004-11-29 04:19:05 +00:00
|
|
|
|
2005-01-28 17:03:57 +00:00
|
|
|
if (!empty($type) && !empty($classname)) {
|
2004-11-29 04:19:05 +00:00
|
|
|
$mappings[$type] = $classname;
|
|
|
|
}
|
2005-01-28 17:03:57 +00:00
|
|
|
|
|
|
|
if (!isset($mappings[$type])) {
|
2006-09-13 09:45:07 +00:00
|
|
|
debugging('Page class mapping requested for unknown type: '.$type);
|
2004-11-29 04:19:05 +00:00
|
|
|
}
|
|
|
|
|
2005-07-20 17:23:07 +00:00
|
|
|
if (empty($classname) && !class_exists($mappings[$type])) {
|
2006-09-13 09:45:07 +00:00
|
|
|
debugging('Page class mapping for id "'.$type.'" exists but class "'.$mappings[$type].'" is not defined');
|
2004-11-29 04:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $mappings[$type];
|
|
|
|
}
|
|
|
|
|
2004-11-08 19:36:07 +00:00
|
|
|
/**
|
|
|
|
* Parent class from which all Moodle page classes derive
|
|
|
|
*
|
|
|
|
* @author Jon Papaioannou
|
|
|
|
* @package pages
|
2004-11-29 04:19:05 +00:00
|
|
|
* @todo This parent class is very messy still. Please for the moment ignore it and move on to the derived class page_course to see the comments there.
|
2004-11-08 19:36:07 +00:00
|
|
|
*/
|
2009-05-06 08:29:22 +00:00
|
|
|
class page_base extends moodle_page {
|
2004-11-10 16:43:57 +00:00
|
|
|
/**
|
|
|
|
* The string identifier for the type of page being described.
|
|
|
|
* @var string $type
|
|
|
|
*/
|
2004-11-08 19:36:07 +00:00
|
|
|
var $type = NULL;
|
2004-11-10 16:43:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The numeric identifier of the page being described.
|
|
|
|
* @var int $id
|
|
|
|
*/
|
2004-11-08 19:36:07 +00:00
|
|
|
var $id = NULL;
|
2004-11-10 16:43:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class bool to determine if the instance's full initialization has been completed.
|
|
|
|
* @var boolean $full_init_done
|
|
|
|
*/
|
2004-11-08 19:36:07 +00:00
|
|
|
var $full_init_done = false;
|
|
|
|
|
2004-11-10 16:43:57 +00:00
|
|
|
/// Class Functions
|
|
|
|
|
2004-11-12 18:39:25 +00:00
|
|
|
// USER-RELATED THINGS
|
|
|
|
|
|
|
|
// By default, no user is editing anything and none CAN edit anything. Developers
|
|
|
|
// will have to override these settings to let Moodle know when it should grant
|
|
|
|
// editing rights to the user viewing the page.
|
|
|
|
function user_allowed_editing() {
|
|
|
|
trigger_error('Page class does not implement method <strong>user_allowed_editing()</strong>', E_USER_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
function user_is_editing() {
|
|
|
|
trigger_error('Page class does not implement method <strong>user_is_editing()</strong>', E_USER_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTML OUTPUT SECTION
|
|
|
|
|
|
|
|
// We have absolutely no idea what derived pages are all about
|
2008-01-25 08:39:14 +00:00
|
|
|
function print_header($title, $morenavlinks=NULL) {
|
2004-11-12 18:39:25 +00:00
|
|
|
trigger_error('Page class does not implement method <strong>print_header()</strong>', E_USER_WARNING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// BLOCKS RELATED SECTION
|
|
|
|
|
|
|
|
// By default, pages don't have any blocks. Override this in your derived class if you need blocks.
|
2004-11-08 19:36:07 +00:00
|
|
|
function blocks_get_positions() {
|
|
|
|
return array();
|
|
|
|
}
|
2004-11-12 18:39:25 +00:00
|
|
|
|
|
|
|
// Thus there is no default block position. If you override the above you should override this one too.
|
|
|
|
// Because this makes sense only if blocks_get_positions() is overridden and because these two should
|
|
|
|
// be overridden as a group or not at all, this one issues a warning. The sneaky part is that this warning
|
|
|
|
// will only be seen if you override blocks_get_positions() but NOT blocks_default_position().
|
|
|
|
function blocks_default_position() {
|
|
|
|
trigger_error('Page class does not implement method <strong>blocks_default_position()</strong>', E_USER_WARNING);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you don't override this, newly constructed pages of this kind won't have any blocks.
|
|
|
|
function blocks_get_default() {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you don't override this, your blocks will not be able to change positions
|
|
|
|
function blocks_move_position(&$instance, $move) {
|
|
|
|
return $instance->position;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SELF-REPORTING SECTION
|
|
|
|
|
|
|
|
// Derived classes HAVE to define their "home url"
|
2004-11-08 19:36:07 +00:00
|
|
|
function url_get_path() {
|
2004-11-12 18:39:25 +00:00
|
|
|
trigger_error('Page class does not implement method <strong>url_get_path()</strong>', E_USER_WARNING);
|
2004-11-08 19:36:07 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-11-12 18:39:25 +00:00
|
|
|
|
|
|
|
// It's not always required to pass any arguments to the home url, so this doesn't trigger any errors (sensible default)
|
2004-11-08 19:36:07 +00:00
|
|
|
function url_get_parameters() {
|
|
|
|
return array();
|
|
|
|
}
|
2004-11-12 18:39:25 +00:00
|
|
|
|
|
|
|
// This should actually NEVER be overridden unless you have GOOD reason. Works fine as it is.
|
2004-11-08 19:36:07 +00:00
|
|
|
function url_get_full($extraparams = array()) {
|
|
|
|
$path = $this->url_get_path();
|
|
|
|
if(empty($path)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
$params = $this->url_get_parameters();
|
2005-11-21 13:13:23 +00:00
|
|
|
if (!empty($params)) {
|
|
|
|
$params = array_merge($params, $extraparams);
|
|
|
|
} else {
|
|
|
|
$params = $extraparams;
|
|
|
|
}
|
2004-11-08 19:36:07 +00:00
|
|
|
|
|
|
|
if(empty($params)) {
|
|
|
|
return $path;
|
|
|
|
}
|
2009-05-06 08:27:12 +00:00
|
|
|
|
2004-11-08 19:36:07 +00:00
|
|
|
$first = true;
|
|
|
|
|
|
|
|
foreach($params as $var => $value) {
|
|
|
|
$path .= $first? '?' : '&';
|
2004-11-10 16:43:57 +00:00
|
|
|
$path .= $var .'='. urlencode($value);
|
2004-11-08 19:36:07 +00:00
|
|
|
$first = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
2004-11-12 18:39:25 +00:00
|
|
|
|
|
|
|
// Simple stuff, do not override this.
|
2004-11-08 19:36:07 +00:00
|
|
|
function get_id() {
|
|
|
|
return $this->id;
|
|
|
|
}
|
2004-11-12 18:39:25 +00:00
|
|
|
|
|
|
|
// Initialize the data members of the parent class
|
2004-11-08 19:36:07 +00:00
|
|
|
function init_quick($data) {
|
|
|
|
$this->type = $data->pagetype;
|
|
|
|
$this->id = $data->pageid;
|
|
|
|
}
|
|
|
|
|
2004-11-12 18:39:25 +00:00
|
|
|
function init_full() {
|
|
|
|
$this->full_init_done = true;
|
|
|
|
}
|
2005-08-23 04:48:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
// is this page always editable, regardless of anything else?
|
|
|
|
function edit_always() {
|
2007-01-29 21:29:27 +00:00
|
|
|
return (has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM)) && defined('ADMIN_STICKYBLOCKS'));
|
2005-08-23 04:48:34 +00:00
|
|
|
}
|
2004-11-08 19:36:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class that models the behavior of a moodle course
|
|
|
|
*
|
|
|
|
* @author Jon Papaioannou
|
|
|
|
* @package pages
|
|
|
|
*/
|
2004-11-25 17:23:57 +00:00
|
|
|
class page_course extends page_base {
|
2004-11-08 19:36:07 +00:00
|
|
|
|
|
|
|
// Do any validation of the officially recognized bits of the data and forward to parent.
|
|
|
|
// Do NOT load up "expensive" resouces (e.g. SQL data) here!
|
|
|
|
function init_quick($data) {
|
2005-10-16 22:34:25 +00:00
|
|
|
if(empty($data->pageid) && !defined('ADMIN_STICKYBLOCKS')) {
|
2008-08-16 12:16:01 +00:00
|
|
|
print_error('cannotinitpage', 'debug', '', (object)array('name'=>'course', 'id'=>'?'));
|
2004-11-08 19:36:07 +00:00
|
|
|
}
|
|
|
|
parent::init_quick($data);
|
|
|
|
}
|
|
|
|
|
2004-11-10 16:43:57 +00:00
|
|
|
// Here you should load up all heavy-duty data for your page. Basically everything that
|
2004-11-08 19:36:07 +00:00
|
|
|
// does not NEED to be loaded for the class to make basic decisions should NOT be loaded
|
|
|
|
// in init_quick() and instead deferred here. Of course this function had better recognize
|
|
|
|
// $this->full_init_done to prevent wasteful multiple-time data retrieval.
|
|
|
|
function init_full() {
|
2008-05-30 20:02:55 +00:00
|
|
|
global $COURSE, $DB;
|
|
|
|
|
2004-11-08 19:36:07 +00:00
|
|
|
if($this->full_init_done) {
|
|
|
|
return;
|
|
|
|
}
|
2005-10-16 22:34:25 +00:00
|
|
|
if (empty($this->id)) {
|
|
|
|
$this->id = 0; // avoid db errors
|
|
|
|
}
|
2007-09-19 06:53:09 +00:00
|
|
|
|
|
|
|
$this->context = get_context_instance(CONTEXT_COURSE, $this->id);
|
|
|
|
|
|
|
|
// Preload - ensures that the context cache is populated
|
|
|
|
// in one DB query...
|
|
|
|
$this->childcontexts = get_child_contexts($this->context);
|
|
|
|
|
|
|
|
// Mark we're done
|
2004-11-08 19:36:07 +00:00
|
|
|
$this->full_init_done = true;
|
|
|
|
}
|
|
|
|
|
2004-11-12 18:39:25 +00:00
|
|
|
// USER-RELATED THINGS
|
|
|
|
|
2006-09-17 17:32:45 +00:00
|
|
|
// Can user edit the course page or "sticky page"?
|
|
|
|
// This is also about editting of blocks BUT mainly activities in course page layout, see
|
2007-08-25 11:28:37 +00:00
|
|
|
// update_course_icon() has very similar checks - it must use the same capabilities
|
2007-09-19 06:53:09 +00:00
|
|
|
//
|
|
|
|
// this is a _very_ expensive check - so cache it during execution
|
|
|
|
//
|
2004-11-08 19:36:07 +00:00
|
|
|
function user_allowed_editing() {
|
2007-09-19 06:53:09 +00:00
|
|
|
$this->init_full();
|
|
|
|
|
|
|
|
if (isset($this->_user_allowed_editing)) {
|
|
|
|
return $this->_user_allowed_editing;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM))
|
2009-05-06 08:38:55 +00:00
|
|
|
&& defined('ADMIN_STICKYBLOCKS')) {
|
2007-09-19 06:53:09 +00:00
|
|
|
$this->_user_allowed_editing = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (has_capability('moodle/course:manageactivities', $this->context)) {
|
|
|
|
$this->_user_allowed_editing = true;
|
2005-10-16 22:34:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-09-19 06:53:09 +00:00
|
|
|
|
|
|
|
// Exhaustive (and expensive!) checks to see if the user
|
|
|
|
// has editing abilities to a specific module/block/group...
|
|
|
|
// This code would benefit from the ability to check specifically
|
|
|
|
// for overrides.
|
|
|
|
foreach ($this->childcontexts as $cc) {
|
|
|
|
if (($cc->contextlevel == CONTEXT_MODULE &&
|
|
|
|
has_capability('moodle/course:manageactivities', $cc)) ||
|
|
|
|
($cc->contextlevel == CONTEXT_BLOCK &&
|
|
|
|
has_capability('moodle/site:manageblocks', $cc))) {
|
|
|
|
$this->_user_allowed_editing = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2004-11-08 19:36:07 +00:00
|
|
|
}
|
|
|
|
|
2006-09-17 17:32:45 +00:00
|
|
|
// Is the user actually editing this course page or "sticky page" right now?
|
2004-11-08 19:36:07 +00:00
|
|
|
function user_is_editing() {
|
2007-01-29 21:29:27 +00:00
|
|
|
if (has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM)) && defined('ADMIN_STICKYBLOCKS')) {
|
2006-09-17 17:32:45 +00:00
|
|
|
//always in edit mode on sticky page
|
2005-10-16 22:34:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-11-08 19:36:07 +00:00
|
|
|
return isediting($this->id);
|
|
|
|
}
|
|
|
|
|
2004-11-12 18:39:25 +00:00
|
|
|
// HTML OUTPUT SECTION
|
|
|
|
|
|
|
|
// This function prints out the common part of the page's header.
|
2004-11-08 19:36:07 +00:00
|
|
|
// You should NEVER print the header "by hand" in other code.
|
2007-12-28 16:13:09 +00:00
|
|
|
function print_header($title, $morenavlinks=NULL, $meta='', $bodytags='', $extrabuttons='') {
|
2005-01-02 15:20:15 +00:00
|
|
|
global $USER, $CFG;
|
|
|
|
|
2004-11-08 19:36:07 +00:00
|
|
|
$this->init_full();
|
|
|
|
$replacements = array(
|
2009-05-06 08:35:39 +00:00
|
|
|
'%fullname%' => $this->course->fullname
|
2004-11-08 19:36:07 +00:00
|
|
|
);
|
|
|
|
foreach($replacements as $search => $replace) {
|
|
|
|
$title = str_replace($search, $replace, $title);
|
|
|
|
}
|
2009-05-06 08:27:12 +00:00
|
|
|
|
2007-07-05 04:40:48 +00:00
|
|
|
$navlinks = array();
|
2009-05-06 08:27:12 +00:00
|
|
|
|
2007-07-05 04:40:48 +00:00
|
|
|
if(!empty($morenavlinks)) {
|
|
|
|
$navlinks = array_merge($navlinks, $morenavlinks);
|
2005-01-02 15:20:15 +00:00
|
|
|
}
|
|
|
|
|
2007-07-05 04:40:48 +00:00
|
|
|
$navigation = build_navigation($navlinks);
|
2005-01-02 15:20:15 +00:00
|
|
|
|
2005-01-04 19:43:09 +00:00
|
|
|
// The "Editing On" button will be appearing only in the "main" course screen
|
|
|
|
// (i.e., no breadcrumbs other than the default one added inside this function)
|
2009-05-06 08:35:39 +00:00
|
|
|
$buttons = switchroles_form($this->course->id);
|
2007-09-19 06:53:09 +00:00
|
|
|
if ($this->user_allowed_editing()) {
|
2009-05-06 08:35:39 +00:00
|
|
|
$buttons .= update_course_icon($this->course->id );
|
2007-09-19 06:53:09 +00:00
|
|
|
}
|
2007-07-05 04:40:48 +00:00
|
|
|
$buttons = empty($morenavlinks) ? $buttons : ' ';
|
2005-01-04 19:43:09 +00:00
|
|
|
|
2007-12-28 16:13:09 +00:00
|
|
|
// Add any extra buttons requested (by the resource module, for example)
|
|
|
|
if ($extrabuttons != '') {
|
|
|
|
$buttons = ($buttons == ' ') ? $extrabuttons : $buttons.$extrabuttons;
|
|
|
|
}
|
|
|
|
|
2009-05-06 08:35:39 +00:00
|
|
|
print_header($title, $this->course->fullname, $navigation,
|
|
|
|
'', $meta, true, $buttons, user_login_string($this->course, $USER), false, $bodytags);
|
2004-11-08 19:36:07 +00:00
|
|
|
}
|
|
|
|
|
2004-11-12 18:39:25 +00:00
|
|
|
// SELF-REPORTING SECTION
|
|
|
|
|
2004-11-08 19:36:07 +00:00
|
|
|
// This should return a fully qualified path to the URL which is responsible for displaying us.
|
|
|
|
function url_get_path() {
|
|
|
|
global $CFG;
|
2005-10-16 22:34:25 +00:00
|
|
|
if (defined('ADMIN_STICKYBLOCKS')) {
|
2006-07-19 08:13:25 +00:00
|
|
|
return $CFG->wwwroot.'/'.$CFG->admin.'/stickyblocks.php';
|
2005-10-16 22:34:25 +00:00
|
|
|
}
|
2004-11-08 19:36:07 +00:00
|
|
|
if($this->id == SITEID) {
|
2004-11-10 16:43:57 +00:00
|
|
|
return $CFG->wwwroot .'/index.php';
|
2004-11-08 19:36:07 +00:00
|
|
|
}
|
|
|
|
else {
|
2004-11-10 16:43:57 +00:00
|
|
|
return $CFG->wwwroot .'/course/view.php';
|
2004-11-08 19:36:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should return an associative array of any GET/POST parameters that are needed by the URL
|
|
|
|
// which displays us to make it work. If none are needed, return an empty array.
|
|
|
|
function url_get_parameters() {
|
2005-10-16 22:34:25 +00:00
|
|
|
if (defined('ADMIN_STICKYBLOCKS')) {
|
|
|
|
return array('pt' => ADMIN_STICKYBLOCKS);
|
|
|
|
}
|
2004-11-08 19:36:07 +00:00
|
|
|
if($this->id == SITEID) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return array('id' => $this->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-12 18:39:25 +00:00
|
|
|
// BLOCKS RELATED SECTION
|
2004-11-08 19:36:07 +00:00
|
|
|
|
|
|
|
// Which are the positions in this page which support blocks? Return an array containing their identifiers.
|
|
|
|
// BE CAREFUL, ORDER DOES MATTER! In textual representations, lists of blocks in a page use the ':' character
|
|
|
|
// to delimit different positions in the page. The part before the first ':' in such a representation will map
|
|
|
|
// directly to the first item of the array you return here, the second to the next one and so on. This way,
|
|
|
|
// you can add more positions in the future without interfering with legacy textual representations.
|
|
|
|
function blocks_get_positions() {
|
|
|
|
return array(BLOCK_POS_LEFT, BLOCK_POS_RIGHT);
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a new block is created in this page, which position should it go to?
|
|
|
|
function blocks_default_position() {
|
|
|
|
return BLOCK_POS_RIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When we are creating a new page, use the data at your disposal to provide a textual representation of the
|
|
|
|
// blocks that are going to get added to this new page. Delimit block names with commas (,) and use double
|
|
|
|
// colons (:) to delimit between block positions in the page. See blocks_get_positions() for additional info.
|
|
|
|
function blocks_get_default() {
|
|
|
|
global $CFG;
|
2009-05-06 08:27:12 +00:00
|
|
|
|
2004-11-08 19:36:07 +00:00
|
|
|
$this->init_full();
|
|
|
|
|
|
|
|
if($this->id == SITEID) {
|
|
|
|
// Is it the site?
|
|
|
|
if (!empty($CFG->defaultblocks_site)) {
|
|
|
|
$blocknames = $CFG->defaultblocks_site;
|
|
|
|
}
|
|
|
|
/// Failsafe - in case nothing was defined.
|
|
|
|
else {
|
2006-09-11 20:05:31 +00:00
|
|
|
$blocknames = 'site_main_menu,admin_tree:course_summary,calendar_month';
|
2004-11-08 19:36:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// It's a normal course, so do it according to the course format
|
|
|
|
else {
|
2009-05-06 08:35:39 +00:00
|
|
|
$pageformat = $this->course->format;
|
2004-11-08 19:36:07 +00:00
|
|
|
if (!empty($CFG->{'defaultblocks_'. $pageformat})) {
|
|
|
|
$blocknames = $CFG->{'defaultblocks_'. $pageformat};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$format_config = $CFG->dirroot.'/course/format/'.$pageformat.'/config.php';
|
|
|
|
if (@is_file($format_config) && is_readable($format_config)) {
|
|
|
|
require($format_config);
|
|
|
|
}
|
|
|
|
if (!empty($format['defaultblocks'])) {
|
|
|
|
$blocknames = $format['defaultblocks'];
|
|
|
|
}
|
|
|
|
else if (!empty($CFG->defaultblocks)){
|
|
|
|
$blocknames = $CFG->defaultblocks;
|
|
|
|
}
|
|
|
|
/// Failsafe - in case nothing was defined.
|
|
|
|
else {
|
|
|
|
$blocknames = 'participants,activity_modules,search_forums,admin,course_list:news_items,calendar_upcoming,recent_activity';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-06 08:27:12 +00:00
|
|
|
|
2004-11-08 19:36:07 +00:00
|
|
|
return $blocknames;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given an instance of a block in this page and the direction in which we want to move it, where is
|
|
|
|
// it going to go? Return the identifier of the instance's new position. This allows us to tell blocklib
|
|
|
|
// how we want the blocks to move around in this page in an arbitrarily complex way. If the move as given
|
|
|
|
// does not make sense, make sure to return the instance's original position.
|
|
|
|
//
|
|
|
|
// Since this is going to get called a LOT, pass the instance by reference purely for speed. Do **NOT**
|
|
|
|
// modify its data in any way, this will actually confuse blocklib!!!
|
|
|
|
function blocks_move_position(&$instance, $move) {
|
|
|
|
if($instance->position == BLOCK_POS_LEFT && $move == BLOCK_MOVE_RIGHT) {
|
|
|
|
return BLOCK_POS_RIGHT;
|
2004-11-10 16:43:57 +00:00
|
|
|
} else if ($instance->position == BLOCK_POS_RIGHT && $move == BLOCK_MOVE_LEFT) {
|
2004-11-08 19:36:07 +00:00
|
|
|
return BLOCK_POS_LEFT;
|
|
|
|
}
|
|
|
|
return $instance->position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-01 07:12:39 +00:00
|
|
|
/**
|
2005-02-09 15:22:16 +00:00
|
|
|
* Class that models the common parts of all activity modules
|
2005-02-01 07:12:39 +00:00
|
|
|
*
|
|
|
|
* @author Jon Papaioannou
|
|
|
|
* @package pages
|
|
|
|
*/
|
2005-02-09 15:22:16 +00:00
|
|
|
class page_generic_activity extends page_base {
|
|
|
|
var $activityname = NULL;
|
|
|
|
var $modulerecord = NULL;
|
|
|
|
var $activityrecord = NULL;
|
2005-02-01 07:12:39 +00:00
|
|
|
|
|
|
|
function init_full() {
|
2008-05-30 20:02:55 +00:00
|
|
|
global $DB;
|
|
|
|
|
2005-02-01 07:12:39 +00:00
|
|
|
if($this->full_init_done) {
|
|
|
|
return;
|
|
|
|
}
|
2005-02-09 15:22:16 +00:00
|
|
|
if(empty($this->activityname)) {
|
2008-05-20 02:53:46 +00:00
|
|
|
print_error('noactivityname', 'debug');
|
2005-02-09 15:22:16 +00:00
|
|
|
}
|
2007-12-06 11:38:53 +00:00
|
|
|
if (!$this->modulerecord = get_coursemodule_from_instance($this->activityname, $this->id)) {
|
2008-08-16 12:16:01 +00:00
|
|
|
print_error('cannotinitpager', 'debug', '', (object)array('name'=>$this->activityname, 'id'=>$this->id));
|
2005-02-01 07:12:39 +00:00
|
|
|
}
|
2008-05-30 20:02:55 +00:00
|
|
|
$this->activityrecord = $DB->get_record($this->activityname, array('id'=>$this->id));
|
2008-01-11 15:44:11 +00:00
|
|
|
if(empty($this->activityrecord)) {
|
2008-08-16 12:16:01 +00:00
|
|
|
print_error('cannotinitpager', 'debug', '', (object)array('name'=>$this->activityname, 'id'=>$this->id));
|
2005-02-01 07:12:39 +00:00
|
|
|
}
|
|
|
|
$this->full_init_done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function user_allowed_editing() {
|
2005-02-04 20:27:54 +00:00
|
|
|
$this->init_full();
|
2007-03-26 02:55:03 +00:00
|
|
|
// Yu: I think this is wrong, should be checking manageactivities instead
|
|
|
|
//return has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_COURSE, $this->modulerecord->course));
|
2009-05-06 08:27:12 +00:00
|
|
|
return has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_MODULE, $this->modulerecord->id));
|
2005-02-01 07:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function user_is_editing() {
|
2005-02-04 20:27:54 +00:00
|
|
|
$this->init_full();
|
2005-02-01 07:12:39 +00:00
|
|
|
return isediting($this->modulerecord->course);
|
|
|
|
}
|
|
|
|
|
2005-02-09 15:22:16 +00:00
|
|
|
function url_get_path() {
|
|
|
|
global $CFG;
|
|
|
|
return $CFG->wwwroot .'/mod/'.$this->activityname.'/view.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
function url_get_parameters() {
|
|
|
|
$this->init_full();
|
|
|
|
return array('id' => $this->modulerecord->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function blocks_get_positions() {
|
|
|
|
return array(BLOCK_POS_LEFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
function blocks_default_position() {
|
|
|
|
return BLOCK_POS_LEFT;
|
|
|
|
}
|
2009-05-06 08:27:12 +00:00
|
|
|
|
2007-07-05 04:40:48 +00:00
|
|
|
function print_header($title, $morenavlinks = NULL, $bodytags = '', $meta = '') {
|
2007-04-16 20:48:48 +00:00
|
|
|
global $USER, $CFG;
|
2009-05-06 08:27:12 +00:00
|
|
|
|
2007-04-16 20:48:48 +00:00
|
|
|
$this->init_full();
|
|
|
|
$replacements = array(
|
|
|
|
'%fullname%' => format_string($this->activityrecord->name)
|
|
|
|
);
|
|
|
|
foreach ($replacements as $search => $replace) {
|
|
|
|
$title = str_replace($search, $replace, $title);
|
|
|
|
}
|
2009-05-06 08:27:12 +00:00
|
|
|
|
2007-07-05 04:40:48 +00:00
|
|
|
if (empty($morenavlinks) && $this->user_allowed_editing()) {
|
2009-05-06 08:35:39 +00:00
|
|
|
$buttons = '<table><tr><td>'.update_module_button($this->modulerecord->id, $this->course->id, get_string('modulename', $this->activityname)).'</td>';
|
2007-04-16 20:48:48 +00:00
|
|
|
if (!empty($CFG->showblocksonmodpages)) {
|
2007-10-16 20:44:55 +00:00
|
|
|
$buttons .= '<td><form '.$CFG->frametarget.' method="get" action="view.php"><div>'.
|
2007-04-16 20:48:48 +00:00
|
|
|
'<input type="hidden" name="id" value="'.$this->modulerecord->id.'" />'.
|
|
|
|
'<input type="hidden" name="edit" value="'.($this->user_is_editing()?'off':'on').'" />'.
|
2007-10-16 20:44:55 +00:00
|
|
|
'<input type="submit" value="'.get_string($this->user_is_editing()?'blockseditoff':'blocksediton').'" /></div></form></td>';
|
2007-04-16 20:48:48 +00:00
|
|
|
}
|
|
|
|
$buttons .= '</tr></table>';
|
|
|
|
} else {
|
|
|
|
$buttons = ' ';
|
|
|
|
}
|
2009-05-06 08:27:12 +00:00
|
|
|
|
2007-10-12 15:55:49 +00:00
|
|
|
if (empty($morenavlinks)) {
|
|
|
|
$morenavlinks = array();
|
|
|
|
}
|
|
|
|
$navigation = build_navigation($morenavlinks, $this->modulerecord);
|
2009-05-06 08:35:39 +00:00
|
|
|
print_header($title, $this->course->fullname, $navigation, '', $meta, true, $buttons, navmenu($this->course, $this->modulerecord), false, $bodytags);
|
2007-04-16 20:48:48 +00:00
|
|
|
}
|
2005-02-09 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
2005-01-28 17:03:57 +00:00
|
|
|
?>
|