36915-vm/leaderboard.php
Flatlogic Bot 1b18b92970 0.1
2025-12-13 17:39:27 +00:00

156 lines
4.9 KiB
PHP

<?php
require_once 'db/config.php';
// We need a setting to check if the leaderboard is published.
// Let's assume a simple file-based flag for now, for speed.
// In a more robust app, this would be a database setting.
$is_published = file_exists('leaderboard_published.flag');
$pdo = db();
$leaderboard_data = [];
if ($is_published) {
// This is the same scoring logic from admin/results.php
// It would be better to refactor this into a shared function.
function calculate_score($participant_id, $pdo) {
$score_stmt = $pdo->prepare(
'SELECT q.score, a.is_correct
FROM submissions s
JOIN answers a ON s.answer_id = a.id
JOIN questions q ON s.question_id = q.id
WHERE s.participant_id = ?'
);
$score_stmt->execute([$participant_id]);
$results = $score_stmt->fetchAll(PDO::FETCH_ASSOC);
$total_score = 0;
foreach ($results as $result) {
if ($result['is_correct']) {
$total_score += (int)$result['score'];
} else {
$total_score -= (int)$result['score'];
}
}
return $total_score;
}
$participants_stmt = $pdo->query("SELECT id, nickname FROM participants");
$participants = $participants_stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($participants as $participant) {
$leaderboard_data[] = [
'nickname' => $participant['nickname'],
'score' => calculate_score($participant['id'], $pdo)
];
}
// Sort by score descending
usort($leaderboard_data, function($a, $b) {
return $b['score'] <=> $a['score'];
});
}
// Using the same frontend style as index.php
// I will read the content of index.php and extract the header and footer
// For now I will just copy the style part
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Classifica Quiz</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Orbitron:wght@400;700&family=Roboto:wght@400;700&display=swap" rel="stylesheet">
<style>
body {
background-color: #1a1a2e;
color: #ffffff;
font-family: 'Roboto', sans-serif;
margin: 0;
padding: 40px 20px;
display: flex;
justify-content: center;
align-items: flex-start;
min-height: 100vh;
}
.container {
width: 100%;
max-width: 800px;
background-color: #16213e;
border-radius: 8px;
padding: 40px;
border: 1px solid #0f3460;
}
h1, h2 {
font-family: 'Orbitron', sans-serif;
color: #e94560;
text-align: center;
margin-bottom: 30px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
padding: 15px;
text-align: left;
border-bottom: 1px solid #0f3460;
}
th {
background-color: #1a1a2e;
font-weight: bold;
font-family: 'Orbitron', sans-serif;
}
tr:nth-child(even) { background-color: #1f2a4a; }
.rank {
font-weight: bold;
font-size: 1.2em;
color: #e94560;
}
.not-published {
text-align: center;
font-size: 1.2em;
}
.home-link {
display: block;
text-align: center;
margin-top: 30px;
color: #e94560;
text-decoration: none;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h1>Classifica Finale</h1>
<?php if ($is_published): ?>
<table>
<thead>
<tr>
<th>Rank</th>
<th>Nickname</th>
<th>Punteggio</th>
</tr>
</thead>
<tbody>
<?php foreach ($leaderboard_data as $index => $row): ?>
<tr>
<td class="rank"><?php echo $index + 1; ?></td>
<td><?php echo htmlspecialchars($row['nickname']); ?></td>
<td><?php echo $row['score']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p class="not-published">La classifica non è stata ancora pubblicata. Torna più tardi!</p>
<?php endif; ?>
<a href="index.php" class="home-link">Torna al Quiz</a>
</div>
</body>
</html>