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

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

    $class = $_REQUEST['class'];

    $subjectiveArray = array();
    $subpaperArray = array();

    $subjectivepapers = $db->getRows("select * from assignment_paper where class='$class' and paper_type=3 and paper_status='Active' ");
    if(count($subjectivepapers) > 0){

        foreach($subjectivepapers as $subjective){

            $subject = $db->getVal("select subject from subject_new where id='$subjective[parent_subject]' and type='subject' ");
            $sub_subject = $db->getVal("select subject from subject_new where id='$subjective[sub_subject]' and type='sub_subject' ");
            $chapter = $db->getVal("select subject from subject_new where id='$subjective[chapter]' and type='chapter' ");

            $allquestions = $db->getRows("select * from assignment_questions where paper_id='$subjective[id]' ");

            $tq = count($allquestions);
            $marks = $subjective['per_que_marks']*$tq;
            $time = $subjective['duration'];
            $level = $subjective['level'];
            $description = $subjective['paper_description'];
            
            if(count($allquestions) > 0){
                $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"=>"http://$_SERVER[HTTP_HOST]/uploads/questionimages/$image[name]"]);
                        }
                        else {
                            array_push($optionimage, ["image"=>"http://$_SERVER[HTTP_HOST]/uploads/questionimages/$image[name]", "optno"=>"$image[option_no]"]);
                        }
                    }

                  array_push($subpaperArray,["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], "quemark"=>$question[quemark] ]);
            	}

            }

            array_push($subjectiveArray, ["id"=>"$subjective[id]", "subject"=>"$subject", "sub_subject"=>"$sub_subject", "chapter"=>"$chapter", "totalque"=>"$tq", "per_que_marks"=>"$subjective[per_que_marks]", "marks"=>"$marks", "time"=>"$time", "level"=>"$level", "description"=>"$description", "all_questions"=>$subpaperArray ]);
        }
    }

    echo json_encode($subjectiveArray);
?>