forked from Shalini-Majumdar/Quizzilla
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult.php
More file actions
78 lines (67 loc) · 2.62 KB
/
result.php
File metadata and controls
78 lines (67 loc) · 2.62 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
<?php
session_start();
include 'db_connect.php';
// Validate attempt_id
if (!isset($_GET['attempt_id']) || !is_numeric($_GET['attempt_id'])) {
die("Invalid attempt ID.");
}
$attempt_id = intval($_GET['attempt_id']);
// Ensure the attempt belongs to the logged-in user
$stmt = $conn->prepare("
SELECT a.quiz_id, a.score, q.name
FROM attempts a
JOIN quizzes q ON a.quiz_id = q.id
WHERE a.id = ? AND a.user_id = ?
");
$stmt->bind_param("ii", $attempt_id, $_SESSION['user_id']);
$stmt->execute();
$meta = $stmt->get_result()->fetch_assoc();
$stmt->close();
if (!$meta) {
die("Attempt not found or unauthorized.");
}
$quiz_name = htmlspecialchars($meta['name']);
$score = $meta['score'];
// Get detailed responses
$stmt = $conn->prepare("
SELECT q.question, q.correct_option, q.option_a, q.option_b, q.option_c, q.option_d, r.selected_option
FROM responses r
JOIN questions q ON r.question_id = q.id
WHERE r.attempt_id = ?
");
$stmt->bind_param("i", $attempt_id);
$stmt->execute();
$responses = $stmt->get_result();
?>
<?php include 'header.php'; ?>
<div class="card p-4 shadow-sm" style="max-width: 900px;">
<h4 class="mb-3">Result for Quiz: <?= $quiz_name ?></h4>
<p class="mb-4"><strong>Your Score:</strong> <?= $score ?></p>
<?php while ($row = $responses->fetch_assoc()): ?>
<div class="mb-4 p-3 border rounded bg-white">
<p class="fw-bold"><?= htmlspecialchars($row['question']) ?></p>
<?php foreach (['A', 'B', 'C', 'D'] as $opt): ?>
<?php
$option_text = htmlspecialchars($row['option_' . strtolower($opt)]);
$is_correct = $row['correct_option'] === $opt;
$is_selected = $row['selected_option'] === $opt;
$classes = "p-2 rounded";
if ($is_correct && $is_selected) {
$classes .= " bg-success text-white";
} elseif ($is_correct) {
$classes .= " bg-success-subtle text-success";
} elseif ($is_selected) {
$classes .= " bg-danger-subtle text-danger";
} else {
$classes .= " bg-light";
}
?>
<div class="<?= $classes ?>">
<?= $opt ?>. <?= $option_text ?>
</div>
<?php endforeach; ?>
</div>
<?php endwhile; ?>
<a href="dashboard.php" class="btn btn-outline-primary">← Return to Dashboard</a>
</div>
<?php include 'footer.php'; ?>