This commit is contained in:
Jonathan Putney
2020-01-09 09:37:58 -05:00
17 changed files with 2031 additions and 534 deletions

View File

@@ -116,7 +116,7 @@ export default class BaseAPI {
result.result : global_constants.SCORM_FALSE;
if (checkTerminated) this.lastErrorCode = 0;
returnValue = global_constants.SCORM_TRUE;
this.processListeners(callbackName);
}
@@ -171,6 +171,9 @@ export default class BaseAPI {
checkTerminated: boolean,
CMIElement,
value) {
if (value !== undefined) {
value = String(value);
}
let returnValue = global_constants.SCORM_FALSE;
if (this.checkState(checkTerminated, this.#error_codes.STORE_BEFORE_INIT,
@@ -183,6 +186,7 @@ export default class BaseAPI {
this.lastErrorCode = e.errorCode;
returnValue = global_constants.SCORM_FALSE;
} else {
console.error(e.getMessage());
this.throwSCORMError(this.#error_codes.GENERAL);
}
}
@@ -808,13 +812,14 @@ export default class BaseAPI {
return;
}
CMIElement = CMIElement || 'cmi';
CMIElement = CMIElement !== undefined ? CMIElement : 'cmi';
this.startingData = json;
// could this be refactored down to flatten(json) then setCMIValue on each?
for (const key in json) {
if ({}.hasOwnProperty.call(json, key) && json[key]) {
const currentCMIElement = CMIElement + '.' + key;
const currentCMIElement = (CMIElement ? CMIElement + '.' : '') + key;
const value = json[key];
if (value['childArray']) {

View File

@@ -503,16 +503,19 @@ export default class Scorm2004API extends BaseAPI {
if (this.cmi.credit === 'credit') {
if (this.cmi.completion_threshold && this.cmi.progress_measure) {
if (this.cmi.progress_measure >= this.cmi.completion_threshold) {
console.debug('Setting Completion Status: Completed');
this.cmi.completion_status = 'completed';
} else {
console.debug('Setting Completion Status: Incomplete');
this.cmi.completion_status = 'incomplete';
}
}
if (this.cmi.scaled_passing_score !== null &&
this.cmi.score.scaled !== '') {
if (this.cmi.scaled_passing_score && this.cmi.score.scaled) {
if (this.cmi.score.scaled >= this.cmi.scaled_passing_score) {
console.debug('Setting Success Status: Passed');
this.cmi.success_status = 'passed';
} else {
console.debug('Setting Success Status: Failed');
this.cmi.success_status = 'failed';
}
}

View File

@@ -362,7 +362,7 @@ class CMICore extends BaseCMI {
* @param {string} lesson_location
*/
set lesson_location(lesson_location) {
if (check12ValidFormat(lesson_location, regex.CMIString256)) {
if (check12ValidFormat(lesson_location, regex.CMIString256, true)) {
this.#lesson_location = lesson_location;
}
}
@@ -462,7 +462,7 @@ class CMICore extends BaseCMI {
* @param {string} exit
*/
set exit(exit) {
if (check12ValidFormat(exit, regex.CMIExit)) {
if (check12ValidFormat(exit, regex.CMIExit, true)) {
this.#exit = exit;
}
}
@@ -494,7 +494,7 @@ class CMICore extends BaseCMI {
return Utilities.addHHMMSSTimeStrings(
this.#total_time,
this.#session_time,
new RegExp(scorm12_regex.CMITimespan)
new RegExp(scorm12_regex.CMITimespan),
);
}
@@ -512,7 +512,6 @@ class CMICore extends BaseCMI {
* lesson_location: string,
* lesson_status: string,
* credit: string,
* total_time: string,
* session_time: *
* }
* }
@@ -526,7 +525,6 @@ class CMICore extends BaseCMI {
'credit': this.credit,
'lesson_status': this.lesson_status,
'entry': this.entry,
'total_time': this.getCurrentTotalTime(),
'lesson_mode': this.lesson_mode,
'exit': this.exit,
'session_time': this.session_time,

View File

@@ -235,7 +235,7 @@ export class CMI extends BaseCMI {
* @param {string} exit
*/
set exit(exit) {
if (check2004ValidFormat(exit, regex.CMIExit)) {
if (check2004ValidFormat(exit, regex.CMIExit, true)) {
this.#exit = exit;
}
}
@@ -507,8 +507,7 @@ export class CMI extends BaseCMI {
* session_time: string,
* success_status: string,
* suspend_data: string,
* time_limit_action: string,
* total_time: string
* time_limit_action: string
* }
* }
*/
@@ -538,7 +537,6 @@ export class CMI extends BaseCMI {
'success_status': this.success_status,
'suspend_data': this.suspend_data,
'time_limit_action': this.time_limit_action,
'total_time': this.getCurrentTotalTime(),
};
delete this.jsonString;
return result;

View File

@@ -28,11 +28,21 @@ export function getSecondsAsHHMMSS(totalSeconds: Number) {
const dateObj = new Date(totalSeconds * 1000);
const minutes = dateObj.getUTCMinutes();
// make sure we add any possible decimal value
const seconds = dateObj.getSeconds() + (totalSeconds % 1.0);
const seconds = dateObj.getSeconds();
const ms = totalSeconds % 1.0;
let msStr = '';
if (countDecimals(ms) > 0) {
if (countDecimals(ms) > 2) {
msStr = ms.toFixed(2);
} else {
msStr = String(ms);
}
msStr = '.' + msStr.split('.')[1];
}
return hours.toString().padStart(2, '0') + ':' +
minutes.toString().padStart(2, '0') + ':' +
seconds.toString().padStart(2, '0');
seconds.toString().padStart(2, '0') + msStr;
}
/**
@@ -119,7 +129,6 @@ export function getDurationAsSeconds(duration: String, durationRegex: RegExp) {
const milliseconds = Number(Number(seconds) % 1).toFixed(6) * 1000.0;
anchor.setMilliseconds(anchor.getMilliseconds() + milliseconds);
}
return ((anchor * 1.0) - now) / 1000.0;
}
@@ -220,3 +229,13 @@ export function unflatten(data) {
}
return result[''] || result;
}
/**
* Counts the number of decimal places
* @param {number} num
* @return {number}
*/
export function countDecimals(num: number) {
if (Math.floor(num) === num) return 0;
return num.toString().split('.')[1].length || 0;
}