Fausto Busuito
commited on
Commit
•
b0a7145
1
Parent(s):
242f01a
Application changes
Browse files- app/static/script.js +45 -70
app/static/script.js
CHANGED
@@ -17,83 +17,58 @@ document.getElementById('start-session').addEventListener('click', async () => {
|
|
17 |
userAnswers = Array(questions.length).fill([]);
|
18 |
startQuiz();
|
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 |
-
input.type = 'checkbox';
|
53 |
-
input.name = `question-${index}`; // Nome unico per ogni domanda
|
54 |
-
input.value = idx;
|
55 |
-
if (userAnswers[index] && userAnswers[index].includes(idx)) input.checked = true; // Pre-seleziona le risposte dell'utente se presenti
|
56 |
-
input.addEventListener('change', () => {
|
57 |
-
const answerIndex = userAnswers[index] || [];
|
58 |
if (input.checked) {
|
59 |
-
|
60 |
} else {
|
61 |
-
|
62 |
-
if (answerIdx > -1) {
|
63 |
-
answerIndex.splice(answerIdx, 1);
|
64 |
-
}
|
65 |
}
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
label.appendChild(input);
|
70 |
-
label.appendChild(document.createTextNode(option));
|
71 |
-
optionsContainer.appendChild(label);
|
72 |
});
|
73 |
-
}
|
74 |
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
const question = questions[index];
|
81 |
-
renderQuestion(question, index);
|
82 |
-
|
83 |
-
// Imposta i pulsanti di navigazione
|
84 |
-
const prevButton = document.getElementById('prev-button');
|
85 |
-
const nextButton = document.getElementById('next-button');
|
86 |
-
|
87 |
-
prevButton.disabled = index === 0;
|
88 |
-
nextButton.disabled = index === questions.length - 1;
|
89 |
-
|
90 |
-
prevButton.onclick = () => showQuestion(index - 1);
|
91 |
-
nextButton.onclick = () => showQuestion(index + 1);
|
92 |
-
}
|
93 |
|
94 |
-
|
95 |
-
function startQuiz() {
|
96 |
-
showQuestion(0); // Inizia con la prima domanda
|
97 |
}
|
98 |
|
99 |
function selectAnswer(optionIndex) {
|
|
|
17 |
userAnswers = Array(questions.length).fill([]);
|
18 |
startQuiz();
|
19 |
});
|
20 |
+
|
21 |
+
function startQuiz() {
|
22 |
+
document.getElementById('file-selection').style.display = 'none';
|
23 |
+
document.getElementById('quiz-container').style.display = 'block';
|
24 |
+
startTime = Date.now();
|
25 |
+
displayQuestion();
|
26 |
+
setInterval(updateTimer, 1000);
|
27 |
+
}
|
28 |
+
|
29 |
+
function displayQuestion() {
|
30 |
+
const question = questions[currentIndex];
|
31 |
+
document.getElementById('question-number').innerText = `Question ${currentIndex + 1} of ${questions.length}`;
|
32 |
+
document.getElementById('question').innerText = question.question;
|
33 |
+
|
34 |
+
// Verifica se ci sono risposte multiple
|
35 |
+
const isMultipleChoice = question.correct.length > 1;
|
36 |
+
|
37 |
+
const optionsDiv = document.getElementById('options');
|
38 |
+
optionsDiv.innerHTML = '';
|
39 |
+
|
40 |
+
const form = document.createElement('form');
|
41 |
+
form.id = 'question-options';
|
42 |
+
|
43 |
+
question.options.forEach((option, i) => {
|
44 |
+
const input = document.createElement('input');
|
45 |
+
input.type = isMultipleChoice ? 'checkbox' : 'radio';
|
46 |
+
input.name = 'answer';
|
47 |
+
input.value = i;
|
48 |
+
input.checked = userAnswers[currentIndex].includes(i);
|
49 |
+
|
50 |
+
input.addEventListener('change', () => {
|
51 |
+
if (isMultipleChoice) {
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
if (input.checked) {
|
53 |
+
userAnswers[currentIndex].push(i);
|
54 |
} else {
|
55 |
+
userAnswers[currentIndex] = userAnswers[currentIndex].filter(j => j !== i);
|
|
|
|
|
|
|
56 |
}
|
57 |
+
} else {
|
58 |
+
userAnswers[currentIndex] = [i];
|
59 |
+
}
|
|
|
|
|
|
|
60 |
});
|
|
|
61 |
|
62 |
+
const label = document.createElement('label');
|
63 |
+
label.appendChild(input);
|
64 |
+
label.appendChild(document.createTextNode(option));
|
65 |
+
label.style.display = 'block';
|
66 |
+
label.style.marginBottom = '10px';
|
67 |
|
68 |
+
form.appendChild(label);
|
69 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
optionsDiv.appendChild(form);
|
|
|
|
|
72 |
}
|
73 |
|
74 |
function selectAnswer(optionIndex) {
|