From ea73dc74832af427eebf53941dc253699f6af845 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Thu, 4 Dec 2025 08:49:26 +0000 Subject: [PATCH] v1 --- assets/css/custom.css | 43 ++++++ create_test.php | 163 +++++++++++++++++++++++ includes/footer.php | 1 + includes/header.php | 50 +++++++ index.php | 298 +++++++++++++++++++++--------------------- login.php | 97 ++++++++++++++ logout.php | 20 +++ register.php | 109 +++++++++++++++ submit_test.php | 72 ++++++++++ take_test.php | 97 ++++++++++++++ test_results.php | 66 ++++++++++ 11 files changed, 870 insertions(+), 146 deletions(-) create mode 100644 assets/css/custom.css create mode 100644 create_test.php create mode 100644 includes/footer.php create mode 100644 includes/header.php create mode 100644 login.php create mode 100644 logout.php create mode 100644 register.php create mode 100644 submit_test.php create mode 100644 take_test.php create mode 100644 test_results.php diff --git a/assets/css/custom.css b/assets/css/custom.css new file mode 100644 index 0000000..499b1df --- /dev/null +++ b/assets/css/custom.css @@ -0,0 +1,43 @@ +/* Psychological Testing System Custom Styles */ +:root { + --primary: #3B82F6; + --secondary: #10B981; + --background: #F9FAFB; + --surface: #FFFFFF; + --text-primary: #1F2937; + --text-secondary: #4B5563; + --border: #E5E7EB; +} + +body { + font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; + background-color: var(--background); + color: var(--text-primary); +} + +.navbar { + background-color: var(--surface); + border-bottom: 1px solid var(--border); +} + +.test-card { + background-color: var(--surface); + border: 1px solid var(--border); + border-radius: 0.5rem; + transition: all 0.2s ease-in-out; +} + +.test-card:hover { + transform: translateY(-5px); + box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08); +} + +.btn-primary { + background-color: var(--primary); + border-color: var(--primary); +} +.btn-primary:hover { + opacity: 0.9; + background-color: var(--primary); + border-color: var(--primary); +} diff --git a/create_test.php b/create_test.php new file mode 100644 index 0000000..6d73995 --- /dev/null +++ b/create_test.php @@ -0,0 +1,163 @@ +beginTransaction(); + + // Insert test + $stmt = $pdo->prepare("INSERT INTO tests (user_id, title, description, is_public) VALUES (?, ?, ?, ?)"); + $stmt->execute([$user_id, $title, $description, $is_public]); + $test_id = $pdo->lastInsertId(); + + // Insert questions and options + foreach ($questions as $q_data) { + $question_text = trim($q_data['text'] ?? ''); + if (empty($question_text)) continue; + + $stmt = $pdo->prepare("INSERT INTO questions (test_id, question_text) VALUES (?, ?)"); + $stmt->execute([$test_id, $question_text]); + $question_id = $pdo->lastInsertId(); + + $options = $q_data['options'] ?? []; + $correct_option_index = isset($q_data['correct']) ? intval($q_data['correct']) : -1; + + foreach ($options as $index => $option_text) { + $option_text = trim($option_text); + if(empty($option_text)) continue; + + $is_correct = ($index === $correct_option_index); + $stmt = $pdo->prepare("INSERT INTO options (question_id, option_text, is_correct) VALUES (?, ?, ?)"); + $stmt->execute([$question_id, $option_text, $is_correct]); + } + } + + $pdo->commit(); + $message = "Test created successfully!"; + } catch (PDOException $e) { + $pdo->rollBack(); + $error = "Database error: " . $e->getMessage(); + } + } else { + $error = "Could not connect to the database."; + } + } +} +?> + +
+
+
+

Create a New Test

+ + +
+ + +
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ +

Questions

+
+ +
+ + +
+ + +
+
+
+
+ + + + + + diff --git a/includes/footer.php b/includes/footer.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/includes/footer.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/includes/header.php b/includes/header.php new file mode 100644 index 0000000..f4695da --- /dev/null +++ b/includes/header.php @@ -0,0 +1,50 @@ + + + + + + + Psychological Testing System + + + + + + + + diff --git a/index.php b/index.php index 7205f3d..e01b8c8 100644 --- a/index.php +++ b/index.php @@ -1,150 +1,156 @@ 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 = []; +} -$phpVersion = PHP_VERSION; -$now = date('Y-m-d H:i:s'); ?> - - - - - - New Style - - - - - - - - - - - - - - - - - - - - - -
-
-

Analyzing your requirements and generating your website…

-
- Loading… -
-

AI is collecting your requirements and applying the first changes.

-

This page will update automatically as the plan is implemented.

-

Runtime: PHP — UTC

-
-
- + + +
+
+

Available Tests

+

Choose a test to start your self-discovery journey.

+
+ + + + +
+ +
+
+
+
+

+ Start Test +
+
+
+ +
+ +
+ + + + - + \ No newline at end of file diff --git a/login.php b/login.php new file mode 100644 index 0000000..44a2d9c --- /dev/null +++ b/login.php @@ -0,0 +1,97 @@ +prepare("SELECT id, username, password, role FROM users WHERE username = ?"); + $stmt->execute([$username]); + $user = $stmt->fetch(); + + if ($user && password_verify($password, $user['password'])) { + // Password is correct, start a new session + $_SESSION['user_id'] = $user['id']; + $_SESSION['username'] = $user['username']; + $_SESSION['role'] = $user['role']; + + // Redirect to the main page + header("Location: index.php"); + exit; + } else { + $error = 'Invalid username or password.'; + } + } catch (PDOException $e) { + $error = 'Database error. Please try again later.'; + } + } +} +?> + + + + + + Login - Psychological Testing System + + + + + + +
+
+
+
+
+

Login

+ +
+ +
+
+ + +
+
+ + +
+
+ +
+
+
+

Don't have an account? Register here

+
+
+
+
+
+
+ + + + + + diff --git a/logout.php b/logout.php new file mode 100644 index 0000000..536ec80 --- /dev/null +++ b/logout.php @@ -0,0 +1,20 @@ +prepare("SELECT id FROM users WHERE username = ?"); + $stmt->execute([$username]); + if ($stmt->fetch()) { + $error = 'Username already taken.'; + } else { + // Hash the password + $hashed_password = password_hash($password, PASSWORD_DEFAULT); + + // Insert new user + $insertStmt = $pdo->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, ?)"); + if ($insertStmt->execute([$username, $hashed_password, $role])) { + $success = 'Registration successful! You can now login.'; + } else { + $error = 'Something went wrong. Please try again later.'; + } + } + } catch (PDOException $e) { + $error = 'Database error. Please try again later.'; + } + } +} +?> + + + + + + Register - Psychological Testing System + + + + + + +
+
+
+
+
+

Create an Account

+ +
+ + +
+ +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+ +
+

Already have an account? Login here

+
+
+
+
+
+
+ + + + + + diff --git a/submit_test.php b/submit_test.php new file mode 100644 index 0000000..8098516 --- /dev/null +++ b/submit_test.php @@ -0,0 +1,72 @@ +prepare("SELECT id, score FROM options WHERE id IN ($placeholders)"); + $scoreStmt->execute($option_ids); + $scores = $scoreStmt->fetchAll(PDO::FETCH_KEY_PAIR); + + // Begin transaction + $db->beginTransaction(); + + + + // Calculate total score + foreach ($answers as $question_id => $option_id) { + if (isset($scores[$option_id])) { + $total_score += $scores[$option_id]; + } + } + + // Save the final score in the user_tests table + $userTestStmt = $db->prepare(" + INSERT INTO user_tests (user_id, test_id, score) + VALUES (:user_id, :test_id, :score) + ON DUPLICATE KEY UPDATE score = :score + "); + $userTestStmt->execute([ + 'user_id' => $user_id, + 'test_id' => $test_id, + 'score' => $total_score + ]); + + // Commit transaction + $db->commit(); + + // Redirect to results page + header("Location: test_results.php?test_id=" . $test_id); + exit(); + +} catch (PDOException $e) { + // Rollback on error + if ($db->inTransaction()) { + $db->rollBack(); + } + // A generic error message is better for production + die("An error occurred while submitting your test. Please try again later. Error: " . $e->getMessage()); +} + +?> \ No newline at end of file diff --git a/take_test.php b/take_test.php new file mode 100644 index 0000000..2befdb7 --- /dev/null +++ b/take_test.php @@ -0,0 +1,97 @@ +
Invalid test ID.
"; + require_once 'includes/footer.php'; + exit(); +} + +$test_id = intval($_GET['test_id']); +$user_id = $_SESSION['user_id']; + +try { + $db = db(); + + // Check if user has already taken this test + $checkStmt = $db->prepare("SELECT COUNT(*) FROM user_tests WHERE user_id = :user_id AND test_id = :test_id"); + $checkStmt->execute(['user_id' => $user_id, 'test_id' => $test_id]); + if ($checkStmt->fetchColumn() > 0) { + echo "
You have already completed this test. View your results.
"; + require_once 'includes/footer.php'; + exit(); + } + + + // Fetch test details + $stmt = $db->prepare("SELECT title, description FROM tests WHERE id = :id"); + $stmt->execute(['id' => $test_id]); + $test = $stmt->fetch(PDO::FETCH_ASSOC); + + if (!$test) { + echo "
Test not found.
"; + require_once 'includes/footer.php'; + exit(); + } + + // Fetch questions and options + $questionsStmt = $db->prepare(" + SELECT q.id AS question_id, q.question_text, o.id AS option_id, o.option_text, o.score + FROM questions q + JOIN options o ON q.id = o.question_id + WHERE q.test_id = :test_id + ORDER BY q.id, o.id + "); + $questionsStmt->execute(['test_id' => $test_id]); + $questionsData = $questionsStmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC); + +} catch (PDOException $e) { + die("Database error: " . $e->getMessage()); +} + +?> + +
+
+
+

+
+
+

+
+ +
This test has no questions yet.
+ +
+ + + $options): ?> +
+
+ +
+ + +
+ +
+ + + +
+ +
+
+
+ + diff --git a/test_results.php b/test_results.php new file mode 100644 index 0000000..75510cf --- /dev/null +++ b/test_results.php @@ -0,0 +1,66 @@ +
Invalid test ID.
"; + require_once 'includes/footer.php'; + exit(); +} + +$test_id = intval($_GET['test_id']); +$user_id = $_SESSION['user_id']; + +try { + $db = db(); + + // Fetch test details + $stmt = $db->prepare("SELECT title FROM tests WHERE id = :id"); + $stmt->execute(['id' => $test_id]); + $test = $stmt->fetch(PDO::FETCH_ASSOC); + + if (!$test) { + echo "
Test not found.
"; + require_once 'includes/footer.php'; + exit(); + } + + // Fetch user's score + $scoreStmt = $db->prepare("SELECT score FROM user_tests WHERE user_id = :user_id AND test_id = :test_id"); + $scoreStmt->execute(['user_id' => $user_id, 'test_id' => $test_id]); + $result = $scoreStmt->fetch(PDO::FETCH_ASSOC); + +} catch (PDOException $e) { + die("Database error: " . $e->getMessage()); +} + +?> + +
+
+
+

Results for

+
+
+ +

Your Score:

+

+

This score reflects your responses to the test questions.

+ +
We couldn't find your results for this test. Please make sure you have completed it.
+ +
+ +
+
+ +