From 679b61796766cb85012cd5b71747c4ea5f2c6dc4 Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Wed, 19 Sep 2007 07:08:24 +0000 Subject: [PATCH] categories: Categories page and get_courses_page() - smarter about context 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 | 7 +++++-- lib/datalib.php | 35 ++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/course/category.php b/course/category.php index dc7cbb2c4a..ffa1d8332e 100644 --- a/course/category.php +++ b/course/category.php @@ -353,8 +353,11 @@ } 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); diff --git a/lib/datalib.php b/lib/datalib.php index f0e24fbb04..9caa18f990 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -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; /** -- 2.39.5