Fausto Busuito commited on
Commit
242f01a
1 Parent(s): 06a56b9

Application changes

Browse files
Files changed (1) hide show
  1. app/static/script.js +70 -49
app/static/script.js CHANGED
@@ -17,63 +17,84 @@ document.getElementById('start-session').addEventListener('click', async () => {
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
 
75
-
76
-
 
 
77
 
78
  function selectAnswer(optionIndex) {
79
  const currentAnswers = userAnswers[currentIndex];
 
17
  userAnswers = Array(questions.length).fill([]);
18
  startQuiz();
19
  });
20
+ function renderQuestion(question, index) {
21
+ const questionContainer = document.getElementById('question-container');
22
+ questionContainer.innerHTML = '';
23
+
24
+ const questionText = document.createElement('p');
25
+ questionText.innerText = `${index + 1}. ${question.question}`;
26
+ questionContainer.appendChild(questionText);
27
+
28
+ const optionsContainer = document.createElement('div');
29
+
30
+ // Se la domanda permette solo una risposta, usa i radio button
31
+ if (question.correct.length === 1) {
32
+ question.options.forEach((option, idx) => {
33
+ const label = document.createElement('label');
34
+ const input = document.createElement('input');
35
+ input.type = 'radio';
36
+ input.name = `question-${index}`; // Nome unico per ogni domanda
37
+ input.value = idx;
38
+ if (userAnswers[index] === idx) input.checked = true; // Pre-seleziona la risposta dell'utente se presente
39
+ input.addEventListener('change', () => {
40
+ userAnswers[index] = [idx]; // Memorizza la risposta
41
+ });
42
+
43
+ label.appendChild(input);
44
+ label.appendChild(document.createTextNode(option));
45
+ optionsContainer.appendChild(label);
46
+ });
47
+ } else {
48
+ // Se la domanda permette risposte multiple, usa le checkbox
49
+ question.options.forEach((option, idx) => {
50
+ const label = document.createElement('label');
51
+ const input = document.createElement('input');
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
+ answerIndex.push(idx);
60
  } else {
61
+ const answerIdx = answerIndex.indexOf(idx);
62
+ if (answerIdx > -1) {
63
+ answerIndex.splice(answerIdx, 1);
64
+ }
65
  }
66
+ userAnswers[index] = answerIndex; // Memorizza la risposta
67
+ });
 
 
68
 
69
+ label.appendChild(input);
70
+ label.appendChild(document.createTextNode(option));
71
+ optionsContainer.appendChild(label);
72
+ });
73
+ }
 
 
 
74
 
75
+ questionContainer.appendChild(optionsContainer);
76
  }
77
 
78
+ // Funzione per navigare tra le domande
79
+ function showQuestion(index) {
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
+ // Avvio del quiz
95
+ function startQuiz() {
96
+ showQuestion(0); // Inizia con la prima domanda
97
+ }
98
 
99
  function selectAnswer(optionIndex) {
100
  const currentAnswers = userAnswers[currentIndex];