<?php
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][2]!=2)
	{
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="./index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		?>
		<style type="text/css">
		#mdt5 { background-color: #fff; }
		#mdt5 td { border: 3px #E77015 solid; }
		#mdt4 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

		// Gestion du calendrier pour l'année en cours
		require_once('./db/proc/caloffsco.php');
		
		echo '<div class="div-loading"><img class="ajax-loading" src="img/div/loadingImage.gif" alt="Loading" /></div>';
		
		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 '<div class="edt_padding"><h1>'.EDTC_STAT1.'</h1>';
		
		// préparation des conditions pour le menu des stats EDT :
		$seqannees_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id WHERE brik.seance = 1 AND event.week >= '".$renweekpre."' AND event.week <= '".$finweekpre."'"));
		$nb_edt = mysqli_num_rows(mysqli_query($bdd,"SELECT edt FROM ".$prefix."_user WHERE edt = '1'"));
		$nb_espedt = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_espaces WHERE edtopt = '1'"));
		// menu stats générales (si d'autres stats dispos)
		echo '<ul class="menustats"><li class="itemstats" id="lies1"><a href="edt.php?page=statsedt&stat=default">'.FSTATS_TIT8.'</a></li>';
		echo '<li class="itemstats" id="lies2"><a href="edt.php?page=statsedt&stat=seances">'.SEAN4.'</a></li>';
		// menu évolution si matière pour les stats
		if($seqannees_nb!=0) {
			echo '<li class="itemstats" id="lies3"><a href="edt.php?page=statsedt&stat=annees">'.FSTATS_TIT3.'</a></li>';
		}
		// menu users en attente
		if ($nb_espedt>0) {
			echo '<!--<li class="itemstats" id="lies4"><a href="edt.php?page=statsedt&stat=espaces">'.PARANAV8.'</a></li>-->';
		}
		if ($nb_edt>0) {
			echo '<!--<li class="itemstats" id="lies5"><a href="edt.php?page=statsedt&stat=users">'.PARANAV2.'</a></li>-->';
		}
		echo '</ul><br>';
		if(isset($_POST['niveaux_valid'])) {
			$freqniv = $_POST['niveaux'];
			mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '".$freqniv."' WHERE param_nom = 'freq_statsniv'");
		}
			
		// Affichage des séances pour toutes les divisions
		$eventall = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id WHERE brik.seance = 1");
		$eventall_nb = mysqli_num_rows($eventall);
		// vérification des niveaux :
		$nivs = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_statsniv'");
		$nivs_info = mysqli_fetch_assoc($nivs);

		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"; }
			
			if(!isset($_GET['stat'])) { 			
				// statistiques générales
				include('./db/proc/edt_stats_default.php');
				?><style type="text/css">
				#art_edt #lies1 {
				background-color: #a24e0e; }
				</style><?php
			}
			else {
			switch($_GET['stat']) {
				case'default':
					?><style type="text/css">
					#art_edt #lies1 {
					background-color: #a24e0e; }
					</style><?php
					include('./db/proc/edt_stats_default.php');
					break;
				case'seances':
					?><style type="text/css">
					#art_edt #lies2 {
					background-color: #a24e0e; }
					</style><?php
					include('./db/proc/edt_stats_seances.php');
					break;
				case'annees':
					?><style type="text/css">
					#art_edt #lies3 {
					background-color: #a24e0e; }
					</style><?php
					include('./db/proc/edt_stats_annees.php');
					break;
				case'users':
					?><style type="text/css">
					#art_edt #lies4 {
					background-color: #a24e0e; }
					</style><?php
					include('./db/proc/edt_stats_users.php');
					break;
				}
			}
		}
	}
}
	
else {
	include('include/noaccess.php');
	echo '<br><a href="./index.php>'.ACCUEIL.'</a></p>';
}
?>