<?php

/*Student API*/

/*Fetching API*/

/* Attempt to connect to MySQL database */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'restax');

define('DB_PASSWORD', 'restax@123');

define('DB_NAME', 'Restax'); 

$con = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

$course_id = $request->getAttribute('course_id');
$paper_shortcode = $request->getAttribute('paper_shortcode');
$exam_id = $request->getAttribute('exam_id');

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

$response=array();

if(isset($course_id)){

//query for status pass
    $sql="SELECT * FROM pwe_answersheet_submit WHERE course_id='$course_id' AND exams_id='$exam_id' AND papers_shortcode='$paper_shortcode' AND state='pass'  ORDER BY id DESC";
$result=mysqli_query($con,$sql);

if ($result->num_rows > 0) {
	
while($row=mysqli_fetch_assoc($result)){
    $passcount=$row['state'];
}
}

//query for status fail
    $sql="SELECT * FROM pwe_answersheet_submit WHERE course_id='$course_id' AND exams_id='$exam_id' AND papers_shortcode='$paper_shortcode' AND state='fail'  ORDER BY id DESC";
$result=mysqli_query($con,$sql);

if ($result->num_rows > 0) {
	
while($row=mysqli_fetch_assoc($result)){
    $failcount=$row['state'];
}
}

//query for status present
    $sql="SELECT * FROM pwe_answersheet_submit WHERE course_id='$course_id' AND exams_id='$exam_id' AND papers_shortcode='$paper_shortcode' AND status='1'  ORDER BY id DESC";
$result=mysqli_query($con,$sql);

if ($result->num_rows > 0) {
	
while($row=mysqli_fetch_assoc($result)){
    $presentcount=$row['status'];
}
}

//query for status absent
    $sql="SELECT * FROM pwe_answersheet_submit WHERE course_id='$course_id' AND exams_id='$exam_id' AND papers_shortcode='$paper_shortcode' AND status='0'  ORDER BY id DESC";
$result=mysqli_query($con,$sql);

if ($result->num_rows > 0) {
	
while($row=mysqli_fetch_assoc($result)){
    $absentcount=$row['status'];
}
}

//query for status malpractice
    $sql="SELECT * FROM pwe_answersheet_submit WHERE course_id='$course_id' AND exams_id='$exam_id' AND papers_shortcode='$paper_shortcode' AND status='2'  ORDER BY id DESC";
$result=mysqli_query($con,$sql);

if ($result->num_rows > 0) {
	
while($row=mysqli_fetch_assoc($result)){
    $malprcaticecount=$row['status'];
}
}

else{
	$response=array('status' => "false",'Message' => "Grades count not found..!");
         echo json_encode(array($response));
}
}
?>