MDL-17556: Improve performance of get_fast_modinfo when courses have many activities

This commit is contained in:
sam_marshall
2008-12-18 17:52:29 +00:00
parent 6093af9bc4
commit 00653161b5
3 changed files with 57 additions and 12 deletions
+50
View File
@@ -2407,6 +2407,56 @@ function cleanup_contexts() {
return true;
}
/**
* Preloads all contexts relating to a course: course, modules, and blocks.
*
* @param int $courseid Course ID
*/
function preload_course_contexts($courseid) {
global $context_cache, $context_cache_id, $CFG, $DB;
// Users can call this multiple times without doing any harm
static $preloadedcourses=array();
if(array_key_exists($courseid,$preloadedcourses)) {
return;
}
$rs=$DB->get_recordset_sql("
SELECT
x.instanceid, x.id, x.contextlevel, x.path, x.depth
FROM
{course_modules} cm
INNER JOIN {context} x ON x.instanceid=cm.id
WHERE
cm.course=?
AND x.contextlevel=".CONTEXT_MODULE."
UNION ALL
SELECT
x.instanceid, x.id, x.contextlevel, x.path, x.depth
FROM
{block_instance} bi
INNER JOIN {context} x ON x.instanceid=bi.id
WHERE
bi.pageid=?
AND bi.pagetype='course-view'
AND x.contextlevel=".CONTEXT_BLOCK."
UNION ALL
SELECT
x.instanceid, x.id, x.contextlevel, x.path, x.depth
FROM
{context} x
WHERE
x.instanceid=?
AND x.contextlevel=".CONTEXT_COURSE."
",array($courseid,$courseid,$courseid)); // Note, repetition of parameter annoying but required
foreach($rs as $context) {
$context_cache[$context->contextlevel][$context->instanceid] = $context;
$context_cache_id[$context->id] = $context;
}
$rs->close();
$preloadedcourses[$courseid]=true;
}
/**
* Get the context instance as an object. This function will create the
* context instance if it does not exist yet.