File size: 7,783 Bytes
9d5eab4
 
 
 
 
a0cc8b0
9d5eab4
 
 
a0cc8b0
9d5eab4
 
a0cc8b0
 
 
 
74bbd61
 
 
 
 
 
 
 
 
 
 
 
a0cc8b0
 
9d5eab4
a0cc8b0
 
 
 
f93d4b6
a0cc8b0
407be16
a0cc8b0
 
 
 
407be16
a0cc8b0
 
407be16
a0cc8b0
 
 
 
90107a7
9d5eab4
 
 
a0cc8b0
 
 
 
 
90107a7
9d5eab4
 
 
5c215d6
3a3be9f
74bbd61
702b6bf
74bbd61
 
 
702b6bf
74bbd61
 
702b6bf
74bbd61
3a3be9f
74bbd61
 
 
 
 
702b6bf
74bbd61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a3be9f
74bbd61
3a3be9f
702b6bf
 
74bbd61
 
 
 
 
 
 
702b6bf
 
 
 
3a3be9f
74bbd61
 
3a3be9f
a0cc8b0
3a3be9f
9d5eab4
a0cc8b0
 
 
3a3be9f
a0cc8b0
9d5eab4
 
3a3be9f
a0cc8b0
 
9d5eab4
a0cc8b0
3a3be9f
a0cc8b0
3a3be9f
a0cc8b0
9d5eab4
 
13b91f4
 
a0cc8b0
3a3be9f
a0cc8b0
9d5eab4
13b91f4
3a3be9f
9d5eab4
 
3a3be9f
9d5eab4
3a3be9f
a0cc8b0
 
 
9d5eab4
3a3be9f
a0cc8b0
9d5eab4
13b91f4
9d5eab4
3a3be9f
a0cc8b0
 
13b91f4
9d5eab4
3a3be9f
74bbd61
 
3a3be9f
74bbd61
9d5eab4
 
 
a0cc8b0
 
3a3be9f
9d5eab4
a0cc8b0
 
3a3be9f
a0cc8b0
 
 
 
 
 
3a3be9f
9d5eab4
a0cc8b0
9d5eab4
 
3a3be9f
a0cc8b0
9d5eab4
 
 
 
a0cc8b0
9d5eab4
 
3a3be9f
74bbd61
 
702b6bf
 
3a3be9f
74bbd61
 
 
3a3be9f
74bbd61
 
3a3be9f
74bbd61
 
 
3a3be9f
74bbd61
9d5eab4
 
 
5c215d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Quiz Generator</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            padding: 20px;
        }
        #app {
            max-width: 800px;
            margin: 0 auto;
        }
        button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            border-radius: 4px;
            font-size: 14px;
        }
        button:hover {
            background-color: #45a049;
        }
        .quiz-section {
            margin-top: 20px;
        }
        .quiz-question {
            margin-bottom: 20px;
            border: 1px solid #ccc;
            padding: 15px;
            border-radius: 4px;
            background-color: #f9f9f9;
        }
        .quiz-question strong {
            font-weight: bold;
        }
        .quiz-answer {
            margin-top: 10px;
            font-style: italic;
            display: none;
        }
        #submitQuiz {
            display: none; 
            margin-top: 20px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="app">
        <div id="quizSection" class="quiz-section"></div>

        <div id="submitQuiz">
            <button onclick="submitQuiz()">Submit Quiz</button>
        </div>
    </div>

    <script>
        // Use the passed response JSON directly
        let textWithAnswers = `{{ textWithAnswers|tojson|safe }}`;
        window.onload = function() {
            document.getElementById('quizText').value = textWithAnswers;
            generateQuiz(); 
        };

        function handletext(text) {
            let lines = text.split(/\n+/);
            let filteredLines = [];
            let questionCount = 0;
            let answersAndExplanations = [];

            lines.forEach(line => {
                line = line.trim();
                
                if (line.startsWith("Question:")) {
                    filteredLines.push(line);
                    questionCount++;
                } else if (line.startsWith("Choices:")) {
                    filteredLines.push(line);
                } else if (line.startsWith("Answer:")) {
                    let answer = line.split(":")[1].trim();
                    answersAndExplanations.push({ Answer: answer });
                } else if (line.startsWith("Explanation:")) {
                    let explanation = line.split(":")[1].trim();
                    answersAndExplanations[answersAndExplanations.length - 1].Explanation = explanation;
                } else if (line.match(/^[A-D]\)\s*/)) {
                    filteredLines.push(line);
                } else if (line.startsWith("    ")) {
                } else {
                    filteredLines.push(line);
                }
            });

            let filteredText = filteredLines.join("\n");

            console.log(filteredText);

            return {
                filteredText: filteredText,
                answersAndExplanations: answersAndExplanations
            };
        }

        function generateQuiz() {
            let quizText = document.getElementById('quizText').value;
            let { filteredText, answersAndExplanations } = handletext(quizText);

            document.getElementById('inputSection').style.display = 'none';

            let quizSection = document.getElementById('quizSection');
            quizSection.innerHTML = ''; 

            let questions = filteredText.split(/\d+\.\s+/).filter(q => q.trim() !== '');

            questions.forEach((question, index) => {
                let parts = question.split('Choices:');
                let questionText = parts[0].replace('Question:', '').trim();
                let choicesText = parts[1].trim();

                let div = document.createElement('div');
                div.className = 'quiz-question';
                div.innerHTML = `<strong>${index + 1}: ${questionText}</strong>`;

                let choices = choicesText.split(/\s*[A-D]\)\s*/).filter(choice => choice.trim() !== '');
                let labels = choicesText.match(/\s*[A-D]\)\s*/g);
                choices.forEach((choice, i) => {
                    let label = labels[i].trim().charAt(0); 

                    let choiceDiv = document.createElement('div');

                    let input = document.createElement('input');
                    input.type = 'radio';
                    input.name = `question_${index + 1}`;
                    input.value = label; 
                    input.id = `q${index + 1}_${label}`; 
                    input.setAttribute('data-answered', 'false'); 

                    let choiceLabel = document.createElement('label');
                    choiceLabel.setAttribute('for', `q${index + 1}_${label}`);
                    choiceLabel.textContent = `${label}) ${choice}`;

                    choiceDiv.appendChild(input);
                    choiceDiv.appendChild(choiceLabel);

                    div.appendChild(choiceDiv);

                    input.addEventListener('change', function() {
                        input.setAttribute('data-answered', 'true');
                    });
                });

                let answerDiv = document.createElement('div');
                answerDiv.className = 'quiz-answer';
                answerDiv.style.display = 'none'; 
                div.appendChild(answerDiv);

                let explanationDiv = document.createElement('div');
                explanationDiv.className = 'quiz-answer';
                explanationDiv.style.display = 'none'; 
                div.appendChild(explanationDiv);

                quizSection.appendChild(div);
            });

            document.getElementById('submitQuiz').style.display = 'block';
        }

        function validateQuiz() {
            let quizSection = document.getElementById('quizSection');
            let questions = quizSection.getElementsByClassName('quiz-question');

            for (let i = 0; i < questions.length; i++) {
                let inputs = questions[i].querySelectorAll('input[type="radio"]');
                let answered = false;

                for (let j = 0; j < inputs.length; j++) {
                    if (inputs[j].checked) {
                        answered = true;
                        break;
                    }
                }

                if (!answered) {
                    return false; 
                }
            }

            return true; 
        }

        function submitQuiz() {
            if (!validateQuiz()) {
                alert('Please answer all questions before submitting.');
                return;
            }

            let quizSection = document.getElementById('quizSection');
            let answerDivs = quizSection.getElementsByClassName('quiz-answer');
            let quizText = document.getElementById('quizText').value;
            let { filteredText, answersAndExplanations } = handletext(quizText);

            for (let i = 0; i < answerDivs.length; i += 2) {
                let answer = answersAndExplanations[i / 2]?.Answer || '';
                let explanation = answersAndExplanations[i / 2]?.Explanation || '';

                answerDivs[i].style.display = 'block';
                answerDivs[i].innerHTML = `Answer: ${answer}`;

                answerDivs[i + 1].style.display = 'block';
                answerDivs[i + 1].innerHTML = `Explanation: ${explanation}`;
            }

            document.getElementById('submitQuiz').style.display = 'none';
        }
    </script>
</body>
</html>