-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuizzSelector.html
More file actions
780 lines (671 loc) · 19.9 KB
/
QuizzSelector.html
File metadata and controls
780 lines (671 loc) · 19.9 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Quiz Hub - GreenFarm</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Segoe UI', Arial, Helvetica, sans-serif; }
body { background: #e3efe7; min-height: 100vh; }
/* NAVBAR */
.navbar {
width: 100%;
background: #fff;
color: #1f3d2b;
display: flex;
justify-content: space-between;
align-items: center;
padding: 18px 60px;
box-sizing: border-box;
position: fixed;
top: 0;
z-index: 1000;
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
}
.nav-left {
font-size: 22px;
font-weight: bold;
color: #1f3d2b;
cursor: pointer;
display: flex;
align-items: center;
gap: 10px;
}
.nav-right { display: flex; gap: 20px; align-items: center; font-size: 14px; }
.nav-pill {
background: #cfe7d7;
padding: 8px 16px;
border-radius: 20px;
font-weight: 600;
color: #2f8f57;
cursor: pointer;
transition: 0.3s;
}
.nav-pill:hover {
background: #bbf7d0;
transform: translateY(-2px);
}
.nav-btn {
background: #2f8f57;
color: white;
padding: 10px 20px;
border-radius: 25px;
font-weight: 600;
cursor: pointer;
transition: 0.3s;
border: none;
}
.nav-btn:hover {
background: #1f6b42;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(47,143,87,0.3);
}
/* CONTAINER */
.container {
width: 100%;
min-height: calc(100vh - 76px);
background: linear-gradient(135deg, #e3efe7 0%, #dbe8df 100%);
padding: 100px 60px 60px;
box-sizing: border-box;
}
.header {
text-align: center;
margin-bottom: 50px;
}
.section-tag {
color: #2f8f57;
font-weight: 700;
letter-spacing: 3px;
font-size: 13px;
margin-bottom: 15px;
}
h1 {
color: #1a2e23;
font-size: 42px;
margin-bottom: 15px;
font-weight: 700;
}
.subtitle {
color: #5a7b69;
font-size: 18px;
line-height: 1.6;
max-width: 700px;
margin: 0 auto;
}
/* QUIZ GRID */
.quiz-list {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 30px;
max-width: 1400px;
margin: 0 auto;
}
.quiz-card {
background: #fff;
padding: 35px;
border-radius: 20px;
cursor: pointer;
transition: 0.4s;
display: flex;
flex-direction: column;
justify-content: space-between;
min-height: 240px;
border: 1px solid #e8ede8;
position: relative;
overflow: hidden;
}
.quiz-card::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 4px;
height: 100%;
background: #2f8f57;
transform: scaleY(0);
transition: 0.3s;
}
.quiz-card:hover::before {
transform: scaleY(1);
}
.quiz-card:hover {
transform: translateY(-10px);
box-shadow: 0 15px 40px rgba(47,143,87,0.15);
border-color: #2f8f57;
}
.quiz-icon {
font-size: 48px;
margin-bottom: 20px;
}
.quiz-title {
font-size: 24px;
font-weight: 700;
color: #1a2e23;
margin-bottom: 12px;
}
.quiz-desc {
font-size: 15px;
color: #5a7b69;
line-height: 1.6;
margin-bottom: 20px;
flex-grow: 1;
}
.quiz-bottom {
margin-top: 20px;
display: flex;
justify-content: space-between;
align-items: center;
background: #f8faf9;
padding: 12px 16px;
border-radius: 12px;
font-weight: 600;
}
.quiz-mode {
color: #2f8f57;
font-size: 13px;
text-transform: uppercase;
letter-spacing: 1px;
}
.quiz-score {
background: #d7efe1;
color: #2f8f57;
padding: 6px 14px;
border-radius: 20px;
font-size: 14px;
}
.difficulty {
display: inline-block;
padding: 4px 10px;
border-radius: 12px;
font-size: 12px;
font-weight: 600;
margin-top: 10px;
}
.easy { background: #d7efe1; color: #2f8f57; }
.medium { background: #fef3c7; color: #d97706; }
.hard { background: #fee2e2; color: #dc2626; }
/* MODAL/MODE PAGE */
.modal {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.7);
z-index: 2000;
align-items: center;
justify-content: center;
backdrop-filter: blur(5px);
}
.modal.active {
display: flex;
}
.mode-container {
background: #fff;
max-width: 900px;
width: 90%;
margin: auto;
padding: 40px;
border-radius: 25px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
max-height: 90vh;
overflow-y: auto;
}
.mode-header {
text-align: center;
margin-bottom: 30px;
padding-bottom: 20px;
border-bottom: 2px solid #e8ede8;
}
.mode-title {
font-size: 32px;
color: #1a2e23;
margin-bottom: 10px;
}
.mode-desc {
color: #5a7b69;
font-size: 16px;
}
.question-box {
background: #f8faf9;
padding: 30px;
border-radius: 15px;
margin-top: 20px;
border: 2px solid #e8ede8;
}
.question-number {
color: #2f8f57;
font-weight: 600;
font-size: 14px;
margin-bottom: 15px;
}
.question-text {
font-size: 20px;
color: #1a2e23;
font-weight: 600;
margin-bottom: 25px;
line-height: 1.5;
}
.option-btn {
display: block;
width: 100%;
padding: 16px 20px;
margin-top: 12px;
border: 2px solid #e8ede8;
border-radius: 12px;
background: #fff;
color: #1a2e23;
cursor: pointer;
font-weight: 500;
transition: 0.3s;
text-align: left;
font-size: 16px;
}
.option-btn:hover {
background: #f0f9f4;
border-color: #2f8f57;
transform: translateX(5px);
}
.option-btn.correct {
background: #d7efe1;
border-color: #2f8f57;
color: #2f8f57;
}
.option-btn.wrong {
background: #fee2e2;
border-color: #dc2626;
color: #dc2626;
}
.btn {
margin-top: 25px;
padding: 14px 32px;
border: none;
border-radius: 30px;
background: #2f8f57;
color: white;
cursor: pointer;
font-weight: 600;
transition: 0.3s;
font-size: 16px;
}
.btn:hover {
background: #1f6b42;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(47,143,87,0.3);
}
.btn-secondary {
background: #6b7280;
}
.btn-secondary:hover {
background: #4b5563;
}
.timer {
font-size: 24px;
font-weight: 700;
color: #dc2626;
margin: 20px 0;
text-align: center;
padding: 15px;
background: #fee2e2;
border-radius: 12px;
}
.score-display {
text-align: center;
padding: 40px;
}
.final-score {
font-size: 64px;
font-weight: 700;
color: #2f8f57;
margin: 20px 0;
}
.score-message {
font-size: 24px;
color: #1a2e23;
margin-bottom: 30px;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
margin: 30px 0;
}
.stat-box {
background: #f8faf9;
padding: 20px;
border-radius: 12px;
text-align: center;
}
.stat-value {
font-size: 32px;
font-weight: 700;
color: #2f8f57;
}
.stat-label {
font-size: 14px;
color: #5a7b69;
margin-top: 5px;
}
.action-buttons {
display: flex;
gap: 15px;
justify-content: center;
margin-top: 30px;
}
/* RESPONSIVE */
@media(max-width: 768px) {
.navbar { padding: 15px 20px; }
.nav-left { font-size: 18px; }
.nav-right { gap: 10px; }
.nav-pill { padding: 6px 12px; font-size: 12px; }
.container { padding: 80px 20px 40px; }
h1 { font-size: 32px; }
.quiz-list { grid-template-columns: 1fr; }
.mode-container { padding: 25px; }
.stats-grid { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<!-- NAVBAR -->
<div class="navbar">
<div class="nav-left" onclick="window.location.href='index.html'">
🌱 GreenFarm
</div>
<div class="nav-right">
<div class="nav-btn" onclick="window.location.href='Dashboard.html'">👤 Dashboard</div>
<div class="nav-pill" onclick="window.location.href='leaderboard.html'">
🏆 Rank: <span id="navRank">#--</span>
</div>
<div class="nav-pill">
⭐ Score: <span id="navScore">0</span>
</div>
<div class="nav-pill">
🏅 Badges: <span id="navBadges">0</span>
</div>
</div>
</div>
<!-- MAIN CONTENT -->
<div class="container" id="selectionPage">
<div class="header">
<div class="section-tag">QUIZ HUB</div>
<h1>Master Sustainable Farming</h1>
<p class="subtitle">Choose your challenge and test your knowledge. Each quiz completed earns you points, badges, and improves your global ranking!</p>
</div>
<div class="quiz-list" id="quizList">
<!-- Quizzes will be loaded here -->
</div>
</div>
<!-- QUIZ MODAL -->
<div class="modal" id="quizModal">
<div class="mode-container">
<div class="mode-header">
<h2 class="mode-title" id="modeTitle"></h2>
<p class="mode-desc" id="modeDesc"></p>
</div>
<div id="timerBox" class="timer" style="display:none;"></div>
<div class="question-box" id="questionBox">
<div class="question-number" id="questionNumber"></div>
<h3 class="question-text" id="questionText"></h3>
<div id="optionsArea"></div>
</div>
<div class="action-buttons">
<button class="btn btn-secondary" onclick="closeQuiz()">Exit Quiz</button>
</div>
</div>
</div>
<script src="auth.js"></script>
<script src="quiz-data.js"></script>
<script>
// Protect this page - require authentication
requireAuth();
// Get current user and load their stats
const currentUser = getCurrentUser();
let userScore = currentUser?.stats?.totalScore || 0;
let userBadges = currentUser?.stats?.badges || 0;
let currentQuiz = null;
let currentQuestionIndex = 0;
let quizScore = 0;
let correctAnswers = 0;
let timerInterval = null;
// Initialize page
function initializePage() {
if (!currentUser) {
window.location.href = 'signin.html';
return;
}
updateNav();
loadQuizzes();
calculateUserRank();
}
// Update navbar with user stats
function updateNav() {
document.getElementById("navScore").innerText = userScore.toLocaleString();
document.getElementById("navBadges").innerText = userBadges;
}
// Calculate and display user's global rank
function calculateUserRank() {
const allUsers = getAllUsersForLeaderboard();
const userIndex = allUsers.findIndex(u => u.email === currentUser.email);
const rank = userIndex !== -1 ? userIndex + 1 : '--';
document.getElementById("navRank").innerText = rank !== '--' ? `#${rank}` : '#--';
// Update user's rank in their stats
if (rank !== '--') {
updateUserStats({ globalRank: rank });
}
}
// Load all available quizzes
function loadQuizzes() {
const quizList = document.getElementById('quizList');
quizList.innerHTML = '';
QUIZ_CATEGORIES.forEach(category => {
const card = document.createElement('div');
card.className = 'quiz-card';
card.onclick = () => startQuiz(category.id);
card.innerHTML = `
<div>
<div class="quiz-icon">${category.icon}</div>
<div class="quiz-title">${category.title}</div>
<div class="quiz-desc">${category.description}</div>
<span class="difficulty ${category.difficulty}">${category.difficulty.toUpperCase()}</span>
</div>
<div class="quiz-bottom">
<div class="quiz-mode">${category.mode}</div>
<div class="quiz-score">+${category.points} pts</div>
</div>
`;
quizList.appendChild(card);
});
}
// Start a quiz
function startQuiz(quizId) {
const category = QUIZ_CATEGORIES.find(c => c.id === quizId);
if (!category) return;
const quizData = QUIZ_DATA[quizId];
if (!quizData || quizData.length === 0) {
alert('This quiz is coming soon!');
return;
}
currentQuiz = {
id: quizId,
category: category,
questions: quizData,
pointsPerQuestion: Math.floor(category.points / quizData.length)
};
currentQuestionIndex = 0;
quizScore = 0;
correctAnswers = 0;
document.getElementById('modeTitle').textContent = category.title;
document.getElementById('modeDesc').textContent = category.description;
loadQuestion();
document.getElementById('quizModal').classList.add('active');
}
// Load current question
function loadQuestion() {
if (currentQuestionIndex >= currentQuiz.questions.length) {
showResults();
return;
}
const question = currentQuiz.questions[currentQuestionIndex];
document.getElementById('questionNumber').textContent =
`Question ${currentQuestionIndex + 1} of ${currentQuiz.questions.length}`;
document.getElementById('questionText').textContent = question.question;
const optionsArea = document.getElementById('optionsArea');
optionsArea.innerHTML = '';
question.options.forEach((option, index) => {
const btn = document.createElement('button');
btn.className = 'option-btn';
btn.textContent = option;
btn.onclick = () => selectAnswer(index);
optionsArea.appendChild(btn);
});
// Start timer if timed quiz
if (currentQuiz.category.timed) {
startTimer();
} else {
document.getElementById('timerBox').style.display = 'none';
}
}
// Handle answer selection
function selectAnswer(selectedIndex) {
if (timerInterval) {
clearInterval(timerInterval);
}
const question = currentQuiz.questions[currentQuestionIndex];
const buttons = document.querySelectorAll('.option-btn');
// Disable all buttons
buttons.forEach(btn => btn.style.pointerEvents = 'none');
// Mark correct and wrong answers
buttons[question.correctIndex].classList.add('correct');
if (selectedIndex !== question.correctIndex) {
buttons[selectedIndex].classList.add('wrong');
} else {
correctAnswers++;
quizScore += currentQuiz.pointsPerQuestion;
}
// Move to next question after delay
setTimeout(() => {
currentQuestionIndex++;
loadQuestion();
}, 1500);
}
// Start timer for timed quizzes
function startTimer() {
const timerBox = document.getElementById('timerBox');
timerBox.style.display = 'block';
let timeLeft = 15;
timerBox.textContent = `Time Left: ${timeLeft}s`;
timerInterval = setInterval(() => {
timeLeft--;
timerBox.textContent = `Time Left: ${timeLeft}s`;
if (timeLeft <= 0) {
clearInterval(timerInterval);
// Auto-select wrong answer
selectAnswer(-1);
}
}, 1000);
}
// Show quiz results
function showResults() {
const questionBox = document.getElementById('questionBox');
const totalQuestions = currentQuiz.questions.length;
const percentage = Math.round((correctAnswers / totalQuestions) * 100);
// Update user stats
userScore += quizScore;
const newQuizzesCompleted = (currentUser.stats.quizzesCompleted || 0) + 1;
const newAccuracy = Math.round(
((currentUser.stats.accuracyRate * currentUser.stats.quizzesCompleted) + percentage) / newQuizzesCompleted
);
updateUserStats({
totalScore: userScore,
quizzesCompleted: newQuizzesCompleted,
accuracyRate: newAccuracy
});
// Add activity
addActivity({
type: 'quiz_completed',
title: `Completed "${currentQuiz.category.title}" Quiz`,
description: `Scored ${percentage}% and earned ${quizScore} points`,
icon: '✅'
});
// Check for badges
checkAndAwardBadges(newQuizzesCompleted, percentage);
questionBox.innerHTML = `
<div class="score-display">
<div class="score-message">Quiz Completed!</div>
<div class="final-score">${percentage}%</div>
<div class="stats-grid">
<div class="stat-box">
<div class="stat-value">${correctAnswers}/${totalQuestions}</div>
<div class="stat-label">Correct Answers</div>
</div>
<div class="stat-box">
<div class="stat-value">+${quizScore}</div>
<div class="stat-label">Points Earned</div>
</div>
<div class="stat-box">
<div class="stat-value">${userScore.toLocaleString()}</div>
<div class="stat-label">Total Score</div>
</div>
</div>
<div class="action-buttons">
<button class="btn" onclick="retakeQuiz()">Retake Quiz</button>
<button class="btn btn-secondary" onclick="closeQuiz()">Back to Hub</button>
</div>
</div>
`;
updateNav();
calculateUserRank();
}
// Check and award badges
function checkAndAwardBadges(quizzesCompleted, percentage) {
// First quiz badge
if (quizzesCompleted === 1) {
awardBadge('First Steps', '🌟');
}
// Perfect score badge
if (percentage === 100) {
awardBadge('Perfect Score', '💯');
}
// Quiz milestones
if (quizzesCompleted === 5) {
awardBadge('Quiz Enthusiast', '📚');
}
if (quizzesCompleted === 10) {
awardBadge('Quiz Master', '🏆');
}
if (quizzesCompleted === 25) {
awardBadge('Quiz Legend', '👑');
}
}
// Award a badge
function awardBadge(name, icon) {
const existingBadge = currentUser.badges?.find(b => b.name === name);
if (!existingBadge) {
addBadge({ name, icon });
userBadges++;
// Show badge notification
alert(`🎉 New Badge Earned: ${icon} ${name}!`);
}
}
// Retake current quiz
function retakeQuiz() {
currentQuestionIndex = 0;
quizScore = 0;
correctAnswers = 0;
loadQuestion();
}
// Close quiz modal
function closeQuiz() {
if (timerInterval) {
clearInterval(timerInterval);
}
document.getElementById('quizModal').classList.remove('active');
currentQuiz = null;
}
// Initialize page on load
initializePage();
</script>
</body>
</html>