Skip to content

Commit

Permalink
MDL-34549 libraries: Replace get_context_instance_by_id() by context:…
Browse files Browse the repository at this point in the history
…:instance_by_id()
  • Loading branch information
ankitagarwal committed Aug 24, 2012
1 parent d71c486 commit d197ea4
Show file tree
Hide file tree
Showing 61 changed files with 92 additions and 92 deletions.
8 changes: 4 additions & 4 deletions admin/roles/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ protected function load_parent_permissions() {
global $DB;

/// Get the capabilities from the parent context, so that can be shown in the interface.
$parentcontext = get_context_instance_by_id(get_parent_contextid($this->context));
$parentcontext = context::instance_by_id(get_parent_contextid($this->context));
$this->parentpermissions = role_context_capabilities($this->roleid, $parentcontext);
}

Expand Down Expand Up @@ -996,7 +996,7 @@ public function __construct($name, $options) {
if (isset($options['context'])) {
$this->context = $options['context'];
} else {
$this->context = get_context_instance_by_id($options['contextid']);
$this->context = context::instance_by_id($options['contextid']);
}
$options['accesscontext'] = $this->context;
parent::__construct($name, $options);
Expand Down Expand Up @@ -1230,7 +1230,7 @@ protected function this_con_group_name($search, $numusers) {
}

protected function parent_con_group_name($search, $contextid) {
$context = get_context_instance_by_id($contextid);
$context = context::instance_by_id($contextid);
$contextname = print_context_name($context, true, true);
if ($search) {
$a = new stdClass;
Expand Down Expand Up @@ -1477,7 +1477,7 @@ public function get_intro_text() {
function roles_get_potential_user_selector($context, $name, $options) {
$blockinsidecourse = false;
if ($context->contextlevel == CONTEXT_BLOCK) {
$parentcontext = get_context_instance_by_id(get_parent_contextid($context));
$parentcontext = context::instance_by_id(get_parent_contextid($context));
$blockinsidecourse = in_array($parentcontext->contextlevel, array(CONTEXT_MODULE, CONTEXT_COURSE));
}

Expand Down
2 changes: 1 addition & 1 deletion admin/user/user_bulk_cohortadd.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
// external cohorts can not be modified
continue;
}
$context = get_context_instance_by_id($c->contextid);
$context = context::instance_by_id($c->contextid);
if (!has_capability('moodle/cohort:assign', $context)) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions blocks/community/block_community.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function user_can_addto($page) {
function user_can_edit() {
// Don't allow people to edit the block if they can't even use it
if (!has_capability('moodle/community:add',
get_context_instance_by_id($this->instance->parentcontextid))) {
context::instance_by_id($this->instance->parentcontextid))) {
return false;
}
return parent::user_can_edit();
Expand All @@ -52,7 +52,7 @@ function user_can_edit() {
function get_content() {
global $CFG, $OUTPUT, $USER;

$coursecontext = get_context_instance_by_id($this->instance->parentcontextid);
$coursecontext = context::instance_by_id($this->instance->parentcontextid);

if (!has_capability('moodle/community:add', $coursecontext)
or $this->content !== NULL) {
Expand Down
2 changes: 1 addition & 1 deletion blocks/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function definition() {

$regionoptions = $this->page->theme->get_all_block_regions();

$parentcontext = get_context_instance_by_id($this->block->instance->parentcontextid);
$parentcontext = context::instance_by_id($this->block->instance->parentcontextid);
$mform->addElement('hidden', 'bui_parentcontextid', $parentcontext->id);

$mform->addElement('static', 'bui_homecontext', get_string('createdat', 'block'), print_context_name($parentcontext));
Expand Down
2 changes: 1 addition & 1 deletion blocks/html/block_html.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function instance_delete() {
function content_is_trusted() {
global $SCRIPT;

if (!$context = get_context_instance_by_id($this->instance->parentcontextid)) {
if (!$context = context::instance_by_id($this->instance->parentcontextid)) {
return false;
}
//find out if this block is on the profile page
Expand Down
2 changes: 1 addition & 1 deletion blocks/html/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function block_html_pluginfile($course, $birecord_or_cm, $context, $filearea, $a
send_file_not_found();
}

if ($parentcontext = get_context_instance_by_id($birecord_or_cm->parentcontextid)) {
if ($parentcontext = context::instance_by_id($birecord_or_cm->parentcontextid)) {
if ($parentcontext->contextlevel == CONTEXT_USER) {
// force download on all personal pages including /my/
//because we do not have reliable way to find out from where this is used
Expand Down
2 changes: 1 addition & 1 deletion blocks/quiz_results/block_quiz_results.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function get_owning_quiz() {
if (empty($this->instance->parentcontextid)) {
return 0;
}
$parentcontext = get_context_instance_by_id($this->instance->parentcontextid);
$parentcontext = context::instance_by_id($this->instance->parentcontextid);
if ($parentcontext->contextlevel != CONTEXT_MODULE) {
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion cohort/assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
require_login();

$cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
$context = get_context_instance_by_id($cohort->contextid, MUST_EXIST);
$context = context::instance_by_id($cohort->contextid, MUST_EXIST);

require_capability('moodle/cohort:assign', $context);

Expand Down
4 changes: 2 additions & 2 deletions cohort/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
$category = null;
if ($id) {
$cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
$context = get_context_instance_by_id($cohort->contextid, MUST_EXIST);
$context = context::instance_by_id($cohort->contextid, MUST_EXIST);
} else {
$context = get_context_instance_by_id($contextid, MUST_EXIST);
$context = context::instance_by_id($contextid, MUST_EXIST);
if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
print_error('invalidcontext');
}
Expand Down
2 changes: 1 addition & 1 deletion cohort/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function get_category_options($currentcontextid) {
}
// always add current - this is not likely, but if the logic gets changed it might be a problem
if (!isset($options[$currentcontextid])) {
$context = get_context_instance_by_id($currentcontextid, MUST_EXIST);
$context = context::instance_by_id($currentcontextid, MUST_EXIST);
$options[$context->id] = print_context_name($syscontext);
}
return $options;
Expand Down
2 changes: 1 addition & 1 deletion cohort/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
require_login();

if ($contextid) {
$context = get_context_instance_by_id($contextid, MUST_EXIST);
$context = context::instance_by_id($contextid, MUST_EXIST);
} else {
$context = context_system::instance();
}
Expand Down
2 changes: 1 addition & 1 deletion comment/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function __construct(stdClass $options) {
$this->contextid = $this->context->id;
} else if(!empty($options->contextid)) {
$this->contextid = $options->contextid;
$this->context = get_context_instance_by_id($this->contextid);
$this->context = context::instance_by_id($this->contextid);
} else {
print_error('invalidcontext');
}
Expand Down
2 changes: 1 addition & 1 deletion comment/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private function setup_course($courseid) {
*/
private function setup_plugin($comment) {
global $DB;
$this->context = get_context_instance_by_id($comment->contextid);
$this->context = context::instance_by_id($comment->contextid);
if (!$this->context) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion draftfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
send_file_not_found();
}

$context = get_context_instance_by_id($contextid);
$context = context::instance_by_id($contextid);
if ($context->contextlevel != CONTEXT_USER) {
send_file_not_found();
}
Expand Down
8 changes: 4 additions & 4 deletions enrol/category/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public static function role_assigned($ra) {
return true;
}

// Only category level roles are interesting.
$parentcontext = get_context_instance_by_id($ra->contextid);
//only category level roles are interesting
$parentcontext = context::instance_by_id($ra->contextid);
if ($parentcontext->contextlevel != CONTEXT_COURSECAT) {
return true;
}
Expand Down Expand Up @@ -102,8 +102,8 @@ public static function role_unassigned($ra) {
return true;
}

// Only category level roles are interesting.
$parentcontext = get_context_instance_by_id($ra->contextid);
// only category level roles are interesting
$parentcontext = context::instance_by_id($ra->contextid);
if ($parentcontext->contextlevel != CONTEXT_COURSECAT) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion enrol/cohort/addinstance_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function definition() {
ORDER BY name ASC";
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $c) {
$context = get_context_instance_by_id($c->contextid);
$context = context::instance_by_id($c->contextid);
if (!has_capability('moodle/cohort:view', $context)) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion enrol/cohort/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected function can_add_new_instances($courseid) {
ORDER BY name ASC";
$cohorts = $DB->get_records_sql($sql, $params);
foreach ($cohorts as $c) {
$context = get_context_instance_by_id($c->contextid);
$context = context::instance_by_id($c->contextid);
if (has_capability('moodle/cohort:view', $context)) {
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions enrol/cohort/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ function enrol_cohort_get_cohorts(course_enrolment_manager $manager) {
ORDER BY name ASC";
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $c) {
$context = get_context_instance_by_id($c->contextid);
$context = context::instance_by_id($c->contextid);
if (!has_capability('moodle/cohort:view', $context)) {
continue;
}
Expand All @@ -394,7 +394,7 @@ function enrol_cohort_can_view_cohort($cohortid) {
global $DB;
$cohort = $DB->get_record('cohort', array('id' => $cohortid), 'id, contextid');
if ($cohort) {
$context = get_context_instance_by_id($cohort->contextid);
$context = context::instance_by_id($cohort->contextid);
if (has_capability('moodle/cohort:view', $context)) {
return true;
}
Expand Down Expand Up @@ -457,7 +457,7 @@ function enrol_cohort_search_cohorts(course_enrolment_manager $manager, $offset
// Track offset
$offset++;
// Check capabilities
$context = get_context_instance_by_id($c->contextid);
$context = context::instance_by_id($c->contextid);
if (!has_capability('moodle/cohort:view', $context)) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions enrol/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public static function assign_roles($assignments) {

foreach ($params['assignments'] as $assignment) {
// Ensure the current user is allowed to run this function in the enrolment context
$context = get_context_instance_by_id($assignment['contextid']);
$context = context::instance_by_id($assignment['contextid']);
self::validate_context($context);
require_capability('moodle/role:assign', $context);

Expand Down Expand Up @@ -445,7 +445,7 @@ public static function unassign_roles($unassignments) {

foreach ($params['unassignments'] as $unassignment) {
// Ensure the current user is allowed to run this function in the unassignment context
$context = get_context_instance_by_id($unassignment['contextid']);
$context = context::instance_by_id($unassignment['contextid']);
self::validate_context($context);
require_capability('moodle/role:assign', $context);

Expand Down
4 changes: 2 additions & 2 deletions files/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function get_files($contextid, $component, $filearea, $itemid, $fi
if (empty($fileinfo['contextid'])) {
$context = get_system_context();
} else {
$context = get_context_instance_by_id($fileinfo['contextid']);
$context = context::instance_by_id($fileinfo['contextid']);
}
if (empty($fileinfo['component'])) {
$fileinfo['component'] = null;
Expand Down Expand Up @@ -272,7 +272,7 @@ public static function upload($contextid, $component, $filearea, $itemid, $filep
}

if (!empty($fileinfo['contextid'])) {
$context = get_context_instance_by_id($fileinfo['contextid']);
$context = context::instance_by_id($fileinfo['contextid']);
} else {
$context = get_system_context();
}
Expand Down
2 changes: 1 addition & 1 deletion files/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ public function __construct(file_info $file_info, array $options = null) {
$this->path = array();
while ($level) {
$params = $level->get_params();
$context = get_context_instance_by_id($params['contextid']);
$context = context::instance_by_id($params['contextid']);
// $this->context is current context
if ($context->id != $this->context->id or empty($params['filearea'])) {
break;
Expand Down
2 changes: 1 addition & 1 deletion grade/grading/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function load($areaid) {
global $DB;

$this->areacache = $DB->get_record('grading_areas', array('id' => $areaid), '*', MUST_EXIST);
$this->context = get_context_instance_by_id($this->areacache->contextid, MUST_EXIST);
$this->context = context::instance_by_id($this->areacache->contextid, MUST_EXIST);
$this->component = $this->areacache->component;
$this->area = $this->areacache->areaname;
}
Expand Down
2 changes: 1 addition & 1 deletion grade/grading/manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
if (is_null($contextid) or is_null($component) or is_null($area)) {
throw new coding_exception('The caller script must identify the gradable area.');
}
$context = get_context_instance_by_id($contextid, MUST_EXIST);
$context = context::instance_by_id($contextid, MUST_EXIST);
$manager = get_grading_manager($context, $component, $area);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/blocklib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ public function process_url_edit() {

$systemcontext = context_system::instance();
$frontpagecontext = context_course::instance(SITEID);
$parentcontext = get_context_instance_by_id($data->bui_parentcontextid);
$parentcontext = context::instance_by_id($data->bui_parentcontextid);

// Updating stickiness and contexts. See MDL-21375 for details.
if (has_capability('moodle/site:manageblocks', $parentcontext)) { // Check permissions in destination
Expand Down
2 changes: 1 addition & 1 deletion lib/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ function external_generate_token($tokentype, $serviceorid, $userid, $contextorid
$service = $serviceorid;
}
if (!is_object($contextorid)){
$context = get_context_instance_by_id($contextorid, MUST_EXIST);
$context = context::instance_by_id($contextorid, MUST_EXIST);
} else {
$context = $contextorid;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/filebrowser/file_info_context_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public function get_children() {
public function get_parent() {
//TODO: error checking if get_parent_contextid() returns false
$pcid = get_parent_contextid($this->context);
$parent = get_context_instance_by_id($pcid);
$parent = context::instance_by_id($pcid);
return $this->browser->get_file_info($parent);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/filebrowser/file_info_context_coursecat.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function get_children() {
*/
public function get_parent() {
$cid = get_parent_contextid($this->context);
$parent = get_context_instance_by_id($cid);
$parent = context::instance_by_id($cid);
return $this->browser->get_file_info($parent);
}
}
2 changes: 1 addition & 1 deletion lib/filebrowser/file_info_context_module.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public function get_children() {
*/
public function get_parent() {
$pcid = get_parent_contextid($this->context);
$parent = get_context_instance_by_id($pcid);
$parent = context::instance_by_id($pcid);
return $this->browser->get_file_info($parent);
}
}
4 changes: 2 additions & 2 deletions lib/questionlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ function question_category_options($contexts, $top = false, $currentcat = 0,
$categoriesarray = array();
foreach ($pcontexts as $pcontext) {
$contextstring = print_context_name(
get_context_instance_by_id($pcontext), true, true);
context::instance_by_id($pcontext), true, true);
foreach ($categories as $category) {
if ($category->contextid == $pcontext) {
$cid = $category->id;
Expand Down Expand Up @@ -1333,7 +1333,7 @@ function question_has_capability_on($question, $cap, $cachecat = -1) {
}
}
$category = $categories[$question->category];
$context = get_context_instance_by_id($category->contextid);
$context = context::instance_by_id($category->contextid);

if (array_search($cap, $question_questioncaps)!== false) {
if (!has_capability('moodle/question:' . $cap . 'all', $context)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/setuplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function __construct($context, $capability, $errormessage, $stringfile) {
$capabilityname = get_capability_string($capability);
if ($context->contextlevel == CONTEXT_MODULE and preg_match('/:view$/', $capability)) {
// we can not go to mod/xx/view.php because we most probably do not have cap to view it, let's go to course instead
$paranetcontext = get_context_instance_by_id(get_parent_contextid($context));
$paranetcontext = context::instance_by_id(get_parent_contextid($context));
$link = get_context_url($paranetcontext);
} else {
$link = get_context_url($context);
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/accesslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,7 @@ public function test_permission_evaluation() {

foreach ($DB->get_records('context') as $contextid=>$record) {
$context = context::instance_by_id($contextid);
$this->assertSame(get_context_instance_by_id($contextid), $context);
$this->assertSame(context::instance_by_id($contextid), $context);
$this->assertSame(get_context_instance($record->contextlevel, $record->instanceid), $context);
$this->assertSame(get_parent_contexts($context), $context->get_parent_context_ids());
if ($context->id == SYSCONTEXTID) {
Expand Down
4 changes: 2 additions & 2 deletions lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_
if (is_object($options['context'])) {
$context = $options['context'];
} else {
$context = get_context_instance_by_id($options['context']);
$context = context::instance_by_id($options['context']);
}
} else if ($courseid_do_not_use) {
// legacy courseid
Expand Down Expand Up @@ -1281,7 +1281,7 @@ function format_string($string, $striplinks = true, $options = NULL) {
// fallback to $PAGE->context this may be problematic in CLI and other non-standard pages :-(
$options['context'] = $PAGE->context;
} else if (is_numeric($options['context'])) {
$options['context'] = get_context_instance_by_id($options['context']);
$options['context'] = context::instance_by_id($options['context']);
}

if (!$options['context']) {
Expand Down
Loading

0 comments on commit d197ea4

Please sign in to comment.