Webbserver - Love Blomberg

Show sourcecode

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

public_html/GYA2/api/

admin_data.php
complete_session.php
export_csv.php
save_maze.php
save_reaction.php
save_simon.php
start_session.php

save_simon.php

67 lines ASCII Unix (LF)
<?php
require __DIR__ '/../config.php';

header('Content-Type: application/json');

if (
$_SERVER['REQUEST_METHOD'] !== 'POST') {
    
http_response_code(405);
    echo 
json_encode(['error' => 'Method not allowed']);
    exit;
}

$data json_decode(file_get_contents('php://input'), true);

if (!
$data) {
    
http_response_code(400);
    echo 
json_encode(['error' => 'Invalid JSON']);
    exit;
}

$pid       = (int)($data['participant_id'] ?? 0);
$maxLevel  = (int)($data['max_level'] ?? 0);
$timeMs    = (int)($data['total_time_ms'] ?? 0);
$rounds    $data['rounds'] ?? [];

if (
$pid <= 0) {
    
http_response_code(400);
    echo 
json_encode(['error' => 'Invalid participant_id']);
    exit;
}

try {
    
$pdo getDB();
    
$pdo->beginTransaction();

    
// Insert summary row
    
$stmt $pdo->prepare(
        
'INSERT INTO simon_results (participant_id, max_level, total_time_ms)
         VALUES (:pid, :max_level, :time)'
    
);
    
$stmt->execute([
        
':pid'       => $pid,
        
':max_level' => $maxLevel,
        
':time'      => $timeMs,
    ]);

    
// Insert per-round detail
    
$stmt2 $pdo->prepare(
        
'INSERT INTO simon_rounds (participant_id, level_number, success, response_times_json)
         VALUES (:pid, :level, :success, :times)'
    
);
    foreach (
$rounds as $round) {
        
$stmt2->execute([
            
':pid'     => $pid,
            
':level'   => (int)($round['level_number'] ?? 0),
            
':success' => (int)($round['success'] ?? 0),
            
':times'   => json_encode($round['response_times'] ?? []),
        ]);
    }

    
$pdo->commit();
    echo 
json_encode(['success' => true]);
} catch (
PDOException $e) {
    
$pdo->rollBack();
    
http_response_code(500);
    echo 
json_encode(['error' => 'Database error']);
}