<?php
include("../../db.php");
header('Content-Type: application/json');

    $jsondata = $_POST['json'];

    $status = 0;
    $msg = '';
    $array = array();

    $decode = json_decode($jsondata, true);

    $uid = $decode['uid'];
    $pid = $decode['paper_id'];
    $time_taken = $decode['time_taken'];
    $speed = $decode['speed'];
    $allanswer = $decode['allanswers'];

    $paperArray = array(
        "pid" => "$pid",
        "uid" => "$uid",
        "time_taken" => "$time_taken",
        "speed" => "$speed",
        "paper_mode" => "mobile"
        );

    if($pid != "" && $uid !=""){
        $flgIn=$db->insertAry("assignment_attempt_paper",$paperArray);
    }

    if($flgIn > 0){
        foreach($allanswer as $answer){

            $ansArray = array(
                "pid" => "$pid",
                "uid" => "$uid",
                "qid" => "$answer[qid]",
                "att_paper_id" => "$flgIn",
                "answer" => json_encode($answer[answer], true),
                "qtype" => "$answer[qtype]",
                "paper_mode" => "mobile"
                );
                
                $ansIn =$db->insertAry("assignment_attempt_question",$ansArray);
            
        }
        
        $status = 1;
        $msg = 'Paper successfully submitted.';
    }
    else {
        $msg = "something went wrong.";
    }

    $array['status'] = $status;
    $array['msg'] = $msg;

echo json_encode($array);

?>