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

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

    $duration = $_POST['duration'] - 1;
    $hours = $_POST['hours'];
    $minutes = $_POST['minutes'];
    $seconds = $_POST['seconds'];
    
    $usedminutes = $hours*60;

    $totalusedminutes = $usedminutes+$minutes;

    $takenmin = $duration-$totalusedminutes;
    $takensec = 60-$seconds;
    
    $timetaken = "$takenmin min, $takensec sec";

    $correct = 0;
    $wrong = 0;
    $skipped = 0;
    
    $status = 0;
    
    $jsondata = $_POST['jsondata'];

    $decode = json_decode($jsondata);

    $data = json_decode(json_encode($decode), true);

    $paperArray = array(
        "pid" => "$pid",
        "uid" => "$uid",
        "paper_mode" => "web",
        "site_name" => "$site_name",
        "time_taken" => "$timetaken"
    );

    if($pid != "" && $uid !=""){
        $flgIn=$db->insertAry("assignment_attempt_paper",$paperArray);
    }

    if($flgIn > 0){
        $status = 1;
       $i = 0;
       foreach($data as $array){

            $aryData = array(
                    "pid" => "$pid",
                    "uid" => "$uid",
                    "qid" => $array[qid],
                    "att_paper_id" => "$flgIn",
                    "answer" => $array[answer],
                    "qtype" => $array[qtype],
                    "paper_mode" => "web",
                    "site_name" => "$site_name"
                );

        $flg = $db->insertAry("assignment_attempt_question", $aryData);

       }
    }

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

            $allquestions = $db->getRows("select * from assignment_questions where paper_id='$pid' ");
            
            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"){
                            $correct += 1;
                        }
                        else {
                            $wrong += 1;
                        }

                    }
                    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){
                            $correct += 1;
                        }
                        else {
                            $wrong += 1;
                        }
                    }
                    
                    else {
                        if($question[answer]==$att_question[answer]){
                            $correct += 1;
                        }
                        else {
                            $wrong += 1;
                        }
                    }

                }
                else {
                    $skipped += 1;
                }
            }
    
            $paperArray = array(
                "correct" => $correct,
                "wrong" => $wrong,
                "skipped" => $skipped
            );

      	    $db->updateAry("assignment_attempt_paper", $paperArray, "where uid='".$uid."' and pid='".$pid."' ");

    }

echo $status;


?>