<?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);

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

// Check connection

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

$response=array();

$sql_seat="SELECT paper,papers_shortcode,roll_number,slot,status 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);

$i=0;
    while ($row_seat=mysqli_fetch_assoc($result_seat)) {  


// $finaldata[] = $row_seat;

$get_status = json_decode($row_seat['status']);
$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']);


// $unique_paper = array_unique($get_paper);
// $last_papers_shortcode = array_unique($get_papers_shortcode);
// $last_slot = array_unique($get_slot);
// $last_count_roll_no = count($get_roll_number);

$lastdata[$i] = array('status'=>$get_status,'paper'=>$get_paper,'papers_shortcode'=>$get_papers_shortcode,'slot'=>$get_slot,'roll_number'=>$get_roll_number);

$i++;

}

//combine all array into single array

$array_merge[0]['status'] = $lastdata[0]['status'];
$array_merge[0]['paper'] = $lastdata[0]['paper'];
$array_merge[0]['papers_shortcode'] = $lastdata[0]['papers_shortcode'];
$array_merge[0]['slot'] = $lastdata[0]['slot'];
$array_merge[0]['roll_number'] = $lastdata[0]['roll_number'];
$h=0;
for($j=1;$j<count($lastdata);$j++)
{
 $array_merge[$j]['status'] = array_merge($array_merge[$h]['status'],$lastdata[$j]['status']);
 $array_merge[$j]['paper'] = array_merge($array_merge[$h]['paper'],$lastdata[$j]['paper']);
 $array_merge[$j]['papers_shortcode'] = array_merge($array_merge[$h]['papers_shortcode'],$lastdata[$j]['papers_shortcode']);
 $array_merge[$j]['slot'] = array_merge($array_merge[$h]['slot'],$lastdata[$j]['slot']);
 $array_merge[$j]['roll_number'] = array_merge($array_merge[$h]['roll_number'],$lastdata[$j]['roll_number']);
$h++;
}


$get_final[] = end($array_merge);
   
  
//get unique paper
 
 $get_c_paper = $get_final[0]['paper'];
 
  $unique_paper1 = array_unique($get_c_paper);
  
$unique_final_paper = array_values($unique_paper1);



//unique collection getfinal data
$sort_paper = $get_final[0]['paper'];
$sort_status = $get_final[0]['status'];
$sort_papers_shortcode = $get_final[0]['papers_shortcode'];
$sort_slot = $get_final[0]['slot'];
$sort_roll_number = $get_final[0]['roll_number'];


//sort by paper of the students

$st = 0;
$pap = 0;

foreach($unique_final_paper as $get_paper_st)
{

foreach($sort_paper as $correct_st)
{
    //separte_paper
    if($unique_final_paper[$pap] == $sort_paper[$st])
    {
        $separte_paper[$pap]['paper'][$st] = $sort_paper[$st];
        $separte_paper[$pap]['status'][$st] = $sort_status[$st];
        $separte_paper[$pap]['papers_shortcode'][$st] = $sort_papers_shortcode[$st];
        $separte_paper[$pap]['slot'][$st] = $sort_slot[$st];
        $separte_paper[$pap]['roll_number'][$st] = $sort_roll_number[$st];
        
       //status check
       if(1 == $sort_status[$st])
       {
        //roll_number pass question answer submit table
        $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 = '$sort_roll_number[$st]'";
$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[$st]['total_score'] = $total_score_datas;
       $data_get[$st]['marks'] = $marks_datas;
      $getmarks[$st] = array_sum($marks_datas);
      
}

 //get pass or fail
     
     $sql_student_table = "SELECT * FROM student WHERE roll_number = '$sort_roll_number[$st]' 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[$st] 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[$st] = $row_grade_table['grade'];
          
          

    $result_pass[$pap][$st] = $row_grade_table['state'];


// $data_get = array_combine
     }

}
else
{
    $result_pass[$pap][$st] = "Fail";
}





    }
    
    

    
    
    $st ++;
    
    $total_student[$pap] = count($separte_paper[$pap]['roll_number']);
    
    
    
}

//array_count_values_get_present_absent

    $total_status_values[$pap] = $separte_paper[$pap]['status'];
    
    $status_st[$pap] = array_count_values($total_status_values[$pap]);
    
    
    //array_count_values_pass or fail
    
    $status_pass[$pap] = array_count_values($result_pass[$pap]);
    
    
//paper_shortcode_get_unique

$unique_paper_short[$pap] = end($separte_paper[$pap]['papers_shortcode']);
    
  $pap ++; 
  $st = 0;
}





//check answer

// echo json_encode($unique_paper_short);



//geting grade
$l=0;
foreach($separte_paper as $collect_grade)
{
    if($status_pass[$l]['Fail'] == null)
    {
        $status_pass[$l]['Fail'] = 0;
    }
        if($status_st[$l][0] == null)
    {
        $status_st[$l][0] = 0;
    }
        if($status_pass[$l]['Pass'] == null)
    {
        $status_pass[$l]['Pass'] = 0;
    }
    $answer_data[$l] = array('total_students'=>$total_student[$l],'present'=>$status_st[$l][1],'absent'=>$status_st[$l][0],'paper'=>$unique_final_paper[$l],'passed'=>$status_pass[$l]['Pass'],'failed'=>$status_pass[$l]['Fail'],'paper_shortcode'=>$unique_paper_short[$l]);
    $l++;
}


//final_answer

echo json_encode($answer_data);





?>