<?php
// Utilisation de la bibliothèque Charts : documentation sur http://www.chartjs.org/docs/
// et exemples sur http://www.chartjs.org/samples/latest/
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	$droits = substr($_SESSION['user_droits'],3,1);
	
	if ($droits!="2")
	{
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="./index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		?>
		<style type="text/css">
		#mfr6 { background-color: #fff; }
		#mfr6 td { border: 3px #B90404 solid; }
		#mfr5 td { border:0; }
		</style>
		<script type="text/javascript">
			jQuery(function($){
				$('.div-loading').hide(); // On masque le gif au chargement de la page
				$('.ajax-load').click(function(e){
					// Affichage du gif de chargement et envoi requête AJAX
					$('.div-loading').show();
					$.get(
						'freq.php?page=stats',
						function(data){
							$('.div-loading').hide(); // On masque le gif 
						}
					);
				});
			});
		</script>
		<?php
		$academie = mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab WHERE academie <> '' OR pays <> ''");
		$etab_nb = mysqli_num_rows($academie);
		if($etab_nb!=0) {
			$acad_info = mysqli_fetch_assoc($academie);
			if($acad_info['pays']=='fra') { $acadexist = $acad_info['academie']; }
			else { $acadexist = $acad_info['pays']; }
		}
		else { $acadexist = ""; }
		//gestion du calendrier scolaire :
		include('./db/proc/caloffsco.php');
		//Pour la gestion des différents espaces :
		$espopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_gestion'"));
		$esp0 = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_first'"));
		$espaces = mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces ORDER BY espace_nom ASC");
		$nb_esp = mysqli_num_rows($espaces);

		//vérification et cas échéant suppression des stats nominatives sur n-1 et stats sur n-6 :
		$year6 = date("Y")-5;
		if(isset($_POST['nonnom'])) {
			if(isset($_POST['input_save'])) {
				$_SESSION['save_auto'] = '2';
				include('./db/backup.php');
			}
			if($acadexist=="fnc"||$acadexist=="civ") {
				mysqli_query($bdd,"DELETE FROM ".$prefix."_freq WHERE date_freq < '".$year6."-01-01'");
				mysqli_query($bdd,"DELETE FROM ".$prefix."_freqres WHERE date_freqres < '".$year6."-01-01'");
				mysqli_query($bdd,"DELETE FROM ".$prefix."_freqgrappes WHERE date_freq < '".$year6."-01-01'");
			}
			else {
				mysqli_query($bdd,"DELETE FROM ".$prefix."_freq WHERE date_freq < '".($year6-1)."-08-15'");
				mysqli_query($bdd,"DELETE FROM ".$prefix."_freqres WHERE date_freqres < '".($year6-1)."-08-15'");
				mysqli_query($bdd,"DELETE FROM ".$prefix."_freqgrappes WHERE date_freq < '".($year6-1)."-08-15'");
			}
			echo '<p>'.FREQC_STATS1.'</p>';
		}
		echo '<div class="div-loading"><img class="ajax-loading" src="img/div/loadingImage.gif" alt="Loading" /></div>';
		if($acadexist=="fnc"||$acadexist=="civ") {
			$freq6_clean = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freq WHERE date_freq < '".$year6."-01-01'");
			$freqgrappes6_clean = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqgrappes WHERE date_freq < '".$year6."-01-01'");
		}
		else {
			$freq6_clean = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freq WHERE date_freq < '".($year6-1)."-08-15'");
			$freqgrappes6_clean = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqgrappes WHERE date_freq < '".($year6-1)."-08-15'");
		}
		if((mysqli_num_rows($freq6_clean)>0||mysqli_num_rows($freqgrappes6_clean)>0)&&date('Y-m-d')>(substr($rentree,0,4).'-'.substr($rentree,5,2).'-'.substr($rentree,8,2))) {
			echo '<p class="signal maxwidth">'.EDTC_ELEV2.substr($rentree,8,2).'/'.substr($rentree,5,2).'/'.substr($rentree,0,4).EDTC_ELEV3.(mysqli_num_rows($freq6_clean)+mysqli_num_rows($freqgrappes6_clean)).EDTC_ELEV8.'</p><form action="" method="POST" name="nonnom"><input type="checkbox" name="input_save" id="input_save" checked /><label for="input_save"> '.DATAC_VIDE5.'</label><br><span class="ecart"><input class="bouton boutfreq" type="submit" value="'.CONFIRM.'" name="nonnom" /></span>';
			$result_tables = mysqli_query($bdd,"SHOW TABLE STATUS LIKE '".$prefix."_%'");
			$_SESSION['nb_tables'] = mysqli_num_rows($result_tables);
			if($result_tables && $result_tables->num_rows) {
				while($obj_table = mysqli_fetch_assoc($result_tables)) {
					echo '<input type="hidden" id="'.$obj_table['Name'].'" name="'.$obj_table['Name'].'" checked />';
				}
			}
			echo '<input type="hidden" name="hidden_save" id="input_save" checked />';
			echo '</form>';
		}
		echo '<script src="./js/chart.umd.js"></script>
		<script type="module" src="./js/chart-utils.umd.js"></script>
		<script src="./js/chartjs-plugin-datalabels.js"></script>';
		
		echo '<h1>'.FREQC_STATS2.'</h1>';
		include('db/proc/etab_config_check.php');
		// gestion des grappes et passages
		$grappes = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_grappes'"));
		$grappes_check = $grappes['valeur'];
		$passverif = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_passages'"));
		$passages_check = $passverif['valeur'];
		// gestion des exclusions avec durées et raisons
		$excluopt_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_excluopt'"));
		// menu stats générales
		echo '<ul class="menustats"><li class="itemstats" id="lifs1"><a href="freq.php?page=stats&stat=default">'.FSTATS_TIT8.'</a></li>';
		// menu élèves
		echo '<li class="itemstats" id="lifs2"><a id="ajax-load" href="freq.php?page=stats&stat=eleves">'.ELEVS2;
		if($excluopt_info['valeur']==1) { echo ' / '.FDIR_STATS_TIT28.'...'; }
		echo '</a></li>';
		echo '<li class="itemstats" id="lifs3"><a href="freq.php?page=stats&stat=proportions">'.FDIR_STATS_PROP12.'</a></li>';
		echo '<li class="itemstats" id="lifs4"><a href="freq.php?page=stats&stat=activites">'.ACTIV2.'</a></li>';
		// menu espaces (si activés)
		if($espopt['valeur']==1) {
			echo '<li class="itemstats" id="lifs5"><a href="freq.php?page=stats&stat=espaces">'.PARANAV8.'</a></li>';	
		}
		echo '<br>&nbsp; <br>';
		// menu passages (si activés)
		if($passages_check==1) {
			echo '<li class="itemstats" id="lifs6"><a href="freq.php?page=stats&stat=passages">'.PASS3.'</a></li>';	
		}
		echo '<li class="itemclassic"></li><li class="itemstats" id="lifs7"><a href="freq.php?page=stats&stat=periodes">'.PERIOD2.'</a></li>';
		// menu créneaux
		echo '<li class="itemclassic"></li><li class="itemstats" id="lifs8"><a href="freq.php?page=stats&stat=creneaux">'.JOURCREN.'</a></li>';
		// menu évolution si matière pour les stats
		$freqannees_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE freq.date_freq >= '".$rentreeprecedente."' AND freq.date_freq <= '".$finprecedente."'"));
		if($grappes_check==1) {
			$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$rentreeprecedente."' AND date_freq <= '".$finprecedente."' AND passage = '0'");
			while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
				$freqannees_nb += $elegrap['nombre'];
			}
		}
		if($freqannees_nb!=0) {
			echo '<li class="itemstats" id="lifs9"><a href="freq.php?page=stats&stat=annees">'.FSTATS_TIT31.'</a></li>';
		}
		// menu comptes
		$user_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT droits FROM ".$prefix."_user"));
		if($user_nb>1) {
			echo '<li class="itemstats" id="lifsa"><a href="freq.php?page=stats&stat=users">'.PARANAV2.'</a></li>';
		}
		echo '</ul>';

		// Si aucune statistique n'est possible (aucune inscription) :
		$verif_freq = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freq");
		$nb_freq = mysqli_num_rows($verif_freq);
		// vérification des niveaux :
		if(isset($_POST['niveaux_valid'])) {
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$_POST['niveaux']."' WHERE param_nom = 'freq_statsniv'");
		}
		$nivs = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_statsniv'");
		$nivs_info = mysqli_fetch_assoc($nivs);
		
		if($nb_freq==0) {
			echo '<h2>'.FREQC_JOUR12.'</h2>';
		}
		else if($nivs_info['valeur']=="") {
			include('./db/proc/niveaux_formdef.php');
		}
		else {
			// récupération des niveaux de l'établissement
			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"; }
			
			if(!isset($_GET['stat'])) { 			
				// statistiques générales
				?><style type="text/css">
				#art_freq #lifs1 {
				background-color: #6a0303; }
				</style><?php
				include('./db/proc/freq_stats_default.php');
			}
			else {
			switch($_GET['stat']) {
				case'default':
					?><style type="text/css">
					#art_freq #lifs1 {
					background-color: #6a0303; }
					</style><?php
					include('./db/proc/freq_stats_default.php');
					break;
				case'eleves':
					?><style type="text/css">
					#art_freq #lifs2 {
					background-color: #6a0303; }
					</style><?php
					include('./db/proc/freq_stats_eleves.php');
					break;
				case'activites':
					?><style type="text/css">
					#art_freq #lifs4 {
					background-color: #6a0303; }
					</style><?php
					include('./db/proc/freq_stats_activ.php');
					break;
				case'espaces':
					?><style type="text/css">
					#art_freq #lifs5 {
					background-color: #6a0303; }
					</style><?php
					include('./db/proc/freq_stats_espaces.php');
					break;
				case'proportions':
					?><style type="text/css">
					#art_freq #lifs3 {
					background-color: #6a0303; }
					</style><?php
					include('./db/proc/freq_stats_props.php');
					break;
				case'periodes':
					?><style type="text/css">
					#art_freq #lifs7 {
					background-color: #6a0303; }
					</style><?php
					include('./db/proc/freq_stats_periodes.php');
					break;
				case'creneaux':
					?><style type="text/css">
					#art_freq #lifs8 {
					background-color: #6a0303; }
					</style><?php
					include('./db/proc/freq_stats_creneaux.php');
					break;
				case'annees':
					?><style type="text/css">
					#art_freq #lifs9 {
					background-color: #6a0303; }
					</style><?php
					include('./db/proc/freq_stats_annees.php');
					break;
				case'passages':
					?><style type="text/css">
					#art_freq #lifs6 {
					background-color: #6a0303; }
					</style><?php
					include('./db/proc/freq_stats_passages.php');
					break;
				case'users':
					?><style type="text/css">
					#art_freq #lifsa {
					background-color: #6a0303; }
					</style><?php
					include('./db/proc/freq_stats_users.php');
					break;
				}
			}
		}
	}
}
else {
	include('include/noaccess.php');
	echo '<br><a href="./index.php">'.ACCUEIL.'</a></p>';
}
?>
