Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-36035 course: reduce database queries when calling print_whole_ca…

…tegory_list

On a Moodle with 1252 course categories, this reduces the database
reads from 1313 to 62 for course/index.php
  • Loading branch information...
commit e226a86d7654656a2f3eff1b7ce956dfe73eb4ed 1 parent 9da506c
@brki brki authored samhemelryk committed
Showing with 44 additions and 5 deletions.
  1. +44 −5 course/lib.php
View
49 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,12 +2225,41 @@ 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.
*/
function make_categories_options() {
@@ -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));
Please sign in to comment.
Something went wrong with that request. Please try again.