-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrokeready-xapi.js
More file actions
197 lines (180 loc) · 6.04 KB
/
Copy pathstrokeready-xapi.js
File metadata and controls
197 lines (180 loc) · 6.04 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
// Configure a TinCan instance (using TinCanJS from Rustici)
//------------------------------------------//
var tincan = new TinCan({
recordStores: [{
endpoint: "[endpoint]",
auth: "[Basic Base64 auth]",
allowFail: false
}],
context: { registration: "[XXXXX]",
platform: deviceName
}
});
TinCan.DEBUG = true;
// Get, assign, and/or increment User
// --------------------------------------------//
var username = "User " + localStorage.userCount,
email = "[predetermined email goes here]";
localStorage["Username"] = username;
localStorage["Email"] = email;
function getUser() {
if (!localStorage.userCount) {
localStorage.userCount = 1;
}
username = "User " + localStorage.userCount;
localStorage["Username"] = username;
}
function incrementUser() {
localStorage.userCount = Number(localStorage.userCount)+1;
}
// Get ISO timestamp
// ----------------------------//
function getTimestamp() {
date = new Date();
ISOtimestamp = date.toISOString();
}
// Start Time Stmt
// -----------------------------------------------------------//
function sendStartTimeStmt() {
getTimestamp();
startTime = moment(date).format('MMMM Do YYYY, h:mm:ss a');
localStorage["startTime - " + username] = ISOtimestamp;
var xAPIstartTime = {
actor: { name: username,
mbox: "mailto:" + email
},
verb: { id: "http://adlnet.gov/expapi/verbs/launched",
display: {und: "launched"}
},
target: { id: "http://www.uofmhealth.org/medical-services/stroke/startTime",
definition: {
name: {und: "Stroke Ready App at " + date},
description: {und: startTime},
type: "http://activitystrea.ms/schema/1.0/application"
}
},
result: {completion: true},
timestamp: ISOtimestamp
},
startTimeString = JSON.stringify(xAPIstartTime);
localStorage["xAPIstmt-Start-Time - " + username] = startTimeString;
}
// Guide Choice Stmt
// ---------------------------------------------------------///
function guideChoiceStmt() {
getTimestamp();
guideID = localStorage.getItem("Guide Choice ID - " + username);
switch (guideID) {
case "guide1":
case "guide2": guideDescr = "Hispanic"; break;
case "guide3":
case "guide4": guideDescr = "Mixed Race"; break;
case "guide5":
case "guide6": guideDescr = "African-American"; break;
case "guide7":
case "guide8": guideDescr = "Caucasian"; break;
}
var guideGenderLowerCase = localStorage.getItem("Voice Gender"),
guideGender = guideGenderLowerCase[0].toUpperCase() + guideGenderLowerCase.substring(1);
var xAPIguideChoice = {
actor: { name: username,
mbox: "mailto:" + email
},
verb: { id: "http://adlnet.gov/expapi/verbs/experienced",
display: {und: "chose"}
},
target: { id: "http://www.uofmhealth.org/medical-services/stroke/guideID",
definition: {
name: {und: guideID},
description: {und: guideDescr},
type: "http://activitystrea.ms/schema/1.0/application"
}
},
result: { completion: true,
extensions: {
"http://www.uofmhealth.org": {
"gender": guideGender
}
}
},
timestamp: ISOtimestamp
},
guideChoiceString = JSON.stringify(xAPIguideChoice);
localStorage["xAPIstmt-Guide-Choice - " + username] = guideChoiceString;
}
// Tin Can statementS sent to LRS and localStorage cleared
// -------------------------------------------->>>>>>
var i, tincanStmtsString, tincanStmtsJSON, userCountVar, tincanStmtsArray = [], storageLength = localStorage.length, err, xhr;
function sendAllStatements() {
sendQueuedStatements();
storageLength = localStorage.length;
for (var i = storageLength-1; i >= 0; i--){
if (localStorage.key(i).indexOf("xAPIstmt") >= 0) {
tincanStmtsString = localStorage.getItem(localStorage.key(i));
tincanStmtsJSON = JSON.parse(tincanStmtsString);
tincanStmtsArray.push(tincanStmtsJSON);
console.log(i + " of " + storageLength + " added to array - " + localStorage.key(i));
localStorage.removeItem(localStorage.key(i));
}
}
tincan.sendStatements(tincanStmtsArray, oneArrayCallback);
}
function oneArrayCallback(statusResults,finalStmt) {
var sentStatus = statusResults[0].xhr.status;
if (sentStatus === 0 || sentStatus === 400) {
console.log("FAIL: sentStatus = " + sentStatus);
clearAllStorageSaveArrays();
tincanStmtsArrayString = JSON.stringify(tincanStmtsArray);
localStorage.setItem("xAPIarray - " + username, tincanStmtsArrayString);
}
else {
console.log("SUCCESS: sentStatus = " + sentStatus);
clearAllStorage();
};
//console.log(finalStmt);
}
function sendQueuedStatements() {
storageLength = localStorage.length;
for (var i = storageLength-1; i >= 0; i--){
if (localStorage.key(i).indexOf("xAPIarray") >= 0) {
tincanArrayString = localStorage.getItem(localStorage.key(i));
tincanArrayJSON = JSON.parse(tincanArrayString);
tincan.sendStatements(tincanArrayJSON, queuedArrayCallback);
}
}
}
function queuedArrayCallback(statusResults,finalStmt) {
userCountVar=localStorage.getItem("userCount");
var sentStatus = statusResults[0].xhr.status;
if (sentStatus === 0 || sentStatus === 400) {
console.log("FAILED TO SEND QUEUED ARRAYS: sentStatus = " + sentStatus);
}
else {
console.log("SUCCESSFULLY SENT QUEUED ARRAY: sentStatus = " + sentStatus);
localStorage.removeItem(localStorage.key(i));
};
localStorage.userCount=userCountVar;
}
function clearAllStorageSaveArrays() {
storageLength = localStorage.length;
userCountVar=localStorage.getItem("userCount");
deviceID=localStorage.getItem("deviceID");
for (var i = storageLength-1; i >= 0; i--){
if (localStorage.key(i).indexOf("xAPIarray") >= 0) {
console.log("Array saved from deletion = " + localStorage.key(i));
}
else {
localStorage.removeItem(localStorage.key(i));
}
}
localStorage.userCount=userCountVar;
localStorage.deviceID=deviceID;
}
function clearAllStorage() {
userCountVar=localStorage.getItem("userCount");
deviceID=localStorage.getItem("deviceID");
localStorage.clear();
console.log("Storage cleared, except for userCount and deviceID");
localStorage.userCount=userCountVar;
localStorage.deviceID=deviceID;
}