<?php

$con = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

$course_id = $request->getAttribute('course_id');
$papers_shortcode = $request->getAttribute('papers_shortcode');
$exams_id = $request->getAttribute('exams_id');
$paper=$request->getAttribute('paper');

$response=array();

if(isset($course_id)&&isset($exams_id)){

$sql="SELECT pwi_submit.*, course.course_name, Exams.examname  FROM pwi_submit 
INNER JOIN course ON course.course_id=pwi_submit.course_id
INNER JOIN Exams ON Exams.id=pwi_submit.exams_id
WHERE pwi_submit.course_id='$course_id' AND pwi_submit.exams_id='$exams_id' 
";
}
$result=mysqli_query($con,$sql);

if ($result->num_rows > 0) {
	
while($row=mysqli_fetch_assoc($result)){
    $exams_id=$row['exams_id'];
    $course_id=$row['course_id'];
    $course_name=$row['course_name'];
    $exams_name=$row['examname'];
    $p=json_decode($row['paper'], true);
    $registernumberlist=json_decode($row['roll_number'],true);
    $statuslist=json_decode($row['status'],true);
    $slotlist=json_decode($row['slot'],true);
    $example = json_decode($row['papers_shortcode'], true);
    $filelist = json_decode($row['file'], true);
}
    $i=0;
  
foreach( $registernumberlist as $name ) {
$list_paper[$i][] = $name;
$list_paper[$i][] = $example[$i];
$list_paper[$i][] = $p[$i];
$list_paper[$i][] = $statuslist[$i];
$list_paper[$i][] = $filelist[$i];
$array_dashboard=array('roll_number','papers_shortcode','paper','status','file',);
$list_answer_data[] = array_combine($array_dashboard, $list_paper[$i]);

$sql2="SELECT state 
         FROM pwe_answersheet_submit 
         WHERE state='pass' AND  roll_number = '$name' ";
        $result2=mysqli_query($con,$sql2);
      
        while ($row2=mysqli_fetch_assoc($result2)) {
          $passed[$i][] = $row2['state'];
         
		}

		$sql3="SELECT state 
         FROM pwe_answersheet_submit 
         WHERE state='fail' AND  roll_number = '$name' ";
        $result3=mysqli_query($con,$sql3);
      
        while ($row3=mysqli_fetch_assoc($result3)) {
          $failed[$i][] = $row3['state'];
         
		}
		
$i = $i+1; 
}

function search($array, $search_list) { 
	
	$result = array(); 
	foreach ($array as $attendance => $value) { 

		foreach ($search_list as $h => $k) { 
			
			if (!isset($value[$h]) || $value[$h] != $k) 
			{ 			
				continue 2; 
			} 
		} 
		$result[] = $value; 
	} 
	return $result; 
} 


$search_items = array('papers_shortcode'=>$papers_shortcode); 

$res = search($list_answer_data, $search_items); 

$j=0;
foreach ($res as $var) { 
	$roll_number[$j][]=$var['roll_number'];
	$status[$j]=$var['status'];
	
$j=$j+1; 
} 

$statuspresent_Array = array();
$statusabsent_Array = array();
$statusmalpractice_Array = array();

foreach ($status as $attendance => $value) {
    if (strpos($value, "1") !== false) {
        $statuspresent_Array[] = $value;
        unset($status[$attendance]);
    }
    else if (strpos($value, "0") !== false) {
        $statusabsent_Array[] = $value;
        unset($status[$attendance]);
    }
    else if (strpos($value, "2") !== false) {
        $statusmalpractice_Array[] = $value;
        unset($status[$attendance]);
    }
}

$data=array([
'Total_students'=>count($roll_number),
'Malpractice'=>count($statusmalpractice_Array),
'Absent'=>count($statusabsent_Array),
'Passed'=>count($passed),
'Failed'=>count($failed),
// 'exams_id'=>$exams_id,
// 'course'=>$course_id,
// 'course_name'=>$course_name,
// 'exam_name'=>$exams_name,
// 'papers_shortcode'=>$papers_shortcode
// 'paper_id'=>$paper
]);
echo json_encode($data);


}
else{
	$response=array('status' => "false",'Message' => "Attendance not found");
         echo json_encode(array($response));
}

?>