MOBILE-2687 myoverview: Move get completion data to a helper

main
Pau Ferrer Ocaña 2018-11-06 16:12:18 +01:00
parent d3c3147a39
commit 6aaedb9bdb
4 changed files with 72 additions and 68 deletions

View File

@ -24,7 +24,7 @@
<!-- "Sort" selector. --> <!-- "Sort" selector. -->
<ion-col [hidden]="!showSortFilter"> <ion-col [hidden]="!showSortFilter">
<ion-select text-start [(ngModel)]="sort" (ngModelChange)="switchSort()" interface="popover" class="core-button-select"> <ion-select text-start [(ngModel)]="sort" (ngModelChange)="switchSort()" interface="popover" class="core-button-select">
<ion-option value="title">{{ 'addon.block_myoverview.title' | translate }}</ion-option> <ion-option value="fullname">{{ 'addon.block_myoverview.title' | translate }}</ion-option>
<ion-option value="lastaccess">{{ 'addon.block_myoverview.lastaccessed' | translate }}</ion-option> <ion-option value="lastaccess">{{ 'addon.block_myoverview.lastaccessed' | translate }}</ion-option>
</ion-select> </ion-select>
</ion-col> </ion-col>

View File

@ -45,7 +45,7 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
hidden: [] hidden: []
}; };
selectedFilter = 'inprogress'; selectedFilter = 'inprogress';
sort = 'title'; sort = 'fullname';
currentSite: any; currentSite: any;
downloadAllCoursesEnabled: boolean; downloadAllCoursesEnabled: boolean;
filteredCourses: any[]; filteredCourses: any[];
@ -145,26 +145,10 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
* @return {Promise<any>} Promise resolved when done. * @return {Promise<any>} Promise resolved when done.
*/ */
protected fetchContent(): Promise<any> { protected fetchContent(): Promise<any> {
return this.coursesHelper.getUserCoursesWithOptions().then((courses) => { return this.coursesHelper.getUserCoursesWithOptions(this.sort).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) => {
this.showSortFilter = courses.length > 0 && typeof courses[0].lastaccess != 'undefined'; this.showSortFilter = courses.length > 0 && typeof courses[0].lastaccess != 'undefined';
this.sortCourses(courses); this.initCourseFilters(courses);
this.courses.filter = ''; this.courses.filter = '';
this.showFilter = false; this.showFilter = false;
@ -242,25 +226,11 @@ export class AddonBlockMyOverviewComponent extends CoreBlockBaseComponent implem
} }
/** /**
* Sort and init courses filters. * Init courses filters.
* @param {any[]} courses Courses to sort. *
* @param {any[]} courses Courses to filter.
*/ */
sortCourses(courses: any[]): void { initCourseFilters(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);
});
}
}
this.courses.all = []; this.courses.all = [];
this.courses.past = []; this.courses.past = [];
this.courses.inprogress = []; 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 { switchSort(): void {
this.currentSite.setLocalSiteConfig('AddonBlockMyOverviewSort', this.sort); 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);
} }
/** /**

View File

@ -103,27 +103,7 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom
* @return {Promise<any>} Promise resolved when done. * @return {Promise<any>} Promise resolved when done.
*/ */
protected fetchContent(): Promise<any> { protected fetchContent(): Promise<any> {
return this.coursesHelper.getUserCoursesWithOptions().then((courses) => { return this.coursesHelper.getUserCoursesWithOptions('lastaccess', 10).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) => {
this.courses = courses; this.courses = courses;
this.initPrefetchCoursesIcons(); this.initPrefetchCoursesIcons();

View File

@ -15,6 +15,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreUtilsProvider } from '@providers/utils/utils';
import { CoreCoursesProvider } from './courses'; import { CoreCoursesProvider } from './courses';
import { AddonCourseCompletionProvider } from '@addon/coursecompletion/providers/coursecompletion';
/** /**
* Helper to gather some common courses functions. * Helper to gather some common courses functions.
@ -22,7 +23,8 @@ import { CoreCoursesProvider } from './courses';
@Injectable() @Injectable()
export class CoreCoursesHelperProvider { 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, * 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. * Get user courses with admin and nav options.
* *
* @return {Promise<any[]>} 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<any[]>} Courses filled with options.
*/ */
getUserCoursesWithOptions(): Promise<any[]> { getUserCoursesWithOptions(sort: string = 'fullname', slice: number = 0): Promise<any[]> {
return this.coursesProvider.getUserCourses().then((courses) => { return this.coursesProvider.getUserCourses().then((courses) => {
const promises = [], const promises = [],
courseIds = courses.map((course) => { courseIds = courses.map((course) => {
@ -107,12 +111,43 @@ export class CoreCoursesHelperProvider {
promises.push(this.loadCoursesExtraInfo(courses)); promises.push(this.loadCoursesExtraInfo(courses));
return Promise.all(promises).then(() => { return Promise.all(promises).then(() => {
return courses.sort((a, b) => { if (courses.length <= 0) {
return [];
}
switch (sort) {
case 'fullname':
courses.sort((a, b) => {
const compareA = a.fullname.toLowerCase(), const compareA = a.fullname.toLowerCase(),
compareB = b.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;
});
}));
}); });
}); });
} }