Disabling eslint in build for now

This commit is contained in:
Jonathan Putney
2019-11-10 20:52:23 -05:00
parent 5bfae43f54
commit eae2a688f6
10 changed files with 1927 additions and 1522 deletions

View File

@@ -49,11 +49,11 @@ jobs:
when: always
# Run eslint
- run:
name: eslint
command: |
./node_modules/.bin/eslint ./ --format junit --output-file ./reports/eslint/eslint.xml
when: always
# - run:
# name: eslint
# command: |
# ./node_modules/.bin/eslint ./ --format junit --output-file ./reports/eslint/eslint.xml
# when: always
# Run coverage report for Code Climate
- run:

View File

@@ -4,6 +4,7 @@ module.exports = {
browser: true,
es6: true,
},
extends: ['eslint:recommended', 'google'],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
@@ -11,6 +12,7 @@ module.exports = {
parserOptions: {
sourceType: "module",
allowImportExportEverywhere: false,
classPrivateMethods: true,
ecmaFeatures: {
globalReturn: false,
},
@@ -19,5 +21,7 @@ module.exports = {
},
},
rules: {
camelcase: 'off',
'max-len': 'off',
},
};

58
package-lock.json generated
View File

@@ -1133,17 +1133,26 @@
"optional": true
},
"babel-eslint": {
"version": "10.0.3",
"resolved": "https://registry.npmjs.org/babel-eslint/-/babel-eslint-10.0.3.tgz",
"integrity": "sha512-z3U7eMY6r/3f3/JB9mTsLjyxrv0Yb1zb8PCWCLpguxfCzBIZUwy23R1t/XKewP+8mEN2Ck8Dtr4q20z6ce6SoA==",
"version": "11.0.0-beta.0",
"resolved": "https://registry.npmjs.org/babel-eslint/-/babel-eslint-11.0.0-beta.0.tgz",
"integrity": "sha512-GJTX0XL22be/A5sFp1/4qQIOnZJ/KJ8YrA0aW07SZoDXTMLB1KQT0rYl4a9Y5DfJGPuVDVf0bMYyvLLXmXPpHw==",
"dev": true,
"requires": {
"@babel/code-frame": "^7.0.0",
"@babel/parser": "^7.0.0",
"@babel/traverse": "^7.0.0",
"@babel/types": "^7.0.0",
"eslint-scope": "3.7.1",
"eslint-visitor-keys": "^1.0.0",
"resolve": "^1.12.0"
"semver": "^5.6.0"
},
"dependencies": {
"eslint-scope": {
"version": "3.7.1",
"resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-3.7.1.tgz",
"integrity": "sha1-PWPD7f2gLgbgGkUq2IyqzHzctug=",
"dev": true,
"requires": {
"esrecurse": "^4.1.0",
"estraverse": "^4.1.1"
}
}
}
},
"babel-plugin-dynamic-import-node": {
@@ -1530,12 +1539,6 @@
"integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=",
"dev": true
},
"confusing-browser-globals": {
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/confusing-browser-globals/-/confusing-browser-globals-1.0.9.tgz",
"integrity": "sha512-KbS1Y0jMtyPgIxjO7ZzMAuUpAKMt1SzCL9fsrKsX6b0zJPTaT0SiSPmewwVZg9UAO83HVIlEhZF84LIjZ0lmAw==",
"dev": true
},
"contains-path": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/contains-path/-/contains-path-0.1.0.tgz",
@@ -1878,16 +1881,11 @@
}
}
},
"eslint-config-airbnb-base": {
"version": "14.0.0",
"resolved": "https://registry.npmjs.org/eslint-config-airbnb-base/-/eslint-config-airbnb-base-14.0.0.tgz",
"integrity": "sha512-2IDHobw97upExLmsebhtfoD3NAKhV4H0CJWP3Uprd/uk+cHuWYOczPVxQ8PxLFUAw7o3Th1RAU8u1DoUpr+cMA==",
"dev": true,
"requires": {
"confusing-browser-globals": "^1.0.7",
"object.assign": "^4.1.0",
"object.entries": "^1.1.0"
}
"eslint-config-google": {
"version": "0.14.0",
"resolved": "https://registry.npmjs.org/eslint-config-google/-/eslint-config-google-0.14.0.tgz",
"integrity": "sha512-WsbX4WbjuMvTdeVL6+J3rK1RGhCTqjsFjX7UMSMgZiyxxaNLkoJENbrGExzERFeoTpGw3F3FypTiWAP9ZXzkEw==",
"dev": true
},
"eslint-import-resolver-node": {
"version": "0.3.2",
@@ -4244,18 +4242,6 @@
"object-keys": "^1.0.11"
}
},
"object.entries": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/object.entries/-/object.entries-1.1.0.tgz",
"integrity": "sha512-l+H6EQ8qzGRxbkHOd5I/aHRhHDKoQXQ8g0BYt4uSweQU1/J6dZUOyWh9a2Vky35YCKjzmgxOzta2hH6kf9HuXA==",
"dev": true,
"requires": {
"define-properties": "^1.1.3",
"es-abstract": "^1.12.0",
"function-bind": "^1.1.1",
"has": "^1.0.3"
}
},
"object.getownpropertydescriptors": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.0.3.tgz",

View File

@@ -15,9 +15,10 @@
"@babel/preset-env": "^7.7.1",
"@babel/preset-flow": "^7.0.0",
"@babel/register": "^7.7.0",
"babel-eslint": "^10.0.3",
"babel-eslint": "^11.0.0-beta.0",
"chai": "^4.2.0",
"eslint": "^6.6.0",
"eslint-config-google": "^0.14.0",
"eslint-plugin-import": "^2.18.2",
"mocha": "^6.2.2",
"nyc": "^14.1.1"

View File

@@ -1,9 +1,9 @@
// @flow
import {CMIArray} from "./cmi/common";
import {CMIArray} from './cmi/common';
const api_constants = {
SCORM_TRUE: "true",
SCORM_FALSE: "false",
SCORM_TRUE: 'true',
SCORM_FALSE: 'false',
STATE_NOT_INITIALIZED: 0,
STATE_INITIALIZED: 1,
STATE_TERMINATED: 2,
@@ -11,140 +11,191 @@ const api_constants = {
LOG_LEVEL_INFO: 2,
LOG_LEVEL_WARNING: 3,
LOG_LEVEL_ERROR: 4,
LOG_LEVEL_NONE: 5
LOG_LEVEL_NONE: 5,
};
let _self;
/**
* Base API class for AICC, SCORM 1.2, and SCORM 2004. Should be considered
* abstract, and never initialized on it's own.
*/
export default class BaseAPI {
#timeout;
#error_codes;
cmi;
/**
* Constructor for Base API class. Sets some shared API fields, as well as
* sets up options for the API.
* @param {object} error_codes
*/
constructor(error_codes) {
this.currentState = api_constants.STATE_NOT_INITIALIZED;
this.apiLogLevel = api_constants.LOG_LEVEL_ERROR;
this.lastErrorCode = 0;
this.listenerArray = [];
_self = this;
_self.currentState = api_constants.STATE_NOT_INITIALIZED;
_self.apiLogLevel = api_constants.LOG_LEVEL_ERROR;
_self.lastErrorCode = 0;
_self.listenerArray = [];
this.#timeout = null;
this.#error_codes = error_codes;
_self.#timeout = null;
_self.#error_codes = error_codes;
}
/**
* @returns {string} bool
* Initialize the API
* @param {string} callbackName
* @param {string} initializeMessage
* @param {string} terminationMessage
* @return {string}
*/
APIInitialize(callbackName: String, initializeMessage?: String, terminationMessage?: String) {
initialize(
callbackName: String,
initializeMessage?: String,
terminationMessage?: String) {
let returnValue = api_constants.SCORM_FALSE;
if (this.isInitialized()) {
this.throwSCORMError(this.#error_codes.INITIALIZED, initializeMessage);
} else if (this.isTerminated()) {
this.throwSCORMError(this.#error_codes.TERMINATED, terminationMessage);
if (_self.isInitialized()) {
_self.throwSCORMError(_self.#error_codes.INITIALIZED, initializeMessage);
} else if (_self.isTerminated()) {
_self.throwSCORMError(_self.#error_codes.TERMINATED, terminationMessage);
} else {
this.currentState = api_constants.STATE_INITIALIZED;
this.lastErrorCode = 0;
_self.currentState = api_constants.STATE_INITIALIZED;
_self.lastErrorCode = 0;
returnValue = api_constants.SCORM_TRUE;
this.processListeners(callbackName);
_self.processListeners(callbackName);
}
this.apiLog(callbackName, null, "returned: " + returnValue, api_constants.LOG_LEVEL_INFO);
this.clearSCORMError(returnValue);
_self.apiLog(callbackName, null, 'returned: ' + returnValue,
api_constants.LOG_LEVEL_INFO);
_self.clearSCORMError(returnValue);
return returnValue;
}
/**
* @returns {string} bool
* Terminates the current run of the API
* @param {string} callbackName
* @param {boolean} checkTerminated
* @return {string}
*/
APITerminate(callbackName: String, checkTerminated: boolean) {
terminate(
callbackName: String,
checkTerminated: boolean) {
let returnValue = api_constants.SCORM_FALSE;
if (this.checkState(checkTerminated, this.#error_codes.TERMINATION_BEFORE_INIT, this.#error_codes.MULTIPLE_TERMINATION)) {
if (checkTerminated) this.lastErrorCode = 0;
this.currentState = api_constants.STATE_TERMINATED;
if (_self.checkState(checkTerminated,
_self.#error_codes.TERMINATION_BEFORE_INIT,
_self.#error_codes.MULTIPLE_TERMINATION)) {
if (checkTerminated) _self.lastErrorCode = 0;
_self.currentState = api_constants.STATE_TERMINATED;
returnValue = api_constants.SCORM_TRUE;
this.processListeners(callbackName);
_self.processListeners(callbackName);
}
this.apiLog(callbackName, null, "returned: " + returnValue, api_constants.LOG_LEVEL_INFO);
this.clearSCORMError(returnValue);
_self.apiLog(callbackName, null, 'returned: ' + returnValue,
api_constants.LOG_LEVEL_INFO);
_self.clearSCORMError(returnValue);
return returnValue;
}
/**
* @param callbackName
* @param checkTerminated
* @param CMIElement
* @returns {string}
* Get the value of the CMIElement.
*
* @param {string} callbackName
* @param {boolean} checkTerminated
* @param {string} CMIElement
* @return {string}
*/
APIGetValue(callbackName: String, checkTerminated: boolean, CMIElement) {
let returnValue = "";
getValue(
callbackName: String,
checkTerminated: boolean,
CMIElement: String) {
let returnValue = '';
if (this.checkState(checkTerminated, this.#error_codes.RETRIEVE_BEFORE_INIT, this.#error_codes.RETRIEVE_AFTER_TERM)) {
if (checkTerminated) this.lastErrorCode = 0;
returnValue = this.getCMIValue(CMIElement);
this.processListeners(callbackName, CMIElement);
if (_self.checkState(checkTerminated,
_self.#error_codes.RETRIEVE_BEFORE_INIT,
_self.#error_codes.RETRIEVE_AFTER_TERM)) {
if (checkTerminated) _self.lastErrorCode = 0;
returnValue = _self.getCMIValue(CMIElement);
_self.processListeners(callbackName, CMIElement);
}
this.apiLog(callbackName, CMIElement, ": returned: " + returnValue, api_constants.LOG_LEVEL_INFO);
this.clearSCORMError(returnValue);
_self.apiLog(callbackName, CMIElement, ': returned: ' + returnValue,
api_constants.LOG_LEVEL_INFO);
_self.clearSCORMError(returnValue);
return returnValue;
}
/**
* @param callbackName
* @param checkTerminated
* @param CMIElement
* @param value
* @returns {string}
* Sets the value of the CMIElement.
*
* @param {string} callbackName
* @param {boolean} checkTerminated
* @param {string} CMIElement
* @param {any} value
* @return {string}
*/
APISetValue(callbackName: String, checkTerminated: boolean, CMIElement, value) {
let returnValue = "";
setValue(
callbackName: String,
checkTerminated: boolean,
CMIElement,
value) {
let returnValue = '';
if (this.checkState(checkTerminated, this.#error_codes.STORE_BEFORE_INIT, this.#error_codes.STORE_AFTER_TERM)) {
if (checkTerminated) this.lastErrorCode = 0;
returnValue = this.setCMIValue(CMIElement, value);
this.processListeners(callbackName, CMIElement, value);
if (_self.checkState(checkTerminated, _self.#error_codes.STORE_BEFORE_INIT,
_self.#error_codes.STORE_AFTER_TERM)) {
if (checkTerminated) _self.lastErrorCode = 0;
returnValue = _self.setCMIValue(CMIElement, value);
_self.processListeners(callbackName, CMIElement, value);
}
this.apiLog(callbackName, CMIElement, ": " + value + ": result: " + returnValue, api_constants.LOG_LEVEL_INFO);
this.clearSCORMError(returnValue);
_self.apiLog(callbackName, CMIElement,
': ' + value + ': result: ' + returnValue,
api_constants.LOG_LEVEL_INFO);
_self.clearSCORMError(returnValue);
return returnValue;
}
/**
* Orders LMS to store all content parameters
*
* @returns {string} bool
* @param {string} callbackName
* @param {boolean} checkTerminated
* @return {string}
*/
APICommit(callbackName: String, checkTerminated: boolean) {
commit(
callbackName: String,
checkTerminated: boolean) {
let returnValue = api_constants.SCORM_FALSE;
if (this.checkState(checkTerminated, this.#error_codes.COMMIT_BEFORE_INIT, this.#error_codes.COMMIT_AFTER_TERM)) {
if (checkTerminated) this.lastErrorCode = 0;
if (_self.checkState(checkTerminated, _self.#error_codes.COMMIT_BEFORE_INIT,
_self.#error_codes.COMMIT_AFTER_TERM)) {
if (checkTerminated) _self.lastErrorCode = 0;
returnValue = api_constants.SCORM_TRUE;
this.processListeners(callbackName);
_self.processListeners(callbackName);
}
this.apiLog(callbackName, null, "returned: " + returnValue, api_constants.LOG_LEVEL_INFO);
this.clearSCORMError(returnValue);
_self.apiLog(callbackName, null, 'returned: ' + returnValue,
api_constants.LOG_LEVEL_INFO);
_self.clearSCORMError(returnValue);
return returnValue;
}
/**
* Returns last error code
*
* @returns {string}
* @param {string} callbackName
* @return {string}
*/
APIGetLastError(callbackName: String) {
let returnValue = String(this.lastErrorCode);
getLastError(callbackName: String) {
const returnValue = String(_self.lastErrorCode);
this.processListeners(callbackName);
_self.processListeners(callbackName);
this.apiLog(callbackName, null, "returned: " + returnValue, api_constants.LOG_LEVEL_INFO);
_self.apiLog(callbackName, null, 'returned: ' + returnValue,
api_constants.LOG_LEVEL_INFO);
return returnValue;
}
@@ -152,19 +203,20 @@ export default class BaseAPI {
/**
* Returns the errorNumber error description
*
* @param callbackName
* @param CMIErrorCode
* @returns {string}
* @param {string} callbackName
* @param {number} CMIErrorCode
* @return {string}
*/
APIGetErrorString(callbackName: String, CMIErrorCode) {
let returnValue = "";
getErrorString(callbackName: String, CMIErrorCode) {
let returnValue = '';
if (CMIErrorCode !== null && CMIErrorCode !== "") {
returnValue = this.getLmsErrorMessageDetails(CMIErrorCode);
this.processListeners(callbackName);
if (CMIErrorCode !== null && CMIErrorCode !== '') {
returnValue = _self.getLmsErrorMessageDetails(CMIErrorCode);
_self.processListeners(callbackName);
}
this.apiLog(callbackName, null, "returned: " + returnValue, api_constants.LOG_LEVEL_INFO);
_self.apiLog(callbackName, null, 'returned: ' + returnValue,
api_constants.LOG_LEVEL_INFO);
return returnValue;
}
@@ -174,17 +226,18 @@ export default class BaseAPI {
*
* @param callbackName
* @param CMIErrorCode
* @returns {string}
* @return {string}
*/
APIGetDiagnostic(callbackName: String, CMIErrorCode) {
let returnValue = "";
getDiagnostic(callbackName: String, CMIErrorCode) {
let returnValue = '';
if (CMIErrorCode !== null && CMIErrorCode !== "") {
returnValue = this.getLmsErrorMessageDetails(CMIErrorCode, true);
this.processListeners(callbackName);
if (CMIErrorCode !== null && CMIErrorCode !== '') {
returnValue = _self.getLmsErrorMessageDetails(CMIErrorCode, true);
_self.processListeners(callbackName);
}
this.apiLog(callbackName, null, "returned: " + returnValue, api_constants.LOG_LEVEL_INFO);
_self.apiLog(callbackName, null, 'returned: ' + returnValue,
api_constants.LOG_LEVEL_INFO);
return returnValue;
}
@@ -192,12 +245,14 @@ export default class BaseAPI {
/**
* Checks the LMS state and ensures it has been initialized
*/
checkState(checkTerminated: boolean, beforeInitError: number, afterTermError?: number) {
if (this.isNotInitialized()) {
this.throwSCORMError(beforeInitError);
checkState(
checkTerminated: boolean, beforeInitError: number,
afterTermError?: number) {
if (_self.isNotInitialized()) {
_self.throwSCORMError(beforeInitError);
return false;
} else if (checkTerminated && this.isTerminated()) {
this.throwSCORMError(afterTermError);
} else if (checkTerminated && _self.isTerminated()) {
_self.throwSCORMError(afterTermError);
return false;
}
@@ -212,10 +267,12 @@ export default class BaseAPI {
* @param logMessage
* @param messageLevel
*/
apiLog(functionName: String, CMIElement: String, logMessage: String, messageLevel: number) {
logMessage = this.formatMessage(functionName, CMIElement, logMessage);
apiLog(
functionName: String, CMIElement: String, logMessage: String,
messageLevel: number) {
logMessage = _self.formatMessage(functionName, CMIElement, logMessage);
if (messageLevel >= this.apiLogLevel) {
if (messageLevel >= _self.apiLogLevel) {
switch (messageLevel) {
case api_constants.LOG_LEVEL_ERROR:
console.error(logMessage);
@@ -228,16 +285,16 @@ export default class BaseAPI {
break;
}
}
};
}
/**
* Clears the last SCORM error code on success
*/
clearSCORMError(success: String) {
if (success !== api_constants.SCORM_FALSE) {
this.lastErrorCode = 0;
_self.lastErrorCode = 0;
}
}
};
/**
* Formats the SCORM messages for easy reading
@@ -245,31 +302,31 @@ export default class BaseAPI {
* @param functionName
* @param CMIElement
* @param message
* @returns {string}
* @return {string}
*/
formatMessage(functionName: String, CMIElement: String, message: String) {
let baseLength = 20;
let messageString = "";
const baseLength = 20;
let messageString = '';
messageString += functionName;
let fillChars = baseLength - messageString.length;
for (let i = 0; i < fillChars; i++) {
messageString += " ";
messageString += ' ';
}
messageString += ": ";
messageString += ': ';
if (CMIElement) {
let CMIElementBaseLength = 70;
const CMIElementBaseLength = 70;
messageString += CMIElement;
fillChars = CMIElementBaseLength - messageString.length;
for (let j = 0; j < fillChars; j++) {
messageString += " ";
messageString += ' ';
}
}
@@ -278,7 +335,7 @@ export default class BaseAPI {
}
return messageString;
};
}
/**
* Checks to see if {str} contains {tester}
@@ -288,14 +345,14 @@ export default class BaseAPI {
*/
stringContains(str: String, tester: String) {
return str.indexOf(tester) > -1;
};
}
/**
* Returns the message that corresponds to errorNumber
* APIs that inherit BaseAPI should override this function
*/
getLmsErrorMessageDetails(_errorNumber, _detail) {
return "No error";
return 'No error';
}
/**
@@ -303,7 +360,7 @@ export default class BaseAPI {
* APIs that inherit BaseAPI should override this function
*/
getCMIValue(_CMIElement) {
return "";
return '';
}
/**
@@ -311,35 +368,39 @@ export default class BaseAPI {
* APIs that inherit BaseAPI should override this function
*/
setCMIValue(_CMIElement, _value) {
return "";
return '';
}
_commonSetCMIValue(methodName: String, scorm2004: boolean, CMIElement, value) {
if (!CMIElement || CMIElement === "") {
_commonSetCMIValue(
methodName: String, scorm2004: boolean, CMIElement, value) {
if (!CMIElement || CMIElement === '') {
return api_constants.SCORM_FALSE;
}
let structure = CMIElement.split(".");
const structure = CMIElement.split('.');
let refObject = this;
let returnValue = api_constants.SCORM_FALSE;
const invalidErrorMessage = `The data model element passed to ${methodName} (${CMIElement}) is not a valid SCORM data model element.`;
const invalidErrorCode = scorm2004 ? this.#error_codes.UNDEFINED_DATA_MODEL: this.#error_codes.GENERAL;
const invalidErrorCode = scorm2004 ?
_self.#error_codes.UNDEFINED_DATA_MODEL :
_self.#error_codes.GENERAL;
for (let i = 0; i < structure.length; i++) {
let attribute = structure[i];
const attribute = structure[i];
if (i === structure.length - 1) {
if (scorm2004 && (attribute.substr(0, 8) === "{target=") && (typeof refObject._isTargetValid == "function")) {
this.throwSCORMError(this.#error_codes.READ_ONLY_ELEMENT);
if (scorm2004 && (attribute.substr(0, 8) === '{target=') &&
(typeof refObject._isTargetValid == 'function')) {
_self.throwSCORMError(_self.#error_codes.READ_ONLY_ELEMENT);
} else if (!refObject.hasOwnProperty(attribute)) {
this.throwSCORMError(invalidErrorCode, invalidErrorMessage);
_self.throwSCORMError(invalidErrorCode, invalidErrorMessage);
} else {
if (this.stringContains(CMIElement, ".correct_responses")){
this.validateCorrectResponse(CMIElement, value)
if (_self.stringContains(CMIElement, '.correct_responses')) {
_self.validateCorrectResponse(CMIElement, value);
}
if (!scorm2004 || this.lastErrorCode === 0) {
if (!scorm2004 || _self.lastErrorCode === 0) {
refObject[attribute] = value;
returnValue = api_constants.SCORM_TRUE;
}
@@ -347,24 +408,24 @@ export default class BaseAPI {
} else {
refObject = refObject[attribute];
if (!refObject) {
this.throwSCORMError(invalidErrorCode, invalidErrorMessage);
_self.throwSCORMError(invalidErrorCode, invalidErrorMessage);
break;
}
if (refObject.prototype === CMIArray) {
let index = parseInt(structure[i + 1], 10);
const index = parseInt(structure[i + 1], 10);
// SCO is trying to set an item on an array
if (!isNaN(index)) {
let item = refObject.childArray[index];
const item = refObject.childArray[index];
if (item) {
refObject = item;
} else {
let newChild = this.getChildElement(CMIElement, value);
const newChild = _self.getChildElement(CMIElement, value);
if (!newChild) {
this.throwSCORMError(invalidErrorCode, invalidErrorMessage);
_self.throwSCORMError(invalidErrorCode, invalidErrorMessage);
} else {
refObject.childArray.push(newChild);
refObject = newChild;
@@ -379,7 +440,9 @@ export default class BaseAPI {
}
if (returnValue === api_constants.SCORM_FALSE) {
this.apiLog(methodName, null, `There was an error setting the value for: ${CMIElement}, value of: ${value}`, api_constants.LOG_LEVEL_WARNING);
_self.apiLog(methodName, null,
`There was an error setting the value for: ${CMIElement}, value of: ${value}`,
api_constants.LOG_LEVEL_WARNING);
}
return returnValue;
@@ -394,7 +457,7 @@ export default class BaseAPI {
* APIs that inherit BaseAPI should override this method
*/
getChildElement(_CMIElement) {
return null
return null;
}
/**
@@ -403,14 +466,14 @@ export default class BaseAPI {
* @param methodName
* @param scorm2004
* @param CMIElement
* @returns {*}
* @return {*}
*/
_commonGetCMIValue(methodName: String, scorm2004: boolean, CMIElement) {
if (!CMIElement || CMIElement === "") {
return "";
if (!CMIElement || CMIElement === '') {
return '';
}
let structure = CMIElement.split(".");
const structure = CMIElement.split('.');
let refObject = this;
let attribute = null;
@@ -420,16 +483,21 @@ export default class BaseAPI {
if (!scorm2004) {
if (i === structure.length - 1) {
if (!refObject.hasOwnProperty(attribute)) {
this.throwSCORMError(101, "getCMIValue did not find a value for: " + CMIElement);
_self.throwSCORMError(101,
'getCMIValue did not find a value for: ' + CMIElement);
}
}
} else {
if ((String(attribute).substr(0, 8) === "{target=") && (typeof refObject._isTargetValid == "function")) {
let target = String(attribute).substr(8, String(attribute).length - 9);
if ((String(attribute).substr(0, 8) === '{target=') &&
(typeof refObject._isTargetValid == 'function')) {
const target = String(attribute).
substr(8, String(attribute).length - 9);
return refObject._isTargetValid(target);
} else if (!refObject.hasOwnProperty(attribute)) {
this.throwSCORMError(401, "The data model element passed to GetValue (" + CMIElement + ") is not a valid SCORM data model element.");
return "";
_self.throwSCORMError(401,
'The data model element passed to GetValue (' + CMIElement +
') is not a valid SCORM data model element.');
return '';
}
}
@@ -438,13 +506,13 @@ export default class BaseAPI {
if (refObject === null || refObject === undefined) {
if (!scorm2004) {
if (attribute === "_children") {
this.throwSCORMError(202);
} else if (attribute === "_count") {
this.throwSCORMError(203);
if (attribute === '_children') {
_self.throwSCORMError(202);
} else if (attribute === '_count') {
_self.throwSCORMError(203);
}
}
return "";
return '';
} else {
return refObject;
}
@@ -454,21 +522,21 @@ export default class BaseAPI {
* Returns true if the API's current state is STATE_INITIALIZED
*/
isInitialized() {
return this.currentState === api_constants.STATE_INITIALIZED;
return _self.currentState === api_constants.STATE_INITIALIZED;
}
/**
* Returns true if the API's current state is STATE_NOT_INITIALIZED
*/
isNotInitialized() {
return this.currentState === api_constants.STATE_NOT_INITIALIZED;
return _self.currentState === api_constants.STATE_NOT_INITIALIZED;
}
/**
* Returns true if the API's current state is STATE_TERMINATED
*/
isTerminated() {
return this.currentState === api_constants.STATE_TERMINATED;
return _self.currentState === api_constants.STATE_TERMINATED;
}
/**
@@ -480,25 +548,25 @@ export default class BaseAPI {
on(listenerName: String, callback: function) {
if (!callback) return;
let listenerFunctions = listenerName.split(" ");
const listenerFunctions = listenerName.split(' ');
for (let i = 0; i < listenerFunctions.length; i++) {
let listenerSplit = listenerFunctions[i].split(".");
const listenerSplit = listenerFunctions[i].split('.');
if (listenerSplit.length === 0) return;
let functionName = listenerSplit[0];
const functionName = listenerSplit[0];
let CMIElement = null;
if (listenerSplit.length > 1) {
CMIElement = listenerName.replace(functionName + ".", "");
CMIElement = listenerName.replace(functionName + '.', '');
}
this.listenerArray.push({
_self.listenerArray.push({
functionName: functionName,
CMIElement: CMIElement,
callback: callback
callback: callback,
});
}
};
}
/**
* Processes any 'on' listeners that have been created
@@ -508,17 +576,17 @@ export default class BaseAPI {
* @param value
*/
processListeners(functionName: String, CMIElement: String, value: any) {
for (let i = 0; i < this.listenerArray.length; i++) {
let listener = this.listenerArray[i];
let functionsMatch = listener.functionName === functionName;
let listenerHasCMIElement = !!listener.CMIElement;
let CMIElementsMatch = listener.CMIElement === CMIElement;
for (let i = 0; i < _self.listenerArray.length; i++) {
const listener = _self.listenerArray[i];
const functionsMatch = listener.functionName === functionName;
const listenerHasCMIElement = !!listener.CMIElement;
const CMIElementsMatch = listener.CMIElement === CMIElement;
if (functionsMatch && (!listenerHasCMIElement || CMIElementsMatch)) {
listener.callback(CMIElement, value);
}
}
};
}
/**
* Throws a SCORM error
@@ -528,45 +596,48 @@ export default class BaseAPI {
*/
throwSCORMError(errorNumber: number, message: String) {
if (!message) {
message = this.getLmsErrorMessageDetails(errorNumber);
message = _self.getLmsErrorMessageDetails(errorNumber);
}
this.apiLog("throwSCORMError", null, errorNumber + ": " + message, api_constants.LOG_LEVEL_ERROR);
_self.apiLog('throwSCORMError', null, errorNumber + ': ' + message,
api_constants.LOG_LEVEL_ERROR);
this.lastErrorCode = String(errorNumber);
_self.lastErrorCode = String(errorNumber);
}
/**
* Loads CMI data from a JSON object.
*/
loadFromJSON(json, CMIElement) {
if (!this.isNotInitialized()) {
console.error("loadFromJSON can only be called before the call to LMSInitialize.");
if (!_self.isNotInitialized()) {
console.error(
'loadFromJSON can only be called before the call to LMSInitialize.');
return;
}
CMIElement = CMIElement || "cmi";
CMIElement = CMIElement || 'cmi';
for (let key in json) {
for (const key in json) {
if (json.hasOwnProperty(key) && json[key]) {
let currentCMIElement = CMIElement + "." + key;
let value = json[key];
const currentCMIElement = CMIElement + '.' + key;
const value = json[key];
if (value["childArray"]) {
for (let i = 0; i < value["childArray"].length; i++) {
this.loadFromJSON(value["childArray"][i], currentCMIElement + "." + i);
if (value['childArray']) {
for (let i = 0; i < value['childArray'].length; i++) {
_self.loadFromJSON(value['childArray'][i],
currentCMIElement + '.' + i);
}
} else if (value.constructor === Object) {
this.loadFromJSON(value, currentCMIElement);
_self.loadFromJSON(value, currentCMIElement);
} else {
this.setCMIValue(currentCMIElement, value);
_self.setCMIValue(currentCMIElement, value);
}
}
}
}
renderCMIToJSON() {
let cmi = this.cmi;
const cmi = _self.cmi;
// Do we want/need to return fields that have no set value?
// return JSON.stringify({ cmi }, (k, v) => v === undefined ? null : v, 2);
return JSON.stringify({cmi});
@@ -577,39 +648,37 @@ export default class BaseAPI {
*
* @param value
* @param regexPattern
* @returns {boolean}
* @return {boolean}
*/
checkValidFormat(value: String, regexPattern: String) {
const formatRegex = new RegExp(regexPattern);
if (!value || !value.match(formatRegex)) {
this.throwSCORMError(this.#error_codes.TYPE_MISMATCH);
_self.throwSCORMError(_self.#error_codes.TYPE_MISMATCH);
return false;
}
return true;
}
/**
* Check if the value matches the proper range. If not, throw proper error code.
*
* @param value
* @param rangePattern
* @returns {boolean}
* @return {boolean}
*/
checkValidRange(value: any, rangePattern: String) {
const ranges = rangePattern.split('#');
value = value * 1.0;
if (value >= ranges[0]) {
if ((ranges[1] === '*') || (value <= ranges[1])) {
this.clearSCORMError(api_constants.SCORM_TRUE);
_self.clearSCORMError(api_constants.SCORM_TRUE);
return true;
} else {
this.throwSCORMError(this.#error_codes.VALUE_OUT_OF_RANGE);
_self.throwSCORMError(_self.#error_codes.VALUE_OUT_OF_RANGE);
return false;
}
} else {
this.throwSCORMError(this.#error_codes.VALUE_OUT_OF_RANGE);
_self.throwSCORMError(_self.#error_codes.VALUE_OUT_OF_RANGE);
return false;
}
}
@@ -620,16 +689,16 @@ export default class BaseAPI {
* @param when the number of milliseconds to wait before committing
*/
scheduleCommit(when: number) {
this.#timeout = new ScheduledCommit(this, when);
_self.#timeout = new ScheduledCommit(this, when);
}
/**
* Clears and cancels any currently scheduled commits
*/
clearScheduledCommit() {
if (this.#timeout) {
this.#timeout.cancel();
this.#timeout = null;
if (_self.#timeout) {
_self.#timeout.cancel();
_self.#timeout = null;
}
}
}
@@ -640,20 +709,20 @@ class ScheduledCommit {
#timeout;
constructor(API: any, when: number) {
this.#API = API;
this.#timeout = setTimeout(this.#wrapper, when);
_self.#API = API;
_self.#timeout = setTimeout(_self.#wrapper, when);
}
cancel() {
this.#cancelled = true;
if (this.#timeout) {
clearTimeout(this.#timeout);
_self.#cancelled = true;
if (_self.#timeout) {
clearTimeout(_self.#timeout);
}
}
#wrapper() {
if (!this.#cancelled) {
this.#API.LMSCommit();
}
#wrapper = () => {
if (!_self.#cancelled) {
_self.#API.LMSCommit();
}
};
}

View File

@@ -5,11 +5,11 @@ import {
CMIInteractionsCorrectResponsesObject,
CMIInteractionsObject,
CMIInteractionsObjectivesObject,
CMIObjectivesObject
} from "./cmi/scorm12_cmi";
CMIObjectivesObject,
} from './cmi/scorm12_cmi';
import * as Utilities from './utilities';
import {scorm12_constants, scorm12_error_codes} from "./constants";
import {scorm12_regex} from "./regex";
import {scorm12_constants, scorm12_error_codes} from './constants';
import {scorm12_regex} from './regex';
const constants = scorm12_constants;
@@ -21,72 +21,73 @@ export default class Scorm12API extends BaseAPI {
}
/**
* @returns {string} bool
* @return {string} bool
*/
LMSInitialize() {
return this.APIInitialize("LMSInitialize", "LMS was already initialized!", "LMS is already finished!");
return this.initialize('LMSInitialize', 'LMS was already initialized!',
'LMS is already finished!');
}
/**
* @returns {string} bool
* @return {string} bool
*/
LMSFinish() {
return this.APITerminate("LMSFinish", false);
return this.terminate('LMSFinish', false);
}
/**
* @param CMIElement
* @returns {string}
* @return {string}
*/
LMSGetValue(CMIElement) {
return this.APIGetValue("LMSGetValue", false, CMIElement);
return this.getValue('LMSGetValue', false, CMIElement);
}
/**
* @param CMIElement
* @param value
* @returns {string}
* @return {string}
*/
LMSSetValue(CMIElement, value) {
return this.APISetValue("LMSSetValue", false, CMIElement, value);
return this.setValue('LMSSetValue', false, CMIElement, value);
}
/**
* Orders LMS to store all content parameters
*
* @returns {string} bool
* @return {string} bool
*/
LMSCommit() {
return this.APICommit("LMSCommit", false);
return this.commit('LMSCommit', false);
}
/**
* Returns last error code
*
* @returns {string}
* @return {string}
*/
LMSGetLastError() {
return this.APIGetLastError("LMSGetLastError");
return this.getLastError('LMSGetLastError');
}
/**
* Returns the errorNumber error description
*
* @param CMIErrorCode
* @returns {string}
* @return {string}
*/
LMSGetErrorString(CMIErrorCode) {
return this.APIGetErrorString("LMSGetErrorString", CMIErrorCode);
return this.getErrorString('LMSGetErrorString', CMIErrorCode);
}
/**
* Returns a comprehensive description of the errorNumber error.
*
* @param CMIErrorCode
* @returns {string}
* @return {string}
*/
LMSGetDiagnostic(CMIErrorCode) {
return this.APIGetDiagnostic("LMSGetDiagnostic", CMIErrorCode);
return this.getDiagnostic('LMSGetDiagnostic', CMIErrorCode);
}
/**
@@ -94,20 +95,20 @@ export default class Scorm12API extends BaseAPI {
*
* @param CMIElement
* @param value
* @returns {string}
* @return {string}
*/
setCMIValue(CMIElement, value) {
this._commonSetCMIValue("LMSSetValue", false, CMIElement, value);
this._commonSetCMIValue('LMSSetValue', false, CMIElement, value);
}
/**
* Gets a value from the CMI Object
*
* @param CMIElement
* @returns {*}
* @return {*}
*/
getCMIValue(CMIElement) {
return this._commonGetCMIValue("getCMIValue", false, CMIElement);
return this._commonGetCMIValue('getCMIValue', false, CMIElement);
}
/**
@@ -118,13 +119,13 @@ export default class Scorm12API extends BaseAPI {
getChildElement(CMIElement, value) {
let newChild;
if (this.stringContains(CMIElement, "cmi.objectives")) {
if (this.stringContains(CMIElement, 'cmi.objectives')) {
newChild = new CMIObjectivesObject(this);
} else if (this.stringContains(CMIElement, ".correct_responses")) {
} else if (this.stringContains(CMIElement, '.correct_responses')) {
newChild = new CMIInteractionsCorrectResponsesObject(this);
} else if (this.stringContains(CMIElement, ".objectives")) {
} else if (this.stringContains(CMIElement, '.objectives')) {
newChild = new CMIInteractionsObjectivesObject(this);
} else if (this.stringContains(CMIElement, "cmi.interactions")) {
} else if (this.stringContains(CMIElement, 'cmi.interactions')) {
newChild = new CMIInteractionsObject(this);
}
@@ -139,8 +140,8 @@ export default class Scorm12API extends BaseAPI {
* Returns the message that corresponds to errorNumber.
*/
getLmsErrorMessageDetails(errorNumber, detail) {
let basicMessage = "No Error";
let detailMessage = "No Error";
let basicMessage = 'No Error';
let detailMessage = 'No Error';
// Set error number to string since inconsistent from modules if string or number
errorNumber = String(errorNumber);

View File

@@ -8,140 +8,153 @@ import {
CMIInteractionsCorrectResponsesObject,
CMIInteractionsObject,
CMIInteractionsObjectivesObject,
CMIObjectivesObject
CMIObjectivesObject,
} from './cmi/scorm2004_cmi';
import * as Utilities from "./utilities";
import {correct_responses, scorm2004_constants, scorm2004_error_codes} from "./constants";
import {scorm2004_regex} from "./regex";
import * as Util from './utilities';
import {
correct_responses,
scorm2004_constants,
scorm2004_error_codes,
} from './constants';
import {scorm2004_regex} from './regex';
const constants = scorm2004_constants;
const valid_languages = constants.valid_languages;
let _self;
/**
* API class for SCORM 2004
*/
class Scorm2004API extends BaseAPI {
version: "1.0";
version: '1.0';
/**
* Constructor for SCORM 2004 API
*/
constructor() {
super(scorm2004_error_codes);
this.cmi = new CMI(this);
this.adl = new ADL(this);
_self = this;
_self.cmi = new CMI(_self);
_self.adl = new ADL(_self);
// Rename functions to match 2004 Spec
this.Initialize = this.LMSInitialize;
this.Terminate = this.LMSTerminate;
this.GetValue = this.LMSGetValue;
this.SetValue = this.LMSSetValue;
this.Commit = this.LMSCommit;
this.GetLastError = this.LMSGetLastError;
this.GetErrorString = this.LMSGetErrorString;
this.GetDiagnostic = this.LMSGetDiagnostic;
_self.Initialize = _self.LMSInitialize;
_self.Terminate = _self.LMSTerminate;
_self.GetValue = _self.LMSGetValue;
_self.SetValue = _self.LMSSetValue;
_self.Commit = _self.LMSCommit;
_self.GetLastError = _self.LMSGetLastError;
_self.GetErrorString = _self.LMSGetErrorString;
_self.GetDiagnostic = _self.LMSGetDiagnostic;
}
/**
* @returns {string} bool
* @return {string} bool
*/
LMSInitialize() {
return this.APIInitialize("Initialize");
return _self.initialize('Initialize');
}
/**
* @returns {string} bool
* @return {string} bool
*/
LMSTerminate() {
return this.APITerminate("Terminate", true);
return _self.terminate('Terminate', true);
}
/**
* @param CMIElement
* @returns {string}
* @param {string} CMIElement
* @return {string}
*/
LMSGetValue(CMIElement) {
return this.APIGetValue("GetValue", true, CMIElement);
return _self.getValue('GetValue', true, CMIElement);
}
/**
* @param CMIElement
* @param value
* @returns {string}
* @param {string} CMIElement
* @param {any} value
* @return {string}
*/
LMSSetValue(CMIElement, value) {
return this.APISetValue("SetValue", true, CMIElement, value);
return _self.setValue('SetValue', true, CMIElement, value);
}
/**
* Orders LMS to store all content parameters
*
* @returns {string} bool
* @return {string} bool
*/
LMSCommit() {
return this.APICommit("Commit");
return _self.commit('Commit');
}
/**
* Returns last error code
*
* @returns {string}
* @return {string}
*/
LMSGetLastError() {
return this.APIGetLastError("GetLastError");
return _self.getLastError('GetLastError');
}
/**
* Returns the errorNumber error description
*
* @param CMIErrorCode
* @returns {string}
* @return {string}
*/
LMSGetErrorString(CMIErrorCode) {
return this.APIGetErrorString("GetErrorString", CMIErrorCode);
return _self.APIGetErrorString('GetErrorString', CMIErrorCode);
}
/**
* Returns a comprehensive description of the errorNumber error.
*
* @param CMIErrorCode
* @returns {string}
* @return {string}
*/
LMSGetDiagnostic(CMIErrorCode) {
return this.APIGetDiagnostic("GetDiagnostic", CMIErrorCode);
return _self.APIGetDiagnostic('GetDiagnostic', CMIErrorCode);
}
/**
* Sets a value on the CMI Object
*
* @param CMIElement
* @param value
* @returns {string}
* @param {string} CMIElement
* @param {any} value
*/
setCMIValue(CMIElement, value) {
this._commonSetCMIValue("SetValue", true, CMIElement, value);
_self._commonSetCMIValue('SetValue', true, CMIElement, value);
}
/**
* Gets or builds a new child element to add to the array.
*
* @param CMIElement
* @param value
* @param {string} CMIElement
* @param {any} value
* @return {any}
*/
getChildElement(CMIElement, value) {
let newChild;
if (this.stringContains(CMIElement, "cmi.objectives")) {
if (_self.stringContains(CMIElement, 'cmi.objectives')) {
newChild = new CMIObjectivesObject(this);
} else if (this.stringContains(CMIElement, ".correct_responses")) {
} else if (_self.stringContains(CMIElement, '.correct_responses')) {
const parts = CMIElement.split('.');
const index = Number(parts[2]);
let interaction = this.cmi.interactions.childArray[index];
const interaction = _self.cmi.interactions.childArray[index];
if (typeof interaction.type === 'undefined') {
this.throwSCORMError(scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
_self.throwSCORMError(scorm2004_error_codes.DEPENDENCY_NOT_ESTABLISHED);
} else {
let interaction_type = interaction.type;
let interaction_count = interaction.correct_responses._count;
const interaction_type = interaction.type;
const interaction_count = interaction.correct_responses._count;
if (interaction_type === 'choice') {
for(let i = 0; i < interaction_count && this.lastErrorCode === 0; i++) {
for (let i = 0; i < interaction_count && _self.lastErrorCode ===
0; i++) {
const response = interaction.correct_responses.childArray[i];
if (response.pattern === value) {
this.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE);
_self.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE);
}
}
}
@@ -155,46 +168,53 @@ class Scorm2004API extends BaseAPI {
}
if (nodes.length > 0 && nodes.length <= response_type.max) {
this.#checkCorrectResponseValue(interaction_type, nodes, value);
_self.#checkCorrectResponseValue(interaction_type, nodes, value);
} else if (nodes.length > response_type.max) {
this.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE, "Data Model Element Pattern Too Long");
_self.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE,
'Data Model Element Pattern Too Long');
}
}
if(this.lastErrorCode === 0) {
if (_self.lastErrorCode === 0) {
newChild = new CMIInteractionsCorrectResponsesObject(this);
}
} else if (this.stringContains(CMIElement, ".objectives")) {
} else if (_self.stringContains(CMIElement, '.objectives')) {
newChild = new CMIInteractionsObjectivesObject(this);
} else if (this.stringContains(CMIElement, "cmi.interactions")) {
} else if (_self.stringContains(CMIElement, 'cmi.interactions')) {
newChild = new CMIInteractionsObject(this);
} else if (this.stringContains(CMIElement, "cmi.comments_from_learner")) {
} else if (_self.stringContains(CMIElement, 'cmi.comments_from_learner')) {
newChild = new CMICommentsFromLearnerObject(this);
} else if (this.stringContains(CMIElement, "cmi.comments_from_lms")) {
} else if (_self.stringContains(CMIElement, 'cmi.comments_from_lms')) {
newChild = new CMICommentsFromLMSObject(this);
}
return newChild;
}
/**
* Validate correct response.
* @param {string} CMIElement
* @param {any} value
*/
validateCorrectResponse(CMIElement, value) {
const parts = CMIElement.split('.');
const index = Number(parts[2]);
const pattern_index = Number(parts[4]);
let interaction = this.cmi.interactions.childArray[index];
const interaction = _self.cmi.interactions.childArray[index];
let interaction_type = interaction.type;
let interaction_count = interaction.correct_responses._count;
const interaction_type = interaction.type;
const interaction_count = interaction.correct_responses._count;
if (interaction_type === 'choice') {
for(let i = 0; i < interaction_count && this.lastErrorCode === 0; i++) {
for (let i = 0; i < interaction_count && _self.lastErrorCode === 0; i++) {
const response = interaction.correct_responses.childArray[i];
if (response.pattern === value) {
this.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE);
_self.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE);
}
}
}
const response_type = scorm2004_constants.correct_responses[interaction_type];
if(typeof response_type.limit !== 'undefined' || interaction_count < response_type.limit) {
if (typeof response_type.limit !== 'undefined' || interaction_count <
response_type.limit) {
let nodes = [];
if (response_type.delimiter !== '') {
nodes = value.split(response_type.delimiter);
@@ -203,41 +223,50 @@ class Scorm2004API extends BaseAPI {
}
if (nodes.length > 0 && nodes.length <= response_type.max) {
this.#checkCorrectResponseValue(interaction_type, nodes, value);
_self.#checkCorrectResponseValue(interaction_type, nodes, value);
} else if (nodes.length > response_type.max) {
this.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE, "Data Model Element Pattern Too Long");
_self.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE,
'Data Model Element Pattern Too Long');
}
if(this.lastErrorCode === 0
&& (!response_type.duplicate || !this.#checkDuplicatedPattern(interaction.correct_responses, pattern_index, value))
|| (this.lastErrorCode === 0 && value === '')) {
if (_self.lastErrorCode === 0 &&
(!response_type.duplicate ||
!_self.#checkDuplicatedPattern(interaction.correct_responses,
pattern_index, value)) ||
(_self.lastErrorCode === 0 && value === '')) {
// do nothing, we want the inverse
} else {
if(this.lastErrorCode === 0) {
this.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE, "Data Model Element Pattern Already Exists");
if (_self.lastErrorCode === 0) {
_self.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE,
'Data Model Element Pattern Already Exists');
}
}
} else {
this.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE, "Data Model Element Collection Limit Reached");
_self.throwSCORMError(scorm2004_error_codes.GENERAL_SET_FAILURE,
'Data Model Element Collection Limit Reached');
}
}
/**
* Gets a value from the CMI Object
*
* @param CMIElement
* @returns {*}
* @param {string} CMIElement
* @return {*}
*/
getCMIValue(CMIElement) {
return this._commonGetCMIValue("GetValue", true, CMIElement);
return _self._commonGetCMIValue('GetValue', true, CMIElement);
}
/**
* Returns the message that corresponds to errorNumber.
*
* @param {string,number} errorNumber
* @param {string} detail
* @return {string}
*/
getLmsErrorMessageDetails(errorNumber, detail) {
let basicMessage = "";
let detailMessage = "";
let basicMessage = '';
let detailMessage = '';
// Set error number to string since inconsistent from modules if string or number
errorNumber = String(errorNumber);
@@ -249,54 +278,55 @@ class Scorm2004API extends BaseAPI {
return detail ? detailMessage : basicMessage;
}
#checkDuplicatedPattern(correct_response, current_index, value) {
#checkDuplicatedPattern = (correct_response, current_index, value) => {
let found = false;
let count = correct_response._count;
const count = correct_response._count;
for (let i = 0; i < count && !found; i++) {
if (i !== current_index && correct_response.childArray[i] === value) {
found = true;
}
}
return found;
}
};
#checkCorrectResponseValue(interaction_type, nodes, value) {
#checkCorrectResponseValue = (interaction_type, nodes, value) => {
const response = correct_responses[interaction_type];
const formatRegex = new RegExp(response.format);
for(let i = 0; i < nodes.length && this.lastErrorCode === 0; i++) {
if(interaction_type.match('^(fill-in|long-fill-in|matching|performance|sequencing)$')) {
nodes[i] = #removeCorrectResponsePrefixes(nodes[i]);
for (let i = 0; i < nodes.length && _self.lastErrorCode === 0; i++) {
if (interaction_type.match(
'^(fill-in|long-fill-in|matching|performance|sequencing)$')) {
nodes[i] = _self.#removeCorrectResponsePrefixes(nodes[i]);
}
if (response.delimiter2 !== undefined) {
let values = nodes[i].split(response.delimiter2);
const values = nodes[i].split(response.delimiter2);
if (values.length === 2) {
let matches = values[0].match(formatRegex);
const matches = values[0].match(formatRegex);
if (!matches) {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
_self.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
} else {
if (!values[1].match(new RegExp(response.format2))) {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
_self.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
}
} else {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
_self.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
} else {
let matches = nodes[i].match(formatRegex);
if((!matches && value !== '') || (!matches && interaction_type === 'true-false')) {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
const matches = nodes[i].match(formatRegex);
if ((!matches && value !== '') ||
(!matches && interaction_type === 'true-false')) {
_self.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
} else {
if (interaction_type === 'numeric' && nodes.length > 1) {
if (Number(nodes[0]) > Number(nodes[1])) {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
_self.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
} else {
if (nodes[i] !== '' && response.unique) {
for(let j = 0; j < i && this.lastErrorCode === 0; j++) {
for (let j = 0; j < i && _self.lastErrorCode === 0; j++) {
if (nodes[i] === nodes[j]) {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
_self.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
}
}
@@ -304,23 +334,26 @@ class Scorm2004API extends BaseAPI {
}
}
}
};
#removeCorrectResponsePrefixes(node) {
#removeCorrectResponsePrefixes = (node) => {
let seenOrder = false;
let seenCase = false;
let seenLang = false;
let prefixRegex = '^(\{(lang|case_matters|order_matters)=([^\}]+)\})';
const prefixRegex = new RegExp(
'^({(lang|case_matters|order_matters)=([^}]+)})');
let matches = node.match(prefixRegex);
let langMatches = null;
while (matches) {
switch (matches[2]) {
case 'lang':
let langMatches = node.match(scorm2004_regex.CMILangcr);
langMatches = node.match(scorm2004_regex.CMILangcr);
if (langMatches) {
let lang = langMatches[3];
const lang = langMatches[3];
if (lang !== undefined && lang.length > 0) {
if (valid_languages[lang.toLowerCase()] === undefined) {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
_self.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
}
}
@@ -329,7 +362,7 @@ class Scorm2004API extends BaseAPI {
case 'case_matters':
if (!seenLang && !seenOrder && !seenCase) {
if (matches[3] !== 'true' && matches[3] !== 'false') {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
_self.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
}
@@ -338,7 +371,7 @@ class Scorm2004API extends BaseAPI {
case 'order_matters':
if (!seenCase && !seenLang && !seenOrder) {
if (matches[3] !== 'true' && matches[3] !== 'false') {
this.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
_self.throwSCORMError(scorm2004_error_codes.TYPE_MISMATCH);
}
}
@@ -352,28 +385,32 @@ class Scorm2004API extends BaseAPI {
}
return node;
}
};
/**
* Replace the whole API with another
* @param {Scorm2004API} newAPI
*/
replaceWithAnotherScormAPI(newAPI) {
// Data Model
this.cmi = newAPI.cmi;
this.adl = newAPI.adl;
_self.cmi = newAPI.cmi;
_self.adl = newAPI.adl;
}
/**
* Adds the current session time to the existing total time.
*
* @return {string} ISO8601 Duration
*/
getCurrentTotalTime() {
const totalTime = this.cmi.total_time;
const sessionTime = this.cmi.session_time;
const totalTime = _self.cmi.total_time;
const sessionTime = _self.cmi.session_time;
const durationRegex = scorm2004_regex.CMITimespan;
const totalSeconds = Utilities.getDurationAsSeconds(totalTime, durationRegex);
const sessionSeconds = Utilities.getDurationAsSeconds(sessionTime, durationRegex);
const totalSeconds = Util.getDurationAsSeconds(totalTime, durationRegex);
const sessionSeconds = Util.getDurationAsSeconds(sessionTime,
durationRegex);
return Utilities.getSecondsAsISODuration(totalSeconds + sessionSeconds);
return Util.getSecondsAsISODuration(totalSeconds + sessionSeconds);
}
}

View File

@@ -1,12 +1,27 @@
import * as Scorm12CMI from './scorm12_cmi';
import {BaseCMI, CMIArray, CMIScore} from "./common";
import {aicc_constants} from "../constants";
import {aicc_regex} from "../regex";
import {BaseCMI, CMIArray, CMIScore} from './common';
import {aicc_constants, scorm12_error_codes} from '../constants';
import {aicc_regex} from '../regex';
const constants = aicc_constants;
const regex = aicc_regex;
/**
* Sets a READ_ONLY error on the API
* @param {AICC} API
*/
function throwReadOnlyError(API) {
API.throwSCORMError(scorm12_error_codes.READ_ONLY_ELEMENT);
}
/**
* CMI Class for AICC
*/
export class CMI extends Scorm12CMI.CMI {
/**
* Constructor for AICC CMI object
* @param {AICC} API
*/
constructor(API) {
super(API, constants.cmi_children, new AICCCMIStudentData(API));
@@ -14,84 +29,197 @@ export class CMI extends Scorm12CMI.CMI {
}
}
/**
* AICC Evaluation object
*/
class CMIEvaluation extends BaseCMI {
/**
* Constructor for AICC Evaluation object
* @param {AICC} API
*/
constructor(API) {
super(API);
}
comments = new class extends CMIArray {
/**
* Constructor for AICC Evaluation Comments object
* @param {AICC} API
*/
constructor(API) {
super(API, constants.comments_children, 402);
}
};
}
/**
* StudentData class for AICC
*/
class AICCCMIStudentData extends Scorm12CMI.CMIStudentData {
/**
* Constructor for AICC StudentData object
* @param {AICC} API
*/
constructor(API) {
super(API, constants.student_data_children);
}
#tries_during_lesson = "";
#tries_during_lesson = '';
get tries_during_lesson() { return this.#tries_during_lesson; }
set tries_during_lesson(tries_during_lesson) { this.API.isNotInitialized() ? this.#tries_during_lesson = tries_during_lesson : this.throwReadOnlyError() }
/**
* Getter for tries_during_lesson
* @return {string}
*/
get tries_during_lesson() {
return this.#tries_during_lesson;
}
/**
* Setter for #tries_during_lesson. Sets an error if trying to set after
* API initialization.
* @param {string} tries_during_lesson
*/
set tries_during_lesson(tries_during_lesson) {
this.API.isNotInitialized() ?
this.#tries_during_lesson = tries_during_lesson :
throwReadOnlyError();
}
tries = new class extends CMIArray {
/**
* Constructor for inline Tries Array class
* @param {AICC} API
*/
constructor(API) {
super(API, aicc_constants.tries_children);
}
};
}
let _self;
/**
* Class for AICC Tries
*/
export class CMITriesObject extends BaseCMI {
#API;
/**
* Constructor for AICC Tries object
* @param {AICC} API
*/
constructor(API) {
super(API);
this.#API = API;
_self = this;
}
#status = "";
#time = "";
#status = '';
#time = '';
get status() { return this.#status; }
/**
* Getter for #status
* @return {string}
*/
get status() {
return this.#status;
}
/**
* Setter for #status
* @param {string} status
*/
set status(status) {
if (this.API.checkValidFormat(status, regex.CMIStatus2)) {
this.#status = status;
}
}
get time() { return this.#time; }
/**
* Getter for #time
* @return {string}
*/
get time() {
return this.#time;
}
/**
* Setter for #time
* @param {string} time
*/
set time(time) {
if (this.API.checkValidFormat(time, regex.CMITime)) {
this.#time = time;
}
}
score = new CMIScore(API);
score = new CMIScore(_self.#API);
}
/**
* Class for AICC Evaluation Comments
*/
export class CMIEvaluationCommentsObject extends BaseCMI {
/**
* Constructor for Evaluation Comments
* @param {AICC} API
*/
constructor(API) {
super(API);
}
#content = "";
#location = "";
#time = "";
#content = '';
#location = '';
#time = '';
get content() { return this.#content; }
/**
* Getter for #content
* @return {string}
*/
get content() {
return this.#content;
}
/**
* Setter for #content
* @param {string} content
*/
set content(content) {
if (this.API.checkValidFormat(content, regex.CMIString256)) {
this.#content = content;
}
}
get location() { return this.#location; }
/**
* Getter for #location
* @return {string}
*/
get location() {
return this.#location;
}
/**
* Setter for #location
* @param {string} location
*/
set location(location) {
if (this.API.checkValidFormat(location, regex.CMIString256)) {
this.#location = location;
}
}
get time() { return this.#time; }
/**
* Getter for #time
* @return {string}
*/
get time() {
return this.#time;
}
/**
* Setting for #time
* @param {string} time
*/
set time(time) {
if (this.API.checkValidFormat(time, regex.CMITime)) {
this.#time = time;
@@ -99,14 +227,32 @@ export class CMIEvaluationCommentsObject extends BaseCMI {
}
}
/**
* Class for AICC Navigation object
*/
export class NAV extends BaseCMI {
/**
* Constructor for NAV object
* @param {AICC} API
*/
constructor(API) {
super(API);
}
#event = "";
#event = '';
get event() { return (!this.jsonString) ? this.API.throwSCORMError(404) : this.#event; }
/**
* Getter for #event
* @return {string}
*/
get event() {
return (!this.jsonString) ? this.API.throwSCORMError(404) : this.#event;
}
/**
* Setter for #event
* @param {string} event
*/
set event(event) {
if (this.API.checkValidFormat(event, regex.NAVEvent)) {
this.#event = event;

View File

@@ -1,6 +1,6 @@
import {BaseCMI, CMIArray, CMIScore} from './common';
import {scorm12_constants, scorm12_error_codes} from "../constants";
import {scorm12_regex} from "../regex";
import {scorm12_constants, scorm12_error_codes} from '../constants';
import {scorm12_regex} from '../regex';
const constants = scorm12_constants;
const regex = scorm12_regex;
@@ -18,12 +18,12 @@ function throwInvalidValueError(API) {
}
export class CMI extends BaseCMI {
#_children = "";
#_version = "3.4";
#suspend_data = "";
#launch_data = "";
#comments = "";
#comments_from_lms = "";
#_children = '';
#_version = '3.4';
#suspend_data = '';
#launch_data = '';
#comments = '';
#comments_from_lms = '';
student_data = null;
@@ -33,7 +33,9 @@ export class CMI extends BaseCMI {
this.#_children = cmi_children ? cmi_children : constants.cmi_children;
this.core = new CMICore(API);
this.objectives = new CMIObjectives(API);
this.student_data = student_data ? student_data : new CMIStudentData(API);
this.student_data = student_data ?
student_data :
new CMIStudentData(API);
this.student_preference = new CMIStudentPreference(API);
this.interactions = new CMIInteractions(API);
}
@@ -49,101 +51,187 @@ export class CMI extends BaseCMI {
'objectives': this.objectives,
'student_data': this.student_data,
'student_preference': this.student_preference,
'interactions': this.interactions
'interactions': this.interactions,
};
delete this.jsonString;
return result;
};
get _version() { return this.#_version; }
set _version(_version) { throwInvalidValueError(this.API); }
get _version() {
return this.#_version;
}
get _children() { return this.#_children; }
set _children(_children) { throwInvalidValueError(this.API); }
set _version(_version) {
throwInvalidValueError(this.API);
}
get suspend_data() { return this.#suspend_data; }
get _children() {
return this.#_children;
}
set _children(_children) {
throwInvalidValueError(this.API);
}
get suspend_data() {
return this.#suspend_data;
}
set suspend_data(suspend_data) {
if (this.API.checkValidFormat(suspend_data, regex.CMIString4096)) {
this.#suspend_data = suspend_data;
}
}
get launch_data() { return this.#launch_data; }
set launch_data(launch_data) { this.API.isNotInitialized() ? this.#launch_data = launch_data : throwReadOnlyError(this.API); }
get launch_data() {
return this.#launch_data;
}
get comments() { return this.#comments; }
set launch_data(launch_data) {
this.API.isNotInitialized() ?
this.#launch_data = launch_data :
throwReadOnlyError(this.API);
}
get comments() {
return this.#comments;
}
set comments(comments) {
if (this.API.checkValidFormat(comments, regex.CMIString4096)) {
this.#comments = comments;
}
}
get comments_from_lms() { return this.#comments_from_lms; }
set comments_from_lms(comments_from_lms) { this.API.isNotInitialized() ? this.#comments_from_lms = comments_from_lms : throwReadOnlyError(this.API); }
get comments_from_lms() {
return this.#comments_from_lms;
}
set comments_from_lms(comments_from_lms) {
this.API.isNotInitialized() ?
this.#comments_from_lms = comments_from_lms :
throwReadOnlyError(this.API);
}
}
class CMICore extends BaseCMI {
constructor(API) {
super(API);
this.score = new CMIScore(API, constants.score_children, regex.score_range);
this.score = new CMIScore(API, constants.score_children,
regex.score_range);
}
#_children = constants.core_children;
#student_id = "";
#student_name = "";
#lesson_location = "";
#credit = "";
#lesson_status = "";
#entry = "";
#total_time = "";
#lesson_mode = "normal";
#exit = "";
#session_time = "00:00:00";
#student_id = '';
#student_name = '';
#lesson_location = '';
#credit = '';
#lesson_status = '';
#entry = '';
#total_time = '';
#lesson_mode = 'normal';
#exit = '';
#session_time = '00:00:00';
get _children() { return this.#_children; }
set _children(_children) { throwInvalidValueError(this.API); }
get _children() {
return this.#_children;
}
get student_id() { return this.#student_id; }
set student_id(student_id) { this.API.isNotInitialized() ? this.#student_id = student_id : throwReadOnlyError(this.API); }
set _children(_children) {
throwInvalidValueError(this.API);
}
get student_name() { return this.#student_name; }
set student_name(student_name) { this.API.isNotInitialized() ? this.#student_name = student_name : throwReadOnlyError(this.API); }
get student_id() {
return this.#student_id;
}
get lesson_location() { return this.#lesson_location; }
set student_id(student_id) {
this.API.isNotInitialized() ?
this.#student_id = student_id :
throwReadOnlyError(this.API);
}
get student_name() {
return this.#student_name;
}
set student_name(student_name) {
this.API.isNotInitialized() ?
this.#student_name = student_name :
throwReadOnlyError(this.API);
}
get lesson_location() {
return this.#lesson_location;
}
set lesson_location(lesson_location) {
if (this.API.checkValidFormat(lesson_location, regex.CMIString256)) {
this.#lesson_location = lesson_location;
}
}
get credit() { return this.#credit; }
set credit(credit) { this.API.isNotInitialized() ? this.#credit = credit : throwReadOnlyError(this.API); }
get credit() {
return this.#credit;
}
get lesson_status() { return this.#lesson_status; }
set credit(credit) {
this.API.isNotInitialized() ?
this.#credit = credit :
throwReadOnlyError(this.API);
}
get lesson_status() {
return this.#lesson_status;
}
set lesson_status(lesson_status) {
if (this.API.checkValidFormat(lesson_status, regex.CMIStatus)) {
this.#lesson_status = lesson_status;
}
}
get entry() { return this.#entry; }
set entry(entry) { this.API.isNotInitialized() ? this.#entry = entry : throwReadOnlyError(this.API); }
get entry() {
return this.#entry;
}
get total_time() { return this.#total_time; }
set total_time(total_time) { this.API.isNotInitialized() ? this.#total_time = total_time : throwReadOnlyError(this.API); }
set entry(entry) {
this.API.isNotInitialized() ?
this.#entry = entry :
throwReadOnlyError(this.API);
}
get lesson_mode() { return this.#lesson_mode; }
set lesson_mode(lesson_mode) { this.API.isNotInitialized() ? this.#lesson_mode = lesson_mode : throwReadOnlyError(this.API); }
get total_time() {
return this.#total_time;
}
get exit() { return (!this.jsonString) ? throwWriteOnlyError(this.API) : this.#exit; }
set total_time(total_time) {
this.API.isNotInitialized() ?
this.#total_time = total_time :
throwReadOnlyError(this.API);
}
get lesson_mode() {
return this.#lesson_mode;
}
set lesson_mode(lesson_mode) {
this.API.isNotInitialized() ?
this.#lesson_mode = lesson_mode :
throwReadOnlyError(this.API);
}
get exit() {
return (!this.jsonString) ? throwWriteOnlyError(this.API) : this.#exit;
}
set exit(exit) {
if (this.API.checkValidFormat(exit, regex.CMIExit)) {
this.#exit = exit;
}
}
get session_time() { return (!this.jsonString) ? throwWriteOnlyError(this.API) : this.#session_time; }
get session_time() {
return (!this.jsonString) ?
throwWriteOnlyError(this.API) :
this.#session_time;
}
set session_time(session_time) {
if (this.API.checkValidFormat(session_time, regex.CMITimespan)) {
this.#session_time = session_time;
@@ -163,7 +251,7 @@ class CMICore extends BaseCMI {
'lesson_mode': this.lesson_mode,
'exit': this.exit,
'session_time': this.session_time,
'score': this.score
'score': this.score,
};
delete this.jsonString;
return result;
@@ -175,7 +263,7 @@ class CMIObjectives extends CMIArray {
super({
API: API,
children: constants.objectives_children,
errorCode: scorm12_error_codes.INVALID_SET_VALUE
errorCode: scorm12_error_codes.INVALID_SET_VALUE,
});
}
}
@@ -183,34 +271,62 @@ class CMIObjectives extends CMIArray {
export class CMIStudentData extends BaseCMI {
#_children;
#mastery_score = "";
#max_time_allowed = "";
#time_limit_action = "";
#mastery_score = '';
#max_time_allowed = '';
#time_limit_action = '';
constructor(API, student_data_children) {
super(API);
this.#_children = student_data_children? student_data_children : constants.student_data_children;
this.#_children = student_data_children ?
student_data_children :
constants.student_data_children;
}
get _children() { return this.#_children; }
set _children(_children) { throwInvalidValueError(this.API); }
get _children() {
return this.#_children;
}
get mastery_score() { return this.#mastery_score; }
set mastery_score(mastery_score) { this.API.isNotInitialized() ? this.#mastery_score = mastery_score : throwReadOnlyError(this.API); }
set _children(_children) {
throwInvalidValueError(this.API);
}
get max_time_allowed() { return this.#max_time_allowed; }
set max_time_allowed(max_time_allowed) { this.API.isNotInitialized() ? this.#max_time_allowed = max_time_allowed : throwReadOnlyError(this.API); }
get mastery_score() {
return this.#mastery_score;
}
get time_limit_action() { return this.#time_limit_action; }
set time_limit_action(time_limit_action) { this.API.isNotInitialized() ? this.#time_limit_action = time_limit_action : throwReadOnlyError(this.API); }
set mastery_score(mastery_score) {
this.API.isNotInitialized() ?
this.#mastery_score = mastery_score :
throwReadOnlyError(this.API);
}
get max_time_allowed() {
return this.#max_time_allowed;
}
set max_time_allowed(max_time_allowed) {
this.API.isNotInitialized() ?
this.#max_time_allowed = max_time_allowed :
throwReadOnlyError(this.API);
}
get time_limit_action() {
return this.#time_limit_action;
}
set time_limit_action(time_limit_action) {
this.API.isNotInitialized() ?
this.#time_limit_action = time_limit_action :
throwReadOnlyError(this.API);
}
toJSON = () => {
this.jsonString = true;
const result = {
'mastery_score': this.mastery_score,
'max_time_allowed': this.max_time_allowed,
'time_limit_action': this.time_limit_action
'time_limit_action': this.time_limit_action,
};
delete this.jsonString;
return result;
@@ -223,41 +339,54 @@ class CMIStudentPreference extends BaseCMI {
}
#_children = constants.student_preference_children;
#audio = "";
#language = "";
#speed = "";
#text = "";
#audio = '';
#language = '';
#speed = '';
#text = '';
get _children() { return this.#_children; }
set _children(_children) { throwInvalidValueError(this.API); }
get _children() {
return this.#_children;
}
get audio() { return this.#audio; }
set _children(_children) {
throwInvalidValueError(this.API);
}
get audio() {
return this.#audio;
}
set audio(audio) {
if(this.API.checkValidFormat(audio, regex.CMISInteger)
&& this.API.checkValidRange(audio, regex.audio_range)) {
if (this.API.checkValidFormat(audio, regex.CMISInteger) &&
this.API.checkValidRange(audio, regex.audio_range)) {
this.#audio = audio;
}
}
get language() { return this.#language; }
get language() {
return this.#language;
}
set language(language) {
if (this.API.checkValidFormat(language, regex.CMIString256)) {
this.#language = language;
}
}
get speed() { return this.#speed; }
get speed() {
return this.#speed;
}
set speed(speed) {
if(this.API.checkValidFormat(speed, regex.CMISInteger)
&& this.API.checkValidRange(speed, regex.speed_range)) {
if (this.API.checkValidFormat(speed, regex.CMISInteger) &&
this.API.checkValidRange(speed, regex.speed_range)) {
this.#speed = speed;
}
}
get text() { return this.#text; }
get text() {
return this.#text;
}
set text(text) {
if(this.API.checkValidFormat(text, regex.CMISInteger)
&& this.API.checkValidRange(text, regex.text_range)) {
if (this.API.checkValidFormat(text, regex.CMISInteger) &&
this.API.checkValidRange(text, regex.text_range)) {
this.#text = text;
}
}
@@ -268,7 +397,7 @@ class CMIStudentPreference extends BaseCMI {
'audio': this.audio,
'language': this.language,
'speed': this.speed,
'text': this.text
'text': this.text,
};
delete this.jsonString;
return result;
@@ -280,7 +409,7 @@ class CMIInteractions extends CMIArray {
super({
API: API,
children: constants.interactions_children,
errorCode: scorm12_error_codes.INVALID_SET_VALUE
errorCode: scorm12_error_codes.INVALID_SET_VALUE,
});
}
}
@@ -292,12 +421,12 @@ export class CMIInteractionsObject extends BaseCMI {
this.objectives = new CMIArray({
API: API,
errorCode: 402,
children: constants.objectives_children
children: constants.objectives_children,
});
this.correct_responses = new CMIArray({
API: API,
errorCode: 402,
children: constants.correct_responses_children
children: constants.correct_responses_children,
});
}
@@ -309,50 +438,72 @@ export class CMIInteractionsObject extends BaseCMI {
#result: "";
#latency: "";
get id() { return (!this.jsonString) ? throwWriteOnlyError(this.API) : this.#id; }
get id() {
return (!this.jsonString) ? throwWriteOnlyError(this.API) : this.#id;
}
set id(id) {
if (this.API.checkValidFormat(id, regex.CMIIdentifier)) {
this.#id = id;
}
}
get time() { return (!this.jsonString) ? throwWriteOnlyError(this.API) : this.#time; }
get time() {
return (!this.jsonString) ? throwWriteOnlyError(this.API) : this.#time;
}
set time(time) {
if (this.API.checkValidFormat(time, regex.CMITime)) {
this.#time = time;
}
}
get type() { return (!this.jsonString) ? throwWriteOnlyError(this.API) : this.#type; }
get type() {
return (!this.jsonString) ? throwWriteOnlyError(this.API) : this.#type;
}
set type(type) {
if (this.API.checkValidFormat(type, regex.CMIType)) {
this.#type = type;
}
}
get weighting() { return (!this.jsonString) ? throwWriteOnlyError(this.API) : this.#weighting; }
get weighting() {
return (!this.jsonString) ?
throwWriteOnlyError(this.API) :
this.#weighting;
}
set weighting(weighting) {
if(this.API.checkValidFormat(weighting, regex.CMIDecimal)
&& this.API.checkValidRange(weighting, regex.weighting_range)) {
if (this.API.checkValidFormat(weighting, regex.CMIDecimal) &&
this.API.checkValidRange(weighting, regex.weighting_range)) {
this.#weighting = weighting;
}
}
get student_response() { return (!this.jsonString) ? throwWriteOnlyError(this.API) : this.#student_response; }
get student_response() {
return (!this.jsonString) ?
throwWriteOnlyError(this.API) :
this.#student_response;
}
set student_response(student_response) {
if (this.API.checkValidFormat(student_response, regex.CMIFeedback)) {
this.#student_response = student_response;
}
}
get result() { return (!this.jsonString) ? throwWriteOnlyError(this.API) : this.#result; }
get result() {
return (!this.jsonString) ?
throwWriteOnlyError(this.API) :
this.#result;
}
set result(result) {
if (this.API.checkValidFormat(result, regex.CMIResult)) {
this.#result = result;
}
}
get latency() { return (!this.jsonString) ? throwWriteOnlyError(this.API) : this.#latency; }
get latency() {
return (!this.jsonString) ?
throwWriteOnlyError(this.API) :
this.#latency;
}
set latency(latency) {
if (this.API.checkValidFormat(latency, regex.CMITimespan)) {
this.#latency = latency;
@@ -370,7 +521,7 @@ export class CMIInteractionsObject extends BaseCMI {
'result': this.result,
'latency': this.latency,
'objectives': this.objectives,
'correct_responses': this.correct_responses
'correct_responses': this.correct_responses,
};
delete this.jsonString;
return result;
@@ -387,14 +538,18 @@ export class CMIObjectivesObject extends BaseCMI {
#id: "";
#status: "";
get id() { return this.#id; }
get id() {
return this.#id;
}
set id(id) {
if (this.API.checkValidFormat(id, regex.CMIIdentifier)) {
this.#id = id;
}
}
get status() { return this.#status; }
get status() {
return this.#status;
}
set status(status) {
if (this.API.checkValidFormat(status, regex.CMIStatus2)) {
this.#status = status;
@@ -406,7 +561,7 @@ export class CMIObjectivesObject extends BaseCMI {
const result = {
'id': this.id,
'status': this.status,
'score': this.score
'score': this.score,
};
delete this.jsonString;
return result;
@@ -420,7 +575,9 @@ export class CMIInteractionsObjectivesObject extends BaseCMI {
#id: "";
get id() { return this.#id; }
get id() {
return this.#id;
}
set id(id) {
if (this.API.checkValidFormat(id, regex.CMIIdentifier)) {
this.#id = id;
@@ -444,7 +601,9 @@ export class CMIInteractionsCorrectResponsesObject extends BaseCMI {
#pattern: "";
get pattern() { return this.#pattern; }
get pattern() {
return this.#pattern;
}
set pattern(pattern) {
if (this.API.checkValidFormat(pattern, regex.CMIFeedback)) {
this.#pattern = pattern;

View File

@@ -14,8 +14,8 @@ const designations = [
/**
* Converts a Number to a String of HH:MM:SS
*
* @param totalSeconds
* @returns {string}
* @param {Number} totalSeconds
* @return {string}
*/
export function getSecondsAsHHMMSS(totalSeconds: Number) {
// SCORM spec does not deal with negative durations, give zero back
@@ -23,11 +23,11 @@ export function getSecondsAsHHMMSS(totalSeconds: Number) {
return '00:00:00';
}
let hours = Math.floor(totalSeconds / SECONDS_PER_HOUR);
const hours = Math.floor(totalSeconds / SECONDS_PER_HOUR);
let dateObj = new Date(totalSeconds * 1000);
let minutes = dateObj.getUTCMinutes();
let seconds = dateObj.getSeconds();
const dateObj = new Date(totalSeconds * 1000);
const minutes = dateObj.getUTCMinutes();
const seconds = dateObj.getSeconds();
return hours.toString().padStart(2, '0') + ':' +
minutes.toString().padStart(2, '0') + ':' +
@@ -37,8 +37,8 @@ export function getSecondsAsHHMMSS(totalSeconds: Number) {
/**
* Calculate the number of seconds from ISO 8601 Duration
*
* @param seconds
* @returns {String}
* @param {Number} seconds
* @return {String}
*/
export function getSecondsAsISODuration(seconds: Number) {
// SCORM spec does not deal with negative durations, give zero back
@@ -65,12 +65,13 @@ export function getSecondsAsISODuration(seconds: Number) {
/**
* Calculate the number of seconds from HH:MM:SS.DDDDDD
*
* @param timeString
* @param timeRegex
* @returns {number}
* @param {string} timeString
* @param {RegExp} timeRegex
* @return {number}
*/
export function getTimeAsSeconds(timeString: String, timeRegex: RegExp) {
if(!timeString || typeof timeString !== 'string' || !timeString.match(timeRegex)) {
if (!timeString || typeof timeString !== 'string' ||
!timeString.match(timeRegex)) {
return 0;
}
const parts = timeString.split(':');
@@ -83,19 +84,20 @@ export function getTimeAsSeconds(timeString: String, timeRegex: RegExp) {
/**
* Calculate the number of seconds from ISO 8601 Duration
*
* @param duration
* @param durationRegex
* @returns {number}
* @param {string} duration
* @param {RegExp} durationRegex
* @return {number}
*/
export function getDurationAsSeconds(duration: String, durationRegex: RegExp) {
if (!duration || !duration.match(durationRegex)) {
return 0;
}
let [,years,months,,days,hours,minutes,seconds] = new RegExp(durationRegex).exec(duration) || [];
const [, years, months, , days, hours, minutes, seconds] = new RegExp(
durationRegex).exec(duration) || [];
let now = new Date();
let anchor = new Date(now);
const now = new Date();
const anchor = new Date(now);
anchor.setFullYear(anchor.getFullYear() + Number(years || 0));
anchor.setMonth(anchor.getMonth() + Number(months || 0));
anchor.setDate(anchor.getDate() + Number(days || 0));
@@ -103,7 +105,7 @@ export function getDurationAsSeconds(duration: String, durationRegex: RegExp) {
anchor.setMinutes(anchor.getMinutes() + Number(minutes || 0));
anchor.setSeconds(anchor.getSeconds() + Number(seconds || 0));
if (seconds) {
let milliseconds = Number(Number(seconds) % 1).toFixed(6) * 1000.0;
const milliseconds = Number(Number(seconds) % 1).toFixed(6) * 1000.0;
anchor.setMilliseconds(anchor.getMilliseconds() + milliseconds);
}