Create quiz.html
Browse files- templates/quiz.html +184 -0
templates/quiz.html
ADDED
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>Generated Quiz</title>
|
7 |
+
<style>
|
8 |
+
body {
|
9 |
+
font-family: Arial, sans-serif;
|
10 |
+
background-color: #f0f0f0;
|
11 |
+
color: #333;
|
12 |
+
margin: 20px;
|
13 |
+
}
|
14 |
+
h2 {
|
15 |
+
background-color: #ffc107;
|
16 |
+
padding: 10px;
|
17 |
+
border-radius: 5px;
|
18 |
+
}
|
19 |
+
.quiz-container {
|
20 |
+
background-color: #fff;
|
21 |
+
padding: 20px;
|
22 |
+
border-radius: 5px;
|
23 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
24 |
+
margin-bottom: 20px;
|
25 |
+
}
|
26 |
+
.quiz-container pre {
|
27 |
+
white-space: pre-wrap;
|
28 |
+
}
|
29 |
+
.generate-link {
|
30 |
+
display: inline-block;
|
31 |
+
margin-top: 10px;
|
32 |
+
border: 2px solid transparent;
|
33 |
+
padding: 5px 10px;
|
34 |
+
text-decoration: none;
|
35 |
+
color: #ffc107;
|
36 |
+
font-weight: bold;
|
37 |
+
border-radius: 4px;
|
38 |
+
background-color: #333;
|
39 |
+
}
|
40 |
+
.generate-link:hover {
|
41 |
+
text-decoration: none;
|
42 |
+
background-color: #555;
|
43 |
+
}
|
44 |
+
a {
|
45 |
+
color: #ffc107;
|
46 |
+
text-decoration: none;
|
47 |
+
}
|
48 |
+
a:hover {
|
49 |
+
text-decoration: underline;
|
50 |
+
}
|
51 |
+
</style>
|
52 |
+
</head>
|
53 |
+
<body>
|
54 |
+
<div class="quiz-container">
|
55 |
+
<h2>Generated Quiz</h2>
|
56 |
+
<pre>{{ filtered_text }}</pre>
|
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>
|
63 |
+
document.addEventListener('DOMContentLoaded', function() {
|
64 |
+
generateQuiz("{{ filtered_text }}", {{ answers_and_explanations | tojson }});
|
65 |
+
});
|
66 |
+
|
67 |
+
function generateQuiz(filteredText, answersAndExplanations) {
|
68 |
+
document.getElementById('inputSection').style.display = 'none';
|
69 |
+
let quizSection = document.getElementById('quizSection');
|
70 |
+
quizSection.innerHTML = '';
|
71 |
+
|
72 |
+
let questions = filteredText.split(/\d+\.\s+/).filter(q => q.trim() !== '');
|
73 |
+
|
74 |
+
questions.forEach((question, index) => {
|
75 |
+
let parts = question.split('Choices:');
|
76 |
+
let questionText = parts[0].replace('Question:', '').trim();
|
77 |
+
let choicesText = parts[1].trim();
|
78 |
+
|
79 |
+
let div = document.createElement('div');
|
80 |
+
div.className = 'quiz-question';
|
81 |
+
div.innerHTML = `<strong>${index + 1}: ${questionText}</strong>`;
|
82 |
+
|
83 |
+
let choices = choicesText.split(/\s*[A-D]\)\s*/).filter(choice => choice.trim() !== '');
|
84 |
+
let labels = choicesText.match(/\s*[A-D]\)\s*/g);
|
85 |
+
choices.forEach((choice, i) => {
|
86 |
+
let label = labels[i].trim().charAt(0);
|
87 |
+
|
88 |
+
let choiceDiv = document.createElement('div');
|
89 |
+
|
90 |
+
let input = document.createElement('input');
|
91 |
+
input.type = 'radio';
|
92 |
+
input.name = `question_${index + 1}`;
|
93 |
+
input.value = label;
|
94 |
+
input.id = `q${index + 1}_${label}`;
|
95 |
+
input.setAttribute('data-answered', 'false');
|
96 |
+
|
97 |
+
let choiceLabel = document.createElement('label');
|
98 |
+
choiceLabel.setAttribute('for', `q${index + 1}_${label}`);
|
99 |
+
choiceLabel.textContent = `${label}) ${choice}`;
|
100 |
+
|
101 |
+
choiceDiv.appendChild(input);
|
102 |
+
choiceDiv.appendChild(choiceLabel);
|
103 |
+
|
104 |
+
div.appendChild(choiceDiv);
|
105 |
+
|
106 |
+
input.addEventListener('change', function() {
|
107 |
+
input.setAttribute('data-answered', 'true');
|
108 |
+
});
|
109 |
+
});
|
110 |
+
|
111 |
+
let answerDiv = document.createElement('div');
|
112 |
+
answerDiv.className = 'quiz-answer';
|
113 |
+
answerDiv.style.display = 'none';
|
114 |
+
div.appendChild(answerDiv);
|
115 |
+
|
116 |
+
let explanationDiv = document.createElement('div');
|
117 |
+
explanationDiv.className = 'quiz-answer';
|
118 |
+
explanationDiv.style.display = 'none';
|
119 |
+
div.appendChild(explanationDiv);
|
120 |
+
|
121 |
+
quizSection.appendChild(div);
|
122 |
+
});
|
123 |
+
|
124 |
+
document.getElementById('submitQuiz').style.display = 'block';
|
125 |
+
}
|
126 |
+
|
127 |
+
function validateQuiz() {
|
128 |
+
let quizSection = document.getElementById('quizSection');
|
129 |
+
let questions = quizSection.getElementsByClassName('quiz-question');
|
130 |
+
|
131 |
+
for (let i = 0; i < questions.length; i++) {
|
132 |
+
let inputs = questions[i].querySelectorAll('input[type="radio"]');
|
133 |
+
let answered = false;
|
134 |
+
|
135 |
+
for (let j = 0; j < inputs.length; j++) {
|
136 |
+
if (inputs[j].checked) {
|
137 |
+
answered = true;
|
138 |
+
break;
|
139 |
+
}
|
140 |
+
}
|
141 |
+
|
142 |
+
if (!answered) {
|
143 |
+
return false;
|
144 |
+
}
|
145 |
+
}
|
146 |
+
|
147 |
+
return true;
|
148 |
+
}
|
149 |
+
|
150 |
+
function submitQuiz() {
|
151 |
+
if (!validateQuiz()) {
|
152 |
+
alert('Please answer all questions before submitting.');
|
153 |
+
return;
|
154 |
+
}
|
155 |
+
|
156 |
+
let quizSection = document.getElementById('quizSection');
|
157 |
+
let answerDivs = quizSection.getElementsByClassName('quiz-answer');
|
158 |
+
fetch('/process_text', {
|
159 |
+
method: 'POST',
|
160 |
+
headers: {
|
161 |
+
'Content-Type': 'application/json'
|
162 |
+
},
|
163 |
+
body: JSON.stringify({ text: "{{ filtered_text }}" })
|
164 |
+
})
|
165 |
+
.then(response => response.json())
|
166 |
+
.then(data => {
|
167 |
+
let answersAndExplanations = data.answersAnd_explanations;
|
168 |
+
for (let i = 0; i < answerDivs.length; i += 2) {
|
169 |
+
let answer = answersAndExplanations[i / 2]?.Answer || '';
|
170 |
+
let explanation = answersAndExplanations[i / 2]?.Explanation || '';
|
171 |
+
|
172 |
+
answerDivs[i].style.display = 'block';
|
173 |
+
answerDivs[i].innerHTML = `Answer: ${answer}`;
|
174 |
+
|
175 |
+
answerDivs[i + 1].style.display = 'block';
|
176 |
+
answerDivs[i + 1].innerHTML = `Explanation: ${explanation}`;
|
177 |
+
}
|
178 |
+
|
179 |
+
document.getElementById('submitQuiz').style.display = 'none';
|
180 |
+
});
|
181 |
+
}
|
182 |
+
</script>
|
183 |
+
</body>
|
184 |
+
</html>
|