prepare(" SELECT id, quiz_name, class_name, subject, total_questions FROM manual_quizzes WHERE id = ? AND institution_id = ? LIMIT 1 "); $stmt->bind_param('ii', $quizId, $institutionId); $stmt->execute(); $quiz = $stmt->get_result()->fetch_assoc(); $stmt->close(); if (!$quiz) { header('Location: manual_mode_setup.php'); exit; } $totalQuestions = (int)$quiz['total_questions']; // Check if key already exists $existingKey = null; $stmt = $conn->prepare(" SELECT id, correct_answers FROM manual_answer_keys WHERE quiz_id = ? LIMIT 1 "); $stmt->bind_param('i', $quizId); $stmt->execute(); $res = $stmt->get_result()->fetch_assoc(); $stmt->close(); if ($res) { $existingKey = $res; } // If existing, prefill array $prefill = array_fill(1, $totalQuestions, ''); if ($existingKey) { $parts = explode(',', $existingKey['correct_answers']); for ($i = 0; $i < $totalQuestions; $i++) { $qNum = $i + 1; if (isset($parts[$i])) { $val = strtoupper(trim($parts[$i])); $prefill[$qNum] = in_array($val, ['A','B','C','D'], true) ? $val : ''; } } } // Handle POST – save/update key if ($_SERVER['REQUEST_METHOD'] === 'POST') { $answers = []; for ($i = 1; $i <= $totalQuestions; $i++) { $key = 'q' . $i; $val = isset($_POST[$key]) ? strtoupper(trim($_POST[$key])) : ''; if (!in_array($val, ['A','B','C','D'], true)) { $errors[] = "Please set a valid option (A/B/C/D) for Question {$i}."; } $answers[] = $val; } if (empty($errors)) { $ansStr = implode(',', $answers); if ($existingKey) { // update $stmt = $conn->prepare(" UPDATE manual_answer_keys SET correct_answers = ? WHERE id = ? "); $stmt->bind_param('si', $ansStr, $existingKey['id']); $stmt->execute(); $stmt->close(); $success = "Answer key updated successfully."; } else { // insert $stmt = $conn->prepare(" INSERT INTO manual_answer_keys (quiz_id, correct_answers) VALUES (?, ?) "); $stmt->bind_param('is', $quizId, $ansStr); $stmt->execute(); $stmt->close(); $success = "Answer key saved successfully."; } // refresh prefill for ($i = 0; $i < $totalQuestions; $i++) { $qNum = $i + 1; $prefill[$qNum] = $answers[$i]; } } } include __DIR__ . '/includes/header.php'; ?>
Quiz:
Class: ·
Subject:
The answer key is used to evaluate each student and to power learning-style and silent-skill detection. Patterns of correct and incorrect answers are analysed to understand how the student thinks.