diff --git a/lib/accesslib.php b/lib/accesslib.php index feac94d9be3..8604f487f40 100644 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -717,7 +717,7 @@ function has_capability_in_accessdata($capability, context $context, array &$acc * @see has_capability() * * @param string $capability the name of the capability to check. For example mod/forum:view - * @param context $context the context to check the capability in. You normally get this with {@link get_context_instance}. + * @param context $context the context to check the capability in. You normally get this with context_xxxx::instance(). * @param int $userid A user id. By default (null) checks the permissions of the current user. * @param bool $doanything If false, ignore effect of admin role assignment * @param string $errormessage The error string to to user. Defaults to 'nopermissions'. @@ -7265,36 +7265,6 @@ function get_system_context($cache = true) { return context_system::instance(0, IGNORE_MISSING, $cache); } -/** - * Get the context instance as an object. This function will create the - * context instance if it does not exist yet. - * - * @deprecated since 2.2, use context_course::instance() or other relevant class instead - * @param integer $contextlevel The context level, for example CONTEXT_COURSE, or CONTEXT_MODULE. - * @param integer $instance The instance id. For $level = CONTEXT_COURSE, this would be $course->id, - * for $level = CONTEXT_MODULE, this would be $cm->id. And so on. Defaults to 0 - * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; - * MUST_EXIST means throw exception if no record or multiple records found - * @return context The context object. - */ -function get_context_instance($contextlevel, $instance = 0, $strictness = IGNORE_MISSING) { - $instances = (array)$instance; - $contexts = array(); - - $classname = context_helper::get_class_for_level($contextlevel); - - // we do not load multiple contexts any more, PAGE should be responsible for any preloading - foreach ($instances as $inst) { - $contexts[$inst] = $classname::instance($inst, $strictness); - } - - if (is_array($instance)) { - return $contexts; - } else { - return $contexts[$instance]; - } -} - /** * Get a context instance as an object, from a given context id. * diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 05a282a6594..50a7baa6178 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -4849,3 +4849,37 @@ function can_use_rotated_text() { debugging('can_use_rotated_text() is deprecated since Moodle 2.5. JS feature detection is used automatically.', DEBUG_DEVELOPER); return true; } + +/** + * Get the context instance as an object. This function will create the + * context instance if it does not exist yet. + * + * @deprecated since 2.2, use context_course::instance() or other relevant class instead + * @todo This will be deleted in Moodle 2.8, refer MDL-34472 + * @param integer $contextlevel The context level, for example CONTEXT_COURSE, or CONTEXT_MODULE. + * @param integer $instance The instance id. For $level = CONTEXT_COURSE, this would be $course->id, + * for $level = CONTEXT_MODULE, this would be $cm->id. And so on. Defaults to 0 + * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; + * MUST_EXIST means throw exception if no record or multiple records found + * @return context The context object. + */ +function get_context_instance($contextlevel, $instance = 0, $strictness = IGNORE_MISSING) { + + debugging('get_context_instance() is deprecated, please use respective context_xxxx::instance().', DEBUG_DEVELOPER); + + $instances = (array)$instance; + $contexts = array(); + + $classname = context_helper::get_class_for_level($contextlevel); + + // we do not load multiple contexts any more, PAGE should be responsible for any preloading + foreach ($instances as $inst) { + $contexts[$inst] = $classname::instance($inst, $strictness); + } + + if (is_array($instance)) { + return $contexts; + } else { + return $contexts[$instance]; + } +} diff --git a/lib/pagelib.php b/lib/pagelib.php index 8ebb63bff2c..556a9dfc345 100644 --- a/lib/pagelib.php +++ b/lib/pagelib.php @@ -899,7 +899,7 @@ class moodle_page { /** * Set the main context to which this page belongs. * - * @param context $context a context object, normally obtained with get_context_instance. + * @param context $context a context object. You normally get this with context_xxxx::instance(). */ public function set_context($context) { if ($context === null) { diff --git a/lib/tests/accesslib_test.php b/lib/tests/accesslib_test.php index 2b8d6e3474a..2007ceee565 100644 --- a/lib/tests/accesslib_test.php +++ b/lib/tests/accesslib_test.php @@ -2367,6 +2367,10 @@ class accesslib_testcase extends advanced_testcase { $this->assertEquals(count($children), $DB->count_records('context')-1); unset($children); + // Make sure a debugging is thrown. + get_context_instance($record->contextlevel, $record->instanceid); + $this->assertDebuggingCalled('get_context_instance() is deprecated, please use respective context_xxxx::instance().', DEBUG_DEVELOPER); + $DB->delete_records('context', array('contextlevel'=>CONTEXT_BLOCK)); create_contexts(); $this->assertFalse($DB->record_exists('context', array('contextlevel'=>CONTEXT_BLOCK))); diff --git a/lib/upgrade.txt b/lib/upgrade.txt index b10aa3dfe55..7c5f2c61d67 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -4,7 +4,7 @@ information provided here is intended especially for developers. === 2.6 === * Use new core_component::* plugin listing and component normalisation methods. * Use core_text::* instead of textlib:: and also core_collator::* instead of collatorlib::*. - +* get_context_instance() now throws deprecated warning. === 2.5.1 === diff --git a/mod/assign/feedback/offline/locallib.php b/mod/assign/feedback/offline/locallib.php index b1fd7b9c03b..1a5838b900d 100644 --- a/mod/assign/feedback/offline/locallib.php +++ b/mod/assign/feedback/offline/locallib.php @@ -80,7 +80,7 @@ class assign_feedback_offline extends assign_feedback_plugin { $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment); - $context = get_context_instance(CONTEXT_USER, $USER->id); + $context = context_user::instance($USER->id); $fs = get_file_storage(); if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $draftid, 'id DESC', false)) { redirect(new moodle_url('view.php', diff --git a/user/lib.php b/user/lib.php index 560e3112580..357facb110c 100644 --- a/user/lib.php +++ b/user/lib.php @@ -527,11 +527,11 @@ function user_get_user_details_courses($user) { */ function can_view_user_details_cap($user, $course = null) { // Check $USER has the capability to view the user details at user context. - $usercontext = get_context_instance(CONTEXT_USER, $user->id); + $usercontext = context_user::instance($user->id); $result = has_capability('moodle/user:viewdetails', $usercontext); // Otherwise can $USER see them at course context. if (!$result && !empty($course)) { - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); $result = has_capability('moodle/user:viewdetails', $context); } return $result;