<?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)){

$sql="SELECT * FROM pwi_submit WHERE course_id='$course_id' AND exams_id='$exam_id' ORDER BY id DESC";
$result=mysqli_query($con,$sql);

if ($result->num_rows > 0) {
	
while($row=mysqli_fetch_assoc($result)){
    $exams_id=$row['exams_id'];
    $file_path=$row['file_path'];
    $registernumberlist=json_decode($row['roll_number'],true);
    $statuslist=json_decode($row['status'],true);
    $slotlist=json_decode($row['slot'],true);
    $example = json_decode($row['papers_shortcode'], true);
    $paper_no = json_decode($row['paper'], true);
    $filelist = json_decode($row['file'], true);
    
    $i=0;
   // echo json_encode($statuslist);
foreach( $registernumberlist as $name ) {
$list_paper[$i][] = $name;
$list_paper[$i][] = $example[$i];
$list_paper[$i][] = $paper_no[$i];
$list_paper[$i][] = $statuslist[$i];
$list_paper[$i][] = $filelist[$i];
$list_paper[$i][]=$file_path.'/'.$filelist[$i];
$array_dashboard=array('roll_number','papers_shortcode','paper','status','file','file_path');
$list_answer_data[] = array_combine($array_dashboard, $list_paper[$i]);
$i = $i+1; 
}
//echo json_encode($list_answer_data);

// PHP program to search for multiple 
// key=>value pairs in array 

function search($array, $search_list) { 

	// Create the result array 
	$result = array(); 

	// Iterate over each array element 
	foreach ($array as $key => $value) { 

		// Iterate over each search condition 
		foreach ($search_list as $k => $v) { 
	
			// If the array element does not meet 
			// the search condition then continue 
			// to the next element 
			if (!isset($value[$k]) || $value[$k] != $v) 
			{ 
				
				// Skip two loops 
				continue 2; 
			} 
		} 
	
		// Append array element's key to the 
		//result array 
		$result[] = $value; 
	} 

	// Return result 
	return $result; 
} 

// Define search list with multiple key=>value pair 
$search_items = array('papers_shortcode'=>$paper_shortcode); 

// Call search and pass the array and 
// the search list 
$res = search($list_answer_data, $search_items); 

// Print search result
$j=0;
foreach ($res as $var) { 
	// echo 'roll_number: ' . $var['roll_number'] . '<br>'; 
	// echo 'papers_shortcode: ' . $var['papers_shortcode'] . '<br>'; 
	// echo 'status: ' . $var['status'] . '<br>';	
	// echo 'file: ' . $var['file'] . '<br>';
$list_paper1[$j][] = $var['roll_number'];
$list_paper1[$j][] = $var['papers_shortcode'];
$list_paper1[$j][] = $var['paper'];
$list_paper1[$j][]= $var['status'];
$list_paper1[$j][]= $var['file'];
$list_paper1[$j][]=$var['file_path'];
$array_dashboard1=array('roll_number','papers_shortcode','paper','status','file','file_path');
$list_answer_data1[] = array_combine($array_dashboard, $list_paper1[$j]);	
$j=$j+1; 
}
echo json_encode($list_answer_data1);
}

}

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