From 9a5e297b4cba125d49a04788321894d0ce797e5e Mon Sep 17 00:00:00 2001 From: Ankit Agarwal Date: Mon, 23 Jul 2012 15:59:04 +0800 Subject: [PATCH 1/3] MDL-34470 course: Replace all instances of get_context_instance() with context_xxx::instance() in course/ --- course/category.php | 10 ++-- course/completion.php | 2 +- course/completion_form.php | 4 +- course/delete.php | 8 ++-- course/delete_category_form.php | 4 +- course/edit.php | 6 +-- course/edit_form.php | 6 +-- course/editcategory.php | 4 +- course/editsection.php | 2 +- course/externallib.php | 10 ++-- course/format/social/format.php | 2 +- course/format/topics/lib.php | 2 +- course/index.php | 12 ++--- course/info.php | 2 +- course/lib.php | 82 ++++++++++++++++----------------- course/loginas.php | 4 +- course/mod.php | 32 ++++++------- course/modduplicate.php | 4 +- course/modedit.php | 18 ++++---- course/moodleform_mod.php | 8 ++-- course/pending.php | 2 +- course/publish/backup.php | 2 +- course/publish/index.php | 4 +- course/publish/metadata.php | 8 ++-- course/recent.php | 4 +- course/recent_form.php | 6 +-- course/renderer.php | 2 +- course/report.php | 2 +- course/request.php | 2 +- course/reset.php | 2 +- course/reset_form.php | 2 +- course/rest.php | 4 +- course/scales.php | 6 +-- course/switchrole.php | 2 +- course/togglecompletion.php | 2 +- course/user.php | 4 +- 36 files changed, 138 insertions(+), 138 deletions(-) diff --git a/course/category.php b/course/category.php index 935b83a2043..91efb2d199b 100644 --- a/course/category.php +++ b/course/category.php @@ -93,7 +93,7 @@ if ($editingon && $sesskeyprovided) { // Some courses are being moved // user must have category update in both cats to perform this require_capability('moodle/category:manage', $context); - require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveto)); + require_capability('moodle/category:manage', context_coursecat::instance($moveto)); if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) { print_error('cannotfindcategory', '', '', $data->moveto); @@ -129,7 +129,7 @@ if ($editingon && $sesskeyprovided) { } if ($course) { - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $coursecontext = context_course::instance($course->id); require_capability('moodle/course:visibility', $coursecontext); // Set the visibility of the course. we set the old flag when user manually changes visibility of course. $DB->update_record('course', array('id' => $course->id, 'visible' => $visible, 'visibleold' => $visible, 'timemodified' => time())); @@ -268,7 +268,7 @@ $baseurl = new moodle_url('/course/category.php'); foreach ($subcategories as $subcategory) { // Preload the context we will need it to format the category name shortly. context_helper::preload_from_record($subcategory); - $context = get_context_instance(CONTEXT_COURSECAT, $subcategory->id); + $context = context_coursecat::instance($subcategory->id); // Prepare the things we need to create a link to the subcategory $attributes = $subcategory->visible ? array() : array('class' => 'dimmed'); $text = format_string($subcategory->name, true, array('context' => $context)); @@ -332,7 +332,7 @@ if (!$courses) { $baseurl = new moodle_url('/course/category.php', $urlparams + array('sesskey' => sesskey())); foreach ($courses as $acourse) { - $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id); + $coursecontext = context_course::instance($acourse->id); $count++; $up = ($count > 1 || !$atfirstpage); @@ -447,7 +447,7 @@ if (has_capability('moodle/course:create', $context)) { } if (!empty($CFG->enablecourserequests) && $category->id == $CFG->defaultrequestcategory) { - print_course_request_buttons(get_context_instance(CONTEXT_SYSTEM)); + print_course_request_buttons(context_system::instance()); } echo ''; diff --git a/course/completion.php b/course/completion.php index 6b38a889c3d..559ccdb5e88 100644 --- a/course/completion.php +++ b/course/completion.php @@ -53,7 +53,7 @@ if ($id) { // editing course print_error('invalidcourseid'); } require_login($course); - require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id)); + require_capability('moodle/course:update', context_course::instance($course->id)); } else { require_login(); diff --git a/course/completion_form.php b/course/completion_form.php index f13f2af69ce..56136d15fe4 100644 --- a/course/completion_form.php +++ b/course/completion_form.php @@ -105,7 +105,7 @@ class course_completion_form extends moodleform { $selectbox = array(); $selected = array(); foreach ($courses as $c) { - $selectbox[$c->id] = $list[$c->category] . ' / ' . format_string($c->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $c->id))); + $selectbox[$c->id] = $list[$c->category] . ' / ' . format_string($c->fullname, true, array('context' => context_course::instance($c->id))); // If already selected if ($c->selected) { @@ -134,7 +134,7 @@ class course_completion_form extends moodleform { // Role completion criteria $mform->addElement('header', 'roles', get_string('manualcompletionby', 'completion')); - $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, get_context_instance(CONTEXT_COURSE, $course->id)); + $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, context_course::instance($course->id)); if (!empty($roles)) { $mform->addElement('select', 'role_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods); diff --git a/course/delete.php b/course/delete.php index ebe6b788e7d..a47822bf3d6 100644 --- a/course/delete.php +++ b/course/delete.php @@ -8,7 +8,7 @@ $delete = optional_param('delete', '', PARAM_ALPHANUM); // delete confirmation hash $PAGE->set_url('/course/delete.php', array('id' => $id)); - $PAGE->set_context(get_context_instance(CONTEXT_SYSTEM)); + $PAGE->set_context(context_system::instance()); require_login(); $site = get_site(); @@ -25,15 +25,15 @@ print_error("invalidcourseid"); } - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $coursecontext = context_course::instance($course->id); if (!can_delete_course($id)) { print_error('cannotdeletecourse'); } $category = $DB->get_record("course_categories", array("id"=>$course->category)); - $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); - $categoryname = format_string($category->name, true, array('context' => get_context_instance(CONTEXT_COURSECAT, $category->id))); + $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id))); + $categoryname = format_string($category->name, true, array('context' => context_coursecat::instance($category->id))); $PAGE->navbar->add($stradministration, new moodle_url('/admin/index.php/')); $PAGE->navbar->add($strcategories, new moodle_url('/course/index.php')); diff --git a/course/delete_category_form.php b/course/delete_category_form.php index 0ceeb8e59a7..8c32405e5ca 100644 --- a/course/delete_category_form.php +++ b/course/delete_category_form.php @@ -16,7 +16,7 @@ class delete_category_form extends moodleform { $mform =& $this->_form; $category = $this->_customdata; - $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id); + $categorycontext = context_coursecat::instance($category->id); $this->_category = $category; /// Check permissions, to see if it OK to give the option to delete @@ -30,7 +30,7 @@ class delete_category_form extends moodleform { $checkcat = array_pop($tocheck); $childcategoryids[] = $checkcat->id; $tocheck = $tocheck + get_child_categories($checkcat->id); - $chcontext = get_context_instance(CONTEXT_COURSECAT, $checkcat->id); + $chcontext = context_coursecat::instance($checkcat->id); if ($candeletecontent && !has_capability('moodle/category:manage', $chcontext)) { $candeletecontent = false; } diff --git a/course/edit.php b/course/edit.php index d8a08c8acb4..3147bad79ea 100644 --- a/course/edit.php +++ b/course/edit.php @@ -44,7 +44,7 @@ if ($id) { // editing course $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST); require_login($course); $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST); - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $coursecontext = context_course::instance($course->id); require_capability('moodle/course:update', $coursecontext); $PAGE->url->param('id',$id); @@ -52,7 +52,7 @@ if ($id) { // editing course $course = null; require_login(); $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST); - $catcontext = get_context_instance(CONTEXT_COURSECAT, $category->id); + $catcontext = context_coursecat::instance($category->id); require_capability('moodle/course:create', $catcontext); $PAGE->url->param('category',$categoryid); $PAGE->set_context($catcontext); @@ -109,7 +109,7 @@ if ($editform->is_cancelled()) { $course = create_course($data, $editoroptions); // Get the context of the newly created course - $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST); + $context = context_course::instance($course->id, MUST_EXIST); if (!empty($CFG->creatornewroleid) and !is_viewing($context, NULL, 'moodle/role:assign') and !is_enrolled($context, NULL, 'moodle/role:assign')) { // deal with course creators - enrol them internally with default role diff --git a/course/edit_form.php b/course/edit_form.php index e665c039c4e..be69a6ff377 100644 --- a/course/edit_form.php +++ b/course/edit_form.php @@ -19,11 +19,11 @@ class course_edit_form extends moodleform { $editoroptions = $this->_customdata['editoroptions']; $returnto = $this->_customdata['returnto']; - $systemcontext = get_context_instance(CONTEXT_SYSTEM); - $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id); + $systemcontext = context_system::instance(); + $categorycontext = context_coursecat::instance($category->id); if (!empty($course->id)) { - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $coursecontext = context_course::instance($course->id); $context = $coursecontext; } else { $coursecontext = null; diff --git a/course/editcategory.php b/course/editcategory.php index 0a9dc89a450..f6d47e83864 100644 --- a/course/editcategory.php +++ b/course/editcategory.php @@ -40,7 +40,7 @@ if ($id) { print_error('unknowcategory'); } $PAGE->set_url('/course/editcategory.php', array('id' => $id)); - $categorycontext = get_context_instance(CONTEXT_COURSECAT, $id); + $categorycontext = context_coursecat::instance($id); $PAGE->set_context($categorycontext); require_capability('moodle/category:manage', $categorycontext); $strtitle = get_string('editcategorysettings'); @@ -54,7 +54,7 @@ if ($id) { if (!$DB->record_exists('course_categories', array('id' => $parent))) { print_error('unknowcategory'); } - $context = get_context_instance(CONTEXT_COURSECAT, $parent); + $context = context_coursecat::instance($parent); } else { $context = get_system_context(); } diff --git a/course/editsection.php b/course/editsection.php index 87ce0d5fa7f..056aac5c6f7 100644 --- a/course/editsection.php +++ b/course/editsection.php @@ -41,7 +41,7 @@ $section = $DB->get_record('course_sections', array('id' => $id), '*', MUST_EXIS $course = $DB->get_record('course', array('id' => $section->course), '*', MUST_EXIST); require_login($course); -$context = get_context_instance(CONTEXT_COURSE, $course->id); +$context = context_course::instance($course->id); require_capability('moodle/course:update', $context); $editoroptions = array('context'=>$context ,'maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true); diff --git a/course/externallib.php b/course/externallib.php index cceca17ddb1..cfb47ac3402 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -85,7 +85,7 @@ class core_course_external extends external_api { } // now security checks - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); try { self::validate_context($context); } catch (Exception $e) { @@ -144,7 +144,7 @@ class core_course_external extends external_api { $module['modicon'] = $cm->get_icon_url()->out(false); $module['indent'] = $cm->indent; - $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $modcontext = context_module::instance($cm->id); if (!empty($cm->showdescription)) { $module['description'] = $cm->get_content(); @@ -157,7 +157,7 @@ class core_course_external extends external_api { } $canviewhidden = has_capability('moodle/course:viewhiddenactivities', - get_context_instance(CONTEXT_MODULE, $cm->id)); + context_module::instance($cm->id)); //user that can view hidden module should know about the visibility $module['visible'] = $cm->visible; @@ -297,7 +297,7 @@ class core_course_external extends external_api { foreach ($courses as $course) { // now security checks - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); try { self::validate_context($context); } catch (Exception $e) { @@ -511,7 +511,7 @@ class core_course_external extends external_api { foreach ($params['courses'] as $course) { // Ensure the current user is allowed to run this function - $context = get_context_instance(CONTEXT_COURSECAT, $course['categoryid']); + $context = context_coursecat::instance($course['categoryid']); try { self::validate_context($context); } catch (Exception $e) { diff --git a/course/format/social/format.php b/course/format/social/format.php index f989f6fc830..b661c225576 100644 --- a/course/format/social/format.php +++ b/course/format/social/format.php @@ -9,7 +9,7 @@ if ($forum = forum_get_course_forum($course->id, 'social')) { $cm = get_coursemodule_from_instance('forum', $forum->id); - $context = get_context_instance(CONTEXT_MODULE, $cm->id); + $context = context_module::instance($cm->id); /// Print forum intro above posts MDL-18483 if (trim($forum->intro) != '') { diff --git a/course/format/topics/lib.php b/course/format/topics/lib.php index 3728ab29788..7c840847180 100644 --- a/course/format/topics/lib.php +++ b/course/format/topics/lib.php @@ -60,7 +60,7 @@ function callback_topics_definition() { function callback_topics_get_section_name($course, $section) { // We can't add a node without any text if ((string)$section->name !== '') { - return format_string($section->name, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); + return format_string($section->name, true, array('context' => context_course::instance($course->id))); } else if ($section->section == 0) { return get_string('section0name', 'format_topics'); } else { diff --git a/course/index.php b/course/index.php index ce1466a494f..3a40df856c1 100644 --- a/course/index.php +++ b/course/index.php @@ -38,7 +38,7 @@ $movedown = optional_param('movedown',0,PARAM_INT); $site = get_site(); -$systemcontext = get_context_instance(CONTEXT_SYSTEM); +$systemcontext = context_system::instance(); $PAGE->set_url('/course/index.php'); $PAGE->set_context($systemcontext); @@ -121,7 +121,7 @@ if (!empty($delete) and confirm_sesskey()) { if (!$deletecat = $DB->get_record('course_categories', array('id'=>$delete))) { print_error('invalidcategoryid'); } - $context = get_context_instance(CONTEXT_COURSECAT, $delete); + $context = context_coursecat::instance($delete); require_capability('moodle/category:manage', $context); require_capability('moodle/category:manage', get_category_or_system_context($deletecat->parent)); @@ -174,7 +174,7 @@ if (!$categories = get_categories()) { /// No category yet! $tempcat = new stdClass(); $tempcat->name = get_string('miscellaneous'); $tempcat->id = $DB->insert_record('course_categories', $tempcat); - $tempcat->context = get_context_instance(CONTEXT_COURSECAT, $tempcat->id); + $tempcat->context = context_coursecat::instance($tempcat->id); mark_context_dirty('/'.SYSCONTEXTID); fix_course_sortorder(); // Required to build course_categories.depth and .path. set_config('defaultrequestcategory', $tempcat->id); @@ -215,14 +215,14 @@ if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) { $swapcategory = NULL; if (!empty($moveup)) { - require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveup)); + require_capability('moodle/category:manage', context_coursecat::instance($moveup)); if ($movecategory = $DB->get_record('course_categories', array('id'=>$moveup))) { if ($swapcategory = $DB->get_records_select('course_categories', "sortordersortorder, $movecategory->parent), 'sortorder DESC', '*', 0, 1)) { $swapcategory = reset($swapcategory); } } } else { - require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $movedown)); + require_capability('moodle/category:manage', context_coursecat::instance($movedown)); if ($movecategory = $DB->get_record('course_categories', array('id'=>$movedown))) { if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder ASC', '*', 0, 1)) { $swapcategory = reset($swapcategory); @@ -306,7 +306,7 @@ function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $ if (!empty($category)) { if (!isset($category->context)) { - $category->context = get_context_instance(CONTEXT_COURSECAT, $category->id); + $category->context = context_coursecat::instance($category->id); } echo ''; diff --git a/course/info.php b/course/info.php index dfdfde98b9f..92cf89641c9 100644 --- a/course/info.php +++ b/course/info.php @@ -28,7 +28,7 @@ require_login(); } - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); if (!$course->visible and !has_capability('moodle/course:viewhiddencourses', $context)) { print_error('coursehidden', '', $CFG->wwwroot .'/'); } diff --git a/course/lib.php b/course/lib.php index f79c5c87444..22bb86b737a 100644 --- a/course/lib.php +++ b/course/lib.php @@ -143,7 +143,7 @@ function build_mnet_logs_array($hostid, $course, $user=0, $date=0, $order="l.tim /// If the group mode is separate, and this user does not have editing privileges, /// then only the user's group can be viewed. - //if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) { + //if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', context_course::instance($course->id))) { // $groupid = get_current_group($course->id); //} /// If this course doesn't have groups, no groupid can be specified. @@ -229,7 +229,7 @@ function build_logs_array($course, $user=0, $date=0, $order="l.time ASC", $limit /// If the group mode is separate, and this user does not have editing privileges, /// then only the user's group can be viewed. - if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) { + if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', context_course::instance($course->id))) { if (isset($SESSION->currentgroup[$course->id])) { $groupid = $SESSION->currentgroup[$course->id]; } else { @@ -409,7 +409,7 @@ function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $per $link = new moodle_url("/iplookup/index.php?ip=$log->ip&user=$log->userid"); $row[] = $OUTPUT->action_link($link, $log->ip, new popup_action('click', $link, 'iplookup', array('height' => 440, 'width' => 700))); - $row[] = html_writer::link(new moodle_url("/user/view.php?id={$log->userid}&course={$log->course}"), fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)))); + $row[] = html_writer::link(new moodle_url("/user/view.php?id={$log->userid}&course={$log->course}"), fullname($log, has_capability('moodle/site:viewfullnames', context_course::instance($course->id)))); $displayaction="$log->module $log->action"; if ($brokenurl) { @@ -505,7 +505,7 @@ function print_mnet_log($hostid, $course, $user=0, $date=0, $order="l.time ASC", echo ''; if ($course->id == SITEID) { - $courseshortname = format_string($courses[$log->course], true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))); + $courseshortname = format_string($courses[$log->course], true, array('context' => context_course::instance(SITEID))); echo "\n"; echo " wwwroot}/course/view.php?id={$log->course}\">".$courseshortname."\n"; echo "\n"; @@ -516,7 +516,7 @@ function print_mnet_log($hostid, $course, $user=0, $date=0, $order="l.time ASC", $link = new moodle_url("/iplookup/index.php?ip=$log->ip&user=$log->userid"); echo $OUTPUT->action_link($link, $log->ip, new popup_action('click', $link, 'iplookup', array('height' => 400, 'width' => 700))); echo "\n"; - $fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id))); + $fullname = fullname($log, has_capability('moodle/site:viewfullnames', context_course::instance($course->id))); echo "\n"; echo " wwwroot/user/view.php?id={$log->userid}\">$fullname\n"; echo "\n"; @@ -599,7 +599,7 @@ function print_log_csv($course, $user, $date, $order='l.time DESC', $modname, $log->info = format_string($log->info); $log->info = strip_tags(urldecode($log->info)); // Some XSS protection - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $coursecontext = context_course::instance($course->id); $firstField = format_string($courses[$log->course], true, array('context' => $coursecontext)); $fullname = fullname($log, has_capability('moodle/site:viewfullnames', $coursecontext)); $row = array($firstField, userdate($log->time, $strftimedatetime), $log->ip, $fullname, $log->module.' '.$log->action, $log->info); @@ -706,7 +706,7 @@ function print_log_xls($course, $user, $date, $order='l.time DESC', $modname, } } - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $coursecontext = context_course::instance($course->id); $myxls->write($row, 0, format_string($courses[$log->course], true, array('context' => $coursecontext)), ''); $myxls->write_date($row, 1, $log->time, $formatDate); // write_date() does conversion/timezone support. MDL-14934 @@ -819,7 +819,7 @@ function print_log_ods($course, $user, $date, $order='l.time DESC', $modname, } } - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $coursecontext = context_course::instance($course->id); $myxls->write_string($row, 0, format_string($courses[$log->course], true, array('context' => $coursecontext))); $myxls->write_date($row, 1, $log->time); @@ -853,7 +853,7 @@ function print_overview($courses, array $remote_courses=array()) { } } foreach ($courses as $course) { - $fullname = format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); + $fullname = format_string($course->fullname, true, array('context' => context_course::instance($course->id))); echo $OUTPUT->box_start('coursebox'); $attributes = array('title' => s($fullname)); if (empty($course->visible)) { @@ -892,7 +892,7 @@ function print_recent_activity($course) { // $course is an object global $CFG, $USER, $SESSION, $DB, $OUTPUT; - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); $viewfullnames = has_capability('moodle/site:viewfullnames', $context); @@ -1242,7 +1242,7 @@ function get_all_mods($courseid, &$mods, &$modnames, &$modnamesplural, &$modname } $mods[$mod->id] = $mod; $mods[$mod->id]->modfullname = $modnames[$mod->modname]; - if (!$mod->visible and !has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_COURSE, $courseid))) { + if (!$mod->visible and !has_capability('moodle/course:viewhiddenactivities', context_course::instance($courseid))) { continue; } // Check groupings @@ -1357,7 +1357,7 @@ function get_print_section_cm_text(cm_info $cm, $course) { filter_preload_activities($cm->get_modinfo()); // Get module context - $modulecontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $modulecontext = context_module::instance($cm->id); $labelformatoptions = new stdClass(); $labelformatoptions->noclean = true; $labelformatoptions->overflowdiv = true; @@ -1368,7 +1368,7 @@ function get_print_section_cm_text(cm_info $cm, $course) { } // Get course context - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $coursecontext = context_course::instance($course->id); $stringoptions = new stdClass; $stringoptions->context = $coursecontext; $instancename = format_string($cm->name, true, $stringoptions); @@ -1592,7 +1592,7 @@ function print_section($course, $section, $mods, $modnamesused, $absolute=false, $accesstext . $content . ''; } - if (!empty($mod->groupingid) && has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) { + if (!empty($mod->groupingid) && has_capability('moodle/course:managegroups', context_course::instance($course->id))) { $groupings = groups_get_all_groupings($course->id); echo " (".format_string($groupings[$mod->groupingid]->name).')'; } @@ -1780,7 +1780,7 @@ function print_section_add_menus($course, $section, $modnames, $vertical=false, global $CFG, $OUTPUT; // check to see if user can add menus - if (!has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $course->id))) { + if (!has_capability('moodle/course:manageactivities', context_course::instance($course->id))) { return false; } @@ -1994,9 +1994,9 @@ function get_module_metadata($course, $modnames, $sectionreturn = 0) { */ function get_category_or_system_context($categoryid) { if ($categoryid) { - return get_context_instance(CONTEXT_COURSECAT, $categoryid); + return context_coursecat::instance($categoryid); } else { - return get_context_instance(CONTEXT_SYSTEM); + return context_system::instance(); } } @@ -2087,7 +2087,7 @@ function make_categories_list(&$list, &$parents, $requiredcapability = '', return; } - $context = get_context_instance(CONTEXT_COURSECAT, $category->id); + $context = context_coursecat::instance($category->id); $categoryname = format_string($category->name, true, array('context' => $context)); // Update $path. @@ -2139,7 +2139,7 @@ function make_categories_list(&$list, &$parents, $requiredcapability = '', */ function get_course_category_tree($id = 0, $depth = 0) { global $DB, $CFG; - $viewhiddencats = has_capability('moodle/category:viewhiddencategories', get_context_instance(CONTEXT_SYSTEM)); + $viewhiddencats = has_capability('moodle/category:viewhiddencategories', context_system::instance()); $categories = get_child_categories($id); $categoryids = array(); foreach ($categories as $key => &$category) { @@ -2184,7 +2184,7 @@ function get_course_category_tree($id = 0, $depth = 0) { continue; } context_instance_preload($course); - if (!empty($course->visible) || has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) { + if (!empty($course->visible) || has_capability('moodle/course:viewhiddencourses', context_course::instance($course->id))) { $categoryids[$course->category]->courses[$course->id] = $course; } } @@ -2209,7 +2209,7 @@ function print_whole_category_list($category=NULL, $displaylist=NULL, $parentsli } if ($category) { - if ($category->visible or has_capability('moodle/category:viewhiddencategories', get_context_instance(CONTEXT_SYSTEM))) { + if ($category->visible or has_capability('moodle/category:viewhiddencategories', context_system::instance())) { print_category_info($category, $depth, $showcourses); } else { return; // Don't bother printing children of invisible categories @@ -2298,7 +2298,7 @@ function print_category_info($category, $depth=0, $showcourses = false) { } $courses = get_courses($category->id, 'c.sortorder ASC', 'c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.summary'); - $context = get_context_instance(CONTEXT_COURSECAT, $category->id); + $context = context_coursecat::instance($category->id); $fullname = format_string($category->name, true, array('context' => $context)); if ($showcourses and $coursecount) { @@ -2479,7 +2479,7 @@ function print_courses($category) { if ($courses) { echo html_writer::start_tag('ul', array('class'=>'unlist')); foreach ($courses as $course) { - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $coursecontext = context_course::instance($course->id); if ($course->visible == 1 || has_capability('moodle/course:viewhiddencourses', $coursecontext)) { echo html_writer::start_tag('li'); print_course($course); @@ -2489,7 +2489,7 @@ function print_courses($category) { echo html_writer::end_tag('ul'); } else { echo $OUTPUT->heading(get_string("nocoursesyet")); - $context = get_context_instance(CONTEXT_SYSTEM); + $context = context_system::instance(); if (has_capability('moodle/course:create', $context)) { $options = array(); if (!empty($category->id)) { @@ -2513,7 +2513,7 @@ function print_courses($category) { function print_course($course, $highlightterms = '') { global $CFG, $USER, $DB, $OUTPUT; - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); // Rewrite file URLs so that they are correct $course->summary = file_rewrite_pluginfile_urls($course->summary, 'pluginfile.php', $context->id, 'course', 'summary', NULL); @@ -3176,8 +3176,8 @@ function make_editing_buttons(stdClass $mod, $absolute_ignored = true, $movesele static $str; - $coursecontext = get_context_instance(CONTEXT_COURSE, $mod->course); - $modcontext = get_context_instance(CONTEXT_MODULE, $mod->id); + $coursecontext = context_course::instance($mod->course); + $modcontext = context_module::instance($mod->id); $editcaps = array('moodle/course:manageactivities', 'moodle/course:activityvisibility', 'moodle/role:assign'); $dupecaps = array('moodle/backup:backuptargetimport', 'moodle/restore:restoretargetimport'); @@ -3391,9 +3391,9 @@ function make_editing_buttons(stdClass $mod, $absolute_ignored = true, $movesele */ function course_format_name ($course,$max=100) { - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); $shortname = format_string($course->shortname, true, array('context' => $context)); - $fullname = format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); + $fullname = format_string($course->fullname, true, array('context' => context_course::instance($course->id))); $str = $shortname.': '. $fullname; if (textlib::strlen($str) <= $max) { return $str; @@ -3561,7 +3561,7 @@ function move_courses($courseids, $categoryid) { } $courseids = array_reverse($courseids); - $newparent = get_context_instance(CONTEXT_COURSECAT, $category->id); + $newparent = context_coursecat::instance($category->id); $i = 1; foreach ($courseids as $courseid) { @@ -3578,7 +3578,7 @@ function move_courses($courseids, $categoryid) { $DB->update_record('course', $course); - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); context_moved($context, $newparent); } } @@ -3641,17 +3641,17 @@ function course_category_show($category) { function move_category($category, $newparentcat) { global $CFG, $DB; - $context = get_context_instance(CONTEXT_COURSECAT, $category->id); + $context = context_coursecat::instance($category->id); $hidecat = false; if (empty($newparentcat->id)) { $DB->set_field('course_categories', 'parent', 0, array('id'=>$category->id)); - $newparent = get_context_instance(CONTEXT_SYSTEM); + $newparent = context_system::instance(); } else { $DB->set_field('course_categories', 'parent', $newparentcat->id, array('id'=>$category->id)); - $newparent = get_context_instance(CONTEXT_COURSECAT, $newparentcat->id); + $newparent = context_coursecat::instance($newparentcat->id); if (!$newparentcat->visible and $category->visible) { // better hide category when moving into hidden category, teachers may unhide afterwards and the hidden children will be restored properly @@ -3784,7 +3784,7 @@ function course_format_ajax_support($format) { function can_delete_course($courseid) { global $USER, $DB; - $context = get_context_instance(CONTEXT_COURSE, $courseid); + $context = context_course::instance($courseid); if (has_capability('moodle/course:delete', $context)) { return true; @@ -3811,7 +3811,7 @@ function can_delete_course($courseid) { */ function save_local_role_names($courseid, $data) { global $DB; - $context = get_context_instance(CONTEXT_COURSE, $courseid); + $context = context_course::instance($courseid); foreach ($data as $fieldname => $value) { if (strpos($fieldname, 'role_') !== 0) { @@ -3886,7 +3886,7 @@ function create_course($data, $editoroptions = NULL) { $data->visibleold = $data->visible; $newcourseid = $DB->insert_record('course', $data); - $context = get_context_instance(CONTEXT_COURSE, $newcourseid, MUST_EXIST); + $context = context_course::instance($newcourseid, MUST_EXIST); if ($editoroptions) { // Save the files used in the summary editor and store @@ -3967,7 +3967,7 @@ function update_course($data, $editoroptions = NULL) { $data->timemodified = time(); $oldcourse = $DB->get_record('course', array('id'=>$data->id), '*', MUST_EXIST); - $context = get_context_instance(CONTEXT_COURSE, $oldcourse->id); + $context = context_course::instance($oldcourse->id); if ($editoroptions) { $data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0); @@ -4003,7 +4003,7 @@ function update_course($data, $editoroptions = NULL) { $course = $DB->get_record('course', array('id'=>$data->id)); if ($movecat) { - $newparent = get_context_instance(CONTEXT_COURSECAT, $course->category); + $newparent = context_coursecat::instance($course->category); context_moved($context, $newparent); } @@ -4340,7 +4340,7 @@ class course_request { $data->lang = $courseconfig->lang; $course = create_course($data); - $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST); + $context = context_course(course->id, MUST_EXIST); // add enrol instances if (!$DB->record_exists('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'))) { @@ -4357,7 +4357,7 @@ class course_request { $this->delete(); $a = new stdClass(); - $a->name = format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))); + $a->name = format_string($course->fullname, true, array('context' => context_course::instance($course->id))); $a->url = $CFG->wwwroot.'/course/view.php?id=' . $course->id; $this->notify($user, $USER, 'courserequestapproved', get_string('courseapprovedsubject'), get_string('courseapprovedemail2', 'moodle', $a)); diff --git a/course/loginas.php b/course/loginas.php index d189b5786d8..b4e5ea70e74 100644 --- a/course/loginas.php +++ b/course/loginas.php @@ -36,8 +36,8 @@ $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST); /// User must be logged in -$systemcontext = get_context_instance(CONTEXT_SYSTEM); -$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); +$systemcontext = context_system::instance(); +$coursecontext = context_course::instance($course->id); require_login(); diff --git a/course/mod.php b/course/mod.php index 181c6c9d21a..46062a36304 100644 --- a/course/mod.php +++ b/course/mod.php @@ -85,8 +85,8 @@ if (!empty($add)) { $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); require_login($course, false, $cm); - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $coursecontext = context_course::instance($course->id); + $modcontext = context_module::instance($cm->id); require_capability('moodle/course:manageactivities', $coursecontext); if (!$confirm or !confirm_sesskey()) { @@ -122,8 +122,8 @@ if (!empty($add)) { $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); require_login($course, false, $cm); - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $coursecontext = context_course::instance($course->id); + $modcontext = context_module::instance($cm->id); require_capability('moodle/course:manageactivities', $modcontext); $return = course_get_url($course, $sectionreturn); @@ -201,8 +201,8 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); require_login($course, false, $cm); - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $coursecontext = context_course::instance($course->id); + $modcontext = context_module::instance($cm->id); require_capability('moodle/course:manageactivities', $modcontext); if (!empty($movetosection)) { @@ -247,8 +247,8 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); require_login($course, false, $cm); - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $coursecontext = context_course::instance($course->id); + $modcontext = context_module::instance($cm->id); require_capability('moodle/course:manageactivities', $modcontext); $cm->indent += $indent; @@ -272,8 +272,8 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); require_login($course, false, $cm); - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $coursecontext = context_course::instance($course->id); + $modcontext = context_module::instance($cm->id); require_capability('moodle/course:activityvisibility', $modcontext); set_coursemodule_visible($cm->id, 0); @@ -291,8 +291,8 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); require_login($course, false, $cm); - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $coursecontext = context_course::instance($course->id); + $modcontext = context_module::instance($cm->id); require_capability('moodle/course:activityvisibility', $modcontext); $section = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST); @@ -317,8 +317,8 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); require_login($course, false, $cm); - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $coursecontext = context_course::instance($course->id); + $modcontext = context_module::instance($cm->id); require_capability('moodle/course:manageactivities', $modcontext); set_coursemodule_groupmode($cm->id, $groupmode); @@ -336,8 +336,8 @@ if ((!empty($movetosection) or !empty($moveto)) and confirm_sesskey()) { $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST); require_login($course, false, $cm); - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); - $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $coursecontext = context_course::instance($course->id); + $modcontext = context_module::instance($cm->id); require_capability('moodle/course:manageactivities', $modcontext); $section = $DB->get_record('course_sections', array('id'=>$cm->section), '*', MUST_EXIST); diff --git a/course/modduplicate.php b/course/modduplicate.php index e8c5c1b3a43..c1bafc5a1f3 100644 --- a/course/modduplicate.php +++ b/course/modduplicate.php @@ -38,8 +38,8 @@ $sectionreturn = optional_param('sr', 0, PARAM_INT); $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); $cm = get_coursemodule_from_id('', $cmid, $course->id, true, MUST_EXIST); -$cmcontext = get_context_instance(CONTEXT_MODULE, $cm->id); -$context = get_context_instance(CONTEXT_COURSE, $courseid); +$cmcontext = context_module::instance($cm->id); +$context = context_course::instance($courseid); $section = $DB->get_record('course_sections', array('id' => $cm->section, 'course' => $cm->course)); require_login($course); diff --git a/course/modedit.php b/course/modedit.php index ff9861382d6..adc9692c808 100644 --- a/course/modedit.php +++ b/course/modedit.php @@ -56,7 +56,7 @@ if (!empty($add)) { $module = $DB->get_record('modules', array('name'=>$add), '*', MUST_EXIST); require_login($course); - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); require_capability('moodle/course:manageactivities', $context); $cw = get_course_section($section, $course->id); @@ -131,7 +131,7 @@ if (!empty($add)) { $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST); require_login($course, false, $cm); // needed to setup proper $COURSE - $context = get_context_instance(CONTEXT_MODULE, $cm->id); + $context = context_module::instance($cm->id); require_capability('moodle/course:manageactivities', $context); $module = $DB->get_record('modules', array('id'=>$cm->module), '*', MUST_EXIST); @@ -282,9 +282,9 @@ if ($mform->is_cancelled()) { } if (!empty($fromform->coursemodule)) { - $context = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule); + $context = context_module::instance($fromform->coursemodule); } else { - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); } $fromform->course = $course->id; @@ -356,7 +356,7 @@ if ($mform->is_cancelled()) { $DB->update_record('course_modules', $cm); - $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule); + $modcontext = context_module::instance($fromform->coursemodule); // update embedded links and save files if (plugin_supports('mod', $fromform->modulename, FEATURE_MOD_INTRO, true)) { @@ -448,7 +448,7 @@ if ($mform->is_cancelled()) { if (!$returnfromfunc or !is_number($returnfromfunc)) { // undo everything we can - $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule); + $modcontext = context_module::instance($fromform->coursemodule); delete_context(CONTEXT_MODULE, $fromform->coursemodule); $DB->delete_records('course_modules', array('id'=>$fromform->coursemodule)); @@ -464,7 +464,7 @@ if ($mform->is_cancelled()) { $DB->set_field('course_modules', 'instance', $returnfromfunc, array('id'=>$fromform->coursemodule)); // update embedded links and save files - $modcontext = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule); + $modcontext = context_module::instance($fromform->coursemodule); if (!empty($introeditor)) { $fromform->intro = file_save_draft_area_files($introeditor['itemid'], $modcontext->id, 'mod_'.$fromform->modulename, 'intro', 0, @@ -643,9 +643,9 @@ if ($mform->is_cancelled()) { $strmodulenameplural = get_string('modulenameplural', $module->name); if (!empty($cm->id)) { - $context = get_context_instance(CONTEXT_MODULE, $cm->id); + $context = context_module::instance($cm->id); } else { - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); } $PAGE->set_heading($course->fullname); diff --git a/course/moodleform_mod.php b/course/moodleform_mod.php index 376283a4004..f71c89b9c38 100644 --- a/course/moodleform_mod.php +++ b/course/moodleform_mod.php @@ -55,9 +55,9 @@ abstract class moodleform_mod extends moodleform { $this->_section = $section; $this->_cm = $cm; if ($this->_cm) { - $this->context = get_context_instance(CONTEXT_MODULE, $this->_cm->id); + $this->context = context_module::instance($this->_cm->id); } else { - $this->context = get_context_instance(CONTEXT_COURSE, $course->id); + $this->context = context_course::instance($course->id); } // Guess module name @@ -424,7 +424,7 @@ abstract class moodleform_mod extends moodleform { $permission=CAP_ALLOW; $rolenamestring = null; if (!empty($this->_cm)) { - $context = get_context_instance(CONTEXT_MODULE, $this->_cm->id); + $context = context_module::instance($this->_cm->id); $rolenames = get_role_names_with_caps_in_context($context, array('moodle/rating:rate', 'mod/'.$this->_cm->modname.':rate')); $rolenamestring = implode(', ', $rolenames); @@ -487,7 +487,7 @@ abstract class moodleform_mod extends moodleform { $mform->addElement('modvisible', 'visible', get_string('visible')); if (!empty($this->_cm)) { - $context = get_context_instance(CONTEXT_MODULE, $this->_cm->id); + $context = context_module::instance($this->_cm->id); if (!has_capability('moodle/course:activityvisibility', $context)) { $mform->hardFreeze('visible'); } diff --git a/course/pending.php b/course/pending.php index 74c05b3dbb2..205a42977e7 100644 --- a/course/pending.php +++ b/course/pending.php @@ -36,7 +36,7 @@ require_once($CFG->dirroot . '/course/lib.php'); require_once($CFG->dirroot . '/course/request_form.php'); require_login(); -require_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM)); +require_capability('moodle/site:approvecourse', context_system::instance()); $approve = optional_param('approve', 0, PARAM_INT); $reject = optional_param('reject', 0, PARAM_INT); diff --git a/course/publish/backup.php b/course/publish/backup.php index ef59eb8522e..cc9c62a9983 100644 --- a/course/publish/backup.php +++ b/course/publish/backup.php @@ -47,7 +47,7 @@ $hubname = optional_param('hubname', '', PARAM_TEXT); //some permissions and parameters checking $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST); require_login($course); -if (!has_capability('moodle/course:publish', get_context_instance(CONTEXT_COURSE, $id)) +if (!has_capability('moodle/course:publish', context_course::instance($id)) or !confirm_sesskey()) { throw new moodle_exception('nopermission'); } diff --git a/course/publish/index.php b/course/publish/index.php index f868ca39a73..d401e5c345b 100644 --- a/course/publish/index.php +++ b/course/publish/index.php @@ -37,7 +37,7 @@ $huburl = optional_param('huburl', 0, PARAM_URL); $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST); require_login($course); -$context = get_context_instance(CONTEXT_COURSE, $course->id); +$context = context_course::instance($course->id); $shortname = format_string($course->shortname, true, array('context' => $context)); $PAGE->set_url('/course/publish/index.php', array('id' => $course->id)); @@ -56,7 +56,7 @@ if (!extension_loaded('xmlrpc')) { die(); } -if (has_capability('moodle/course:publish', get_context_instance(CONTEXT_COURSE, $id))) { +if (has_capability('moodle/course:publish', context_course::instance($id))) { $publicationmanager = new course_publish_manager(); $confirmmessage = ''; diff --git a/course/publish/metadata.php b/course/publish/metadata.php index 9067f795848..298e5f3e1d4 100644 --- a/course/publish/metadata.php +++ b/course/publish/metadata.php @@ -53,7 +53,7 @@ $PAGE->set_heading($course->fullname); if (!extension_loaded('xmlrpc')) { $errornotification = $OUTPUT->doc_link('admin/environment/php_extension/xmlrpc', ''); $errornotification .= get_string('xmlrpcdisabledpublish', 'hub'); - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); $shortname = format_string($course->shortname, true, array('context' => $context)); echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('publishcourse', 'hub', $shortname), 3, 'main'); @@ -62,7 +62,7 @@ if (!extension_loaded('xmlrpc')) { die(); } -if (has_capability('moodle/course:publish', get_context_instance(CONTEXT_COURSE, $id))) { +if (has_capability('moodle/course:publish', context_course::instance($id))) { //retrieve hub name and hub url $huburl = optional_param('huburl', '', PARAM_URL); @@ -135,7 +135,7 @@ if (has_capability('moodle/course:publish', get_context_instance(CONTEXT_COURSE, } //retrieve the content information from the course - $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $coursecontext = context_course::instance($course->id); $courseblocks = $publicationmanager->get_block_instances_by_context($coursecontext->id, 'blockname'); if (!empty($courseblocks)) { @@ -177,7 +177,7 @@ if (has_capability('moodle/course:publish', get_context_instance(CONTEXT_COURSE, if (!empty($fromform->screenshots)) { $screenshots = $fromform->screenshots; $fs = get_file_storage(); - $files = $fs->get_area_files(get_context_instance(CONTEXT_USER, $USER->id)->id, 'user', 'draft', $screenshots); + $files = $fs->get_area_files(context_user::instance($USER->id)->id, 'user', 'draft', $screenshots); if (!empty($files)) { $courseinfo->screenshots = $courseinfo->screenshots + count($files) - 1; //minus the ./ directory } diff --git a/course/recent.php b/course/recent.php index 8b31e29835f..71c83f52568 100644 --- a/course/recent.php +++ b/course/recent.php @@ -39,7 +39,7 @@ require_login($course); add_to_log($course->id, "course", "recent", "recent.php?id=$course->id", $course->id); -$context = get_context_instance(CONTEXT_COURSE, $course->id); +$context = context_course::instance($course->id); $lastlogin = time() - COURSE_MAX_RECENT_PERIOD; if (!isguestuser() and !empty($USER->lastcourseaccess[$COURSE->id])) { @@ -248,7 +248,7 @@ if (!empty($activities)) { } else { if (!isset($viewfullnames[$activity->cmid])) { - $cm_context = get_context_instance(CONTEXT_MODULE, $activity->cmid); + $cm_context = context_module::instance($activity->cmid); $viewfullnames[$activity->cmid] = has_capability('moodle/site:viewfullnames', $cm_context); } diff --git a/course/recent_form.php b/course/recent_form.php index b79e44372f8..642a24dfdef 100644 --- a/course/recent_form.php +++ b/course/recent_form.php @@ -34,7 +34,7 @@ class recent_form extends moodleform { global $CFG, $COURSE, $USER; $mform =& $this->_form; - $context = get_context_instance(CONTEXT_COURSE, $COURSE->id); + $context = context_course::instance($COURSE->id); $modinfo = get_fast_modinfo($COURSE); $sections = get_all_sections($COURSE->id); @@ -65,13 +65,13 @@ class recent_form extends moodleform { } if ($COURSE->id == SITEID) { - $viewparticipants = has_capability('moodle/site:viewparticipants', get_context_instance(CONTEXT_SYSTEM)); + $viewparticipants = has_capability('moodle/site:viewparticipants', context_system::instance()); } else { $viewparticipants = has_capability('moodle/course:viewparticipants', $context); } if ($viewparticipants) { - $viewfullnames = has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $COURSE->id)); + $viewfullnames = has_capability('moodle/site:viewfullnames', context_course::instance($COURSE->id)); $options = array(); $options[0] = get_string('allparticipants'); diff --git a/course/renderer.php b/course/renderer.php index 2206a5e22b1..2814e218d45 100644 --- a/course/renderer.php +++ b/course/renderer.php @@ -107,7 +107,7 @@ class core_course_renderer extends plugin_renderer_base { $classes[] = 'collapsed'; } } - $categoryname = format_string($category->name, true, array('context' => get_context_instance(CONTEXT_COURSECAT, $category->id))); + $categoryname = format_string($category->name, true, array('context' => context_coursecat::instance($category->id))); $content .= html_writer::start_tag('div', array('class'=>join(' ', $classes))); $content .= html_writer::start_tag('div', array('class'=>'category_label')); diff --git a/course/report.php b/course/report.php index 6520311c006..5cf8d7a6b52 100644 --- a/course/report.php +++ b/course/report.php @@ -9,7 +9,7 @@ $PAGE->set_pagelayout('standard'); require_login($course); - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); require_capability('moodle/site:viewreports', $context); // basic capability for listing of reports $strreports = get_string('reports'); diff --git a/course/request.php b/course/request.php index 3e0e097b903..bf6fefcafd0 100644 --- a/course/request.php +++ b/course/request.php @@ -41,7 +41,7 @@ if (isguestuser()) { if (empty($CFG->enablecourserequests)) { print_error('courserequestdisabled', '', $returnurl); } -$context = get_context_instance(CONTEXT_SYSTEM); +$context = context_system::instance(); $PAGE->set_context($context); require_capability('moodle/course:request', $context); diff --git a/course/reset.php b/course/reset.php index cd0a66b640d..528c90886b8 100644 --- a/course/reset.php +++ b/course/reset.php @@ -39,7 +39,7 @@ if (!$course = $DB->get_record('course', array('id'=>$id))) { $PAGE->set_url('/course/reset.php', array('id'=>$id)); require_login($course); -require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id)); +require_capability('moodle/course:update', context_course::instance($course->id)); $strreset = get_string('reset'); $strresetcourse = get_string('resetcourse'); diff --git a/course/reset_form.php b/course/reset_form.php index cb899f0d63b..f3407253998 100644 --- a/course/reset_form.php +++ b/course/reset_form.php @@ -26,7 +26,7 @@ class course_reset_form extends moodleform { $mform->addElement('header', 'rolesheader', get_string('roles')); - $roles = get_assignable_roles(get_context_instance(CONTEXT_COURSE, $COURSE->id)); + $roles = get_assignable_roles(context_course::instance($COURSE->id)); $roles[0] = get_string('noroles', 'role'); $roles = array_reverse($roles, true); diff --git a/course/rest.php b/course/rest.php index 6efd228db65..c8073a77f4b 100644 --- a/course/rest.php +++ b/course/rest.php @@ -54,11 +54,11 @@ $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); if (in_array($class, array('resource'))) { $cm = get_coursemodule_from_id(null, $id, $course->id, false, MUST_EXIST); require_login($course, false, $cm); - $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id); + $modcontext = context_module::instance($cm->id); } else { require_login($course); } -$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); +$coursecontext = context_course::instance($course->id); require_sesskey(); echo $OUTPUT->header(); // send headers diff --git a/course/scales.php b/course/scales.php index 07bf4fabb3c..37d64738c2e 100644 --- a/course/scales.php +++ b/course/scales.php @@ -39,11 +39,11 @@ $PAGE->set_url($url); $context = null; if ($course = $DB->get_record('course', array('id'=>$id))) { require_login($course); - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); } else { //$id will be 0 for site level scales require_login(); - $context = get_context_instance(CONTEXT_SYSTEM); + $context = context_system::instance(); } $PAGE->set_context($context); @@ -81,7 +81,7 @@ if ($scaleid) { } } -$systemcontext = get_context_instance(CONTEXT_SYSTEM); +$systemcontext = context_system::instance(); if ($scales = $DB->get_records("scale", array("courseid"=>$course->id), "name ASC")) { echo $OUTPUT->heading($strcustomscales); diff --git a/course/switchrole.php b/course/switchrole.php index 12cba2041e8..46273d1d9d6 100644 --- a/course/switchrole.php +++ b/course/switchrole.php @@ -47,7 +47,7 @@ if (! ($course = $DB->get_record('course', array('id'=>$id)))) { print_error('invalidcourseid', 'error'); } -if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) { +if (!$context = context_course::instance($course->id)) { print_error('nocontext'); } diff --git a/course/togglecompletion.php b/course/togglecompletion.php index 19a584bb6fe..3773890099a 100644 --- a/course/togglecompletion.php +++ b/course/togglecompletion.php @@ -41,7 +41,7 @@ if ($courseid) { // Check user is logged in $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); - $context = get_context_instance(CONTEXT_COURSE, $course->id); + $context = context_course::instance($course->id); require_login($course); $completion = new completion_info($course); diff --git a/course/user.php b/course/user.php index 7f7535e3e65..73839e183b2 100644 --- a/course/user.php +++ b/course/user.php @@ -52,8 +52,8 @@ if ($mode === 'coursecompletions' or $mode === 'coursecompletion') { redirect($url); } -$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); -$personalcontext = get_context_instance(CONTEXT_USER, $user->id); +$coursecontext = context_course::instance($course->id); +$personalcontext = context_user::instance($user->id); $PAGE->set_url('/course/user.php', array('id'=>$id, 'user'=>$user->id, 'mode'=>$mode)); From 1f364c87e37f446a7fb537828055e1a48121c7b0 Mon Sep 17 00:00:00 2001 From: Ankit Agarwal Date: Tue, 24 Jul 2012 11:49:44 +0800 Subject: [PATCH 2/3] MDL-34459 course: changing strictness of context calls as needed --- course/completion_form.php | 2 +- course/externallib.php | 6 +++--- course/lib.php | 2 +- course/switchrole.php | 4 +--- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/course/completion_form.php b/course/completion_form.php index 56136d15fe4..1f681017bbd 100644 --- a/course/completion_form.php +++ b/course/completion_form.php @@ -134,7 +134,7 @@ class course_completion_form extends moodleform { // Role completion criteria $mform->addElement('header', 'roles', get_string('manualcompletionby', 'completion')); - $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, context_course::instance($course->id)); + $roles = get_roles_with_capability('moodle/course:markcomplete', CAP_ALLOW, context_course::instance($course->id, IGNORE_MISSING)); if (!empty($roles)) { $mform->addElement('select', 'role_aggregation', get_string('aggregationmethod', 'completion'), $aggregation_methods); diff --git a/course/externallib.php b/course/externallib.php index cfb47ac3402..22e2e4ef9f0 100644 --- a/course/externallib.php +++ b/course/externallib.php @@ -85,7 +85,7 @@ class core_course_external extends external_api { } // now security checks - $context = context_course::instance($course->id); + $context = context_course::instance($course->id, IGNORE_MISSING); try { self::validate_context($context); } catch (Exception $e) { @@ -297,7 +297,7 @@ class core_course_external extends external_api { foreach ($courses as $course) { // now security checks - $context = context_course::instance($course->id); + $context = context_course::instance($course->id, IGNORE_MISSING); try { self::validate_context($context); } catch (Exception $e) { @@ -511,7 +511,7 @@ class core_course_external extends external_api { foreach ($params['courses'] as $course) { // Ensure the current user is allowed to run this function - $context = context_coursecat::instance($course['categoryid']); + $context = context_coursecat::instance($course['categoryid'], IGNORE_MISSING); try { self::validate_context($context); } catch (Exception $e) { diff --git a/course/lib.php b/course/lib.php index 22bb86b737a..87d220d2117 100644 --- a/course/lib.php +++ b/course/lib.php @@ -4340,7 +4340,7 @@ class course_request { $data->lang = $courseconfig->lang; $course = create_course($data); - $context = context_course(course->id, MUST_EXIST); + $context = context_course::instance($course->id, MUST_EXIST); // add enrol instances if (!$DB->record_exists('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'))) { diff --git a/course/switchrole.php b/course/switchrole.php index 46273d1d9d6..719762e5ad8 100644 --- a/course/switchrole.php +++ b/course/switchrole.php @@ -47,9 +47,7 @@ if (! ($course = $DB->get_record('course', array('id'=>$id)))) { print_error('invalidcourseid', 'error'); } -if (!$context = context_course::instance($course->id)) { - print_error('nocontext'); -} +$context = context_course::instance($course->id); // Remove any switched roles before checking login if ($switchrole == 0) { From 4658aec5eb724d3f2f98dc837880ef702b9fc44b Mon Sep 17 00:00:00 2001 From: Ankit Agarwal Date: Tue, 24 Jul 2012 11:52:38 +0800 Subject: [PATCH 3/3] MDL-34459 course: Changing strictness of context call in get_category_or_system_context() to keep it consistent with past usages --- course/lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/course/lib.php b/course/lib.php index 87d220d2117..c2bc4054a78 100644 --- a/course/lib.php +++ b/course/lib.php @@ -1994,7 +1994,7 @@ function get_module_metadata($course, $modnames, $sectionreturn = 0) { */ function get_category_or_system_context($categoryid) { if ($categoryid) { - return context_coursecat::instance($categoryid); + return context_coursecat::instance($categoryid, IGNORE_MISSING); } else { return context_system::instance(); }