Permalink
Browse files

Merge branch 'wip-MDL-27405-m23' of git://github.com/samhemelryk/moodle

  • Loading branch information...
2 parents b39877e + bc40124 commit f02702760afcee8e3f6107a77bae1719b58a92a7 @stronk7 stronk7 committed Mar 13, 2012
Showing with 81 additions and 3 deletions.
  1. +1 −0 admin/settings/appearance.php
  2. +2 −0 lang/en/admin.php
  3. +78 −3 lib/navigationlib.php
@@ -97,6 +97,7 @@
);
$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));
View
@@ -703,6 +703,8 @@
$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';
View
@@ -1055,15 +1055,85 @@ public function initialise() {
$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);
$ismycourse = (array_key_exists($this->page->course->id, $mycourses));
// 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.
+ $allcategories = $DB->get_records_list('course_categories', 'id', $categoryids, 'depth ASC, sortorder ASC');
+ if (is_array($allcategories) && count($allcategories) > 0) {
+ $categories = array_merge($categories, $allcategories);
+ }
+ }
+
+ // 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 +2266,11 @@ public function add_course(stdClass $course, $forcegeneric = false, $ismycourse
$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 +3375,7 @@ protected function load_course_settings($forceopen = false) {
// 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

0 comments on commit f027027

Please sign in to comment.