forked from Petrosz007/jog_quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz.js
452 lines (355 loc) · 12.3 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
let data
let og_data
let h_data = {}
let curr_question
let curr_question_id
let curr_answered = false
let answered_questions = {}
let num_good_ans = 0
let num_ans = 0
let quiz_complete = false
let shuffleQuestions = false
let og_endscreen = ""
let og_start = ""
let og_quiz = ""
document.addEventListener('keydown', event => {
if(!document.getElementById("start").classList.contains("hidden")) // quiz not started yet
return
const keyName = event.key;
const answerOptions = document.getElementById("answer-options").children
switch (keyName) {
case "1":
answerOptions[0].onclick()
break
case "2":
answerOptions[1].onclick()
break
case "3":
answerOptions[2].onclick()
break
case "4":
answerOptions[3].onclick()
break
case " ":
case "Enter":
next()
break
default:
break
}
})
isGoodAns = answer => {
if(!curr_answered) {
changeBgClass("#ans-"+answer, "btn-danger")
changeBgClass("#ans-"+curr_question.answer, "btn-success")
curr_answered = true
changeNextBtnState(false)
curr_question.my_answer = answer
if(curr_question.answer == answer) num_good_ans++
num_ans++
}
}
next = () => {
if(curr_answered) {
answered_questions[curr_question_id] = curr_question
init_question()
}
}
displayCurrQuestion = () => {
if(document.getElementById("random-answer-order").checked) {
const answerOptions = document.getElementById("answer-options").children
let shuffledAns = shuffle(answerOptions)
document.getElementById("answer-options").innerHTML = ""
shuffledAns.forEach(e => document.getElementById("answer-options").innerHTML += e.outerHTML)
}
$('#question').html(curr_question.question)
$('#ans-a').html(curr_question.a)
$('#ans-b').html(curr_question.b)
$('#ans-c').html(curr_question.c)
$('#ans-d').html(curr_question.d)
$('#num-good-ans').html(num_good_ans)
$('#num-ans').html(num_ans)
if(num_ans > 0)
$('#num-percentage').html(parseFloat(num_good_ans / num_ans * 100).toFixed(2))
$('#num-remaining').html(Object.keys(data).length)
}
changeNextBtnState = new_state => {
$("#nextBtn").prop("disabled", new_state)
if(new_state) {
$("#nextBtn").attr("disabled", true)
$("#nextBtn").addClass("btn-secondary").removeClass("btn-info")
} else {
$("#nextBtn").attr("disabled", false)
$("#nextBtn").removeClass("btn-secondary").addClass("btn-info")
}
}
changeBgClass = (elem, new_class) => {
$(elem).removeClass("btn-outline-secondary btn-success btn-danger").addClass(new_class)
}
init_question = () => {
if(!isEmpty(data)) {
if(document.getElementById("random-question-order").checked)
curr_question_id = randomPropertyKey(data)
else
curr_question_id = Object.keys(data)[0]
curr_question = data[curr_question_id]
delete data[curr_question_id]
displayCurrQuestion()
changeNextBtnState(true)
curr_answered = false
changeBgClass("#ans-a", "btn-outline-secondary")
changeBgClass("#ans-b", "btn-outline-secondary")
changeBgClass("#ans-c", "btn-outline-secondary")
changeBgClass("#ans-d", "btn-outline-secondary")
}
else if(!quiz_complete){
quiz_complete = true
init_stats()
}
}
randomPropertyKey = obj => {
let keys = Object.keys(obj)
return keys[ keys.length * Math.random() << 0]
}
isEmpty = map => {
for(var key in map) {
if (map.hasOwnProperty(key)) return false
}
return true;
}
initStartQuestions = () => {
$("#start").removeClass("hidden")
$("#endscreen").addClass("hidden")
$("#quiz").addClass("hidden")
let categories = new Set()
for(let key in h_data) {
categories.add(h_data[key].b)
}
Array.from(categories).sort((a,b)=>{return b-a}).forEach(i => {
document.getElementById("start-questions").innerHTML += `
<div>
<details id="bad-${i}" open>
<summary>
<h3>${i}x elhibázva
<button type="button" class="btn btn-outline-info checkBtn" onclick="checkAllInside(true, '#bad-${i}')"><i class="fas fa-check"></i> Mindet ezen belül</button>
<button type="button" class="btn btn-outline-secondary checkBtn" onclick="checkAllInside(false, '#bad-${i}')"><i class="fas fa-times"></i> Mindet ezen belül</button>
</h3>
</summary>
</details>
</div>`
})
for(let key in og_data) {
document.getElementById(`bad-${h_data[key].b}`).innerHTML += `
<input type="checkbox" id="check-for-${key}" name="${key}" class="sq-checks" checked>
<label for="check-for-${key}">
${key}. ${og_data[key].question}
</label>
<br>`
}
$('.sq-checks').click(e => {
$(e.target).attr('checked', e.target.checked)
})
$("#start-questions").submit(function( event ) {
let values = $(this).serializeArray()
let keys = []
let questions = {}
values.forEach(e => {
questions[e["name"]] = data[e["name"]]
});
data = questions
$("#start").addClass("hidden")
$("#endscreen").addClass("hidden")
$("#quiz").removeClass("hidden")
init_question()
event.preventDefault()
})
}
checkAll = e => {
$(".sq-checks").attr("checked", e)
}
checkAllInside = (e,id) => {
$(id + " .sq-checks").attr("checked", e)
}
checkRange = (from, to, e) => {
for(let i = from; i <= to; i += 1) {
$("#check-for-" + i).attr("checked", e)
}
}
checkRangeOnClick = e => {
let from = parseInt(document.getElementById("from-text").value, 10)
let to = parseInt(document.getElementById("to-text").value, 10)
checkRange(from, to, e)
}
init_stats = () => {
// Hiding the quiz and making the stat-screen visible
$("#quiz").addClass("hidden")
$("#start").addClass("hidden")
$("#endscreen").removeClass("hidden")
// Separating the good and bad answers
let good = []
let bad = []
for(let key in answered_questions) {
let a = answered_questions[key].answer == answered_questions[key].my_answer
let value = {key: key, ans: answered_questions[key]}
if(a) good.push(value)
else bad.push(value)
}
// Printing and saving the good and bad answers
good.forEach(e => {
$("#end-good").append(`<li value="${e.key}">${e.ans.question}</li>`)
if(h_data[e.key].g) h_data[e.key].g += 1
else h_data[e.key].g = 1
})
bad.forEach(e => {
$("#end-bad").append(`<li value="${e.key}">${e.ans.question}</li>`)
if(h_data[e.key].b) h_data[e.key].b += 1
else h_data[e.key].b = 1
})
// Displaying the overall statistics
document.getElementById("end-num-good-ans").innerHTML = num_good_ans
document.getElementById("end-num-ans").innerHTML = num_ans
document.getElementById("end-num-percentage").innerHTML = parseFloat(num_good_ans / num_ans * 100).toFixed(2)
// Writing the historical data to cookie
write_cookie()
}
function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
eraseCookie = name => {
document.cookie = name+'=; Max-Age=-99999999;';
}
reset_h_data = () => {
h_data = {}
let keys = Object.keys(og_data)
keys.forEach(key => {
h_data[key] = {g: 0, b: 0}
})
}
load_cookie = () => {
let h_cookie_data = getCookie("h_data")
if(h_cookie_data != null)
h_data = JSON.parse(h_cookie_data)
else {
reset_h_data()
}
}
write_cookie = () => {
let cookie_data = JSON.stringify(h_data)
setCookie("h_data", cookie_data, 100000)
}
reset_cookie = () => {
eraseCookie("h_data")
reset_h_data()
write_cookie()
init_quiz()
}
$('#importModal').on('shown.bs.modal', function () {
document.getElementById("importModal-ta").value = ""
})
$('#exportModal').on('shown.bs.modal', function () {
let export_modal = document.getElementById("exportModal-ta")
export_modal.value = btoa(getCookie("h_data"))
export_modal.focus()
export_modal.select()
})
$('#confirmModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var trigger = button.data('trigger') // Extract info from data-* attributes
var modal = $(this)
switch (trigger) {
case "restart":
modal.find('.modal-title').text("Biztosan újra akarod indítani a quizt?")
modal.find('.modal-footer #submit-modal').attr("onclick", "init_quiz()")
break;
case "reset-data":
modal.find('.modal-title').text("Biztosan ki akarod törölni a metett adatokat? (Ez újra fogja indítani a quiz-t)")
modal.find('.modal-footer #submit-modal').attr("onclick", "reset_cookie()")
break;
default:
break;
}
})
import_h_data = () => {
if(window.confirm("Biztosan importálni akarod ezeket az adatokat? (Ez újra fogja indítani a quiz-t)")) {
let cookie_data = atob(document.getElementById("importModal-ta").value)
setCookie("h_data", cookie_data, 100000)
load_cookie()
init_quiz()
}
}
restart_quiz = () => {
if(window.confirm("Biztosan újra akarod indítani a quizt?")) {
init_quiz()
}
}
init_quiz_data = () => {
data = og_data
curr_answered = false
answered_questions = {}
num_good_ans = 0
num_ans = 0
quiz_complete = false
load_cookie()
}
init_quiz = () => {
init_quiz_data()
load_og_state("all")
initStartQuestions()
}
save_og_states = () => {
og_start = document.getElementById("start").innerHTML
og_endscreen = document.getElementById("endscreen").innerHTML
og_quiz = document.getElementById("quiz").innerHTML
}
load_og_state = id => {
switch (id) {
case "og_start":
document.getElementById("start").innerHTML = og_start
break
case "og_endscreen":
document.getElementById("endscreen").innerHTML = og_endscreen
break
case "og_quiz":
document.getElementById("quiz").innerHTML = og_quiz
break
case "all":
document.getElementById("start").innerHTML = og_start
document.getElementById("endscreen").innerHTML = og_endscreen
document.getElementById("quiz").innerHTML = og_quiz
break
}
}
shuffle = obj => {
let array = Object.keys(obj).map(function(val) { return obj[val] })
var currentIndex = array.length, temporaryValue, randomIndex
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex -= 1
// And swap it with the current element.
temporaryValue = array[currentIndex]
array[currentIndex] = array[randomIndex]
array[randomIndex] = temporaryValue
}
return array;
}
og_data = JSON.parse(quiz_data)
save_og_states()
init_quiz()