MOSTRAR O RESULATDO NO DISPLAY E DEIXAR MODERNO O STILO DESSE CODIGO INTARATIVO:<?php
session_start();
require_once 'includes/db.php';

// Verifica se o usuário está logado e é estudante
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
    header("Location: login.php");
    exit;
}

// Processamento do formulário
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $userId = $_SESSION['user_id'];
    $answers = $_POST['answers'] ?? [];

    // Gabarito das questões
    $correctAnswers = [
        1 => 'b', 2 => 'a', 3 => 'c', 4 => 'a', 5 => 'b',
        6 => 'c', 7 => 'a', 8 => 'b', 9 => 'c', 10 => 'a',
        11 => 'a', 12 => 'b', 13 => 'c', 14 => 'a', 15 => 'a',
        16 => 'b', 17 => 'a', 18 => 'b', 19 => 'b', 20 => 'a',
        21 => 'b', 22 => 'a', 23 => 'c', 24 => 'b', 25 => 'c'
    ];

    $score = 0;

    // Verifica as respostas corretas
    foreach ($correctAnswers as $question => $correct) {
        if (isset($answers[$question]) && $answers[$question] === $correct) {
            $score++;
        }
    }

    // Define o nível do aluno com base na pontuação
    if ($score <= 5) {
        $level = 'A1';
    } elseif ($score <= 10) {
        $level = 'A2';
    } elseif ($score <= 15) {
        $level = 'B1';
    } elseif ($score <= 20) {
        $level = 'B2';
    } elseif ($score <= 23) {
        $level = 'C1';
    } else {
        $level = 'C2';
    }

    // Atualiza o nível no banco de dados
    $stmt = $pdo->prepare("UPDATE users SET level = :level, placement_done = 1 WHERE id = :id");
    $stmt->execute([
        ':level' => $level,
        ':id' => $userId
    ]);

    // Redireciona para a página inicial
    header("Location: index.php");
    exit;
}
?>

<!DOCTYPE html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <title>Teste de Nivelamento</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: auto;
            padding: 20px;
        }
        h2 {
            text-align: center;
        }
        form {
            margin-top: 30px;
        }
        .question {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-left: 20px;
        }
        input[type="submit"] {
            margin-top: 30px;
            padding: 10px 25px;
            font-size: 16px;
        }
    </style>
</head>
<body>

<h2>Teste de Nivelamento</h2>
<p>Responda às 25 perguntas abaixo para descobrir seu nível de inglês.</p>

<form method="post" action="">
    <?php
    $questions = [
        1 => ["What’s your name?", ['a' => 'How are you?', 'b' => 'My name is John.', 'c' => 'I am fine.']],
        2 => ["Where do you live?", ['a' => 'In São Paulo.', 'b' => 'I’m 25 years old.', 'c' => 'I have a dog.']],
        3 => ["What time is it?", ['a' => 'I like pizza.', 'b' => 'I go to work.', 'c' => 'It\'s 3 PM.']],
        4 => ["Choose the correct verb: 'She ___ to school.'", ['a' => 'goes', 'b' => 'go', 'c' => 'going']],
        5 => ["What’s the past tense of “eat”?", ['a' => 'eating', 'b' => 'ate', 'c' => 'eaten']],
        6 => ["I have been ____ English for 2 years.", ['a' => 'learn', 'b' => 'learned', 'c' => 'learning']],
        7 => ["Which is correct?", ['a' => 'There is a book on the table.', 'b' => 'There are a book.', 'c' => 'There is books.']],
        8 => ["Synonym of 'big'?", ['a' => 'small', 'b' => 'large', 'c' => 'short']],
        9 => ["Which sentence is future?", ['a' => 'I go to the gym.', 'b' => 'I went to the gym.', 'c' => 'I will go.']],
        10 => ["If I ___ rich...", ['a' => 'were', 'b' => 'was', 'c' => 'am']],
        11 => ["He ____ every day.", ['a' => 'studies', 'b' => 'study', 'c' => 'studying']],
        12 => ["They ____ dinner now.", ['a' => 'ate', 'b' => 'are eating', 'c' => 'eats']],
        13 => ["We ____ to Canada last year.", ['a' => 'go', 'b' => 'gone', 'c' => 'went']],
        14 => ["I have ____ this movie.", ['a' => 'seen', 'b' => 'see', 'c' => 'saw']],
        15 => ["Opposite of 'cold'?", ['a' => 'hot', 'b' => 'warm', 'c' => 'cool']],
        16 => ["He is ____ than his brother.", ['a' => 'tall', 'b' => 'taller', 'c' => 'more tall']],
        17 => ["I ____ playing guitar.", ['a' => 'enjoy', 'b' => 'enjoys', 'c' => 'enjoyed']],
        18 => ["We ____ at home yesterday.", ['a' => 'are', 'b' => 'were', 'c' => 'was']],
        19 => ["He ____ already finished.", ['a' => 'have', 'b' => 'has', 'c' => 'had']],
        20 => ["How ____ sugar do you want?", ['a' => 'much', 'b' => 'many', 'c' => 'some']],
        21 => ["They ____ to travel soon.", ['a' => 'go', 'b' => 'are going', 'c' => 'goes']],
        22 => ["She ____ TV every night.", ['a' => 'watches', 'b' => 'watch', 'c' => 'watched']],
        23 => ["We ____ seen that film.", ['a' => 'has', 'b' => 'saw', 'c' => 'have']],
        24 => ["Would you like ____ tea?", ['a' => 'any', 'b' => 'some', 'c' => 'many']],
        25 => ["Can you ____ me please?", ['a' => 'hear', 'b' => 'listen', 'c' => 'help']]
    ];

    foreach ($questions as $num => $q) {
        echo "<div class='question'>";
        echo "<strong>$num. {$q[0]}</strong>";
        foreach ($q[1] as $key => $value) {
            echo "<label><input type='radio' name='answers[$num]' value='$key' required> $value</label>";
        }
        echo "</div>";
    }
    ?>
    <input type="submit" value="Enviar Teste">
</form>

</body>
</html>