-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquizlyio.ec6.js
266 lines (197 loc) · 7.59 KB
/
quizlyio.ec6.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
/*
* Quizlyio
* Simple Javascript library for embedding interactive quizes.
* By @joshwayman for @compassionau
*
* Currently works only for radio button questions.
* The name of the inputs needs to be q + the question number.
*/
class Quizlyio {
/*
Create a new quiz requires one param
- Parent container element Id (string)
*/
constructor(elId) {
var container = document.getElementById(elId);
var questions = container.getElementsByClassName('quizlyio-question');
var quizJson = {};
for ( var i = 0; i < questions.length; i++ ) {
var question = questions[i];
var dataset = question.dataset;
var q = "q" + dataset.quizlyioQuestion;
quizJson[q] = {};
quizJson[q].answer = dataset.quizlyioAnswer;
quizJson[q].response = null;
quizJson[q].correct = null;
}
this.el = {};
this.el.container = container;
this.el.id = elId;
this.el.questions = questions;
this.data = {};
this.data.quizLength = questions.length;
this.data.quizJson = quizJson;
this.AddClickListeners();
}
CompleteQuiz() {
var quizJson = this.data.quizJson;
var numberCorrect = 0;
//console.log(quizJson);
//console.log(this.data.quizLength);
for (var i = 0; i < this.data.quizLength; i++) {
var n = i+1,
q = "q"+n;
//console.log(q);
//console.log(quizJson[q]);
//this.CheckAnswer(n);
if(quizJson[q].correct == true) {
numberCorrect += 1;
}
else if(quizJson[q].correct == null) {
// Throws an error if you haven't answered all questions
this.HighlightQuestion(n);
return;
}
}
this.data.numberCorrect = numberCorrect;
var percent = numberCorrect/ this.data.quizLength * 100;
this.data.percentCorrect = percent;
document.getElementById('quizlyio-result').style.display="block";
document.getElementById('quizlyio-score').innerHTML=numberCorrect;
document.getElementById('quizlyio-max-score').innerHTML=this.data.quizLength;
//document.getElementById('quizlyio-percent').innerHTML=percent+"%";
if ( percent >= 80 ) {
document.getElementById('quizlio-score-80').style.display="block";
} else if ( percent >= 50 ) {
document.getElementById('quizlio-score-50').style.display="block";
} else {
document.getElementById('quizlio-score-40').style.display="block";
}
dataLayer.push({
event : 'ga-event',
eventCategory : 'Quiz',
eventLabel : 'Completed',
eventAction : percent,
});
return percent +"%";
}
HighlightQuestion(questionNumber) {
console.log("didn't answer question " + questionNumber);
return;
}
AddClickListeners() {
var container = this.el.container
var inputs = container.getElementsByTagName('input');
var that = this;
for(var i=0; i < inputs.length; i++) {
var input = inputs[i];
input.onclick = function() {
var q = this.name.split("q")[1];
//console.log(q);
that.CheckAnswer(q);
return;
}
}
}
RelvealAnswer(questionNumber) {
var question = this.el.questions[questionNumber-1];
// This could be done nicer and a bit more robustly.
question.getElementsByClassName('quizlyio-question-reveal')[0].classList.add('quizlyio-show');
}
/*
Method to check the answer to a question. This is referred to by all other methods.
Will reveal the response below by default.
*/
CheckAnswer(questionNumber) {
if (
questionNumber == 0 ||
questionNumber > this.data.quizLength )
{
return false;
}
var q = "q"+questionNumber,
radios = document.getElementsByName(q);
var status;
for ( var i = 0; i < radios.length; i++ ) {
if ( radios[i].checked ) {
var response = radios[i].value;
this.data.quizJson[q].response = response;
var ans = this.data.quizJson[q].answer;
if (response == ans) {
//radios[i].parentElement.classList.add('quizlyio-correct');
status = true;
} else {
//radios[i].parentElement.classList.add('quizlyio-wrong');
status = false;
}
this.HandleResponse(questionNumber, status);
this.data.quizJson[q].correct = status;
break;
}
}
if(this.CheckCompletionStatus()) {
this.CompleteQuiz();
}
return status;
}
CheckCompletionStatus() {
var quizJson = this.data.quizJson;
for(var i = 0; i < this.data.quizLength; i++) {
var n = i+1,
q = "q"+n;
if(quizJson[q].response == null) {
return false;
}
}
return true;
}
HandleResponse(questionNumber, response) {
var quizJson = this.data.quizJson;
var question = quizJson["q"+questionNumber];
var resp = question.response;
var answ = question.answer;
var corr = question.correct;
var questionEl = this.el.questions[questionNumber-1],
inputs = questionEl.getElementsByTagName('input');
for ( var i = 0; i < inputs.length; i++ ) {
var input = inputs[i],
val = input.value;
//console.log( "Resp = " + resp + " : Answ = " + answ + " : Corr = " + corr + " : VAL = " + val );
// If val is the answer make it correct
if ( val == answ ) {
input.parentElement.classList.add('quizlyio-correct');
} else if ( val == resp ) {
input.parentElement.classList.add('quizlyio-wrong');
} else if ( corr ) {
input.parentElement.classList.add('quizlyio-correct');
}
}
this.RelvealAnswer(questionNumber);
}
}
var quizlyio;
$(function(){
quizlyio = new Quizlyio('quizlyio-container');
});
/*
* social_media_sharing.js
*
* Adapted from:
* https://jonsuh.com/blog/social-share-links/
*
**/
function windowPopup(url, width, height) {
// Calculate the position of the popup so
// it’s centered on the screen.
var left = (screen.width / 2) - (width / 2),
top = (screen.height / 2) - (height / 2);
window.open(
url,
"",
"menubar=no,toolbar=no,resizable=yes,scrollbars=yes,width=" + width + ",height=" + height + ",top=" + top + ",left=" + left
);
}
$(".js-social-share > a").on("click", function(e) {
e.preventDefault();
windowPopup($(this).attr("href"), 500, 300);
});