Files

417 lines
13 KiB
JavaScript

function LMSInitialize(dummyString) {
// already initialized or already finished
/*if ((flagInitialized) || (flagFinished)) {
if( flagFinished ){
return "false";
}else{
return "true";
}
}*/
// set initialization flag
flagInitialized = true;
/*
if (LMSGetValue('cmi.core.score.raw') == '100' &&
LMSGetValue('cmi.core.lesson_status') == 'completed'
|| LMSGetValue('cmi.core.lesson_status') == 'passed' ) {
*/
if ( LMSGetValue('cmi.core.lesson_status') == 'completed'
|| LMSGetValue('cmi.core.lesson_status') == 'passed' ) {
flagFinished = true;
window.removeEventListener("onunload", LMSFinish);
window.removeEventListener("onbeforeunload", LMSFinish);
$('.btn-return').show();
} else {
flagFinished = false;
$('.btn-return').hide();
}
//console.dir(cache);
// return success value
return "true";
}
// ------------------------------------------
// SCORM RTE Functions - Getting and Setting Values
// ------------------------------------------
function LMSGetValue(varname) {
// not initialized or already finished
if ((!flagInitialized)) {
return "false";
}
// otherwise, return the requested data
return cache[varname];
}
function LMSSetValue(varname, varvalue) {
// not initialized or already finished
if ((!flagInitialized)) {
return "false";
}
// otherwise, set the requested data, and return success value
cache[varname] = varvalue;
if( varname == "cmi.core.score.raw" || varname == "cmi.core.lesson_status"){
LMSRealCommit('');
}
return "true";
}
// ------------------------------------------
// SCORM RTE Functions - Saving the Cache to the Database
// ------------------------------------------
function LMSCommit(dummyString) {
return "true";
}
// ------------------------------------------
// SCORM RTE Functions - Saving the Cache to the Database
// ------------------------------------------
function LMSRealCommit(dummyString) {
// not initialized or already finished
if ((!flagInitialized) || (flagFinished)) {
return "false";
} else {
// code to prevent caching
var d = new Date();
var data = new Object();
data['cmi.core.lesson_location'] = cache['cmi.core.lesson_location'];
data['cmi.core.lesson_status'] = cache['cmi.core.lesson_status'];
data['cmi.core.exit'] = cache['cmi.core.exit'];
data['cmi.core.score.raw'] = cache['cmi.core.score.raw'];
if (cache['cmi.core.score.raw'] == "100") {
data['cmi.core.lesson_status'] = "completed";
}
if (cache['cmi.core.lesson_status'] == "completed" || cache['cmi.core.lesson_status'] == "passed") {
$('.btn-return').show();
}
data['cmi.suspend_data'] = cache['cmi.suspend_data'];
data['cmi.core.session_time'] = cache['cmi.core.session_time'];
var elr = "";
$.ajax({
url: basepathsaves__ + 'runtime/commit.php',
type: 'POST',
data: {
SCOInstanceID: SCOInstanceID,
data: JSON.stringify(data),
code: d.getTime()
}
})
.done(function() {
if (cache['cmi.core.score.raw'] == "100" && cache['cmi.core.lesson_status'] == "completed") {
flagFinished = true;
$('.btn-return').show();
}
elr = "true";
})
.fail(function() {
console.log('Problem with AJAX Request in LMSCommit()');
elr = "false";
})
.always(function() {
return elr;
});
}
}
// ------------------------------------------
// SCORM RTE Functions - Closing The Session
// ------------------------------------------
function LMSFinish(dummyString) {
var body = document.body,
html = document.documentElement;
var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
if( $(".layer-load").length > 0 ){
document.getElementsByClassName("layer-load")[0].style.height = height + "px";
document.getElementsByClassName("layer-load")[0].style.display = "block";
window.onresize = function(event) {
var body = document.body,
html = document.documentElement;
var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
document.getElementsByClassName("layer-load")[0].style.height = height + "px";
};
}
// not initialized or already finished
if ((!flagInitialized) || (flagFinished)) {
console.log("out");
} else {
flagFinished = true;
var d = new Date();
// commit cached values to the database
// not initialized or already finished
// code to prevent caching
var d = new Date();
var data = new Object();
data['cmi.core.lesson_location'] = cache['cmi.core.lesson_location'];
data['cmi.core.lesson_status'] = cache['cmi.core.lesson_status'];
data['cmi.core.exit'] = cache['cmi.core.exit'];
data['cmi.core.score.raw'] = cache['cmi.core.score.raw'];
if (cache['cmi.core.score.raw'] == "100") {
data['cmi.core.lesson_status'] = "completed";
}
if (cache['cmi.core.lesson_status'] == "completed" || cache['cmi.core.lesson_status'] == "passed") {
$('.btn-return').show();
}
data['cmi.suspend_data'] = cache['cmi.suspend_data'];
data['cmi.core.session_time'] = cache['cmi.core.session_time'];
var elr = "";
$.ajax({
url: basepathsaves__ + 'runtime/commit.php',
type: 'POST',
data: {
SCOInstanceID: SCOInstanceID,
data: JSON.stringify(data),
code: d.getTime()
}
})
.done(function() {
elr = "true";
})
.fail(function() {
console.log('Problem with AJAX Request in LMSCommit()');
elr = "false";
})
.always(function() {
return elr;
});
//var data = new Object();
//data['cmi.core.lesson_location'] = cache['cmi.core.lesson_location'];
//data['cmi.core.lesson_status'] = cache['cmi.core.lesson_status'];
//data['cmi.core.exit'] = cache['cmi.core.exit'];
//data['cmi.core.score.raw'] = cache['cmi.core.score.raw'];
//data['cmi.suspend_data'] = cache['cmi.suspend_data'];
//data['cmi.core.session_time'] = cache['cmi.core.session_time'];
// commit cached values to the database
// code to prevent caching
/*var computeTimeString = computeTime();
var elr = "";
$.ajax({
url: basepathsaves__+'runtime/finish.php',
type: 'POST',
data: {
SCOInstanceID: SCOInstanceID,
code: d.getTime(),
computeTimeString: computeTimeString,
data: JSON.stringify(data)
},
})
.done(function() {
elr = "true";
})
.fail(function() {
alert('Problem with AJAX Request in LMSFinish()');
elr = "";
})
.always(function() {
flagFinished = true;
return elr;
});*/
}
}
window.addEventListener("pagehide", LMSFinish);
window.onbeforeunload = LMSFinish;
// ------------------------------------------
// SCORM RTE Functions - Error Handling
// ------------------------------------------
function LMSGetLastError() {
return 0;
}
function LMSGetDiagnostic(errorCode) {
return "";
}
function LMSGetErrorString(errorCode) {
return "";
}
// ------------------------------------------
// AJAX Request Handling
// ------------------------------------------
function createRequest() {
var request;
try {
request = new XMLHttpRequest();
} catch (tryIE) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (tryOlderIE) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
alert("Error creating XMLHttpRequest");
}
}
}
return request;
}
// ------------------------------------------
// URL Encoding
// ------------------------------------------
function urlencode(str) {
var histogram = {},
unicodeStr = '',
hexEscStr = '';
var ret = (str + '').toString();
var replacer = function(search, replace, str) {
var tmp_arr = [];
tmp_arr = str.split(search);
return tmp_arr.join(replace);
};
// The histogram is identical to the one in urldecode.
histogram["'"] = '%27';
histogram['('] = '%28';
histogram[')'] = '%29';
histogram['*'] = '%2A';
histogram['~'] = '%7E';
histogram['!'] = '%21';
histogram['%20'] = '+';
histogram['\u00DC'] = '%DC';
histogram['\u00FC'] = '%FC';
histogram['\u00C4'] = '%D4';
histogram['\u00E4'] = '%E4';
histogram['\u00D6'] = '%D6';
histogram['\u00F6'] = '%F6';
histogram['\u00DF'] = '%DF';
histogram['\u20AC'] = '%80';
histogram['\u0081'] = '%81';
histogram['\u201A'] = '%82';
histogram['\u0192'] = '%83';
histogram['\u201E'] = '%84';
histogram['\u2026'] = '%85';
histogram['\u2020'] = '%86';
histogram['\u2021'] = '%87';
histogram['\u02C6'] = '%88';
histogram['\u2030'] = '%89';
histogram['\u0160'] = '%8A';
histogram['\u2039'] = '%8B';
histogram['\u0152'] = '%8C';
histogram['\u008D'] = '%8D';
histogram['\u017D'] = '%8E';
histogram['\u008F'] = '%8F';
histogram['\u0090'] = '%90';
histogram['\u2018'] = '%91';
histogram['\u2019'] = '%92';
histogram['\u201C'] = '%93';
histogram['\u201D'] = '%94';
histogram['\u2022'] = '%95';
histogram['\u2013'] = '%96';
histogram['\u2014'] = '%97';
histogram['\u02DC'] = '%98';
histogram['\u2122'] = '%99';
histogram['\u0161'] = '%9A';
histogram['\u203A'] = '%9B';
histogram['\u0153'] = '%9C';
histogram['\u009D'] = '%9D';
histogram['\u017E'] = '%9E';
histogram['\u0178'] = '%9F';
// Begin with encodeURIComponent, which most resembles PHP's encoding functions
ret = encodeURIComponent(ret);
for (unicodeStr in histogram) {
hexEscStr = histogram[unicodeStr];
ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
}
// Uppercase for full PHP compatibility
return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
return "%" + m2.toUpperCase();
});
}
API.LMSInitialize = LMSInitialize;
API.LMSGetValue = LMSGetValue;
API.LMSSetValue = LMSSetValue;
API.LMSCommit = LMSCommit;
API.LMSFinish = LMSFinish;
API.LMSGetLastError = LMSGetLastError;
API.LMSGetErrorString = LMSGetErrorString;
API.LMSGetDiagnostic = LMSGetDiagnostic;
var startDate = 0;
function resizeIframe(iframe) {
var padding = 50;
if ($("#navsco").length > 0) {
var navbar360 = $("#navsco").outerHeight();
} else {
var navbar360 = 0;
}
padding = padding + navbar360;
if (iframe.contentWindow.document.body.scrollHeight < (window.innerHeight - padding))
iframe.height = iframe.contentWindow.document.body.scrollHeight + "px";
else
iframe.height = (window.innerHeight + padding) + "px";
}
/**
* Descripción: Inica el conteo del tiempo en el sco
* @return
* @method startTimer
* @return
*/
function startTimer() {
startDate = new Date().getTime();
}
/**
* Descripción: calucla el tiempo que el usuario permance en el sco
* @method computeTime
* @return formattedTime
*/
function computeTime() {
var formattedTime = "00:00:00.0";
if (startDate != 0) {
var currentDate = new Date().getTime();
var elapsedSeconds = ((currentDate - startDate) / 1000);
formattedTime = convertTotalSeconds(elapsedSeconds);
}
return formattedTime;
}
/**
* Descripción: convierte el tiempo en segundos
* @method convertTotalSeconds
* @param ts de tipo time
* @return rtnVal
*/
function convertTotalSeconds(ts) {
var Sec = (ts % 60);
ts -= Sec;
var tmp = (ts % 3600);
ts -= tmp;
if ((ts % 3600) != 0) var Hour = "00";
else var Hour = "" + (ts / 3600);
if ((tmp % 60) != 0) var Min = "00";
else var Min = "" + (tmp / 60);
Sec = "" + Sec
Sec = Sec.substring(0, Sec.indexOf("."))
if (Hour.length < 2) Hour = "0" + Hour;
if (Min.length < 2) Min = "0" + Min;
if (Sec.length < 2) Sec = "0" + Sec;
var rtnVal = Hour + ":" + Min + ":" + Sec;
return rtnVal;
}