prepare("SELECT id, question_text FROM questions WHERE id = :id"); $stmt->execute(['id' => $question_id]); $question = $stmt->fetch(PDO::FETCH_ASSOC); if (!$question) { return null; } $stmt = db()->prepare("SELECT id, answer_text FROM answers WHERE question_id = :id ORDER BY RAND()"); $stmt->execute(['id' => $question_id]); $question['answers'] = $stmt->fetchAll(PDO::FETCH_ASSOC); return $question; } catch (PDOException $e) { error_log("Error in get_question: " . $e->getMessage()); return null; } } /** * Fetches a set of random question IDs for a given category. * @param string $category_name * @param int $limit * @return array */ function get_random_question_ids(string $category_name, int $limit): array { try { // First, get the category ID from the name $cat_stmt = db()->prepare("SELECT id FROM categories WHERE name = :name"); $cat_stmt->execute(['name' => $category_name]); $category = $cat_stmt->fetch(PDO::FETCH_ASSOC); if (!$category) { return []; } $category_id = $category['id']; // Now, fetch the questions $stmt = db()->prepare("SELECT id FROM questions WHERE category_id = :category_id ORDER BY RAND() LIMIT :limit"); $stmt->bindParam('category_id', $category_id, PDO::PARAM_INT); $stmt->bindParam('limit', $limit, PDO::PARAM_INT); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_COLUMN); } catch (PDOException $e) { error_log("Error in get_random_question_ids: " . $e->getMessage()); return []; } } /** * Checks if the provided answer is correct. * @param int $answer_id * @return bool */ function is_answer_correct(int $answer_id): bool { try { $stmt = db()->prepare("SELECT is_correct FROM answers WHERE id = :id"); $stmt->execute(['id' => $answer_id]); $result = $stmt->fetch(PDO::FETCH_ASSOC); return $result && $result['is_correct']; } catch (PDOException $e) { error_log("Error in is_answer_correct: " . $e->getMessage()); return false; } } // --- State Initialization --- $question_ids_str = ''; $current_index = 0; $score = 0; $category_name = ''; $is_quiz_finished = false; // --- Logic --- // Is this a POST request (user answered a question)? if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Retrieve state from the form $question_ids_str = $_POST['question_ids'] ?? ''; $current_index = isset($_POST['current_index']) ? (int)$_POST['current_index'] : 0; $score = isset($_POST['score']) ? (int)$_POST['score'] : 0; $submitted_answer_id = isset($_POST['answer_id']) ? (int)$_POST['answer_id'] : 0; // Check if the answer was correct if ($submitted_answer_id > 0 && is_answer_correct($submitted_answer_id)) { $score++; } // Move to the next question $current_index++; } // Or is this a GET request (start of the quiz)? else if (isset($_GET['action']) && $_GET['action'] === 'start') { $category_name = $_GET['category'] ?? ''; if (!empty($category_name)) { $question_ids = get_random_question_ids($category_name, $num_questions_per_quiz); if (count($question_ids) < $num_questions_per_quiz) { $error_message = "Non ci sono abbastanza domande (ne servono {$num_questions_per_quiz}) in questa categoria per iniziare il quiz."; $is_quiz_finished = true; // Stop the quiz } else { $question_ids_str = implode(',', $question_ids); } } else { $error_message = "Nome della categoria non valido."; $is_quiz_finished = true; // Stop the quiz } } $question_ids = !empty($question_ids_str) ? explode(',', $question_ids_str) : []; $total_questions = count($question_ids); // Check if the quiz is over if ($current_index >= $total_questions && $total_questions > 0) { $is_quiz_finished = true; } ?>
$question_ids_str,
'Current Index' => $current_index,
'Score' => $score,
'Total Questions' => $total_questions
], true)); ?>