-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuestionHandler.js
130 lines (109 loc) · 4.31 KB
/
QuestionHandler.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
class QuestionHandler {
constructor(questionSelector) {
this.statistics = new Statistics()
this.timer = new Timer()
this.isDone = false
this.questionSelector = questionSelector
this.showQuestionNavigation()
this.newQuestion()
}
htmlDecode(input) {
var doc = new DOMParser().parseFromString(input, "text/html")
return doc.documentElement.textContent
}
checkAnswer(element) {
if (this.questionSelector.hasQuestionSelection()) {
let questionSelectorButton = document.getElementById("question-selector-" + (this.questionSelector.currentQuestionNumber + 1))
questionSelectorButton.style.color = GREY
}
for (let index = 0; index < 5; index++) {
let option = document.getElementById('option' + index)
option.style.color = BLACK
}
let answerIndex = parseInt(element.value)
let isAnswerCorrect = this.currentQuestion.giveAnswer(answerIndex)
element.style.color = GREY
if (this.questionSelector.hasCheckAnswer()) {
if (isAnswerCorrect) {
this.statistics.addSuccess()
element.style.color = FRESH_ONION
} else {
this.statistics.addFailure()
element.style.color = PURE_CRIMSON
document.getElementById('option' + this.currentQuestion.correctAnswerId).style.color = FRESH_ONION
}
for (let index = 0; index < 5; index++) {
let option = document.getElementById('option' + index)
option.disabled = true
}
}
if (this.questionSelector.hasAutoNewQuestion()) {
this.newQuestion()
}
}
showStats() {
if (!this.isDone) {
return
}
this.statistics.checkQuestions(this.questionSelector.questions)
this.timer.stopTimer()
htmlConfigurer.hideEverything()
htmlConfigurer.showStatistics()
htmlConfigurer.showScore()
this.questionSelector.questions.forEach(
(answer, index) =>
document
.getElementById("outer")
.appendChild(answer.generateHtml(index + 1))
)
}
newQuestion() {
if (this.isDone || this.questionSelector.isDone()) {
return
}
let newQuestion = this.questionSelector.newQuestion()
this.changeQuestion(newQuestion)
}
finishTest() {
this.isDone = true
this.showStats()
}
changeQuestionByNumber(questionNumber) {
let question = this.questionSelector.questionByNumber(questionNumber)
this.changeQuestion(question)
}
changeQuestion(question) {
this.currentQuestion = question
this.updateHtml()
}
updateHtml() {
document.getElementById('question-number').innerHTML = "# " + (this.questionSelector.currentQuestionNumber + 1)
document.getElementById('question').innerHTML = this.currentQuestion.question
this.updateOption('option0', this.currentQuestion.option0)
this.updateOption('option1', this.currentQuestion.option1)
this.updateOption('option2', this.currentQuestion.option2)
this.updateOption('option3', this.currentQuestion.option3)
this.updateOption('option4', this.currentQuestion.option4)
if (this.currentQuestion.givenAnswer != null) {
let chosenOption = document.getElementById("option" + this.currentQuestion.givenAnswerId)
chosenOption.style.color = GREY
}
}
updateOption(id, value) {
let element = document.getElementById(id)
element.innerHTML = value
element.style.color = BLACK
element.disabled = false
}
showQuestionNavigation() {
let container = document.getElementById('question-navigation')
self = this
for (let questionNumber = 1; questionNumber <= this.questionSelector.numberOfQuestions; questionNumber++) {
var button = document.createElement("button")
button.innerHTML = questionNumber
button.id = "question-selector-" + questionNumber
button.onclick = function() { questionHandler.changeQuestionByNumber(this.innerHTML - 1) }
container.appendChild(button)
}
}
}