<?php

/* Attempt to connect to MySQL database */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'Restax_final');

define('DB_PASSWORD', 'Restax_final123');

define('DB_NAME', 'Restax_api_data'); 


$con = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
$course_id = $request->getAttribute('course_id');
$season_id = $request->getAttribute('season_id');
$exams_id = $request->getAttribute('exams_id');
$date = $request->getAttribute('date');
$time = $request->getAttribute('time');
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$response=array();

$list_present = array();
//////////////get submit answer/////////////////
if(isset($course_id)&&isset($season_id)&&isset($exams_id)&&isset($date)&&isset($time))
{
$sql_get_submit="SELECT pwi_submit.*,faculty.Faculty_name FROM pwi_submit INNER JOIN faculty ON faculty.id = pwi_submit.proctor_id WHERE pwi_submit.course_id ='$course_id' AND pwi_submit.season_id ='$season_id' AND pwi_submit.exams_id ='$exams_id' AND pwi_submit.date ='$date' AND pwi_submit.time ='$time'";
}
elseif(isset($course_id)&&isset($season_id)&&isset($exams_id)&&isset($date))
{
$sql_get_submit="SELECT pwi_submit.*,faculty.Faculty_name FROM pwi_submit INNER JOIN faculty ON faculty.id = pwi_submit.proctor_id WHERE pwi_submit.course_id ='$course_id' AND pwi_submit.season_id ='$season_id' AND pwi_submit.exams_id ='$exams_id' AND pwi_submit.date ='$date'";
}
elseif(isset($course_id)&&isset($season_id)&&isset($exams_id))
{
$sql_get_submit="SELECT pwi_submit.*,faculty.Faculty_name FROM pwi_submit INNER JOIN faculty ON faculty.id = pwi_submit.proctor_id WHERE pwi_submit.course_id ='$course_id' AND pwi_submit.season_id ='$season_id' AND pwi_submit.exams_id ='$exams_id'";
}
elseif(isset($course_id)&&isset($season_id)){
  $sql_get_submit="SELECT pwi_submit.*,faculty.Faculty_name FROM pwi_submit INNER JOIN faculty ON faculty.id = pwi_submit.proctor_id WHERE pwi_submit.course_id ='$course_id' AND pwi_submit.season_id ='$season_id'";
}
elseif(isset($course_id)){
  $sql_get_submit="SELECT pwi_submit.*,faculty.Faculty_name FROM pwi_submit INNER JOIN faculty ON faculty.id = pwi_submit.proctor_id WHERE pwi_submit.course_id ='$course_id'";
}
else{
   $sql_get_submit="SELECT pwi_submit.*,faculty.Faculty_name FROM pwi_submit INNER JOIN faculty ON faculty.id = pwi_submit.proctor_id";
}
$result_get_submit=mysqli_query($con,$sql_get_submit);

    while ($row_get_submit=mysqli_fetch_assoc($result_get_submit)) {  


 $user_get_submit[]=$row_get_submit;
 $status_collection[] =json_decode($row_get_submit['status']);
 $id[] =json_decode($row_get_submit['id']);
  $proctor_id[] =json_decode($row_get_submit['proctor_id']);
  $faculty_name[] =$row_get_submit['Faculty_name'];



    }

for($i=0;$i<count($user_get_submit);$i++)
{
$statuscount = $status_collection[$i];
//echo count($statuscount);

//print_r($status_collection[$i][5]);

   for($j=0;$j<count($statuscount);$j++)
    {

          if($status_collection[$i][$j] == 1)

           {
             $present[$i][] = $status_collection[$i][$j];

           }

           elseif($status_collection[$i][$j] == 0)
           {
            $absent[$i][] = $status_collection[$i][$j];
           }

           else
           {
              $malpractice[$i][] = $status_collection[$i][$j];
           }


//echo count($present);
    }
//
//echo count($present[$i]);
 $presented_count+=count($present[$i])+count($malpractice[$i]);
    $total_student_count+=count($status_collection[$i]);

   

       $list_present_count[$i][]=count($present[$i]);
       $list_present_count[$i][]=count($absent[$i]);
       $list_present_count[$i][]=count($malpractice[$i]);
       $list_present_count[$i][]=count($status_collection[$i]);
       $list_present_count[$i][]=$id[$i];
       $list_present_count[$i][]=$proctor_id[$i];
       $list_present_count[$i][]=$faculty_name[$i];
       

   echo $present_student;
$keys = array('present', 'absent', 'malpractice','total','id','proctor_id','proctor_name');
       $list_present[] = array_combine($keys, $list_present_count[$i]);


}
      
 //$attendence_per = $presented_count/$total_student_count*100;
// $absent_per = 100-$attendence_per;
// echo $attendence_per;
 // echo $absent_per;


  echo json_encode($list_present);

       //echo json_encode($status_collection);  

?>