Show sourcecode
The following files exists in this folder. Click to view.
admin_data.php
complete_session.php
export_csv.php
save_maze.php
save_reaction.php
save_simon.php
start_session.php
export_csv.php
106 lines ASCII Unix (LF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
<?php
session_start();
if (empty($_SESSION['admin_logged_in'])) {
http_response_code(403);
echo 'Unauthorized';
exit;
}
require __DIR__ . '/../config.php';
$table = $_GET['table'] ?? 'all';
$pdo = getDB();
// Helper: output a result set as CSV
function outputCSV($filename, $rows) {
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
// BOM for Excel UTF-8 compatibility
echo "\xEF\xBB\xBF";
$out = fopen('php://output', 'w');
if (count($rows) > 0) {
fputcsv($out, array_keys($rows[0]));
foreach ($rows as $row) {
fputcsv($out, $row);
}
}
fclose($out);
}
switch ($table) {
case 'participants':
$rows = $pdo->query("
SELECT id, email, group_type, created_at, completed_at
FROM participants ORDER BY id
")->fetchAll();
outputCSV('participants.csv', $rows);
break;
case 'reaction_times':
$rows = $pdo->query("
SELECT r.participant_id, p.email, p.group_type,
r.round_number, r.reaction_ms, r.was_premature, r.delay_ms, r.recorded_at
FROM reaction_times r
JOIN participants p ON p.id = r.participant_id
ORDER BY r.participant_id, r.round_number
")->fetchAll();
outputCSV('reaction_times.csv', $rows);
break;
case 'maze_results':
$rows = $pdo->query("
SELECT m.participant_id, p.email, p.group_type,
m.total_time_ms, m.total_moves, m.recorded_at
FROM maze_results m
JOIN participants p ON p.id = m.participant_id
ORDER BY m.participant_id
")->fetchAll();
outputCSV('maze_results.csv', $rows);
break;
case 'simon_results':
$rows = $pdo->query("
SELECT s.participant_id, p.email, p.group_type,
s.max_level, s.total_time_ms, s.recorded_at
FROM simon_results s
JOIN participants p ON p.id = s.participant_id
ORDER BY s.participant_id
")->fetchAll();
outputCSV('simon_results.csv', $rows);
break;
case 'all':
// Combined summary: one row per participant with all test results
$rows = $pdo->query("
SELECT
p.id AS participant_id,
p.email,
p.group_type,
p.created_at,
p.completed_at,
ROUND(reaction_avg.avg_ms) AS reaction_avg_ms,
reaction_avg.best_ms AS reaction_best_ms,
m.total_time_ms AS maze_time_ms,
m.total_moves AS maze_moves,
s.max_level AS simon_max_level,
s.total_time_ms AS simon_time_ms
FROM participants p
LEFT JOIN (
SELECT participant_id,
AVG(CASE WHEN was_premature = 0 THEN reaction_ms END) AS avg_ms,
MIN(CASE WHEN was_premature = 0 THEN reaction_ms END) AS best_ms
FROM reaction_times
GROUP BY participant_id
) reaction_avg ON reaction_avg.participant_id = p.id
LEFT JOIN maze_results m ON m.participant_id = p.id
LEFT JOIN simon_results s ON s.participant_id = p.id
ORDER BY p.id
")->fetchAll();
outputCSV('experiment_all_data.csv', $rows);
break;
default:
http_response_code(400);
echo 'Invalid table name';
}