forked from adlnet/xapi-jqm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
410 lines (340 loc) · 11.2 KB
/
functions.js
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* in progress */
// Global Actor
actor = getActor();
/* Page Change Logic */
if ( actor == false ) {
checkLoggedIn();
} else { // silly thing to wrap in an else but I need to restructure the code to handle a missing actor on login page
doConfig();
// Handle chapter clicks to send launch statements
$( document ).on("vclick", "a.chapter", function() {
$chapter = $(this);
var chapter = $chapter.parent("li").attr("id");
var name = $chapter.text();
chapterLaunched(chapter, name);
});
// Abstracted page changing logic -- catch-all
$( window ).on("pagechange", function(event) {
var chapter = $("body").attr("data-chapter");
var pageID = $.mobile.activePage.attr("id");
var activityID = moduleID + chapter + "/" + pageID;
var context = createContext(chapter);
var stmt = {
"actor": actor,
"verb": ADL.custom.verbs.read,
"context": context,
"object": {
"id" : activityID,
"objectType": "Activity",
"definition": {
"name": {
"en-US": moduleName + ": " + chapter + ", page: " + pageID
}
}
}
};
// Send a statement
ADL.XAPIWrapper.sendStatement(stmt);
ADL.XAPIWrapper.sendState(moduleID, actor, "session-state", null, { "info": "reading", "chapter": chapter, "page": pageID });
});
} // end silly else
/* State functions */
function getState() {
return ADL.XAPIWrapper.getState(moduleID, actor, "session-state");
}
/* Course Progress */
// Get from State API
function getChaptersCompleted() {
var chaptersCompleted = ADL.XAPIWrapper.getState(moduleID, actor, "chapters-completed");
return chaptersCompleted.chapters;
}
// Set in State API
function setChapterComplete() {
var chapterID = $("body").attr("data-chapter");
var currentCompletedChapters = getChaptersCompleted();
var chapterCompleted = [ chapterID ];
var hash = {}, union = [];
// #thatHappened
$.each($.merge($.merge([], currentCompletedChapters), chapterCompleted), function (index, value) { hash[value] = value; });
$.each(hash, function (key, value) { union.push(key); } );
ADL.XAPIWrapper.sendState(moduleID, actor, "chapters-completed", null, { "chapters": union });
doConfig();
// statement for launching content
var stmt = {
"actor": actor,
"verb": ADL.verbs.completed,
"context": createContext(),
"object": {
"id": moduleID + chapterCompleted,
"objectType": "Activity",
"definition": {
"name": {
"en-US": moduleName + ": " + chapterCompleted
}
}
}
};
// Send chapterComplete statement
ADL.XAPIWrapper.sendStatement(stmt);
}
/* Helpers */
function doConfig() { // sorry
Config.actor = actor;
ADL.XAPIWrapper.changeConfig(Config);
}
function getPage() {
var url = window.location.pathname;
var filename = url.substring(url.lastIndexOf('/')+1);
return filename;
}
/* Name, Email, Actor, gets and sets */
// Actor
function getActor() {
var name = localStorage.getItem(storageKeyName);
var email = localStorage.getItem(storageKeyEmail);
if ( name == null || email == null ) {
return false;
} else {
var actor = { "mbox": "mailto:" + email, "name": name };
return actor;
}
}
function setActor( name, email ) {
setUserName(name);
setUserEmail(email);
}
// Name
function getUserName() {
return localStorage.getItem(storageKeyName);
}
function setUserName(name) {
localStorage.setItem(storageKeyName, name);
}
// Email
function getUserEmail() {
return localStorage.getItem(storageKeyEmail);
}
function setUserEmail(email) {
localStorage.setItem(storageKeyEmail, email);
}
// Destroy all the things
function clearActor() {
localStorage.removeItem(storageKeyName);
localStorage.removeItem(storageKeyEmail);
}
/* Login / Logout functions */
function checkLoggedIn() {
// If the actor doesn't exist, send them to the login page
if ( getPage() != "00-account.html" ) {
userLogin();
}
}
/*
function getBaseURL() {
// silly regex hack for now #helpWanted
var regex = new RegExp("(index.html|.*\/chapters\/.*|.*\/glossary.html)");
var location = window.location.href;
if ( regex.test(location) ) {
var str = location.split("/").pop();
var baseurl = location.replace(str, "");
var str = "chapters/"
var baseurl = baseurl.replace(str, "");
} else {
// otherwise give up and send them to the github version
var baseurl = "http://adlnet.github.io/xapi-jqm/demos/course/";
}
return baseurl;
}*/
function userLogin() {
// Should get the page root
window.location = "chapters/00-account.html#login";
}
function userLogout() {
courseExited();
clearActor();
window.location = "../"; // lol
}
function userRegister( name, email ) {
// should error check this
setActor(name, email);
// Set global actor var so other functions can use it
actor = getActor();
courseRegistered();
// Setup chapters-complete
ADL.XAPIWrapper.sendState(moduleID, actor, "chapters-completed", null, { "chapters": [] });
}
// jqm's submission process is the reason I'm doing it this way
function userRegisterSubmit() {
if ( $("#reg-name").val() != "" && $("#reg-email").val() != "" ) {
userRegister($("#reg-name").val(), $("#reg-email").val());
courseLaunched();
window.location = "../index.html"
}
}
/*
* xAPIy
*/
function checkboxClicked(chapter, pageID, checkboxID, checkboxName) {
doConfig();
// Figure out if it was checked or unchecked
var isChecked = $("#"+checkboxID).prop('checked');
var checkedVerb = (isChecked) ? ADL.custom.verbs.checked : ADL.custom.verbs.unchecked;
var baseActivity = {
"id": moduleID + "/" + chapter + "/" + pageID + "#" + checkboxID,
"definition": {
"name": {
"en-US": checkboxName
},
"description": {
"en-US": "The " + checkboxName + " checkbox from chapter " + chapter + "; page " + pageID
}
},
"objectType": "Activity"
};
// statement for checking content
var stmt = {
"actor": actor,
"verb": checkedVerb,
"object": baseActivity,
"context": createContext(chapter, pageID, undefined, true)
};
// Send statement
ADL.XAPIWrapper.sendStatement(stmt);
}
/*
* SCORMy
*/
function courseRegistered() {
doConfig();
// statement for launching content
var stmt = {
"actor": actor,
"verb": ADL.verbs.registered,
"object": baseActivity
};
// Send registered statement
ADL.XAPIWrapper.sendStatement(stmt);
}
function courseLaunched() {
doConfig();
// statement for launching content
var stmt = {
"actor": actor,
"verb": ADL.verbs.launched,
"object": baseActivity
};
// Send launched statement
ADL.XAPIWrapper.sendStatement(stmt);
}
function chapterLaunched(chapter, name) {
var activityID = moduleID + chapter;
var stmt = {
"actor": actor,
"verb": ADL.verbs.launched,
"context": createContext(),
"object": {
"id": activityID,
"objectType": "Activity",
"definition": {
"name": {
"en-US": moduleName + ": " + chapter
}
}
}
};
// Send a statement
ADL.XAPIWrapper.sendStatement(stmt);
}
function courseMastered() {
doConfig();
// statement for launching content
var stmt = {
"actor": actor,
"verb": ADL.verbs.mastered,
"object": baseActivity
};
// Send launched statement
ADL.XAPIWrapper.sendStatement(stmt);
}
function courseExited() {
doConfig();
// statement for launching content
var stmt = {
"actor": actor,
"verb": ADL.verbs.exited,
"object": baseActivity
};
// Send exited statement
ADL.XAPIWrapper.sendStatement(stmt);
}
// supply the chapter, the page, and any sub-activity in that chapter and page. add both if you want the parentChapter activity
// added as a separate activity in the context from the parentChapter/parentPage activity
function createContext( parentChapter, parentPage, subParentActivity, both ) {
var baseContext = {
"contextActivities": {
"parent": [
baseActivity
]
}
};
// set both
if ( typeof both === "undefined") {
both = false;
}
// if parent chapter make the chapterActivity
if ( typeof parentChapter !== "undefined" ) {
var chapterActivity = {
"id": moduleID + parentChapter,
"definition": {
"name": {
"en-US": moduleName + ": " + parentChapter
}
},
"objectType": "Activity"
};
// if parent page and don't want both, just append the parent page to the end of the parentChapter activity
if ( typeof parentPage !== "undefined" && !both ) {
chapterActivity["id"] = chapterActivity["id"] + "/" + parentPage;
chapterActivity["definition"]["name"]["en-US"] = chapterActivity["definition"]["name"]["en-US"] + ", page: " + parentPage;
}
// else they want both
else if ( typeof parentPage !== "undefined" && both ) {
var chapterParentActivity = {
"id": moduleID + parentChapter + "/" + parentPage,
"definition": {
"name": {
"en-US": moduleName + ": " + parentChapter + ", page: " + parentPage
}
},
"objectType": "Activity"
};
baseContext.contextActivities.parent.push(chapterParentActivity);
}
baseContext.contextActivities.parent.push(chapterActivity);
// if there is a sub activity, add it
if ( typeof subParentActivity !== "undefined" ) {
var subActivity = {
"id": moduleID + parentChapter + "/" + parentPage + "#" + subParentActivity,
"definition": {
"name": {
"en-US": moduleName + ": " + parentChapter + ", page: " + parentPage + " " + subParentActivity
}
},
"objectType": "Activity"
};
baseContext.contextActivities.parent.push(subActivity);
}
}
return baseContext;
}
$( document ).ready(function() {
// Handle checkbox clicks -- basic no knowledge of context or checked
$(":checkbox").change(function(event) {
$checkbox = $(this);
var checkboxID = $checkbox.attr("id");
var checkboxName = $checkbox.siblings("label").text();
var chapter = $("body").attr("data-chapter");
var pageID = $.mobile.activePage.attr("id");
checkboxClicked(chapter, pageID, checkboxID, checkboxName);
});
});