nehal2006 commited on
Commit
37de2f1
Β·
1 Parent(s): 36ee2b5

Feat: Improve quiz mode with level-based filtering, inline feedback, and retry logic

Browse files
frontend/static/js/script.js CHANGED
@@ -105,6 +105,14 @@ const quizData = {
105
  { text: "It was your fault! 😠", correct: false },
106
  { text: "Say nothing 🀐", correct: false }
107
  ]
 
 
 
 
 
 
 
 
108
  }
109
  ],
110
  'Daily Routine': [
@@ -139,6 +147,14 @@ const quizData = {
139
  { text: "On the floor 🧹", correct: false },
140
  { text: "In the fridge 🧊", correct: false }
141
  ]
 
 
 
 
 
 
 
 
142
  }
143
  ],
144
  'Safety & Help': [
@@ -165,6 +181,22 @@ const quizData = {
165
  { text: "Keep running πŸƒ", correct: false },
166
  { text: "Cry all day 😭", correct: false }
167
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
  }
169
  ],
170
  'Advanced Social Skills': [
@@ -191,6 +223,22 @@ const quizData = {
191
  { text: "Jump into the middle of the game πŸƒ", correct: false },
192
  { text: "Take the ball away ⚽", correct: false }
193
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  }
195
  ]
196
  };
@@ -1654,45 +1702,74 @@ function loadQuizQuestion() {
1654
  document.getElementById('quiz-question-text').innerText = q.question;
1655
  const progress = (currentQuestionIndex / questions.length) * 100;
1656
  document.getElementById('progress-fill').style.width = `${progress}%`;
 
 
 
 
 
1657
  const optionsGrid = document.getElementById('quiz-options-grid');
1658
  optionsGrid.innerHTML = '';
1659
  q.options.forEach(opt => {
1660
  const div = document.createElement('div');
1661
  div.className = 'card quiz-option';
1662
  div.innerHTML = `<span>${opt.text.split(' ').pop()}</span><p>${opt.text.split(' ').slice(0, -1).join(' ')}</p>`;
1663
- div.onclick = () => window.selectQuizOption(opt.correct);
1664
  optionsGrid.appendChild(div);
1665
  });
1666
  }
1667
 
1668
- window.selectQuizOption = function(isCorrect) {
 
1669
  if (isCorrect) {
1670
  quizScore++;
1671
- alert("Correct! Great thinking! 🌟");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1672
  } else {
1673
- alert("Not quite. Let's try to remember for next time! πŸ‘");
 
 
 
 
 
 
1674
  }
1675
- currentQuestionIndex++;
1676
- if (currentQuestionIndex < quizData[currentQuizType].length) loadQuizQuestion();
1677
- else finishQuiz();
1678
  };
1679
 
1680
  async function finishQuiz() {
1681
  const total = quizData[currentQuizType].length;
 
 
 
1682
  await fetch('/activities/log-quiz', {
1683
  method: 'POST',
1684
  headers: { 'Content-Type': 'application/json' },
1685
  body: JSON.stringify({
1686
  child_id: selectedChildId,
1687
  quiz_name: currentQuizType,
1688
- score: quizScore,
1689
  total_questions: total
1690
  })
1691
  });
1692
  document.getElementById('progress-fill').style.width = `100%`;
1693
  document.getElementById('quiz-content').classList.add('hidden');
1694
  document.getElementById('quiz-results').classList.remove('hidden');
1695
- document.getElementById('final-score-text').innerText = `You got ${quizScore} out of ${total} correct!`;
1696
  }
1697
 
1698
  async function loadChildrenForDashboard() {
@@ -1710,7 +1787,13 @@ async function loadChildrenForDashboard() {
1710
  }
1711
 
1712
  function getLevelInfo(child) {
1713
- const level = child.level || 1;
 
 
 
 
 
 
1714
  if (level === 3) return { level: 3, name: "Level 3: Advanced (10+ or High-Risk)", desc: "Complex emotions and mood analysis.", theme: "purple" };
1715
  if (level === 2) return { level: 2, name: "Level 2: Intermediate (7-9 or History)", desc: "Patterns and alphabetical logic.", theme: "sand" };
1716
  return { level: 1, name: "Level 1: Basic (3-6)", desc: "Colors, shapes, and basic emotions.", theme: "orange" };
 
105
  { text: "It was your fault! 😠", correct: false },
106
  { text: "Say nothing 🀐", correct: false }
107
  ]
108
+ },
109
+ {
110
+ question: "A friend is talking, but you have something to say too. What should you do?",
111
+ options: [
112
+ { text: "Wait for them to finish, then speak πŸ™Š", correct: true },
113
+ { text: "Interrupt them immediately πŸ“’", correct: false },
114
+ { text: "Start talking louder πŸ—£οΈ", correct: false }
115
+ ]
116
  }
117
  ],
118
  'Daily Routine': [
 
147
  { text: "On the floor 🧹", correct: false },
148
  { text: "In the fridge 🧊", correct: false }
149
  ]
150
+ },
151
+ {
152
+ question: "What do we do before going to bed at night?",
153
+ options: [
154
+ { text: "Put on pajamas and read a story πŸ“–", correct: true },
155
+ { text: "Go for a swim 🏊", correct: false },
156
+ { text: "Eat a big meal πŸ•", correct: false }
157
+ ]
158
  }
159
  ],
160
  'Safety & Help': [
 
181
  { text: "Keep running πŸƒ", correct: false },
182
  { text: "Cry all day 😭", correct: false }
183
  ]
184
+ },
185
+ {
186
+ question: "A stranger asks you to go with them. What do you do?",
187
+ options: [
188
+ { text: "Say 'NO' and run to a safe adult πŸ›‘", correct: true },
189
+ { text: "Go with them 🚢", correct: false },
190
+ { text: "Take the candy they offer 🍬", correct: false }
191
+ ]
192
+ },
193
+ {
194
+ question: "What number should you know for emergencies?",
195
+ options: [
196
+ { text: "911 (or your local emergency number) ☎️", correct: true },
197
+ { text: "123 πŸ”’", correct: false },
198
+ { text: "555 πŸ“ž", correct: false }
199
+ ]
200
  }
201
  ],
202
  'Advanced Social Skills': [
 
223
  { text: "Jump into the middle of the game πŸƒ", correct: false },
224
  { text: "Take the ball away ⚽", correct: false }
225
  ]
226
+ },
227
+ {
228
+ question: "You see someone sitting alone at recess. What could you do?",
229
+ options: [
230
+ { text: "Ask if they want to play with you 🀝", correct: true },
231
+ { text: "Ignore them πŸ™ˆ", correct: false },
232
+ { text: "Point and laugh ☝️", correct: false }
233
+ ]
234
+ },
235
+ {
236
+ question: "If your friend wins a game and you lose, what should you say?",
237
+ options: [
238
+ { text: "Good game! You did well! πŸ‘", correct: true },
239
+ { text: "I hate this game! 😠", correct: false },
240
+ { text: "You cheated! 😀", correct: false }
241
+ ]
242
  }
243
  ]
244
  };
 
1702
  document.getElementById('quiz-question-text').innerText = q.question;
1703
  const progress = (currentQuestionIndex / questions.length) * 100;
1704
  document.getElementById('progress-fill').style.width = `${progress}%`;
1705
+
1706
+ // Reset feedback
1707
+ const feedback = document.getElementById('quiz-feedback');
1708
+ if (feedback) feedback.innerText = "";
1709
+
1710
  const optionsGrid = document.getElementById('quiz-options-grid');
1711
  optionsGrid.innerHTML = '';
1712
  q.options.forEach(opt => {
1713
  const div = document.createElement('div');
1714
  div.className = 'card quiz-option';
1715
  div.innerHTML = `<span>${opt.text.split(' ').pop()}</span><p>${opt.text.split(' ').slice(0, -1).join(' ')}</p>`;
1716
+ div.onclick = () => window.selectQuizOption(opt.correct, div);
1717
  optionsGrid.appendChild(div);
1718
  });
1719
  }
1720
 
1721
+ window.selectQuizOption = function(isCorrect, element) {
1722
+ const feedback = document.getElementById('quiz-feedback');
1723
  if (isCorrect) {
1724
  quizScore++;
1725
+ if (feedback) {
1726
+ feedback.innerText = "Correct! Great thinking! 🌟";
1727
+ feedback.style.color = "var(--success)";
1728
+ }
1729
+ element.style.borderColor = "var(--success)";
1730
+ element.style.background = "#e8f5e9";
1731
+
1732
+ // Disable all options during the transition
1733
+ document.querySelectorAll('.quiz-option').forEach(opt => opt.onclick = null);
1734
+
1735
+ setTimeout(() => {
1736
+ currentQuestionIndex++;
1737
+ if (currentQuestionIndex < quizData[currentQuizType].length) {
1738
+ loadQuizQuestion();
1739
+ } else {
1740
+ finishQuiz();
1741
+ }
1742
+ }, 1500);
1743
  } else {
1744
+ if (feedback) {
1745
+ feedback.innerText = "Not quite. Let's try again! πŸ‘";
1746
+ feedback.style.color = "#e74c3c";
1747
+ }
1748
+ element.style.borderColor = "#e74c3c";
1749
+ element.style.opacity = "0.7";
1750
+ // Do NOT increment index, allowing retry
1751
  }
 
 
 
1752
  };
1753
 
1754
  async function finishQuiz() {
1755
  const total = quizData[currentQuizType].length;
1756
+ // Every question was eventually answered correctly
1757
+ const finalScore = total;
1758
+
1759
  await fetch('/activities/log-quiz', {
1760
  method: 'POST',
1761
  headers: { 'Content-Type': 'application/json' },
1762
  body: JSON.stringify({
1763
  child_id: selectedChildId,
1764
  quiz_name: currentQuizType,
1765
+ score: finalScore,
1766
  total_questions: total
1767
  })
1768
  });
1769
  document.getElementById('progress-fill').style.width = `100%`;
1770
  document.getElementById('quiz-content').classList.add('hidden');
1771
  document.getElementById('quiz-results').classList.remove('hidden');
1772
+ document.getElementById('final-score-text').innerText = `Amazing! You completed all ${total} questions! πŸŽ‰`;
1773
  }
1774
 
1775
  async function loadChildrenForDashboard() {
 
1787
  }
1788
 
1789
  function getLevelInfo(child) {
1790
+ let level = 1;
1791
+ if (typeof child === 'number') {
1792
+ level = child;
1793
+ } else if (child && typeof child === 'object') {
1794
+ level = child.level || 1;
1795
+ }
1796
+
1797
  if (level === 3) return { level: 3, name: "Level 3: Advanced (10+ or High-Risk)", desc: "Complex emotions and mood analysis.", theme: "purple" };
1798
  if (level === 2) return { level: 2, name: "Level 2: Intermediate (7-9 or History)", desc: "Patterns and alphabetical logic.", theme: "sand" };
1799
  return { level: 1, name: "Level 1: Basic (3-6)", desc: "Colors, shapes, and basic emotions.", theme: "orange" };
frontend/templates/quiz.html CHANGED
@@ -72,7 +72,7 @@
72
  <button class="btn-kids">Start Quiz</button>
73
  </div>
74
 
75
- <div class="kids-card theme-orange age-level-2 age-level-3" onclick="startNewQuiz('Safety & Help')">
76
  <span class="icon">πŸ†˜</span>
77
  <h3>Safety & Help</h3>
78
  <p>Learn who to ask for help and stay safe.</p>
@@ -101,6 +101,7 @@
101
  <div id="quiz-options-grid" class="grid" style="grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));">
102
  <!-- Options injected via JS -->
103
  </div>
 
104
  </div>
105
 
106
  <div id="quiz-results" class="hidden" style="text-align: center; padding: 20px;">
@@ -118,16 +119,20 @@
118
  filterQuizzesByAge();
119
  });
120
 
121
- function filterQuizzesByAge() {
122
  const ageStr = localStorage.getItem('selectedChildAge');
 
123
  const age = ageStr ? parseInt(ageStr) : 0;
 
 
124
  const indicator = document.getElementById('age-level-indicator');
125
 
126
- const levelInfo = getLevelInfo(age);
 
127
  if (indicator) indicator.innerText = levelInfo.name;
128
 
129
  document.querySelectorAll('.kids-card').forEach(card => {
130
- if (card.classList.contains(`age-level-${levelInfo.level}`)) {
131
  card.style.display = 'flex';
132
  } else {
133
  card.style.display = 'none';
 
72
  <button class="btn-kids">Start Quiz</button>
73
  </div>
74
 
75
+ <div class="kids-card theme-orange age-level-1 age-level-2 age-level-3" onclick="startNewQuiz('Safety & Help')">
76
  <span class="icon">πŸ†˜</span>
77
  <h3>Safety & Help</h3>
78
  <p>Learn who to ask for help and stay safe.</p>
 
101
  <div id="quiz-options-grid" class="grid" style="grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));">
102
  <!-- Options injected via JS -->
103
  </div>
104
+ <div id="quiz-feedback" style="text-align: center; margin-top: 20px; font-size: 1.5rem; font-weight: bold; min-height: 2.5rem;"></div>
105
  </div>
106
 
107
  <div id="quiz-results" class="hidden" style="text-align: center; padding: 20px;">
 
119
  filterQuizzesByAge();
120
  });
121
 
122
+ async function filterQuizzesByAge() {
123
  const ageStr = localStorage.getItem('selectedChildAge');
124
+ const levelStr = localStorage.getItem('selectedChildLevel');
125
  const age = ageStr ? parseInt(ageStr) : 0;
126
+ const level = levelStr ? parseInt(levelStr) : 1;
127
+
128
  const indicator = document.getElementById('age-level-indicator');
129
 
130
+ // Use the stored level for selection
131
+ const levelInfo = getLevelInfo({ level, age });
132
  if (indicator) indicator.innerText = levelInfo.name;
133
 
134
  document.querySelectorAll('.kids-card').forEach(card => {
135
+ if (card.classList.contains(`age-level-${level}`)) {
136
  card.style.display = 'flex';
137
  } else {
138
  card.style.display = 'none';