<?php
	// On appelle la session
	session_start();
	include_once('Library/Basededonnees.php');
	
	$DB_DATABASE = $_SESSION["DB_DATABASE"];

	$database = new Basededonnees($DB_DATABASE);
	$bdd = $database->getBdd();
	
	include('/home/africawebstudent/public_html/gs/progicielgeststudent/getAdresseip.php');
	//include('../config.php');
	include('notifierErreur2.php');
	$matricule = $_SESSION["etudiant_i"];
	$idEtablissement = $_SESSION["idEtablissement"];
	$ip = get_ip();
	$d  = getdate();
	$y  = $d["year"]; 
	$mo = $d["mon"]; 
	$wd = $d["mday"];
	   
	$h = $d["hours"]; 
	$m = $d["minutes"];
	$s = $d["seconds"];

	date_default_timezone_set('Africa/Porto-Novo'); 
	// Récupère l'heure
	$localtime =  localtime();
	$seconde   =  $localtime[0];
	$minute    =  $localtime[1];
	$heure     =  $localtime[2];
	
	$date = "$y-$mo-$wd $heure:$minute:$seconde";
	
	$operation = "Déconnexion du panneau de contrôle d'administration principale";
	
	try{
		$sql  = 'INSERT INTO `historique` (`matricule`, `adresseip`, `dateActivite`, `operation`, `idEtablissement`) ';
		$sql .= 'VALUES (:matricule, :adresseip, :dateActivite, :operation, :idEtablissement)';
		$statement = $bdd->prepare($sql);
		$statement->bindParam(':matricule', 	$matricule, 	PDO::PARAM_STR);
		$statement->bindParam(':adresseip', 	$ip,			PDO::PARAM_STR);
		$statement->bindParam(':dateActivite',  $date, 			PDO::PARAM_STR);
		$statement->bindParam(':operation',  	$operation, 	PDO::PARAM_STR);
		$statement->bindParam(':idEtablissement',  	$idEtablissement, 	PDO::PARAM_STR);
		$statement->execute();
	}catch(PDOException  $e ){
		//echo $e->getMessage( );
		echo notifierErreur2($e->getCode( ), $e->getMessage( ));
		//die();
	}
	if(isset($_SESSION['token']) ) {
	    $redirectUrl = $_SESSION['redirectUrl'];
    	// On écrase le tableau de session
    	$_SESSION = array();
    	 
    	// On détruit la session
    	session_destroy();
	    header('Location: '.$redirectUrl);
	}else{
    	// On écrase le tableau de session
    	$_SESSION = array();
    	 
    	// On détruit la session
    	session_destroy();
    	 
    	// On redirige le visiteur vers la page d'accueil
    	header('Location: ../');	
	}
?>
