From f87ce4e9a09acb27c4189552d56a97597910a096 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Tue, 19 Feb 2013 12:12:24 +1300 Subject: [PATCH] MDL-32975 navigation: Added option to select how my courses are sorted --- admin/settings/appearance.php | 7 +++ lang/en/admin.php | 6 +++ lib/navigationlib.php | 85 +++++++++++++++++++---------------- 3 files changed, 59 insertions(+), 39 deletions(-) diff --git a/admin/settings/appearance.php b/admin/settings/appearance.php index f3f63c91e394b..a11f019e99ed0 100644 --- a/admin/settings/appearance.php +++ b/admin/settings/appearance.php @@ -114,6 +114,13 @@ $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)); + $sortoptions = array( + 'sortorder' => new lang_string('sort_sortorder', 'admin'), + 'fullname' => new lang_string('sort_fullname', 'admin'), + 'shortname' => new lang_string('sort_shortname', 'admin'), + 'idnumber' => new lang_string('sort_idnumber', 'admin'), + ); + $temp->add(new admin_setting_configselect('navsortmycoursessort', new lang_string('navsortmycoursessort', 'admin'), new lang_string('navsortmycoursessort_help', 'admin'), 'sortorder', $sortoptions)); $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('usesitenameforsitepages', new lang_string('usesitenameforsitepages', 'admin'), new lang_string('configusesitenameforsitepages', 'admin'), 0)); $temp->add(new admin_setting_configcheckbox('linkadmincategories', new lang_string('linkadmincategories', 'admin'), new lang_string('linkadmincategories_help', 'admin'), 0)); diff --git a/lang/en/admin.php b/lang/en/admin.php index 53f68e28f73e3..3d36a0a8b5872 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -747,6 +747,8 @@ $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['navsortmycoursessort'] = 'Sort my courses'; +$string['navsortmycoursessort_help'] = 'Determines how the courses displayed within the My courses branch on the navigation will be sorted. By default sortorder it used.'; $string['neverdeleteruns'] = 'Never delete runs'; $string['nobookmarksforuser'] = 'You do not have any bookmarks.'; $string['nodatabase'] = 'No database'; @@ -971,6 +973,10 @@ $string['slasharguments'] = 'Use slash arguments'; $string['smartpix'] = 'Smart pix search'; $string['soaprecommended'] = 'Installing the optional soap extension is useful for web services and some contrib modules.'; +$string['sort_fullname'] = 'Fullname'; +$string['sort_idnumber'] = 'ID Number'; +$string['sort_shortname'] = 'Shortname'; +$string['sort_sortorder'] = 'Sort order'; $string['spellengine'] = 'Spell engine'; $string['spelllanguagelist'] = 'Spell language list'; $string['splrequired'] = 'The SPL PHP extension is now required by Moodle.'; diff --git a/lib/navigationlib.php b/lib/navigationlib.php index 3a341cb8a347b..6e34192fde2ee 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -1062,7 +1062,7 @@ public function initialise() { $this->rootnodes['site'] = $this->add_course($SITE); $this->rootnodes['myprofile'] = $this->add(get_string('myprofile'), null, self::TYPE_USER, null, 'myprofile'); $this->rootnodes['currentcourse'] = $this->add(get_string('currentcourse'), null, self::TYPE_ROOTNODE, null, 'currentcourse'); - $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), new moodle_url('/my'), self::TYPE_ROOTNODE, null, 'mycourses'); + $this->rootnodes['mycourses'] = $this->add(get_string('mycourses'), new moodle_url('/my/'), self::TYPE_ROOTNODE, null, 'mycourses'); $this->rootnodes['courses'] = $this->add(get_string('courses'), new moodle_url('/course/index.php'), self::TYPE_ROOTNODE, null, 'courses'); $this->rootnodes['users'] = $this->add(get_string('users'), null, self::TYPE_ROOTNODE, null, 'users'); @@ -1089,6 +1089,10 @@ public function initialise() { $this->rootnodes['courses']->isexpandable = true; } + if ($this->rootnodes['mycourses']->isactive) { + $this->load_courses_enrolled(); + } + $canviewcourseprofile = true; // Next load context specific content into the navigation @@ -2556,6 +2560,47 @@ public function find($key, $type) { } return parent::find($key, $type); } + + /** + * They've expanded the 'my courses' branch. + */ + protected function load_courses_enrolled() { + global $CFG, $DB; + $sortorder = 'visible DESC'; + // Append the chosen sortorder. + $sortorder = $sortorder . ',' . $CFG->navsortmycoursessort . ' ASC'; + $courses = enrol_get_my_courses(null, $sortorder); + if ($this->show_my_categories(true)) { + // 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. + $categoryids = array(); + foreach ($courses as $course) { + $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']); + } + $categories->close(); + } else { + foreach ($courses as $course) { + $this->add_course($course, false, self::COURSE_MY); + } + } + } } /** @@ -2682,44 +2727,6 @@ public function initialise() { return $this->expandable; } - /** - * They've expanded the 'my courses' branch. - */ - protected function load_courses_enrolled() { - global $DB; - $courses = enrol_get_my_courses(); - if ($this->show_my_categories(true)) { - // 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. - $categoryids = array(); - foreach ($courses as $course) { - $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); - } - $categories->close(); - } else { - foreach ($courses as $course) { - $this->add_course($course, false, self::COURSE_MY); - } - } - } - /** * They've expanded the general 'courses' branch. */