<?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);

$paperId = $decode['paper_id'];

$paper_status = $decode['status'];
$session = $decode['session'];
$added_by = $decode['added_by'];

$allquestions = $decode['allquestions'];

$paperArray = array("paper_status" => "$paper_status");

$db->updateAry("table_practice", $paperArray, " where id='$paperId'");

if (true) {
    foreach ($allquestions as $que) {

        $queArray = array(
            "paper_id" => $paperId,
            "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("practice_questions", $queArray);

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

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

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

            foreach ($option_image as $optimg) {
                $optimgAry = array(
                    "paper_id" => $paperId,
                    "question_id" => $queIn,
                    "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("practice_question_files", $optimgAry);
            }

        }


    }

    $status = 1;
    $msg = 'Practice successfully created.';
}

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

echo json_encode($array);