<?php
 $con = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
//$course_id = $request->getAttribute('course_id');
$roll_no = $request->getAttribute('roll_no');
date_default_timezone_set('Asia/Kolkata');
$response=array();

$timestamp =  date("H:i:s", time());
$time3 = strtotime($timestamp);

// exam start query
$sql="SELECT student.roll_number,student.student_name,Exams.exam_start_date_time,Exams.exam_end_date_time,Exams.exam_end,assign_papers.course_id,assign_papers.season_id,assign_papers.exams_id,assign_papers.id,assign_papers.papers_id,season.seasonname FROM student INNER JOIN Exams ON Exams.course_id = student.course INNER JOIN assign_papers ON assign_papers.course_id = Exams.course_id AND assign_papers.season_id = Exams.season_id AND assign_papers.exams_id = Exams.id INNER JOIN season ON season.id = assign_papers.season_id WHERE student.roll_number='$roll_no' AND Exams.exam_start = 1 ";

$i=0;
$result=mysqli_query($con,$sql);
///

if(mysqli_num_rows($result)>0){
    
    while ($row=mysqli_fetch_assoc($result)) {  
      $user[$i]=$row;
     $user[$i]['Get_paper'] = json_decode($row['id']);
     $course_data = $row['course_id'];
     $season_data = $row['season_id'];
     $exam_data = $row['exams_id'];
     $exam_end_data = $row['exam_end'];
      $start_date = $row['exam_start_date_time'];
      $end_date = $row['exam_end_date_time'];
      $user1=json_decode($row['papers_id']);
      $user[$i]['papers_count'] = count($user1);
      $user[$i]['result'] = "False";
      
      
      $current_date = date("yy-m-d");
       
       //one day before
      $one_day_before = date("yy-m-d",strtotime($start_date."-1 day"));
      
      //one day after
        $one_day_after = date("yy-m-d",strtotime($current_date."+1 day"));
      
        $end_date_change = date("yy-m-d",strtotime($end_date));
      
      if($current_date < $one_day_before){
          
          $user[$i]['exam_type'] = "UPCOMING";
      }
      
      else if($start_date <= $current_date && $current_date <= $end_date_change )
      {



$sql_check = "SELECT schedule_exam.*,set_papers.papers_shortcode,set_papers.paper_name,set_papers.credits,set_marks.max_marks,seated_students.blockid,seated_students.room_id,set_rooms.room_name,set_blocks.block_shortcode FROM schedule_exam INNER JOIN set_papers ON set_papers.papers_id =schedule_exam.paper_id INNER JOIN set_marks ON set_marks.paper = set_papers.papers_id INNER JOIN seated_students ON seated_students.course_id = schedule_exam.course_id AND seated_students.season_id = schedule_exam.season_id AND seated_students.exams_id = schedule_exam.exams_id AND seated_students.date = schedule_exam.date AND seated_students.time = schedule_exam.time INNER JOIN set_rooms ON  set_rooms.room_id = seated_students.room_id INNER JOIN set_blocks ON set_blocks.block_id = seated_students.blockid WHERE schedule_exam.course_id = '$course_data' AND schedule_exam.season_id = '$season_data' AND schedule_exam.exams_id = '$exam_data' AND schedule_exam.date = '$current_date'";

$result_second = mysqli_query($con,$sql_check);

if(mysqli_num_rows($result_second)>0){
    
          while($row_second = mysqli_fetch_assoc($result_second)){ 
           
              $user[$i]['blockid'] = $row_second['blockid'];
              $user[$i]['room_name'] = $row_second['room_name'];
              $user[$i]['seasonname'] = $row_second['paper_name'];
              $user[$i]['paper_date'] = $row_second['date'];
               $user[$i]['papers_shortcode'] = $row_second['papers_shortcode']; 
               $user[$i]['time'] = $row_second['time'];
                  $user[$i]['credits'] = $row_second['credits'];
                  $user[$i]['max_marks'] = $row_second['max_marks'];
                $get_time= $row_second['time'];
                   $user[$i]['block_shortcode'] = $row_second['block_shortcode'];
                   $user[$i]['exam_end_date_time'] = $row_second['block_shortcode'];


$time2 = strtotime($get_time);
$difference1 = round(abs($time2 - $time3) / 3600,2);

$difference2 = round(abs($time2 - $time3) / 60);

          }
          
          if($difference1 > '0.99')
          {
          
              $time_left = "$difference1 hrs";
              $user[$i]['min'] = 100;
          }
          else
          {
              
              $time_left = "$difference2 mins";
              $user[$i]['min'] = $difference2;
          }
       
             $user[$i]['exam_type'] = $row['seasonname'];
             $user[$i]['exam_start_date_time'] = $time_left;
     
}

   else{
       $user[$i]['exam_type'] = "ONGOING";
        
      }



      }
      else if($end_date_change < $current_date)
      {

          if($exam_end_data == 1){
              
              foreach($user1 as $repeat) 
              {
                  $sql_third = "SELECT set_papers.credits,question_answer_submit.marks FROM set_papers INNER JOIN question_answer_submit WHERE set_papers.papers_id = '$repeat' AND question_answer_submit.course_id='$course_data' AND question_answer_submit.season_id = '$season_data' AND question_answer_submit.exam_id = '$exam_data' AND question_answer_submit.paper = '$repeat' ";
                  $result_third = mysqli_query($con,$sql_third);
                  
                  while($row_third = mysqli_fetch_assoc($result_third))
                  {
                      $credits =$row_third['credits'];
                      $marks =json_decode($row_third['marks']);
                      
                       $paper_marks = array_sum($marks);
                       
                       //weighted graded points
                     $gradedpoints = $credits * $paper_marks;
                  }
                  
                   $creditss +=  $credits;
                   $total_grade_point += $gradedpoints; 
                  }
      
                 $gpa_calc = $total_grade_point / $creditss;
              $cgp = round($gpa_calc,3);

           $user[$i]['papers_count'] = $creditss;   
           $user[$i]['exam_type'] = "COMPLETED";
           $user[$i]['exam_end_date_time'] = "NIL";
           $user[$i]['exam_start_date_time'] = $cgp;
          }
     else{
            $user[$i]['exam_type'] = "COMPLETED";
          $user[$i]['result'] = "AHH! EXAMS OVER";
     }
      }
    else{

        
      }
            


$i++;
    }
}

    echo json_encode($user); 
    


?>