<?php

/*Student API*/


/*Fetching API*/


/* 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);

$id = $request->getAttribute('id');
// Check connection

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

$response=array();





$sql_seat="SELECT * FROM pwi_submit WHERE id = '$id'";

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

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


$user_seat = $row_seat;
$roll_number = json_decode($row_seat['roll_number']);
$paper = json_decode($row_seat['paper']);
$papers_shortcode = json_decode($row_seat['papers_shortcode']);
$slot = json_decode($row_seat['slot']);
$status =json_decode($row_seat['status']);


}


foreach(array_keys($roll_number) as $i)
{
    $i+1;
  $sql_student_exam = "SELECT total_score,marks,re_check,paper,roll_number,status FROM question_answer_submit WHERE roll_number = '$roll_number[$i]'";
   
   $result_conn = mysqli_query($con,$sql_student_exam);
   if(mysqli_num_rows($result_conn)>0){
       while($row_get = mysqli_fetch_assoc($result_conn))
     {
        $total_score_datas = json_decode($row_get['total_score']);
        $marks_datas = json_decode($row_get['marks']);
       $data_get[] = $row_get;
       $data_get[$i]['total_score'] = $total_score_datas;
       $data_get[$i]['marks'] = $marks_datas;
      $getmarks[$i] = array_sum($marks_datas);
      
     }
     
     //get pass or fail
     
     $sql_student_table = "SELECT * FROM student WHERE roll_number = '$roll_number[$i]' 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'];
         
     }
     
     // pass gradeing table
      $sql_grade_table = "SELECT * FROM set_grade WHERE $getmarks[$i] 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[$i] = $row_grade_table['grade'];
           $state[$i] = $row_grade_table['state'];



$array_key_name = array('state');
// $data_get = array_combine
     }
    //  $result_last = array_merge($data_get, $array_result_pass);
    
    $data_get[$i]['state'] = $state[$i];
     $data_get[$i]['grade'] = $grade[$i];
   }
   else
   {
       $keys_second = array('total_score','marks','re_check','paper','roll_number','status','state','grade');
       $keys_comb[$i][] = 0;
       $keys_comb[$i][] = 0;
       $keys_comb[$i][] = 0;
       $keys_comb[$i][]  = $paper[$i];
       $keys_comb[$i][] = $roll_number[$i];
       $keys_comb[$i][] = $status[$i];
       $keys_comb[$i][] = "Nill";
       $keys_comb[$i][] = "Nill";
       
       $data_get[] = array_combine($keys_second,$keys_comb[$i]);
   }
   
}

// foreach(array_keys($roll_number) as $i)
// {
//      $keys = array('roll_number','paper','papers_shortcode','slot','status');
     
//     $i +1;
//   $keys1[$i][]  = $roll_number[$i];
//   $keys1[$i][]  = $paper[$i];
//   $keys1[$i][]  = $papers_shortcode[$i];
//   $keys1[$i][]  = $slot[$i];
//   $keys1[$i][]  = $status[$i];

//  $finaldata[] = array_combine($keys,$keys1[$i]);
   
   
// }




//  echo json_encode($finaldata);

//  echo json_encode($data_get);
 
 
   echo json_encode($data_get);


?>