Show sourcecode
The following files exists in this folder. Click to view.
public_html/exercises/quizproject/quiz/
createquiz.php
submission.php
submit_quiz.php
takequiz.php
viewquizzes.php
viewsubmissions.php
viewsubmissions.php
182 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
<?php
session_start();
// Auth så att användare är inloggad och/eller admin
$loggedIn = isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == 1;
$isAdmin = isset($_SESSION['is_admin']) && $_SESSION['is_admin'] == 1;
// Hämta namn från session, annars Guest
$name = $loggedIn && isset($_SESSION['name']) ? htmlspecialchars($_SESSION['name']) : 'Guest';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quizzer | Submissions</title>
<link rel="stylesheet" href="../quiz_styles.css">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<div class="page page-narrow">
<?php if ($loggedIn): ?>
<div class="page-header">
<div>
<h1 class="page-title">Quiz Submissions</h1>
<p class="page-subtitle">View submissions of your quiz.</p>
</div>
<div class="actions">
<a class="btn btn-ghost" href="./viewquizzes.php">← Back to quizzes</a>
</div>
</div>
<?php endif; ?>
<div class="quiz-container">
<?php
include('../dbconnection.php');
if (!$dbconn) {
die("Connection failed: Can't connect to database.");
}
// Check auth
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] != 1) {
echo "<div class='alert alert-error'>Access denied. Please log in.</div>";
echo '<div class="actions"><a class="btn" href="../login.php">Log in -></a></div>';
exit;
}
// Ta quiz id från url via GET
if (!isset($_GET['quiz_id']) || empty($_GET['quiz_id'])) {
echo "<div class='alert alert-warning'>No quiz selected.</div>";
echo '<div class="actions"><a class="btn btn-secondary" href="../index.php">Back to dashboard -></a></div>';
exit;
}
// Sätt quiz id till en variabel
$quiz_id = intval($_GET['quiz_id']);
$user_id = $_SESSION['user_id'] ?? null;
try {
// Quiz info
$stmt = $dbconn->prepare("SELECT quiz_name, owner_id FROM quizzes WHERE quiz_id = ?");
$stmt->execute([$quiz_id]);
$quiz = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$quiz) {
echo "<div class='alert alert-error'>Quiz not found.</div>";
exit;
}
// Authorization: endast ägare och admin
if (($quiz['owner_id'] != $user_id) && ($_SESSION['is_admin'] != 1)) {
echo "<div class='alert alert-error'>Access denied. You do not own this quiz.</div>";
echo '<div class="actions"><a class="btn btn-secondary" href="../index.php">Back to dashboard -></a></div>';
exit;
}
echo "<h1 class='page-title'>" . htmlspecialchars($quiz['quiz_name']) . "</h1>";
// Hämta alla submissions för quizzen
$stmt = $dbconn->prepare("SELECT s.submission_id, s.user_id, s.submitted_time, s.score, u.username FROM submissions s LEFT JOIN users u ON s.user_id = u.user_id WHERE s.quiz_id = ? ORDER BY s.submitted_time DESC");
$stmt->execute([$quiz_id]);
$submissions = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($submissions)) {
echo "<div class='alert alert-warning'>No submissions found for this quiz.</div>";
exit;
}
echo "<table class='table'>";
echo "<thead><tr><th>ID</th><th>User</th><th>Submitted</th><th>Score</th></tr></thead><tbody>";
foreach ($submissions as $sub) {
$userLabel = $sub['username'] ? htmlspecialchars($sub['username']) : 'Unknown';
$scoreLabel = ($sub['score'] !== null) ? (int)$sub['score'] : 'N/A';
echo "<tr>";
echo "<td>" . (int)$sub['submission_id'] . "</td>";
echo "<td>" . $userLabel . "</td>";
echo "<td>" . htmlspecialchars($sub['submitted_time']) . "</td>";
echo "<td>" . $scoreLabel . "</td>";
echo "<td>" . '<a class="btn btn-secondary" href="submission.php?submission_id=' . (int)$sub['submission_id'] . '">View -></a></td>';
echo "</tr>";
}
echo "</tbody></table>";
} catch (PDOException $e) {
echo "<div class='alert alert-error'>Error: " . htmlspecialchars($e->getMessage()) . "</div>";
echo '<div class="actions"><a class="btn btn-secondary" href="../index.php">Back to dashboard -></a></div>';
}
try {
// Quiz info
$stmt = $dbconn->prepare("SELECT score, COUNT(*) as submission_count FROM submissions WHERE quiz_id = ? GROUP BY score ORDER BY score DESC");
$stmt->execute([$quiz_id]);
$statistics = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($statistics)) {
echo "<div class='alert alert-warning'>No submissions found for this quiz.</div>";
} else {
$labels = [];
$data = [];
foreach ($statistics as $stat) {
$labels[] = ($stat['score'] !== null) ? "Score: " . (int)$stat['score'] : 'N/A';
$data[] = (int)$stat['submission_count'];
}
echo "<h3>Submission Statistics</h3>";
echo "<div style='max-width: 500px; margin: 20px auto;'>";
echo "<canvas id='scoreChart'></canvas>";
echo "</div>";
echo "<table class='table'>";
echo "<thead><tr><th>Score</th><th>Number of Submissions</th></tr></thead><tbody>";
foreach ($statistics as $stat) {
$scoreLabel = ($stat['score'] !== null) ? (int)$stat['score'] : 'N/A';
echo "<tr>";
echo "<td>" . $scoreLabel . "</td>";
echo "<td>" . (int)$stat['submission_count'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
echo "<script>
const ctx = document.getElementById('scoreChart');
new Chart(ctx, {
type: 'bar',
data: {
labels: " . json_encode($labels) . ",
datasets: [{
label: 'Number of Submissions',
data: " . json_encode($data) . ",
borderWidth: 0
}]
},
options: {
responsive: true,
scales: {
y: {
beginAtZero: true,
ticks: {
stepSize: 1
}
}
},
plugins: {
legend: {
display: false
}
}
}
});
</script>";
}
} catch (PDOException $e) {
echo "<div class='alert alert-error'>Error: " . htmlspecialchars($e->getMessage()) . "</div>";
echo '<div class="actions"><a class="btn btn-secondary" href="../index.php">Back to dashboard -></a></div>';
}
$dbconn = null;
?>
</div>
</div>
</body>
</html>