Browse files

Merge branch 'MDL-40297_25' of git://github.com/stronk7/moodle into M…

…OODLE_25_STABLE
  • Loading branch information...
2 parents e235c97 + 1608fbe commit a852df6f11d1f88e4820838ff23e81c083263fc8 @danpoltawski danpoltawski committed Jul 2, 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.
+ $coursesubcategories = array();
+ $addedsubcategories = 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){
+ $coursesubcategories = array_merge($coursesubcategories, explode('/', trim($category->path, "/")));
+ }
+ $coursesubcategories = array_unique($coursesubcategories);
+
+ // Only add a subcategory if it is part of the path to user's course and
+ // wasn't already added.
+ foreach ($subcategories as $subid => $subcategory) {
+ if (in_array($subid, $coursesubcategories) &&
+ !in_array($subid, $addedsubcategories)) {
+ $this->add_category($subcategory, $basecategory, $nodetype);
+ $addedsubcategories[] = $subid;
+ }
+ }
+ }
+
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 a852df6

Please sign in to comment.