<?php
session_start();

// Check if admin is logged in
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: admin.php');
    exit;
}

require_once 'db/config.php';

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=quiz_attempts.csv');

$output = fopen('php://output', 'w');

fputcsv($output, array('ID', 'Visual Score', 'Auditory Score', 'Read/Write Score', 'Kinesthetic Score', 'Primary Style', 'Date'));

$pdo = db();
$stmt = $pdo->query("SELECT * FROM quiz_attempts ORDER BY created_at DESC");

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    fputcsv($output, $row);
}

fclose($output);
exit;
?>