Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbserverprogrammering/submissions/projekt-quiz/admin/

admin_login.php
admin_manage_quiz.php
admin_manage_users.php
admin_panel.php
admin_statistics.php
make_admin.php

admin_statistics.php

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

// Kontrollera att admin är inloggad
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    
header("Location: admin_login.php");
    exit();
}

// Hämta alla quiz för dropdown
$stmt $pdo->query("SELECT id, title FROM quizzes ORDER BY title");
$quizzes $stmt->fetchAll(PDO::FETCH_ASSOC);

// Välj quiz för statistik
$selected_quiz = isset($_GET['quiz_id']) ? $_GET['quiz_id'] : null;

$stats = [];
$total_attempts 0;

if (
$selected_quiz) {
    
// Hämta poängfördelning
    
$stmt $pdo->prepare("
        SELECT score, total_questions, COUNT(*) as count
        FROM quiz_attempts
        WHERE quiz_id = :quiz_id
        GROUP BY score, total_questions
        ORDER BY score DESC
    "
);
    
$stmt->execute([':quiz_id' => $selected_quiz]);
    
$results $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    
// Beräkna statistik
    
if ($results) {
        
$total_questions $results[0]['total_questions'];
        
        
// Initiera stats array
        
for ($i 0$i <= $total_questions$i++) {
            
$stats[$i] = 0;
        }
        
        
// Fyll i stats
        
foreach ($results as $result) {
            
$stats[$result['score']] = $result['count'];
            
$total_attempts += $result['count'];
        }
    }
}

$page_title 'Statistik';
require_once 
'../includes/header.php';
?>
    
    <div class="container">
        <div class="admin-section">
            <h1>Statistik</h1>
            
            <a href="admin_panel.php" class="btn btn-secondary">Tillbaka till Admin Panel</a>
            
            <div class="quiz-selector">
                <form method="GET">
                    <label for="quiz_id">Välj quiz:</label>
                    <select name="quiz_id" id="quiz_id" onchange="this.form.submit()">
                        <option value="">-- Välj ett 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>
                </form>
            </div>
            
            <?php if ($selected_quiz && !empty($stats)): ?>
                <div class="statistics-section">
                    <h2>Poängfördelning</h2>
                    <p>Totalt genomförda: <?php echo $total_attempts?></p>
                    
                    <div class="bar-chart">
                        <?php 
                        $max_count 
max($stats);
                        foreach (
array_reverse($statstrue) as $score => $count): 
                            
$percentage $max_count ? ($count $max_count) * 100 0;
                            
$percent_of_total $total_attempts round(($count $total_attempts) * 1001) : 0;
                        
?>
                            <div class="bar-row">
                                <div class="bar-label"><?php echo $score?>p:</div>
                                <div class="bar-container">
                                    <div class="bar-fill" style="width: <?php echo $percentage?>%;"></div>
                                </div>
                                <div class="bar-value"><?php echo $count?> (<?php echo $percent_of_total?>%)</div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            <?php elseif ($selected_quiz): ?>
                <p>Inga försök har gjorts för detta quiz ännu.</p>
            <?php else: ?>
                <p>Välj ett quiz för att se statistik.</p>
            <?php endif; ?>
        </div>
    </div>
    
    <style>
        .bar-chart {
            margin-top: 20px;
        }
        .bar-row {
            display: flex;
            align-items: center;
            margin-bottom: 10px;
        }
        .bar-label {
            width: 50px;
            font-weight: bold;
            color: var(--text-primary);
        }
        .bar-container {
            flex: 1;
            background: var(--bg-input);
            height: 30px;
            border-radius: 8px;
            overflow: hidden;
            margin: 0 10px;
            border: 1px solid var(--border-color);
        }
        .bar-fill {
            background: linear-gradient(135deg, var(--purple-primary), var(--accent-pink));
            height: 100%;
            transition: width 0.3s;
        }
        .bar-value {
            width: 100px;
            text-align: right;
            color: var(--text-secondary);
        }
        .quiz-selector {
            margin: 25px 0;
            padding: 20px;
            background: var(--bg-card);
            border-radius: 12px;
            border: 1px solid var(--border-color);
        }
        .quiz-selector select {
            margin-left: 10px;
            padding: 10px 15px;
            background: var(--bg-input);
            color: var(--text-primary);
            border: 1px solid var(--border-color);
            border-radius: 8px;
            font-size: 1em;
        }
        .statistics-section {
            margin-top: 30px;
        }
    </style>

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