mou3az commited on
Commit
407be16
1 Parent(s): 9a722a3

Update templates/quiz.html

Browse files
Files changed (1) hide show
  1. templates/quiz.html +25 -19
templates/quiz.html CHANGED
@@ -48,6 +48,20 @@
48
  a:hover {
49
  text-decoration: underline;
50
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  </style>
52
  </head>
53
  <body>
@@ -57,6 +71,7 @@
57
  <a href="/" class="generate-link">Back to generate another quiz</a>
58
  </div>
59
  <div id="quizSection"></div>
 
60
  <button id="submitQuiz" style="display: none;" onclick="submitQuiz()">Submit Quiz</button>
61
 
62
  <script>
@@ -81,6 +96,7 @@
81
 
82
  let choices = choicesText.split(/\s*[A-D]\)\s*/).filter(choice => choice.trim() !== '');
83
  let labels = choicesText.match(/\s*[A-D]\)\s*/g);
 
84
  choices.forEach((choice, i) => {
85
  let label = labels[i].trim().charAt(0);
86
 
@@ -121,35 +137,28 @@
121
  function validateQuiz() {
122
  let quizSection = document.getElementById('quizSection');
123
  let questions = quizSection.getElementsByClassName('quiz-question');
124
-
125
  for (let i = 0; i < questions.length; i++) {
126
  let inputs = questions[i].querySelectorAll('input[type="radio"]');
127
- let answered = false;
128
-
129
- for (let j = 0; j < inputs.length; j++) {
130
- if (inputs[j].checked) {
131
- answered = true;
132
- break;
133
- }
134
- }
135
-
136
  if (!answered) {
137
- return false;
 
138
  }
139
  }
140
-
141
- return true;
142
  }
143
 
144
  function submitQuiz() {
145
  if (!validateQuiz()) {
146
- alert('Please answer all questions before submitting.');
 
147
  return;
148
  }
 
149
 
150
  let quizSection = document.getElementById('quizSection');
151
  let questions = quizSection.getElementsByClassName('quiz-question');
152
-
153
  fetch('/process_text', {
154
  method: 'POST',
155
  headers: {
@@ -165,17 +174,14 @@
165
  let explanationDiv = questions[i].querySelector('.quiz-explanation');
166
  let answer = answersAndExplanations[i]?.Answer || '';
167
  let explanation = answersAndExplanations[i]?.Explanation || '';
168
-
169
  answerDiv.style.display = 'block';
170
  answerDiv.innerHTML = `Answer: ${answer}`;
171
-
172
  explanationDiv.style.display = 'block';
173
  explanationDiv.innerHTML = `Explanation: ${explanation}`;
174
  }
175
-
176
  document.getElementById('submitQuiz').style.display = 'none';
177
  });
178
  }
179
  </script>
180
  </body>
181
- </html>
 
48
  a:hover {
49
  text-decoration: underline;
50
  }
51
+ .quiz-question {
52
+ margin-bottom: 20px;
53
+ }
54
+ .quiz-answer, .quiz-explanation {
55
+ margin-top: 10px;
56
+ padding: 10px;
57
+ border: 1px solid #ccc;
58
+ border-radius: 5px;
59
+ background-color: #f9f9f9;
60
+ }
61
+ .error {
62
+ color: red;
63
+ margin-top: 10px;
64
+ }
65
  </style>
66
  </head>
67
  <body>
 
71
  <a href="/" class="generate-link">Back to generate another quiz</a>
72
  </div>
73
  <div id="quizSection"></div>
74
+ <div id="error-message" class="error" style="display: none;"></div>
75
  <button id="submitQuiz" style="display: none;" onclick="submitQuiz()">Submit Quiz</button>
76
 
77
  <script>
 
96
 
97
  let choices = choicesText.split(/\s*[A-D]\)\s*/).filter(choice => choice.trim() !== '');
98
  let labels = choicesText.match(/\s*[A-D]\)\s*/g);
99
+
100
  choices.forEach((choice, i) => {
101
  let label = labels[i].trim().charAt(0);
102
 
 
137
  function validateQuiz() {
138
  let quizSection = document.getElementById('quizSection');
139
  let questions = quizSection.getElementsByClassName('quiz-question');
140
+ let allAnswered = true;
141
  for (let i = 0; i < questions.length; i++) {
142
  let inputs = questions[i].querySelectorAll('input[type="radio"]');
143
+ let answered = Array.from(inputs).some(input => input.checked);
 
 
 
 
 
 
 
 
144
  if (!answered) {
145
+ allAnswered = false;
146
+ break;
147
  }
148
  }
149
+ return allAnswered;
 
150
  }
151
 
152
  function submitQuiz() {
153
  if (!validateQuiz()) {
154
+ document.getElementById('error-message').textContent = 'Please answer all questions before submitting.';
155
+ document.getElementById('error-message').style.display = 'block';
156
  return;
157
  }
158
+ document.getElementById('error-message').style.display = 'none';
159
 
160
  let quizSection = document.getElementById('quizSection');
161
  let questions = quizSection.getElementsByClassName('quiz-question');
 
162
  fetch('/process_text', {
163
  method: 'POST',
164
  headers: {
 
174
  let explanationDiv = questions[i].querySelector('.quiz-explanation');
175
  let answer = answersAndExplanations[i]?.Answer || '';
176
  let explanation = answersAndExplanations[i]?.Explanation || '';
 
177
  answerDiv.style.display = 'block';
178
  answerDiv.innerHTML = `Answer: ${answer}`;
 
179
  explanationDiv.style.display = 'block';
180
  explanationDiv.innerHTML = `Explanation: ${explanation}`;
181
  }
 
182
  document.getElementById('submitQuiz').style.display = 'none';
183
  });
184
  }
185
  </script>
186
  </body>
187
+ </html>