Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/projects/anton-quiz/admin/
create_answer.php
create_question.php
create_quiz.php
dashboard.php
edit_quiz.php
show_results.php
view_details.php
show_results.php
123 lines UTF-8 Windows (CRLF)
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
<?php
session_start();
// Kontroll: endast admin
if (!isset($_SESSION["is_admin"]) || $_SESSION["is_admin"] != 1) {
header('Location: ../index.php');
exit;
}
require_once __DIR__ . '/../dbconnect.php';
// 1) Hämta test_id från GET (via en länk t.ex. admin_test_results.php?test_id=2)
$test_id = $_GET["test_id"] ?? 0;
$test_id = (int)$test_id;
// 2) Hämta info om det specifika testet
$stmtTest = $conn->prepare("
SELECT id, test_name, is_enabled
FROM quizdb_tests
WHERE id = ?
");
$stmtTest->bind_param("i", $test_id);
$stmtTest->execute();
$testResult = $stmtTest->get_result();
$test = $testResult->fetch_assoc();
// Om inget test hittades, avbryt
if (!$test) {
echo "<p>Test med id=$test_id hittades ej.</p>";
exit;
}
// 3) Hämta sammanställning för detta test: antal försök, medelpoäng, min och max
$stmtSummary = $conn->prepare("
SELECT
COUNT(*) AS total_attempts,
IFNULL(AVG(score), 0) AS average_score,
IFNULL(MIN(score), 0) AS min_score,
IFNULL(MAX(score), 0) AS max_score
FROM quizdb_results
WHERE test_id = ?
");
$stmtSummary->bind_param("i", $test_id);
$stmtSummary->execute();
$summaryResult = $stmtSummary->get_result();
$summary = $summaryResult->fetch_assoc();
// 4) Hämta alla individuella resultat för detta test
$stmtResults = $conn->prepare("
SELECT r.id AS result_id,
r.score,
r.taken_at,
u.username
FROM quizdb_results r
JOIN quizdb_users u ON r.user_id = u.id
WHERE r.test_id = ?
ORDER BY r.taken_at DESC
");
$stmtResults->bind_param("i", $test_id);
$stmtResults->execute();
$allResults = $stmtResults->get_result();
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<title>Admin - Resultat för <?php echo htmlspecialchars($test["test_name"]); ?></title>
</head>
<body>
<h1>Resultat för test: <?php echo htmlspecialchars($test["test_name"]); ?></h1>
<p>
<strong>Testets status:</strong>
<?php echo ($test["is_enabled"] == 1) ? "Aktivt" : "Inaktivt"; ?>
</p>
<hr>
<h2>Sammanställning</h2>
<p>
<strong>Antal gjorda försök:</strong> <?php echo $summary["total_attempts"]; ?><br>
<strong>Medelpoäng:</strong> <?php echo round($summary["average_score"], 2); ?><br>
<strong>Lägsta poäng:</strong> <?php echo $summary["min_score"]; ?><br>
<strong>Högsta poäng:</strong> <?php echo $summary["max_score"]; ?>
</p>
<hr>
<h2>Individuella resultat</h2>
<?php if ($allResults->num_rows === 0) : ?>
<p>Inga resultat registrerade ännu för detta test.</p>
<?php else : ?>
<table border="1" cellpadding="5" cellspacing="0">
<thead>
<tr>
<th>Användare</th>
<th>Poäng</th>
<th>Genomförd</th>
<th>Mer info</th> <!-- Om du vill länka till en detaljsida -->
</tr>
</thead>
<tbody>
<?php while ($row = $allResults->fetch_assoc()) : ?>
<tr>
<td><?php echo htmlspecialchars($row["username"]); ?></td>
<td><?php echo (int)$row["score"]; ?></td>
<td><?php echo $row["taken_at"]; ?></td>
<td>
<!-- Exempel-länk, om du t.ex. vill visa detaljerade svar: -->
<a href="view_details.php?result_id=<?php echo $row["result_id"]; ?>">
Visa detaljer
</a>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<?php endif; ?>
</body>
</html>