From 6e4dc10f545a4e23705e3882f562c35a77232790 Mon Sep 17 00:00:00 2001
From: skodak
Date: Sat, 2 Sep 2006 13:14:57 +0000
Subject: [PATCH] deglobalization of $ADMIN; merged admin/adminlib.php into
lib/adminlib.php; fixed includes and some other minor fixes
---
admin/adminlib.php | 1657 ----------------
admin/auth.php | 9 +-
admin/block.php | 9 +-
admin/blocks.php | 11 +-
admin/cron.php | 2 +-
admin/enrol.php | 9 +-
admin/enrol_config.php | 9 +-
admin/environment.php | 9 +-
admin/filter.php | 9 +-
admin/filters.php | 9 +-
admin/index.php | 19 +-
admin/lang.php | 11 +-
admin/langdoc.php | 9 +-
admin/langimport.php | 9 +-
admin/maintenance.php | 9 +-
admin/module.php | 9 +-
admin/modules.php | 11 +-
admin/pagelib.php | 15 +-
admin/roles/manage.php | 11 +-
admin/settings.php | 8 +-
admin/settings/first.php | 2 +-
admin/timezoneimport.php | 9 +-
admin/upgradesettings.php | 12 +-
backup/lib.php | 6 +-
blocks/admin/block_admin.php | 2 +-
blocks/admin_2/block_admin_2.php | 14 +-
.../admin_bookmarks/block_admin_bookmarks.php | 11 +-
blocks/admin_bookmarks/create.php | 5 +-
blocks/admin_bookmarks/delete.php | 5 +-
course/category.php | 9 +-
course/index.php | 9 +-
lib/adminlib.php | 1664 +++++++++++++++++
lib/environmentlib.php | 2 +-
theme/index.php | 11 +-
34 files changed, 1819 insertions(+), 1786 deletions(-)
delete mode 100644 admin/adminlib.php
diff --git a/admin/adminlib.php b/admin/adminlib.php
deleted file mode 100644
index 87f2697121..0000000000
--- a/admin/adminlib.php
+++ /dev/null
@@ -1,1657 +0,0 @@
-add('userinterface', new admin_externalpage('foo', get_string('foo'),
-/// $CFG->wwwdir . '/' . '$CFG->admin . '/foo.php', 'some_role_permission'));
-
-/// Next, in foo.php, your file structure would resemble the following:
-
-/// require_once('.../config.php');
-/// require_once($CFG->dirroot . '/' . $CFG->admin . '/adminlib.php');
-/// admin_externalpage_setup('foo');
-/// // functionality like processing form submissions goes here
-/// admin_externalpage_print_header();
-/// // your HTML goes here
-/// admin_externalpage_print_footer();
-
-/// The admin_externalpage_setup() function call ensures the user is logged in,
-/// and makes sure that they have the proper role permission to access the page.
-
-/// The admin_externalpage_print_header() function prints the header (it figures
-/// out what category and subcategories the page is classified under) and ensures
-/// that you're using the admin pagelib (which provides the admin tree block and
-/// the admin bookmarks block).
-
-/// The admin_externalpage_print_footer() function properly closes the tables
-/// opened up by the admin_externalpage_print_header() function and prints the
-/// standard Moodle footer.
-
-/// ADMIN_CATEGORY OBJECTS
-
-/// Above and beyond all this, we have admin_category objects. These objects
-/// appear as folders in the admin tree block. They contain admin_settingpage's,
-/// admin_externalpage's, and other admin_category's.
-
-/// OTHER NOTES
-
-/// admin_settingpage's, admin_externalpage's, and admin_category's all inherit
-/// from part_of_admin_tree (a pseudointerface). This interface insists that
-/// a class has a check_access method for access permissions, a locate method
-/// used to find a specific node in the $ADMIN tree, and a path method used
-/// to determine the path to a specific node in the $ADMIN tree.
-
-/// admin_category's inherit from parentable_part_of_admin_tree. This pseudo-
-/// interface ensures that the class implements a recursive add function which
-/// accepts a part_of_admin_tree object and searches for the proper place to
-/// put it. parentable_part_of_admin_tree implies part_of_admin_tree.
-
-/// Please note that the $this->name field of any part_of_admin_tree must be
-/// UNIQUE throughout the ENTIRE admin tree.
-
-/// The $this->name field of an admin_setting object (which is *not* part_of_
-/// admin_tree) must be unique on the respective admin_settingpage where it is
-/// used.
-
-
-/// MISCELLANEOUS STUFF (used by classes defined below) ///////////////////////
-include_once($CFG->dirroot . '/backup/lib.php');
-
-/// CLASS DEFINITIONS /////////////////////////////////////////////////////////
-
-/**
- * Pseudointerface for anything appearing in the admin tree
- *
- * The pseudointerface that is implemented by anything that appears in the admin tree
- * block. It forces inheriting classes to define a method for checking user permissions
- * and methods for finding something in the admin tree.
- *
- * @author Vincenzo K. Marcovecchio
- * @package admin
- */
-class part_of_admin_tree {
-
- /**
- * Finds a named part_of_admin_tree.
- *
- * Used to find a part_of_admin_tree. If a class only inherits part_of_admin_tree
- * and not parentable_part_of_admin_tree, then this function should only check if
- * $this->name matches $name. If it does, it should return a reference to $this,
- * otherwise, it should return a reference to NULL.
- *
- * If a class inherits parentable_part_of_admin_tree, this method should be called
- * recursively on all child objects (assuming, of course, the parent object's name
- * doesn't match the search criterion).
- *
- * @param string $name The internal name of the part_of_admin_tree we're searching for.
- * @return mixed An object reference or a NULL reference.
- */
- function &locate($name) {
- trigger_error('Admin class does not implement method locate()', E_USER_WARNING);
- return;
- }
-
- /**
- * Verifies current user's access to this part_of_admin_tree.
- *
- * Used to check if the current user has access to this part of the admin tree or
- * not. If a class only inherits part_of_admin_tree and not parentable_part_of_admin_tree,
- * then this method is usually just a call to has_capability() in the site context.
- *
- * If a class inherits parentable_part_of_admin_tree, this method should return the
- * logical OR of the return of check_access() on all child objects.
- *
- * @return bool True if the user has access, false if she doesn't.
- */
- function check_access() {
- trigger_error('Admin class does not implement method check_access()', E_USER_WARNING);
- return;
- }
-
- /**
- * Determines the path to $name in the admin tree.
- *
- * Used to determine the path to $name in the admin tree. If a class inherits only
- * part_of_admin_tree and not parentable_part_of_admin_tree, then this method should
- * check if $this->name matches $name. If it does, $name is pushed onto the $path
- * array (at the end), and $path should be returned. If it doesn't, NULL should be
- * returned.
- *
- * If a class inherits parentable_part_of_admin_tree, it should do the above, but not
- * return NULL on failure. Instead, it pushes $this->name onto $path, and then
- * recursively calls path() on its child objects. If any are non-NULL, it should
- * return $path (being certain that the last element of $path is equal to $name).
- * If they are all NULL, it returns NULL.
- *
- * @param string $name The internal name of the part_of_admin_tree we're searching for.
- * @param array $path Not used on external calls. Defaults to empty array.
- * @return mixed If found, an array containing the internal names of each part_of_admin_tree that leads to $name. If not found, NULL.
- */
- function path($name, $path = array()) {
- trigger_error('Admin class does not implement method path()', E_USER_WARNING);
- return;
- }
-}
-
-/**
- * Pseudointerface implemented by any part_of_admin_tree that has children.
- *
- * The pseudointerface implemented by any part_of_admin_tree that can be a parent
- * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart
- * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods
- * include an add method for adding other part_of_admin_tree objects as children.
- *
- * @author Vincenzo K. Marcovecchio
- * @package admin
- */
-class parentable_part_of_admin_tree extends part_of_admin_tree {
-
- /**
- * Adds a part_of_admin_tree object to the admin tree.
- *
- * Used to add a part_of_admin_tree object to this object or a child of this
- * object. $something should only be added if $destinationname matches
- * $this->name. If it doesn't, add should be called on child objects that are
- * also parentable_part_of_admin_tree's.
- *
- * @param string $destinationname The internal name of the new parent for $something.
- * @param part_of_admin_tree &$something The object to be added.
- * @return bool True on success, false on failure.
- */
- function add($destinationname, &$something) {
- trigger_error('Admin class does not implement method add()', E_USER_WARNING);
- return;
- }
-
-}
-
-/**
- * The object used to represent folders (a.k.a. categories) in the admin tree block.
- *
- * Each admin_category object contains a number of part_of_admin_tree objects.
- *
- * @author Vincenzo K. Marcovecchio
- * @package admin
- */
-class admin_category extends parentable_part_of_admin_tree {
-
- /**
- * @var mixed An array of part_of_admin_tree objects that are this object's children
- */
- var $children;
-
- /**
- * @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
- */
- var $name;
-
- /**
- * @var string The displayed name for this category. Usually obtained through get_string()
- */
- var $visiblename;
-
- // constructor for an empty admin category
- // $name is the internal name of the category. it MUST be unique in the entire hierarchy
- // $visiblename is the displayed name of the category. use a get_string for this
-
- /**
- * Constructor for an empty admin category
- *
- * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
- * @param string $visiblename The displayed named for this category. Usually obtained through get_string()
- * @return mixed Returns the new object.
- */
- function admin_category($name, $visiblename) {
- $this->children = array();
- $this->name = $name;
- $this->visiblename = $visiblename;
- }
-
- /**
- * Finds the path to the part_of_admin_tree called $name.
- *
- * @param string $name The internal name that we're searching for.
- * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
- * @return mixed An array of internal names that leads to $name, or NULL if not found.
- */
- function path($name, $path = array()) {
-
- $path[count($path)] = $this->name;
-
- if ($this->name == $name) {
- return $path;
- }
-
- foreach($this->children as $child) {
- if ($return = $child->path($name, $path)) {
- return $return;
- }
- }
-
- return NULL;
-
- }
-
- /**
- * Returns a reference to the part_of_admin_tree object with internal name $name.
- *
- * @param string $name The internal name of the object we want.
- * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
- */
- function &locate($name) {
-
- if ($this->name == $name) {
- return $this;
- }
-
- foreach($this->children as $child) {
- if ($return =& $child->locate($name)) {
- return $return;
- }
- }
- $return = NULL;
- return $return;
- }
-
- /**
- * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object.
- *
- * @param string $destinationame The internal name of the immediate parent that we want for &$something.
- * @param mixed &$something A part_of_admin_tree object to be added.
- * @param int $precedence The precedence of &$something when displayed. Smaller numbers mean it'll be displayed higher up in the admin menu. Defaults to '', meaning "next available position".
- * @return bool True if successfully added, false if &$something is not a part_of_admin_tree or if $name is not found.
- */
- function add($destinationname, &$something, $precedence = '') {
-
- if (!is_a($something, 'part_of_admin_tree')) {
- return false;
- }
-
- if ($destinationname == $this->name) {
- if ($precedence === '') {
- $this->children[] = $something;
- } else {
- if (isset($this->children[$precedence])) { // this should never, ever be triggered in a release version of moodle.
- echo ('There is a precedence conflict in the category ' . $this->name . '. The object named ' . $something->name . ' is overwriting the object named ' . $this->children[$precedence]->name . '. ');
- }
- $this->children[$precedence] = $something;
- }
- return true;
- }
-
- unset($entries);
-
- $entries = array_keys($this->children);
-
- foreach($entries as $entry) {
- $child =& $this->children[$entry];
- if (is_a($child, 'parentable_part_of_admin_tree')) {
- if ($child->add($destinationname, $something, $precedence)) {
- return true;
- }
- }
- }
-
- return false;
-
- }
-
- /**
- * Checks if the user has access to anything in this category.
- *
- * @return bool True if the user has access to atleast one child in this category, false otherwise.
- */
- function check_access() {
-
- $return = false;
- foreach ($this->children as $child) {
- $return = $return || $child->check_access();
- }
-
- return $return;
-
- }
-
-}
-
-/**
- * Links external PHP pages into the admin tree.
- *
- * See detailed usage example at the top of this document (adminlib.php)
- *
- * @author Vincenzo K. Marcovecchio
- * @package admin
- */
-class admin_externalpage extends part_of_admin_tree {
-
- /**
- * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
- */
- var $name;
-
- /**
- * @var string The displayed name for this external page. Usually obtained through get_string().
- */
- var $visiblename;
-
- /**
- * @var string The external URL that we should link to when someone requests this external page.
- */
- var $url;
-
- /**
- * @var string The role capability/permission a user must have to access this external page.
- */
- var $role;
-
- /**
- * Constructor for adding an external page into the admin tree.
- *
- * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
- * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
- * @param string $url The external URL that we should link to when someone requests this external page.
- * @param string $role The role capability/permission a user must have to access this external page. Defaults to 'moodle/legacy:admin'.
- */
- function admin_externalpage($name, $visiblename, $url, $role = 'moodle/legacy:admin') {
- $this->name = $name;
- $this->visiblename = $visiblename;
- $this->url = $url;
- $this->role = $role;
- }
-
- /**
- * Finds the path to the part_of_admin_tree called $name.
- *
- * @param string $name The internal name that we're searching for.
- * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
- * @return mixed An array of internal names that leads to $name, or NULL if not found.
- */
- function path($name, $path = array()) {
- if ($name == $this->name) {
- array_push($path, $this->name);
- return $path;
- } else {
- return NULL;
- }
- }
-
- /**
- * Returns a reference to the part_of_admin_tree object with internal name $name.
- *
- * @param string $name The internal name of the object we want.
- * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
- */
- function &locate($name) {
- $return = ($this->name == $name ? $this : NULL);
- return $return;
- }
-
- /**
- * Determines if the current user has access to this external page based on $this->role.
- *
- * @uses CONTEXT_SYSTEM
- * @uses SITEID
- * @return bool True if user has access, false otherwise.
- */
- function check_access() {
- if (!get_site()) {
- return true; // no access check before site is fully set up
- }
- $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
- return has_capability($this->role, $context);
- }
-
-}
-
-/**
- * Used to group a number of admin_setting objects into a page and add them to the admin tree.
- *
- * @author Vincenzo K. Marcovecchio
- * @package admin
- */
-class admin_settingpage extends part_of_admin_tree {
-
- /**
- * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
- */
- var $name;
-
- /**
- * @var string The displayed name for this external page. Usually obtained through get_string().
- */
- var $visiblename;
- /**
- * @var mixed An array of admin_setting objects that are part of this setting page.
- */
- var $settings;
-
- /**
- * @var string The role capability/permission a user must have to access this external page.
- */
- var $role;
-
- // see admin_category
- function path($name, $path = array()) {
- if ($name == $this->name) {
- array_push($path, $this->name);
- return $path;
- } else {
- return NULL;
- }
- }
-
- // see admin_category
- function &locate($name) {
- $return = ($this->name == $name ? $this : NULL);
- return $return;
- }
-
- // see admin_externalpage
- function admin_settingpage($name, $visiblename, $role = 'moodle/legacy:admin') {
- global $CFG;
- $this->settings = new stdClass();
- $this->name = $name;
- $this->visiblename = $visiblename;
- $this->role = $role;
- }
-
- // not the same as add for admin_category. adds an admin_setting to this admin_settingpage. settings appear (on the settingpage) in the order in which they're added
- // n.b. each admin_setting in an admin_settingpage must have a unique internal name
- // &$setting is the admin_setting object you want to add
- // returns true if successful, false if not (will fail if &$setting is an admin_setting or child thereof)
- function add(&$setting) {
- if (is_a($setting, 'admin_setting')) {
- $this->settings->{$setting->name} =& $setting;
- return true;
- }
- return false;
- }
-
- // see admin_externalpage
- function check_access() {
- if (!get_site()) {
- return true; // no access check before site is fully set up
- }
- $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
- return has_capability($this->role, $context);
- }
-
- // outputs this page as html in a table (suitable for inclusion in an admin pagetype)
- // returns a string of the html
- function output_html() {
- $return = '
\n";
echo "";
- admin_externalpage_print_footer();
+ admin_externalpage_print_footer($adminroot);
?>
\ No newline at end of file
diff --git a/admin/enrol_config.php b/admin/enrol_config.php
index 1bbc993910..aafd31a66d 100644
--- a/admin/enrol_config.php
+++ b/admin/enrol_config.php
@@ -3,9 +3,10 @@
// Yes, enrol is correct English spelling.
require_once("../config.php");
- require_once($CFG->dirroot . '/admin/adminlib.php');
+ require_once($CFG->libdir.'/adminlib.php');
- admin_externalpage_setup('enrolment');
+ $adminroot = admin_get_root();
+ admin_externalpage_setup('enrolment', $adminroot);
$enrol = required_param('enrol', PARAM_ALPHA);
$CFG->pagepath = 'enrol/' . $enrol;
@@ -41,7 +42,7 @@
}
asort($options);
- admin_externalpage_print_header();
+ admin_externalpage_print_header($adminroot);
echo "';
-admin_externalpage_print_footer();
+admin_externalpage_print_footer($adminroot);
diff --git a/backup/lib.php b/backup/lib.php
index 95c768c25c..f5f36627a8 100644
--- a/backup/lib.php
+++ b/backup/lib.php
@@ -319,7 +319,7 @@
/// This function upgrades the backup tables, if necessary
/// It's called from admin/index.php, also backup.php and restore.php
- global $CFG, $db, $ADMIN;
+ global $CFG, $db;
require_once ("$CFG->dirroot/backup/version.php"); // Get code versions
@@ -346,8 +346,8 @@
if ($status) {
if (set_config("backup_version", $backup_version) and set_config("backup_release", $backup_release)) {
//initialize default backup settings now
- require_once($CFG->dirroot . '/admin/adminlib.php');
- apply_default_settings($ADMIN->locate('backups'));
+ $adminroot = admin_get_root();
+ apply_default_settings($adminroot->locate('backups'));
notify(get_string("databasesuccess"), "green");
notify(get_string("databaseupgradebackups", "", $backup_version), "green");
print_continue($continueto);
diff --git a/blocks/admin/block_admin.php b/blocks/admin/block_admin.php
index 1101cbc800..d922617042 100644
--- a/blocks/admin/block_admin.php
+++ b/blocks/admin/block_admin.php
@@ -70,7 +70,7 @@ class block_admin extends block_list {
$this->content->items[] = ''.get_string('sitefiles').'';
$this->content->icons[] = '';
- if (file_exists($CFG->dirroot.'/'.$CFG->admin.'/'.$CFG->dbtype)) {
+ if (file_exists($CFG->dirroot.'/admin/'.$CFG->dbtype)) {
$this->content->items[] = ''.get_string('managedatabase').'';
$this->content->icons[] = '';
}
diff --git a/blocks/admin_2/block_admin_2.php b/blocks/admin_2/block_admin_2.php
index 523ab9d347..bb1ed89200 100644
--- a/blocks/admin_2/block_admin_2.php
+++ b/blocks/admin_2/block_admin_2.php
@@ -89,31 +89,29 @@ class block_admin_2 extends block_base {
global $CFG, $ADMIN;
- // better place to require this?
- if (!$ADMIN) {
- require_once($CFG->dirroot . '/admin/adminlib.php');
- }
+ require_once($CFG->libdir.'/adminlib.php');
+ $adminroot = admin_get_root();
if ($this->content !== NULL) {
return $this->content;
}
- if ($this->pathtosection = $ADMIN->path($this->section)) {
+ if ($this->pathtosection = $adminroot->path($this->section)) {
$this->pathtosection = array_reverse($this->pathtosection);
array_pop($this->pathtosection);
}
- // we need to do this instead of $this->build_tree($ADMIN) because the top-level folder
+ // we need to do this instead of $this->build_tree($adminroot) because the top-level folder
// is redundant (and ideally ignored). (the top-level folder is "administration".)
unset($entries);
- $entries = array_keys($ADMIN->children);
+ $entries = array_keys($adminroot->children);
asort($entries);
foreach ($entries as $entry) {
- $this->build_tree($ADMIN->children[$entry]);
+ $this->build_tree($adminroot->children[$entry]);
}
if ($this->tempcontent !== '') {
diff --git a/blocks/admin_bookmarks/block_admin_bookmarks.php b/blocks/admin_bookmarks/block_admin_bookmarks.php
index e6f273b1d2..fa7ae9cabe 100644
--- a/blocks/admin_bookmarks/block_admin_bookmarks.php
+++ b/blocks/admin_bookmarks/block_admin_bookmarks.php
@@ -21,11 +21,10 @@ class block_admin_bookmarks extends block_base {
function get_content() {
- global $CFG, $USER, $ADMIN, $PAGE;
+ global $CFG, $USER, $PAGE;
- if (!$ADMIN) {
- require_once($CFG->dirroot . '/admin/adminlib.php');
- }
+ require_once($CFG->libdir.'/adminlib.php');
+ $adminroot = admin_get_root();
if ($this->content !== NULL) {
return $this->content;
@@ -36,9 +35,9 @@ class block_admin_bookmarks extends block_base {
if (isset($USER->preference['admin_bookmarks'])) {
$bookmarks = explode(',',$USER->preference['admin_bookmarks']);
// hmm... just a liiitle (potentially) processor-intensive
- // (recall that $ADMIN->locate is a huge recursive call... and we're calling it repeatedly here
+ // (recall that $adminroot->locate is a huge recursive call... and we're calling it repeatedly here
foreach($bookmarks as $bookmark) {
- $temp = $ADMIN->locate($bookmark);
+ $temp = $adminroot->locate($bookmark);
if (is_a($temp, 'admin_settingpage')) {
$this->content->text .= '' . $temp->visiblename . '' . ' ';
} elseif (is_a($temp, 'admin_externalpage')) {
diff --git a/blocks/admin_bookmarks/create.php b/blocks/admin_bookmarks/create.php
index d66c81e48e..c47ef8ab08 100644
--- a/blocks/admin_bookmarks/create.php
+++ b/blocks/admin_bookmarks/create.php
@@ -2,7 +2,8 @@
require('../../config.php');
-require_once($CFG->dirroot . '/' . $CFG->admin . '/adminlib.php');
+require_once($CFG->libdir.'/adminlib.php');
+$adminroot = admin_get_root();
if ($section = optional_param('section', '', PARAM_ALPHAEXT)) {
@@ -18,7 +19,7 @@ if ($section = optional_param('section', '', PARAM_ALPHAEXT)) {
$bookmarks = array();
}
- $temp = $ADMIN->locate($section);
+ $temp = $adminroot->locate($section);
if (is_a($temp, 'admin_settingpage') || is_a($temp, 'admin_externalpage')) {
diff --git a/blocks/admin_bookmarks/delete.php b/blocks/admin_bookmarks/delete.php
index 22fd1860b4..9a8f7fd1ed 100644
--- a/blocks/admin_bookmarks/delete.php
+++ b/blocks/admin_bookmarks/delete.php
@@ -2,7 +2,8 @@
require('../../config.php');
-require_once($CFG->dirroot . '/' . $CFG->admin . '/adminlib.php');
+require_once($CFG->libdir.'/adminlib.php');
+$adminroot = admin_get_root();
if ($section = optional_param('section', '', PARAM_ALPHAEXT)) {
@@ -21,7 +22,7 @@ if ($section = optional_param('section', '', PARAM_ALPHAEXT)) {
$bookmarks = implode(',',$bookmarks);
set_user_preference('admin_bookmarks', $bookmarks);
- $temp = $ADMIN->locate($section);
+ $temp = $adminroot->locate($section);
if (is_a($temp, 'admin_externalpage')) {
redirect($temp->url, 'Bookmark deleted.',1);
diff --git a/course/category.php b/course/category.php
index ec0aff91da..e9708c043f 100644
--- a/course/category.php
+++ b/course/category.php
@@ -91,9 +91,10 @@
if ($adminediting) {
// modify this to treat this as an admin page
- require_once($CFG->dirroot . '/admin/adminlib.php');
- admin_externalpage_setup('coursemgmt');
- admin_externalpage_print_header();
+ require_once($CFG->libdir.'/adminlib.php');
+ $adminroot = admin_get_root();
+ admin_externalpage_setup('coursemgmt', $adminroot);
+ admin_externalpage_print_header($adminroot);
} else {
print_header("$site->shortname: $category->name", "$site->fullname: $strcourses",
"$strcategories -> $category->name", "", "", true, $navbaritem);
@@ -444,7 +445,7 @@
echo "";
if ($adminediting) {
- admin_externalpage_print_footer();
+ admin_externalpage_print_footer($adminroot);
} else {
print_footer();
}
diff --git a/course/index.php b/course/index.php
index ed57d7d0bd..93ff5b72ec 100644
--- a/course/index.php
+++ b/course/index.php
@@ -75,8 +75,9 @@
/// From now on is all the admin functions
- require_once($CFG->dirroot . '/admin/adminlib.php');
- admin_externalpage_setup('coursemgmt');
+ require_once($CFG->libdir.'/adminlib.php');
+ $adminroot = admin_get_root();
+ admin_externalpage_setup('coursemgmt', $adminroot);
/// Print headings
@@ -92,7 +93,7 @@
- admin_externalpage_print_header();
+ admin_externalpage_print_header($adminroot);
print_heading($strcategories);
@@ -323,7 +324,7 @@
echo " ";
echo "";
- admin_externalpage_print_footer();
+ admin_externalpage_print_footer($adminroot);
diff --git a/lib/adminlib.php b/lib/adminlib.php
index a36c1351f9..8e91b6ae1b 100644
--- a/lib/adminlib.php
+++ b/lib/adminlib.php
@@ -609,4 +609,1668 @@ function is_dataroot_insecure() {
}
return false;
}
+
+/// =============================================================================================================
+/// administration tree classes and functions
+
+
+// n.b. documentation is still in progress for this code
+
+/// INTRODUCTION
+
+/// This file performs the following tasks:
+/// -it defines the necessary objects and interfaces to build the Moodle
+/// admin hierarchy
+/// -it defines the admin_externalpage_setup(), admin_externalpage_print_header(),
+/// and admin_externalpage_print_footer() functions used on admin pages
+
+/// ADMIN_SETTING OBJECTS
+
+/// Moodle settings are represented by objects that inherit from the admin_setting
+/// class. These objects encapsulate how to read a setting, how to write a new value
+/// to a setting, and how to appropriately display the HTML to modify the setting.
+
+/// ADMIN_SETTINGPAGE OBJECTS
+
+/// The admin_setting objects are then grouped into admin_settingpages. The latter
+/// appear in the Moodle admin tree block. All interaction with admin_settingpage
+/// objects is handled by the admin/settings.php file.
+
+/// ADMIN_EXTERNALPAGE OBJECTS
+
+/// There are some settings in Moodle that are too complex to (efficiently) handle
+/// with admin_settingpages. (Consider, for example, user management and displaying
+/// lists of users.) In this case, we use the admin_externalpage object. This object
+/// places a link to an external PHP file in the admin tree block.
+
+/// If you're using an admin_externalpage object for some settings, you can take
+/// advantage of the admin_externalpage_* functions. For example, suppose you wanted
+/// to add a foo.php file into admin. First off, you add the following line to
+/// admin/settings/first.php (at the end of the file) or to some other file in
+/// admin/settings:
+
+/// $ADMIN->add('userinterface', new admin_externalpage('foo', get_string('foo'),
+/// $CFG->wwwdir . '/' . '$CFG->admin . '/foo.php', 'some_role_permission'));
+
+/// Next, in foo.php, your file structure would resemble the following:
+
+/// require_once('.../config.php');
+/// require_once($CFG->libdir.'/adminlib.php');
+/// $adminroot = admin_get_root();
+/// admin_externalpage_setup('foo', $adminroot);
+/// // functionality like processing form submissions goes here
+/// admin_externalpage_print_header($adminroot);
+/// // your HTML goes here
+/// admin_externalpage_print_footer($adminroot);
+
+/// The admin_externalpage_setup() function call ensures the user is logged in,
+/// and makes sure that they have the proper role permission to access the page.
+
+/// The admin_externalpage_print_header() function prints the header (it figures
+/// out what category and subcategories the page is classified under) and ensures
+/// that you're using the admin pagelib (which provides the admin tree block and
+/// the admin bookmarks block).
+
+/// The admin_externalpage_print_footer() function properly closes the tables
+/// opened up by the admin_externalpage_print_header() function and prints the
+/// standard Moodle footer.
+
+/// ADMIN_CATEGORY OBJECTS
+
+/// Above and beyond all this, we have admin_category objects. These objects
+/// appear as folders in the admin tree block. They contain admin_settingpage's,
+/// admin_externalpage's, and other admin_category's.
+
+/// OTHER NOTES
+
+/// admin_settingpage's, admin_externalpage's, and admin_category's all inherit
+/// from part_of_admin_tree (a pseudointerface). This interface insists that
+/// a class has a check_access method for access permissions, a locate method
+/// used to find a specific node in the admin tree, and a path method used
+/// to determine the path to a specific node in the $ADMIN tree.
+
+/// admin_category's inherit from parentable_part_of_admin_tree. This pseudo-
+/// interface ensures that the class implements a recursive add function which
+/// accepts a part_of_admin_tree object and searches for the proper place to
+/// put it. parentable_part_of_admin_tree implies part_of_admin_tree.
+
+/// Please note that the $this->name field of any part_of_admin_tree must be
+/// UNIQUE throughout the ENTIRE admin tree.
+
+/// The $this->name field of an admin_setting object (which is *not* part_of_
+/// admin_tree) must be unique on the respective admin_settingpage where it is
+/// used.
+
+
+/// MISCELLANEOUS STUFF (used by classes defined below) ///////////////////////
+include_once($CFG->dirroot . '/backup/lib.php');
+
+/// CLASS DEFINITIONS /////////////////////////////////////////////////////////
+
+/**
+ * Pseudointerface for anything appearing in the admin tree
+ *
+ * The pseudointerface that is implemented by anything that appears in the admin tree
+ * block. It forces inheriting classes to define a method for checking user permissions
+ * and methods for finding something in the admin tree.
+ *
+ * @author Vincenzo K. Marcovecchio
+ * @package admin
+ */
+class part_of_admin_tree {
+
+ /**
+ * Finds a named part_of_admin_tree.
+ *
+ * Used to find a part_of_admin_tree. If a class only inherits part_of_admin_tree
+ * and not parentable_part_of_admin_tree, then this function should only check if
+ * $this->name matches $name. If it does, it should return a reference to $this,
+ * otherwise, it should return a reference to NULL.
+ *
+ * If a class inherits parentable_part_of_admin_tree, this method should be called
+ * recursively on all child objects (assuming, of course, the parent object's name
+ * doesn't match the search criterion).
+ *
+ * @param string $name The internal name of the part_of_admin_tree we're searching for.
+ * @return mixed An object reference or a NULL reference.
+ */
+ function &locate($name) {
+ trigger_error('Admin class does not implement method locate()', E_USER_WARNING);
+ return;
+ }
+
+ /**
+ * Verifies current user's access to this part_of_admin_tree.
+ *
+ * Used to check if the current user has access to this part of the admin tree or
+ * not. If a class only inherits part_of_admin_tree and not parentable_part_of_admin_tree,
+ * then this method is usually just a call to has_capability() in the site context.
+ *
+ * If a class inherits parentable_part_of_admin_tree, this method should return the
+ * logical OR of the return of check_access() on all child objects.
+ *
+ * @return bool True if the user has access, false if she doesn't.
+ */
+ function check_access() {
+ trigger_error('Admin class does not implement method check_access()', E_USER_WARNING);
+ return;
+ }
+
+ /**
+ * Determines the path to $name in the admin tree.
+ *
+ * Used to determine the path to $name in the admin tree. If a class inherits only
+ * part_of_admin_tree and not parentable_part_of_admin_tree, then this method should
+ * check if $this->name matches $name. If it does, $name is pushed onto the $path
+ * array (at the end), and $path should be returned. If it doesn't, NULL should be
+ * returned.
+ *
+ * If a class inherits parentable_part_of_admin_tree, it should do the above, but not
+ * return NULL on failure. Instead, it pushes $this->name onto $path, and then
+ * recursively calls path() on its child objects. If any are non-NULL, it should
+ * return $path (being certain that the last element of $path is equal to $name).
+ * If they are all NULL, it returns NULL.
+ *
+ * @param string $name The internal name of the part_of_admin_tree we're searching for.
+ * @param array $path Not used on external calls. Defaults to empty array.
+ * @return mixed If found, an array containing the internal names of each part_of_admin_tree that leads to $name. If not found, NULL.
+ */
+ function path($name, $path = array()) {
+ trigger_error('Admin class does not implement method path()', E_USER_WARNING);
+ return;
+ }
+}
+
+/**
+ * Pseudointerface implemented by any part_of_admin_tree that has children.
+ *
+ * The pseudointerface implemented by any part_of_admin_tree that can be a parent
+ * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart
+ * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods
+ * include an add method for adding other part_of_admin_tree objects as children.
+ *
+ * @author Vincenzo K. Marcovecchio
+ * @package admin
+ */
+class parentable_part_of_admin_tree extends part_of_admin_tree {
+
+ /**
+ * Adds a part_of_admin_tree object to the admin tree.
+ *
+ * Used to add a part_of_admin_tree object to this object or a child of this
+ * object. $something should only be added if $destinationname matches
+ * $this->name. If it doesn't, add should be called on child objects that are
+ * also parentable_part_of_admin_tree's.
+ *
+ * @param string $destinationname The internal name of the new parent for $something.
+ * @param part_of_admin_tree &$something The object to be added.
+ * @return bool True on success, false on failure.
+ */
+ function add($destinationname, &$something) {
+ trigger_error('Admin class does not implement method add()', E_USER_WARNING);
+ return;
+ }
+
+}
+
+/**
+ * The object used to represent folders (a.k.a. categories) in the admin tree block.
+ *
+ * Each admin_category object contains a number of part_of_admin_tree objects.
+ *
+ * @author Vincenzo K. Marcovecchio
+ * @package admin
+ */
+class admin_category extends parentable_part_of_admin_tree {
+
+ /**
+ * @var mixed An array of part_of_admin_tree objects that are this object's children
+ */
+ var $children;
+
+ /**
+ * @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
+ */
+ var $name;
+
+ /**
+ * @var string The displayed name for this category. Usually obtained through get_string()
+ */
+ var $visiblename;
+
+ // constructor for an empty admin category
+ // $name is the internal name of the category. it MUST be unique in the entire hierarchy
+ // $visiblename is the displayed name of the category. use a get_string for this
+
+ /**
+ * Constructor for an empty admin category
+ *
+ * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
+ * @param string $visiblename The displayed named for this category. Usually obtained through get_string()
+ * @return mixed Returns the new object.
+ */
+ function admin_category($name, $visiblename) {
+ $this->children = array();
+ $this->name = $name;
+ $this->visiblename = $visiblename;
+ }
+
+ /**
+ * Finds the path to the part_of_admin_tree called $name.
+ *
+ * @param string $name The internal name that we're searching for.
+ * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
+ * @return mixed An array of internal names that leads to $name, or NULL if not found.
+ */
+ function path($name, $path = array()) {
+
+ $path[count($path)] = $this->name;
+
+ if ($this->name == $name) {
+ return $path;
+ }
+
+ foreach($this->children as $child) {
+ if ($return = $child->path($name, $path)) {
+ return $return;
+ }
+ }
+
+ return NULL;
+
+ }
+
+ /**
+ * Returns a reference to the part_of_admin_tree object with internal name $name.
+ *
+ * @param string $name The internal name of the object we want.
+ * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
+ */
+ function &locate($name) {
+
+ if ($this->name == $name) {
+ return $this;
+ }
+
+ foreach($this->children as $child) {
+ if ($return =& $child->locate($name)) {
+ return $return;
+ }
+ }
+ $return = NULL;
+ return $return;
+ }
+
+ /**
+ * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object.
+ *
+ * @param string $destinationame The internal name of the immediate parent that we want for &$something.
+ * @param mixed &$something A part_of_admin_tree object to be added.
+ * @param int $precedence The precedence of &$something when displayed. Smaller numbers mean it'll be displayed higher up in the admin menu. Defaults to '', meaning "next available position".
+ * @return bool True if successfully added, false if &$something is not a part_of_admin_tree or if $name is not found.
+ */
+ function add($destinationname, &$something, $precedence = '') {
+
+ if (!is_a($something, 'part_of_admin_tree')) {
+ return false;
+ }
+
+ if ($destinationname == $this->name) {
+ if ($precedence === '') {
+ $this->children[] = $something;
+ } else {
+ if (isset($this->children[$precedence])) { // this should never, ever be triggered in a release version of moodle.
+ echo ('There is a precedence conflict in the category ' . $this->name . '. The object named ' . $something->name . ' is overwriting the object named ' . $this->children[$precedence]->name . '. ');
+ }
+ $this->children[$precedence] = $something;
+ }
+ return true;
+ }
+
+ unset($entries);
+
+ $entries = array_keys($this->children);
+
+ foreach($entries as $entry) {
+ $child =& $this->children[$entry];
+ if (is_a($child, 'parentable_part_of_admin_tree')) {
+ if ($child->add($destinationname, $something, $precedence)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+
+ }
+
+ /**
+ * Checks if the user has access to anything in this category.
+ *
+ * @return bool True if the user has access to atleast one child in this category, false otherwise.
+ */
+ function check_access() {
+
+ $return = false;
+ foreach ($this->children as $child) {
+ $return = $return || $child->check_access();
+ }
+
+ return $return;
+
+ }
+
+}
+
+/**
+ * Links external PHP pages into the admin tree.
+ *
+ * See detailed usage example at the top of this document (adminlib.php)
+ *
+ * @author Vincenzo K. Marcovecchio
+ * @package admin
+ */
+class admin_externalpage extends part_of_admin_tree {
+
+ /**
+ * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
+ */
+ var $name;
+
+ /**
+ * @var string The displayed name for this external page. Usually obtained through get_string().
+ */
+ var $visiblename;
+
+ /**
+ * @var string The external URL that we should link to when someone requests this external page.
+ */
+ var $url;
+
+ /**
+ * @var string The role capability/permission a user must have to access this external page.
+ */
+ var $role;
+
+ /**
+ * Constructor for adding an external page into the admin tree.
+ *
+ * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
+ * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
+ * @param string $url The external URL that we should link to when someone requests this external page.
+ * @param string $role The role capability/permission a user must have to access this external page. Defaults to 'moodle/legacy:admin'.
+ */
+ function admin_externalpage($name, $visiblename, $url, $role = 'moodle/legacy:admin') {
+ $this->name = $name;
+ $this->visiblename = $visiblename;
+ $this->url = $url;
+ $this->role = $role;
+ }
+
+ /**
+ * Finds the path to the part_of_admin_tree called $name.
+ *
+ * @param string $name The internal name that we're searching for.
+ * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
+ * @return mixed An array of internal names that leads to $name, or NULL if not found.
+ */
+ function path($name, $path = array()) {
+ if ($name == $this->name) {
+ array_push($path, $this->name);
+ return $path;
+ } else {
+ return NULL;
+ }
+ }
+
+ /**
+ * Returns a reference to the part_of_admin_tree object with internal name $name.
+ *
+ * @param string $name The internal name of the object we want.
+ * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
+ */
+ function &locate($name) {
+ $return = ($this->name == $name ? $this : NULL);
+ return $return;
+ }
+
+ /**
+ * Determines if the current user has access to this external page based on $this->role.
+ *
+ * @uses CONTEXT_SYSTEM
+ * @uses SITEID
+ * @return bool True if user has access, false otherwise.
+ */
+ function check_access() {
+ if (!get_site()) {
+ return true; // no access check before site is fully set up
+ }
+ $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
+ return has_capability($this->role, $context);
+ }
+
+}
+
+/**
+ * Used to group a number of admin_setting objects into a page and add them to the admin tree.
+ *
+ * @author Vincenzo K. Marcovecchio
+ * @package admin
+ */
+class admin_settingpage extends part_of_admin_tree {
+
+ /**
+ * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
+ */
+ var $name;
+
+ /**
+ * @var string The displayed name for this external page. Usually obtained through get_string().
+ */
+ var $visiblename;
+ /**
+ * @var mixed An array of admin_setting objects that are part of this setting page.
+ */
+ var $settings;
+
+ /**
+ * @var string The role capability/permission a user must have to access this external page.
+ */
+ var $role;
+
+ // see admin_category
+ function path($name, $path = array()) {
+ if ($name == $this->name) {
+ array_push($path, $this->name);
+ return $path;
+ } else {
+ return NULL;
+ }
+ }
+
+ // see admin_category
+ function &locate($name) {
+ $return = ($this->name == $name ? $this : NULL);
+ return $return;
+ }
+
+ // see admin_externalpage
+ function admin_settingpage($name, $visiblename, $role = 'moodle/legacy:admin') {
+ global $CFG;
+ $this->settings = new stdClass();
+ $this->name = $name;
+ $this->visiblename = $visiblename;
+ $this->role = $role;
+ }
+
+ // not the same as add for admin_category. adds an admin_setting to this admin_settingpage. settings appear (on the settingpage) in the order in which they're added
+ // n.b. each admin_setting in an admin_settingpage must have a unique internal name
+ // &$setting is the admin_setting object you want to add
+ // returns true if successful, false if not (will fail if &$setting is an admin_setting or child thereof)
+ function add(&$setting) {
+ if (is_a($setting, 'admin_setting')) {
+ $this->settings->{$setting->name} =& $setting;
+ return true;
+ }
+ return false;
+ }
+
+ // see admin_externalpage
+ function check_access() {
+ if (!get_site()) {
+ return true; // no access check before site is fully set up
+ }
+ $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
+ return has_capability($this->role, $context);
+ }
+
+ // outputs this page as html in a table (suitable for inclusion in an admin pagetype)
+ // returns a string of the html
+ function output_html() {
+ $return = '