Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
template-scorm/js/scormfunctions.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
188 lines (166 sloc)
6.15 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Source code created by Rustici Software, LLC is licensed under a | |
Creative Commons Attribution 3.0 United States License | |
(http://creativecommons.org/licenses/by/3.0/us/) | |
Want to make SCORM easy? See our solutions at http://www.scorm.com. | |
This example provides for the bare minimum SCORM run-time calls. | |
It will demonstrate using the API discovery algorithm to find the | |
SCORM API and then calling Initialize and Terminate when the page | |
loads and unloads respectively. This example also demonstrates | |
some basic SCORM error handling. | |
*/ | |
//Include the standard ADL-supplied API discovery algorithm | |
/////////////////////////////////////////// | |
//Begin ADL-provided API discovery algorithm | |
/////////////////////////////////////////// | |
var findAPITries = 0 | |
/** | |
* Check to see if the window (win) contains the API | |
if the window (win) does not contain the API and | |
the window (win) has a parent window and the parent window | |
is not the same as the window (win) | |
* @param {window} win the top-level window object | |
*/ | |
function findAPI(win) { | |
while ( (win.API == null) && (win.parent != null) && (win.parent != win) ) { | |
// increment the number of findAPITries | |
findAPITries++ | |
// Note: 7 is an arbitrary number, but should be more than sufficient | |
if (findAPITries > 7) { | |
console.error('Error finding API -- too deeply nested.') | |
return null | |
} | |
// set the variable that represents the window being | |
// being searched to be the parent of the current window | |
// then search for the API again | |
win = win.parent | |
} | |
return win.API | |
} | |
/** | |
* open the API and return | |
*/ | |
function getAPI() { | |
// start by looking for the API in the current window | |
let theAPI = findAPI(window) | |
// if the API is null (could not be found in the current window) | |
// and the current window has an opener window | |
if ( (theAPI == null) && (window.opener != null) && (typeof(window.opener) != 'undefined') ) { | |
// try to find the API in the current window�s opener | |
theAPI = findAPI(window.opener) | |
} | |
// if the API has not been found | |
if (theAPI == null) { | |
// Alert the user that the API Adapter could not be found | |
console.error('Unable to find an API adapter') | |
} | |
return theAPI | |
} | |
/////////////////////////////////////////// | |
//End ADL-provided API discovery algorithm | |
/////////////////////////////////////////// | |
function MillisecondsToCMIDuration(ms) { | |
//Convert duration from milliseconds to 0000:00:00.00 format | |
var hms = '' | |
var dtm = new Date | |
dtm.setTime(ms) | |
var h = "000" + Math.floor(ms/3600000) | |
var m = "0" + dtm.getMinutes() | |
var s = "0" + dtm.getSeconds() | |
var cs = "0" + Math.round(dtm.getMilliseconds() / 10); hms = h.substr(h.length-4)+":"+m.substr(m.length-2)+":"; | |
hms += s.substr(s.length-2)+"."+cs.substr(cs.length-2); | |
return hms | |
} | |
//Create function handlers for the loading and unloading of the page | |
//Constants | |
const SCORM_TRUE = 'true' | |
const SCORM_FALSE = 'false' | |
const SCORM_NO_ERROR = '0' | |
//Since the Unload handler will be called twice, from both the onunload | |
//and onbeforeunload events, ensure that we only call LMSFinish once. | |
let finishCalled = false | |
//Track whether or not we successfully initialized. | |
let initialized = false | |
let API = null | |
function ScormProcessInitialize(){ | |
var result | |
API = getAPI() | |
if (API == null) { | |
console.error('ERROR - Could not establish a connection with the LMS.') | |
return | |
} | |
result = API.LMSInitialize('') | |
if (result == SCORM_FALSE) { | |
var errorNumber = API.LMSGetLastError() | |
var errorString = API.LMSGetErrorString(errorNumber) | |
var diagnostic = API.LMSGetDiagnostic(errorNumber) | |
var errorDescription = ` Number: ${errorNumber}\n Description: ${errorString}\n Diagnostic: ${diagnostic}` | |
console.error(`Error - Could not initialize communication with the LMS.\n${errorDescription}`) | |
return | |
} | |
initialized = true | |
} | |
function ScormProcessFinish() { | |
let result | |
//Don't terminate if we haven't initialized or if we've already terminated | |
if (initialized == false || finishCalled == true) { | |
return | |
} | |
result = API.LMSFinish('') | |
finishCalled = true | |
if (result == SCORM_FALSE) { | |
const errorNumber = API.LMSGetLastError() | |
const errorString = API.LMSGetErrorString(errorNumber) | |
const diagnostic = API.LMSGetDiagnostic(errorNumber) | |
const errorDescription = ` Number: ${errorNumber}\n Description: ${errorString}\n Diagnostic: ${diagnostic}` | |
console.error(`Error - Could not terminate communication with the LMS.\n${errorDescription}`) | |
return | |
} | |
} | |
/* | |
The onload and onunload event handlers are assigned in launchpage.html because more processing needs to | |
occur at these times in this example. | |
*/ | |
//window.onload = ScormProcessInitialize; | |
//window.onunload = ScormProcessTerminate; | |
//window.onbeforeunload = ScormProcessTerminate; | |
function ScormProcessGetValue(element) { | |
let result | |
if(initialized == false || finishCalled == true) { | |
return | |
} | |
result = API.LMSGetValue(element) | |
if (result == '') { | |
const errorNumber = API.LMSGetLastError() | |
if (errorNumber != SCORM_NO_ERROR) { | |
const errorString = API.LMSGetErrorString(errorNumber) | |
const diagnostic = API.LMSGetDiagnostic(errorNumber) | |
const errorDescription = ` Number: ${errorNumber}\n Description: ${errorString}\n Diagnostic: ${diagnostic}` | |
console.error(`Error - Could not retrieve a value from the LMS.\n${errorDescription}`) | |
return '' | |
} | |
} | |
return result | |
} | |
function ScormProcessSetValue(element, value) { | |
let result | |
if (initialized == false || finishCalled == true) { | |
return | |
} | |
if(element.includes('cmi.student_data')) { | |
console.warn(`element ${element} is readonly. value not saved.`) | |
return | |
} | |
result = API.LMSSetValue(element, value) | |
console.log(result) | |
API.LMSCommit('') | |
console.log('data committed') | |
if (result == SCORM_FALSE) { | |
const errorNumber = API.LMSGetLastError() | |
const errorString = API.LMSGetErrorString(errorNumber) | |
const diagnostic = API.LMSGetDiagnostic(errorNumber) | |
const errorDescription = ` Number: ${errorNumber}\n Description: ${errorString}\n Diagnostic: ${diagnostic}` | |
console.error(`Error - Could not store a value in the LMS.\n${errorDescription}`) | |
return | |
} | |
} |