/
adapt-extension-savetoSheet.js
266 lines (221 loc) · 8.38 KB
/
adapt-extension-savetoSheet.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
define([
'coreJS/adapt'
], function(Adapt) {
var t; //reference to this
var dataToPost; //String: "course is complete", "assessment is passed", "assessmentLMS is passed"
var useMethod; //String: "ajaxBB", "ajaxJQ", "W3Sform", "modelCORS"
var SD1; //Submit Data 1
var SD2; //Submit Data 2
var FE1 = 'entry.119597203'; //form entry 1
var FE2 = 'entry.126901470'; //form entry 2
var submitRef = '&submit=-360594335577950894';
var FormURL = 'https://docs.google.com/forms/d/e/1FAIpQLSdGyTBv-jMf9b7z7sfFqXH3uchT0QXwaQ3rBwQ2uITXAVR5sg/formResponse?';
var SAVETOSHEET = _.extend({
PLUGIN_NAME: "_savetoSheet",
initialize: function() {
t = this;
t.listenToOnce(Adapt, {
"app:dataLoaded": t.setupListeners
});
},
setupListeners: function() {
t.listenToOnce(Adapt, "app:dataReady", t.setupModel);
},
setupModel: function() {
t.model = Adapt.course.get(t.PLUGIN_NAME);
dataToPost = t.model._dataToPost;
useMethod = t.model._useMethod;
if(dataToPost === "assessment is passed"){
Adapt.on("assessments:complete", t.quizDone); //with or without spoor enabled
}
else if(dataToPost === "assessmentLMS is passed"){
Adapt.on("assessment:complete", t.quizDone); //only with spoor enabled
}
else{
Adapt.course.on('change:_isComplete', t.courseDone); //all elements visited
}
},
courseDone: function(state){
SD1 = 'course';
SD2 = 'is completed';
t.onCompletion();
},
quizDone: function(state){
if(Boolean(state.isPass)){
SD1 = 'assessment';
SD2 = 'is passed';
t.onCompletion();
}
},
submitMyAjaxBB: function() {
var tempData = {
'entry.119597203': SD1,
'entry.126901470': SD2
}
Backbone.ajax(_.extend({
url: FormURL,
method: "POST",
contentType: "application/x-www-form-urlencoded", //sending type
data: tempData,
dataType: "html", //receiving type
statusCode: {
0: function () {
//console.log('statusCode: error');
},
200: function () {
//console.log('statusCode: OK');
}
},
complete: function() {
//console.log('complete: data has been posted');
t.pushMe();
}
}));
},
submitMyAjaxJQ: function() {
var tempData = {
'entry.119597203': SD1,
'entry.126901470': SD2
}
$.ajax({
url: FormURL,
contentType: "application/x-www-form-urlencoded;", //sending type
data: tempData,
type: "POST",
dataType: "html", //receiving type
statusCode: {
0: function () {
//console.log('statusCode: error');
},
200: function () {
//console.log('statusCode: OK');
}
},
complete: function() {
//console.log('complete: data has been posted');
t.pushMe();
}
});
},
submitMyW3Sform: function() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
//console.log("readyState4: request finished and response is ready");
t.pushMe();
if (this.status == 200) {
//console.log("status200: OK")
}
}
};
xhttp.open("POST", FormURL, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(FE1 + '=' + SD1 + '&' + FE2 + '=' + SD2 + submitRef);
},
submitMyModelCORS: function() {
var session = new Session();
session.set({
SavedData1: SD1,
SavedData2: SD2
});
session.submitData({
myCallback: t.onDataSuccess
});
},
onDataSuccess: function() {
t.pushMe();
},
onCompletion: function(){
switch(t.model._useMethod){
case 'ajaxBB': t.submitMyAjaxBB();
break;
case 'ajaxJQ': t.submitMyAjaxJQ();
break;
case 'W3Sform': t.submitMyW3Sform();
break;
case 'modelCORS': t.submitMyModelCORS();
break;
default: ;
}
},
pushMe: function(){
var pushObject = {
title: "Success!",
body: "You're data has been submitted.",
_timeout: 5000,
_callbackEvent: /*"pushNotify:clicked"*/ "" /*The _callbackEvent is triggered only if the push notification is clicked*/
};
Adapt.on('pushNotify:clicked', function() {
//console.log('A push notification was clicked');
});
Adapt.trigger('notify:push', pushObject);
}
}, Backbone.Events);
//
var Session = Backbone.Model.extend({
urlRoot: 'https://docs.google.com/forms/d/e/1FAIpQLSdGyTBv-jMf9b7z7sfFqXH3uchT0QXwaQ3rBwQ2uITXAVR5sg/formResponse?',
defaults: {
FormEntry1: 'entry.119597203',
FormEntry2: 'entry.126901470',
SubmitReference: '&submit=-360594335577950894',
CallbackOption: null,
SavedData1: null,
SavedData2: null
},
submitData: function(option) {
this.set('CallbackOption', option);
this.makeCorsRequest();
},
// Create the XHR object.
createCORSRequest: function(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
},
// Make the actual CORS request.
makeCorsRequest: function() {
var xhr = this.createCORSRequest('POST', this.urlRoot); //this.url() works, too
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
var tSession = this;
//When the request has successfully completed.
xhr.onload = function() {
//console.log('Response from CORS request');
};
//When the request has ended in success or failure. NO IE!
xhr.onloadend = function() {
//console.log('Request ended, with either success or failure.');
};
//When the request has failed.
xhr.onerror = function() {
//console.log('Woops, there was an error making the request.');
};
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
//console.log("readyState4: request finished and response is ready")
tSession.get('CallbackOption').myCallback();
if (this.status == 200) {
//console.log("status200: OK")
}
}
};
xhr.send(this.get('FormEntry1') + "=" + this.get('SavedData1') + "&" + this.get('FormEntry2') + "=" + this.get('SavedData2') + this.get('SubmitReference'));
}
});
SAVETOSHEET.initialize();
return SAVETOSHEET;
});