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

$jsondata = $_POST['json'];

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

$decode = json_decode($jsondata, true);

//$class = $decode['class'];
//$parent_subject = $decode['subject'];
//$sub_subject = $decode['sub_subject'];
//$chapter = $decode['chapter'];


$per_que_marks = $decode['perquemark'];
$level = $decode['level'];
$paper_description = $decode['description'];
$duration = $decode['duration'];
$total_questions = $decode['totalque'];
$paper_status = $decode['status'];
$livedate = $decode['livedate'];
$livetime = $decode['livetime'];


$session = $decode['session'];
$paper_id = $decode['paper_id'];
$allquestions = $decode['allquestions'];

$paperArray = array(
    "per_que_marks" => "$per_que_marks",
    "level" => "$level",
    "paper_description" => "$paper_description",
    "duration" => "$duration",
    "total_questions" => "$total_questions",
    "paper_status" => "$paper_status",
    "livedate" => "$livedate",
    "livetime" => "$livetime",
);

$flgIn = $db->updateAry("assignment_paper", $paperArray, " where id='$paper_id' and session='$session' ");

if ($paper_id > 0) {
    foreach ($allquestions as $que) {

        $queArray = array(
            "paper_id" => $paper_id,
            "question" => "$que[question]",
            "options" => json_encode($que['options']),
            "m_options" => json_encode($que['m_options']),
            "answer" => json_encode($que['correct_ans']),
            "qtype" => "$que[qtype]",
            "session" => "$session"
        );

        $queIn = $db->insertAry("assignment_questions", $queArray);

        $q_image = $que['q_image'];
        $option_image = $que['options_image'];

        if ($queIn > 0) {
            foreach ($q_image as $qimg) {
                if($qimg) {
                    $imgAry = array(
                        "paper_id" => $paper_id,
                        "question_id" => $queIn,
                        "name" => $qimg,
                        "image_type" => "question_image",
                        "qtype" => "$que[qtype]",
                        "status" => 1
                    );

                    $imgIn = $db->insertAry("assignment_question_files", $imgAry);
                }
            }

            foreach ($option_image as $optimg) {
                if ($optimg['img_name']) {
                    $optimgAry = array(
                        "paper_id" => $paper_id,
                        "question_id" => $queIn,
                        "name" => $optimg['img_name'],
                        "option_no" => $optimg['option_number'],
                        "image_type" => "option_image",
                        "qtype" => "$que[qtype]",
                        "status" => 1
                    );
                    $optimgIn = $db->insertAry("assignment_question_files", $optimgAry);
                }
            }

        }

    }

    $status = 1;
    $msg = 'Paper successfully submitted.';
} else {
    $msg = "something went wrong.";
}

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

echo json_encode($array);

?>