<?php
require_once '../Config/Database.php';
require_once '../Model/Submission.php';
$submission = new Submission();

if(isset($_POST['save_script'])){
    $data = $_POST;
    $dataArray = $data["data"];
    $script = '';
    $exam_module =  $data["exam_module"];
    $key =  $data["key"];
    $student_id = $data["student_id"];
    foreach ($dataArray as $questionNumber => $answer) {
        $script .= "<p>$questionNumber: $answer</p>";
    }
    $model_data = [
        "exam_key"=>$key,
        "student_id"=>$student_id,
        "script"=>$script,
        "exam_module"=>$exam_module
    ];
    $exists = $submission->check($student_id,$key,$exam_module);
    if(!$exists){
        $status = $submission->add_submission($model_data);
        if($status){
            echo json_encode(["message"=>"success"]);
        }else{
            echo json_encode(["message"=>"failed"]);
        }
    }else{
        echo json_encode(["message"=>"exists"]);
    }
    
}