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

  $paperid = $_POST['paper_id'];
    // $paperid = 93;
    $status = 0;

    $paperArray = array();

    $array = array();

	$paper = $db->getRow("select * from assignment_paper where id='".$paperid."' ");
    if(count($paper)>0){

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

            	foreach($allquestions as $question){
            	    $options = json_decode($question[options], true);
            	    $m_options = json_decode($question[m_options], true);
            	    
                    $allimages = $db->getRows("SELECT * from assignment_question_files where question_id='$question[id]' ");
                    $imageArray = array();
                    $optionimage = array();

                    foreach($allimages as $image){
                        if($image[image_type]=="question_image"){
                            array_push($imageArray, ["image"=>"https://$_SERVER[HTTP_HOST]/uploads/questionimages/$image[name]"]);
                        }
                        else {
                            array_push($optionimage, ["image"=>"https://$_SERVER[HTTP_HOST]/uploads/questionimages/$image[name]", "optno"=>"$image[option_no]"]);
                        }
                    }

                  array_push($paperArray,["id"=>"$question[id]","question"=>"$question[question]","q_image"=>$imageArray,"options"=>$options, "opt_image"=>$optionimage, "m_options"=>$m_options, "answer"=>"$question[answer]", "hint"=>"$question[hint]", "solution"=>"$question[solution]", "qtype"=>$question[qtype] ]);
            	}

            }
    }
    
    $site_name = $db->getVal("select online_study_server from tbl_info");

    $array['status'] = $status;
    $array['level'] = $paper['level'];
    $array['description'] = $paper['paper_description'];
    $array['duration'] = $paper['duration'];
    $array['perquemark'] = $paper['per_que_marks'];
    $array['totalque'] = count($allquestions);
    $array['paper_detail'] = $paperArray;
    $array['site_name'] = $site_name;

    echo json_encode($array);
?>