219 lines
8.4 KiB
PHP
219 lines
8.4 KiB
PHP
<?php
|
|
require_once 'db/config.php';
|
|
|
|
// --- Configuration ---
|
|
$num_questions_per_quiz = 5;
|
|
|
|
// --- Helper Functions ---
|
|
|
|
/**
|
|
* Fetches a single question and its answers from the database.
|
|
* @param int $question_id
|
|
* @return array|null
|
|
*/
|
|
function get_question(int $question_id): ?array {
|
|
try {
|
|
$stmt = db()->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;
|
|
}
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="it">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Quiz in Corso - MAVI</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body>
|
|
<div class="container mt-5">
|
|
<a href="quiz.php" class="btn btn-secondary mb-4">← Torna alle Categorie</a>
|
|
|
|
<?php if ($is_quiz_finished): ?>
|
|
<div class="card text-center">
|
|
<div class="card-body">
|
|
<h1 class="card-title">Quiz Terminato!</h1>
|
|
<?php if (isset($error_message)): ?>
|
|
<div class="alert alert-warning"><?php echo $error_message; ?></div>
|
|
<?php else: ?>
|
|
<p class="card-text fs-4">Il tuo punteggio finale è:</p>
|
|
<p class="display-3"><strong><?php echo $score; ?> su <?php echo $total_questions; ?></strong></p>
|
|
<?php endif; ?>
|
|
<a href="quiz.php" class="btn btn-primary mt-3">Gioca Ancora</a>
|
|
</div>
|
|
</div>
|
|
|
|
<?php elseif (!empty($question_ids)): ?>
|
|
<?php
|
|
$question_id = (int)$question_ids[$current_index];
|
|
$question = get_question($question_id);
|
|
?>
|
|
<?php if ($question): ?>
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Domanda <?php echo ($current_index + 1); ?> di <?php echo $total_questions; ?></h5>
|
|
<p class="card-text fs-4"><?php echo htmlspecialchars($question['question_text']); ?></p>
|
|
|
|
<form method="POST" action="quiz_play.php">
|
|
<?php foreach ($question['answers'] as $answer): ?>
|
|
<div class="form-check">
|
|
<input class="form-check-input" type="radio" name="answer_id" value="<?php echo $answer['id']; ?>" id="answer_<?php echo $answer['id']; ?>" required>
|
|
<label class="form-check-label" for="answer_<?php echo $answer['id']; ?>">
|
|
<?php echo htmlspecialchars($answer['answer_text']); ?>
|
|
</label>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
|
|
<input type="hidden" name="question_ids" value="<?php echo htmlspecialchars($question_ids_str); ?>">
|
|
<input type="hidden" name="current_index" value="<?php echo $current_index; ?>">
|
|
<input type="hidden" name="score" value="<?php echo $score; ?>">
|
|
|
|
<button type="submit" class="btn btn-primary mt-4">Rispondi</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- VISIBLE DEBUG INFO -->
|
|
<div class="card bg-light mt-4">
|
|
<div class="card-body">
|
|
<h6 class="card-title">Informazioni di Debug</h6>
|
|
<pre class="small"><?php echo htmlspecialchars(print_r([
|
|
'Question IDs' => $question_ids_str,
|
|
'Current Index' => $current_index,
|
|
'Score' => $score,
|
|
'Total Questions' => $total_questions
|
|
], true)); ?></pre>
|
|
</div>
|
|
</div>
|
|
|
|
<?php else: ?>
|
|
<div class="alert alert-danger">Errore: Impossibile caricare la domanda.</div>
|
|
<?php endif; ?>
|
|
|
|
<?php elseif ($_SERVER['REQUEST_METHOD'] !== 'POST'): ?>
|
|
<div class="alert alert-info">Seleziona una categoria dalla <a href="quiz.php">pagina dei quiz</a> per iniziare.</div>
|
|
<?php endif; ?>
|
|
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|