diff --git a/lib/navigationlib.php b/lib/navigationlib.php index d858fb2dad4..1af75d29eb9 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -1487,9 +1487,10 @@ class global_navigation extends navigation_node { * @return bool */ protected function show_my_categories() { - global $CFG, $DB; + global $CFG; if ($this->showmycategories === null) { - $this->showmycategories = !empty($CFG->navshowmycoursecategories) && $DB->count_records('course_categories') > 1; + require_once('coursecatlib.php'); + $this->showmycategories = !empty($CFG->navshowmycoursecategories) && coursecat::count_all() > 1; } return $this->showmycategories; } @@ -2881,7 +2882,7 @@ class global_navigation extends navigation_node { * They've expanded the 'my courses' branch. */ protected function load_courses_enrolled() { - global $CFG, $DB; + global $CFG; $sortorder = 'visible DESC'; // Prevent undefined $CFG->navsortmycoursessort errors. if (empty($CFG->navsortmycoursessort)) { @@ -2891,35 +2892,61 @@ class global_navigation extends navigation_node { $sortorder = $sortorder . ',' . $CFG->navsortmycoursessort . ' ASC'; $courses = enrol_get_my_courses(null, $sortorder); if (count($courses) && $this->show_my_categories()) { - // OK Actually we are loading categories. We only want to load categories that have a parent of 0. - // In order to make sure we load everything required we must first find the categories that are not - // base categories and work out the bottom category in thier path. + // Generate an array containing unique values of all the courses' categories. $categoryids = array(); foreach ($courses as $course) { + if (in_array($course->category, $categoryids)) { + continue; + } $categoryids[] = $course->category; } - $categoryids = array_unique($categoryids); - list($sql, $params) = $DB->get_in_or_equal($categoryids); - $categories = $DB->get_recordset_select('course_categories', 'id '.$sql.' AND parent <> 0', $params, 'sortorder, id', 'id, path'); - foreach ($categories as $category) { - $bits = explode('/', trim($category->path,'/')); - $categoryids[] = array_shift($bits); - } - $categoryids = array_unique($categoryids); - $categories->close(); - // Now we load the base categories. - list($sql, $params) = $DB->get_in_or_equal($categoryids); - $categories = $DB->get_recordset_select('course_categories', 'id '.$sql.' AND parent = 0', $params, 'sortorder, id'); - foreach ($categories as $category) { - $this->add_category($category, $this->rootnodes['mycourses'], self::TYPE_MY_CATEGORY); + // Array of category IDs that include the categories of the user's courses and the related course categories. + $fullpathcategoryids = []; + // Get the course categories for the enrolled courses' category IDs. + require_once('coursecatlib.php'); + $mycoursecategories = coursecat::get_many($categoryids); + // Loop over each of these categories and build the category tree using each category's path. + foreach ($mycoursecategories as $mycoursecat) { + $pathcategoryids = explode('/', $mycoursecat->path); + // First element of the exploded path is empty since paths begin with '/'. + array_shift($pathcategoryids); + // Merge the exploded category IDs into the full list of category IDs that we will fetch. + $fullpathcategoryids = array_merge($fullpathcategoryids, $pathcategoryids); } - $categories->close(); - } else { - foreach ($courses as $course) { - $this->add_course($course, false, self::COURSE_MY); + + // Fetch all of the categories related to the user's courses. + $pathcategories = coursecat::get_many($fullpathcategoryids); + // Loop over each of these categories and build the category tree. + foreach ($pathcategories as $coursecat) { + // No need to process categories that have already been added. + if (isset($this->addedcategories[$coursecat->id])) { + continue; + } + + // Get this course category's parent node. + $parent = null; + if ($coursecat->parent && isset($this->addedcategories[$coursecat->parent])) { + $parent = $this->addedcategories[$coursecat->parent]; + } + if (!$parent) { + // If it has no parent, then it should be right under the My courses node. + $parent = $this->rootnodes['mycourses']; + } + + // Build the category object based from the coursecat object. + $mycategory = new stdClass(); + $mycategory->id = $coursecat->id; + $mycategory->name = $coursecat->name; + $mycategory->visible = $coursecat->visible; + + // Add this category to the nav tree. + $this->add_category($mycategory, $parent, self::TYPE_MY_CATEGORY); } } + foreach ($courses as $course) { + $this->add_course($course, false, self::COURSE_MY); + } } }