mou3az commited on
Commit
74bbd61
1 Parent(s): 5eed380

Update templates/quiz.html

Browse files
Files changed (1) hide show
  1. templates/quiz.html +111 -35
templates/quiz.html CHANGED
@@ -14,6 +14,29 @@
14
  max-width: 800px;
15
  margin: 0 auto;
16
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  .quiz-section {
18
  margin-top: 20px;
19
  }
@@ -42,6 +65,11 @@
42
  <body>
43
  <div id="app">
44
  <h1>Quiz Generator</h1>
 
 
 
 
 
45
  <div id="quizSection" class="quiz-section"></div>
46
 
47
  <div id="submitQuiz">
@@ -50,97 +78,130 @@
50
  </div>
51
 
52
  <script>
53
- document.addEventListener("DOMContentLoaded", function() {
54
- fetch('/')
55
- .then(response => response.json())
56
- .then(data => {
57
- if (data.error) {
58
- alert(data.error);
59
- return;
60
- }
61
- displayQuiz(data.filtered_text, data.answers_and_explanations);
62
- })
63
- .catch(error => console.error('Error:', error));
64
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
- function displayQuiz(filteredText, answersAndExplanations) {
67
- document.getElementById('quizSection').innerHTML = '';
68
  let questions = filteredText.split(/\d+\.\s+/).filter(q => q.trim() !== '');
 
69
  questions.forEach((question, index) => {
70
  let parts = question.split('Choices:');
71
  let questionText = parts[0].replace('Question:', '').trim();
72
  let choicesText = parts[1].trim();
 
73
  let div = document.createElement('div');
74
  div.className = 'quiz-question';
75
  div.innerHTML = `<strong>${index + 1}: ${questionText}</strong>`;
 
76
  let choices = choicesText.split(/\s*[A-D]\)\s*/).filter(choice => choice.trim() !== '');
77
  let labels = choicesText.match(/\s*[A-D]\)\s*/g);
78
  choices.forEach((choice, i) => {
79
  let label = labels[i].trim().charAt(0);
 
80
  let choiceDiv = document.createElement('div');
 
81
  let input = document.createElement('input');
82
  input.type = 'radio';
83
  input.name = `question_${index + 1}`;
84
  input.value = label;
85
  input.id = `q${index + 1}_${label}`;
86
  input.setAttribute('data-answered', 'false');
 
87
  let choiceLabel = document.createElement('label');
88
  choiceLabel.setAttribute('for', `q${index + 1}_${label}`);
89
  choiceLabel.textContent = `${label}) ${choice}`;
 
90
  choiceDiv.appendChild(input);
91
  choiceDiv.appendChild(choiceLabel);
 
92
  div.appendChild(choiceDiv);
 
93
  input.addEventListener('change', function() {
94
  input.setAttribute('data-answered', 'true');
95
  });
96
  });
 
97
  let answerDiv = document.createElement('div');
98
  answerDiv.className = 'quiz-answer';
99
  answerDiv.style.display = 'none';
100
  div.appendChild(answerDiv);
 
101
  let explanationDiv = document.createElement('div');
102
  explanationDiv.className = 'quiz-answer';
103
  explanationDiv.style.display = 'none';
104
  div.appendChild(explanationDiv);
105
- document.getElementById('quizSection').appendChild(div);
106
- });
107
 
108
- let submitSection = document.getElementById('submitQuiz');
109
- if (answersAnd_explanations) {
110
- submitSection.style.display = 'block';
111
- displayAnswers(answersAnd_explanations);
112
- }
113
- }
114
 
115
- function displayAnswers(answersAndExplanations) {
116
- let quizSection = document.getElementById('quizSection');
117
- let answerDivs = quizSection.getElementsByClassName('quiz-answer');
118
- for (let i = 0; i < answerDivs.length; i += 2) {
119
- let answer = answersAndExplanations[i / 2]?.Answer || '';
120
- let explanation = answersAndExplanations[i / 2]?.Explanation || '';
121
- answerDivs[i].style.display = 'block';
122
- answerDivs[i].innerHTML = `Answer: ${answer}`;
123
- answerDivs[i + 1].style.display = 'block';
124
- answerDivs[i + 1].innerHTML = `Explanation: ${explanation}`;
125
- }
126
  }
127
 
128
  function validateQuiz() {
129
  let quizSection = document.getElementById('quizSection');
130
  let questions = quizSection.getElementsByClassName('quiz-question');
 
131
  for (let i = 0; i < questions.length; i++) {
132
  let inputs = questions[i].querySelectorAll('input[type="radio"]');
133
  let answered = false;
 
134
  for (let j = 0; j < inputs.length; j++) {
135
  if (inputs[j].checked) {
136
  answered = true;
137
  break;
138
  }
139
  }
 
140
  if (!answered) {
141
  return false;
142
  }
143
  }
 
144
  return true;
145
  }
146
 
@@ -149,8 +210,23 @@
149
  alert('Please answer all questions before submitting.');
150
  return;
151
  }
152
- // Add functionality to handle quiz submission, if needed
153
- alert('Quiz submitted successfully!');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  }
155
  </script>
156
  </body>
 
14
  max-width: 800px;
15
  margin: 0 auto;
16
  }
17
+ textarea {
18
+ width: 100%;
19
+ height: 200px;
20
+ margin-bottom: 10px;
21
+ padding: 10px;
22
+ box-sizing: border-box;
23
+ border: 1px solid #ccc;
24
+ border-radius: 4px;
25
+ font-size: 14px;
26
+ line-height: 1.4;
27
+ }
28
+ button {
29
+ padding: 10px 20px;
30
+ background-color: #4CAF50;
31
+ color: white;
32
+ border: none;
33
+ cursor: pointer;
34
+ border-radius: 4px;
35
+ font-size: 14px;
36
+ }
37
+ button:hover {
38
+ background-color: #45a049;
39
+ }
40
  .quiz-section {
41
  margin-top: 20px;
42
  }
 
65
  <body>
66
  <div id="app">
67
  <h1>Quiz Generator</h1>
68
+ <div id="inputSection" style="display: none;">
69
+ <textarea id="quizText" placeholder="Paste or type your quiz text here..."></textarea>
70
+ <button onclick="generateQuiz()">Generate Quiz</button>
71
+ </div>
72
+
73
  <div id="quizSection" class="quiz-section"></div>
74
 
75
  <div id="submitQuiz">
 
78
  </div>
79
 
80
  <script>
81
+ // Use the passed textWithAnswers directly
82
+ let textWithAnswers = `{{ textWithAnswers|tojson }}`;
83
+
84
+ window.onload = function() {
85
+ generateQuiz();
86
+ };
87
+
88
+ function handletext(text) {
89
+ let lines = text.split(/\n+/);
90
+ let filteredLines = [];
91
+ let answersAndExplanations = [];
92
+
93
+ lines.forEach(line => {
94
+ line = line.trim();
95
+
96
+ if (line.startsWith("Question:")) {
97
+ filteredLines.push(line);
98
+ } else if (line.startsWith("Choices:")) {
99
+ filteredLines.push(line);
100
+ } else if (line.startsWith("Answer:")) {
101
+ let answer = line.split(":")[1].trim();
102
+ answersAndExplanations.push({ Answer: answer });
103
+ } else if (line.startsWith("Explanation:")) {
104
+ let explanation = line.split(":")[1].trim();
105
+ answersAndExplanations[answersAndExplanations.length - 1].Explanation = explanation;
106
+ } else if (line.match(/^[A-D]\)\s*/)) {
107
+ filteredLines.push(line);
108
+ } else if (line.startsWith(" ")) {
109
+ } else {
110
+ filteredLines.push(line);
111
+ }
112
+ });
113
+
114
+ let filteredText = filteredLines.join("\n");
115
+
116
+ return {
117
+ filteredText: filteredText,
118
+ answersAndExplanations: answersAndExplanations
119
+ };
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
  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>