From 8f4b2df43ab46aabddda681d33d1b075917962fe Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 20 Dec 2021 12:42:34 +0100 Subject: [PATCH] MOBILE-3948 h5p: User uniqueness by id instead of email --- .../h5p/assets/moodle/js/h5p_overrides.js | 43 ++++++++++++++++++- src/core/features/h5p/classes/helper.ts | 15 +++---- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/core/features/h5p/assets/moodle/js/h5p_overrides.js b/src/core/features/h5p/assets/moodle/js/h5p_overrides.js index c9350e204..b4758cf31 100644 --- a/src/core/features/h5p/assets/moodle/js/h5p_overrides.js +++ b/src/core/features/h5p/assets/moodle/js/h5p_overrides.js @@ -52,4 +52,45 @@ H5P.getMoodleComponent = function () { return H5PIntegration.moodleComponent; } return undefined; -}; \ No newline at end of file +}; + +/** + * Set the actor. (Moved to overrides due to MDL-69467) + */ + H5P.XAPIEvent.prototype.setActor = function () { + if (H5PIntegration.user !== undefined) { + this.data.statement.actor = { + 'name': H5PIntegration.user.name, + 'objectType': 'Agent' + }; + if (H5PIntegration.user.id !== undefined) { + this.data.statement.actor.account = { + 'name': H5PIntegration.user.id, + 'homePage': H5PIntegration.siteUrl + } + } else if (H5PIntegration.user.mail !== undefined) { + this.data.statement.actor.mbox = 'mailto:' + H5PIntegration.user.mail; + } + } else { + var uuid; + try { + if (localStorage.H5PUserUUID) { + uuid = localStorage.H5PUserUUID; + } else { + uuid = H5P.createUUID(); + localStorage.H5PUserUUID = uuid; + } + } + catch (err) { + // LocalStorage and Cookies are probably disabled. Do not track the user. + uuid = 'not-trackable-' + H5P.createUUID(); + } + this.data.statement.actor = { + 'account': { + 'name': uuid, + 'homePage': H5PIntegration.siteUrl + }, + 'objectType': 'Agent' + }; + } +}; diff --git a/src/core/features/h5p/classes/helper.ts b/src/core/features/h5p/classes/helper.ts index 23e447b93..7e685b06b 100644 --- a/src/core/features/h5p/classes/helper.ts +++ b/src/core/features/h5p/classes/helper.ts @@ -19,10 +19,8 @@ import { CoreSites } from '@services/sites'; import { CoreMimetypeUtils } from '@services/utils/mimetype'; import { CoreTextUtils } from '@services/utils/text'; import { CoreUtils } from '@services/utils/utils'; -import { CoreUser } from '@features/user/services/user'; import { CoreH5P } from '../services/h5p'; import { CoreH5PCore, CoreH5PDisplayOptions } from './core'; -import { Translate } from '@singletons'; import { CoreError } from '@classes/errors/error'; /** @@ -117,11 +115,11 @@ export class CoreH5PHelper { const site = await CoreSites.getSite(siteId); - const userId = site.getUserId(); - const user = await CoreUtils.ignoreErrors(CoreUser.getProfile(userId, undefined, false, siteId)); + const info = site.getInfo(); - if (!user || !user.email) { - throw new CoreError(Translate.instant('core.h5p.errorgetemail')); + if (!info) { + // Shouldn't happen for authenticated sites. + throw new CoreError('Site info could not be fetched.'); } const basePath = CoreFile.getBasePathInstant(); @@ -151,7 +149,7 @@ export class CoreH5PHelper { l10n: { H5P: CoreH5P.h5pCore.getLocalization(), // eslint-disable-line @typescript-eslint/naming-convention }, - user: { name: site.getInfo()!.fullname, mail: user.email }, + user: { name: info.username, id: info.userid }, hubIsEnabled: false, reportingIsEnabled: false, crossorigin: null, @@ -252,7 +250,8 @@ export type CoreH5PCoreSettings = { }; user: { name: string; - mail: string; + id?: number; + mail?: string; }; hubIsEnabled: boolean; reportingIsEnabled: boolean;