<?php


$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');


// $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($dept_id)&&isset($level_id)&&isset($year)&&isset($term)){
		$sql="SELECT papers_id,papers_shortcode 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 papers_id,papers_shortcode 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 papers_id,papers_shortcode 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 papers_id,papers_shortcode FROM set_papers WHERE course_id='$course_id' AND dept_id='$dept_id' AND level_id='$level_id'";
}

elseif(isset($course_id)&&isset($dept_id)){
		$sql="SELECT papers_id,papers_shortcode FROM set_papers WHERE course_id='$course_id' AND dept_id='$dept_id'";
}

elseif(isset($course_id)){
		$sql="SELECT papers_id,papers_shortcode FROM set_papers WHERE course_id='$course_id'";
}
else{
		$sql="SELECT papers_id,papers_shortcode FROM set_papers";
}

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

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

      $user=$row;

    }

echo json_encode($user);
?>