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

$chapterid = $_GET['chapter_id'];

$status = 0;

$subjectiveArray = array();
$ncertArray = array();

$paperArray = array();

$array = array();

$subpaper = $db->getRow("select * from assignment_paper where chapter='$chapterid' and paper_type=2 ");
if(count($subpaper)>0){

    $allsubquestions = $db->getRows("select * from assignment_questions where paper_id='".$subpaper['id']."' ");
    if(count($allsubquestions) > 0){
        $status = 1;

        foreach($allsubquestions as $question){

            $allimages = $db->getRows("SELECT * from assignment_question_files where question_id='$question[id]' ");
            $imageArray = array();
            $solutionimg = array();

            foreach($allimages as $image){
                if($image[image_type]=="question_image"){
                    $imageArray []= "https://$_SERVER[HTTP_HOST]/uploads/questionimages/$image[name]";
                }
                if($image[image_type]=="solution_image"){
                    $solutionimg []= "https://$_SERVER[HTTP_HOST]/uploads/questionimages/$image[name]";
                }
            }

            array_push($subjectiveArray,["qid"=>"$question[id]", "qtype"=>$question[qtype], "question"=>"$question[question]","q_image"=>$imageArray, "solution"=>"$question[solution]","sol_image"=>$solutionimg, "quemark"=>"$question[quemark]", "quesession"=>"$question[quesession]" ]);
        }

    }
}


$ncertpaper = $db->getRow("select * from assignment_paper where chapter='$chapterid' and paper_type=3 ");
if(count($ncertpaper)>0){

    $allquestions = $db->getRows("select * from assignment_questions where paper_id='".$ncertpaper['id']."' ");
    if(count($allquestions) > 0){
        $status = 1;

        foreach($allquestions as $question){

            $allimages = $db->getRows("SELECT * from assignment_question_files where question_id='$question[id]' ");
            $imageArray = array();
            $solutionimg = array();
            foreach($allimages as $image){
                if($image[image_type]=="question_image"){
                    $imageArray []= "$_SERVER[HTTP_HOST]/uploads/questionimages/$image[name]";
                }
                if($image[image_type]=="solution_image"){
                    $solutionimg []= "$_SERVER[HTTP_HOST]/uploads/questionimages/$image[name]";
                }
            }

            array_push($ncertArray,["qid"=>"$question[id]", "qtype"=>$question[qtype], "question"=>"$question[question]","q_image"=>$imageArray, "solution"=>"$question[solution]","sol_image"=>$solutionimg, "quemark"=>"$question[quemark]", "quesession"=>"$question[quesession]" ]);
        }

    }
}

$array['status'] = $status;
$array['subjective']['level'] = "$subpaper[level]";
$array['subjective']['description'] = "$subpaper[paper_description]";
$array['subjective']['duration'] = "$subpaper[duration]";
$array['subjective']['perquemark'] = "$subpaper[per_que_marks]";
$array['subjective']['paper_status'] = "$subpaper[paper_status]";
$array['subjective']['livedate'] = "$subpaper[livedate]";
$array['subjective']['livetime'] = "$subpaper[livetime]";
$array['subjective']['totalque'] = count($allsubquestions);
$array['subjective']['allquestion'] = $subjectiveArray;


$array['ncert']['level'] = "$ncertpaper[level]";
$array['ncert']['description'] = "$ncertpaper[paper_description]";
$array['ncert']['duration'] = "$ncertpaper[duration]";
$array['ncert']['perquemark'] = "$ncertpaper[per_que_marks]";
$array['ncert']['paper_status'] = "$ncertpaper[paper_status]";
$array['ncert']['livedate'] = "$ncertpaper[livedate]";
$array['ncert']['livetime'] = "$ncertpaper[livetime]";
$array['ncert']['totalque'] = count($allquestions);
$array['ncert']['allquestion'] = $ncertArray;

// $array['description'] = $paper['paper_description'];
// $array['duration'] = $paper['duration'];
// $array['perquemark'] = $paper['per_que_marks'];
// $array['totalque'] = count($allquestions);
// $array['allquestions'] = $paperArray;

echo json_encode($array);

?>