Not requiring fields to be previously set while loading existing data

This commit is contained in:
Jonathan Putney
2020-07-30 16:54:15 -04:00
parent dcad088b25
commit c341d3f70f
6 changed files with 199 additions and 188 deletions

View File

@@ -848,7 +848,6 @@ export default class BaseAPI {
* @param {string} CMIElement
*/
loadFromFlattenedJSON(json, CMIElement) {
console.clear();
if (!this.isNotInitialized()) {
console.error(
'loadFromFlattenedJSON can only be called before the call to lmsInitialize.');
@@ -866,39 +865,23 @@ export default class BaseAPI {
function testPattern(a, c, a_pattern) {
const a_match = a.match(a_pattern);
if (a_match !== null) {
let c_match = c.match(
new RegExp(a_match[1] + Number(a_match[2]) + '.(.*)'));
if (c_match !== null) {
const a_int = Number(a_match[2]);
if (a_match[3] === 'id') {
return -1;
} else if (a_match[3] === 'type' && c_match[1] !== 'id') {
return -1;
} else {
return 1;
}
} else if ((c_match = c.match(a_pattern)) !== null) {
return Number(a_match[2]) - Number(c_match[2]);
} else {
return testPattern(c, a, a_pattern);
}
let c_match;
if (a_match !== null && (c_match = c.match(a_pattern)) !== null) {
return Number(a_match[2]) - Number(c_match[2]);
}
return null;
}
/**
* Function to sort the array, which we'll call until we're done.
*
* @param {string} a
* @param {string} b
* @param {string} c
* @param {string} d
* @return {number}
*/
function resultSort([a, b], [c, d]) {
const int_pattern = /^(cmi\.interactions\.)(\d+)\.(.*)$/;
const obj_pattern = /^(cmi\.objectives\.)(\d+)\.(.*)$/;
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]) {
let test;
if ((test = testPattern(a, c, int_pattern)) !== null) {
return test;
@@ -914,25 +897,10 @@ export default class BaseAPI {
return 1;
}
return 0;
}
const int_pattern = /^(cmi\.interactions\.)(\d+)\.(.*)$/;
const obj_pattern = /^(cmi\.objectives\.)(\d+)\.(.*)$/;
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
// Call the sort until everything is properly sorted.
// I must be missing an edge case in my sort?
let sorted_result = [];
while (sorted_result !== result.sort(resultSort)) {
sorted_result = result;
}
let obj;
sorted_result.forEach((element) => {
result.forEach((element) => {
obj = {};
obj[element[0]] = element[1];
this.loadFromJSON(unflatten(obj), CMIElement);