]> git.mjollnir.org Git - moodle.git/commitdiff
categories: Categories page and get_courses_page() - smarter about context
authormartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:08:24 +0000 (07:08 +0000)
committermartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:08:24 +0000 (07:08 +0000)
This patch saves 1600 context lookups on a 1600 course category. rcache
does help a bit with small categories, but on large setups, this is
not sustainable.

And it's not needed either. We have the data right at our fingertips.
Just get it when it's cheap...

course/category.php
lib/datalib.php

index dc7cbb2c4ae927bce8018cf278768941edb0bcae..ffa1d8332e863c831b438b48c92677c374aa57fe 100644 (file)
         }
 
         foreach ($courses as $acourse) {
-
-            $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
+            if (isset($acourse->context)) {
+                $coursecontext = $acourse->context;
+            } else {
+                $coursecontext = get_context_instance(CONTEXT_COURSE, $acourse->id);
+            }
 
             $count++;
             $up = ($count > 1 || !$atfirstpage);
index f0e24fbb04fc5ebd6ce1f3e765e9aa894af4ee8a..9caa18f990d21bfce25799602cf0ad5c0cae49cf 100644 (file)
@@ -508,16 +508,19 @@ function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c
 
     // pull out all course matching the cat
     $visiblecourses = array();
-    if (!($courses = get_records_sql("SELECT $fields
-                                FROM {$CFG->prefix}course c
-                                $categoryselect
-                                ORDER BY $sort"))) {
+    if (!($rs = get_recordset_sql("SELECT $fields,
+                                          ctx.id AS ctxid, ctx.path AS ctxpath, ctx.depth as ctxdepth
+                                   FROM {$CFG->prefix}course c
+                                   JOIN {$CFG->prefix}context ctx
+                                     ON (c.id = ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
+                                   $categoryselect
+                                   ORDER BY $sort"))) {
         return $visiblecourses;
     }
     $totalcount = 0;
 
     if (!$limitnum) {
-        $limitnum = count($courses);
+        $limitnum = $rs->RecordCount();
     }
 
     if (!$limitfrom) {
@@ -525,23 +528,25 @@ function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c
     }
 
     // iteration will have to be done inside loop to keep track of the limitfrom and limitnum
-    foreach ($courses as $course) {
-        if ($course->visible <= 0) {
-            // for hidden courses, require visibility check
-            if (has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $course->id))) {
+    if ($rs->RecordCount()) {
+        while ($course = rs_fetch_next_record($rs)) {
+            $course = make_context_subobj($course);
+            if ($course->visible <= 0) {
+                // for hidden courses, require visibility check
+                if (has_capability('moodle/course:viewhiddencourses', $course->context)) {
+                    $totalcount++;
+                    if ($totalcount > $limitfrom && count($visiblecourses) < $limitnum) {
+                        $visiblecourses [] = $course;
+                    }
+                }
+            } else {
                 $totalcount++;
                 if ($totalcount > $limitfrom && count($visiblecourses) < $limitnum) {
                     $visiblecourses [] = $course;
                 }
             }
-        } else {
-            $totalcount++;
-            if ($totalcount > $limitfrom && count($visiblecourses) < $limitnum) {
-                $visiblecourses [] = $course;
-            }
         }
     }
-
     return $visiblecourses;
 
 /**