<?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');
$semester_id = $request->getAttribute('semester_id');
$exams_id = $request->getAttribute('exams_id');
$paper = $request->getAttribute('paper');
$slot = $request->getAttribute('slot');
// Check connection

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$response=array();

//second screen
if(isset($course_id) && isset($semester_id) && isset($exams_id) && isset($paper) && isset($slot))
{


$sql_seat="SELECT paper,papers_shortcode,roll_number,slot FROM pwi_submit WHERE course_id = '$course_id' AND season_id = '$semester_id' AND exams_id = '$exams_id'";

$result_seat=mysqli_query($con,$sql_seat);

    while ($row_seat=mysqli_fetch_assoc($result_seat)) {  


$finaldata = $row_seat;

$get_paper = json_decode($row_seat['paper']);
$get_papers_shortcode = json_decode($row_seat['papers_shortcode']);
$get_slot = json_decode($row_seat['slot']);
$get_roll_number = json_decode($row_seat['roll_number']);

}

$j=0;
foreach($get_roll_number as $data){
    
    if($paper == $get_paper[$j] && $slot == $get_slot[$j])
    {
        $fetch_roll[] =$get_roll_number[$j]; 
        $fetch_paper_shortcode[] =$get_papers_shortcode[$j]; 
    }
    
    $j++;
}

$p=0;
foreach($fetch_roll as $pass)
{
$sql_select = "SELECT total_score,marks,re_check,paper,roll_number,status FROM question_answer_submit WHERE course_id = '$course_id' AND season_id ='$semester_id' AND exam_id = '$exams_id' AND roll_number = '$fetch_roll[$p]'";
$result_select=mysqli_query($con,$sql_select);
  
  while ($row_get=mysqli_fetch_assoc($result_select)) {  
    
           $total_score_datas = json_decode($row_get['total_score']);
        $marks_datas = json_decode($row_get['marks']);
       $data_get[] = $row_get;
       $data_get[$p]['total_score'] = $total_score_datas;
       $data_get[$p]['marks'] = $marks_datas;
      $getmarks[$p] = array_sum($marks_datas);
      
}

 //get pass or fail
     
     $sql_student_table = "SELECT * FROM student WHERE roll_number = '$fetch_roll[$p]' AND status='1'";
     $sql_student_table_con = mysqli_query($con,$sql_student_table);
     while($row_student_table = mysqli_fetch_assoc($sql_student_table_con)){
         $course = $row_student_table['course'];
          $department = $row_student_table['department'];
          $level_id = $row_student_table['level_id'];
          $student_id[] = $row_student_table['student_id'];
         
     }

 // pass gradeing table
      $sql_grade_table = "SELECT * FROM set_grade WHERE $getmarks[$p] between range_start AND range_end AND course_id = '$course' AND dept_id ='$department' AND level_id = '$level_id'";
     $sql_grade_table_con = mysqli_query($con,$sql_grade_table);
     
     while($row_grade_table = mysqli_fetch_assoc($sql_grade_table_con))
     {

                   
          $grade[$p] = $row_grade_table['grade'];
           $state[$p] = $row_grade_table['state'];

// if($grade[$p] == s)
// {
//     $result_pass[$p] = "pass";
// }
// else{
    
//     $result_pass[$p] = "fail";
// }

$array_key_name = array('state');
// $data_get = array_combine
     }


$data_get[$p]['state'] = $state[$p];
     $data_get[$p]['grade'] = $grade[$p];
     $data_get[$p]['student_id'] = $student_id[$p];
     $data_get[$p]['paper_shortcode'] = $fetch_paper_shortcode[$p];


    $p++;
    
}

}
///third screen
else
{
    
$sql_seat="SELECT paper,papers_shortcode,roll_number,slot FROM pwi_submit WHERE course_id = '$course_id' AND season_id = '$semester_id' AND exams_id = '$exams_id'";

$result_seat=mysqli_query($con,$sql_seat);

    while ($row_seat=mysqli_fetch_assoc($result_seat)) {  


$finaldata = $row_seat;

$get_paper = json_decode($row_seat['paper']);
$get_papers_shortcode = json_decode($row_seat['papers_shortcode']);
$get_slot = json_decode($row_seat['slot']);
$get_roll_number = json_decode($row_seat['roll_number']);

}

$j=0;
foreach($get_roll_number as $data){
    
    if($paper == $get_paper[$j])
    {
        $fetch_roll[] =$get_roll_number[$j]; 
        $fetch_paper_shortcode[] =$get_papers_shortcode[$j]; 
    }
    
    $j++;
}

$p=0;
foreach($fetch_roll as $pass)
{
$sql_select = "SELECT total_score,marks,re_check,paper,roll_number,status FROM question_answer_submit WHERE course_id = '$course_id' AND season_id ='$semester_id' AND exam_id = '$exams_id' AND roll_number = '$fetch_roll[$p]'";
$result_select=mysqli_query($con,$sql_select);
  
  while ($row_get=mysqli_fetch_assoc($result_select)) {  
    
           $total_score_datas = json_decode($row_get['total_score']);
        $marks_datas = json_decode($row_get['marks']);
       $data_get[] = $row_get;
       $data_get[$p]['total_score'] = $total_score_datas;
       $data_get[$p]['marks'] = $marks_datas;
      $getmarks[$p] = array_sum($marks_datas);
      
}

 //get pass or fail
     
     $sql_student_table = "SELECT * FROM student WHERE roll_number = '$fetch_roll[$p]' AND status='1'";
     $sql_student_table_con = mysqli_query($con,$sql_student_table);
     while($row_student_table = mysqli_fetch_assoc($sql_student_table_con)){
         $course = $row_student_table['course'];
          $department = $row_student_table['department'];
          $level_id = $row_student_table['level_id'];
          $student_id[] = $row_student_table['student_id'];
         
     }

 // pass gradeing table
      $sql_grade_table = "SELECT * FROM set_grade WHERE $getmarks[$p] between range_start AND range_end AND course_id = '$course' AND dept_id ='$department' AND level_id = '$level_id'";
     $sql_grade_table_con = mysqli_query($con,$sql_grade_table);
     
     while($row_grade_table = mysqli_fetch_assoc($sql_grade_table_con))
     {

                   
          $grade[$p] = $row_grade_table['grade'];
          $state[$p] = $row_grade_table['state'];

// if($grade[$p] == s)
// {
//     $result_pass[$p] = "pass";
// }
// else{
    
//     $result_pass[$p] = "fail";
// }

$array_key_name = array('state');
// $data_get = array_combine
     }


$data_get[$p]['state'] = $state[$p];
     $data_get[$p]['grade'] = $grade[$p];
     $data_get[$p]['student_id'] = $student_id[$p];
     $data_get[$p]['paper_shortcode'] = $fetch_paper_shortcode[$p];


    $p++;
    
}

}


echo json_encode($data_get);


 

?>