Skip to content

Commit

Permalink
MDL-40424 libraries: Deprecate context_instance_preload_sql()
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Massart committed Jul 12, 2013
1 parent ac11252 commit 2e4c0c9
Show file tree
Hide file tree
Showing 19 changed files with 108 additions and 48 deletions.
5 changes: 3 additions & 2 deletions admin/handlevirus.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
$action = clam_handle_infected_file($file,$log->userid,true);
clam_replace_infected_file($file);

list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$sql = "SELECT c.id, c.fullname $ctxselect FROM {course} c $ctxjoin WHERE c.id = :courseid";
$course = $DB->get_record_sql($sql, array('courseid' => $log->course));
$course = $DB->get_record_sql($sql, array('courseid' => $log->course, 'contextlevel' => CONTEXT_COURSE));
context_helper::preload_from_record($course);

$user = $DB->get_record("user", array("id"=>$log->userid));
Expand Down
5 changes: 3 additions & 2 deletions admin/oacleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ function online_assignment_cleanup($output=false) {


/// get a list of all courses on this site
list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$sql = "SELECT c.* $ctxselect FROM {course} c $ctxjoin";
$courses = $DB->get_records_sql($sql);
$courses = $DB->get_records_sql($sql, array('contextlevel' => CONTEXT_COURSE));

/// cycle through each course
foreach ($courses as $course) {
Expand Down
8 changes: 6 additions & 2 deletions backup/util/ui/restore_ui_components.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,10 @@ protected function setup_restrictions() {
protected function get_searchsql() {
global $DB;

list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$params = array(
'contextlevel' => CONTEXT_COURSE,
'fullnamesearch' => '%'.$this->get_search().'%',
'shortnamesearch' => '%'.$this->get_search().'%',
'siteid' => SITEID
Expand Down Expand Up @@ -326,8 +328,10 @@ public function __construct(array $config=array()) {
protected function get_searchsql() {
global $DB;

list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT, 'ctx');
$ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$params = array(
'contextlevel' => CONTEXT_COURSECAT,
'namesearch' => '%'.$this->get_search().'%',
);

Expand Down
5 changes: 3 additions & 2 deletions calendar/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1568,13 +1568,14 @@ function calendar_get_default_courses() {

$courses = array();
if (!empty($CFG->calendar_adminseesall) && has_capability('moodle/calendar:manageentries', context_system::instance())) {
list ($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$select = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$join = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$sql = "SELECT c.* $select
FROM {course} c
$join
WHERE EXISTS (SELECT 1 FROM {event} e WHERE e.courseid = c.id)
";
$courses = $DB->get_records_sql($sql, null, 0, 20);
$courses = $DB->get_records_sql($sql, array('contextlevel' => CONTEXT_COURSE), 0, 20);
foreach ($courses as $course) {
context_helper::preload_from_record($course);
}
Expand Down
5 changes: 3 additions & 2 deletions enrol/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,9 @@ public static function get_enrolled_users($courseid, $options = array()) {
}

list($enrolledsql, $enrolledparams) = get_enrolled_sql($coursecontext, $withcapability, $groupid, $onlyactive);
list($ctxselect, $ctxjoin) = context_instance_preload_sql('u.id', CONTEXT_USER, 'ctx');
$sqlparams['courseid'] = $courseid;
$ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)";
$enrolledparams['contextlevel'] = CONTEXT_USER;
$sql = "SELECT u.* $ctxselect
FROM {user} u $ctxjoin
WHERE u.id IN ($enrolledsql)
Expand Down
5 changes: 3 additions & 2 deletions enrol/meta/addinstance_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ function definition() {

// TODO: this has to be done via ajax or else it will fail very badly on large sites!
$courses = array('' => get_string('choosedots'));
list ($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$select = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$join = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$sql = "SELECT c.id, c.fullname, c.shortname, c.visible $select FROM {course} c $join ORDER BY c.sortorder ASC";
$rs = $DB->get_recordset_sql($sql);
$rs = $DB->get_recordset_sql($sql, array('contextlevel' => CONTEXT_COURSE));
foreach ($rs as $c) {
if ($c->id == SITEID or $c->id == $course->id or isset($existing[$c->id])) {
continue;
Expand Down
15 changes: 0 additions & 15 deletions lib/accesslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -7230,21 +7230,6 @@ protected static function build_paths($force) {
// before removing devs will be warned with a debugging message first,
// then we will add error message and only after that we can remove the functions
// completely.
/**
* Preloads context information together with instances.
* Use context_helper::preload_from_record() to strip the context info from the record and cache the context instance.
*
* @deprecated since 2.2
* @param string $joinon for example 'u.id'
* @param string $contextlevel context level of instance in $joinon
* @param string $tablealias context table alias
* @return array with two values - select and join part
*/
function context_instance_preload_sql($joinon, $contextlevel, $tablealias) {
$select = ", ".context_helper::get_preload_record_columns_sql($tablealias);
$join = "LEFT JOIN {context} $tablealias ON ($tablealias.instanceid = $joinon AND $tablealias.contextlevel = $contextlevel)";
return array($select, $join);
}

/**
* Runs get_records select on context table and returns the result
Expand Down
4 changes: 3 additions & 1 deletion lib/blocklib.php
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,11 @@ public function load_blocks($includeinvisible = null) {
list($pagetypepatterntest, $pagetypepatternparams) =
$DB->get_in_or_equal($pagetypepatterns, SQL_PARAMS_NAMED, 'pagetypepatterntest');

list($ccselect, $ccjoin) = context_instance_preload_sql('bi.id', CONTEXT_BLOCK, 'ctx');
$ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = bi.id AND ctx.contextlevel = :contextlevel)";

$params = array(
'contextlevel' => CONTEXT_BLOCK,
'subpage1' => $this->page->subpage,
'subpage2' => $this->page->subpage,
'contextid1' => $context->id,
Expand Down
13 changes: 10 additions & 3 deletions lib/datalib.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,9 @@ function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*")

$visiblecourses = array();

list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$params['contextlevel'] = CONTEXT_COURSE;

$sql = "SELECT $fields $ccselect
FROM {course} c
Expand Down Expand Up @@ -681,7 +683,9 @@ function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c
$categoryselect = "";
}

list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$params['contextlevel'] = CONTEXT_COURSE;

$totalcount = 0;
if (!$limitfrom) {
Expand Down Expand Up @@ -799,7 +803,10 @@ function get_courses_search($searchterms, $sort, $page, $recordsperpage, &$total
$limitfrom = $page * $recordsperpage;
$limitto = $limitfrom + $recordsperpage;

list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$params['contextlevel'] = CONTEXT_COURSE;

$fields = array_diff(array_keys($DB->get_columns('course')), array('modinfo', 'sectioncache'));
$sql = "SELECT c.".join(',c.',$fields)." $ccselect
FROM {course} c
Expand Down
29 changes: 29 additions & 0 deletions lib/deprecatedlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4306,4 +4306,33 @@ function get_courseid_from_context(context $context) {
} else {
return false;
}

/**
* Preloads context information together with instances.
* Use context_instance_preload() to strip the context info from the record and cache the context instance.
*
* If you are using this methid, you should have something like this:
*
* list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
*
* To prevent the use of this deprecated function, replace the line above with something similar to this:
*
* $ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
* ^
* $ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
* ^ ^ ^ ^
* $params = array('contextlevel' => CONTEXT_COURSE);
* ^
* @see context_helper:;get_preload_record_columns_sql()
* @deprecated since 2.2
* @param string $joinon for example 'u.id'
* @param string $contextlevel context level of instance in $joinon
* @param string $tablealias context table alias
* @return array with two values - select and join part
*/
function context_instance_preload_sql($joinon, $contextlevel, $tablealias) {
debugging('context_instance_preload_sql() is deprecated, please use context_helper::get_preload_record_columns_sql() instead.', DEBUG_DEVELOPER);
$select = ", " . context_helper::get_preload_record_columns_sql($tablealias);
$join = "LEFT JOIN {context} $tablealias ON ($tablealias.instanceid = $joinon AND $tablealias.contextlevel = $contextlevel)";
return array($select, $join);
}
12 changes: 9 additions & 3 deletions lib/enrollib.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ function enrol_get_shared_courses($user1, $user2, $preloadcontexts = false, $che
$ctxselect = '';
$ctxjoin = '';
if ($preloadcontexts) {
list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$params['contextlevel'] = CONTEXT_COURSE;
}

$sql = "SELECT c.* $ctxselect
Expand Down Expand Up @@ -572,7 +574,9 @@ function enrol_get_my_courses($fields = NULL, $sort = 'visible DESC,sortorder AS
}

$coursefields = 'c.' .join(',c.', $fields);
list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$params['contextlevel'] = CONTEXT_COURSE;
$wheres = implode(" AND ", $wheres);

//note: we can not use DISTINCT + text fields due to Oracle and MS limitations, that is why we have the subselect there
Expand Down Expand Up @@ -830,7 +834,9 @@ function enrol_get_all_users_courses($userid, $onlyactive = false, $fields = NUL
}

$coursefields = 'c.' .join(',c.', $fields);
list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$params['contextlevel'] = CONTEXT_COURSE;

//note: we can not use DISTINCT + text fields due to Oracle and MS limitations, that is why we have the subselect there
$sql = "SELECT $coursefields $ccselect
Expand Down
13 changes: 9 additions & 4 deletions lib/navigationlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1423,8 +1423,10 @@ protected function load_all_courses($categoryids = null) {
if (count($fullfetch)) {
// First up fetch all of the courses in categories where we know that we are going to
// need the majority of courses.
list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
list($categoryids, $categoryparams) = $DB->get_in_or_equal($fullfetch, SQL_PARAMS_NAMED, 'lcategory');
$ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$categoryparams['contextlevel'] = CONTEXT_COURSE;
$sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect
FROM {course} c
$ccjoin
Expand Down Expand Up @@ -1458,13 +1460,14 @@ protected function load_all_courses($categoryids = null) {
// Next we will work our way through the categories where we will likely only need a small
// proportion of the courses.
foreach ($partfetch as $categoryid) {
list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect
FROM {course} c
$ccjoin
WHERE c.category = :categoryid
ORDER BY c.sortorder ASC";
$courseparams = array('categoryid' => $categoryid);
$courseparams = array('categoryid' => $categoryid, 'contextlevel' => CONTEXT_COURSE);
$coursesrs = $DB->get_recordset_sql($sql, $courseparams, 0, $limit * 5);
foreach ($coursesrs as $course) {
if ($course->id == $SITE->id) {
Expand Down Expand Up @@ -1492,8 +1495,10 @@ protected function load_all_courses($categoryids = null) {
}
} else {
// Prepare the SQL to load the courses and their contexts
list($ccselect, $ccjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
list($courseids, $courseparams) = $DB->get_in_or_equal(array_keys($this->addedcourses), SQL_PARAMS_NAMED, 'lc', false);
$ccselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$courseparams['contextlevel'] = CONTEXT_COURSE;
$sql = "SELECT c.id, c.sortorder, c.visible, c.fullname, c.shortname, c.category $ccselect
FROM {course} c
$ccjoin
Expand Down
3 changes: 3 additions & 0 deletions lib/tests/accesslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2428,6 +2428,9 @@ public function test_permission_evaluation() {

context_helper::reset_caches();
list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSECAT, 'ctx');
$this->assertDebuggingCalled('context_instance_preload_sql() is deprecated, please use context_helper::get_preload_record_columns_sql() instead.', DEBUG_DEVELOPER);
$this->assertEquals(', ' . context_helper::get_preload_record_columns_sql('ctx'), $select);
$this->assertEquals('LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = ' . CONTEXT_COURSECAT . ')', $join);
$sql = "SELECT c.id $select FROM {course_categories} c $join";
$records = $DB->get_records_sql($sql);
foreach ($records as $record) {
Expand Down
1 change: 1 addition & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Accesslib:
* context_moved() -> context::update_moved()
* preload_course_contexts() -> context_helper::preload_course()
* context_instance_preload() -> context_helper::preload_from_record()
* context_instance_preload_sql()-> context_helper::get_preload_record_columns_sql()
* get_contextlevel_name() -> context_helper::get_level_name()
* create_contexts() -> context_helper::create_instances()
* cleanup_contexts() -> context_helper::cleanup_instances()
Expand Down
4 changes: 3 additions & 1 deletion mod/forum/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -8186,7 +8186,9 @@ function forum_get_courses_user_posted_in($user, $discussionsonly = false, $incl

// Join to the context table so that we can preload contexts if required.
if ($includecontexts) {
list($ctxselect, $ctxjoin) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$params['contextlevel'] = CONTEXT_COURSE;
} else {
$ctxselect = '';
$ctxjoin = '';
Expand Down
8 changes: 5 additions & 3 deletions rating/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -791,15 +791,17 @@ public function get_user_grades($options) {
$modulename = $options->modulename;
$moduleid = intval($options->moduleid);

//going direct to the db for the context id seems wrong
list($ctxselect, $ctxjoin) = context_instance_preload_sql('cm.id', CONTEXT_MODULE, 'ctx');
// Going direct to the db for the context id seems wrong.
$ctxselect = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$ctxjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = cm.id AND ctx.contextlevel = :contextlevel)";
$sql = "SELECT cm.* $ctxselect
FROM {course_modules} cm
LEFT JOIN {modules} mo ON mo.id = cm.module
LEFT JOIN {{$modulename}} m ON m.id = cm.instance $ctxjoin
WHERE mo.name=:modulename AND
m.id=:moduleid";
$contextrecord = $DB->get_record_sql($sql, array('modulename'=>$modulename, 'moduleid'=>$moduleid), '*', MUST_EXIST);
$params = array('modulename' => $modulename, 'moduleid' => $moduleid, 'contextlevel' => CONTEXT_MODULE);
$contextrecord = $DB->get_record_sql($sql, $params, '*', MUST_EXIST);
$contextid = $contextrecord->ctxid;
}

Expand Down
5 changes: 3 additions & 2 deletions report/backups/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@
$strskipped = get_string("skipped");
$strwarning = get_string("warning");

list($select, $join) = context_instance_preload_sql('c.id', CONTEXT_COURSE, 'ctx');
$select = ', ' . context_helper::get_preload_record_columns_sql('ctx');
$join = "LEFT JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel)";
$sql = "SELECT bc.*, c.fullname $select
FROM {backup_courses} bc
JOIN {course} c ON c.id = bc.courseid
$join";
$rs = $DB->get_recordset_sql($sql);
$rs = $DB->get_recordset_sql($sql, array('contextlevel' => CONTEXT_COURSE));
foreach ($rs as $backuprow) {

// Cache the course context
Expand Down
Loading

0 comments on commit 2e4c0c9

Please sign in to comment.