<?php



$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');
$paper_id = $request->getAttribute('paper');
$date = $request->getAttribute('date');
// $set_grade_id=$request->getMethod('set_grade_id');                       
 // $dept_id=$request->getParsedBody()['dept_id'];
 // $level_id=$request->getParsedBody()['level_id'];
 //  $type=$request->getParsedBody()['type'];
// $start_module='2';

// Check connection

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

$response=array();
if(isset($course_id)&&isset($season_id)&&isset($exams_id)&&isset($date)){
$sql="SELECT schedule_exam.id,schedule_exam.course_id,schedule_exam.exams_id,schedule_exam.season_id,schedule_exam.paper_id,schedule_exam.slots,schedule_exam.date,schedule_exam.time,schedule_exam.duration,course.course_name,Exams.examname,season.seasonname,set_papers.papers_shortcode,set_papers.paper_name FROM schedule_exam INNER JOIN course ON course.course_id = schedule_exam.course_id INNER JOIN Exams
ON Exams.id = schedule_exam.exams_id INNER JOIN season ON season.id=schedule_exam.season_id INNER JOIN set_papers ON set_papers.papers_id=schedule_exam.paper_id WHERE schedule_exam.course_id = '$course_id' AND schedule_exam.season_id = '$season_id' AND schedule_exam.exams_id='$exams_id'  AND schedule_exam.date='$date'";
}
/*
elseif(isset($course_id)&&isset($season_id)&&isset($exams_id)&&isset($paper_id)){
$sql="SELECT schedule_exam.id,schedule_exam.course_id,schedule_exam.exams_id,schedule_exam.season_id,schedule_exam.paper,schedule_exam.slots,schedule_exam.date,schedule_exam.time,schedule_exam.duration,course.course_name,Exams.examname,season.seasonname,set_papers.papers_shortcode,set_papers.paper_name FROM schedule_exam INNER JOIN course ON course.course_id = schedule_exam.course_id INNER JOIN Exams
ON Exams.id = schedule_exam.exams_id INNER JOIN season ON season.id=schedule_exam.season_id INNER JOIN set_papers ON set_papers.papers_id=schedule_exam.paper WHERE schedule_exam.course_id = '$course_id' AND schedule_exam.season_id = '$season_id' AND schedule_exam.exams_id='$exams_id' AND schedule_exam.paper='$paper_id'";
} 
*/
elseif(isset($course_id)&&isset($season_id)&&isset($exams_id)){
$sql="SELECT schedule_exam.id,schedule_exam.course_id,schedule_exam.exams_id,schedule_exam.season_id,schedule_exam.paper_id,schedule_exam.slots,schedule_exam.date,schedule_exam.time,schedule_exam.duration,course.course_name,Exams.examname,season.seasonname,set_papers.papers_shortcode,set_papers.paper_name FROM schedule_exam INNER JOIN course ON course.course_id = schedule_exam.course_id INNER JOIN Exams
ON Exams.id = schedule_exam.exams_id INNER JOIN season ON season.id=schedule_exam.season_id INNER JOIN set_papers ON set_papers.papers_id=schedule_exam.paper_id WHERE schedule_exam.course_id = '$course_id' AND schedule_exam.season_id = '$season_id' AND schedule_exam.exams_id='$exams_id'";
} 

elseif(isset($course_id)&&isset($season_id)){
 $sql="SELECT schedule_exam.id,schedule_exam.course_id,schedule_exam.exams_id,schedule_exam.season_id,schedule_exam.paper_id,schedule_exam.slots,schedule_exam.date,schedule_exam.time,schedule_exam.duration,course.course_name,Exams.examname,season.seasonname,set_papers.papers_shortcode,set_papers.paper_name FROM schedule_exam INNER JOIN course ON course.course_id = schedule_exam.course_id INNER JOIN Exams
ON Exams.id = schedule_exam.exams_id INNER JOIN season ON season.id=schedule_exam.season_id INNER JOIN set_papers ON set_papers.papers_id=schedule_exam.paper_id WHERE schedule_exam.course_id = '$course_id' AND schedule_exam.season_id = '$season_id'";
}

elseif(isset($course_id)){
 $sql="SELECT schedule_exam.id,schedule_exam.course_id,schedule_exam.exams_id,schedule_exam.season_id,schedule_exam.paper_id,schedule_exam.slots,schedule_exam.date,schedule_exam.time,schedule_exam.duration,course.course_name,Exams.examname,season.seasonname,set_papers.papers_shortcode,set_papers.paper_name FROM schedule_exam INNER JOIN course ON course.course_id = schedule_exam.course_id INNER JOIN Exams
ON Exams.id = schedule_exam.exams_id INNER JOIN season ON season.id=schedule_exam.season_id INNER JOIN set_papers ON set_papers.papers_id=schedule_exam.paper_id WHERE schedule_exam.course_id = '$course_id'";
}
else{
  $sql="SELECT schedule_exam.id,schedule_exam.course_id,schedule_exam.exams_id,schedule_exam.season_id,
  schedule_exam.paper_id,schedule_exam.slots,schedule_exam.date,schedule_exam.time,schedule_exam.duration,
  course.course_name,Exams.examname,season.seasonname,set_papers.papers_shortcode,set_papers.paper_name
  FROM schedule_exam INNER JOIN course ON course.course_id = schedule_exam.course_id INNER JOIN Exams
ON Exams.id = schedule_exam.exams_id INNER JOIN season ON season.id=schedule_exam.season_id
 INNER JOIN set_papers ON set_papers.papers_id=schedule_exam.paper_id";
}


// $sql="SELECT  set_grade.set_grade_id,set_grade.course_id,set_grade.dept_id,set_grade.level_id,set_grade.range_in,set_grade.grade,department.dept_shortcode,level.level_shortcode  FROM set_grade INNER JOIN department ON department.dept_id = set_grade.dept_id INNER JOIN level ON level.level_id = set_grade.level_id WHERE course_id = '$course_id'AND dept_id = '$dept_id'";


$result=mysqli_query($con,$sql);

    while ($row[]=mysqli_fetch_assoc($result)) {  

      $user=$row;

//      $array1 = array("5","26","38","42");

// $array2 = array("15","36","38","42");

//      $user = array_intersect($array1, $array2);



    }
// foreach ($user as &$value) {
//     $intersect[] = $value;

//        $providers = array_intersect($intersect[$value]);

// }
      // echo json_encode($course_id);
      // echo json_encode($dept_id);


echo json_encode($user);





?>