forked from davetaz/Learning_Strat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonline_API_wrapper.js
More file actions
237 lines (219 loc) · 6.27 KB
/
Copy pathonline_API_wrapper.js
File metadata and controls
237 lines (219 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var api_url = "https://odi-elearning.herokuapp.com/";
function setID() {
$.get( api_url + "create_id.php", function( data ) {
window.localStorage.setItem("_id",data);
})
.fail( function() {
setTimeout(function() {setID();},10000);
});
}
if (!localStorage.getItem("_id")) {
setID();
}
var moduleId = "";
var lang = ""
var config = {};
$.getJSON("course/config.json",function(data) {
moduleId = data._moduleId;
config = data;
});
var id = "";
$(document).ready(function() {
$.getJSON("course/config.json",function(data) {
moduleId = data._moduleId;
lang = data._defaultLanguage;
setRawValue("lang",lang);
if (moduleId == "ODI_nav"){
setInterval(function() {updateProgress();},5000);
}
});
setTimeout(function() {setRawValue("theme",theme)},1000);
});
function updateProgress() {
// var frame = document.getElementById('contentFrame').contentDocument;
for (i=1;i<13;i++) {
key = "ODI_" + i + "_cmi.suspend_data";
try {
value = localStorage.getItem(key);
data = $.parseJSON(value);
completion = data.spoor.completion;
total = completion.length;
complete = completion.match(/1/g || []).length;
percent = (complete/total) * 100;
document.getElementById('ODI_' + i).setAttribute('value',percent);
}
catch(err) {
}
}
}
function getModuleId() {
return moduleId;
}
function setSaveClass(toClass) {
// var frame = document.getElementById('contentFrame').contentDocument;
var sl = document.getElementById('save-section');
$(sl).addClass("saving");
$(sl).fadeIn();
$(sl).css('background-image','url(adapt/css/assets/' + toClass + '.gif)');
var ss = document.getElementById('cloud-status-text');
$(ss).html(config["_phrases"][toClass]);
var ssi = document.getElementById('cloud-status-img');
$(ssi).attr('src','adapt/css/assets/' + toClass + '.gif');
}
function updateRemote() {
var flag = localStorage.getItem("email");
if (flag) { setSaveClass('cloud_saving'); }
if(window.localStorage!==undefined) {
send = {};
send.data = JSON.stringify(localStorage);
$.ajax({
type: "POST",
url: api_url + "store.php",
data: send,
success: function(ret) {
d = new Date();
localStorage.setItem("ODI_lastSave",d.toString());
localStorage.setItem(moduleId+"_lastSave",d.toString());
if (flag) { setSaveClass('cloud_success'); }
},
error: function (xhr, ajaxOptions, thrownError) {
if (flag) { setSaveClass('cloud_failed'); }
}
});
} else {
}
}
function fetchRemote() {
if (typeof id == "undefined") {
return;
}
url = api_url + "load.php?id=" + id;
return $.getJSON( url , function() {
})
.done(function(data) {
var update = false;
if (localStorage.getItem("_id") != id) {
localStorage.clear();
update = true;
console.log("new data from remote");
} else {
lastGlobalSave = data["ODI_lastSave"];
localSave = localStorage.getItem("ODI_lastSave");
if (lastGlobalSave > localSave) {
update = true;
console.log("Updating data from remote");
}
}
if (update) {
$.each(data, function(key, value) {
localStorage.setItem(key,value);
});
window.location.href=location.protocol + '//' + location.host + location.pathname;
}
})
.fail(function() {
console.log("Failed to load data");
//window.location.href=location.protocol + '//' + location.host + location.pathname;
});
}
function getRawValue(cname) {
value = localStorage.getItem(cname);
if (value) return value;
return "";
}
function getValue(cname) {
module_id = getModuleId();
cname = module_id + "_" + cname;
value = localStorage.getItem(cname);
if (value) return value;
return "";
}
function setRawValue(cname,cvalue) {
localStorage.setItem(cname,cvalue);
setTimeout(function() {updateRemote();},2000);
}
function setValue(cname, cvalue) {
module_id = getModuleId();
cname = module_id + "_" + cname;
localStorage.setItem(cname,cvalue);
setTimeout(function() {updateRemote();},2000);
}
function setValueLocal(cname, cvalue) {
localStorage.setItem(cname,cvalue);
}
var QueryString = function () {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = decodeURIComponent(pair[1]);
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(decodeURIComponent(pair[1]));
}
}
return query_string;
}();
var API = {
LMSInitialize: function() {
this.data = {};
this.data["cmi.core.lesson_status"] = "not attempted";
var lesson_status = getValue("cmi.core.lesson_status");
if (lesson_status != "") {
this.data["cmi.core.lesson_status"] = lesson_status;
}
this.data["cmi.suspend_data"] = getValue("cmi.suspend_data");
return true;
},
LMSFinish: function() {
return "true";
},
LMSGetValue: function(key) {
// window.console && console.log('LMSGetValue("' + key + '") - ' + this.data[key]);
if (typeof this.data[key] == "undefined") {
localValue = getValue(key);
this.data[key] = localValue;
return localValue;
} else {
return this.data[key];
}
},
LMSSetValue: function(key, value) {
// window.console && console.log('LMSSetValue("' + key + '") - ' + value);
this.data[key] = value;
setValue(key,value,365);
return "true";
},
LMSCommit: function() {
return "true";
},
LMSGetLastError: function() {
return 0;
},
LMSGetErrorString: function() {
return "Fake error string.";
},
LMSGetDiagnostic: function() {
return "Fake diagnostic information."
}
}
id = QueryString.id;
if (typeof id != "undefined") {
fetchRemote();
} else if (!localStorage.getItem("_id")) {
$.get( api_url + "create_id.php", function( data ) {
window.localStorage.setItem("_id",data);
});
} else {
id = localStorage.getItem("_id");
fetchRemote();
}