From 7b5a5bed8f4c8c28f627b99d8ffc0ae795b15b5e Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 20 Jun 2022 15:05:51 +0200 Subject: [PATCH] MOBILE-4099 scorm: Don't call WS if user cannot save tracks --- src/addons/mod/scorm/classes/data-model-12.ts | 34 ++++++-------- src/addons/mod/scorm/pages/player/player.ts | 45 +++++++++++++++---- src/addons/mod/scorm/services/scorm.ts | 9 ++++ 3 files changed, 58 insertions(+), 30 deletions(-) diff --git a/src/addons/mod/scorm/classes/data-model-12.ts b/src/addons/mod/scorm/classes/data-model-12.ts index 8c82d6876..3fb1609cd 100644 --- a/src/addons/mod/scorm/classes/data-model-12.ts +++ b/src/addons/mod/scorm/classes/data-model-12.ts @@ -93,13 +93,6 @@ export class AddonModScormDataModel12 { protected errorCode = '0'; // Last error. protected timeout?: number; // Timeout to commit changes. - protected siteId: string; - protected scorm: AddonModScormScorm; - protected scoId: number; - protected attempt: number; - protected mode: string; - protected offline: boolean; - /** * Constructor. * @@ -110,23 +103,18 @@ export class AddonModScormDataModel12 { * @param userData The user default data. * @param mode Mode being played. By default, MODENORMAL. * @param offline Whether the attempt is offline. + * @param canSaveTracks Whether the user can save tracks. */ constructor( - siteId: string, - scorm: AddonModScormScorm, - scoId: number, - attempt: number, - userData: AddonModScormUserDataMap, - mode?: string, - offline?: boolean, + protected siteId: string, + protected scorm: AddonModScormScorm, + protected scoId: number, + protected attempt: number, + protected userData: AddonModScormUserDataMap, + protected mode = AddonModScormProvider.MODENORMAL, + protected offline = false, + protected canSaveTracks = true, ) { - this.siteId = siteId; - this.scorm = scorm; - this.scoId = scoId; - this.attempt = attempt; - this.mode = mode || AddonModScormProvider.MODENORMAL; - this.offline = !!offline; - this.init(userData); } @@ -981,6 +969,10 @@ export class AddonModScormDataModel12 { * @return True if success, false otherwise. */ protected storeData(storeTotalTime?: boolean): boolean { + if (!this.canSaveTracks) { + return true; + } + let tracks: AddonModScormDataEntry[]; if (storeTotalTime) { diff --git a/src/addons/mod/scorm/pages/player/player.ts b/src/addons/mod/scorm/pages/player/player.ts index d6dabb226..cdf25d82b 100644 --- a/src/addons/mod/scorm/pages/player/player.ts +++ b/src/addons/mod/scorm/pages/player/player.ts @@ -132,6 +132,10 @@ export class AddonModScormPlayerPage implements OnInit, OnDestroy { } + get canSaveTracks(): boolean { + return !this.accessInfo || !!this.accessInfo.cansavetrack; + } + /** * Initialize. * @@ -235,9 +239,13 @@ export class AddonModScormPlayerPage implements OnInit, OnDestroy { * Determine the attempt to use, the mode (normal/preview) and if it's offline or online. * * @param attemptsData Attempts count. + * @param accessInfo Access info. * @return Promise resolved when done. */ - protected async determineAttemptAndMode(attemptsData: AddonModScormAttemptCountResult): Promise { + protected async determineAttemptAndMode( + attemptsData: AddonModScormAttemptCountResult, + accessInfo: AddonModScormGetScormAccessInformationWSResponse, + ): Promise { const data = await AddonModScormHelper.determineAttemptToContinue(this.scorm, attemptsData); let incomplete = false; @@ -257,7 +265,14 @@ export class AddonModScormPlayerPage implements OnInit, OnDestroy { } // Determine mode and attempt to use. - const result = AddonModScorm.determineAttemptAndMode(this.scorm, this.mode, this.attempt, this.newAttempt, incomplete); + const result = AddonModScorm.determineAttemptAndMode( + this.scorm, + this.mode, + this.attempt, + this.newAttempt, + incomplete, + accessInfo.cansavetrack, + ); if (result.attempt > this.attempt) { // We're creating a new attempt. @@ -300,23 +315,26 @@ export class AddonModScormPlayerPage implements OnInit, OnDestroy { try { // Get attempts data. - const attemptsData = await AddonModScorm.getAttemptCount(this.scorm.id, { cmId: this.cmId }); + const [attemptsData, accessInfo] = await Promise.all([ + AddonModScorm.getAttemptCount(this.scorm.id, { cmId: this.cmId }), + AddonModScorm.getAccessInformation(this.scorm.id, { + cmId: this.cmId, + }), + ]); - await this.determineAttemptAndMode(attemptsData); + this.accessInfo = accessInfo; - const [data, accessInfo] = await Promise.all([ + await this.determineAttemptAndMode(attemptsData, accessInfo); + + const [data] = await Promise.all([ AddonModScorm.getScormUserData(this.scorm.id, this.attempt, { cmId: this.cmId, offline: this.offline, }), - AddonModScorm.getAccessInformation(this.scorm.id, { - cmId: this.cmId, - }), this.fetchToc(), ]); this.userData = data; - this.accessInfo = accessInfo; } catch (error) { CoreDomUtils.showErrorModalDefault(error, 'addon.mod_scorm.errorgetscorm', true); } @@ -400,6 +418,7 @@ export class AddonModScormPlayerPage implements OnInit, OnDestroy { this.userData!, this.mode, this.offline, + this.canSaveTracks, ); // Add the model to the window so the SCORM can access it. @@ -453,6 +472,10 @@ export class AddonModScormPlayerPage implements OnInit, OnDestroy { * @return Promise resolved when done. */ protected async markCompleted(sco: AddonModScormScoWithData): Promise { + if (!this.canSaveTracks) { + return; + } + const tracks = [{ element: 'cmi.core.lesson_status', value: 'completed', @@ -536,6 +559,10 @@ export class AddonModScormPlayerPage implements OnInit, OnDestroy { * @return Promise resolved when done. */ protected async setStartTime(scoId: number): Promise { + if (!this.canSaveTracks) { + return; + } + const tracks = [{ element: 'x.start.time', value: String(CoreTimeUtils.timestamp()), diff --git a/src/addons/mod/scorm/services/scorm.ts b/src/addons/mod/scorm/services/scorm.ts index 27bb5c945..87c9fa072 100644 --- a/src/addons/mod/scorm/services/scorm.ts +++ b/src/addons/mod/scorm/services/scorm.ts @@ -206,6 +206,7 @@ export class AddonModScormProvider { * @param attempt Current attempt. * @param newAttempt Whether it should start a new attempt. * @param incomplete Whether current attempt is incomplete. + * @param canSaveTracks Whether the user can save tracks. * @return Mode, attempt number and whether to start a new attempt. */ determineAttemptAndMode( @@ -214,7 +215,15 @@ export class AddonModScormProvider { attempt: number, newAttempt?: boolean, incomplete?: boolean, + canSaveTracks = true, ): {mode: string; attempt: number; newAttempt: boolean} { + if (!canSaveTracks) { + return { + mode: scorm.hidebrowse ? AddonModScormProvider.MODENORMAL : mode, + attempt, + newAttempt: false, + }; + } if (mode == AddonModScormProvider.MODEBROWSE) { if (scorm.hidebrowse) {