mou3az commited on
Commit
5c215d6
1 Parent(s): cc1add1

Update templates/quiz.html

Browse files
Files changed (1) hide show
  1. templates/quiz.html +5 -33
templates/quiz.html CHANGED
@@ -78,8 +78,8 @@
78
  </div>
79
 
80
  <script>
81
- // Use the passed textWithAnswers directly
82
- let textWithAnswers = `{{ textWithAnswers|escapejs }}`;
83
 
84
  window.onload = function() {
85
  generateQuiz();
@@ -89,7 +89,6 @@
89
  let lines = text.split(/\n+/);
90
  let filteredLines = [];
91
  let answersAndExplanations = [];
92
-
93
  lines.forEach(line => {
94
  line = line.trim();
95
 
@@ -110,9 +109,7 @@
110
  filteredLines.push(line);
111
  }
112
  });
113
-
114
  let filteredText = filteredLines.join("\n");
115
-
116
  return {
117
  filteredText: filteredText,
118
  answersAndExplanations: answersAndExplanations
@@ -120,88 +117,68 @@
120
  }
121
 
122
  function generateQuiz() {
123
- let { filteredText, answersAndExplanations } = handletext(textWithAnswers);
124
-
125
  document.getElementById('inputSection').style.display = 'none';
126
-
127
  let quizSection = document.getElementById('quizSection');
128
  quizSection.innerHTML = '';
129
-
130
  let questions = filteredText.split(/\d+\.\s+/).filter(q => q.trim() !== '');
131
-
132
  questions.forEach((question, index) => {
133
  let parts = question.split('Choices:');
134
  let questionText = parts[0].replace('Question:', '').trim();
135
  let choicesText = parts[1].trim();
136
-
137
  let div = document.createElement('div');
138
  div.className = 'quiz-question';
139
  div.innerHTML = `<strong>${index + 1}: ${questionText}</strong>`;
140
-
141
  let choices = choicesText.split(/\s*[A-D]\)\s*/).filter(choice => choice.trim() !== '');
142
  let labels = choicesText.match(/\s*[A-D]\)\s*/g);
143
  choices.forEach((choice, i) => {
144
  let label = labels[i].trim().charAt(0);
145
-
146
  let choiceDiv = document.createElement('div');
147
-
148
  let input = document.createElement('input');
149
  input.type = 'radio';
150
  input.name = `question_${index + 1}`;
151
  input.value = label;
152
  input.id = `q${index + 1}_${label}`;
153
  input.setAttribute('data-answered', 'false');
154
-
155
  let choiceLabel = document.createElement('label');
156
  choiceLabel.setAttribute('for', `q${index + 1}_${label}`);
157
  choiceLabel.textContent = `${label}) ${choice}`;
158
-
159
  choiceDiv.appendChild(input);
160
  choiceDiv.appendChild(choiceLabel);
161
-
162
  div.appendChild(choiceDiv);
163
-
164
  input.addEventListener('change', function() {
165
  input.setAttribute('data-answered', 'true');
166
  });
167
  });
168
-
169
  let answerDiv = document.createElement('div');
170
  answerDiv.className = 'quiz-answer';
171
  answerDiv.style.display = 'none';
172
  div.appendChild(answerDiv);
173
-
174
  let explanationDiv = document.createElement('div');
175
  explanationDiv.className = 'quiz-answer';
176
  explanationDiv.style.display = 'none';
177
  div.appendChild(explanationDiv);
178
-
179
  quizSection.appendChild(div);
180
  });
181
-
182
  document.getElementById('submitQuiz').style.display = 'block';
183
  }
184
 
185
  function validateQuiz() {
186
  let quizSection = document.getElementById('quizSection');
187
  let questions = quizSection.getElementsByClassName('quiz-question');
188
-
189
  for (let i = 0; i < questions.length; i++) {
190
  let inputs = questions[i].querySelectorAll('input[type="radio"]');
191
  let answered = false;
192
-
193
  for (let j = 0; j < inputs.length; j++) {
194
  if (inputs[j].checked) {
195
  answered = true;
196
  break;
197
  }
198
  }
199
-
200
  if (!answered) {
201
  return false;
202
  }
203
  }
204
-
205
  return true;
206
  }
207
 
@@ -210,24 +187,19 @@
210
  alert('Please answer all questions before submitting.');
211
  return;
212
  }
213
-
214
  let quizSection = document.getElementById('quizSection');
215
  let answerDivs = quizSection.getElementsByClassName('quiz-answer');
216
- let { answersAndExplanations } = handletext(textWithAnswers);
217
-
218
  for (let i = 0; i < answerDivs.length; i += 2) {
219
  let answer = answersAndExplanations[i / 2]?.Answer || '';
220
  let explanation = answersAndExplanations[i / 2]?.Explanation || '';
221
-
222
  answerDivs[i].style.display = 'block';
223
  answerDivs[i].innerHTML = `Answer: ${answer}`;
224
-
225
  answerDivs[i + 1].style.display = 'block';
226
  answerDivs[i + 1].innerHTML = `Explanation: ${explanation}`;
227
  }
228
-
229
  document.getElementById('submitQuiz').style.display = 'none';
230
  }
231
  </script>
232
  </body>
233
- </html>
 
78
  </div>
79
 
80
  <script>
81
+ // Use the passed response JSON directly
82
+ let response = JSON.parse(`{{ response_json|tojson|safe }}`);
83
 
84
  window.onload = function() {
85
  generateQuiz();
 
89
  let lines = text.split(/\n+/);
90
  let filteredLines = [];
91
  let answersAndExplanations = [];
 
92
  lines.forEach(line => {
93
  line = line.trim();
94
 
 
109
  filteredLines.push(line);
110
  }
111
  });
 
112
  let filteredText = filteredLines.join("\n");
 
113
  return {
114
  filteredText: filteredText,
115
  answersAndExplanations: answersAndExplanations
 
117
  }
118
 
119
  function generateQuiz() {
120
+ let { filteredText, answersAndExplanations } = handletext(response);
 
121
  document.getElementById('inputSection').style.display = 'none';
 
122
  let quizSection = document.getElementById('quizSection');
123
  quizSection.innerHTML = '';
 
124
  let questions = filteredText.split(/\d+\.\s+/).filter(q => q.trim() !== '');
 
125
  questions.forEach((question, index) => {
126
  let parts = question.split('Choices:');
127
  let questionText = parts[0].replace('Question:', '').trim();
128
  let choicesText = parts[1].trim();
 
129
  let div = document.createElement('div');
130
  div.className = 'quiz-question';
131
  div.innerHTML = `<strong>${index + 1}: ${questionText}</strong>`;
 
132
  let choices = choicesText.split(/\s*[A-D]\)\s*/).filter(choice => choice.trim() !== '');
133
  let labels = choicesText.match(/\s*[A-D]\)\s*/g);
134
  choices.forEach((choice, i) => {
135
  let label = labels[i].trim().charAt(0);
 
136
  let choiceDiv = document.createElement('div');
 
137
  let input = document.createElement('input');
138
  input.type = 'radio';
139
  input.name = `question_${index + 1}`;
140
  input.value = label;
141
  input.id = `q${index + 1}_${label}`;
142
  input.setAttribute('data-answered', 'false');
 
143
  let choiceLabel = document.createElement('label');
144
  choiceLabel.setAttribute('for', `q${index + 1}_${label}`);
145
  choiceLabel.textContent = `${label}) ${choice}`;
 
146
  choiceDiv.appendChild(input);
147
  choiceDiv.appendChild(choiceLabel);
 
148
  div.appendChild(choiceDiv);
 
149
  input.addEventListener('change', function() {
150
  input.setAttribute('data-answered', 'true');
151
  });
152
  });
 
153
  let answerDiv = document.createElement('div');
154
  answerDiv.className = 'quiz-answer';
155
  answerDiv.style.display = 'none';
156
  div.appendChild(answerDiv);
 
157
  let explanationDiv = document.createElement('div');
158
  explanationDiv.className = 'quiz-answer';
159
  explanationDiv.style.display = 'none';
160
  div.appendChild(explanationDiv);
 
161
  quizSection.appendChild(div);
162
  });
 
163
  document.getElementById('submitQuiz').style.display = 'block';
164
  }
165
 
166
  function validateQuiz() {
167
  let quizSection = document.getElementById('quizSection');
168
  let questions = quizSection.getElementsByClassName('quiz-question');
 
169
  for (let i = 0; i < questions.length; i++) {
170
  let inputs = questions[i].querySelectorAll('input[type="radio"]');
171
  let answered = false;
 
172
  for (let j = 0; j < inputs.length; j++) {
173
  if (inputs[j].checked) {
174
  answered = true;
175
  break;
176
  }
177
  }
 
178
  if (!answered) {
179
  return false;
180
  }
181
  }
 
182
  return true;
183
  }
184
 
 
187
  alert('Please answer all questions before submitting.');
188
  return;
189
  }
 
190
  let quizSection = document.getElementById('quizSection');
191
  let answerDivs = quizSection.getElementsByClassName('quiz-answer');
192
+ let { answersAndExplanations } = handletext(response);
 
193
  for (let i = 0; i < answerDivs.length; i += 2) {
194
  let answer = answersAndExplanations[i / 2]?.Answer || '';
195
  let explanation = answersAndExplanations[i / 2]?.Explanation || '';
 
196
  answerDivs[i].style.display = 'block';
197
  answerDivs[i].innerHTML = `Answer: ${answer}`;
 
198
  answerDivs[i + 1].style.display = 'block';
199
  answerDivs[i + 1].innerHTML = `Explanation: ${explanation}`;
200
  }
 
201
  document.getElementById('submitQuiz').style.display = 'none';
202
  }
203
  </script>
204
  </body>
205
+ </html>