<?php
chdir('../');
session_start();
require_once('public/connection.php');

// Check if student_id, exam_id, and answers are provided
if (!isset($_POST['student_id']) || !isset($_POST['exam_id']) || !isset($_POST['answers'])) {
    echo "Invalid request.";
    exit;
}

$student_id = $_POST['student_id'];
$exam_id = $_POST['exam_id'];
$answers = $_POST['answers'];

// Update the theory score and calculate the total score
$theory_score = 0;
foreach ($answers as $question_id => $answer_text) {
    // Update theory answer (you can add logic here to check if the answer is correct)
    $stmt = $conn->prepare("UPDATE exam_answers SET answer_text = ? WHERE student_id = ? AND question_id = ? AND exam_result_id = (SELECT id FROM exam_results WHERE student_id = ? AND exam_id = ?)");
    $stmt->bind_param("siiii", $answer_text, $student_id, $question_id, $student_id, $exam_id);
    $stmt->execute();

    // Add to theory score (for simplicity, you can add fixed marks or logic to calculate score)
    $theory_score += 5; // Assuming each question is worth 5 marks
}

// Update the total score in exam_results
$stmt = $conn->prepare("UPDATE exam_results SET theory_score = ?, total_score = mcq_score + ?, status = 'completed' WHERE student_id = ? AND exam_id = ?");
$stmt->bind_param("diii", $theory_score, $theory_score, $student_id, $exam_id);
$stmt->execute();

echo "Review submitted successfully.";
?>

