From 6aaedb9bdbcf112b5c2d520a18c126f23294075e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Tue, 6 Nov 2018 16:12:18 +0100 Subject: [PATCH] MOBILE-2687 myoverview: Move get completion data to a helper --- .../myoverview/addon-block-myoverview.html | 2 +- .../components/myoverview/myoverview.ts | 65 ++++++++----------- .../recentlyaccessedcourses.ts | 22 +------ src/core/courses/providers/helper.ts | 51 ++++++++++++--- 4 files changed, 72 insertions(+), 68 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 40e1441c6..c5d5d1521 100644 --- a/src/addon/block/myoverview/components/myoverview/addon-block-myoverview.html +++ b/src/addon/block/myoverview/components/myoverview/addon-block-myoverview.html @@ -24,7 +24,7 @@ - {{ 'addon.block_myoverview.title' | translate }} + {{ 'addon.block_myoverview.title' | translate }} {{ 'addon.block_myoverview.lastaccessed' | translate }} diff --git a/src/addon/block/myoverview/components/myoverview/myoverview.ts b/src/addon/block/myoverview/components/myoverview/myoverview.ts index a218c9718..b628deccd 100644 --- a/src/addon/block/myoverview/components/myoverview/myoverview.ts +++ b/src/addon/block/myoverview/components/myoverview/myoverview.ts @@ -45,7 +45,7 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem hidden: [] }; selectedFilter = 'inprogress'; - sort = 'title'; + sort = 'fullname'; currentSite: any; downloadAllCoursesEnabled: boolean; filteredCourses: any[]; @@ -145,26 +145,10 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem * @return {Promise} Promise resolved when done. */ protected fetchContent(): Promise { - return this.coursesHelper.getUserCoursesWithOptions().then((courses) => { - // Fetch course completion status. - return Promise.all(courses.map((course) => { - if (typeof course.enablecompletion != 'undefined' && course.enablecompletion == 0) { - // Completion is disabled for this course, there is no need to fetch the completion status. - return Promise.resolve(course); - } - - return this.courseCompletionProvider.getCompletion(course.id).catch(() => { - // Ignore error, maybe course compleiton is disabled or user ha no permission. - }).then((completion) => { - course.completed = completion && completion.completed; - - return course; - }); - })); - }).then((courses) => { + return this.coursesHelper.getUserCoursesWithOptions(this.sort).then((courses) => { this.showSortFilter = courses.length > 0 && typeof courses[0].lastaccess != 'undefined'; - this.sortCourses(courses); + this.initCourseFilters(courses); this.courses.filter = ''; this.showFilter = false; @@ -242,25 +226,11 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem } /** - * Sort and init courses filters. - * @param {any[]} courses Courses to sort. + * Init courses filters. + * + * @param {any[]} courses Courses to filter. */ - sortCourses(courses: any[]): void { - if (this.showSortFilter) { - if (this.sort == 'lastaccess') { - courses.sort((a, b) => { - return b.lastaccess - a.lastaccess; - }); - } else if (this.sort == 'title') { - courses.sort((a, b) => { - const compareA = a.fullname.toLowerCase(), - compareB = b.fullname.toLowerCase(); - - return compareA.localeCompare(compareB); - }); - } - } - + initCourseFilters(courses: any[]): void { this.courses.all = []; this.courses.past = []; this.courses.inprogress = []; @@ -291,6 +261,8 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem } } }); + + this.filteredCourses = this.courses[this.selectedFilter]; } /** @@ -298,7 +270,24 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem */ switchSort(): void { this.currentSite.setLocalSiteConfig('AddonBlockMyOverviewSort', this.sort); - this.sortCourses(this.courses.all); + const courses = this.courses.all.concat(this.courses.hidden); + + if (this.showSortFilter) { + if (this.sort == 'lastaccess') { + courses.sort((a, b) => { + return b.lastaccess - a.lastaccess; + }); + } else if (this.sort == 'fullname') { + courses.sort((a, b) => { + const compareA = a.fullname.toLowerCase(), + compareB = b.fullname.toLowerCase(); + + return compareA.localeCompare(compareB); + }); + } + } + + this.initCourseFilters(courses); } /** diff --git a/src/addon/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts b/src/addon/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts index 8f8ab21d8..9f1b3d64d 100644 --- a/src/addon/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts +++ b/src/addon/block/recentlyaccessedcourses/components/recentlyaccessedcourses/recentlyaccessedcourses.ts @@ -103,27 +103,7 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom * @return {Promise} Promise resolved when done. */ protected fetchContent(): Promise { - return this.coursesHelper.getUserCoursesWithOptions().then((courses) => { - courses = courses.sort((a, b) => { - return b.lastaccess - b.lastaccess; - }).slice(0, 10); - - // Fetch course completion status. - return Promise.all(courses.map((course) => { - if (typeof course.enablecompletion != 'undefined' && course.enablecompletion == 0) { - // Completion is disabled for this course, there is no need to fetch the completion status. - return Promise.resolve(course); - } - - return this.courseCompletionProvider.getCompletion(course.id).catch(() => { - // Ignore error, maybe course completion is disabled or user has no permission. - }).then((completion) => { - course.completed = completion && completion.completed; - - return course; - }); - })); - }).then((courses) => { + return this.coursesHelper.getUserCoursesWithOptions('lastaccess', 10).then((courses) => { this.courses = courses; this.initPrefetchCoursesIcons(); diff --git a/src/core/courses/providers/helper.ts b/src/core/courses/providers/helper.ts index 87d168465..77673b760 100644 --- a/src/core/courses/providers/helper.ts +++ b/src/core/courses/providers/helper.ts @@ -15,6 +15,7 @@ import { Injectable } from '@angular/core'; import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreCoursesProvider } from './courses'; +import { AddonCourseCompletionProvider } from '@addon/coursecompletion/providers/coursecompletion'; /** * Helper to gather some common courses functions. @@ -22,7 +23,8 @@ import { CoreCoursesProvider } from './courses'; @Injectable() export class CoreCoursesHelperProvider { - constructor(private coursesProvider: CoreCoursesProvider, private utils: CoreUtilsProvider) { } + constructor(private coursesProvider: CoreCoursesProvider, private utils: CoreUtilsProvider, + private courseCompletionProvider: AddonCourseCompletionProvider) { } /** * Given a course object returned by core_enrol_get_users_courses and another one returned by core_course_get_courses_by_field, @@ -85,9 +87,11 @@ export class CoreCoursesHelperProvider { /** * Get user courses with admin and nav options. * - * @return {Promise} Promise resolved when done. + * @param {string} [sort=fullname] Sort courses after get them. If sort is not defined it won't be sorted. + * @param {number} [slice=0] Slice results to get the X first one. If slice > 0 it will be done after sorting. + * @return {Promise} Courses filled with options. */ - getUserCoursesWithOptions(): Promise { + getUserCoursesWithOptions(sort: string = 'fullname', slice: number = 0): Promise { return this.coursesProvider.getUserCourses().then((courses) => { const promises = [], courseIds = courses.map((course) => { @@ -107,12 +111,43 @@ export class CoreCoursesHelperProvider { promises.push(this.loadCoursesExtraInfo(courses)); return Promise.all(promises).then(() => { - return courses.sort((a, b) => { - const compareA = a.fullname.toLowerCase(), - compareB = b.fullname.toLowerCase(); + if (courses.length <= 0) { + return []; + } + switch (sort) { + case 'fullname': + courses.sort((a, b) => { + const compareA = a.fullname.toLowerCase(), + compareB = b.fullname.toLowerCase(); - return compareA.localeCompare(compareB); - }); + return compareA.localeCompare(compareB); + }); + break; + case 'lastaccess': + courses.sort((a, b) => { + return b.lastaccess - b.lastaccess; + }); + break; + default: + // Sort not implemented. Do not sort. + } + courses = slice > 0 ? courses.slice(0, slice) : courses; + + // Fetch course completion status. + return Promise.all(courses.map((course) => { + if (typeof course.enablecompletion != 'undefined' && course.enablecompletion == 0) { + // Completion is disabled for this course, there is no need to fetch the completion status. + return Promise.resolve(course); + } + + return this.courseCompletionProvider.getCompletion(course.id).catch(() => { + // Ignore error, maybe course completion is disabled or user has no permission. + }).then((completion) => { + course.completed = completion && completion.completed; + + return course; + }); + })); }); }); }