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

    $pid = $_POST['pid'];
    $uid = $_POST['uid'];

    $totalquestion = 0;
    $correct = 0;
    $wrong = 0;
    $skipped = 0;
    
    $totalmarks = 0;
    $obtained   = 0;
    $percentage = 0;
    
    $papertime = 0;
    $timetaken = 0;
    
    $status = 0;
    
    $paperArray = array();

    if($pid != "" && $uid !=""){

    $result = $db->getRow("select * from assignment_attempt_paper where pid='$pid' and uid='$uid' ");
        if(count($result) > 0){

         $status = 1;

         $totalquestion = $db->getVal("select count(id) from assignment_questions where paper_id='$pid' ");

         $correct = $result['correct'];
         $wrong = $result['wrong'];
         $skipped = $result['skipped'];
         
         $timetaken = $result['time_taken'];

        $paperinfo= $db->getRow("select * from assignment_paper where id='$pid' ");

        $perquemark = $paperinfo['per_que_marks'];
        $totalmarks = $totalquestion*$perquemark;
        $obtained = $correct*$perquemark;

        $percentage = number_format(($obtained/$totalmarks)*100, 2);

        $papertime = $paperinfo['duration'];

        
        if(count($paperinfo)>0){
            $allquestions = $db->getRows("select * from assignment_questions where paper_id='".$pid."' ");
            if(count($allquestions)>1){
                           $questatus = '';

            foreach($allquestions as $question){

                $correctans = json_decode($question['answer']);

                $att_question = $db->getRow("select * from assignment_attempt_question where att_paper_id='$result[id]' and pid='$pid' and uid='$uid' and qid='$question[id]' ");
                if(count($att_question) > 0){

                    if($question[qtype] == 2){

                        $givenans = json_decode($att_question['answer']);

                        $multipleans = '';

                        foreach($givenans as $ans){
                            if(in_array($ans, $correctans) ){
                                $multipleans = "Correct";
                                $break;
                            }
                        }

                        if($multipleans=="Correct"){
                            $questatus = 1;
                        }
                        else {
                            $questatus = 2;
                        }

                    }
                    elseif($question[qtype] == 6){
                        
                        $matchingans = 0;
                        
                        $givenans = json_decode($att_question['answer']);
                        
                        $corrarr1 = $correctans[0];
                        $corrarr2 = $correctans[1];
                        $corrarr3 = $correctans[2];
                        $corrarr4 = $correctans[3];
                        
                        $arr1 = $givenans[0][0];
                        $arr2 = $givenans[1][0];
                        $arr3 = $givenans[2][0];
                        $arr4 = $givenans[3][0];

                        if(in_array($arr1, $corrarr1) ){
                            $matchingans = 1;
                        }

                        if(in_array($arr2, $corrarr2) ){
                            $matchingans = 1;
                        }

                        if(in_array($arr3, $corrarr3) ){
                            $matchingans = 1;
                        }

                        if(in_array($arr4, $corrarr4) ){
                            $matchingans = 1;
                        }
                        
                        if($matchingans==1){
                            $questatus = 1;
                        }
                        else {
                            $questatus = 2;
                        }

                    }
                    
                    else {
                        if($question[answer]==$att_question[answer]){
                            $questatus = 1;
                        }
                        else {
                            $questatus = 2;
                        }
                    }

                }
                else {
                    $questatus = 3;
                }


                    $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"){
                            $imageArray []= "https://$_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){
                            $optimages []= "https://$_SERVER[HTTP_HOST]/uploads/questionimages/$optimg[name]";
                        }
            	        array_push($optionArray,["answer1"=>$opt[answer], "answer2"=>"$ans2", "option_image"=>$optimages ]);
            	        $j++;
            	        $k++;
            	    }

                    if($att_question[answer] != ''){
                    $givenans = json_decode($att_question[answer], true);
                    }
                    else {
                            $givenans = [];
                    }
                    array_push($paperArray,["qid"=>"$question[id]", "qtype"=>$question[qtype], "question"=>"$question[question]","q_image"=>$imageArray,"options"=>$optionArray, "correct_ans"=>json_decode($question[answer]), "given_ans"=>$givenans, "status"=>"$questatus", "hint"=>"$question[hint]", "solution"=>"$question[solution]" ]);
                } 
            }
        }


        }

    }

    $array['status'] = $status;
    $array['totalque'] = $totalquestion;
    $array['correct'] = $correct;
    $array['wrong'] = $wrong;
    $array['skipped'] = $skipped;
    $array['totalmarks'] = $totalmarks;
    $array['obtained'] = $obtained;
    $array['percentage'] = $percentage;
    $array['papertime'] = $papertime;
    $array['timetaken'] = $timetaken;
    
    $array['solution'] = $paperArray;

    echo json_encode($array);
?>