forked from sung/CamObsGynCSF2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz.js
executable file
·433 lines (356 loc) · 11.4 KB
/
quiz.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*
Quick quiz bootstrap extension
*/
;(function($) {
// onload modal screen by Sung
var stallAttend=0;
$(window).load(function(){
swal({
title: "Have you looked at our stall yet?",
type: "info",
showCancelButton: true,
confirmButtonText: "Yes",
cancelButtonText: "No",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
stallAttend=1;
swal("Thanks!", "Enjoy the quiz.", "success");
} else {
// swal("No problem!", "Please visit our stall after having the quiz :)", "error");
swal("No problem!", "Please visit our stall after having the quiz :)", "success");
}
});
});
// keep track of number of quizes added to page
var quiz_count = 0;
// add jQuery selection method to create
// quiz structure from question json file
// "filename" can be path to question json
// or javascript object
$.fn.quiz = function(filename) {
if (typeof filename === "string") {
$.getJSON(filename, render.bind(this));
} else {
render.call(this, filename);
}
};
// create html structure for quiz
// using loaded questions json
function render(quiz_opts) {
// list of questions to insert into quiz
var questions = quiz_opts.questions;
// keep track of the state of correct
// answers to the quiz so far
var state = {
correct : 0,
total : questions.length
};
var $quiz = $(this)
.attr("class", "carousel slide")
.attr("data-ride", "carousel");
// unique ID for container to refer to in carousel
var name = $quiz.attr("id") || "urban_quiz_" + (++quiz_count);
$quiz.attr('id', name);
var height = $quiz.height();
/*
Add carousel indicators
*/
/*
Slides container div
*/
var $slides = $("<div>")
.attr("class", "carousel-inner")
.attr("role", "listbox")
.appendTo($quiz);
/*
Create title slide
*/
var $title_slide = $("<div>")
.attr("class", "item active")
.attr("height", height + "px")
.appendTo($slides);
$('<h1>')
.text(quiz_opts.title)
.attr('class', 'quiz-title')
.appendTo($title_slide);
$('<p>')
.text("published on March 22, 2020")
.attr('class', 'published-on-date')
.appendTo($title_slide);
$('<img>')
.attr('src','img/SPROG_logo.png')
.css("width","300px")
.css("margin","0 auto")
// .attr('class', 'quiz-title')
.appendTo($title_slide);
$('<p>')
.text("Cover photo by Sprog.")
.attr("class", "image-credit")
.appendTo($title_slide);
var $start_button = $("<div>")
.attr("class", "quiz-answers")
.css("margin-bottom","50px")
.appendTo($title_slide);
var $dummy2 = $("<div>")
.text("a")
.css("color","white")
.appendTo($title_slide)
var $indicators = $('<ol>')
.attr('class', 'progress-circles')
var $dummy = $("<div>")
.text("a")
.css("color","white")
$("<button>")
.attr('class', 'start-quiz quiz-button btn')
.text("Take the quiz!")
.click(function() {
$quiz.carousel('next');
$indicators.addClass('show');
$(".active .quiz-button.btn").each(function(){
console.log(this.getBoundingClientRect())
$(this).css("margin-left", function(){
return ((250 - this.getBoundingClientRect().width) *0.5) + "px"
})
})
})
.appendTo($start_button);
$indicators
.appendTo($quiz);
$dummy
.appendTo($quiz)
$.each(questions, function(question_index, question) {
$('<li>')
.attr('class', question_index ? "" : "dark")
.appendTo($indicators);
});
/*
Add all question slides
*/
$.each(questions, function(question_index, question) {
var last_question = (question_index + 1 === state.total);
var $item = $("<div>")
.attr("class", "item")
.attr("height", height + "px")
.appendTo($slides);
var $img_div;
if (question.image) {
$img_div = $('<div>')
.attr('class', 'question-image')
.appendTo($item);
$("<img>")
.attr("class", "img-responsive")
.attr("src", question.image)
.appendTo($img_div);
$('<p>')
.text(question.image_credit)
.attr("class", "image-credit")
.appendTo($img_div);
}
$("<div>")
.attr("class", "quiz-question")
.html(question.prompt)
.appendTo($item);
var $answers = $("<div>")
.attr("class", "quiz-answers")
.appendTo($item);
// if the question has an image
// append a container with the image to the item
// for each possible answer to the question
// add a button with a click event
$.each(question.answers, function(answer_index, answer) {
// create an answer button div
// and add to the answer container
var ans_btn = $("<div>")
.attr('class', 'quiz-button btn')
.html(answer)
.appendTo($answers);
// This question is correct if it's
// index is the correct index
var correct = (question.correct.index === answer_index);
// default opts for both outcomes
var opts = {
allowOutsideClick : true,
allowEscapeKey : false,
confirmButtonText: "Next Question",
html : true,
confirmButtonColor: "#0096D2"
};
// set options for correct/incorrect
// answer dialogue
if (correct) {
opts = $.extend(opts, {
title: "Nice",
text: "Well done!" + (
question.correct.text ?
("<div class=\"correct-text\">" +
question.correct.text +
"</div>"
) : ""),
type: "success"
});
} else {
opts = $.extend(opts, {
title: "Hmm",
text: (
"Nope, not quite right!<br/><br/>" +
"The correct answer was \"" +
question.answers[question.correct.index] + ".\"" + (
question.correct.text ?
("<div class=\"correct-text\">" +
question.correct.text +
"</div>"
) : "")
),
type: "error"
});
}
if (last_question) {
opts.confirmButtonText = "See your results";
}
// bind click event to answer button,
// using specified sweet alert options
ans_btn.on('click', function() {
function next() {
$('html,body').delay( 500 ).animate({ scrollTop: 0 }, 'slow');
// if correct answer is selected,
// keep track in total
if (correct) state.correct++;
$quiz.carousel('next');
// if we've reached the final question
// set the results text
if (last_question) {
$results_title.html(resultsText(state));
$results_ratio.text(
"You got " +
Math.round(100*(state.correct/state.total)) +
"% of the questions correct!"
);
$twitter_link.attr('href', tweet(state, quiz_opts));
$facebook_link.attr('href', facebook(state, quiz_opts));
$indicators.removeClass('show');
// indicate the question number
$indicators.find('li')
.removeClass('dark')
.eq(0)
.addClass('dark');
// below to store the final score to google firestore
var newScore={
target: quiz_opts.title,
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
attend: stallAttend,
score: state.correct
};
// below from Sung via google firestore
var db = firebase.firestore();
var newScoreRef = db.collection("quiz_score").doc();
newScoreRef.set(newScore);
} else {
// indicate the question number
$indicators.find('li')
.removeClass('dark')
.eq(question_index+1)
.addClass('dark');
}
// unbind event handler
$('.sweet-overlay').off('click', next);
} // end of next()
// advance to next question on OK click or
// click of overlay
swal(opts, next);
$('.sweet-overlay').on('click', next);
}); // event of clicking answer
}); // end of each answers
}); // end of each questions
// final results slide
var $results_slide = $("<div>")
.attr("class", "item")
.attr("height", height + "px")
.appendTo($slides);
var $results_title = $('<h1>')
.attr('class', 'quiz-title')
.appendTo($results_slide);
var $results_ratio = $('<div>')
.attr('class', 'results-ratio')
.appendTo($results_slide);
var $restart_button = $("<div>")
.attr("class", "quiz-answers")
.appendTo($results_slide);
var $links = $("<div>")
.attr('class', 'results-social')
.html('<div id = "links-text">To find out more, read <a href="https://www.tommys.org/pregnancy-information/im-pregnant/early-pregnancy/10-pregnancy-myths"><em>this article</em></a> for a pregancy, <a href="https://www.nhs.uk/conditions/pregnancy-and-baby/ultrasound-anomaly-baby-scans-pregnant/"><em>this NHS website</em></a> for an untrasound scan, and <a href="https://www.bbc.co.uk/bitesize/guides/z9fgr82/revision/5"><em>this BBC site</em></a> for fetal reproduction and birth.</div>')
.appendTo($results_slide);
var $social = $("<div>")
.attr('class', 'results-social')
.html('<div id = "social-text">Did you like the quiz? Share your results with your friends, so they can give it a shot!</div>')
.appendTo($results_slide);
var $twitter_link = $('<a>')
.html('<span class="social social-twitter follow-tw"></span>')
.appendTo($social);
var $facebook_link = $('<a>')
.html('<span class="social social-facebook follow-fb"></span>')
.appendTo($social);
$social.css("margin-bottom","100px")
var $dummy3 = $("<div>")
.text("a")
.css("color","white")
.appendTo($results_slide)
$("<button>")
.attr('class', 'quiz-button btn')
.text("Try again?")
.click(function() {
state.correct = 0;
$quiz.carousel(0);
})
.appendTo($restart_button);
$quiz.carousel({
"interval" : false
});
$(window).on('resize', function() {
$quiz.find(".item")
.attr('height', $quiz.height() + "px");
});
} // end of render()
function resultsText(state) {
var ratio = state.correct / state.total;
var text;
switch (true) {
case (ratio === 1):
text = "Wow—perfect score!";
break;
case (ratio >= 0.9):
text = "Awesome job, you got most of them right.";
break;
case (ratio >= 0.60):
text = "Pretty good; we'll say that's a pass.";
break;
case (ratio >= 0.5):
text = "Well, at least you got half of them right…";
break;
case (ratio < 0.5 && ratio !== 0):
text = "Looks like this was a tough one; better luck next time.";
break;
case (ratio === 0):
text = "Yikes, none correct. Well, maybe it was rigged?";
break;
}
return text;
}
function tweet(state, opts) {
var body = (
"I got " + state.correct +
" out of " + state.total +
" on @sunggong & @ObsGynaeCam’s \"" + opts.title +
"\" quiz. Test your knowledge here: " + opts.url
);
return (
"http://twitter.com/intent/tweet?text=" +
encodeURIComponent(body)
);
}
function facebook(state, opts) {
return "https://www.facebook.com/sharer/sharer.php?u=" + opts.url;
}
})(jQuery);