From a689cd1def7d6bc774f86de33439b306068f5021 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Tue, 24 Jul 2012 10:26:39 +0800 Subject: [PATCH] MDL-34469 - lib - replacement of the deprecated get_context_instance and get_context_by_id functions (group 15) --- backup/backup.php | 6 +++--- backup/backupfilesedit.php | 2 +- backup/converter/imscc11/backuplib.php | 2 +- backup/import.php | 4 ++-- backup/log.php | 2 +- backup/moodle2/backup_activity_task.class.php | 2 +- backup/moodle2/backup_block_task.class.php | 4 ++-- backup/moodle2/backup_course_task.class.php | 2 +- backup/moodle2/backup_final_task.class.php | 2 +- backup/moodle2/backup_section_task.class.php | 2 +- backup/moodle2/backup_stepslib.php | 10 +++++----- backup/moodle2/restore_course_task.class.php | 2 +- backup/moodle2/restore_section_task.class.php | 2 +- backup/moodle2/restore_stepslib.php | 16 +++++++-------- backup/restorefile.php | 8 ++++---- backup/util/checks/backup_check.class.php | 4 ++-- backup/util/checks/restore_check.class.php | 2 +- backup/util/dbops/backup_plan_dbops.class.php | 6 +++--- backup/util/dbops/restore_dbops.class.php | 20 +++++++++---------- .../util/helper/backup_cron_helper.class.php | 4 ++-- backup/util/helper/backup_helper.class.php | 10 +++++----- .../helper/restore_prechecks_helper.class.php | 2 +- .../plan/restore_structure_step.class.php | 2 +- backup/util/ui/backup_ui_stage.class.php | 4 ++-- backup/util/ui/renderer.php | 10 +++++----- backup/util/ui/restore_ui_components.php | 3 ++- backup/util/ui/restore_ui_stage.class.php | 6 +++--- login/change_password.php | 4 ++-- login/confirm.php | 2 +- login/forgot_password.php | 2 +- login/index.php | 2 +- login/logout.php | 2 +- login/signup.php | 2 +- login/token.php | 6 +++--- 34 files changed, 80 insertions(+), 79 deletions(-) diff --git a/backup/backup.php b/backup/backup.php index ef0a897590c..1f80265845f 100644 --- a/backup/backup.php +++ b/backup/backup.php @@ -56,11 +56,11 @@ require_login($course, false, $cm); switch ($type) { case backup::TYPE_1COURSE : - require_capability('moodle/backup:backupcourse', get_context_instance(CONTEXT_COURSE, $course->id)); + require_capability('moodle/backup:backupcourse', context_course::instance($course->id)); $heading = get_string('backupcourse', 'backup', $course->shortname); break; case backup::TYPE_1SECTION : - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $coursecontext = context_course::instance($course->id); require_capability('moodle/backup:backupsection', $coursecontext); if ((string)$section->name !== '') { $sectionname = format_string($section->name, true, array('context' => $coursecontext)); @@ -72,7 +72,7 @@ switch ($type) { } break; case backup::TYPE_1ACTIVITY : - require_capability('moodle/backup:backupactivity', get_context_instance(CONTEXT_MODULE, $cm->id)); + require_capability('moodle/backup:backupactivity', context_module::instance($cm->id)); $heading = get_string('backupactivity', 'backup', $cm->name); break; default : diff --git a/backup/backupfilesedit.php b/backup/backupfilesedit.php index 0059bbc0f3b..6b81b7d1061 100644 --- a/backup/backupfilesedit.php +++ b/backup/backupfilesedit.php @@ -36,7 +36,7 @@ $filearea = optional_param('filearea', null, PARAM_AREA); $returnurl = optional_param('returnurl', null, PARAM_URL); list($context, $course, $cm) = get_context_info_array($currentcontext); -$filecontext = get_context_instance_by_id($contextid); +$filecontext = context::instance_by_id($contextid, IGNORE_MISSING); $url = new moodle_url('/backup/backupfilesedit.php', array('currentcontext'=>$currentcontext, 'contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea)); diff --git a/backup/converter/imscc11/backuplib.php b/backup/converter/imscc11/backuplib.php index 81b9657fee5..ef876003735 100644 --- a/backup/converter/imscc11/backuplib.php +++ b/backup/converter/imscc11/backuplib.php @@ -76,7 +76,7 @@ class imscc11_store_backup_file extends backup_execution_step { $id = $dinfo[0]->id; // Id of activity/section/course (depends of type) $courseid = $dinfo[0]->courseid; // Id of the course - $ctxid = get_context_instance(CONTEXT_USER, $userid)->id; + $ctxid = context_user::instance($userid)->id; $component = 'user'; $filearea = 'backup'; $itemid = 0; diff --git a/backup/import.php b/backup/import.php index d205a64f59c..79096c07ad5 100644 --- a/backup/import.php +++ b/backup/import.php @@ -16,7 +16,7 @@ $restoretarget = optional_param('target', backup::TARGET_CURRENT_ADDING, PARAM_I // Load the course and context $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST); -$context = get_context_instance(CONTEXT_COURSE, $courseid); +$context = context_course::instance($courseid); // Must pass login require_login($course); @@ -50,7 +50,7 @@ if ($importcourseid === false) { // Load the course +context to import from $importcourse = $DB->get_record('course', array('id'=>$importcourseid), '*', MUST_EXIST); -$importcontext = get_context_instance(CONTEXT_COURSE, $importcourseid); +$importcontext = context_course::instance($importcourseid); // Make sure the user can backup from that course require_capability('moodle/backup:backuptargetimport', $importcontext); diff --git a/backup/log.php b/backup/log.php index 470bbb5376c..7cb17f8f090 100644 --- a/backup/log.php +++ b/backup/log.php @@ -6,6 +6,6 @@ require_login(); - require_capability('moodle/backup:backupcourse', get_context_instance(CONTEXT_SYSTEM)); + require_capability('moodle/backup:backupcourse', context_system::instance()); redirect("$CFG->wwwroot/report/backups/index.php", '', 'admin', 1); diff --git a/backup/moodle2/backup_activity_task.class.php b/backup/moodle2/backup_activity_task.class.php index fef94cc02a9..a5974259534 100644 --- a/backup/moodle2/backup_activity_task.class.php +++ b/backup/moodle2/backup_activity_task.class.php @@ -64,7 +64,7 @@ abstract class backup_activity_task extends backup_task { $this->sectionid = $coursemodule->section; $this->modulename = $coursemodule->modname; $this->activityid = $coursemodule->instance; - $this->contextid = get_context_instance(CONTEXT_MODULE, $this->moduleid)->id; + $this->contextid = context_module::instance($this->moduleid)->id; parent::__construct($name, $plan); } diff --git a/backup/moodle2/backup_block_task.class.php b/backup/moodle2/backup_block_task.class.php index 91e02537816..3a1fcdd9448 100644 --- a/backup/moodle2/backup_block_task.class.php +++ b/backup/moodle2/backup_block_task.class.php @@ -53,7 +53,7 @@ abstract class backup_block_task extends backup_task { $this->blockid = $blockid; $this->blockname = $block->blockname; - $this->contextid = get_context_instance(CONTEXT_BLOCK, $this->blockid)->id; + $this->contextid = context_block::instance($this->blockid)->id; $this->moduleid = $moduleid; $this->modulename = null; $this->parentcontextid = null; @@ -71,7 +71,7 @@ abstract class backup_block_task extends backup_task { $this->moduleid = $moduleid; $this->modulename = $coursemodule->modname; - $this->parentcontextid = get_context_instance(CONTEXT_MODULE, $this->moduleid)->id; + $this->parentcontextid = context_module::instance($this->moduleid)->id; } parent::__construct($name, $plan); diff --git a/backup/moodle2/backup_course_task.class.php b/backup/moodle2/backup_course_task.class.php index 6cc262baf87..5d4cd9b4f45 100644 --- a/backup/moodle2/backup_course_task.class.php +++ b/backup/moodle2/backup_course_task.class.php @@ -42,7 +42,7 @@ class backup_course_task extends backup_task { public function __construct($name, $courseid, $plan = null) { $this->courseid = $courseid; - $this->contextid = get_context_instance(CONTEXT_COURSE, $this->courseid)->id; + $this->contextid = context_course::instance($this->courseid)->id; parent::__construct($name, $plan); } diff --git a/backup/moodle2/backup_final_task.class.php b/backup/moodle2/backup_final_task.class.php index 45d491af3c0..38bccb6823b 100644 --- a/backup/moodle2/backup_final_task.class.php +++ b/backup/moodle2/backup_final_task.class.php @@ -43,7 +43,7 @@ class backup_final_task extends backup_task { global $CFG; // Set the backup::VAR_CONTEXTID setting to course context as far as next steps require that - $coursectxid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id; + $coursectxid = context_course::instance($this->get_courseid())->id; $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $coursectxid)); // Set the backup::VAR_COURSEID setting to course, we'll need that in some steps diff --git a/backup/moodle2/backup_section_task.class.php b/backup/moodle2/backup_section_task.class.php index ab4b4c8a10d..b84d3388cd3 100644 --- a/backup/moodle2/backup_section_task.class.php +++ b/backup/moodle2/backup_section_task.class.php @@ -71,7 +71,7 @@ class backup_section_task extends backup_task { public function build() { // Set the backup::VAR_CONTEXTID setting to course context as far as next steps require that - $coursectxid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id; + $coursectxid = context_course::instance($this->get_courseid())->id; $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $coursectxid)); // Add some extra settings that related processors are going to need diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index a73cafa587e..24d7da9b77c 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -667,7 +667,7 @@ class backup_final_scales_structure_step extends backup_structure_step { AND bi.itemname = 'scalefinal'", array(backup::VAR_BACKUPID)); // Annotate scale files (they store files in system context, so pass it instead of default one) - $scale->annotate_files('grade', 'scale', 'id', get_context_instance(CONTEXT_SYSTEM)->id); + $scale->annotate_files('grade', 'scale', 'id', context_system::instance()->id); // Return main element (scalesdef) return $scalesdef; @@ -704,7 +704,7 @@ class backup_final_outcomes_structure_step extends backup_structure_step { AND bi.itemname = 'outcomefinal'", array(backup::VAR_BACKUPID)); // Annotate outcome files (they store files in system context, so pass it instead of default one) - $outcome->annotate_files('grade', 'outcome', 'id', get_context_instance(CONTEXT_SYSTEM)->id); + $outcome->annotate_files('grade', 'outcome', 'id', context_system::instance()->id); // Return main element (outcomesdef) return $outcomesdef; @@ -1470,8 +1470,8 @@ class backup_main_structure_step extends backup_structure_step { $info['original_course_fullname'] = $originalcourseinfo->fullname; $info['original_course_shortname'] = $originalcourseinfo->shortname; $info['original_course_startdate'] = $originalcourseinfo->startdate; - $info['original_course_contextid'] = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id; - $info['original_system_contextid'] = get_context_instance(CONTEXT_SYSTEM)->id; + $info['original_course_contextid'] = context_course::instance($this->get_courseid())->id; + $info['original_system_contextid'] = context_system::instance()->id; // Get more information from controller list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information($this->get_backupid()); @@ -1835,7 +1835,7 @@ class backup_annotate_all_user_files extends backup_execution_step { 'backupid' => $this->get_backupid(), 'itemname' => 'userfinal')); foreach ($rs as $record) { $userid = $record->itemid; - $userctx = get_context_instance(CONTEXT_USER, $userid); + $userctx = context_user::instance($userid); if (!$userctx) { continue; // User has not context, sure it's a deleted user, so cannot have files } diff --git a/backup/moodle2/restore_course_task.class.php b/backup/moodle2/restore_course_task.class.php index 97a2376d0ba..f39e6c63607 100644 --- a/backup/moodle2/restore_course_task.class.php +++ b/backup/moodle2/restore_course_task.class.php @@ -64,7 +64,7 @@ class restore_course_task extends restore_task { public function build() { // Define the task contextid (the course one) - $this->contextid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id; + $this->contextid = context_course::instance($this->get_courseid())->id; // Executed conditionally if restoring to new course or if overwrite_conf setting is enabled if ($this->get_target() == backup::TARGET_NEW_COURSE || $this->get_setting_value('overwrite_conf') == true) { diff --git a/backup/moodle2/restore_section_task.class.php b/backup/moodle2/restore_section_task.class.php index 9d48778fed1..ab0843dad99 100644 --- a/backup/moodle2/restore_section_task.class.php +++ b/backup/moodle2/restore_section_task.class.php @@ -74,7 +74,7 @@ class restore_section_task extends restore_task { public function build() { // Define the task contextid (the course one) - $this->contextid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id; + $this->contextid = context_course::instance($this->get_courseid())->id; // We always try to restore as much info from sections as possible, no matter of the type // of restore (new, existing, deleting, import...). MDL-27764 diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 1bd5a48b973..3a4cf9abdef 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -42,11 +42,11 @@ class restore_create_and_clean_temp_stuff extends restore_execution_step { } // Create the old-course-ctxid to new-course-ctxid mapping, we need that available since the beginning $itemid = $this->task->get_old_contextid(); - $newitemid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id; + $newitemid = context_course::instance($this->get_courseid())->id; restore_dbops::set_backup_ids_record($this->get_restoreid(), 'context', $itemid, $newitemid); // Create the old-system-ctxid to new-system-ctxid mapping, we need that available since the beginning $itemid = $this->task->get_old_system_contextid(); - $newitemid = get_context_instance(CONTEXT_SYSTEM)->id; + $newitemid = context_system::instance()->id; restore_dbops::set_backup_ids_record($this->get_restoreid(), 'context', $itemid, $newitemid); // Create the old-course-id to new-course-id mapping, we need that available since the beginning $itemid = $this->task->get_old_courseid(); @@ -268,7 +268,7 @@ class restore_gradebook_structure_step extends restore_structure_step { $data = (object)$data; $oldid = $data->id; - $data->contextid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id; + $data->contextid = context_course::instance($this->get_courseid())->id; $newitemid = $DB->insert_record('grade_letters', $data); $this->set_mapping('grade_letter', $oldid, $newitemid); @@ -888,7 +888,7 @@ class restore_scales_structure_step extends restore_structure_step { $data->courseid = $data->courseid ? $this->get_courseid() : 0; // If global scale (course=0), check the user has perms to create it // falling to course scale if not - $systemctx = get_context_instance(CONTEXT_SYSTEM); + $systemctx = context_system::instance(); if ($data->courseid == 0 && !has_capability('moodle/course:managescales', $systemctx , $this->task->get_userid())) { $data->courseid = $this->get_courseid(); } @@ -950,7 +950,7 @@ class restore_outcomes_structure_step extends restore_structure_step { $data->courseid = $data->courseid ? $this->get_courseid() : null; // If global outcome (course=null), check the user has perms to create it // falling to course outcome if not - $systemctx = get_context_instance(CONTEXT_SYSTEM); + $systemctx = context_system::instance(); if (is_null($data->courseid) && !has_capability('moodle/grade:manageoutcomes', $systemctx , $this->task->get_userid())) { $data->courseid = $this->get_courseid(); } @@ -1275,7 +1275,7 @@ class restore_course_structure_step extends restore_structure_step { $data->fullname = $fullname; $data->shortname= $shortname; - $context = get_context_instance_by_id($this->task->get_contextid()); + $context = context::instance_by_id($this->task->get_contextid()); if (has_capability('moodle/course:changeidnumber', $context, $this->task->get_userid())) { $data->idnumber = ''; } else { @@ -2484,7 +2484,7 @@ class restore_block_instance_structure_step extends restore_structure_step { // Save the mapping (with restorefiles support) $this->set_mapping('block_instance', $oldid, $newitemid, true); // Create the block context - $newcontextid = get_context_instance(CONTEXT_BLOCK, $newitemid)->id; + $newcontextid = context_block::instance($newitemid)->id; // Save the block contexts mapping and sent it to task $this->set_mapping('context', $oldcontextid, $newcontextid); $this->task->set_contextid($newcontextid); @@ -2619,7 +2619,7 @@ class restore_module_structure_step extends restore_structure_step { // set the new course_module id in the task $this->task->set_moduleid($newitemid); // we can now create the context safely - $ctxid = get_context_instance(CONTEXT_MODULE, $newitemid)->id; + $ctxid = context_module::instance($newitemid)->id; // set the new context id in the task $this->task->set_contextid($ctxid); // update sequence field in course_section diff --git a/backup/restorefile.php b/backup/restorefile.php index 92f8eb0ab3d..0c4095ba4c9 100644 --- a/backup/restorefile.php +++ b/backup/restorefile.php @@ -43,7 +43,7 @@ list($context, $course, $cm) = get_context_info_array($contextid); // will be used when restore if (!empty($filecontextid)) { - $filecontext = get_context_instance_by_id($filecontextid); + $filecontext = context::instance_by_id($filecontextid); } $url = new moodle_url('/backup/restorefile.php', array('contextid'=>$contextid)); @@ -116,7 +116,7 @@ if ($context->contextlevel == CONTEXT_MODULE) { echo $OUTPUT->heading_with_help(get_string('choosefilefromactivitybackup', 'backup'), 'choosefilefromuserbackup', 'backup'); echo $OUTPUT->container_start(); $treeview_options = array(); - $user_context = get_context_instance(CONTEXT_USER, $USER->id); + $user_context = context_user::instance($USER->id); $treeview_options['filecontext'] = $context; $treeview_options['currentcontext'] = $context; $treeview_options['component'] = 'backup'; @@ -142,7 +142,7 @@ echo $OUTPUT->container_end(); echo $OUTPUT->heading_with_help(get_string('choosefilefromuserbackup', 'backup'), 'choosefilefromuserbackup', 'backup'); echo $OUTPUT->container_start(); $treeview_options = array(); -$user_context = get_context_instance(CONTEXT_USER, $USER->id); +$user_context = context_user::instance($USER->id); $treeview_options['filecontext'] = $user_context; $treeview_options['currentcontext'] = $context; $treeview_options['component'] = 'user'; @@ -157,7 +157,7 @@ if (!empty($automatedbackups)) { echo $OUTPUT->heading_with_help(get_string('choosefilefromautomatedbackup', 'backup'), 'choosefilefromautomatedbackup', 'backup'); echo $OUTPUT->container_start(); $treeview_options = array(); - $user_context = get_context_instance(CONTEXT_USER, $USER->id); + $user_context = context_user::instance($USER->id); $treeview_options['filecontext'] = $context; $treeview_options['currentcontext'] = $context; $treeview_options['component'] = 'backup'; diff --git a/backup/util/checks/backup_check.class.php b/backup/util/checks/backup_check.class.php index fb7839dd399..164da0b3856 100644 --- a/backup/util/checks/backup_check.class.php +++ b/backup/util/checks/backup_check.class.php @@ -95,7 +95,7 @@ abstract class backup_check { $type = $backup_controller->get_type(); $mode = $backup_controller->get_mode(); $courseid = $backup_controller->get_courseid(); - $coursectx= get_context_instance(CONTEXT_COURSE, $courseid); + $coursectx= context_course::instance($courseid); $userid = $backup_controller->get_userid(); $id = $backup_controller->get_id(); // courseid / sectionid / cmid @@ -118,7 +118,7 @@ abstract class backup_check { break; case backup::TYPE_1ACTIVITY : get_coursemodule_from_id(null, $id, $courseid, false, MUST_EXIST); // cm exists - $modulectx = get_context_instance(CONTEXT_MODULE, $id); + $modulectx = context_module::instance($id); $typecapstocheck['moodle/backup:backupactivity'] = $modulectx; break; default : diff --git a/backup/util/checks/restore_check.class.php b/backup/util/checks/restore_check.class.php index 915112cf230..7b455f94cb5 100644 --- a/backup/util/checks/restore_check.class.php +++ b/backup/util/checks/restore_check.class.php @@ -62,7 +62,7 @@ abstract class restore_check { $type = $restore_controller->get_type(); $mode = $restore_controller->get_mode(); $courseid = $restore_controller->get_courseid(); - $coursectx= get_context_instance(CONTEXT_COURSE, $courseid); + $coursectx= context_course::instance($courseid); $userid = $restore_controller->get_userid(); // Note: all the checks along the function MUST be performed for $userid, that diff --git a/backup/util/dbops/backup_plan_dbops.class.php b/backup/util/dbops/backup_plan_dbops.class.php index 2ce9434ee3d..4ff5d8c0d62 100644 --- a/backup/util/dbops/backup_plan_dbops.class.php +++ b/backup/util/dbops/backup_plan_dbops.class.php @@ -39,7 +39,7 @@ abstract class backup_plan_dbops extends backup_dbops { global $DB; // Get the context of the module - $contextid = get_context_instance(CONTEXT_MODULE, $moduleid)->id; + $contextid = context_module::instance($moduleid)->id; // Get all the block instances which parentcontextid is the module contextid $blockids = array(); @@ -57,7 +57,7 @@ abstract class backup_plan_dbops extends backup_dbops { global $DB; // Get the context of the course - $contextid = get_context_instance(CONTEXT_COURSE, $courseid)->id; + $contextid = context_course::instance($courseid)->id; // Get all the block instances which parentcontextid is the course contextid $blockids = array(); @@ -214,7 +214,7 @@ abstract class backup_plan_dbops extends backup_dbops { switch ($type) { case backup::TYPE_1COURSE: $shortname = $DB->get_field('course', 'shortname', array('id' => $id)); - $context = get_context_instance(CONTEXT_COURSE, $id); + $context = context_course::instance($id); $shortname = format_string($shortname, true, array('context' => $context)); break; case backup::TYPE_1SECTION: diff --git a/backup/util/dbops/restore_dbops.class.php b/backup/util/dbops/restore_dbops.class.php index 1e7cbbb773b..3bd86e374db 100644 --- a/backup/util/dbops/restore_dbops.class.php +++ b/backup/util/dbops/restore_dbops.class.php @@ -346,7 +346,7 @@ abstract class restore_dbops { global $CFG, $DB; // Gather various information about roles - $coursectx = get_context_instance(CONTEXT_COURSE, $courseid); + $coursectx = context_course::instance($courseid); $assignablerolesshortname = get_assignable_roles($coursectx, ROLENAME_SHORT, false, $userid); // Note: under 1.9 we had one function restore_samerole() that performed one complete @@ -719,7 +719,7 @@ abstract class restore_dbops { switch ($contextlevel) { // For system is easy, the best context is the system context case CONTEXT_SYSTEM: - $targetcontext = get_context_instance(CONTEXT_SYSTEM); + $targetcontext = context_system::instance(); break; // For coursecat, we are going to look for stamps in all the @@ -739,8 +739,8 @@ abstract class restore_dbops { } $contexts = array(); // Build the array of contexts we are going to look - $systemctx = get_context_instance(CONTEXT_SYSTEM); - $coursectx = get_context_instance(CONTEXT_COURSE, $courseid); + $systemctx = context_system::instance(); + $coursectx = context_course::instance($courseid); $parentctxs= get_parent_contexts($coursectx); foreach ($parentctxs as $parentctx) { // Exclude system context @@ -761,14 +761,14 @@ abstract class restore_dbops { $matchingcontexts = $DB->get_records_sql($sql, $params); // Only if ONE and ONLY ONE context is found, use it as valid target if (count($matchingcontexts) == 1) { - $targetcontext = get_context_instance_by_id(reset($matchingcontexts)->contextid); + $targetcontext = context::instance_by_id(reset($matchingcontexts)->contextid); } } break; // For course is easy, the best context is the course context case CONTEXT_COURSE: - $targetcontext = get_context_instance(CONTEXT_COURSE, $courseid); + $targetcontext = context_course::instance($courseid); break; // For module is easy, there is not best context, as far as the @@ -777,7 +777,7 @@ abstract class restore_dbops { // case is handled by {@link prechek_precheck_qbanks_by_level} // in an special way case CONTEXT_MODULE: - $targetcontext = get_context_instance(CONTEXT_COURSE, $courseid); + $targetcontext = context_course::instance($courseid); break; } return $targetcontext; @@ -1065,7 +1065,7 @@ abstract class restore_dbops { // but for deleted users that don't have a context anymore (MDL-30192). We are done for them // and nothing else (custom fields, prefs, tags, files...) will be created. if (empty($user->deleted)) { - $newuserctxid = $user->deleted ? 0 : get_context_instance(CONTEXT_USER, $newuserid)->id; + $newuserctxid = $user->deleted ? 0 : context_user::instance($newuserid)->id; self::set_backup_ids_record($restoreid, 'context', $recuser->parentitemid, $newuserctxid); // Process custom fields @@ -1342,7 +1342,7 @@ abstract class restore_dbops { $mnethosts = $DB->get_records('mnet_host', array(), 'wwwroot', 'wwwroot, id'); // Calculate the context we are going to use for capability checking - $context = get_context_instance(CONTEXT_COURSE, $courseid); + $context = context_course::instance($courseid); // Calculate if we have perms to create users, by checking: // to 'moodle/restore:createuser' and 'moodle/restore:userinfo' @@ -1510,7 +1510,7 @@ abstract class restore_dbops { global $DB; // Get the course context - $coursectx = get_context_instance(CONTEXT_COURSE, $courseid); + $coursectx = context_course::instance($courseid); // Get all the mapped roles we have $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $restoreid, 'itemname' => 'role'), '', 'itemid'); foreach ($rs as $recrole) { diff --git a/backup/util/helper/backup_cron_helper.class.php b/backup/util/helper/backup_cron_helper.class.php index 00db13dc390..4bc6b6191a9 100644 --- a/backup/util/helper/backup_cron_helper.class.php +++ b/backup/util/helper/backup_cron_helper.class.php @@ -207,7 +207,7 @@ abstract class backup_cron_automated_helper { //Build the message subject $site = get_site(); - $prefix = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))).": "; + $prefix = format_string($site->shortname, true, array('context' => context_course::instance(SITEID))).": "; if ($haserrors) { $prefix .= "[".strtoupper(get_string('error'))."] "; } @@ -478,7 +478,7 @@ abstract class backup_cron_automated_helper { // Clean up excess backups in the course backup filearea if ($storage == 0 || $storage == 2) { $fs = get_file_storage(); - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); $component = 'backup'; $filearea = 'automated'; $itemid = 0; diff --git a/backup/util/helper/backup_helper.class.php b/backup/util/helper/backup_helper.class.php index 59047517cfb..efc0c498635 100644 --- a/backup/util/helper/backup_helper.class.php +++ b/backup/util/helper/backup_helper.class.php @@ -226,19 +226,19 @@ abstract class backup_helper { $itemid = 0; switch ($backuptype) { case backup::TYPE_1ACTIVITY: - $ctxid = get_context_instance(CONTEXT_MODULE, $id)->id; + $ctxid = context_module::instance($id)->id; $component = 'backup'; $filearea = 'activity'; $itemid = 0; break; case backup::TYPE_1SECTION: - $ctxid = get_context_instance(CONTEXT_COURSE, $courseid)->id; + $ctxid = context_course::instance($courseid)->id; $component = 'backup'; $filearea = 'section'; $itemid = $id; break; case backup::TYPE_1COURSE: - $ctxid = get_context_instance(CONTEXT_COURSE, $courseid)->id; + $ctxid = context_course::instance($courseid)->id; $component = 'backup'; $filearea = 'course'; $itemid = 0; @@ -273,7 +273,7 @@ abstract class backup_helper { // are sent to user's "user_tohub" file area. The upload process // will be responsible for cleaning that filearea once finished if ($backupmode == backup::MODE_HUB) { - $ctxid = get_context_instance(CONTEXT_USER, $userid)->id; + $ctxid = context_user::instance($userid)->id; $component = 'user'; $filearea = 'tohub'; $itemid = 0; @@ -284,7 +284,7 @@ abstract class backup_helper { // file area. Maintenance of such area is responsibility of // the user via corresponding file manager frontend if ($backupmode == backup::MODE_GENERAL && (!$hasusers || $isannon)) { - $ctxid = get_context_instance(CONTEXT_USER, $userid)->id; + $ctxid = context_user::instance($userid)->id; $component = 'user'; $filearea = 'backup'; $itemid = 0; diff --git a/backup/util/helper/restore_prechecks_helper.class.php b/backup/util/helper/restore_prechecks_helper.class.php index 53ec22ea3df..95bc9685846 100644 --- a/backup/util/helper/restore_prechecks_helper.class.php +++ b/backup/util/helper/restore_prechecks_helper.class.php @@ -99,7 +99,7 @@ abstract class restore_prechecks_helper { // If restoring to different site and restoring users and backup has mnet users warn/error if (!$samesite && $restoreusers && $hasmnetusers) { // User is admin (can create users at sysctx), warn - if (has_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM), $controller->get_userid())) { + if (has_capability('moodle/user:create', context_system::instance(), $controller->get_userid())) { $warnings[] = get_string('mnetrestore_extusers_admin', 'admin'); // User not admin } else { diff --git a/backup/util/plan/restore_structure_step.class.php b/backup/util/plan/restore_structure_step.class.php index 4b0ff58348e..42491cf432b 100644 --- a/backup/util/plan/restore_structure_step.class.php +++ b/backup/util/plan/restore_structure_step.class.php @@ -256,7 +256,7 @@ abstract class restore_structure_step extends restore_step { // Re-enforce 'moodle/restore:rolldates' capability for the user in the course, just in case } else if (!has_capability('moodle/restore:rolldates', - get_context_instance(CONTEXT_COURSE, $this->get_courseid()), + context_course::instance($this->get_courseid()), $this->task->get_userid())) { $cache[$this->get_restoreid()] = 0; diff --git a/backup/util/ui/backup_ui_stage.class.php b/backup/util/ui/backup_ui_stage.class.php index 799556157e0..472294af5a7 100644 --- a/backup/util/ui/backup_ui_stage.class.php +++ b/backup/util/ui/backup_ui_stage.class.php @@ -473,12 +473,12 @@ class backup_ui_stage_complete extends backup_ui_stage_final { case 'activity': $cmid = $this->get_ui()->get_controller()->get_id(); $cm = get_coursemodule_from_id(null, $cmid, $courseid); - $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $modcontext = context_module::instance($cm->id); $restorerul = new moodle_url('/backup/restorefile.php', array('contextid'=>$modcontext->id)); break; case 'course': default: - $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid); + $coursecontext = context_course::instance($courseid); $restorerul = new moodle_url('/backup/restorefile.php', array('contextid'=>$coursecontext->id)); } diff --git a/backup/util/ui/renderer.php b/backup/util/ui/renderer.php index 9e0ab9dd4f9..2a5f3dac3e2 100644 --- a/backup/util/ui/renderer.php +++ b/backup/util/ui/renderer.php @@ -535,8 +535,8 @@ class core_backup_renderer extends plugin_renderer_base { } $row->cells = array( html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$course->id)), - format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))), - format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))) + format_string($course->shortname, true, array('context' => context_course::instance($course->id))), + format_string($course->fullname, true, array('context' => context_course::instance($course->id))) ); $table->data[] = $row; } @@ -605,8 +605,8 @@ class core_backup_renderer extends plugin_renderer_base { } $row->cells = array( html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'importid', 'value'=>$course->id)), - format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))), - format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))) + format_string($course->shortname, true, array('context' => context_course::instance($course->id))), + format_string($course->fullname, true, array('context' => context_course::instance($course->id))) ); $table->data[] = $row; } @@ -647,7 +647,7 @@ class core_backup_renderer extends plugin_renderer_base { } $row->cells = array( html_writer::empty_tag('input', array('type'=>'radio', 'name'=>'targetid', 'value'=>$category->id)), - format_string($category->name, true, array('context' => get_context_instance(CONTEXT_COURSECAT, $category->id))), + format_string($category->name, true, array('context' => context_coursecat::instance($category->id))), format_text($category->description, $category->descriptionformat, array('overflowdiv'=>true)) ); $table->data[] = $row; diff --git a/backup/util/ui/restore_ui_components.php b/backup/util/ui/restore_ui_components.php index b95681dee5a..7ebaea8fadc 100644 --- a/backup/util/ui/restore_ui_components.php +++ b/backup/util/ui/restore_ui_components.php @@ -182,7 +182,8 @@ abstract class restore_search_base implements renderable { $resultset = $DB->get_records_sql($sql, $params, $offs, $blocksz); foreach ($resultset as $result) { context_instance_preload($result); - $context = get_context_instance($contextlevel, $result->id); + $classname = context_helper::get_class_for_level($contextlevel); + $context = $classname::instance($result->id); if (count($requiredcaps) > 0) { if (!has_all_capabilities($requiredcaps, $context, $userid)) { continue; diff --git a/backup/util/ui/restore_ui_stage.class.php b/backup/util/ui/restore_ui_stage.class.php index 7e920695502..6c282544f05 100644 --- a/backup/util/ui/restore_ui_stage.class.php +++ b/backup/util/ui/restore_ui_stage.class.php @@ -231,7 +231,7 @@ class restore_ui_stage_destination extends restore_ui_independent_stage { 'filepath'=>$this->filepath, 'contextid'=>$this->contextid, 'stage'=>restore_ui::STAGE_DESTINATION)); - $this->coursesearch = new restore_course_search(array('url'=>$url), get_context_instance_by_id($contextid)->instanceid); + $this->coursesearch = new restore_course_search(array('url'=>$url), context::instance_by_id($contextid)->instanceid); $this->categorysearch = new restore_category_search(array('url'=>$url)); } public function process() { @@ -286,7 +286,7 @@ class restore_ui_stage_destination extends restore_ui_independent_stage { 'contextid' => $this->contextid, 'filepath' => $this->filepath, 'stage' => restore_ui::STAGE_SETTINGS)); - $context = get_context_instance_by_id($this->contextid); + $context = context::instance_by_id($this->contextid); if ($context->contextlevel == CONTEXT_COURSE and has_capability('moodle/restore:restorecourse', $context)) { $currentcourse = $context->instanceid; @@ -703,7 +703,7 @@ class restore_ui_stage_process extends restore_ui_stage { $haserrors = (!empty($results['errors'])); $html .= $renderer->precheck_notices($results); if (!empty($info->role_mappings->mappings)) { - $context = get_context_instance(CONTEXT_COURSE, $this->ui->get_controller()->get_courseid()); + $context = context_course::instance($this->ui->get_controller()->get_courseid()); $assignableroles = get_assignable_roles($context, ROLENAME_ALIAS, false); $html .= $renderer->role_mappings($info->role_mappings->mappings, $assignableroles); } diff --git a/login/change_password.php b/login/change_password.php index 2608079a004..e2cc58d5e18 100644 --- a/login/change_password.php +++ b/login/change_password.php @@ -35,7 +35,7 @@ $PAGE->https_required(); $PAGE->set_url('/login/change_password.php', array('id'=>$id)); -$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); +$PAGE->set_context(context_system::instance()); if ($return) { // this redirect prevents security warning because https can not POST to http pages @@ -52,7 +52,7 @@ if ($return) { $strparticipants = get_string('participants'); -$systemcontext = get_context_instance(CONTEXT_SYSTEM); +$systemcontext = context_system::instance(); if (!$course = $DB->get_record('course', array('id'=>$id))) { print_error('invalidcourseid'); diff --git a/login/confirm.php b/login/confirm.php index 5fad8389568..d765361c288 100644 --- a/login/confirm.php +++ b/login/confirm.php @@ -32,7 +32,7 @@ $p = optional_param('p', '', PARAM_ALPHANUM); // Old parameter: secret $s = optional_param('s', '', PARAM_RAW); // Old parameter: username $PAGE->set_url('/login/confirm.php'); -$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); +$PAGE->set_context(context_system::instance()); if (empty($CFG->registerauth)) { print_error('cannotusepage2'); diff --git a/login/forgot_password.php b/login/forgot_password.php index 807cbc3ed57..d16ff7e4bb6 100644 --- a/login/forgot_password.php +++ b/login/forgot_password.php @@ -36,7 +36,7 @@ $p_username = optional_param('s', false, PARAM_RAW); $PAGE->https_required(); $PAGE->set_url('/login/forgot_password.php'); -$systemcontext = get_context_instance(CONTEXT_SYSTEM); +$systemcontext = context_system::instance(); $PAGE->set_context($systemcontext); // setup text strings diff --git a/login/index.php b/login/index.php index 3f398a4c0fa..909a9faeefa 100644 --- a/login/index.php +++ b/login/index.php @@ -38,7 +38,7 @@ if ($cancel) { //HTTPS is required in this page when $CFG->loginhttps enabled $PAGE->https_required(); -$context = get_context_instance(CONTEXT_SYSTEM); +$context = context_system::instance(); $PAGE->set_url("$CFG->httpswwwroot/login/index.php"); $PAGE->set_context($context); $PAGE->set_pagelayout('login'); diff --git a/login/logout.php b/login/logout.php index fa5ad4d7ed5..664b179aa95 100644 --- a/login/logout.php +++ b/login/logout.php @@ -27,7 +27,7 @@ require_once('../config.php'); $PAGE->set_url('/login/logout.php'); -$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); +$PAGE->set_context(context_system::instance()); $sesskey = optional_param('sesskey', '__notpresent__', PARAM_RAW); // we want not null default to prevent required sesskey warning $login = optional_param('loginpage', 0, PARAM_BOOL); diff --git a/login/signup.php b/login/signup.php index fb27841c8b3..e622aba21e8 100644 --- a/login/signup.php +++ b/login/signup.php @@ -41,7 +41,7 @@ if (!$authplugin->can_signup()) { $PAGE->https_required(); $PAGE->set_url('/login/signup.php'); -$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); +$PAGE->set_context(context_system::instance()); $mform_signup = new login_signup_form(null, null, 'post', '', array('autocomplete'=>'on')); diff --git a/login/token.php b/login/token.php index adfe1d4175d..ae3adf012fe 100644 --- a/login/token.php +++ b/login/token.php @@ -43,7 +43,7 @@ $user = authenticate_user_login($username, $password); if (!empty($user)) { //Non admin can not authenticate if maintenance mode - $hassiteconfig = has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM), $user); + $hassiteconfig = has_capability('moodle/site:config', context_system::instance(), $user); if (!empty($CFG->maintenance_enabled) and !$hassiteconfig) { throw new moodle_exception('sitemaintenance', 'admin'); } @@ -77,7 +77,7 @@ if (!empty($user)) { } //check if there is any required system capability - if ($service->requiredcapability and !has_capability($service->requiredcapability, get_context_instance(CONTEXT_SYSTEM), $user)) { + if ($service->requiredcapability and !has_capability($service->requiredcapability, context_system::instance(), $user)) { throw new moodle_exception('missingrequiredcapability', 'webservice', '', $service->requiredcapability); } @@ -155,7 +155,7 @@ if (!empty($user)) { $token->token = md5(uniqid(rand(), 1)); $token->userid = $user->id; $token->tokentype = EXTERNAL_TOKEN_PERMANENT; - $token->contextid = get_context_instance(CONTEXT_SYSTEM)->id; + $token->contextid = context_system::instance()->id; $token->creatorid = $user->id; $token->timecreated = time(); $token->externalserviceid = $service_record->id;