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

ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);

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

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

$subjectArray = array();


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



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


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 = '2'");

        $total_test_taken = 0;

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

        $my_subject_mark_obt = 0;
        $my_subject_mark_total = 0;

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

        foreach ($subject_paper as $item) {

            $my_marks_sub = $db->getRow("SELECT SUM(aaq.mark_obtained) AS mark_obt, SUM(aq.quemark) as sum_total FROM assignment_attempt_question aaq 
                                LEFT JOIN assignment_questions aq ON aaq.qid = aq.id WHERE aaq.pid = '$item[id]' AND aaq.uid = '$uid' AND aaq.qtype = 7 AND aq.id IS NOT NULL");

            $my_subject_mark_obt += $my_marks_sub['mark_obt'];
            $my_subject_mark_total += $my_marks_sub['sum_total'];

            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]'");

                $rrx = $db->getVal("SELECT ((SUM(aaq.mark_obtained) / SUM(aq.quemark)) * 100) AS DEMO FROM assignment_attempt_question aaq 
                                LEFT JOIN assignment_questions aq ON aaq.qid = aq.id WHERE aaq.pid = '$item[id]' AND aaq.uid = '$row_student[idno]' AND aaq.qtype = 7 AND aq.id IS NOT NULL");

                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;

                   // print_r("student=>".$rrx."==>IDNO=>".$row_student['idno']."\n");
                }

            }

          //  print_r($my_marks_sub);
          //  print_r("avg=>".$avg_mark_sub);
          //  print_r("\nstu_count=>".$stu_count_sub."\n\n");

        }

        /////
        $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 = 0;
        $temp_all = $crt + $wrng + $skip;
        if ($temp_all > 0){
            $my_performance = ($crt / ($crt + $wrng + $skip)) * 100;
        }

        if (is_nan($my_performance)){
            $my_performance = 0;
        }

        $my_score_sub = 0;
        if ($my_subject_mark_total > 0){
            $my_score_sub = ($my_subject_mark_obt / $my_subject_mark_total) * 100;
        }

        $avg_sub_score = 0;
        if ($stu_count_sub > 0) {
            $avg_sub_score = $avg_mark_sub / $stu_count_sub;
        }

        if (is_nan($avg_sub_score)){
            $avg_sub_score = 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(is_nan($my_performance)? 0 : $my_performance,2), "total_test_taken" => $total_test_taken, "correct" => $crt,
                "wrong" => $wrng, "skipped"=>$skip, "top_sub_score" => number_format(is_nan($max_percent_sub)?0:$max_percent_sub, 2),
                "avg_sub_score" => number_format($avg_sub_score ,2) , "total_practice_question" => $total_practice_question,
                "new_sub_mark_obt" => $my_subject_mark_obt, "new_sub_mark_total"=>$my_subject_mark_total, "my_score"=>number_format(is_nan($my_score_sub)?0:$my_score_sub, 2),
                
                "subject_mark_sum" => $my_subject_mark_total ]);

    }



}





$my_marks = $db->getVal("SELECT ((SUM(aaq.mark_obtained) / SUM(aq.quemark)) * 100) as sum_total FROM assignment_attempt_question aaq 
    LEFT JOIN assignment_questions aq ON aaq.qid = aq.id WHERE aaq.uid = '$uid' AND aaq.qtype = 7 AND aq.id IS NOT NULL");

$max_percent = 0;
$user_id = 0;

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

    $rr = $db->getVal("SELECT ((SUM(aaq.mark_obtained) / SUM(aq.quemark)) * 100) as sum_total FROM assignment_attempt_question aaq LEFT JOIN assignment_questions aq ON aaq.qid = aq.id WHERE aaq.uid = '$row_pap[idno]' AND aaq.qtype = 7 AND aq.id IS NOT NULL");

    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['my_mark'] = number_format($my_marks, 2);
$array['top_user_mark'] = number_format($max_percent, 2);
$array['avg_user_mark'] = number_format($avg_mark/$stu_count, 2);

echo json_encode($array);

return


$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 != '' && $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);