From fcf0d2ef8062197ef3efc81a7c36b2c959056289 Mon Sep 17 00:00:00 2001 From: dpalou Date: Fri, 9 Nov 2018 11:18:41 +0100 Subject: [PATCH] MOBILE-2655 course: Decrease WS calls to get course completion --- .../providers/coursecompletion.ts | 73 ++++++++++++++----- src/core/courses/providers/helper.ts | 7 +- 2 files changed, 60 insertions(+), 20 deletions(-) diff --git a/src/addon/coursecompletion/providers/coursecompletion.ts b/src/addon/coursecompletion/providers/coursecompletion.ts index a490f2b95..fba2e064d 100644 --- a/src/addon/coursecompletion/providers/coursecompletion.ts +++ b/src/addon/coursecompletion/providers/coursecompletion.ts @@ -165,7 +165,19 @@ export class AddonCourseCompletionProvider { } 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,27 +189,50 @@ export class AddonCourseCompletionProvider { * @return {Promise} Promise resolved with true if plugin is enabled, rejected or resolved with false otherwise. */ isPluginViewEnabledForUser(courseId: number, userId?: number): Promise { - // Disable emergency cache to be able to detect that the plugin has been disabled (WS will fail). - const preSets: any = { - emergencyCache: 0 - }; + // Check if user wants to view his own completion. + const currentUserId = this.sitesProvider.getCurrentSiteUserId(); + let promise; - return this.getCompletion(courseId, userId, preSets).then(() => { - return true; - }).catch((error) => { - if (this.utils.isWebServiceError(error)) { - // The WS returned an error, plugin is not enabled. - return false; - } else { - // Not a WS error. Check if we have a cached value. - preSets.omitExpires = true; - - return this.getCompletion(courseId, userId, preSets).then(() => { + 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; - }).catch(() => { + } + + 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). + const preSets: any = { + emergencyCache: 0 + }; + + return this.getCompletion(courseId, userId, preSets).then(() => { + return true; + }).catch((error) => { + if (this.utils.isWebServiceError(error)) { + // The WS returned an error, plugin is not enabled. return false; - }); - } + } else { + // Not a WS error. Check if we have a cached value. + preSets.omitExpires = true; + + return this.getCompletion(courseId, userId, preSets).then(() => { + return true; + }).catch(() => { + return false; + }); + } + }); }); } diff --git a/src/core/courses/providers/helper.ts b/src/core/courses/providers/helper.ts index 77673b760..9561e8300 100644 --- a/src/core/courses/providers/helper.ts +++ b/src/core/courses/providers/helper.ts @@ -133,8 +133,13 @@ export class CoreCoursesHelperProvider { } courses = slice > 0 ? courses.slice(0, slice) : courses; - // Fetch course completion status. + // Fetch course completion status if needed. 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) { // Completion is disabled for this course, there is no need to fetch the completion status. return Promise.resolve(course);