From 3b89d7bf123805aa29ee57c79b5d7f77323f7bd6 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 26 Oct 2020 15:39:58 +0100 Subject: [PATCH] MOBILE-3578 competency: Fix multiple requests when view user profile --- src/addon/competency/providers/competency.ts | 88 ++++++++++++++----- .../competency/providers/user-handler.ts | 7 +- 2 files changed, 68 insertions(+), 27 deletions(-) diff --git a/src/addon/competency/providers/competency.ts b/src/addon/competency/providers/competency.ts index 7d2704da9..ca1eadb8b 100644 --- a/src/addon/competency/providers/competency.ts +++ b/src/addon/competency/providers/competency.ts @@ -62,6 +62,39 @@ export class AddonCompetencyProvider { }); } + /** + * Returns whether current user can see another user competencies in a course. + * + * @param courseId Course ID. + * @param userId User ID. + * @param siteId Site ID. If not defined, current site. + * @return Promise resolved with boolean: whether the user can view the competencies. + */ + canViewUserCompetenciesInCourse(courseId: number, userId?: number, siteId?: string): Promise { + if (!this.sitesProvider.isLoggedIn()) { + return Promise.resolve(false); + } + + return this.getCourseCompetenciesPage(courseId, siteId).then((response) => { + if (!response.competencies.length) { + // No competencies. + return false; + } + + if (!userId || userId == this.sitesProvider.getCurrentSiteUserId()) { + // Current user. + return true; + } + + // Check if current user can view any competency of the user. + return this.getCompetencyInCourse(courseId, response.competencies[0].competency.id, userId, siteId).then(() => { + return true; + }); + }).catch(() => { + return false; + }); + } + /** * Get cache key for user learning plans data WS calls. * @@ -333,7 +366,7 @@ export class AddonCompetencyProvider { } /** - * Get all competencies in a course. + * Get all competencies in a course for a certain user. * * @param courseId ID of the course. * @param userId ID of the user. @@ -344,6 +377,39 @@ export class AddonCompetencyProvider { getCourseCompetencies(courseId: number, userId?: number, siteId?: string, ignoreCache?: boolean) : Promise { + return this.getCourseCompetenciesPage(courseId, siteId, ignoreCache).then((response) => { + + if (!userId || userId == this.sitesProvider.getCurrentSiteUserId()) { + return response; + } + + let promises: Promise[]; + + promises = response.competencies.map((competency) => + this.getCompetencyInCourse(courseId, competency.competency.id, userId, siteId) + ); + + return Promise.all(promises).then((responses: AddonCompetencyUserCompetencySummaryInCourse[]) => { + responses.forEach((resp, index) => { + response.competencies[index].usercompetencycourse = resp.usercompetencysummary.usercompetencycourse; + }); + + return response; + }); + }); + } + + /** + * Get all competencies in a course. + * + * @param courseId ID of the course. + * @param siteId Site ID. If not defined, current site. + * @param ignoreCache True if it should ignore cached data (it will always fail in offline or server down). + * @return Promise to be resolved when the course competencies are retrieved. + */ + getCourseCompetenciesPage(courseId: number, siteId?: string, ignoreCache?: boolean) + : Promise { + return this.sitesProvider.getSite(siteId).then((site) => { this.logger.debug('Get course competencies for course ' + courseId); @@ -370,26 +436,6 @@ export class AddonCompetencyProvider { return Promise.reject(null); }); - - }).then((response) => { - - if (!userId || userId == this.sitesProvider.getCurrentSiteUserId()) { - return response; - } - - let promises: Promise[]; - - promises = response.competencies.map((competency) => - this.getCompetencyInCourse(courseId, competency.competency.id, userId, siteId) - ); - - return Promise.all(promises).then((responses: AddonCompetencyUserCompetencySummaryInCourse[]) => { - responses.forEach((resp, index) => { - response.competencies[index].usercompetencycourse = resp.usercompetencysummary.usercompetencycourse; - }); - - return response; - }); }); } diff --git a/src/addon/competency/providers/user-handler.ts b/src/addon/competency/providers/user-handler.ts index b2c07ebc0..216909eda 100644 --- a/src/addon/competency/providers/user-handler.ts +++ b/src/addon/competency/providers/user-handler.ts @@ -71,15 +71,10 @@ export class AddonCompetencyUserHandler implements CoreUserProfileHandler { return this.participantsNavEnabledCache[cacheKey]; } - return this.competencyProvider.getCourseCompetencies(courseId, user.id).then((response) => { - const enabled = response.competencies.length > 0; + return this.competencyProvider.canViewUserCompetenciesInCourse(courseId, user.id).then((enabled) => { this.participantsNavEnabledCache[cacheKey] = enabled; return enabled; - }).catch((message) => { - this.participantsNavEnabledCache[cacheKey] = false; - - return false; }); } else { // Link on a user site profile.