From 656418b17923b3da40c7133921a2c4129859e8b2 Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Tue, 26 Feb 2008 21:48:08 +0000 Subject: [PATCH] MDL-11180 datalib: get_courses() - grab the course context as part of the select 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 | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/datalib.php b/lib/datalib.php index b607625f3c..b550ea781c 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -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 { -- 2.39.5