]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-11180 datalib: get_courses() - grab the course context as part of the select
authormartinlanghoff <martinlanghoff>
Tue, 26 Feb 2008 21:48:08 +0000 (21:48 +0000)
committermartinlanghoff <martinlanghoff>
Tue, 26 Feb 2008 21:48:08 +0000 (21:48 +0000)
This speeds up course & category listing enormously - cutting 2500 DB queries
for a site with 2500 courses. It's the power of the JOIN...

lib/datalib.php

index b607625f3ceda27431b89e5e03b7df1db5ba3d86..b550ea781c1641be25d09052f9fa4691f5634d62 100644 (file)
@@ -405,18 +405,22 @@ function get_courses($categoryid="all", $sort="c.sortorder ASC", $fields="c.*")
     $visiblecourses = array();
 
     // pull out all course matching the cat
-    if ($courses = get_records_sql("SELECT $fields
-                                FROM {$CFG->prefix}course c
-                                $categoryselect
-                                $sortstatement")) {
+    if ($courses = get_records_sql("SELECT $fields,
+                                    ctx.id AS ctxid, ctx.path AS ctxpath,
+                                    ctx.depth AS ctxdepth, ctx.contextlevel AS ctxlevel
+                                    FROM {$CFG->prefix}course c
+                                    JOIN {$CFG->prefix}context ctx
+                                      ON (c.id = ctx.instanceid 
+                                          AND ctx.contextlevel=".CONTEXT_COURSE.")
+                                    $categoryselect
+                                    $sortstatement")) {
 
         // loop throught them
         foreach ($courses as $course) {
-
+            $course = make_context_subobj($course);
             if (isset($course->visible) && $course->visible <= 0) {
                 // for hidden courses, require visibility check
-                if (has_capability('moodle/course:viewhiddencourses',
-                        get_context_instance(CONTEXT_COURSE, $course->id))) {
+                if (has_capability('moodle/course:viewhiddencourses', $course->context)) {
                     $visiblecourses [] = $course;
                 }
             } else {