Merge pull request #2586 from dpalou/MOBILE-3578

MOBILE-3578 competency: Fix multiple requests when view user profile
main
Juan Leyva 2020-11-03 13:33:14 +01:00 committed by GitHub
commit 08b3dc4723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 27 deletions

View File

@ -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<boolean> {
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<AddonCompetencyDataForCourseCompetenciesPageResult> {
return this.getCourseCompetenciesPage(courseId, siteId, ignoreCache).then((response) => {
if (!userId || userId == this.sitesProvider.getCurrentSiteUserId()) {
return response;
}
let promises: Promise<AddonCompetencyUserCompetencySummaryInCourse>[];
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<AddonCompetencyDataForCourseCompetenciesPageResult> {
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<AddonCompetencyUserCompetencySummaryInCourse>[];
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;
});
});
}

View File

@ -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.