From b0712163e6eee4f8f704a625e95d5d59309d7694 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Wed, 22 Feb 2012 15:30:13 +1300 Subject: [PATCH] MDL-27405 navigation: My courses branch now shows categories if navshowcategories is enabled --- admin/settings/appearance.php | 1 + lang/en/admin.php | 2 + lib/navigationlib.php | 78 +++++++++++++++++++++++++++++++++-- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/admin/settings/appearance.php b/admin/settings/appearance.php index 9c86f4f26c2..c791205ca97 100644 --- a/admin/settings/appearance.php +++ b/admin/settings/appearance.php @@ -97,6 +97,7 @@ if ($hassiteconfig) { // speedup for non-admins, add all caps used on this page ); $temp->add(new admin_setting_configselect('defaulthomepage', new lang_string('defaulthomepage', 'admin'), new lang_string('configdefaulthomepage', 'admin'), HOMEPAGE_SITE, $choices)); $temp->add(new admin_setting_configcheckbox('navshowcategories', new lang_string('navshowcategories', 'admin'), new lang_string('confignavshowcategories', 'admin'), 1)); + $temp->add(new admin_setting_configcheckbox('navshowmycoursecategories', new lang_string('navshowmycoursecategories', 'admin'), new lang_string('navshowmycoursecategories_help', 'admin'), 0)); $temp->add(new admin_setting_configcheckbox('navshowallcourses', new lang_string('navshowallcourses', 'admin'), new lang_string('confignavshowallcourses', 'admin'), 0)); $temp->add(new admin_setting_configtext('navcourselimit',new lang_string('navcourselimit','admin'),new lang_string('confignavcourselimit', 'admin'),20,PARAM_INT)); $temp->add(new admin_setting_configcheckbox('navlinkcoursesections', new lang_string('navlinkcoursesections', 'admin'), new lang_string('navlinkcoursesections_help', 'admin'), 0)); diff --git a/lang/en/admin.php b/lang/en/admin.php index f9597da7515..80193fe93e3 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -702,6 +702,8 @@ $string['navshowfrontpagemods'] = 'Show front page activities in the navigation' $string['navshowfrontpagemods_help'] = 'If enabled, front page activities will be shown on the navigation under site pages.'; $string['navshowallcourses'] = 'Show all courses'; $string['navshowcategories'] = 'Show course categories'; +$string['navshowmycoursecategories'] = 'Show my course categories'; +$string['navshowmycoursecategories_help'] = 'If enabled courses in the users my courses branch will be shown in categories.'; $string['neverdeleteruns'] = 'Never delete runs'; $string['nobookmarksforuser'] = 'You do not have any bookmarks.'; $string['nodatabase'] = 'No database'; diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 19ef2f5951a..da26d5c9978 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -1055,7 +1055,7 @@ class global_navigation extends navigation_node { $limit = $CFG->navcourselimit; } - $mycourses = enrol_get_my_courses(NULL, 'visible DESC,sortorder ASC', $limit); + $mycourses = enrol_get_my_courses(NULL, 'visible DESC,sortorder ASC'); $showallcourses = (count($mycourses) == 0 || !empty($CFG->navshowallcourses)); $showcategories = ($showallcourses && $this->show_categories()); $issite = ($this->page->course->id == SITEID); @@ -1063,7 +1063,74 @@ class global_navigation extends navigation_node { // Check if any courses were returned. if (count($mycourses) > 0) { - // Add all of the users courses to the navigation + + // Check if categories should be displayed within the my courses branch + if (!empty($CFG->navshowmycoursecategories)) { + + // Find the category of each mycourse + $categories = array(); + foreach ($mycourses as $course) { + $categories[] = $course->category; + } + + // Do a single DB query to get the categories immediately associated with + // courses the user is enrolled in. + $categories = $DB->get_records_list('course_categories', 'id', array_unique($categories), 'depth ASC, sortorder ASC'); + // Work out the parent categories that we need to load that we havn't + // already got. + $categoryids = array(); + foreach ($categories as $category) { + $categoryids = array_merge($categoryids, explode('/', trim($category->path, '/'))); + } + $categoryids = array_unique($categoryids); + $categoryids = array_diff($categoryids, array_keys($categories)); + + if (count($categoryids)) { + // Fetch any other categories we need. + $categories = array_merge($DB->get_records_list('course_categories', 'id', $categoryids, 'depth ASC, sortorder ASC'), $categories); + } + + // We ONLY want the categories, we need to get rid of the keys + $categories = array_values($categories); + $addedcategories = array(); + while (($category = array_shift($categories)) !== null) { + if ($category->parent == '0') { + $categoryparent = $this->rootnodes['mycourses']; + } else if (array_key_exists($category->parent, $addedcategories)) { + $categoryparent = $addedcategories[$category->parent]; + } else { + // Prepare to count iterations. We don't want to loop forever + // accidentally if for some reason a category can't be placed. + if (!isset($category->loopcount)) { + $category->loopcount = 0; + } + $category->loopcount++; + if ($category->loopcount > 5) { + // This is a pretty serious problem and this should never happen. + // If it does then for some reason a category has been loaded but + // its parents have now. It could be data corruption. + debugging('Category '.$category->id.' could not be placed within the navigation', DEBUG_DEVELOPER); + } else { + // Add it back to the end of the categories array + array_push($categories, $category); + } + continue; + } + + $url = new moodle_url('/course/category.php', array('id' => $category->id)); + $addedcategories[$category->id] = $categoryparent->add($category->name, $url, self::TYPE_CATEGORY, $category->name, $category->id); + + if (!$category->visible) { + if (!has_capability('moodle/category:viewhiddencategories', get_context_instance(CONTEXT_COURSECAT, $category->parent))) { + $addedcategories[$category->id]->display = false; + } else { + $addedcategories[$category->id]->hidden = true; + } + } + } + } + + // Add all of the users courses to the navigation. foreach ($mycourses as $course) { $course->coursenode = $this->add_course($course, false, true); } @@ -2196,7 +2263,11 @@ class global_navigation extends navigation_node { $shortname = get_string('sitepages'); } } else if ($ismycourse) { - $parent = $this->rootnodes['mycourses']; + if (!empty($CFG->navshowmycoursecategories) && ($parent = $this->rootnodes['mycourses']->find($course->category, self::TYPE_CATEGORY))) { + // Nothing to do here the above statement set $parent to the category within mycourses. + } else { + $parent = $this->rootnodes['mycourses']; + } $url = new moodle_url('/course/view.php', array('id'=>$course->id)); } else { $parent = $this->rootnodes['courses']; @@ -3301,6 +3372,7 @@ class settings_navigation extends navigation_node { // hidden in new courses and courses where legacy files were turned off $url = new moodle_url('/files/index.php', array('contextid'=>$coursecontext->id)); $coursenode->add(get_string('courselegacyfiles'), $url, self::TYPE_SETTING, null, 'coursefiles', new pix_icon('i/files', '')); + } // Switch roles