diff --git a/src/addons/mod/h5pactivity/pages/attempt-results/attempt-results.ts b/src/addons/mod/h5pactivity/pages/attempt-results/attempt-results.ts index 741e78f28..3f3d79731 100644 --- a/src/addons/mod/h5pactivity/pages/attempt-results/attempt-results.ts +++ b/src/addons/mod/h5pactivity/pages/attempt-results/attempt-results.ts @@ -56,6 +56,8 @@ export class AddonModH5PActivityAttemptResultsPage implements OnInit { try { await this.fetchData(); + + await AddonModH5PActivity.logViewReport(this.h5pActivity!.id, this.h5pActivity!.name, { attemptId: this.attemptId }); } catch (error) { CoreDomUtils.showErrorModalDefault(error, 'Error loading attempt.'); } finally { diff --git a/src/addons/mod/h5pactivity/pages/user-attempts/user-attempts.ts b/src/addons/mod/h5pactivity/pages/user-attempts/user-attempts.ts index 9f6d8b1d1..5f09b4c02 100644 --- a/src/addons/mod/h5pactivity/pages/user-attempts/user-attempts.ts +++ b/src/addons/mod/h5pactivity/pages/user-attempts/user-attempts.ts @@ -58,6 +58,8 @@ export class AddonModH5PActivityUserAttemptsPage implements OnInit { try { await this.fetchData(); + + await AddonModH5PActivity.logViewReport(this.h5pActivity!.id, this.h5pActivity!.name, { userId: this.userId }); } catch (error) { CoreDomUtils.showErrorModalDefault(error, 'Error loading attempts.'); } finally { diff --git a/src/addons/mod/h5pactivity/services/h5pactivity.ts b/src/addons/mod/h5pactivity/services/h5pactivity.ts index 7902596d1..28927c099 100644 --- a/src/addons/mod/h5pactivity/services/h5pactivity.ts +++ b/src/addons/mod/h5pactivity/services/h5pactivity.ts @@ -580,7 +580,7 @@ export class AddonModH5PActivityProvider { * @return Promise resolved when the WS call is successful. */ logView(id: number, name?: string, siteId?: string): Promise { - const params: AddonModH5pactivityViewH5pactivityWSParams = { + const params: AddonModH5PActivityViewH5pactivityWSParams = { h5pactivityid: id, }; @@ -596,6 +596,40 @@ export class AddonModH5PActivityProvider { ); } + /** + * Report an H5P activity report as being viewed. + * + * @param id H5P activity ID. + * @param name Name of the activity. + * @param options Options. + * @return Promise resolved when the WS call is successful. + */ + async logViewReport(id: number, name?: string, options: AddonModH5PActivityViewReportOptions = {}): Promise { + const site = await CoreSites.getSite(options.siteId); + + if (!site.wsAvailable('mod_h5pactivity_log_report_viewed')) { + // Site doesn't support the WS, stop. + return; + } + + const params: AddonModH5PActivityLogReportViewedWSParams = { + h5pactivityid: id, + userid: options.userId, + attemptid: options.attemptId, + }; + + return CoreCourseLogHelper.logSingle( + 'mod_h5pactivity_log_report_viewed', + params, + AddonModH5PActivityProvider.COMPONENT, + id, + name, + 'h5pactivity', + {}, + site.getId(), + ); + } + } export const AddonModH5PActivity = makeSingleton(AddonModH5PActivityProvider); @@ -840,10 +874,28 @@ export type AddonModH5PActivityGetAttemptsOptions = AddonModH5PActivityGetAttemp /** * Params of mod_h5pactivity_view_h5pactivity WS. */ -export type AddonModH5pactivityViewH5pactivityWSParams = { +export type AddonModH5PActivityViewH5pactivityWSParams = { h5pactivityid: number; // H5P activity instance id. }; +/** + * Params of mod_h5pactivity_log_report_viewed WS. + */ +export type AddonModH5PActivityLogReportViewedWSParams = { + h5pactivityid: number; // H5P activity instance id. + userid?: number | null; // The user id to log attempt (null means only current user). + attemptid?: number | null; // The attempt id. +}; + +/** + * Options for logViewReport. + */ +export type AddonModH5PActivityViewReportOptions = { + userId?: number; // User ID being viewed. Undefined for current user or when viewing an attempt. + attemptId?: number; // Attempt ID being viewed. Undefined if no attempt. + siteId?: string; // Site ID. If not defined, current site. +}; + declare module '@singletons/events' { /**