156 lines
6.1 KiB
PHP
156 lines
6.1 KiB
PHP
<?php
|
|
ini_set('display_errors', 0);
|
|
error_reporting(0);
|
|
|
|
require_once 'db/config.php';
|
|
|
|
function setupDatabase($pdo) {
|
|
try {
|
|
// Create users table
|
|
$pdo->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 = [];
|
|
}
|
|
|
|
?>
|
|
<?php require_once 'includes/header.php'; ?>
|
|
|
|
<main class="container">
|
|
<div class="text-center mb-5">
|
|
<h1 class="fw-bold">Available Tests</h1>
|
|
<p class="lead text-secondary">Choose a test to start your self-discovery journey.</p>
|
|
</div>
|
|
|
|
<?php if (empty($tests)): ?>
|
|
<div class="alert alert-warning text-center" role="alert">
|
|
Could not connect to the database or no tests are available at the moment. Please try again later.
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="row g-4">
|
|
<?php foreach ($tests as $test): ?>
|
|
<div class="col-md-6 col-lg-4">
|
|
<div class="card h-100 test-card p-3">
|
|
<div class="card-body d-flex flex-column">
|
|
<h5 class="card-title fw-bold"><?php echo htmlspecialchars($test['title']); ?></h5>
|
|
<p class="card-text text-secondary flex-grow-1"><?php echo htmlspecialchars($test['description']); ?></p>
|
|
<a href="take_test.php?test_id=<?php echo $test['id']; ?>" class="btn btn-primary mt-3">Start Test</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</main>
|
|
|
|
<footer class="py-5 mt-5 text-center text-secondary">
|
|
<p>© <?php echo date('Y'); ?> Psychological Testing System. All rights reserved.</p>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|