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

//mysql_query("SET NAMES utf8");

$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();
$json  = array();

$query = "SELECT code_unite, intituleUnite, credit, moy_pondere 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)){
	$moyUE = 0;
	$moyUEPonderee = 0;
	$unites = $row['intituleUnite'];
	$code_unite = $row['code_unite'];
	$credit = $row['credit'];
	$moy_pondere = $row['moy_pondere'];
		
	$query = "select credit, code_ecu from ecu WHERE idFiliere = :id and annee_acad = :acad AND annee = :annee and code_unite = :code order by intituleEcu ASC";
	$statement1 = $bdd->prepare($query);
	$statement1->bindParam(':id', $idFiliere, PDO::PARAM_STR);
	$statement1->bindParam(':acad', $annee_acad, PDO::PARAM_STR);
	$statement1->bindParam(':annee', $annee, PDO::PARAM_STR);
	$statement1->bindParam(':code', $code_unite, PDO::PARAM_STR);
	$statement1->execute();
	$m = $statement1->rowCount();
	
	while($row1=$statement1->fetch(PDO::FETCH_ASSOC)){
		$poids    = $row1['credit'];
		$code_ecu = $row1['code_ecu'];

		$sql2 = "select noteEcu from  noteEcu where idFiliere = :id and annee_acad = :acad AND annee = :annee and matricule = :matri and code_ecu = :code"; 	
		$statement2 = $bdd->prepare($sql2);
		$statement2->bindParam(':id', $idFiliere, PDO::PARAM_STR);
		$statement2->bindParam(':acad', $annee_acad, PDO::PARAM_STR);
		$statement2->bindParam(':annee', $annee, PDO::PARAM_STR);
		$statement2->bindParam(':matri', $matricule, PDO::PARAM_STR);
		$statement2->bindParam(':code', $code_ecu, PDO::PARAM_STR);
		$statement2->execute();		
		$row2=$statement2->fetch(PDO::FETCH_ASSOC);
		$note2    = $row2['noteEcu'];
		$moyUE = $moyUE + $note2;	
		$moyUEPonderee = $moyUEPonderee + $note2*$poids;
	}
	if($moy_pondere == 'YES') {
		$moyUE = myround($moyUEPonderee / $credit, 2);
	}else{
		$moyUE = myround($moyUE / $m, 2);
	}  
	$arr = array('code_unite' => $code_unite, 'intituleUnite' => $unites, 'credit' => $credit, 'note' => $moyUE);	
	$json1['unites'][]=$arr;		
}
$json['ue'][]   = $json1;
echo json_encode($json); 
?>
