exec("CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
role ENUM('user', 'psychologist') NOT NULL DEFAULT 'user',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);");
// Create tests table
$pdo->exec("CREATE TABLE IF NOT EXISTS tests (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
is_public BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);");
// Add user_id column to tests table if it doesn't exist
$stmt = $pdo->query("SHOW COLUMNS FROM tests LIKE 'user_id'");
if ($stmt->rowCount() == 0) {
$pdo->exec("ALTER TABLE tests ADD COLUMN user_id INT, ADD FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL;");
}
// Create questions table
$pdo->exec("CREATE TABLE IF NOT EXISTS questions (
id INT AUTO_INCREMENT PRIMARY KEY,
test_id INT NOT NULL,
question_text TEXT NOT NULL,
FOREIGN KEY (test_id) REFERENCES tests(id) ON DELETE CASCADE
);");
// Create options table
$pdo->exec("CREATE TABLE IF NOT EXISTS options (
id INT AUTO_INCREMENT PRIMARY KEY,
question_id INT NOT NULL,
option_text VARCHAR(255) NOT NULL,
FOREIGN KEY (question_id) REFERENCES questions(id) ON DELETE CASCADE
);");
// Add score column to options table if it doesn't exist
$stmt = $pdo->query("SHOW COLUMNS FROM options LIKE 'score'");
if ($stmt->rowCount() == 0) {
$pdo->exec("ALTER TABLE options ADD COLUMN score INT NOT NULL DEFAULT 0;");
}
// Create user_tests table to track completed tests and scores
$pdo->exec("CREATE TABLE IF NOT EXISTS user_tests (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
test_id INT NOT NULL,
score INT NOT NULL,
completed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (test_id) REFERENCES tests(id) ON DELETE CASCADE,
UNIQUE(user_id, test_id)
);");
// Create test_results_interpretations table
$pdo->exec("CREATE TABLE IF NOT EXISTS test_results_interpretations (
id INT AUTO_INCREMENT PRIMARY KEY,
test_id INT NOT NULL,
min_score INT NOT NULL,
max_score INT NOT NULL,
interpretation TEXT NOT NULL,
FOREIGN KEY (test_id) REFERENCES tests(id) ON DELETE CASCADE
);");
// Check if there's any data
$stmt = $pdo->query('SELECT COUNT(*) FROM tests');
$count = $stmt->fetchColumn();
if ($count == 0) {
// Insert dummy data
$tests = [
['title' => 'Big Five Personality Test', 'description' => 'A comprehensive test to understand your personality based on five major traits.'],
['title' => 'IQ Test - Logic & Reasoning', 'description' => 'Assess your logical reasoning and problem-solving abilities.'],
['title' => 'Myers-Briggs Type Indicator (MBTI)', 'description' => 'Discover your personality type and how you perceive the world.'],
['title' => 'Emotional Intelligence (EQ) Test', 'description' => 'Evaluate your ability to perceive, control, and evaluate emotions.'],
];
$insertStmt = $pdo->prepare('INSERT INTO tests (title, description) VALUES (?, ?)');
foreach ($tests as $test) {
$insertStmt->execute([$test['title'], $test['description']]);
}
}
} catch (PDOException $e) {
// Don't expose error details to the user
return false;
}
return true;
}
$pdo = db();
if ($pdo) {
setupDatabase($pdo);
try {
$stmt = $pdo->query('SELECT id, title, description FROM tests WHERE is_public = TRUE ORDER BY created_at DESC');
$tests = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
$tests = [];
}
} else {
$tests = [];
}
?>
Available Tests
Choose a test to start your self-discovery journey.
Could not connect to the database or no tests are available at the moment. Please try again later.