<?php
include("../../db.php");
//header("Access-Control-Allow-Origin: * ");
header('Content-Type: application/json');

$class = $_REQUEST['class'];
$uid = $_REQUEST['uid'];
$array = array();
$status = 0;

$attemptpaper = 0;
$correct = 0;
$wrong = 0;
$skipped = 0;

$subjectArray = array();

$allSubject = $db->getRows("select * from subject_new where class='$class' and type='subject'");

$student_by_class = $db->getRows("select idno from student where class='$class'");

if(count($allSubject) > 0){
    $status = 1;

    foreach($allSubject as $subject){

        $count_chapter = count($db->getRows("select id from subject_new where parent_subject = '$subject[id]' AND type='chapter'"));
        $covered_chapter = count($db->getRows("select id from syllabus_covered where subject_id = '$subject[id]'"));

        $percentage_covered = (number_format((($covered_chapter / $count_chapter) * 100), 2));

        $subject_paper = $db->getRows("SELECT id FROM assignment_paper WHERE parent_subject = '$subject[id]' AND paper_type = '1'");

        $total_test_taken = 0;

        $crt = 0;
        $wrng = 0;
        $skip = 0;

        ///////
        $max_percent_sub = 0;
        $user_id_sub = 0;
        //
        $avg_mark_sub = 0;
        $stu_count_sub = 0;

        foreach ($subject_paper as $item) {
            $ms = $db->getRow("SELECT correct,wrong,skipped FROM assignment_attempt_paper WHERE uid = '$uid' AND pid = '$item[id]'");

            if ($ms){
                $total_test_taken++;

                $crt += $ms['correct'];
                $wrng += $ms['wrong'];
                $skip += $ms['skipped'];
            }

            foreach ($student_by_class as $row_student) {
                $rrx = $db->getVal("SELECT ((SUM(correct) / (SUM(correct) + SUM(wrong) + SUM(skipped))) * 100) as DEMO FROM assignment_attempt_paper WHERE pid = '$item[id]' AND uid = '$row_student[idno]'");

                if ($rrx != null && $rrx != ''){
                    if ($rrx > $max_percent_sub){
                        $max_percent_sub = $rrx;
                        $user_id_sub= $row_student['idno'];
                    }

                    $stu_count_sub++;
                    $avg_mark_sub += $rrx;
                }
            }

        }

        /////
        $total_practice_question = 0;
        $practice_paper = $db->getRows("SELECT id FROM table_practice WHERE parent_subject = '$subject[id]' /* AND paper_type = '1'*/");
        foreach ($practice_paper as $paper_item) {
            $res_practice = $db->getRows("SELECT id FROM practice_questions WHERE paper_id = '$paper_item[id]'");
            $total_practice_question += count($res_practice);
        }


        $my_performance = ($crt / ($crt + $wrng + $skip)) * 100;
        if (is_nan($my_performance)){
            $my_performance = 0;
        }

        array_push($subjectArray,
            ["id"=>"$subject[id]","subject"=>"$subject[subject]", "image"=>"https://$_SERVER[HTTP_HOST]/uploads/subjectimage/$subject[subject_image]" ,
            "sub_covered_percent" => $percentage_covered, "my_performance" => number_format($my_performance,2), "total_test_taken" => $total_test_taken, "correct" => $crt,
            "wrong" => $wrng, "skipped"=>$skip, "top_sub_score" => number_format($max_percent_sub, 2),
                "avg_sub_score" => number_format($avg_mark_sub/$stu_count_sub, 2), "total_practice_question" => $total_practice_question]);

    }

}

$attempt_paper = $db->getRows("select * from assignment_attempt_paper where uid='$uid' ");
if(count($attempt_paper) > 0){
    $attemptpaper = count($attemptpaper);
    foreach($attempt_paper as $paper){
        $correct += $paper['correct'];
        $wrong += $paper['wrong'];
        $skipped += $paper['skipped'];
    }
}

$max_percent = 0;
$user_id = 0;

$avg_mark = 0;
$stu_count = 0;
foreach ($student_by_class as $row_pap) {

    $rr = $db->getVal("SELECT ((SUM(correct) / (SUM(correct) + SUM(wrong) + SUM(skipped))) * 100) as DEMO FROM assignment_attempt_paper WHERE uid = '$row_pap[idno]'");

    if ($rr != null && $rr != ''){
        if ($rr > $max_percent){
            $max_percent = $rr;
            $user_id= $row_pap['idno'];
        }

        $stu_count++;
        $avg_mark += $rr;
    }

}

$array['status'] = $status;
$array['total_attempt'] = count($attempt_paper);
$array['correct'] = $correct;
$array['wrong'] = $wrong;
$array['skipped'] = $skipped;
$array['data'] = $subjectArray;

$array['top_user_mark'] = number_format($max_percent, 2);
$array['avg_user_mark'] = number_format($avg_mark/$stu_count, 2);

echo json_encode($array);