Webbserver - Love Blomberg

Show sourcecode

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

public_html/GYA2/

admin.php
admin_login.php
api/
config.php
css/
db_setup.php
index.php
js/
results.php
tests/

db_setup.php

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

// Connect without specifying a database first
$pdo = new PDO(
    
'mysql:host=' DB_HOST ';charset=utf8mb4',
    
DB_USER,
    
DB_PASS,
    [
        
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    ]
);

$statements = [
    
"CREATE DATABASE IF NOT EXISTS `" DB_NAME "`
        CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"
,

    
"USE `" DB_NAME "`",

    
"CREATE TABLE IF NOT EXISTS participants (
        id           INT AUTO_INCREMENT PRIMARY KEY,
        email        VARCHAR(255) NOT NULL,
        group_type   ENUM('placebo', 'control') NOT NULL,
        user_agent   TEXT,
        ip_address   VARCHAR(45),
        created_at   DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        completed_at DATETIME DEFAULT NULL
    ) ENGINE=InnoDB"
,

    
"CREATE TABLE IF NOT EXISTS reaction_times (
        id             INT AUTO_INCREMENT PRIMARY KEY,
        participant_id INT NOT NULL,
        round_number   TINYINT UNSIGNED NOT NULL,
        reaction_ms    INT UNSIGNED NOT NULL,
        was_premature  TINYINT(1) NOT NULL DEFAULT 0,
        delay_ms       INT UNSIGNED NOT NULL,
        recorded_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (participant_id) REFERENCES participants(id) ON DELETE CASCADE
    ) ENGINE=InnoDB"
,

    
"CREATE TABLE IF NOT EXISTS maze_results (
        id             INT AUTO_INCREMENT PRIMARY KEY,
        participant_id INT NOT NULL,
        total_time_ms  INT UNSIGNED NOT NULL,
        path_json      JSON NOT NULL,
        total_moves    INT UNSIGNED NOT NULL DEFAULT 0,
        completed      TINYINT(1) NOT NULL DEFAULT 1,
        recorded_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (participant_id) REFERENCES participants(id) ON DELETE CASCADE
    ) ENGINE=InnoDB"
,

    
"CREATE TABLE IF NOT EXISTS simon_results (
        id             INT AUTO_INCREMENT PRIMARY KEY,
        participant_id INT NOT NULL,
        max_level      INT UNSIGNED NOT NULL DEFAULT 0,
        total_time_ms  INT UNSIGNED NOT NULL,
        recorded_at    DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (participant_id) REFERENCES participants(id) ON DELETE CASCADE
    ) ENGINE=InnoDB"
,

    
"CREATE TABLE IF NOT EXISTS simon_rounds (
        id                  INT AUTO_INCREMENT PRIMARY KEY,
        participant_id      INT NOT NULL,
        level_number        INT UNSIGNED NOT NULL,
        success             TINYINT(1) NOT NULL,
        response_times_json JSON NOT NULL,
        recorded_at         DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (participant_id) REFERENCES participants(id) ON DELETE CASCADE
    ) ENGINE=InnoDB"
,
];

echo 
"<h1>Database Setup</h1><pre>";
foreach (
$statements as $sql) {
    try {
        
$pdo->exec($sql);
        
// Show a short description of what ran
        
$first_line strtok($sql"\n");
        echo 
"OK: " trim($first_line) . "\n";
    } catch (
PDOException $e) {
        echo 
"ERROR: " $e->getMessage() . "\n";
    }
}
echo 
"\nDone. Database '" DB_NAME "' is ready.</pre>";
echo 
'<p><a href="index.php">Go to experiment &rarr;</a></p>';