Fixing an issue with CMI interactions not being loaded in the proper order

Interaction ID and Type need to be loaded before most other interaction fields, so I added a built in sort when loading using the flattened JSON load.
This commit is contained in:
Jonathan Putney
2020-07-28 22:36:37 -04:00
parent f4ab13ca8c
commit 1b48c9ee4d
7 changed files with 138 additions and 15 deletions

80
dist/scorm-again.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{ {
"name": "scorm-again", "name": "scorm-again",
"version": "1.3.1", "version": "1.3.2",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@@ -1,6 +1,6 @@
{ {
"name": "scorm-again", "name": "scorm-again",
"version": "1.3.1", "version": "1.3.2",
"description": "A modern SCORM JavaScript run-time library for AICC, SCORM 1.2, and SCORM 2004", "description": "A modern SCORM JavaScript run-time library for AICC, SCORM 1.2, and SCORM 2004",
"main": "dist/scorm-again.min.js", "main": "dist/scorm-again.min.js",
"directories": { "directories": {

View File

@@ -3,7 +3,7 @@ import {CMIArray} from './cmi/common';
import {ValidationError} from './exceptions'; import {ValidationError} from './exceptions';
import ErrorCodes from './constants/error_codes'; import ErrorCodes from './constants/error_codes';
import APIConstants from './constants/api_constants'; import APIConstants from './constants/api_constants';
import {unflatten} from './utilities'; import {sortFlattenedCMIElements, unflatten} from './utilities';
import debounce from 'lodash.debounce'; import debounce from 'lodash.debounce';
const global_constants = APIConstants.global; const global_constants = APIConstants.global;
@@ -848,7 +848,55 @@ export default class BaseAPI {
* @param {string} CMIElement * @param {string} CMIElement
*/ */
loadFromFlattenedJSON(json, CMIElement) { loadFromFlattenedJSON(json, CMIElement) {
this.loadFromJSON(unflatten(json), CMIElement); if (!this.isNotInitialized()) {
console.error(
'loadFromFlattenedJSON can only be called before the call to lmsInitialize.');
return;
}
console.info(json);
const result = Object.keys(json).map(function(key) {
return [String(key), json[key]];
});
// CMI interactions need to have id and type loaded before any other fields
result.sort(function([a, b], [c, d]) {
const id_pattern = /^cmi\.interactions\.(\d+)\.id$/;
const type_pattern = /^cmi\.interactions\.(\d+)\.type$/;
const pattern = /^cmi\.interactions\.(\d+)/;
if (a.match(id_pattern) && c.match(pattern)) {
if (a.match(id_pattern)[1] < c.match(pattern)[1]) return -1;
if (a.match(id_pattern)[1] > c.match(pattern)[1]) return 1;
return -1;
} else if (a.match(pattern) && c.match(id_pattern)) {
if (a.match(pattern)[1] < c.match(id_pattern)[1]) return -1;
if (a.match(pattern)[1] > c.match(id_pattern)[1]) return 1;
return 1;
} else if (a.match(type_pattern) && c.match(pattern)) {
if (a.match(type_pattern)[1] < c.match(pattern)[1]) return -1;
if (a.match(type_pattern)[1] > c.match(pattern)[1]) return 1;
return -1;
} else if (a.match(pattern) && c.match(type_pattern)) {
if (a.match(pattern)[1] < c.match(type_pattern)[1]) return -1;
if (a.match(pattern)[1] > c.match(type_pattern)[1]) return 1;
return 1;
}
if (a < c) return -1;
if (a > c) return 1;
return 0;
});
console.info(result);
let obj;
result.forEach((element) => {
obj = {};
obj[element[0]] = element[1];
this.loadFromJSON(unflatten(obj), CMIElement);
});
} }
/** /**

View File

@@ -819,8 +819,13 @@ export class CMIInteractionsObject extends BaseCMI {
* @param {string} type * @param {string} type
*/ */
set type(type) { set type(type) {
if (check2004ValidFormat(type, scorm2004_regex.CMIType)) { if (typeof this.id === 'undefined') {
this.#type = type; throw new ValidationError(
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
} else {
if (check2004ValidFormat(type, scorm2004_regex.CMIType)) {
this.#type = type;
}
} }
} }
@@ -874,7 +879,7 @@ export class CMIInteractionsObject extends BaseCMI {
* @param {string} learner_response * @param {string} learner_response
*/ */
set learner_response(learner_response) { set learner_response(learner_response) {
if (typeof this.type === 'undefined') { if (typeof this.type === 'undefined' || typeof this.id === 'undefined') {
throw new ValidationError( throw new ValidationError(
scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED); scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
} else { } else {