From d529807a6588142ac9bc0d8d5c3c9677a3124b39 Mon Sep 17 00:00:00 2001 From: tjhunt Date: Wed, 6 May 2009 08:38:55 +0000 Subject: [PATCH] moodle_page: MDL-12212 kill $CFG->pagepath (use $PAGE->set_pagetype) deprecate $PAGE->get_format_name (use $PAGE->pagetype) deprecate page_id_and_class (use $PAGE->pagetype) --- admin/auth_config.php | 2 +- admin/mnet/enr_course_enrol.php | 2 +- admin/mnet/enr_courses.php | 2 +- admin/mnet/enr_hosts.php | 3 +- admin/stickyblocks.php | 2 +- blog/blogpage.php | 9 -- course/mod.php | 2 +- course/modedit.php | 7 +- lib/blocklib.php | 8 +- lib/pagelib.php | 129 ++++++++++++++++----------- lib/weblib.php | 44 +++------ login/index.php | 5 +- mod/data/field.php | 13 +-- mod/hotpot/view.php | 7 +- mod/lesson/action/addbranchtable.php | 2 - mod/lesson/action/addpage.php | 2 - my/pagelib.php | 10 +-- question/question.php | 2 +- tag/pagelib.php | 6 +- 19 files changed, 115 insertions(+), 142 deletions(-) diff --git a/admin/auth_config.php b/admin/auth_config.php index 0ceca51e79..542a56463b 100644 --- a/admin/auth_config.php +++ b/admin/auth_config.php @@ -8,7 +8,7 @@ require_once $CFG->libdir.'/adminlib.php'; $auth = required_param('auth', PARAM_SAFEDIR); -$CFG->pagepath = 'auth/' . $auth; +$PAGE->set_pagetype('auth-' . $auth); admin_externalpage_setup('authsetting'.$auth); diff --git a/admin/mnet/enr_course_enrol.php b/admin/mnet/enr_course_enrol.php index 52dbae7442..7cfb03e812 100644 --- a/admin/mnet/enr_course_enrol.php +++ b/admin/mnet/enr_course_enrol.php @@ -11,7 +11,7 @@ } admin_externalpage_setup('mnetenrol'); - $CFG->pagepath = 'admin/mnet'; + $PAGE->set_pagetype('admin-mnet'); require_once("$CFG->dirroot/enrol/enrol.class.php"); /// Open the factory class $enrolment = enrolment_factory::factory('mnet'); diff --git a/admin/mnet/enr_courses.php b/admin/mnet/enr_courses.php index cd16a7dd0a..0d9fd8d0b2 100644 --- a/admin/mnet/enr_courses.php +++ b/admin/mnet/enr_courses.php @@ -11,7 +11,7 @@ admin_externalpage_setup('mnetenrol'); - $CFG->pagepath = 'admin/mnet'; + $PAGE->set_pagetype('admin-mnet'); require_once("$CFG->dirroot/enrol/enrol.class.php"); /// Open the factory class $enrolment = enrolment_factory::factory('mnet'); diff --git a/admin/mnet/enr_hosts.php b/admin/mnet/enr_hosts.php index 23f47d0c81..3a7cdfb81b 100644 --- a/admin/mnet/enr_hosts.php +++ b/admin/mnet/enr_hosts.php @@ -6,8 +6,7 @@ require_once($CFG->libdir.'/adminlib.php'); admin_externalpage_setup('mnetenrol'); - $CFG->pagepath = 'admin/mnet'; - + $PAGE->set_pagetype('admin-mnet'); require_once("$CFG->dirroot/enrol/enrol.class.php"); /// Open the factory class diff --git a/admin/stickyblocks.php b/admin/stickyblocks.php index c2aefae6e9..8b9eaf07ae 100644 --- a/admin/stickyblocks.php +++ b/admin/stickyblocks.php @@ -40,7 +40,7 @@ define('ADMIN_STICKYBLOCKS',$pt); $PAGE = page_create_object($pt, SITEID); - $blocks = blocks_setup($PAGE,BLOCKS_PINNED_TRUE); + $blocks = blocks_setup($PAGE, BLOCKS_PINNED_TRUE); $blocks_preferred_width = bounded_number(180, blocks_preferred_width($blocks[BLOCK_POS_LEFT]), 210); $navlinks = array(array('name' => get_string('administration'), diff --git a/blog/blogpage.php b/blog/blogpage.php index 43839a9dfe..67c0111308 100644 --- a/blog/blogpage.php +++ b/blog/blogpage.php @@ -17,15 +17,6 @@ class page_blog extends page_base { var $filterselect = NULL; var $tagid = NULL; - // we have no format type, use 'blog' - //I think it's a bug, but if this is left the default NULL value then pages can - //fail to load completely - function get_format_name() { - global $CFG; - require_once($CFG->dirroot .'/blog/lib.php'); - return PAGE_BLOG_VIEW; - } - // 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) { diff --git a/course/mod.php b/course/mod.php index a01b065eaf..054e5b30a7 100644 --- a/course/mod.php +++ b/course/mod.php @@ -63,7 +63,7 @@ $strdeletecheck = get_string('deletecheck', '', $fullmodulename); $strdeletecheckfull = get_string('deletecheckfull', '', "$fullmodulename '$cm->name'"); - $CFG->pagepath = 'mod/'.$cm->modname.'/delete'; + $PAGE->set_pagetype('mod-' . $cm->modname . '-delete'); print_header_simple($strdeletecheck, '', build_navigation(array(array('name'=>$strdeletecheck, 'link'=>'', 'type'=>'misc')))); diff --git a/course/modedit.php b/course/modedit.php index 0c8c6adc81..a68de8b6ae 100644 --- a/course/modedit.php +++ b/course/modedit.php @@ -80,12 +80,13 @@ $pageheading = get_string('addinganew', 'moodle', $fullmodulename); } - $CFG->pagepath = 'mod/'.$module->name; + $pagepath = 'mod-' . $module->name . '-'; if (!empty($type)) { - $CFG->pagepath .= '/'.$type; + $pagepath .= $type; } else { - $CFG->pagepath .= '/mod'; + $pagepath .= 'mod'; } + $PAGE->set_pagetype($pagepath); $navlinksinstancename = ''; diff --git a/lib/blocklib.php b/lib/blocklib.php index 9f7273155f..5323c72e19 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -94,7 +94,7 @@ function blocks_get_missing(&$page, &$pageblocks) { $missingblocks = array(); $allblocks = blocks_get_record(); - $pageformat = $page->get_format_name(); + $pageformat = $page->pagetype; if(!empty($allblocks)) { foreach($allblocks as $block) { @@ -117,7 +117,7 @@ function blocks_remove_inappropriate($page) { return; } - if(($pageformat = $page->get_format_name()) == NULL) { + if(($pageformat = $page->pagetype) == NULL) { return; } @@ -325,10 +325,10 @@ function blocks_print_group(&$page, &$pageblocks, $position) { include_once($CFG->dirroot.'/my/pagelib.php'); $coursecontext = get_context_instance(CONTEXT_COURSE, $COURSE->id); - $myownblogpage = (isset($page->filtertype) && isset($page->filterselect) && $page->type=='blog-view' && $page->filtertype=='user' && $page->filterselect == $USER->id); + $myownblogpage = (isset($page->filtertype) && isset($page->filterselect) && $page->pagetype=='blog-view' && $page->filtertype=='user' && $page->filterselect == $USER->id); $managecourseblocks = has_capability('moodle/site:manageblocks', $coursecontext); - $editmymoodle = $page->type == PAGE_MY_MOODLE && has_capability('moodle/my:manageblocks', $coursecontext); + $editmymoodle = $page->pagetype == PAGE_MY_MOODLE && has_capability('moodle/my:manageblocks', $coursecontext); if ($page->blocks_default_position() == $position && $page->user_is_editing() && diff --git a/lib/pagelib.php b/lib/pagelib.php index 99ea6139c5..5eb552b0c4 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -56,6 +56,8 @@ class moodle_page { protected $_pagetype = null; + protected $_legacyclass = null; + /** * @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 @@ -102,8 +104,8 @@ class moodle_page { * @return string e.g. 'my-index' or 'mod-quiz-attempt'. Same as the id attribute on . */ public function get_pagetype() { - if (is_null($this->_pagetype)) { - throw new coding_exception('$PAGE->pagetype accessed before it was known.'); + if (is_null($this->_pagetype) || isset($CFG->pagepath)) { + $this->initialise_default_pagetype(); } return $this->_pagetype; } @@ -193,7 +195,46 @@ class moodle_page { } } + /** + * 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); + } + } + /// Deperecated fields and methods for backwards compatibility ================= + /** * @deprecated since Moodle 2.0 - use $PAGE->pagetype instead. * @return string page type. @@ -202,6 +243,15 @@ class moodle_page { debugging('Call to deprecated method moodle_page::get_type. Please use $PAGE->pagetype instead.'); return $this->get_pagetype(); } + + /** + * @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(); + } + /** * @deprecated since Moodle 2.0 - use $PAGE->course instead. * @return object course. @@ -210,6 +260,25 @@ class moodle_page { debugging('Call to deprecated method moodle_page::get_courserecord. Please use $PAGE->course instead.'); return $this->get_course(); } + + /** + * @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)) { + $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, '-')); + } + } + debugging('Call to deprecated method moodle_page::get_legacyclass.'); + return $this->_legacyclass; + } } /** @@ -220,17 +289,16 @@ class moodle_page { */ function page_import_types($path) { global $CFG; - debugging('Call to deprecated function page_import_types.', DEBUG_DEVELOPER); } /** - * 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. + * @deprecated since Moodle 2.0 + * @param integer $instance legacy page instance id. + * @return the global $PAGE object. */ function page_create_instance($instance) { - page_id_and_class($id, $class); - return page_create_object($id, $instance); + return page_create_object($PAGE->pagetype, $instance); } /** @@ -309,32 +377,8 @@ class page_base extends moodle_page { */ var $full_init_done = false; - /** - * 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; - /// Class Functions - // 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() { - page_id_and_class($this->body_id, $this->body_class); - } - // USER-RELATED THINGS // By default, no user is editing anything and none CAN edit anything. Developers @@ -430,11 +474,6 @@ class page_base extends moodle_page { return $this->id; } - // "Sensible default" case here. Take it from the body id. - function get_format_name() { - return $this->body_id; - } - // Initialize the data members of the parent class function init_quick($data) { $this->type = $data->pagetype; @@ -503,7 +542,6 @@ class page_course extends page_base { // this is a _very_ expensive check - so cache it during execution // function user_allowed_editing() { - $this->init_full(); if (isset($this->_user_allowed_editing)) { @@ -511,7 +549,7 @@ class page_course extends page_base { } if (has_capability('moodle/site:manageblocks', get_context_instance(CONTEXT_SYSTEM)) - && defined('ADMIN_STICKYBLOCKS')) { + && defined('ADMIN_STICKYBLOCKS')) { $this->_user_allowed_editing = true; return true; } @@ -586,21 +624,6 @@ class page_course extends page_base { // SELF-REPORTING SECTION - // 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 - // the format_name might be that activity's name etc. - function get_format_name() { - $this->init_full(); - if (defined('ADMIN_STICKYBLOCKS')) { - return PAGE_COURSE_VIEW; - } - if($this->id == SITEID) { - return parent::get_format_name(); - } - // This needs to reflect the path hierarchy under Moodle root. - return 'course-view-'.$this->course->format; - } - // This should return a fully qualified path to the URL which is responsible for displaying us. function url_get_path() { global $CFG; diff --git a/lib/weblib.php b/lib/weblib.php index 284db8b4bc..cac8859079 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -2327,7 +2327,8 @@ function print_header ($title='', $heading='', $navigation='', $focus='', // Create class and id for this page - page_id_and_class($pageid, $pageclass); + $pageid = $PAGE->pagetype; + $pageclass = $PAGE->legacyclass; $pageclass .= ' course-'.$COURSE->id; @@ -6493,39 +6494,16 @@ function print_side_block_end($attributes = array(), $title='') { } - +/** + * @deprecated since Moodle 2.0 - use $PAGE->pagetype instead of the . + * @param string $getid used to return $PAGE->pagetype. + * @param string $getclass used to return $PAGE->legacyclass. + */ function page_id_and_class(&$getid, &$getclass) { - // Create class and id for this page - global $CFG, $SCRIPT; - - static $class = NULL; - static $id = NULL; - - if (empty($CFG->pagepath)) { - $CFG->pagepath = ltrim($SCRIPT, '/'); - } - - if (empty($class) || empty($id)) { - $path = str_replace('.php', '', $CFG->pagepath); - if (substr($path, -1) == '/') { - $path .= 'index'; - } - if (empty($path) || $path == 'index') { - $id = 'site-index'; - $class = 'course'; - } else if (substr($path, 0, 5) == 'admin') { - $id = str_replace('/', '-', $path); - $class = 'admin'; - } else { - $id = str_replace('/', '-', $path); - $class = explode('-', $id); - array_pop($class); - $class = implode('-', $class); - } - } - - $getid = $id; - $getclass = $class; + global $PAGE; + debugging('Call to deprecated function page_id_and_class. Please use $PAGE->pagetype instead.', DEBUG_DEVELOPER); + $getid = $PAGE->pagetype; + $getclass = $PAGE->legacyclass; } /** diff --git a/login/index.php b/login/index.php index b4c1b81d51..da4d187620 100644 --- a/login/index.php +++ b/login/index.php @@ -12,6 +12,9 @@ $loginguest = optional_param('loginguest', 0, PARAM_BOOL); // determines whether visitors are logged in as guest automatically $testcookies = optional_param('testcookies', 0, PARAM_BOOL); // request cookie test + $context = get_context_instance(CONTEXT_SYSTEM); + $PAGE->set_context($context); + //initialize variables $errormsg = ''; $errorcode = 0; @@ -159,7 +162,7 @@ } /// Go to my-moodle page instead of homepage if mymoodleredirect enabled - if (!has_capability('moodle/site:config',get_context_instance(CONTEXT_SYSTEM)) and !empty($CFG->mymoodleredirect) and !isguest()) { + if (!has_capability('moodle/site:config', $context) and !empty($CFG->mymoodleredirect) and !isguest()) { if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') { $urltogo = $CFG->wwwroot.'/my/'; } diff --git a/mod/data/field.php b/mod/data/field.php index 7ba49e9bd7..3f64edf8f7 100755 --- a/mod/data/field.php +++ b/mod/data/field.php @@ -229,26 +229,21 @@ } asort($menufield); //sort in alphabetical order - + $PAGE->set_pagetype('mod-data-field-' . $newtype); if (($mode == 'new') && (!empty($newtype)) && confirm_sesskey()) { /// Adding a new field - $CFG->pagepath='mod/data/field/'.$newtype; - data_print_header($course,$cm,$data,'fields'); + data_print_header($course, $cm, $data,'fields'); $field = data_get_field_new($newtype, $data); $field->display_edit_field(); } else if ($mode == 'display' && confirm_sesskey()) { /// Display/edit existing field - $CFG->pagepath='mod/data/field/'.$newtype; - data_print_header($course,$cm,$data,'fields'); + data_print_header($course, $cm, $data,'fields'); $field = data_get_field_from_id($fid, $data); $field->display_edit_field(); } else { /// Display the main listing of all fields - - $CFG->pagepath='mod/data/field/'.$newtype; - data_print_header($course,$cm,$data,'fields'); - + data_print_header($course, $cm, $data,'fields'); if (!$DB->record_exists('data_fields', array('dataid'=>$data->id))) { notify(get_string('nofieldindatabase','data')); // nothing in database diff --git a/mod/hotpot/view.php b/mod/hotpot/view.php index 49033d6818..909a2aefd6 100644 --- a/mod/hotpot/view.php +++ b/mod/hotpot/view.php @@ -264,11 +264,8 @@ // insert hot-potatoes.js $hp->insert_script(HOTPOT_JS); // get Moodle pageid and pageclass - $pageid = ''; - $pageclass = ''; - if (function_exists('page_id_and_class')) { - page_id_and_class($pageid, $pageclass); - } + $pageid = $PAGE->pagetype; + // extract first tag $head = ''; $pattern = '|]*)>(.*?)|is'; diff --git a/mod/lesson/action/addbranchtable.php b/mod/lesson/action/addbranchtable.php index ae518e9172..b7e7309a79 100644 --- a/mod/lesson/action/addbranchtable.php +++ b/mod/lesson/action/addbranchtable.php @@ -7,8 +7,6 @@ * @package lesson **/ - $CFG->pagepath = 'mod/lesson/addbranchtable'; - // first get the preceeding page $pageid = required_param('pageid', PARAM_INT); diff --git a/mod/lesson/action/addpage.php b/mod/lesson/action/addpage.php index 10229e1f3e..3093d460c1 100644 --- a/mod/lesson/action/addpage.php +++ b/mod/lesson/action/addpage.php @@ -6,8 +6,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @package lesson **/ - $CFG->pagepath = 'mod/lesson/addpage'; - // first get the preceeding page $pageid = required_param('pageid', PARAM_INT); $qtype = optional_param('qtype', LESSON_MULTICHOICE, PARAM_INT); diff --git a/my/pagelib.php b/my/pagelib.php index ce7cdd4d94..d6e2b9f76f 100644 --- a/my/pagelib.php +++ b/my/pagelib.php @@ -5,8 +5,7 @@ require_once($CFG->libdir.'/pagelib.php'); class page_my_moodle extends page_base { function user_allowed_editing() { - page_id_and_class($id,$class); - if ($id == PAGE_MY_MOODLE) { + if ($PAGE->pagetype == PAGE_MY_MOODLE) { return true; } else if (has_capability('moodle/my:manageblocks', get_context_instance(CONTEXT_SYSTEM)) && defined('ADMIN_STICKYBLOCKS')) { return true; @@ -58,8 +57,7 @@ class page_my_moodle extends page_base { function url_get_path() { global $CFG; - page_id_and_class($id,$class); - if ($id == PAGE_MY_MOODLE) { + if ($PAGE->pagetype == PAGE_MY_MOODLE) { return $CFG->wwwroot.'/my/index.php'; } elseif (defined('ADMIN_STICKYBLOCKS')){ return $CFG->wwwroot.'/'.$CFG->admin.'/stickyblocks.php'; @@ -90,10 +88,6 @@ class page_my_moodle extends page_base { } return $instance->position; } - - function get_format_name() { - return MY_MOODLE_FORMAT; - } } diff --git a/question/question.php b/question/question.php index 2e0b2ca313..9148d1a952 100644 --- a/question/question.php +++ b/question/question.php @@ -115,7 +115,7 @@ if ($id) { if (!isset($QTYPES[$question->qtype])) { print_error('unknownquestiontype', 'question', $returnurl, $question->qtype); } -$CFG->pagepath = 'question/type/' . $question->qtype; +$PAGE->set_pagetype('question-type-' . $question->qtype); // Create the question editing form. if ($wizardnow!=='' && !$movecontext){ diff --git a/tag/pagelib.php b/tag/pagelib.php index c1611d2b95..27a1306df8 100644 --- a/tag/pagelib.php +++ b/tag/pagelib.php @@ -51,12 +51,8 @@ class page_tag extends page_base { return $instance->position; } - function get_format_name() { - return TAG_FORMAT; - } - //----------- printing funtions ----------- - + function print_header() { global $USER, $CFG; -- 2.39.5