<?php
session_start();
require_once(dirname(__DIR__, 2) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
if($_SESSION['user_droits'][5]>1) {
	if($_SESSION['format']=='csv') {
		header('Content-Type: text/csv; charset=utf-8');  
		header('Content-Disposition: attachment; filename=idl_extraction.csv');  
      	$output = fopen("php://output", "w");  
      	if($_SESSION['division']=='tout') {
			$eleves = mysqli_query($bdd,"SELECT ".$_SESSION['tabcsv']." FROM ".$prefix."_eleves ORDER BY nom ASC, prenom ASC");
		}
		else {
			$division = mysqli_real_escape_string($bdd,$_SESSION['division']);
			$eleves = mysqli_query($bdd,"SELECT ".$_SESSION['tabcsv']." FROM ".$prefix."_eleves WHERE division = '".$division."' ORDER BY nom ASC, prenom ASC");
		}  
      	foreach($eleves as $eleve)  
      	{  
           fputs($output, implode(';',$eleve)."\n");  
      	}  
      	fclose($output); 
	}
	else {
		header("Content-type: application/vnd.ms-excel charset=utf-8");
		header("Content-type: application/x-msexcel; charset=utf-8");
		header("Content-Disposition: attachment; filename=idl_extraction.".$_SESSION['format']);
		print $_SESSION['bctab'];
	}
}
mysqli_close($bdd);
?>