forked from davetaz/Learning_Strat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffline_API_wrapper.js
More file actions
205 lines (190 loc) · 5.4 KB
/
offline_API_wrapper.js
File metadata and controls
205 lines (190 loc) · 5.4 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
var api_url = "https://odi-elearning.herokuapp.com/";
if (!localStorage.getItem("_id")) {
$.get( api_url + "create_id.php", function( data ) {
window.localStorage.setItem("_id",data);
});
}
var lang = ""
$.getJSON("course/config.json",function(data) {
moduleId = data._moduleId;
lang = data._defaultLanguage;
setRawValue("lang",lang);
});
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() {
if (typeof theme !== "undefined") {
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;
frame.getElementById('ODI_' + i).setAttribute('value',percent);
}
catch(err) {
}
}
}
function getModuleId() {
return moduleId;
}
function setSaveClass(toClass) {
var frame = document.getElementById('contentFrame').contentDocument;
var sl = frame.getElementById('save-section');
$(sl).addClass('saving');
$(sl).fadeIn();
$(sl).css('background-image','url(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(moduleId+"_lastSave",d.toString());
if (flag) { setSaveClass('cloud_success'); }
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(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) {
localStorage.clear;
$.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;
}();
id = QueryString.id;
fetchRemote();
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."
}
}