Update templates/quiz.html
Browse files- templates/quiz.html +38 -27
templates/quiz.html
CHANGED
@@ -15,6 +15,7 @@
|
|
15 |
background-color: #ffc107;
|
16 |
padding: 10px;
|
17 |
border-radius: 5px;
|
|
|
18 |
}
|
19 |
.quiz-container {
|
20 |
background-color: #fff;
|
@@ -25,6 +26,10 @@
|
|
25 |
}
|
26 |
.quiz-container pre {
|
27 |
white-space: pre-wrap;
|
|
|
|
|
|
|
|
|
28 |
}
|
29 |
.generate-link {
|
30 |
display: inline-block;
|
@@ -50,6 +55,10 @@
|
|
50 |
}
|
51 |
.quiz-answer, .quiz-explanation {
|
52 |
margin-top: 10px;
|
|
|
|
|
|
|
|
|
53 |
}
|
54 |
.error-message {
|
55 |
color: red;
|
@@ -75,39 +84,41 @@
|
|
75 |
|
76 |
<script>
|
77 |
document.addEventListener('DOMContentLoaded', function() {
|
78 |
-
|
|
|
|
|
79 |
});
|
80 |
|
81 |
-
function generateQuiz(filteredText,
|
82 |
-
|
83 |
quizSection.innerHTML = '';
|
84 |
|
85 |
-
|
86 |
|
87 |
questions.forEach((question, index) => {
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
|
92 |
-
|
93 |
div.className = 'quiz-question';
|
94 |
div.innerHTML = `<strong>${index + 1}: ${questionText}</strong>`;
|
95 |
|
96 |
-
|
97 |
-
|
98 |
|
99 |
choices.forEach((choice, i) => {
|
100 |
-
|
101 |
|
102 |
-
|
103 |
|
104 |
-
|
105 |
input.type = 'radio';
|
106 |
input.name = `question_${index + 1}`;
|
107 |
input.value = label;
|
108 |
input.id = `q${index + 1}_${label}`;
|
109 |
|
110 |
-
|
111 |
choiceLabel.setAttribute('for', `q${index + 1}_${label}`);
|
112 |
choiceLabel.textContent = `${label}) ${choice}`;
|
113 |
|
@@ -117,12 +128,12 @@
|
|
117 |
div.appendChild(choiceDiv);
|
118 |
});
|
119 |
|
120 |
-
|
121 |
answerDiv.className = 'quiz-answer';
|
122 |
answerDiv.style.display = 'none';
|
123 |
div.appendChild(answerDiv);
|
124 |
|
125 |
-
|
126 |
explanationDiv.className = 'quiz-explanation';
|
127 |
explanationDiv.style.display = 'none';
|
128 |
div.appendChild(explanationDiv);
|
@@ -132,13 +143,13 @@
|
|
132 |
}
|
133 |
|
134 |
function validateQuiz() {
|
135 |
-
|
136 |
-
|
137 |
let allAnswered = true;
|
138 |
|
139 |
for (let i = 0; i < questions.length; i++) {
|
140 |
-
|
141 |
-
|
142 |
|
143 |
if (!answered) {
|
144 |
allAnswered = false;
|
@@ -167,17 +178,17 @@
|
|
167 |
})
|
168 |
.then(response => response.json())
|
169 |
.then(data => {
|
170 |
-
console.log('Server response:', data);
|
171 |
-
|
172 |
-
|
173 |
|
174 |
for (let i = 0; i < questions.length; i++) {
|
175 |
-
|
176 |
-
|
177 |
|
178 |
if (answersAndExplanations[i]) {
|
179 |
-
|
180 |
-
|
181 |
|
182 |
answerDiv.style.display = 'block';
|
183 |
answerDiv.innerHTML = `Answer: ${answer}`;
|
|
|
15 |
background-color: #ffc107;
|
16 |
padding: 10px;
|
17 |
border-radius: 5px;
|
18 |
+
margin-bottom: 20px;
|
19 |
}
|
20 |
.quiz-container {
|
21 |
background-color: #fff;
|
|
|
26 |
}
|
27 |
.quiz-container pre {
|
28 |
white-space: pre-wrap;
|
29 |
+
background-color: #e9ecef;
|
30 |
+
padding: 10px;
|
31 |
+
border-radius: 4px;
|
32 |
+
overflow-x: auto;
|
33 |
}
|
34 |
.generate-link {
|
35 |
display: inline-block;
|
|
|
55 |
}
|
56 |
.quiz-answer, .quiz-explanation {
|
57 |
margin-top: 10px;
|
58 |
+
padding: 10px;
|
59 |
+
background-color: #f9f9f9;
|
60 |
+
border: 1px solid #ddd;
|
61 |
+
border-radius: 4px;
|
62 |
}
|
63 |
.error-message {
|
64 |
color: red;
|
|
|
84 |
|
85 |
<script>
|
86 |
document.addEventListener('DOMContentLoaded', function() {
|
87 |
+
const filteredText = "{{ filtered_text }}";
|
88 |
+
const answersAndExplanations = {{ answers_and_explanations | tojson }};
|
89 |
+
generateQuiz(filteredText, answersAndExplanations);
|
90 |
});
|
91 |
|
92 |
+
function generateQuiz(filteredText, answersAndExplanations) {
|
93 |
+
const quizSection = document.getElementById('quizSection');
|
94 |
quizSection.innerHTML = '';
|
95 |
|
96 |
+
const questions = filteredText.split(/\d+\.\s+/).filter(q => q.trim() !== '');
|
97 |
|
98 |
questions.forEach((question, index) => {
|
99 |
+
const parts = question.split('Choices:');
|
100 |
+
const questionText = parts[0].replace('Question:', '').trim();
|
101 |
+
const choicesText = parts[1].trim();
|
102 |
|
103 |
+
const div = document.createElement('div');
|
104 |
div.className = 'quiz-question';
|
105 |
div.innerHTML = `<strong>${index + 1}: ${questionText}</strong>`;
|
106 |
|
107 |
+
const choices = choicesText.split(/\s*[A-D]\)\s*/).filter(choice => choice.trim() !== '');
|
108 |
+
const labels = choicesText.match(/\s*[A-D]\)\s*/g);
|
109 |
|
110 |
choices.forEach((choice, i) => {
|
111 |
+
const label = labels[i].trim().charAt(0);
|
112 |
|
113 |
+
const choiceDiv = document.createElement('div');
|
114 |
|
115 |
+
const input = document.createElement('input');
|
116 |
input.type = 'radio';
|
117 |
input.name = `question_${index + 1}`;
|
118 |
input.value = label;
|
119 |
input.id = `q${index + 1}_${label}`;
|
120 |
|
121 |
+
const choiceLabel = document.createElement('label');
|
122 |
choiceLabel.setAttribute('for', `q${index + 1}_${label}`);
|
123 |
choiceLabel.textContent = `${label}) ${choice}`;
|
124 |
|
|
|
128 |
div.appendChild(choiceDiv);
|
129 |
});
|
130 |
|
131 |
+
const answerDiv = document.createElement('div');
|
132 |
answerDiv.className = 'quiz-answer';
|
133 |
answerDiv.style.display = 'none';
|
134 |
div.appendChild(answerDiv);
|
135 |
|
136 |
+
const explanationDiv = document.createElement('div');
|
137 |
explanationDiv.className = 'quiz-explanation';
|
138 |
explanationDiv.style.display = 'none';
|
139 |
div.appendChild(explanationDiv);
|
|
|
143 |
}
|
144 |
|
145 |
function validateQuiz() {
|
146 |
+
const quizSection = document.getElementById('quizSection');
|
147 |
+
const questions = quizSection.getElementsByClassName('quiz-question');
|
148 |
let allAnswered = true;
|
149 |
|
150 |
for (let i = 0; i < questions.length; i++) {
|
151 |
+
const inputs = questions[i].querySelectorAll('input[type="radio"]');
|
152 |
+
const answered = Array.from(inputs).some(input => input.checked);
|
153 |
|
154 |
if (!answered) {
|
155 |
allAnswered = false;
|
|
|
178 |
})
|
179 |
.then(response => response.json())
|
180 |
.then(data => {
|
181 |
+
console.log('Server response:', data);
|
182 |
+
const answersAndExplanations = data.answers_and_explanations || [];
|
183 |
+
const questions = document.getElementsByClassName('quiz-question');
|
184 |
|
185 |
for (let i = 0; i < questions.length; i++) {
|
186 |
+
const answerDiv = questions[i].querySelector('.quiz-answer');
|
187 |
+
const explanationDiv = questions[i].querySelector('.quiz-explanation');
|
188 |
|
189 |
if (answersAndExplanations[i]) {
|
190 |
+
const answer = answersAndExplanations[i].Answer || '';
|
191 |
+
const explanation = answersAndExplanations[i].Explanation || '';
|
192 |
|
193 |
answerDiv.style.display = 'block';
|
194 |
answerDiv.innerHTML = `Answer: ${answer}`;
|