<?php
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');
$dept_id = $request->getAttribute('dept_id');
$level_id = $request->getAttribute('level_id');
$year = $request->getAttribute('year');
$term = $request->getAttribute('term');
$type = $request->getAttribute('type');
$papers_shortcode = $request->getAttribute('papers_shortcode');


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

$response=array();




if(isset($course_id)&&isset($dept_id)&&isset($level_id)&&isset($year)&&isset($term)&&isset($type)&&isset($papers_shortcode)){
	$sql="SELECT * FROM set_papers WHERE course_id='$course_id' AND dept_id='$dept_id' AND level_id='$level_id' AND year='$year'AND term='$term'AND type='$type'AND papers_shortcode='$papers_shortcode'";
}
if(isset($course_id)&&isset($dept_id)&&isset($level_id)&&isset($year)&&isset($term)&&isset($type)){
	$sql="SELECT * FROM set_papers WHERE course_id='$course_id' AND dept_id='$dept_id' AND level_id='$level_id' AND year='$year'AND term='$term'AND type='$type'";
}
elseif(isset($course_id)&&isset($dept_id)&&isset($level_id)&&isset($year)&&isset($term)){
		$sql="SELECT * FROM set_papers WHERE course_id='$course_id' AND dept_id='$dept_id' AND level_id='$level_id' AND year='$year'AND term='$term'";
}
elseif(isset($course_id)&&isset($dept_id)&&isset($level_id)&&isset($year)){
		$sql="SELECT * FROM set_papers WHERE course_id='$course_id' AND dept_id='$dept_id' AND level_id='$level_id' AND year='$year'";
}
elseif(isset($course_id)&&isset($dept_id)&&isset($level_id)){
		$sql="SELECT * FROM assign_papers WHERE course_id='$course_id' AND season_id='$dept_id' AND exams_id='$level_id'";
}
elseif(isset($course_id)&&isset($dept_id)){
		$sql="SELECT * FROM assign_papers WHERE course_id='$course_id'AND season_id='$dept_id'";
}
elseif(isset($course_id)){
		$sql="SELECT * FROM assign_papers WHERE course_id='$course_id'";
}
else{
		$sql="SELECT * FROM assign_papers";
}




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

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

      $user=$row;

$user_paper = json_decode($row[$i]["papers_id"]);

$i++;
    }


foreach($user_paper as $user_data)
{
   $select_get_papers = "SELECT * FROM set_papers WHERE papers_id = '$user_data'";
   
   $com_sql_que = mysqli_query($con,$select_get_papers);
   
   while($row_get = mysqli_fetch_assoc($com_sql_que))
   {
       $papers_data[] = $row_get;
   }
    
}


 echo json_encode($papers_data);




?>