From 4e727ba860510a78511d4f8f5e52bdb1cd861e29 Mon Sep 17 00:00:00 2001 From: Kiet Ly Chan Date: Thu, 12 Dec 2019 13:37:12 +0700 Subject: [PATCH] MOBILE-3249 blocks: Sort courses by short name --- .../myoverview/addon-block-myoverview.html | 3 ++- .../components/myoverview/myoverview.ts | 23 ++++++++++++++++++- src/addon/block/myoverview/lang/en.json | 3 ++- src/assets/lang/en.json | 1 + src/core/courses/providers/helper.ts | 8 +++++++ 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/addon/block/myoverview/components/myoverview/addon-block-myoverview.html b/src/addon/block/myoverview/components/myoverview/addon-block-myoverview.html index e4016144a..4b7495bfa 100644 --- a/src/addon/block/myoverview/components/myoverview/addon-block-myoverview.html +++ b/src/addon/block/myoverview/components/myoverview/addon-block-myoverview.html @@ -11,7 +11,8 @@ - + + diff --git a/src/addon/block/myoverview/components/myoverview/myoverview.ts b/src/addon/block/myoverview/components/myoverview/myoverview.ts index 3ed75d517..074b176b9 100644 --- a/src/addon/block/myoverview/components/myoverview/myoverview.ts +++ b/src/addon/block/myoverview/components/myoverview/myoverview.ts @@ -83,6 +83,7 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem protected updateSiteObserver; protected courseIds = []; protected fetchContentDefaultError = 'Error getting my overview data.'; + protected showSortByShortName = false; constructor(injector: Injector, protected coursesProvider: CoreCoursesProvider, @@ -181,6 +182,18 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem const showCategories = config && config.displaycategories && config.displaycategories.value == '1'; return this.coursesHelper.getUserCoursesWithOptions(this.sort, null, null, showCategories).then((courses) => { + // Check to show sort by short name only if the text is visible. + if (courses.length > 0) { + const sampleCourse = courses[0]; + this.showSortByShortName = sampleCourse.displayname && sampleCourse.shortname && + sampleCourse.fullname != sampleCourse.displayname; + } + + // Rollback to sort by full name if user is sorting by short name then Moodle web change the config. + if (!this.showSortByShortName && this.sort === 'shortname') { + this.switchSort('fullname'); + } + this.courseIds = courses.map((course) => { return course.id; }); @@ -395,7 +408,7 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem this.courses.allincludinghidden = courses; if (this.showSortFilter) { - if (this.sort == 'lastaccess') { + if (this.sort == 'lastaccess') { courses.sort((a, b) => { return b.lastaccess - a.lastaccess; }); @@ -404,6 +417,14 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem const compareA = a.fullname.toLowerCase(), compareB = b.fullname.toLowerCase(); + return compareA.localeCompare(compareB); + }); + } else if (this.sort == 'shortname') { + courses.sort((a, b) => { + const compareA = a.shortname.toLowerCase(), + compareB = b.shortname.toLowerCase(); + compareA.localeCompare(); + return compareA.localeCompare(compareB); }); } diff --git a/src/addon/block/myoverview/lang/en.json b/src/addon/block/myoverview/lang/en.json index 4c9ca27af..9d38164d8 100644 --- a/src/addon/block/myoverview/lang/en.json +++ b/src/addon/block/myoverview/lang/en.json @@ -10,5 +10,6 @@ "nocourses": "No courses", "past": "Past", "pluginname": "Course overview", + "shortname": "Short name", "title": "Course name" -} \ No newline at end of file +} diff --git a/src/assets/lang/en.json b/src/assets/lang/en.json index 1b340614c..290fe79f4 100644 --- a/src/assets/lang/en.json +++ b/src/assets/lang/en.json @@ -49,6 +49,7 @@ "addon.block_myoverview.nocourses": "No courses", "addon.block_myoverview.past": "Past", "addon.block_myoverview.pluginname": "Course overview", + "addon.block_myoverview.shortname": "Short name", "addon.block_myoverview.title": "Course name", "addon.block_newsitems.pluginname": "Latest announcements", "addon.block_onlineusers.pluginname": "Online users", diff --git a/src/core/courses/providers/helper.ts b/src/core/courses/providers/helper.ts index 30e3ab092..d7ac68a4b 100644 --- a/src/core/courses/providers/helper.ts +++ b/src/core/courses/providers/helper.ts @@ -212,6 +212,14 @@ export class CoreCoursesHelperProvider { return b.timemodified - a.timemodified; }); break; + case 'shortname': + courses.sort((a, b) => { + const compareA = a.shortname.toLowerCase(), + compareB = b.shortname.toLowerCase(); + + return compareA.localeCompare(compareB); + }); + break; default: // Sort not implemented. Do not sort. }