Created
October 17, 2015 01:08
-
-
Save mdx86/1b1e0b10bd24f24faf2d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(function ($) { | |
//#region Console mock | |
var console = { | |
log: function (text) { | |
if (window.console.log) { | |
try { | |
window.console.log(text); | |
} catch (e) { } | |
} | |
}, | |
warn: function (text) { | |
if (window.console.warn) { | |
try { | |
window.console.warn(text); | |
} catch (e) { } | |
} | |
}, | |
info: function (text) { | |
if (window.console.info) { | |
try { | |
window.console.info(text); | |
} catch (e) { } | |
} | |
}, | |
dir: function (text) { | |
if (window.console.dir) { | |
try { | |
window.console.dir(text); | |
} catch (e) { } | |
} | |
} | |
}; | |
//#endregion | |
//#region AICC via Http API | |
function AICCViaHttpAPI(launchData) { | |
//#region Vars | |
var launchUrl, aiccUrl, sessionId, inited = false, hasChanged = false, | |
aiccCache = {}, responseCache = {}, stringGroupReg = /Core_Lesson|Comments/ig, aiccModel; | |
var HttpCommands = { | |
GetParam: "GetParam", | |
PutParam: "PutParam", | |
PutComments: "PutComments", | |
PutObjectives: "PutObjectives", | |
PutPath: "PutPath", | |
PutInteractions: "PutInteractions", | |
PutPerformance: "PutPerformance" | |
}; | |
//#endregion | |
//#region Methods | |
/*Session Methods | |
------------------------------*/ | |
this.LMSInitialize = function (name) { | |
if (inited) { | |
return "false"; | |
} | |
var result = post(HttpCommands.GetParam); | |
loadData(result); | |
console.log(responseCache); | |
console.log(aiccCache); | |
inited = true; | |
return "true"; | |
} | |
this.LMSFinish = function (input) { | |
if (!inited) { | |
return "false"; | |
} | |
this.LMSCommit(""); | |
inited = false; | |
} | |
/*Data-transfer Methods | |
------------------------------*/ | |
this.LMSGetValue = function (name) { | |
if (!inited) { | |
return ""; | |
} | |
return aiccModel.GetValue(name); | |
} | |
this.LMSSetValue = function () { | |
if (!inited) { | |
return ""; | |
} | |
} | |
this.LMSCommit = function (input) { | |
if (!inited || !hasChanged) { | |
return "false"; | |
} | |
} | |
/*Error handling Methods | |
------------------------------*/ | |
this.LMSGetLastError = function (input) { | |
var value = 0; | |
if (responseCache && responseCache["error"]) { | |
value = parseInt(responseCache["error"], 10); | |
value = isNaN(value) ? 0 : value; | |
} | |
return value; | |
} | |
this.LMSGetErrorString = function (input) { | |
return ""; | |
} | |
this.LMSGetDiagnostic = function (input) { | |
return ""; | |
} | |
//#endregion | |
//#region Functions | |
function post(command, aicc_data) { | |
var data = {}; | |
if (aicc_data) { | |
data.aicc_data = aicc_data; | |
} | |
data.command = command; | |
data.session_id = sessionId; | |
var result = null; | |
$.ajax({ | |
url: aiccUrl, | |
async: false, | |
cache: false, | |
type: 'POST', | |
dataType: 'html', | |
data: data, | |
success: function (data, textStatus, jqXHR) { | |
result = data; | |
}, | |
error: function (ex) { | |
console.log("PostError:"); | |
console.log(data); | |
} | |
}); | |
return result; | |
} | |
function loadData(text) { | |
if (!text) { | |
return; | |
} | |
text = $.trim(text); | |
var items = text.split(/aicc_data=/ig); | |
readResponse(items[0]); | |
readAICC(items[1]); | |
aiccModel = new AICCModelContainer(items[1]); | |
} | |
function readResponse(text) { | |
text = text ? $.trim(text) : text; | |
if (!text) { | |
return; | |
} | |
var line, lines = text.split("\n"); | |
for (var i = 0, len = lines.length; i < len; i++) { | |
line = $.trim(lines[i]); | |
if (!line || line.charAt(0) == ";") { | |
continue; | |
} | |
var nameValue = readNameValue(line); | |
if (nameValue && nameValue.name) { | |
responseCache[nameValue.name.toLowerCase()] = nameValue.value; | |
} | |
} | |
} | |
function readAICC(text) { | |
text = text ? $.trim(text) : text; | |
if (!text) { | |
return; | |
} | |
var reg = /\[[^\[\]]+\]/ig; | |
var groups = text.match(reg); | |
var groupItems = text.split(reg); | |
var groupName, isStr, groupValue, storeValue; | |
for (var i = 0, len = groups.length; i < len; i++) { | |
groupName = getGroupName(groups[i]).toLowerCase(); | |
groupValue = decodeURIComponent($.trim(groupItems[i + 1])); | |
isStr = isStringGroup(groupName); | |
storeValue = isStr ? groupValue : readNameValues(groupValue); | |
aiccCache[groupName] = storeValue; | |
} | |
function getGroupName(text) { | |
var reg = /\[([^\[\]]+)\]/ig; | |
var name = reg.exec(text); | |
return name[1]; | |
} | |
function isStringGroup(groupName) { | |
return stringGroupReg.test(groupName); | |
} | |
} | |
function readNameValues(text) { | |
text = $.trim(text); | |
if (!text) { | |
return null; | |
} | |
var line, lines = text.split("\n"); | |
var arr = []; | |
for (var i = 0, len = lines.length; i < len; i++) { | |
line = lines[i]; | |
var data = readNameValue(line); | |
if (data) { | |
var key = data.name.toLowerCase(); | |
arr[key] = data; | |
} | |
} | |
return arr; | |
} | |
function readNameValue(line) { | |
var reg = /([^=]+)=([\s\S]*)/ig; | |
if (line) { | |
var match = reg.exec(line); | |
var data = { | |
name: $.trim(match[1]), | |
value: decodeURIComponent($.trim(match[2])) | |
}; | |
return data; | |
} | |
return null; | |
} | |
function checkInit() { | |
if (!inited) { | |
console.log("Doesn't Init yet"); | |
} | |
return inited; | |
} | |
function setup() { | |
aiccUrl = launchData.AICC_URL; | |
launchUrl = launchData.LaunchUrl; | |
sessionId = launchData.AICC_SID; | |
} | |
//#endregion | |
setup(); | |
} | |
AICCViaHttpAPI.getQueryValue = function (name, search) { | |
search = search ? search : window.location.search; | |
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); | |
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(search); | |
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); | |
} | |
AICCViaHttpAPI.findLaunchData = function (win) { | |
while (win) { | |
var aiccUrl = AICCViaHttpAPI.getQueryValue("AICC_URL", win.location.search); | |
var sessionId = AICCViaHttpAPI.getQueryValue("AICC_SID", win.location.search); | |
if (aiccUrl && sessionId) { | |
var launchUrl = win.location; | |
var data = { | |
AICC_URL: aiccUrl, | |
AICC_SID: sessionId, | |
LaunchUrl: launchUrl, | |
LaunchWindow: win | |
}; | |
return data; | |
} | |
win = win.opener ? win.opener : win.parent == win ? null : win.parent; | |
} | |
return null; | |
} | |
//AICCModelContainer | |
function AICCModelContainer(hacpData) { | |
var stringGroupReg = /Core_Lesson|Comments/ig, aiccCache = {}; | |
var lessonStatuses = ["passed", "completed", "failed", "incomplete", "browsed", "not attempted"]; | |
var apiBindingMapper = { | |
"cmi.suspend_data": "Core_Lesson" | |
}; | |
var dataConvertor = { | |
"cmi.core.lesson_status": { | |
get: function (value) { | |
if (value) { | |
var items = value.split(","); | |
var tmp = null; | |
$.each(items, function (index, item) { | |
item = $.trim(item).toLowerCase(); | |
$.each(lessonStatuses, function (i, status) { | |
if (status.indexOf(item) == 0) { | |
tmp = status; | |
return false; | |
} | |
}); | |
if (tmp) { | |
value = tmp; | |
return false; | |
} | |
}) | |
} | |
return value; | |
} | |
} | |
} | |
//#region Methods | |
this.GetValue = function (name) { | |
var value = null; | |
if (!name) { | |
return ""; | |
} | |
var nodes; | |
var key = apiBindingMapper[name]; | |
if (key) { | |
nodes = key.split("."); | |
} else { | |
nodes = name.split("."); | |
nodes.shift(); //remove the cmi node | |
} | |
var node, aiccNode = aiccCache; | |
for (var i = 0, len = nodes.length; i < len; i++) { | |
node = nodes[i].toLowerCase(); | |
aiccNode = aiccNode[node]; | |
if (i == len - 1 && aiccNode) { | |
value = aiccNode.constructor == Object ? aiccNode.value : aiccNode; | |
} | |
if (!aiccCache) { | |
break; | |
} | |
} | |
if (value) { | |
var convertor = dataConvertor[name]; | |
if (convertor && convertor.get) { | |
value = convertor.get(value); | |
} | |
} | |
return value; | |
} | |
this.SetValue = function (name, value) { | |
} | |
this.toHACPData = function () { | |
} | |
//#endregion | |
//#region Functions | |
function readAICC(text) { | |
text = text ? $.trim(text) : text; | |
if (!text) { | |
return; | |
} | |
var reg = /\[[^\[\]]+\]/ig; | |
var groups = text.match(reg); | |
var groupItems = text.split(reg); | |
var groupName, isStr, groupValue, storeValue; | |
for (var i = 0, len = groups.length; i < len; i++) { | |
groupName = getGroupName(groups[i]).toLowerCase(); | |
groupValue = decodeURIComponent($.trim(groupItems[i + 1])); | |
isStr = isStringGroup(groupName); | |
storeValue = isStr ? groupValue : readNameValues(groupValue); | |
aiccCache[groupName] = storeValue; | |
} | |
function getGroupName(text) { | |
var reg = /\[([^\[\]]+)\]/ig; | |
var name = reg.exec(text); | |
return name[1]; | |
} | |
function isStringGroup(groupName) { | |
return stringGroupReg.test(groupName); | |
} | |
} | |
function readNameValues(text) { | |
text = $.trim(text); | |
if (!text) { | |
return null; | |
} | |
var line, lines = text.split("\n"); | |
var arr = []; | |
for (var i = 0, len = lines.length; i < len; i++) { | |
line = lines[i]; | |
var data = readNameValue(line); | |
if (data) { | |
var key = data.name.toLowerCase(); | |
arr[key] = data; | |
} | |
} | |
return arr; | |
} | |
function readNameValue(line) { | |
var reg = /([^=]+)=([\s\S]*)/ig; | |
if (line) { | |
var match = reg.exec(line); | |
var data = { | |
name: $.trim(match[1]), | |
value: decodeURIComponent($.trim(match[2])) | |
}; | |
return data; | |
} | |
return null; | |
} | |
function init() { | |
readAICC(hacpData); | |
} | |
//#endregion | |
init(); | |
} | |
//#endregion | |
//#region setup | |
function setup() { | |
var aiccLaunchData = AICCViaHttpAPI.findLaunchData(window); | |
if (aiccLaunchData) { | |
var aiccApi = new AICCViaHttpAPI(aiccLaunchData); | |
window.AICCAPI = aiccApi; | |
aiccApi.LMSInitialize(""); | |
testGet(aiccApi, ["cmi.suspend_data", "cmi.core.student_id", "cmi.core.student_name", "cmi.core.lesson_status"]); | |
var result = aiccApi.LMSSetValue("cmi.suspend_data", "test cmi.suspend_data"); | |
console.log(result); | |
aiccApi.LMSFinish(""); | |
} | |
function testGet(api, names) { | |
for (var i = 0, len = names.length; i < len; i++) { | |
var name = names[i]; | |
var value = api.LMSGetValue(name); | |
console.log(name + "=" + value); | |
} | |
} | |
} | |
setup(); | |
//#endregion | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
test comment.