<? include ("../settings.php"); ?>
<?

        $results = $db->select("tbl_abstract limit 1");
        $result = $results[0];
        $output = "";
        foreach (array_keys($result) as $key){
            if ($output=="")
                $output = "\"".$key."\"";
            else
                $output .= ",\"".$key."\"";
        }
    	$output .= ",\"Reviews\"\n";
	$file = $_SERVER['DOCUMENT_ROOT']."/ACFS2023/csv/abstractdata.csv";
	$fp = fopen($file, 'w');
	fwrite($fp, $output);
	$results = $db->select("tbl_abstract");
	foreach ($results as $result){

	        $output = "";
			foreach ($result as $record){
	            if ($output=="")
	                $output = "\"".$record."\"";
	            else
	                $output .= ",\"".$record."\"";
	        }
			$bind = array(
				":search1"=>$result['abstract_id']
			);
			$responses = $db->select("tbl_abstract_remarks as a, tbl_abstract_user as b","a.user_id = b.user_id AND a.abstract_id = :search1 ORDER BY a.user_id",$bind);
			foreach ($responses as $response){
			$output .= $response['user_login']."|";
				$output .= $response['score1']."|";
				$output .= $response['score2']."|";
				$output .= $response['score3']."|";
				$output .= $response['score4']."|";
				$output .= $response['score5']."|";
				$output .= $response['score6']."|";
				$output .= $response['recommendation']."|";
				$output .= $response['remarks']."||";
			}
	        $output .= "\n";
	        fwrite($fp, $output);
	}

	fclose($fp);

	// Set headers
	header("Cache-Control: public");
	header("Content-Description: File Transfer");
	header("Content-Disposition: attachment; filename=abstractdata.csv");
	header("Content-Type: application/zip");
	header("Content-Transfer-Encoding: binary");
	
	// Read the file from disk
	readfile($file);

?>