<?php
$DB_DATABASE = "";
$prefix = "iitech_";
if(isset($_POST['database'])){
	$DB_DATABASE = $prefix.$_POST['database'];
}

// include db connect class
require_once __DIR__ . '/include/new_db_connect.php';

// connecting to db
$bdd = connect($DB_DATABASE);

require_once 'include/fonctions_utiles.php';

$matricule = "";
$niveau = "";
$annee = "";
$idSemestre = "";
$code_ue = "";

if(isset($_POST['matricule'])){
	$matricule = $_POST['matricule'];
}
if(isset($_POST['niveau'])){
    $niveau = $_POST['niveau'];
}
if(isset($_POST['annee'])){
    $annee = $_POST['annee'];
}
if(isset($_POST['idSemestre'])){
    $idSemestre = $_POST['idSemestre'];
}
if(isset($_POST['code_ue'])){
    $code_ue = $_POST['code_ue'];
}

$query = "SELECT annee_acad, idFiliere FROM  tabinscription WHERE matricule = :matricule AND annee = :annee AND niveauFormation = :niveau";
$statement = $bdd->prepare($query);
$statement->bindParam(':matricule', $matricule, PDO::PARAM_STR);
$statement->bindParam(':annee', $annee, PDO::PARAM_STR);
$statement->bindParam(':niveau', $niveau, PDO::PARAM_STR);
$statement->execute();
$tab=$statement->fetch(PDO::FETCH_ASSOC);

$annee_acad = $tab['annee_acad'];	 
$idFiliere 	= $tab['idFiliere'];	

$json1  = array();
$json2 = array();
$json  = array();
$jsonecue  = array();

$query = "SELECT code_unite, intituleUnite, credit from unites WHERE idFiliere = :id and annee_acad = :acad AND annee = :annee and semestre = :sem order by intituleUnite ASC";
$statement = $bdd->prepare($query);
$statement->bindParam(':id', $idFiliere, PDO::PARAM_STR);
$statement->bindParam(':acad', $annee_acad, PDO::PARAM_STR);
$statement->bindParam(':annee', $annee, PDO::PARAM_STR);
$statement->bindParam(':sem', $idSemestre, PDO::PARAM_STR);
$statement->execute();

while($row=$statement->fetch(PDO::FETCH_ASSOC)){
	$json1['unites'][]=$row;
}
$json['ue'][]   = $json1;
echo json_encode($json); 	
?>
