Permalink
Browse files

MDL-40297 Hide empty categories in my courses

This is a followup of MDL-38631 were was detected that
whith categories being displayed in my courses, some of
them, not having courses for the user, were still shown.

With this patch, any category in the tree, not having courses
is automatically hidden, any depth.
  • Loading branch information...
1 parent f0d37f4 commit 42b40f99c1d02a7da2b5f13b72948fb2eccf85d3 @alendit alendit committed with stronk7 Mar 25, 2013
Showing with 41 additions and 7 deletions.
  1. +41 −7 lib/navigationlib.php
View
48 lib/navigationlib.php
@@ -2834,27 +2834,61 @@ protected function load_category($categoryid, $nodetype = self::TYPE_CATEGORY) {
$this->add_category($category, $this, $nodetype);
$basecategory = $this->addedcategories[$category->id];
} else {
- $subcategories[] = $category;
+ $subcategories[$category->id] = $category;
}
}
$categories->close();
- if (!is_null($basecategory)) {
- foreach ($subcategories as $category) {
- $this->add_category($category, $basecategory, $nodetype);
- }
- }
- // If category is shown in MyHome then only show enrolled courses, else show all courses.
+ // If category is shown in MyHome then only show enrolled courses and hide empty subcategories,
+ // else show all courses.
if ($nodetype === self::TYPE_MY_CATEGORY) {
$courses = enrol_get_my_courses();
+ $categoryids = array();
+
+ //only search for categories if basecategory was found
+ if (!is_null($basecategory)) {
+ // get course parent category ids
+ foreach ($courses as $course) {
+ $categoryids[] = $course->category;
+ }
+
+ // get a unique list of category ids which a part of the path
+ // to user's courses
+ $course_subcategories = array();
+ $added_subcategories = array();
+
+ list($sql, $params) = $DB->get_in_or_equal($categoryids);
+ $categories = $DB->get_recordset_select('course_categories', 'id '.$sql, $params, 'sortorder, id', 'id, path');
+
+ foreach ($categories as $category){
+ $course_subcategories = array_merge($course_subcategories, explode('/', trim($category->path, "/")));
+ }
+ $course_subcategories = array_unique($course_subcategories);
+
+ // only add a subcategory if it is part of the path to user's course and
+ // wasn't already added
+ foreach ($subcategories as $sub_id => $subcategory) {
+ if (in_array($sub_id, $course_subcategories) &&
+ !in_array($sub_id, $added_subcategories)) {
+ $this->add_category($subcategory, $basecategory, $nodetype);
+ $added_subcategories[] = $sub_id;
+ }
+ }
+ }
+
foreach ($courses as $course) {
// Add course if it's in category.
if (in_array($course->category, $categorylist)) {
$this->add_course($course, true, self::COURSE_MY);
}
}
} else {
+ if (!is_null($basecategory)) {
+ foreach ($subcategories as $key=>$category) {
+ $this->add_category($category, $basecategory, $nodetype);
+ }
+ }
$courses = $DB->get_recordset('course', array('category' => $categoryid), 'sortorder', '*' , 0, $limit);
foreach ($courses as $course) {
$this->add_course($course);

0 comments on commit 42b40f9

Please sign in to comment.