From d10fa5db3cce02ec23b5b4ae65766b260198c046 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 8 Mar 2022 15:51:42 +0100 Subject: [PATCH] MOBILE-3930 h5pactivity: Support links to all users attempts --- .../services/handlers/report-link.ts | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/addons/mod/h5pactivity/services/handlers/report-link.ts b/src/addons/mod/h5pactivity/services/handlers/report-link.ts index f2f37c19c..f3272d2bf 100644 --- a/src/addons/mod/h5pactivity/services/handlers/report-link.ts +++ b/src/addons/mod/h5pactivity/services/handlers/report-link.ts @@ -20,6 +20,7 @@ import { CoreCourse } from '@features/course/services/course'; import { CoreNavigator } from '@services/navigator'; import { CoreSites, CoreSitesReadingStrategy } from '@services/sites'; import { CoreDomUtils } from '@services/utils/dom'; +import { CoreUtils } from '@services/utils/utils'; import { makeSingleton } from '@singletons'; import { AddonModH5PActivity } from '../h5pactivity'; import { AddonModH5PActivityModuleHandlerService } from './module'; @@ -60,7 +61,7 @@ export class AddonModH5PActivityReportLinkHandlerService extends CoreContentLink } else { const userId = params.userid ? Number(params.userid) : undefined; - this.openUserAttempts(module.id, module.course, siteId, userId); + await this.openUserAttempts(module.id, module.course, instanceId, siteId, userId); } } catch (error) { CoreDomUtils.showErrorModalDefault(error, 'Error processing link.'); @@ -100,13 +101,35 @@ export class AddonModH5PActivityReportLinkHandlerService extends CoreContentLink * * @param cmId Module ID. * @param courseId Course ID. + * @param id Instance ID. * @param siteId Site ID. * @param userId User ID. If not defined, current user in site. * @return Promise resolved when done. */ - protected openUserAttempts(cmId: number, courseId: number, siteId: string, userId?: number): void { - userId = userId || CoreSites.getCurrentSiteUserId(); - const path = AddonModH5PActivityModuleHandlerService.PAGE_NAME + `/${courseId}/${cmId}/userattempts/${userId}`; + protected async openUserAttempts(cmId: number, courseId: number, id: number, siteId: string, userId?: number): Promise { + let canViewAllAttempts = false; + + if (!userId) { + // No user ID specified. Check if current user can view all attempts. + userId = CoreSites.getCurrentSiteUserId(); + canViewAllAttempts = await AddonModH5PActivity.canGetUsersAttempts(siteId); + + if (canViewAllAttempts) { + const accessInfo = await CoreUtils.ignoreErrors(AddonModH5PActivity.getAccessInformation(id, { + cmId, + siteId, + })); + + canViewAllAttempts = !!accessInfo?.canreviewattempts; + } + } + + let path: string; + if (canViewAllAttempts) { + path = `${AddonModH5PActivityModuleHandlerService.PAGE_NAME}/${courseId}/${cmId}/users`; + } else { + path = `${AddonModH5PActivityModuleHandlerService.PAGE_NAME}/${courseId}/${cmId}/userattempts/${userId}`; + } CoreNavigator.navigateToSitePath(path, { siteId,