diff --git a/admin_panel.php b/admin_panel.php new file mode 100644 index 0000000..5daf619 --- /dev/null +++ b/admin_panel.php @@ -0,0 +1,60 @@ +query(" + SELECT u.username, t.title, ut.score, ut.completed_at + FROM user_tests ut + JOIN users u ON ut.user_id = u.id + JOIN tests t ON ut.test_id = t.id + ORDER BY ut.completed_at DESC +"); +$results = $stmt->fetchAll(); + +require_once 'includes/header.php'; +?> + +
+

Панель психолога

+

Добро пожаловать, !

+ +

Результаты тестов

+ + + + +
+ + + + + + + + + + + + + + + + + + + +
ПользовательТестБаллыДата
+
+ + +
+ + \ No newline at end of file diff --git a/assets/pasted-20251204-152901-9d863924.jpg b/assets/pasted-20251204-152901-9d863924.jpg new file mode 100644 index 0000000..352929b Binary files /dev/null and b/assets/pasted-20251204-152901-9d863924.jpg differ diff --git a/assets/vm-shot-2025-12-04T15-27-54-201Z.jpg b/assets/vm-shot-2025-12-04T15-27-54-201Z.jpg new file mode 100644 index 0000000..352929b Binary files /dev/null and b/assets/vm-shot-2025-12-04T15-27-54-201Z.jpg differ diff --git a/includes/header.php b/includes/header.php index f4695da..6a17fa2 100644 --- a/includes/header.php +++ b/includes/header.php @@ -28,7 +28,13 @@ if (session_status() === PHP_SESSION_NONE) { + + diff --git a/register.php b/register.php index b45d055..600a449 100644 --- a/register.php +++ b/register.php @@ -8,9 +8,9 @@ $success = ''; if ($_SERVER["REQUEST_METHOD"] == "POST") { $username = trim($_POST['username']); $password = $_POST['password']; - $role = $_POST['role']; + $role = $_POST['role'] ?? 'user'; - if (empty($username) || empty($password) || empty($role)) { + if (empty($username) || empty($password)) { $error = 'Please fill in all fields.'; } elseif (strlen($password) < 6) { $error = 'Password must be at least 6 characters long.'; @@ -79,13 +79,14 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") { -
- +
+
+
diff --git a/retake_test.php b/retake_test.php new file mode 100644 index 0000000..f81d8a3 --- /dev/null +++ b/retake_test.php @@ -0,0 +1,52 @@ +beginTransaction(); + + // Delete from user_answers + $stmt_answers = $db->prepare("DELETE FROM user_answers WHERE user_id = :user_id AND question_id IN (SELECT id FROM questions WHERE test_id = :test_id)"); + $stmt_answers->execute(['user_id' => $user_id, 'test_id' => $test_id]); + + // Delete from user_tests + $stmt_tests = $db->prepare("DELETE FROM user_tests WHERE user_id = :user_id AND test_id = :test_id"); + $stmt_tests->execute(['user_id' => $user_id, 'test_id' => $test_id]); + + // Commit the transaction + $db->commit(); + + // Redirect to the test page + header('Location: take_test.php?test_id=' . $test_id); + exit(); + +} catch (PDOException $e) { + // Rollback the transaction if something failed + if ($db->inTransaction()) { + $db->rollBack(); + } + // You might want to log this error instead of showing it to the user + die("Database error while trying to reset the test. Please try again."); +} + +// No need for footer as we are redirecting +?> diff --git a/submit_test.php b/submit_test.php index 8098516..5d47790 100644 --- a/submit_test.php +++ b/submit_test.php @@ -32,13 +32,24 @@ try { // Begin transaction $db->beginTransaction(); + // Prepare statement for inserting or updating user answers + $answerStmt = $db->prepare( + "INSERT INTO user_answers (user_id, question_id, option_id) VALUES (:user_id, :question_id, :option_id) + ON DUPLICATE KEY UPDATE option_id = VALUES(option_id)" + ); - - // Calculate total score + // Calculate total score and insert individual answers foreach ($answers as $question_id => $option_id) { if (isset($scores[$option_id])) { $total_score += $scores[$option_id]; } + + // Insert the answer + $answerStmt->execute([ + 'user_id' => $user_id, + 'question_id' => $question_id, + 'option_id' => $option_id + ]); } // Save the final score in the user_tests table diff --git a/take_test.php b/take_test.php index 2befdb7..eade55c 100644 --- a/take_test.php +++ b/take_test.php @@ -20,17 +20,16 @@ $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(); + 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]); @@ -52,12 +51,33 @@ try { "); $questionsStmt->execute(['test_id' => $test_id]); $questionsData = $questionsStmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_ASSOC); - + $question_keys = array_keys($questionsData); } catch (PDOException $e) { die("Database error: " . $e->getMessage()); } - ?> +
@@ -67,31 +87,98 @@ try {


+
This test has no questions yet.
-
+
+
+
+

+ + - $options): ?> -
-
- -
- - -
- + $question_id): + $options = $questionsData[$question_id]; + $is_first = ($index === 0); + $is_last = ($index === count($question_keys) - 1); + ?> +
+
+
+ +
+ + +
+ +
+
+ + + + + + + + + +
- -
+ + diff --git a/test_results.php b/test_results.php index 75510cf..31787d9 100644 --- a/test_results.php +++ b/test_results.php @@ -58,7 +58,8 @@ try {