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

   $paperid = $_GET['paper_id'];

    $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) > 0){
                $status = 1;

            	foreach($allquestions as $question){

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

            	    $options = json_decode($question[options], true);
            	    $m_options = json_decode($question[m_options], true);
                    $optionArray = array();
            	    $j=0;
            	    $k=1;
            	    foreach($options as $opt){
            	        $ans2 = $m_options[$j][answer];
            	        $optionimages = $db->getRows("select * from assignment_question_files where question_id='$question[id]' and option_no='$k' ");
                        $optimages = array();
                        foreach($optionimages as $optimg){
                            if ($optimg['name'] != '') {
                                $optimages [] = "$_SERVER[HTTP_HOST]/uploads/questionimages/$optimg[name]";
                            }
                        }
            	        array_push($optionArray,["answer1"=>$opt[answer], "answer2"=>"$ans2", "option_image"=>$optimages ]);
            	        $j++;
            	        $k++;
            	    }

                    array_push($paperArray,["qid"=>"$question[id]", "qtype"=>$question[qtype], "question"=>"$question[question]","q_image"=>$imageArray,"options"=>$optionArray,
                        "hint"=>"$question[hint]", "solution"=>"$question[solution]", "quemark"=>"$question[quemark]", "quesession"=>"$question[quesession]",
                        "correct_ans"=>json_decode($question['answer'])]);
            	}

            }
    }

    $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['allquestions'] = $paperArray;

    echo json_encode($array);

?>