Working on the saving of data
This commit is contained in:
@@ -8,9 +8,8 @@ import {
|
||||
CMIObjectivesObject,
|
||||
} from './cmi/scorm12_cmi';
|
||||
import * as Utilities from './utilities';
|
||||
import {scorm12_constants} from './constants/api_constants';
|
||||
import {global_constants, scorm12_constants} from './constants/api_constants';
|
||||
import {scorm12_error_codes} from './constants/error_codes';
|
||||
import {scorm12_regex} from './constants/regex';
|
||||
|
||||
const constants = scorm12_constants;
|
||||
|
||||
@@ -20,9 +19,16 @@ const constants = scorm12_constants;
|
||||
export default class Scorm12API extends BaseAPI {
|
||||
/**
|
||||
* Constructor for SCORM 1.2 API
|
||||
* @param {object} settings
|
||||
*/
|
||||
constructor() {
|
||||
super(scorm12_error_codes);
|
||||
constructor(settings: {}) {
|
||||
const finalSettings = {
|
||||
...{
|
||||
mastery_override: false,
|
||||
}, ...settings,
|
||||
};
|
||||
|
||||
super(scorm12_error_codes, finalSettings);
|
||||
|
||||
this.cmi = new CMI();
|
||||
// Rename functions to match 1.2 Spec and expose to modules
|
||||
@@ -149,9 +155,11 @@ export default class Scorm12API extends BaseAPI {
|
||||
|
||||
if (this.stringMatches(CMIElement, 'cmi\\.objectives\\.\\d')) {
|
||||
newChild = new CMIObjectivesObject();
|
||||
} else if (foundFirstIndex && this.stringMatches(CMIElement, 'cmi\\.interactions\\.\\d\\.correct_responses\\.\\d')) {
|
||||
} else if (foundFirstIndex && this.stringMatches(CMIElement,
|
||||
'cmi\\.interactions\\.\\d\\.correct_responses\\.\\d')) {
|
||||
newChild = new CMIInteractionsCorrectResponsesObject();
|
||||
} else if (foundFirstIndex && this.stringMatches(CMIElement, 'cmi\\.interactions\\.\\d\\.objectives\\.\\d')) {
|
||||
} else if (foundFirstIndex && this.stringMatches(CMIElement,
|
||||
'cmi\\.interactions\\.\\d\\.objectives\\.\\d')) {
|
||||
newChild = new CMIInteractionsObjectivesObject();
|
||||
} else if (this.stringMatches(CMIElement, 'cmi\\.interactions\\.\\d')) {
|
||||
newChild = new CMIInteractionsObject();
|
||||
@@ -201,4 +209,86 @@ export default class Scorm12API extends BaseAPI {
|
||||
// Data Model
|
||||
this.cmi = newAPI.cmi;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the cmi object to the proper format for LMS commit
|
||||
*
|
||||
* @param {boolean} terminateCommit
|
||||
* @return {object|Array}
|
||||
*/
|
||||
renderCommitCMI(terminateCommit: boolean) {
|
||||
const cmiExport = this.renderCMIToJSONObject();
|
||||
|
||||
if (terminateCommit) {
|
||||
cmiExport.cmi.core.total_time = this.getCurrentTotalTime();
|
||||
}
|
||||
|
||||
const result = [];
|
||||
const flattened = Utilities.flatten(cmiExport);
|
||||
switch (this.settings.dataCommitFormat) {
|
||||
case 'flattened':
|
||||
return Utilities.flatten(cmiExport);
|
||||
case 'params':
|
||||
for (const item in flattened) {
|
||||
if ({}.hasOwnProperty.call(flattened, item)) {
|
||||
result.push(`${item}=${flattened[item]}`);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
case 'json':
|
||||
default:
|
||||
return cmiExport;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to store the data to the LMS
|
||||
*
|
||||
* @param {boolean} terminateCommit
|
||||
* @return {string}
|
||||
*/
|
||||
storeData(terminateCommit: boolean) {
|
||||
if (terminateCommit) {
|
||||
const originalStatus = this.cmi.core.lesson_status;
|
||||
if (originalStatus === 'not attempted') {
|
||||
this.cmi.core.lesson_status = 'completed';
|
||||
}
|
||||
|
||||
if (this.cmi.core.lesson_mode === 'normal') {
|
||||
if (this.cmi.core.credit === 'credit') {
|
||||
if (this.settings.mastery_override &&
|
||||
this.cmi.student_data.mastery_score !== '' &&
|
||||
this.cmi.core.score.raw !== '') {
|
||||
if (parseFloat(this.cmi.core.score.raw) >=
|
||||
parseFloat(this.cmi.student_data.mastery_score)) {
|
||||
this.cmi.core.lesson_status = 'passed';
|
||||
} else {
|
||||
this.cmi.core.lesson_status = 'failed';
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (this.cmi.core.lesson_mode === 'browse') {
|
||||
if ((this.startingData?.cmi?.core?.lesson_status || '') === '' &&
|
||||
originalStatus === 'not attempted') {
|
||||
this.cmi.core.lesson_status = 'browsed';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const commitObject = this.renderCommitCMI(terminateCommit);
|
||||
|
||||
if (this.settings.lmsCommitUrl) {
|
||||
if (this.apiLogLevel === global_constants.LOG_LEVEL_DEBUG) {
|
||||
console.debug('Commit (terminated: ' +
|
||||
(terminateCommit ? 'yes' : 'no') + '): ');
|
||||
console.debug(commitObject);
|
||||
}
|
||||
return this.processHttpRequest(this.settings.lmsCommitUrl, commitObject);
|
||||
} else {
|
||||
console.log('Commit (terminated: ' +
|
||||
(terminateCommit ? 'yes' : 'no') + '): ');
|
||||
console.log(commitObject);
|
||||
return global_constants.SCORM_TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user