File size: 11,480 Bytes
f2f372e
 
 
 
 
 
 
 
 
484bee1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2f372e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484bee1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2f372e
 
 
484bee1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2f372e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484bee1
 
 
 
 
 
 
 
 
 
f2f372e
484bee1
f2f372e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484bee1
 
f2f372e
 
 
484bee1
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
def style():
    css = """
             .submit-btn {
                padding: 10px 20px;
                color: #333;
                background-color: #face00;
                border: 1px solid #ddd;
                border-radius: 5px;
                margin-top: 20px;
                cursor: pointer;
                transition: all 0.3s ease;
                }
             .submit-btn.disabled {
                background-color: #cccccc;
                color: #666666;
                cursor: not-allowed;
                opacity: 0.7;
                }
             .validation-alert {
                background-color: #ffdddd;
                color: #990000;
                padding: 10px;
                margin-bottom: 15px;
                border-radius: 5px;
                font-weight: bold;
                text-align: center;
                animation: fadeIn 0.5s;
                }
             .invalid-question {
                border: 2px solid #ff0000;
                padding: 10px;
                border-radius: 5px;
                margin-bottom: 10px;
                }
             @keyframes fadeIn {
                0% { opacity: 0; }
                100% { opacity: 1; }
                }
             #result {
                display: none;
                margin: 0;
                padding-left: 20px;
                padding-bottom: 20px;
                color: #000000;
                background-color: darkviolet
                }
             input[type="text"] {
                width: 100%;
                padding: 10px;
                color: #333333;
                border-radius: 5px;
                }
             #quiz-answers {
                margin-top: 30px;
                padding: 20px;
                background-color: #dfdfdf;
                border: 1px solid #ddd;
                border-radius: 5px;
             }
             #quiz-answers summary {
                display: inline-block;
                padding-bottom: 5px;
                cursor: pointer;
             }
             #quiz-answers summary, #quiz-answers h2 {
                color: #383838;
             }
             .answers-container {
                display: flex;
                flex-direction: column;
                gap: 20px;
             }
             
             .answer-item {
                padding: 15px;
                background-color: #e1ece4;
                border: 2px solid lightgreen;
                border-radius: 5px;
                box-shadow: 0 2px 4px rgba(0,0,0,0.05);
             }
             
             .answer-item h3 {
                margin-top: 0;
                color: #333;
                font-size: 18px;
             }
             
             .answer-item p {
                margin-bottom: 0;
                color: #009214;
                font-size: 16px;
                font-weight: 500;
             }
             .answer-item p strong {
                color: #1636d5;
             }
    """
    js = """
            <script>
            function validateForm() {
                const questions = document.querySelectorAll(".question");
                let isValid = true;
                
                questions.forEach((question) => {
                    const questionType = question.getAttribute("data-type");
                    const inputs = question.querySelectorAll("input");
                    
                    if (questionType === "single_choice" || questionType === "true_false") {
                        // Check if at least one radio button is selected
                        let hasChecked = false;
                        inputs.forEach((input) => {
                            if (input.checked) {
                                hasChecked = true;
                            }
                        });
                        if (!hasChecked) {
                            isValid = false;
                            // Highlight the question that needs attention
                            question.style.border = "2px solid red";
                            question.style.padding = "10px";
                            question.style.borderRadius = "5px";
                        } else {
                            question.style.border = "none";
                            question.style.padding = "0";
                        }
                    } else if (questionType === "multiple_choice") {
                        // Check if at least one checkbox is selected
                        let hasChecked = false;
                        inputs.forEach((input) => {
                            if (input.checked) {
                                hasChecked = true;
                            }
                        });
                        if (!hasChecked) {
                            isValid = false;
                            question.style.border = "2px solid red";
                            question.style.padding = "10px";
                            question.style.borderRadius = "5px";
                        } else {
                            question.style.border = "none";
                            question.style.padding = "0";
                        }
                    } else if (questionType === "fill_in_the_blank") {
                        // Check if the text input has a value
                        if (inputs[0].value.trim() === "") {
                            isValid = false;
                            question.style.border = "2px solid red";
                            question.style.padding = "10px";
                            question.style.borderRadius = "5px";
                        } else {
                            question.style.border = "none";
                            question.style.padding = "0";
                        }
                    }
                });
                
                return isValid;
            }
            
            function checkAnswers(event) {
                event.preventDefault();
                
                // First validate the form
                if (!validateForm()) {
                    // Show an alert message
                    const alertDiv = document.createElement('div');
                    alertDiv.className = 'validation-alert';
                    alertDiv.innerHTML = '<p>Please answer all questions before submitting!</p>';
                    alertDiv.style.backgroundColor = '#ffdddd';
                    alertDiv.style.color = '#990000';
                    alertDiv.style.padding = '10px';
                    alertDiv.style.borderRadius = '5px';
                    alertDiv.style.marginBottom = '15px';
                    alertDiv.style.fontWeight = 'bold';
                    alertDiv.style.textAlign = 'center';
                    
                    // Insert at the top of the form
                    const form = document.getElementById('quiz-form');
                    const existingAlert = document.querySelector('.validation-alert');
                    if (existingAlert) {
                        form.removeChild(existingAlert);
                    }
                    form.insertBefore(alertDiv, form.firstChild);
                    
                    // Scroll to the top
                    window.scrollTo({ top: 0, behavior: 'smooth' });
                    return;
                }
                
                let score = 0;
                let total = 0;
                const questions = document.querySelectorAll(".question");
                let userAnswer = "";
                const resultElement = document.getElementById("result");

                questions.forEach((question) => {
                  total++;
                  const questionType = question.getAttribute("data-type");
                  const correctAnswerJSON = question.getAttribute("data-answer");
                  const correctAnswer = JSON.parse(correctAnswerJSON);
                  const inputs = question.querySelectorAll("input");

                  if (questionType === "single_choice" || questionType === "true_false") {
                    // For radio buttons (single selection)
                    inputs.forEach((input) => {
                      if (input.checked) {
                        userAnswer = input.value;
                      }
                    });

                    if (userAnswer === correctAnswer) {
                      score++;
                    }
                  } else if (questionType === "multiple_choice") {
                    // For checkboxes (multiple selection)
                    const selectedOptions = Array.from(inputs)
                      .filter((input) => input.checked)
                      .map((input) => input.value);

                    // Compare arrays (needs to match exactly)
                    const correctArray = Array.isArray(correctAnswer)
                      ? correctAnswer
                      : [correctAnswer];
                    if (
                      selectedOptions.length === correctArray.length &&
                      selectedOptions.every((opt) => correctArray.includes(opt))
                    ) {
                      score++;
                    }
                  } else if (questionType === "fill_in_the_blank") {
                    // For text input
                    userAnswer = inputs[0].value.trim().toLowerCase();
                    const correctText = String(correctAnswer).trim().toLowerCase();

                    if (userAnswer === correctText) {
                      score++;
                    }
                  }
                });                // Remove any validation alerts once submitted successfully
                const existingAlert = document.querySelector('.validation-alert');
                if (existingAlert) {
                    existingAlert.remove();
                }
                
                // Remove any highlight on questions
                document.querySelectorAll('.question').forEach(q => {
                    q.style.border = 'none';
                    q.style.padding = '0';
                });
                
                // Display the result
                resultElement.style.display = "block";
                if (score === total) {
                  resultElement.innerHTML = `<h2>Result: </h2> <br />
                  <h3>Congratulations! You answered all questions correctly!</h3>
                  <h3>Your score: ${score}/${total}</h3>`;
                } else if (score < total / 2) {
                  resultElement.innerHTML = `<h2>Result: </h2> <br /> <h3>Keep trying! You can do better!</h3> <h3>Your score: ${score}/${total}</h3>`;
                } else {
                  resultElement.innerHTML = `<h2>Result: </h2> <br />
                  <h3>Good job! You answered ${score} out of ${total} questions correctly.</h3>
                  <h3>Your score: ${score}/${total}</h3>`;
                }
                // resultElement.style.backgroundColor = score > (total/2) ? '#e6ffe6' : '#fff0f0';
                resultElement.style.border = "1px solid #ddd";
                resultElement.style.borderRadius = "5px";
                resultElement.style.marginTop = "20px";
                answerContainer = document.getElementById('quiz-answers');
                answerContainer.style.display = 'block';
            }
            </script>
    """
    return css, js