<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>

	    <link href="https://fonts.googleapis.com/css?family=Roboto+Condensed" rel="stylesheet">
        <link href="dist/attention.css" rel="stylesheet">
        <meta http-equiv=Content-Type content=text/html; charset=iso-8859-1 />
<title>cbt</title>
<link rel=stylesheet type=text/css href=css/style1.css />
</head>

<body>
        <script src="dist/attention.js"></script>
		
<?php
// Include the database connection
include 'connection/config.php';

// Initialize session to store current question index
session_start();

// Fetch the current question index from the session, default to 1 if not set
if (!isset($_SESSION['question_index'])) {
    $_SESSION['question_index'] = 1;
}

// Handle "Next" and "Previous" button clicks
if (isset($_POST['next'])) {
    $_SESSION['question_index']++;
} elseif (isset($_POST['previous'])) {
    $_SESSION['question_index'] = max(1, $_SESSION['question_index'] - 1);
}

// Fetch the current question based on the index
$questionIndex = $_SESSION['question_index'];

try {
    // Prepare the SQL statement to fetch the question and its options from 'questionbase' table in 'cbt' database
    $sql = "SELECT * FROM questionbase LIMIT 1 OFFSET :question_index";
    $stmt = $pdo->prepare($sql);
    $stmt->bindValue(':question_index', $questionIndex - 1, PDO::PARAM_INT);
    $stmt->execute();
    
    // Fetch the question row
    $question = $stmt->fetch(PDO::FETCH_ASSOC);
    
    // If the question exists, display it
    if ($question) {
        ?>
        <form method="POST" action="">
	    <p><?php echo "Question " . $questionIndex . ": " . $question['qst']; ?></p>
            <input type="radio" name="selected_option" value="a" required> <?php echo $question['opt1']; ?><br>
            <input type="radio" name="selected_option" value="b"> <?php echo $question['opt2']; ?><br>
            <input type="radio" name="selected_option" value="c"> <?php echo $question['opt3']; ?><br>
            <input type="radio" name="selected_option" value="d"> <?php echo $question['opt4']; ?><br>

            <!-- Hidden input to store the correct answer -->
            <input type="hidden" name="correct_option" value="<?php echo $question['ans']; ?>">

            <!-- Previous and Next buttons -->
            <button type="submit" name="previous">Previous</button>
            <button type="submit" name="next">Next/Submit</button>
        </form>
        <?php
    } else {
        echo "No more questions.";
	session_unset();  // Unset all session variables
	session_destroy();  // Destroy the session

	// Optional: Redirect the user to a login page or another page after session destruction
	header("Location: index.php");
    }
    
    // Check if the user selected the correct answer when the "Next" button is clicked
    if (isset($_POST['next']) && isset($_POST['selected_option'])) {
        $selectedOption = $_POST['selected_option'];
        $correctOption = $_POST['correct_option'];
        
        if ($selectedOption === $correctOption) {
            echo "<p style='color:green;'>Correct Answer!</p>";
        } else {
            echo "<p style='color:red;'>Wrong Answer! The correct answer was: " . $question[$correctOption] . "</p>";
        }
    }

} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
?>


</body>
</html>

<?php 

?>