Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/submissions/projekt-quiz/
README.md
admin/
create_admin.php
create_quiz.php
css/
dashboard.php
debug_questions.php
includes/
index.php
leaderboard.php
login.php
logout.php
my_results.php
quiz.php
quiz_result.php
register.php
sqlcredentials
leaderboard.php
200 lines UTF-8 Unix (LF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
<?php
session_start();
require_once 'includes/db_connect.php';
require_once 'includes/session_config.php';
requireLogin();
// Hämta alla quiz för filter
$stmt = $pdo->query("SELECT id, title FROM quizzes ORDER BY title");
$quizzes = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Välj quiz och sortering
$selected_quiz = isset($_GET['quiz_id']) ? intval($_GET['quiz_id']) : null;
$sort_by = isset($_GET['sort']) ? $_GET['sort'] : 'score';
// Bygg query
$where = $selected_quiz ? "WHERE qa.quiz_id = :quiz_id" : "";
$order = "ORDER BY ";
if ($sort_by === 'name') {
$order .= "u.name ASC, qa.score DESC";
} else {
$order .= "qa.score DESC, u.name ASC";
}
$sql = "
SELECT
u.name,
q.title as quiz_title,
qa.score,
qa.total_questions,
qa.completed_at
FROM quiz_attempts qa
JOIN users u ON qa.user_id = u.id
JOIN quizzes q ON qa.quiz_id = q.id
$where
$order
";
$stmt = $pdo->prepare($sql);
if ($selected_quiz) {
$stmt->execute([':quiz_id' => $selected_quiz]);
} else {
$stmt->execute();
}
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Get current user name for highlighting
if (isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true) {
$current_name = $_SESSION['admin_name'];
} else {
$current_name = $_SESSION['name'];
}
$page_title = 'Resultattavla';
require_once 'includes/header.php';
?>
<style>
.current-user {
background-color: rgba(155, 89, 182, 0.2) !important;
}
.current-user td {
background-color: transparent !important;
}
.badge {
background: linear-gradient(135deg, var(--purple-primary), var(--accent-pink));
color: white;
padding: 3px 10px;
border-radius: 20px;
font-size: 0.75em;
margin-left: 8px;
}
.filter-section {
margin: 25px 0;
padding: 20px;
background: var(--bg-card);
border-radius: 12px;
border: 1px solid var(--border-color);
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 20px;
}
.filter-section label {
color: var(--text-secondary);
margin-right: 10px;
}
.filter-section select {
padding: 10px 15px;
background: var(--bg-input);
border: 1px solid var(--border-color);
border-radius: 8px;
color: var(--text-primary);
font-size: 15px;
min-width: 200px;
}
.filter-section select:focus {
outline: none;
border-color: var(--purple-primary);
}
.sort-options {
display: flex;
align-items: center;
gap: 10px;
}
.sort-options span {
color: var(--text-secondary);
}
.sort-options .btn.active {
background: linear-gradient(135deg, var(--purple-primary), var(--purple-dark));
color: white;
}
</style>
<div class="container">
<div class="leaderboard-container">
<h1>Resultattavla</h1>
<a href="dashboard.php" class="btn btn-secondary">Tillbaka till Dashboard</a>
<div class="filter-section">
<form method="GET">
<label for="quiz_id">Filtrera per quiz:</label>
<select name="quiz_id" id="quiz_id" onchange="this.form.submit()">
<option value="">Alla quiz</option>
<?php foreach ($quizzes as $quiz): ?>
<option value="<?php echo $quiz['id']; ?>"
<?php echo ($selected_quiz == $quiz['id']) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($quiz['title']); ?>
</option>
<?php endforeach; ?>
</select>
<input type="hidden" name="sort" value="<?php echo htmlspecialchars($sort_by); ?>">
</form>
<div class="sort-options">
<span>Sortera:</span>
<a href="?quiz_id=<?php echo $selected_quiz; ?>&sort=score"
class="btn btn-small <?php echo $sort_by === 'score' ? 'active' : ''; ?>">
Poäng
</a>
<a href="?quiz_id=<?php echo $selected_quiz; ?>&sort=name"
class="btn btn-small <?php echo $sort_by === 'name' ? 'active' : ''; ?>">
Namn
</a>
</div>
</div>
<?php if (empty($results)): ?>
<p>Inga resultat att visa.</p>
<?php else: ?>
<table>
<thead>
<tr>
<th>Plats</th>
<th>Namn</th>
<?php if (!$selected_quiz): ?>
<th>Quiz</th>
<?php endif; ?>
<th>Poäng</th>
<th>Procent</th>
<th>Datum</th>
</tr>
</thead>
<tbody>
<?php
$position = 1;
foreach ($results as $result):
$percentage = round(($result['score'] / $result['total_questions']) * 100);
$is_current_user = ($result['name'] === $current_name);
?>
<tr <?php echo $is_current_user ? 'class="current-user"' : ''; ?>>
<td><?php echo $position; ?></td>
<td>
<?php echo htmlspecialchars($result['name']); ?>
<?php if ($is_current_user): ?>
<span class="badge">(Du)</span>
<?php endif; ?>
</td>
<?php if (!$selected_quiz): ?>
<td><?php echo htmlspecialchars($result['quiz_title']); ?></td>
<?php endif; ?>
<td><?php echo $result['score']; ?>/<?php echo $result['total_questions']; ?></td>
<td><?php echo $percentage; ?>%</td>
<td><?php echo date('Y-m-d', strtotime($result['completed_at'])); ?></td>
</tr>
<?php
$position++;
endforeach;
?>
</tbody>
</table>
<?php endif; ?>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>