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


ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);


$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_type = 2;
$paper_status = $decode['status'];
$livedate = $decode['livedate'];
$livetime = $decode['livetime'];
$session = $decode['session'];
$added_by = $decode['added_by'];

$allquestions = $decode['allquestions'];

$paper_id = $decode['paper_id'];


$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' ");


////

foreach ($allquestions as $que) {


    $qid = $que['qid'];

    $queArray = array(
        "paper_id" => $paper_id,
        "question" => "$que[question]",

        "options" => json_encode(array()),
        "m_options" => json_encode(array()),
        "answer" => json_encode(array()),

        "solution" => $que['solution'],
        "qtype" => "$que[qtype]",
        "added_by" => $added_by,
        "school_id" => 1,
        "session" => "$session",
        "quemark" => $que['quemark']
    );

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

    $queIn = $db->updateAry("assignment_questions", $queArray, " where id='$qid' and paper_id='$paper_id' and session='$session' ");



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

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

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

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

    }


}

$status = 1;
$msg = 'Question added successfully.';

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

$array['jsx'] = $decode;

echo json_encode($array);

