<?php
require_once 'autoload.php';
require_once '../public/connection.php';
use PhpOffice\PhpSpreadsheet\IOFactory;


if ($_FILES['questions_file']['error'] == UPLOAD_ERR_OK) {
    $filePath = $_FILES['questions_file']['tmp_name'];

    $spreadsheet = IOFactory::load($filePath);
    $sheet = $spreadsheet->getActiveSheet();
    $rows = $sheet->toArray();

    for ($i = 1; $i < count($rows); $i++) {
        list($exam_id, $question_text, $option_a, $option_b, $option_c, $option_d, $correct_answer, $marks) = $rows[$i];

        $question_text = $conn->real_escape_string($question_text);
        $option_a = $conn->real_escape_string($option_a);
        $option_b = $conn->real_escape_string($option_b);
        $option_c = $conn->real_escape_string($option_c);
        $option_d = $conn->real_escape_string($option_d);
        $correct_answer = $conn->real_escape_string($correct_answer);
        $marks = floatval($marks);

        $sql = "INSERT INTO questions (exam_id, question_text, question_image, question_type, option_a, option_b, option_c, option_d, correct_answer, marks)
                VALUES ($exam_id, '$question_text', NULL, 'objective', '$option_a', '$option_b', '$option_c', '$option_d', '$correct_answer', $marks)";
        $conn->query($sql);
    }

    echo "Import complete!";
} else {
    echo "Upload error.";
}
?>
