<?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');
$slot = $request->getAttribute('slot');

$response=array();

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

$sql="SELECT * FROM pwi_submit WHERE course_id='$course_id' AND exams_id='$exams_id' ";
}
$result=mysqli_query($con,$sql);


	
while($row=mysqli_fetch_assoc($result)){
  
    $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][] = $statuslist[$i];
$list_paper[$i][] = $filelist[$i];
$list_paper[$i][] = $slotlist[$i];
$array_dashboard=array('roll_number','papers_shortcode','status','file','slot');
$list_answer_data[] = array_combine($array_dashboard, $list_paper[$i]);
$i = $i+1; 
}
}
function search($array, $search_list) { 

	
	$result = array(); 

	
	foreach ($array as $key => $value) { 

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

	
	return $result; 
} 

$search_items2 = array('papers_shortcode'=>$papers_shortcode,'slot'=>$slot); 
$res = search($list_answer_data, $search_items2); 


$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 $key => $value) {
    if (strpos($value, "1") !== false) {
        $statuspresent_Array[] = $value;
        unset($status[$key]);
    }
    elseif (strpos($value, "0") !== false) {
        $statusabsent_Array[] = $value;
        unset($status[$key]);
    }
    elseif (strpos($value, "2") !== false) {
        $statusmalpractice_Array[] = $value;
        unset($status[$key]);
    }
}


$present=$statuspresent_Array;
$Absent=$statusabsent_Array;
$Malpractice=$statusmalpractice_Array;

$tot=$present+$Malpractice;

$data=array([
// 'students'=>count($roll_number),
'total_files'=>count($tot)

]);
echo json_encode($data);





?>