<?php
$nivs_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_statsniv'"));
if($nivs_info['valeur']=="COL") { $statsnivs = "COL"; }
else if($nivs_info['valeur']=="LYC") { $statsnivs = "LYC"; }
else if($nivs_info['valeur']=="LYA") { $statsnivs = "LYA"; }
else if($nivs_info['valeur']=="CIT") { $statsnivs = "CIT"; }
else if($nivs_info['valeur']=="CIA") { $statsnivs = "CIA"; }
else if($nivs_info['valeur']=="PER") { $statsnivs = "PER"; }
			
$nivcolor = mysqli_query($bdd,"SELECT id, niveau, bgcolor FROM ".$prefix."_edtnivcolor ORDER BY id");
$tabDeballniveau = array('all', '6', '5', '4', '3', '2', '1', 't', 'a');
$tabDebniveau = array('6', '5', '4', '3', '2', '1', 't', 'a');
$taballNiveau = array('Tout', '6ème', '5ème', '4ème', '3ème', '2nde', '1re', 'Tale', 'Autre');
$tabNiveau = array('6ème', '5ème', '4ème', '3ème', '2nde', '1re', 'Tale', 'Autre');
$tabBgcolor = array();
$tabBgallcolor = array();
$tabDebgenre = array('F', 'M');
$tabGenre = array('Filles', 'Garçons');
$tabGenreColor = array('green', 'red');
$tabYearColor = array('#FF0000','#FF4000','#FF8000','#FFBF00','#FFFF00');
array_push($tabBgallcolor, 'grey');
while($champsniv = mysqli_fetch_assoc($nivcolor)) {
	array_push($tabBgcolor, $champsniv['bgcolor']);
	array_push($tabBgallcolor, $champsniv['bgcolor']);
}
//recréation des tableaux selon les niveaux de l'établissement (pas de changement pour CIA)
if($statsnivs=="COL") { 
	for($iu=4;$iu<25;$iu++) {
		unset($tabBgcolor[$iu]);
		unset($tabNiveau[$iu]); 
		unset($tabDebniveau[$iu]);
		unset($taballNiveau[($iu+1)]); 
		unset($tabDeballniveau[($iu+1)]);  
	}
}
else if($statsnivs=="LYC") { 
	for($iu=0;$iu<4;$iu++) {
		unset($tabBgcolor[$iu]);
		unset($tabNiveau[$iu]);
		unset($tabDebniveau[$iu]); 
		unset($taballNiveau[($iu+1)]); 
		unset($tabDeballniveau[($iu+1)]); 
	}
	for($iu=7;$iu<25;$iu++) {
		unset($tabBgcolor[$iu]);
		unset($tabNiveau[$iu]);
		unset($tabDebniveau[$iu]); 
		unset($taballNiveau[($iu+1)]); 
		unset($tabDeballniveau[($iu+1)]); 
	}
	unset($tabBgcolor[8]);
	unset($tabNiveau[8]);
	unset($tabDebniveau[8]); 
	unset($taballNiveau[9]);
	unset($tabDeballniveau[9]); 
	$tabBgcolor = array_values($tabBgcolor);
	$tabNiveau = array_values($tabNiveau);
	$tabDebniveau = array_values($tabDebniveau);
	$taballNiveau = array_values($taballNiveau);
	$tabDeballniveau = array_values($tabDeballniveau);
}
else if($statsnivs=="LYA") { 
	for($iu=0;$iu<4;$iu++) {
		unset($tabBgcolor[$iu]);
		unset($tabNiveau[$iu]);
		unset($tabDebniveau[$iu]); 
		unset($taballNiveau[($iu+1)]); 
		unset($tabDeballniveau[($iu+1)]);  
	}
	for($iu=8;$iu<25;$iu++) {
		unset($tabBgcolor[$iu]);
		unset($tabNiveau[$iu]);
		unset($tabDebniveau[$iu]); 
		unset($taballNiveau[($iu+1)]); 
		unset($tabDeballniveau[($iu+1)]); 
	}
	$tabBgcolor = array_values($tabBgcolor);
	$tabNiveau = array_values($tabNiveau);
	$tabDebniveau = array_values($tabDebniveau);
	$taballNiveau = array_values($taballNiveau);
	$tabDeballniveau = array_values($tabDeballniveau);
}
else if($statsnivs=="CIT") { 
	unset($tabBgcolor[8]);
	unset($tabNiveau[7]);
	unset($tabDebniveau[7]); 
	unset($taballNiveau[8]);
	unset($tabDeballniveau[8]); 
	for($iu=9;$iu<25;$iu++) {
		unset($tabBgcolor[$iu]);
		unset($tabNiveau[$iu]);
		unset($tabDebniveau[$iu]); 
		unset($taballNiveau[($iu+1)]); 
		unset($tabDeballniveau[($iu+1)]); 
	}
}
else if($statsnivs=="CIA") { 
	for($iu=8;$iu<25;$iu++) {
		unset($tabBgcolor[$iu]);
		unset($tabNiveau[$iu]);
		unset($tabDebniveau[$iu]); 
		unset($taballNiveau[($iu+1)]); 
		unset($tabDeballniveau[($iu+1)]); 
	}
}
else if($statsnivs=="PER") {
	$tabBgcolor = array();
	$tabNiveau = array(); 
	$tabDebniveau = array(); 
	$taballNiveau = array();
	$tabDeballniveau = array();
	$carac_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_statsniv_persoc'"));
	$nom_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_statsniv_person'"));
	$caracs = explode(",", $carac_info['valeur']);
	array_push($tabDeballniveau, 'all');
	$nb_niv=0;
	foreach($caracs as $checked_caracs) {
		array_push($tabDeballniveau, $checked_caracs);
		array_push($tabDebniveau, $checked_caracs);
		$nb_niv+=1;
	}
	$noms = explode(",", $nom_info['valeur']);
	array_push($taballNiveau, 'Tout');
	foreach($noms as $checked_noms) {
		array_push($taballNiveau, $checked_noms);
		array_push($tabNiveau, $checked_noms);
	}
	for($iu=0;$iu<$nb_niv;$iu++) {
		$color_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$tabDebniveau[$iu]."'"));  
		array_push($tabBgcolor, @$color_info['bgcolor']);
	}
}

// gestion des divisions :
$tabDivisions = array();
$tabDivColors = array();
$liste_division = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves WHERE categ = '1' GROUP BY division ORDER BY division ASC");
if($statsnivs!="PER") {
	while($listediv = mysqli_fetch_assoc($liste_division)) {
		array_push($tabDivisions, $listediv['division']);
		$colorniv = substr($listediv['division'],0,1);
		$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
		$color_info = mysqli_fetch_assoc($color);
		@array_push($tabDivColors, $color_info['bgcolor']);
	}
}
else {
	while($listediv = mysqli_fetch_assoc($liste_division)) {
		array_push($tabDivisions, $listediv['division']);
		$checkdiv = mysqli_query($bdd,"SELECT niveau FROM ".$prefix."_edtnivcolor");
		foreach($checkdiv as $checkdivv) {
			$taille = strlen($checkdivv['niveau']);
			if(substr($listediv['division'],0,$taille)==$checkdivv['niveau']) {
				$colorniv = substr($listediv['division'],0,$taille);
				$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
				$color_info = mysqli_fetch_assoc($color);
				array_push($tabDivColors, $color_info['bgcolor']);		
			}
		}		
	}
}
// même chose pour les statistiques évolution (classes) :
$tabDivisionsFreq = array();
$tabDivColorsFreq = array();
$liste_division = mysqli_query($bdd,"SELECT division FROM ".$prefix."_freq GROUP BY division ORDER BY division ASC");
if($statsnivs!="PER") {
	while($listediv = mysqli_fetch_assoc($liste_division)) {
		if($listediv['division']!="") {
			array_push($tabDivisionsFreq, $listediv['division']);
			$colorniv = substr($listediv['division'],0,1);
			$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
			$color_info = mysqli_fetch_assoc($color);
			@array_push($tabDivColorsFreq, $color_info['bgcolor']);
		}
	}
}
else {
	while($listediv = mysqli_fetch_assoc($liste_division)) {
		if($listediv['division']!="") {
			array_push($tabDivisionsFreq, $listediv['division']);
			$checkdiv = mysqli_query($bdd,"SELECT niveau FROM ".$prefix."_edtnivcolor");
			foreach($checkdiv as $checkdivv) {
				$taille = strlen($checkdivv['niveau']);
				if(substr($listediv['division'],0,$taille)==$checkdivv['niveau']) {
					$colorniv = substr($listediv['division'],0,$taille);
					$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
					$color_info = mysqli_fetch_assoc($color);
					array_push($tabDivColorsFreq, $color_info['bgcolor']);		
				}
			}
		}		
	}
}
// gestion des groupes :
$tabGroupes = array();
$tabGrColors = array();
$liste_groupe = mysqli_query($bdd,"SELECT groupe_nom FROM ".$prefix."_groupes GROUP BY groupe_nom ORDER BY groupe_nom ASC");
while($listegr = mysqli_fetch_assoc($liste_groupe)) {
	array_push($tabGroupes, $listegr['groupe_nom']);
	$colorniv = substr($listegr['groupe_nom'],0,1);
	$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
	$color_info = mysqli_fetch_assoc($color);
	if(isset($color_info['bgcolor'])) {
		array_push($tabGrColors, $color_info['bgcolor']);
	}
}
?>