Spaces:
Sleeping
Sleeping
Feat: Improve quiz mode with level-based filtering, inline feedback, and retry logic
Browse files- frontend/static/js/script.js +93 -10
- frontend/templates/quiz.html +9 -4
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1672 |
} else {
|
| 1673 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|
| 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
|
| 1696 |
}
|
| 1697 |
|
| 1698 |
async function loadChildrenForDashboard() {
|
|
@@ -1710,7 +1787,13 @@ async function loadChildrenForDashboard() {
|
|
| 1710 |
}
|
| 1711 |
|
| 1712 |
function getLevelInfo(child) {
|
| 1713 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 127 |
if (indicator) indicator.innerText = levelInfo.name;
|
| 128 |
|
| 129 |
document.querySelectorAll('.kids-card').forEach(card => {
|
| 130 |
-
if (card.classList.contains(`age-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';
|