MOBILE-2655 course: Decrease WS calls to get course completion

main
dpalou 2018-11-09 11:18:41 +01:00
parent 8ae7153ad8
commit fcf0d2ef80
2 changed files with 60 additions and 20 deletions

View File

@ -165,7 +165,19 @@ export class AddonCourseCompletionProvider {
} }
return this.coursesProvider.getUserCourse(courseId, preferCache).then((course) => { return this.coursesProvider.getUserCourse(courseId, preferCache).then((course) => {
return !(course && typeof course.enablecompletion != 'undefined' && course.enablecompletion == 0); if (course) {
if (typeof course.enablecompletion != 'undefined' && course.enablecompletion == 0) {
// Completion not enabled for the course.
return false;
}
if (typeof course.completionhascriteria != 'undefined' && course.completionhascriteria == 0) {
// No criteria, cannot view completion.
return false;
}
}
return true;
}); });
} }
@ -177,6 +189,28 @@ export class AddonCourseCompletionProvider {
* @return {Promise<boolean>} Promise resolved with true if plugin is enabled, rejected or resolved with false otherwise. * @return {Promise<boolean>} Promise resolved with true if plugin is enabled, rejected or resolved with false otherwise.
*/ */
isPluginViewEnabledForUser(courseId: number, userId?: number): Promise<boolean> { isPluginViewEnabledForUser(courseId: number, userId?: number): Promise<boolean> {
// Check if user wants to view his own completion.
const currentUserId = this.sitesProvider.getCurrentSiteUserId();
let promise;
if (!userId || userId == currentUserId) {
// Viewing own completion. Get the course to check if it has completion criteria.
promise = this.coursesProvider.getUserCourse(courseId, true).then((course): any => {
// If the site is returning the completionhascriteria then the user can view his own completion.
// We already checked the value in isPluginViewEnabledForCourse.
if (course && typeof course.completionhascriteria != 'undefined') {
return true;
}
return Promise.reject(null);
});
} else {
promise = Promise.reject(null);
}
return promise.catch(() => {
// User not viewing own completion or the site doesn't tell us if the course has criteria.
// The only way to know if completion can be viewed is to call the WS.
// Disable emergency cache to be able to detect that the plugin has been disabled (WS will fail). // Disable emergency cache to be able to detect that the plugin has been disabled (WS will fail).
const preSets: any = { const preSets: any = {
emergencyCache: 0 emergencyCache: 0
@ -199,6 +233,7 @@ export class AddonCourseCompletionProvider {
}); });
} }
}); });
});
} }
/** /**

View File

@ -133,8 +133,13 @@ export class CoreCoursesHelperProvider {
} }
courses = slice > 0 ? courses.slice(0, slice) : courses; courses = slice > 0 ? courses.slice(0, slice) : courses;
// Fetch course completion status. // Fetch course completion status if needed.
return Promise.all(courses.map((course) => { return Promise.all(courses.map((course) => {
if (typeof course.completed != 'undefined') {
// The WebService already returns the completed status, no need to fetch it.
return Promise.resolve(course);
}
if (typeof course.enablecompletion != 'undefined' && course.enablecompletion == 0) { if (typeof course.enablecompletion != 'undefined' && course.enablecompletion == 0) {
// Completion is disabled for this course, there is no need to fetch the completion status. // Completion is disabled for this course, there is no need to fetch the completion status.
return Promise.resolve(course); return Promise.resolve(course);