diff --git a/src/BaseAPI.js b/src/BaseAPI.js index 8cf1926..32b2ace 100644 --- a/src/BaseAPI.js +++ b/src/BaseAPI.js @@ -837,14 +837,6 @@ export default class BaseAPI { 'The storeData method has not been implemented'); } - /** - * Gets the current total time as total_time + session_time - * APIs that inherit BaseAPI should override this function - */ - getCurrentTotalTime() { - this.cmi.getCurrentTotalTime(); - } - /** * Send the request to the LMS * @param {string} url diff --git a/src/Scorm12API.js b/src/Scorm12API.js index d8c7053..36d6c36 100644 --- a/src/Scorm12API.js +++ b/src/Scorm12API.js @@ -220,7 +220,7 @@ export default class Scorm12API extends BaseAPI { const cmiExport = this.renderCMIToJSONObject(); if (terminateCommit) { - cmiExport.cmi.core.total_time = this.getCurrentTotalTime(); + cmiExport.cmi.core.total_time = this.cmi.getCurrentTotalTime(); } const result = []; diff --git a/src/Scorm2004API.js b/src/Scorm2004API.js index bca50a1..e2b84ab 100644 --- a/src/Scorm2004API.js +++ b/src/Scorm2004API.js @@ -439,17 +439,17 @@ export default class Scorm2004API extends BaseAPI { * @return {object|Array} */ renderCommitCMI(terminateCommit: boolean) { - const cmi = this.renderCMIToJSONObject(); + const cmiExport = this.renderCMIToJSONObject(); if (terminateCommit) { - cmi.total_time = this.getCurrentTotalTime(); + cmiExport.cmi.total_time = this.cmi.getCurrentTotalTime(); } const result = []; - const flattened = Utilities.flatten(cmi); + const flattened = Utilities.flatten(cmiExport); switch (this.settings.dataCommitFormat) { case 'flattened': - return Utilities.flatten(cmi); + return Utilities.flatten(cmiExport); case 'params': for (const item in flattened) { if ({}.hasOwnProperty.call(flattened, item)) { @@ -459,7 +459,7 @@ export default class Scorm2004API extends BaseAPI { return result; case 'json': default: - return cmi; + return cmiExport; } } @@ -471,7 +471,7 @@ export default class Scorm2004API extends BaseAPI { */ storeData(terminateCommit: boolean) { if (terminateCommit) { - if (this.cmi.lesson_mode === 'normal') { + if (this.cmi.mode === 'normal') { if (this.cmi.credit === 'credit') { if (this.cmi.completion_threshold && this.cmi.progress_measure) { if (this.cmi.progress_measure >= this.cmi.completion_threshold) { @@ -493,7 +493,8 @@ export default class Scorm2004API extends BaseAPI { } let navRequest = false; - if (this.adl.nav.request !== (this.startingData?.adl?.nav?.request || '')) { + if (this.adl.nav.request !== (this.startingData?.adl?.nav?.request) && + this.adl.nav.request !== '_none_') { this.adl.nav.request = encodeURIComponent(this.adl.nav.request); navRequest = true; } diff --git a/src/cmi/common.js b/src/cmi/common.js index 72f4298..7de7d4e 100644 --- a/src/cmi/common.js +++ b/src/cmi/common.js @@ -223,11 +223,14 @@ export class CMIScore extends BaseCMI { * @return {{min: string, max: string, raw: string}} */ toJSON() { - return { + this.jsonString = true; + const result = { 'raw': this.raw, 'min': this.min, 'max': this.max, }; + delete this.jsonString; + return result; } } diff --git a/src/cmi/scorm2004_cmi.js b/src/cmi/scorm2004_cmi.js index 0cc0786..718b130 100644 --- a/src/cmi/scorm2004_cmi.js +++ b/src/cmi/scorm2004_cmi.js @@ -67,6 +67,23 @@ function check2004ValidRange(value: any, rangePattern: String) { * Class representing cmi object for SCORM 2004 */ export class CMI extends BaseCMI { + /** + * Constructor for the SCORM 2004 cmi object + * @param {boolean} initialized + */ + constructor(initialized: boolean) { + super(); + + this.learner_preference = new CMILearnerPreference(); + this.score = new Scorm2004CMIScore(); + this.comments_from_learner = new CMICommentsFromLearner(); + this.comments_from_lms = new CMICommentsFromLMS(); + this.interactions = new CMIInteractions(); + this.objectives = new CMIObjectives(); + + if (initialized) this.initialize(); + } + #_version = '1.0'; #_children = constants.cmi_children; #completion_status = 'unknown'; @@ -88,23 +105,6 @@ export class CMI extends BaseCMI { #time_limit_action = 'continue,no message'; #total_time = '0'; - /** - * Constructor for the SCORM 2004 cmi object - * @param {boolean} initialized - */ - constructor(initialized: boolean) { - super(); - - this.learner_preference = new CMILearnerPreference(); - this.score = new Scorm2004CMIScore(); - this.comments_from_learner = new CMICommentsFromLearner(); - this.comments_from_lms = new CMICommentsFromLMS(); - this.interactions = new CMIInteractions(); - this.objectives = new CMIObjectives(); - - if (initialized) this.initialize(); - } - /** * Called when the API has been initialized after the CMI has been created */ diff --git a/test/Scorm12API.spec.js b/test/Scorm12API.spec.js index 223ff73..1a0ddc3 100644 --- a/test/Scorm12API.spec.js +++ b/test/Scorm12API.spec.js @@ -272,6 +272,19 @@ describe('SCORM 1.2 API Tests', () => { ).to.equal('student_2'); }); + describe('renderCommitCMI()', () => { + it('should calculate total time when terminateCommit passed', + () => { + const scorm12API = api(); + scorm12API.cmi.core.total_time = '12:34:56'; + scorm12API.cmi.core.session_time = '23:59:59'; + const cmiExport = scorm12API.renderCommitCMI(true); + expect( + cmiExport.cmi.core.total_time + ).to.equal('36:34:55'); + }); + }); + describe('storeData()', () => { it('should set cmi.core.lesson_status to "completed"', () => { const scorm12API = api(); diff --git a/test/Scorm2004API.spec.js b/test/Scorm2004API.spec.js index da29356..14ee28f 100644 --- a/test/Scorm2004API.spec.js +++ b/test/Scorm2004API.spec.js @@ -287,4 +287,56 @@ describe('SCORM 2004 API Tests', () => { }); }); }); + + describe('renderCommitCMI()', () => { + it('should calculate total time when terminateCommit passed', + () => { + const scorm2004API = api(); + scorm2004API.cmi.total_time = 'PT12H34M56S'; + scorm2004API.cmi.session_time = 'PT23H59M59S'; + const cmiExport = scorm2004API.renderCommitCMI(true); + expect( + cmiExport.cmi.total_time + ).to.equal('P1DT12H34M55S'); + }); + }); + + describe('storeData()', () => { + it('should set cmi.completion_status to "completed"', + () => { + const scorm2004API = api(); + scorm2004API.cmi.credit = 'credit'; + scorm2004API.cmi.completion_threshold = '0.6'; + scorm2004API.cmi.progress_measure = '0.75'; + scorm2004API.storeData(true); + expect(scorm2004API.cmi.completion_status).to.equal('completed'); + }); + it('should set cmi.completion_status to "incomplete"', + () => { + const scorm2004API = api(); + scorm2004API.cmi.credit = 'credit'; + scorm2004API.cmi.completion_threshold = '0.7'; + scorm2004API.cmi.progress_measure = '0.6'; + scorm2004API.storeData(true); + expect(scorm2004API.cmi.completion_status).to.equal('incomplete'); + }); + it('should set cmi.success_status to "passed"', + () => { + const scorm2004API = api(); + scorm2004API.cmi.credit = 'credit'; + scorm2004API.cmi.score.scaled = '0.7'; + scorm2004API.cmi.scaled_passing_score = '0.6'; + scorm2004API.storeData(true); + expect(scorm2004API.cmi.success_status).to.equal('passed'); + }); + it('should set cmi.success_status to "failed"', + () => { + const scorm2004API = api(); + scorm2004API.cmi.credit = 'credit'; + scorm2004API.cmi.score.scaled = '0.6'; + scorm2004API.cmi.scaled_passing_score = '0.7'; + scorm2004API.storeData(true); + expect(scorm2004API.cmi.success_status).to.equal('failed'); + }); + }); });