<?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');
$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_id=$request->getAttribute('papers_id');



$response=array();
if(isset($course_id)&&isset($season_id)&&isset($exams_id)&&isset($dept_id)&&isset($level_id)&&isset($year)&&isset($term)&&isset($type)&&isset($papers_id)){
$sql="SELECT assign_papers.id, assign_papers.course_id, assign_papers.season_id, assign_papers.exams_id,
 assign_papers.dept_id, assign_papers.level_id, assign_papers.year, assign_papers.term, assign_papers.type, assign_papers.papers_id,
course.course_name, season.seasonname, Exams.examname, department.dept_shortcode, level.level_shortcode
 FROM (assign_papers INNER JOIN course ON course.course_id=assign_papers.course_id 
 INNER JOIN season ON season.id=assign_papers.season_id
 INNER JOIN Exams ON Exams.id=assign_papers.exams_id
 INNER JOIN department ON department.dept_id=assign_papers.dept_id
 INNER JOIN level ON level.level_id=assign_papers.level_id
 )
 WHERE assign_papers.course_id='$course_id' AND assign_papers.season_id='$season_id' AND assign_papers.exams_id='$exams_id'
 AND assign_papers.dept_id='$dept_id' AND assign_papers.level_id='$level_id'
 AND assign_papers.year='$year' AND assign_papers.term='$term' AND assign_papers.type='$type'
 AND assign_papers.papers_id='$papers_id'";
}

elseif(isset($course_id)&&isset($season_id)&&isset($exams_id)&&isset($dept_id)&&isset($level_id)&&isset($year)&&isset($term)&&isset($type)){
$sql="SELECT assign_papers.id, assign_papers.course_id, assign_papers.season_id, assign_papers.exams_id,
 assign_papers.dept_id, assign_papers.level_id, assign_papers.year, assign_papers.term, assign_papers.type, assign_papers.papers_id,
course.course_name, season.seasonname, Exams.examname, department.dept_shortcode, level.level_shortcode
 FROM (assign_papers INNER JOIN course ON course.course_id=assign_papers.course_id 
 INNER JOIN season ON season.id=assign_papers.season_id
 INNER JOIN Exams ON Exams.id=assign_papers.exams_id
 INNER JOIN department ON department.dept_id=assign_papers.dept_id
 INNER JOIN level ON level.level_id=assign_papers.level_id
 )
 WHERE assign_papers.course_id='$course_id' AND assign_papers.season_id='$season_id' AND assign_papers.exams_id='$exams_id'
 AND assign_papers.dept_id='$dept_id' AND assign_papers.level_id='$level_id'
 AND assign_papers.year='$year' AND assign_papers.term='$term' AND assign_papers.type='$type'";
}

elseif(isset($course_id)&&isset($season_id)&&isset($exams_id)&&isset($dept_id)&&isset($level_id)&&isset($year)&&isset($term)){
$sql="SELECT assign_papers.id, assign_papers.course_id, assign_papers.season_id, assign_papers.exams_id,
 assign_papers.dept_id, assign_papers.level_id, assign_papers.year, assign_papers.term, assign_papers.type, assign_papers.papers_id,
course.course_name, season.seasonname, Exams.examname, department.dept_shortcode, level.level_shortcode
 FROM (assign_papers INNER JOIN course ON course.course_id=assign_papers.course_id 
 INNER JOIN season ON season.id=assign_papers.season_id
 INNER JOIN Exams ON Exams.id=assign_papers.exams_id
 INNER JOIN department ON department.dept_id=assign_papers.dept_id
 INNER JOIN level ON level.level_id=assign_papers.level_id
 )
 WHERE assign_papers.course_id='$course_id' AND assign_papers.season_id='$season_id' AND assign_papers.exams_id='$exams_id'
 AND assign_papers.dept_id='$dept_id' AND assign_papers.level_id='$level_id'
 AND assign_papers.year='$year' AND assign_papers.term='$term'";
}

elseif(isset($course_id)&&isset($season_id)&&isset($exams_id)&&isset($dept_id)&&isset($level_id)&&isset($year)){
$sql="SELECT assign_papers.id, assign_papers.course_id, assign_papers.season_id, assign_papers.exams_id,
 assign_papers.dept_id, assign_papers.level_id, assign_papers.year, assign_papers.term, assign_papers.type, assign_papers.papers_id,
course.course_name, season.seasonname, Exams.examname, department.dept_shortcode, level.level_shortcode
 FROM (assign_papers INNER JOIN course ON course.course_id=assign_papers.course_id 
 INNER JOIN season ON season.id=assign_papers.season_id
 INNER JOIN Exams ON Exams.id=assign_papers.exams_id
 INNER JOIN department ON department.dept_id=assign_papers.dept_id
 INNER JOIN level ON level.level_id=assign_papers.level_id
 )
 WHERE assign_papers.course_id='$course_id' AND assign_papers.season_id='$season_id' AND assign_papers.exams_id='$exams_id'
 AND assign_papers.dept_id='$dept_id' AND assign_papers.level_id='$level_id'
 AND assign_papers.year='$year'";
}

elseif(isset($course_id)&&isset($season_id)&&isset($exams_id)&&isset($dept_id)&&isset($level_id)){
$sql="SELECT assign_papers.id, assign_papers.course_id, assign_papers.season_id, assign_papers.exams_id,
 assign_papers.dept_id, assign_papers.level_id, assign_papers.year, assign_papers.term, assign_papers.type, assign_papers.papers_id,
course.course_name, season.seasonname, Exams.examname, department.dept_shortcode, level.level_shortcode
 FROM (assign_papers INNER JOIN course ON course.course_id=assign_papers.course_id 
 INNER JOIN season ON season.id=assign_papers.season_id
 INNER JOIN Exams ON Exams.id=assign_papers.exams_id
 INNER JOIN department ON department.dept_id=assign_papers.dept_id
 INNER JOIN level ON level.level_id=assign_papers.level_id
 )
 WHERE assign_papers.course_id='$course_id' AND assign_papers.season_id='$season_id' AND assign_papers.exams_id='$exams_id'
 AND assign_papers.dept_id='$dept_id' AND assign_papers.level_id='$level_id'";
}
elseif(isset($course_id)&&isset($season_id)&&isset($exams_id)&&isset($dept_id)){
$sql="SELECT assign_papers.id, assign_papers.course_id, assign_papers.season_id, assign_papers.exams_id,
 assign_papers.dept_id, assign_papers.level_id, assign_papers.year, assign_papers.term, assign_papers.type, assign_papers.papers_id,
course.course_name, season.seasonname, Exams.examname, department.dept_shortcode, level.level_shortcode
 FROM (assign_papers INNER JOIN course ON course.course_id=assign_papers.course_id 
 INNER JOIN season ON season.id=assign_papers.season_id
 INNER JOIN Exams ON Exams.id=assign_papers.exams_id
 INNER JOIN department ON department.dept_id=assign_papers.dept_id
 INNER JOIN level ON level.level_id=assign_papers.level_id
)
 WHERE assign_papers.course_id='$course_id' AND assign_papers.season_id='$season_id' AND assign_papers.exams_id='$exams_id'
 AND assign_papers.dept_id='$dept_id'";
}
elseif(isset($course_id)&&isset($season_id)&&isset($exams_id)){
$sql="SELECT assign_papers.id, assign_papers.course_id, assign_papers.season_id, assign_papers.exams_id,
 assign_papers.dept_id, assign_papers.level_id, assign_papers.year, assign_papers.term, assign_papers.type, assign_papers.papers_id,
course.course_name, season.seasonname, Exams.examname, department.dept_shortcode, level.level_shortcode
 FROM (assign_papers INNER JOIN course ON course.course_id=assign_papers.course_id 
 INNER JOIN season ON season.id=assign_papers.season_id
 INNER JOIN Exams ON Exams.id=assign_papers.exams_id
 INNER JOIN department ON department.dept_id=assign_papers.dept_id
 INNER JOIN level ON level.level_id=assign_papers.level_id
 )
 WHERE assign_papers.course_id='$course_id' AND assign_papers.season_id='$season_id' AND assign_papers.exams_id='$exams_id'";
}
elseif(isset($course_id)&&isset($season_id)){
$sql="SELECT assign_papers.id, assign_papers.course_id, assign_papers.season_id, assign_papers.exams_id,
 assign_papers.dept_id, assign_papers.level_id, assign_papers.year, assign_papers.term, assign_papers.type, assign_papers.papers_id,
course.course_name, season.seasonname, Exams.examname, department.dept_shortcode, level.level_shortcode
 FROM (assign_papers INNER JOIN course ON course.course_id=assign_papers.course_id 
 INNER JOIN season ON season.id=assign_papers.season_id
 INNER JOIN Exams ON Exams.id=assign_papers.exams_id
 INNER JOIN department ON department.dept_id=assign_papers.dept_id
 INNER JOIN level ON level.level_id=assign_papers.level_id
 )
 WHERE assign_papers.course_id='$course_id' AND assign_papers.season_id='$season_id'";
}

elseif(isset($course_id)){
$sql="SELECT assign_papers.id, assign_papers.course_id, assign_papers.season_id, assign_papers.exams_id,
 assign_papers.dept_id, assign_papers.level_id, assign_papers.year, assign_papers.term, assign_papers.type, assign_papers.papers_id,
course.course_name, season.seasonname, Exams.examname, department.dept_shortcode, level.level_shortcode
 FROM (assign_papers INNER JOIN course ON course.course_id=assign_papers.course_id 
 INNER JOIN season ON season.id=assign_papers.season_id
 INNER JOIN Exams ON Exams.id=assign_papers.exams_id
 INNER JOIN department ON department.dept_id=assign_papers.dept_id
 INNER JOIN level ON level.level_id=assign_papers.level_id
)
 WHERE assign_papers.course_id='$course_id'";
}

else{
$sql="SELECT assign_papers.id, assign_papers.course_id, assign_papers.season_id, assign_papers.exams_id,
 assign_papers.dept_id, assign_papers.level_id, assign_papers.year, assign_papers.term, assign_papers.type, assign_papers.papers_id,
course.course_name, season.seasonname, Exams.examname, department.dept_shortcode, level.level_shortcode
 FROM (assign_papers INNER JOIN course ON course.course_id=assign_papers.course_id 
 INNER JOIN season ON season.id=assign_papers.season_id
 INNER JOIN Exams ON Exams.id=assign_papers.exams_id
 INNER JOIN department ON department.dept_id=assign_papers.dept_id
 INNER JOIN level ON level.level_id=assign_papers.level_id
 
 )";
}



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

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

      $user=$row;

	}


echo json_encode($user);

?>