MOBILE-2687 myoverview: Move get completion data to a helper
parent
d3c3147a39
commit
6aaedb9bdb
|
@ -24,7 +24,7 @@
|
|||
<!-- "Sort" selector. -->
|
||||
<ion-col [hidden]="!showSortFilter">
|
||||
<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-select>
|
||||
</ion-col>
|
||||
|
|
|
@ -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<any>} Promise resolved when done.
|
||||
*/
|
||||
protected fetchContent(): Promise<any> {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -103,27 +103,7 @@ export class AddonBlockRecentlyAccessedCoursesComponent extends CoreBlockBaseCom
|
|||
* @return {Promise<any>} Promise resolved when done.
|
||||
*/
|
||||
protected fetchContent(): Promise<any> {
|
||||
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();
|
||||
|
|
|
@ -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<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) => {
|
||||
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;
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue