Index: lib/accesslib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/accesslib.php,v retrieving revision 1.421.2.86 diff -u -r1.421.2.86 accesslib.php --- lib/accesslib.php 8 Dec 2008 07:28:04 -0000 1.421.2.86 +++ lib/accesslib.php 8 Dec 2008 18:35:47 -0000 @@ -2435,9 +2435,13 @@ * @param integer $level The context level, for example CONTEXT_COURSE, or CONTEXT_MODULE. * @param integer $instance The instance id. For $level = CONTEXT_COURSE, this would be $course->id, * for $level = CONTEXT_MODULE, this would be $cm->id. And so on. + * @param object $rs Optional record-set. This should be a record-set resulting + * from a query on the mdl_context table containing at least the fields + * id, instanceid, contextlevel, path, depth. If specified, this function + * just caches all that data, closes the recordset, then returns true * @return object The context object. */ -function get_context_instance($contextlevel, $instance=0) { +function get_context_instance($contextlevel, $instance=0, $rs=null) { global $context_cache, $context_cache_id, $CFG; static $allowed_contexts = array(CONTEXT_SYSTEM, CONTEXT_USER, CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_GROUP, CONTEXT_MODULE, CONTEXT_BLOCK); @@ -2462,6 +2466,19 @@ error('Error: get_context_instance() called with incorrect context level "'.s($contextlevel).'"'); } + if($rs) { + // When query is specified, use that query to obtain context records + // and automatically cache them + $contexts=array(); + while($context=rs_fetch_next_record($rs)) { + $context_cache[$contextlevel][$context->instanceid] = $context; + $context_cache_id[$context->id] = $context; + $contexts[$context->instanceid]=$context; + } + rs_close($rs); + return $contexts; + } + if (!is_array($instance)) { /// Check the cache if (isset($context_cache[$contextlevel][$instance])) { // Already cached @@ -2482,7 +2499,6 @@ return $context; } - /// ok, somebody wants to load several contexts to save some db queries ;-) $instances = $instance; $result = array(); Index: course/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/lib.php,v retrieving revision 1.538.2.60 diff -u -r1.538.2.60 lib.php --- course/lib.php 8 Dec 2008 07:28:05 -0000 1.538.2.60 +++ course/lib.php 8 Dec 2008 18:35:46 -0000 @@ -1088,14 +1088,20 @@ $modlurals = array(); - $cmids = array(); - $contexts = null; - foreach ($info as $mod) { - $cmids[$mod->cm] = $mod->cm; - } - if ($cmids) { + if (count($info)>0) { // preload all module contexts with one query - $contexts = get_context_instance(CONTEXT_MODULE, $cmids); + // note that this join is faster (with large # of activities) than + // using an IN clause with the instance numbers + $contexts = get_context_instance(CONTEXT_MODULE,0,get_recordset_sql(" +SELECT + x.instanceid, x.id, x.contextlevel, x.path, x.depth +FROM + {$CFG->prefix}course_modules cm + INNER JOIN {$CFG->prefix}context x ON x.instanceid=cm.id +WHERE + cm.course={$course->id} + AND x.contextlevel=70 +")); } foreach ($info as $mod) {