<?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 = "";

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'];
}

$query = "SELECT annee_acad, idFiliere, pourcentageCredit, resultat, percentS1, resultatS1, percentS2, resultatS2 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'];	

$percent     = "";
$resultat    = "";
$res = 'A';
$reqNBUE = "";
$sql0 = "";

$nombreUE = 0;
$m0= 0;

if($idSemestre== 0) {
	$percent     = $tab['pourcentageCredit'];
	$resultat    = $tab['resultat'];	
	$reqNBUE= "SELECT  code_unite FROM unites WHERE annee = :annee AND niveauFormation = :niveau AND idFiliere = :idfiliere AND annee_acad = :acad";
	$sql0 = "select code_unite from tabinscriunite where idFiliere = :idfiliere AND annee = :annee and matricule = :matricule and resultat = :res"; 
	
	$statement = $bdd->prepare($reqNBUE);
	$statement->bindParam(':annee', $annee, PDO::PARAM_STR);
	$statement->bindParam(':niveau', $niveau, PDO::PARAM_STR);
	$statement->bindParam(':idfiliere', $idFiliere, PDO::PARAM_STR);
	$statement->bindParam(':acad', $annee_acad, PDO::PARAM_STR);
	$statement->execute();
	$nombreUE = $statement->rowCount();
	
	$statement = $bdd->prepare($sql0);
	$statement->bindParam(':idfiliere', $idFiliere, PDO::PARAM_STR);
	$statement->bindParam(':annee', $annee, PDO::PARAM_STR);
	$statement->bindParam(':matricule', $matricule, PDO::PARAM_STR);
	$statement->bindParam(':res', $res, PDO::PARAM_STR);
	$statement->execute();
	$m0 = $statement->rowCount();
}else{
	if($idSemestre % 2 == 0) {//semestre pair
		$percent   = $tab['percentS2'];	
		$resultat  = $tab['resultatS2'];
	}else if($idSemestre % 2 == 1){//semestre impair
		$percent   = $tab['percentS1'];
		$resultat  = $tab['resultatS1'];	
	}
	$reqNBUE= "SELECT  code_unite FROM unites WHERE semestre = :idSemestre AND annee = :annee AND niveauFormation = :niveau AND idFiliere = :idfiliere AND annee_acad = :acad";
	$sql0 = "select code_unite from tabinscriunite where idFiliere = :idfiliere AND annee = :annee and matricule = :matricule and resultat = :res and code_unite in (SELECT  code_unite FROM unites WHERE semestre = :idSemestre AND annee = :annee AND idFiliere = :idfiliere AND annee_acad = :acad)"; 

	$statement = $bdd->prepare($reqNBUE);
	$statement->bindParam(':idSemestre', $idSemestre, PDO::PARAM_STR);
	$statement->bindParam(':annee', $annee, PDO::PARAM_STR);
	$statement->bindParam(':niveau', $niveau, PDO::PARAM_STR);
	$statement->bindParam(':idfiliere', $idFiliere, PDO::PARAM_STR);
	$statement->bindParam(':acad', $annee_acad, PDO::PARAM_STR);
	$statement->execute();
	$nombreUE = $statement->rowCount();
	
	$statement = $bdd->prepare($sql0);
	$statement->bindParam(':idfiliere', $idFiliere, PDO::PARAM_STR);
	$statement->bindParam(':annee', $annee, PDO::PARAM_STR);
	$statement->bindParam(':matricule', $matricule, PDO::PARAM_STR);
	$statement->bindParam(':res', $res, PDO::PARAM_STR);
	$statement->bindParam(':idSemestre', $idSemestre, PDO::PARAM_STR);
	$statement->bindParam(':annee', $annee, PDO::PARAM_STR);
	$statement->bindParam(':idfiliere', $idFiliere, PDO::PARAM_STR);
	$statement->bindParam(':acad', $annee_acad, PDO::PARAM_STR);
	$statement->execute();
	$m0 = $statement->rowCount();
}

$arr1 = array('percent' => $percent, 'resultat' => $resultat, 'ueValide' => $m0, 'ueTotal' => $nombreUE , 'idSemestre' => $idSemestre);

$jsonResultat1  = array();
$jsonResultat1['unites'][]=$arr1;

$json   = array();

$json['ue'][]   = $jsonResultat1;
echo json_encode($json); 
?>
