<?php
// Start session
session_start();
// Include database configuration
require_once '../../config/database.php';
// Check if user is logged in and has student role
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Unauthorized access']);
exit;
}
$user_id = $_SESSION['user_id'];
// Check if this is a POST request
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Invalid request method']);
exit;
}
// Get POST data
$student_exam_id = isset($_POST['student_exam_id']) ? intval($_POST['student_exam_id']) : 0;
$question_id = isset($_POST['question_id']) ? intval($_POST['question_id']) : 0;
$answer_type = isset($_POST['answer_type']) ? $_POST['answer_type'] : '';
$answer_value = isset($_POST['answer_value']) ? $_POST['answer_value'] : '';
// Validate required parameters
if (empty($student_exam_id) || empty($question_id) || empty($answer_type)) {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Missing required parameters']);
exit;
}
// Verify the student exam belongs to the current user
$verify_query = "SELECT se.*, es.id as exam_id
FROM student_exams se
JOIN exam_schedules es ON se.exam_id = es.id
WHERE se.id = ? AND se.user_id = ?";
$stmt = $conn->prepare($verify_query);
$stmt->bind_param("ii", $student_exam_id, $user_id);
$stmt->execute();
$verify_result = $stmt->get_result();
if ($verify_result->num_rows === 0) {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Invalid exam session']);
exit;
}
$student_exam = $verify_result->fetch_assoc();
// Check if the exam is in progress
if ($student_exam['status'] !== 'in_progress') {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Exam is not in progress']);
exit;
}
// Verify the question belongs to this exam
$question_query = "SELECT q.*, eqm.id as map_id
FROM questions q
JOIN exam_question_maps eqm ON q.id = eqm.question_id
WHERE q.id = ? AND eqm.exam_id = ?";
$stmt = $conn->prepare($question_query);
$stmt->bind_param("ii", $question_id, $student_exam['exam_id']);
$stmt->execute();
$question_result = $stmt->get_result();
if ($question_result->num_rows === 0) {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Question not found in this exam']);
exit;
}
$question = $question_result->fetch_assoc();
// Prepare variables for answer
$selected_option_id = null;
$answer_text = null;
$is_correct = null;
$marks_obtained = null;
// Process based on answer type
if ($answer_type === 'option' && !empty($answer_value)) {
// For multiple choice or true/false questions
$selected_option_id = intval($answer_value);
// Check if this option is correct
$option_query = "SELECT * FROM question_options
WHERE id = ? AND question_id = ?";
$stmt = $conn->prepare($option_query);
$stmt->bind_param("ii", $selected_option_id, $question_id);
$stmt->execute();
$option_result = $stmt->get_result();
if ($option_result->num_rows > 0) {
$option = $option_result->fetch_assoc();
$is_correct = $option['is_correct'] ? 1 : 0;
$marks_obtained = $is_correct ? $question['marks'] : 0;
} else {
// Invalid option ID
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Invalid option selected']);
exit;
}
} elseif ($answer_type === 'text') {
// For short answer or essay questions
$answer_text = trim($answer_value);
// These will be manually graded later
$is_correct = null;
$marks_obtained = null;
}
// Check if an answer already exists for this question
$existing_answer_query = "SELECT id FROM student_answers
WHERE student_exam_id = ? AND question_id = ?";
$stmt = $conn->prepare($existing_answer_query);
$stmt->bind_param("ii", $student_exam_id, $question_id);
$stmt->execute();
$existing_result = $stmt->get_result();
if ($existing_result->num_rows > 0) {
// Update existing answer
$existing_answer = $existing_result->fetch_assoc();
$answer_id = $existing_answer['id'];
$update_query = "UPDATE student_answers
SET answer_text = ?, selected_option_id = ?, is_correct = ?, marks_obtained = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?";
$stmt = $conn->prepare($update_query);
$stmt->bind_param("siidi", $answer_text, $selected_option_id, $is_correct, $marks_obtained, $answer_id);
if (!$stmt->execute()) {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Failed to update answer: ' . $stmt->error]);
exit;
}
} else {
// Insert new answer
$insert_query = "INSERT INTO student_answers
(student_exam_id, question_id, answer_text, selected_option_id, is_correct, marks_obtained)
VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $conn->prepare($insert_query);
$stmt->bind_param("iisiid", $student_exam_id, $question_id, $answer_text, $selected_option_id, $is_correct, $marks_obtained);
if (!$stmt->execute()) {
header('Content-Type: application/json');
echo json_encode(['success' => false, 'message' => 'Failed to save answer: ' . $stmt->error]);
exit;
}
}
// Return success response
header('Content-Type: application/json');
echo json_encode([
'success' => true,
'message' => 'Answer saved successfully',
'question_id' => $question_id,
'is_correct' => $is_correct,
'marks_obtained' => $marks_obtained
]);
exit;