<?php
// include db connect class
require_once 'include/db_connect.php';
require_once 'include/fonctions_utiles.php';

// connecting to db
$db = new DB_CONNECT();

$matricule = "";

mysql_query("SET NAMES utf8");

if(isset($_POST['matricule'])){
	$matricule = $_POST['matricule'];
}

$sql1 = "select annee, idFiliere, annee_acad from tabetudiant where matricule = '$matricule' order by annee asc";
$result1 = mysql_query($sql1);
$json1  = array();
$json2  = array();
$json   = array();
$jsonResultat1  = array();
$jsonResultat2  = array();


$arr1 = array();
$arr2 = array();
$arr3 = array();
$arr4 = array();
$arr5 = array();

$arr = array();

$maSomme1 = 0;

while($tab1=mysql_fetch_assoc($result1)) {
	$annee = $tab1['annee'];
	$idfiliere = $tab1['idFiliere']; //
	$annee_acad = $tab1['annee_acad'];

	$reqvali1 = "SELECT *  FROM tabinscriunite WHERE matricule = '$matricule' ";
	$reqvali1 .= "AND annee <= '$annee' AND resultat = 'A'";
    $resvali1 = mysql_query($reqvali1);        
        
    while ($tabvali1 = mysql_fetch_array($resvali1, MYSQL_ASSOC)) {
        $code_unite1 = $tabvali1['code_unite'];
	    $pi1         = $tabvali1['premiere_inscription'];
	    $idF 	   = $tabvali1['idFiliere'];
        $maReq1 = "SELECT credit FROM unites WHERE code_unite = '$code_unite1' AND annee = '$annee' AND idFiliere = '$idfiliere' 
              AND  annee_acad = '$pi1'";
        $monRes1 = mysql_query($maReq1);
        $maTab1 = mysql_fetch_array($monRes1, MYSQL_ASSOC);
        $maSomme1 = $maSomme1 + $maTab1['credit'];
    }

	if($annee == 1) {
		  $ids1 = 1;
		  $ids2 = 2;
		  $modulo = 60;
	}else if($annee == 2) {
		  $ids1 = 3;
		  $ids2 = 4;
		  $modulo = 120;
	} else if($annee == 3){
		  $ids1 = 5;
		  $ids2 = 6;
		  $modulo = 180;
	}

	$requete  = "SELECT percent, resultat, percentS1, resultatS1, percentS2, resultatS2 FROM classement where matricule = '$matricule' and annee = '$annee' and idFiliere = '$idfiliere'";
	$resultat = mysql_query($requete);
	$tab      = mysql_fetch_assoc($resultat);

	$percent     = $tab['percent'];
	$resultat    = $tab['resultat'];

	$percentS1   = $tab['percentS1'];
	$resultatS1  = $tab['resultatS1'];

	$resultatS2  = $tab['resultatS2'];
	$percentS2   = $tab['percentS2'];

	$arr1['code_unite'] = $ids1;
	$arr1['intituleUnite'] = "Result semester $ids1";
	
	$arr1['code_unite'] = $ids2;
	$arr2['intituleUnite'] = "Result semester $ids2";
	
	$arr3['code_unite'] = "$ids1 $ids2";
	$arr3['intituleUnite'] = "Annual result : semester $ids1 & $ids2";
	
	$arr4['code_unite'] = "liste";
	$arr4['intituleUnite'] = "List of teaching units to validate : semester $ids1 & $ids2";
		
	$sql00 = "select max(inscription_actuelle) as ia from tabinscriunite where idFiliere = $idfiliere AND annee = '$annee' and matricule = '$matricule'";
	$result00 = mysql_query($sql00);
	$row00=mysql_fetch_assoc($result00);
	$ia = $row00['ia'];

	$sql0 = "select code_unite, resultat, inscription_actuelle from tabinscriunite where idFiliere = $idfiliere AND annee = '$annee' and matricule = '$matricule' and resultat = 'R'"; 
	 
	//$sql0 = "select code_unite, resultat from tabinscriunite where idFiliere = $idfiliere and inscription_actuelle = '$annee_acad' AND annee = '$annee' and matricule = '$matricule' and resultat = 'R'"; 
	 
	$result0 = mysql_query($sql0);
	$m = mysql_num_rows($result0);
	if($maSomme1%$modulo != 0) {
		$message = "In progress : Accumulated credit : $maSomme1";
	}else{
		$message = "Completed : Accumulated credit : $maSomme1";
	}
	$periode = $annee_acad - 1;
	$arr5['code_unite'] = "periode";
	$arr5['intituleUnite'] = "-=-=-Year $annee : $periode-$ia : $message-=-=-";
		
	/*$arr1 = array('code_unite' => $ids1, 'intituleUnite' => "Result semester $ids1");
	$arr2 = array('code_unite' => $ids2, 'intituleUnite' => "Result semester $ids2");
	$arr3 = array('code_unite' => "$ids1 $ids2", 'intituleUnite' => "Annual result $annee_acad");
	$arr4 = array('code_unite' => "liste", 'intituleUnite' => "List of teaching units to validate $annee_acad");*/
		
	
	$reqvali2 = "SELECT *  FROM tabinscriunite WHERE matricule = '$matricule' ";
	$reqvali2 .= "AND annee = '$annee' AND idFiliere = '$idfiliere' AND resultat = 'A'";
    $resvali2 = mysql_query($reqvali2);
        
        $maSomme2 = 0;

        while ($tabvali2 = mysql_fetch_array($resvali2, MYSQL_ASSOC)) {
              $code_unite2 = $tabvali2['code_unite'];
	      $pi2         = $tabvali2['premiere_inscription'];
	      $idF 	   = $tabvali2['idFiliere'];
              $maReq2 = "SELECT credit FROM unites WHERE code_unite = '$code_unite2' AND annee = '$annee' AND idFiliere = '$idF' 
              AND  annee_acad = '$pi2'";
              $monRes2 = mysql_query($maReq2);
              $maTab2 = mysql_fetch_array($monRes2, MYSQL_ASSOC);
              $maSomme2 = $maSomme2 + $maTab2['credit'];
        }

	$jsonResultat1['unites'][]=$arr5;
	$arrs1 = array('intituleEcu' => 'Validated credit', 'note' => "$maSomme2");	
	$jsonResultat2["-=-=-Year $annee : $periode-$ia : $message-=-=-"][]=$arrs1;

	$jsonResultat1['unites'][]=$arr1;
	$arrs1 = array('intituleEcu' => "Percent semester $ids1", 'note' => $percentS1);	
	$arrs2 = array('intituleEcu' => "Result semester $ids1", 'note' => $resultatS1);	
	$jsonResultat2["Result semester $ids1"][]=$arrs1;
	$jsonResultat2["Result semester $ids1"][]=$arrs2;

	$jsonResultat1['unites'][]=$arr2;
	$arrs1 = array('intituleEcu' => "Percent semester $ids2", 'note' => $percentS2);	
	$arrs2 = array('intituleEcu' => "Result semester $ids2", 'note' => $resultatS2);	
	$jsonResultat2["Result semester $ids2"][]=$arrs1;
	$jsonResultat2["Result semester $ids2"][]=$arrs2;

	$jsonResultat1['unites'][]=$arr3;
	$arrs1 = array('intituleEcu' => "Percent semester $ids1 & $ids2", 'note' => $percent);	
	$arrs2 = array('intituleEcu' => "Result semester $ids1 & $ids2",  'note' => $resultat);	
	$jsonResultat2["Annual result : semester $ids1 & $ids2"][]=$arrs1;
	$jsonResultat2["Annual result : semester $ids1 & $ids2"][]=$arrs2;
	 
	$string = ". See below the complete list.";
	$jsonResultat1['unites'][]=$arr4;
	$arrs1 = array('intituleEcu' => "The total number", 'note' => $m. $string);	
	$jsonResultat2["List of teaching units to validate : semester $ids1 & $ids2"][]=$arrs1;

	while($row0=mysql_fetch_assoc($result0)){
		$code_unite = $row0['code_unite'];
		
		$sql = "select intituleUnite, type_ue, moy_pondere, credit from unites where idFiliere = $idfiliere and annee_acad = '$annee_acad' AND annee = '$annee' and code_unite = '$code_unite'"; 
		 
		$result = mysql_query($sql);
		$row    = mysql_fetch_assoc($result);
		$unites = $row['intituleUnite'];
		$moy_pondere = $row['moy_pondere'];
		$credit = $row['credit'];

		//$arr = array('code_unite' => $code_unite, 'intituleUnite' => "$unites : $credit cr");
		$arr['code_unite']=$code_unite;
		$arr['intituleUnite']="$unites";
		$arr['credit']="$credit";
		$jsonResultat1['unites'][]	= $arr;	
		$sql1 = "select intituleEcu, code_ecu, credit from ecu where idFiliere = $idfiliere and annee_acad = '$annee_acad' AND annee = '$annee' and code_unite = '$code_unite'  order by  intituleEcu 		ASC"; 		 
		$result2 = mysql_query($sql1);
		$m = mysql_num_rows($result2);
		$moyUE = 0;
		$moyUEPonderee = 0;
		while($row2=mysql_fetch_assoc($result2)){
			$intituleEcu = $row2['intituleEcu'];
			$code_ecu = $row2['code_ecu'];
			$poids    = $row2['credit'];
			$sql3 = "select noteEcu from  notes_ecu where idFiliere = $idfiliere and annee_acad = '$annee_acad' AND annee = '$annee' and matricule = '$matricule' and code_ecu = '$code_ecu'"; 	
			$result3 = mysql_query($sql3);	
			$row3    = mysql_fetch_assoc($result3);
			$note3    = $row3['noteEcu'];
			
			$moyUE = $moyUE + $note3;	
			$moyUEPonderee = $moyUEPonderee + $note3*$poids;
								
			$arr = array('intituleEcu' => $intituleEcu, 'note' => $note3);	
			$jsonResultat2[$unites][]=$arr;
		}
		if($moy_pondere == 'YES') {
			$moyUE = myround($moyUEPonderee, 2);
		}else{
			$moyUE = myround($moyUE / $m, 2);
		}                                           
		$arr = array('intituleEcu' => "Moyenne de l'UE", 'note' => $moyUE);	
		$jsonResultat2[$unites][]=$arr;
	}
}
$json['ue'][]   = $jsonResultat1;
$json['ecue'][] = $jsonResultat2;
echo json_encode($json); 
?>