Webbserverprogrammering 1

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

my_results.php

78 lines UTF-8 Unix (LF)
<?php
session_start
();
require_once 
'includes/db_connect.php';
require_once 
'includes/session_config.php';

requireLogin();

// Determine user_id based on session type
if (isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true) {
    
$current_user_id $_SESSION['admin_id'];
} else {
    
$current_user_id $_SESSION['user_id'];
}

// Hämta alla användarens quiz-försök
$stmt $pdo->prepare("
    SELECT 
        qa.id,
        qa.score,
        qa.total_questions,
        qa.completed_at,
        q.title as quiz_title
    FROM quiz_attempts qa
    JOIN quizzes q ON qa.quiz_id = q.id
    WHERE qa.user_id = :user_id
    ORDER BY qa.completed_at DESC
"
);
$stmt->execute([':user_id' => $current_user_id]);
$attempts $stmt->fetchAll(PDO::FETCH_ASSOC);

$page_title 'Mina resultat';
require_once 
'includes/header.php';
?>

    <div class="container">
        <div class="results-container">
            <h1>Mina Quiz-resultat</h1>
            
            <?php if (empty($attempts)): ?>
                <p>Du har inte genomfört några quiz ännu.</p>
                <a href="dashboard.php" class="btn btn-primary">Gå till quiz</a>
            <?php else: ?>
                <table>
                    <thead>
                        <tr>
                            <th>Quiz</th>
                            <th>Datum</th>
                            <th>Poäng</th>
                            <th>Procent</th>
                            <th>Detaljer</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($attempts as $attempt): ?>
                            <?php
                            $percentage 
round(($attempt['score'] / $attempt['total_questions']) * 100);
                            
?>
                            <tr>
                                <td><?php echo htmlspecialchars($attempt['quiz_title']); ?></td>
                                <td><?php echo date('Y-m-d H:i'strtotime($attempt['completed_at'])); ?></td>
                                <td><?php echo $attempt['score']; ?>/<?php echo $attempt['total_questions']; ?></td>
                                <td><?php echo $percentage?>%</td>
                                <td>
                                    <a href="quiz_result.php?attempt_id=<?php echo $attempt['id']; ?>" class="btn btn-small btn-secondary">
                                        Visa detaljer
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
    </div>

<?php require_once 'includes/footer.php'?>
</html>