From e226a86d7654656a2f3eff1b7ce956dfe73eb4ed Mon Sep 17 00:00:00 2001 From: Brian King Date: Thu, 4 Oct 2012 18:25:41 +0200 Subject: [PATCH] MDL-36035 course: reduce database queries when calling print_whole_category_list On a Moodle with 1252 course categories, this reduces the database reads from 1313 to 62 for course/index.php --- course/lib.php | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/course/lib.php b/course/lib.php index a58b86ad3c9..36814d2dbc8 100644 --- a/course/lib.php +++ b/course/lib.php @@ -2179,7 +2179,7 @@ function get_course_category_tree($id = 0, $depth = 0) { * Recursive function to print out all the categories in a nice format * with or without courses included */ -function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1, $showcourses = true) { +function print_whole_category_list($category=NULL, $displaylist=NULL, $parentslist=NULL, $depth=-1, $showcourses = true, $categorycourses=NULL) { global $CFG; // maxcategorydepth == 0 meant no limit @@ -2191,9 +2191,17 @@ function print_whole_category_list($category=NULL, $displaylist=NULL, $parentsli make_categories_list($displaylist, $parentslist); } + if (!$categorycourses) { + if ($category) { + $categorycourses = flattened_course_category_tree($category->id); + } else { + $categorycourses = flattened_course_category_tree(); + } + } + if ($category) { if ($category->visible or has_capability('moodle/category:viewhiddencategories', context_system::instance())) { - print_category_info($category, $depth, $showcourses); + print_category_info($category, $depth, $showcourses, $categorycourses[$category->id]); } else { return; // Don't bother printing children of invisible categories } @@ -2217,11 +2225,40 @@ function print_whole_category_list($category=NULL, $displaylist=NULL, $parentsli $down = $last ? false : true; $first = false; - print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1, $showcourses); + print_whole_category_list($cat, $displaylist, $parentslist, $depth + 1, $showcourses, $categorycourses); } } } +/** + * Gets an array whose keys are category ids an whose values are arrays of courses in the + * corresponding category. + * + * @param int $categoryid + * @return array + */ +function flattened_course_category_tree($categoryid=0) { + $tree = get_course_category_tree($categoryid); + $flattened = array(); + foreach ($tree as $category) { + gather_flattened_courses($flattened, $category); + } + return $flattened; +} + +/** + * Recursive function to help flatten the course category tree + * + * @param $flattened + * @param $category + */ +function gather_flattened_courses(&$flattened, $category) { + $flattened[$category->id] = $category->courses; + foreach ($category->categories as $childcategory) { + gather_flattened_courses($flattened, $childcategory); + } +} + /** * This function will return $options array for html_writer::select(), with whitespace to denote nesting. */ @@ -2243,7 +2280,7 @@ function make_categories_options() { * Prints the category info in indented fashion * This function is only used by print_whole_category_list() above */ -function print_category_info($category, $depth=0, $showcourses = false) { +function print_category_info($category, $depth=0, $showcourses = false, $courses=NULL) { global $CFG, $DB, $OUTPUT; $strsummary = get_string('summary'); @@ -2264,7 +2301,9 @@ function print_category_info($category, $depth=0, $showcourses = false) { $catimage = " "; } - $courses = get_courses($category->id, 'c.sortorder ASC', 'c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.summary'); + if (is_null($courses)) { + $courses = get_courses($category->id, 'c.sortorder ASC', 'c.id,c.sortorder,c.visible,c.fullname,c.shortname,c.summary'); + } $context = context_coursecat::instance($category->id); $fullname = format_string($category->name, true, array('context' => $context));