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;
|
|
|
|
/**#@-*/
|
|
|
|
|
|
|
|
protected $_state = self::STATE_BEFORE_HEADER;
|
|
|
|
|
|
|
|
protected $_course = null;
|
|
|
|
|
2009-05-06 08:30:25 +00:00
|
|
|
protected $_context = null;
|
|
|
|
|
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: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: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.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($state == self::STATE_PRINTING_HEADER && !$this->_course) {
|
|
|
|
global $SITE;
|
|
|
|
$this->set_course($SITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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) {
|
|
|
|
global $COURSE, $SITE;
|
|
|
|
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$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:29:22 +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: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;
|
2005-02-28 04:16:41 +00:00
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Factory function page_create_object(). Called with a numeric ID for a page, it autodetects
|
|
|
|
* the page type, constructs the correct object and returns it.
|
|
|
|
*/
|
|
|
|
function page_create_instance($instance) {
|
|
|
|
page_id_and_class($id, $class);
|
|
|
|
return page_create_object($id, $instance);
|
|
|
|
}
|
|
|
|
|
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:29:22 +00:00
|
|
|
global $CFG, $PAGE;
|
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
|
|
|
// TODO: subclassing check here
|
|
|
|
|
2005-01-28 17:03:57 +00:00
|
|
|
if ($object->get_type() !== $type) {
|
2004-11-29 04:19:05 +00:00
|
|
|
// Somehow somewhere someone made a mistake
|
2006-09-13 09:45:07 +00:00
|
|
|
debugging('Page object\'s type ('. $object->get_type() .') does not match requested type ('. $type .')');
|
2004-11-29 04:19:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$object->init_quick($data);
|
2009-05-06 08:29:22 +00:00
|
|
|
$object->set_course($PAGE->course);
|
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;
|
|
|
|
|
2005-01-26 15:33:50 +00:00
|
|
|
/**
|
|
|
|
* The class attribute that Moodle has to assign to the BODY tag for this page.
|
|
|
|
* @var string $body_class
|
|
|
|
*/
|
|
|
|
var $body_class = NULL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The id attribute that Moodle has to assign to the BODY tag for this page.
|
|
|
|
* @var string $body_id
|
|
|
|
*/
|
|
|
|
var $body_id = NULL;
|
|
|
|
|
2004-11-10 16:43:57 +00:00
|
|
|
/// Class Functions
|
|
|
|
|
2005-01-26 15:33:50 +00:00
|
|
|
// CONSTRUCTION
|
|
|
|
|
|
|
|
// A whole battery of functions to allow standardized-name constructors in all versions of PHP.
|
|
|
|
// The constructor is actually called construct()
|
|
|
|
function __construct() {
|
|
|
|
$this->construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
function construct() {
|
2005-02-02 02:32:40 +00:00
|
|
|
page_id_and_class($this->body_id, $this->body_class);
|
2005-01-26 15:33:50 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// This forces implementers to actually hardwire their page identification constant in the class.
|
|
|
|
// Good thing, if you ask me. That way we can later auto-detect "installed" page types by querying
|
|
|
|
// the classes themselves in the future.
|
2004-11-08 19:36:07 +00:00
|
|
|
function get_type() {
|
2004-11-12 18:39:25 +00:00
|
|
|
trigger_error('Page class does not implement method <strong>get_type()</strong>', E_USER_ERROR);
|
|
|
|
return NULL;
|
2004-11-08 19:36:07 +00:00
|
|
|
}
|
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
|
|
|
|
2005-02-08 02:59:44 +00:00
|
|
|
// "Sensible default" case here. Take it from the body id.
|
2004-11-08 19:36:07 +00:00
|
|
|
function get_format_name() {
|
2005-02-08 02:59:44 +00:00
|
|
|
return $this->body_id;
|
2004-11-08 19:36:07 +00:00
|
|
|
}
|
|
|
|
|
2005-02-02 02:32:40 +00:00
|
|
|
// Returns $this->body_class
|
|
|
|
function get_body_class() {
|
|
|
|
return $this->body_class;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns $this->body_id
|
|
|
|
function get_body_id() {
|
|
|
|
return $this->body_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
|
|
|
|
|
|
|
// Any data we might need to store specifically about ourself should be declared here.
|
|
|
|
// After init_full() is called for the first time, ALL of these variables should be
|
|
|
|
// initialized correctly and ready for use.
|
|
|
|
var $courserecord = NULL;
|
|
|
|
|
|
|
|
// 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-08-26 08:24:50 +00:00
|
|
|
if ($this->id == $COURSE->id) {
|
|
|
|
$this->courserecord = $COURSE;
|
|
|
|
} else {
|
2008-05-31 17:44:57 +00:00
|
|
|
$this->courserecord = $DB->get_record('course', array('id'=>$this->id));
|
2007-08-26 08:24:50 +00:00
|
|
|
}
|
|
|
|
|
2005-10-16 22:34:25 +00:00
|
|
|
if(empty($this->courserecord) && !defined('ADMIN_STICKYBLOCKS')) {
|
2008-08-16 12:16:01 +00:00
|
|
|
print_error('cannotinitpage', 'debug', '', (object)array('name'=>'course', 'id'=>$this->id));
|
2004-11-08 19:36:07 +00:00
|
|
|
}
|
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-08-25 11:28:37 +00:00
|
|
|
|
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))
|
|
|
|
&& defined('ADMIN_STICKYBLOCKS')) {
|
|
|
|
$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(
|
|
|
|
'%fullname%' => $this->courserecord->fullname
|
|
|
|
);
|
|
|
|
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)
|
2007-09-19 06:53:09 +00:00
|
|
|
$buttons = switchroles_form($this->courserecord->id);
|
|
|
|
if ($this->user_allowed_editing()) {
|
|
|
|
$buttons .= update_course_icon($this->courserecord->id );
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2007-04-16 20:48:48 +00:00
|
|
|
print_header($title, $this->courserecord->fullname, $navigation,
|
2006-10-24 09:42:06 +00:00
|
|
|
'', $meta, true, $buttons, user_login_string($this->courserecord, $USER), false, $bodytags);
|
2004-11-08 19:36:07 +00:00
|
|
|
}
|
|
|
|
|
2004-11-12 18:39:25 +00:00
|
|
|
// SELF-REPORTING SECTION
|
|
|
|
|
2004-11-29 04:19:05 +00:00
|
|
|
// This is hardwired here so the factory function page_create_object() can be sure there was no mistake.
|
2004-11-08 19:36:07 +00:00
|
|
|
// Also, it doubles as a way to let others inquire about our type.
|
|
|
|
function get_type() {
|
2005-01-31 02:18:15 +00:00
|
|
|
return PAGE_COURSE_VIEW;
|
2004-11-08 19:36:07 +00:00
|
|
|
}
|
|
|
|
|
2005-01-31 02:18:15 +00:00
|
|
|
// This is like the "category" of a page of this "type". For example, if the type is PAGE_COURSE_VIEW
|
|
|
|
// the format_name is the actual name of the course format. If the type were PAGE_ACTIVITY_VIEW, then
|
2004-11-08 19:36:07 +00:00
|
|
|
// the format_name might be that activity's name etc.
|
|
|
|
function get_format_name() {
|
|
|
|
$this->init_full();
|
2006-03-17 03:17:44 +00:00
|
|
|
if (defined('ADMIN_STICKYBLOCKS')) {
|
|
|
|
return PAGE_COURSE_VIEW;
|
|
|
|
}
|
2005-02-08 02:59:44 +00:00
|
|
|
if($this->id == SITEID) {
|
|
|
|
return parent::get_format_name();
|
|
|
|
}
|
2007-02-24 23:38:42 +00:00
|
|
|
// This needs to reflect the path hierarchy under Moodle root.
|
|
|
|
return 'course-view-'.$this->courserecord->format;
|
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 {
|
|
|
|
$pageformat = $this->courserecord->format;
|
|
|
|
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 $courserecord = 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->courserecord = $DB->get_record('course', array('id'=>$this->modulerecord->course));
|
2005-02-01 07:12:39 +00:00
|
|
|
if(empty($this->courserecord)) {
|
2008-05-20 02:53:46 +00:00
|
|
|
print_error('invalidcourseid');
|
2005-02-01 07:34:17 +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()) {
|
2007-04-16 20:48:48 +00:00
|
|
|
$buttons = '<table><tr><td>'.update_module_button($this->modulerecord->id, $this->courserecord->id, get_string('modulename', $this->activityname)).'</td>';
|
|
|
|
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);
|
2007-04-16 20:48:48 +00:00
|
|
|
print_header($title, $this->courserecord->fullname, $navigation, '', $meta, true, $buttons, navmenu($this->courserecord, $this->modulerecord), false, $bodytags);
|
|
|
|
}
|
2005-02-09 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
2005-01-28 17:03:57 +00:00
|
|
|
?>
|