<?php 
include("../../db.php");

header('Content-Type: application/json');

    $class = $_REQUEST['class'];

    $objectiveArray = array();

    $pid = $_GET['pid'];

    $objective = $db->getRow("select * from assignment_paper where id='$pid' and paper_type=1");
    // if(count($objectivepapers) > 0){

        // foreach($objectivepapers as $objective){
            $paperArray = array();
            $subject = $db->getVal("select subject from subject_new where id='$objective[parent_subject]' and type='subject' ");
            $sub_subject = $db->getVal("select subject from subject_new where id='$objective[sub_subject]' and type='sub_subject' ");
            $chapter = $db->getVal("select subject from subject_new where id='$objective[chapter]' and type='chapter' ");

            $flag = 0;
            $allquestions = $db->getRows("select * from assignment_questions where paper_id='$objective[id]' ");
            
            	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] ]);
            	}
            
            $checkattempt = $db->getRow("select * from assignment_attempt_paper where pid='".$objective[id]."' and uid='".$uid."' ");
            if(count($checkattempt)>0){
                $flag = 1;
            }

            $tq = count($allquestions);
            $marks = $objective['per_que_marks']*$tq;
            $time = $objective['duration'];
            $level = $objective['level'];
            $description = $objective['paper_description'];
 
            array_push($objectiveArray, ["id"=>"$objective[id]", "all_questions"=>$paperArray, "subject"=>"$subject", "sub_subject"=>"$sub_subject", "chapter"=>"$chapter", "totalque"=>"$tq", "per_que_marks"=>"$objective[per_que_marks]", "marks"=>"$marks", "time"=>"$time", "level"=>"$level", "description"=>"$description", "attempted"=>$flag]);
        // }
    // }

 echo json_encode($objectiveArray);
?>
