<?php

if(isset($_GET["s"])){
	if($_GET["s"]=="passperniv"||$_GET["s"]=="pcpassperniv"||$_GET["s"]=="all") {
		//passages par niveau :
		$tabNbPerNiveau = array();
		$tabPercentPerNiveau = array();
		foreach($tabDebniveau as $divdeb) {
			$nbeleves = 0;
			$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND niveau = '".$divdeb."' AND passage = '1' ".$filtre_activs."");
			while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
				$nbeleves += $elegrap['nombre'];
			}
			array_push($tabNbPerNiveau, $nbeleves);
			$percent = round(($nbeleves * 100 / $totalpass_nb),2);
			array_push($tabPercentPerNiveau, $percent);
		}
		if($_GET["s"]=="passperniv"||$_GET["s"]=="all") {
			include('freq_stats_passchart_passperniv.php');
		}
		if($_GET["s"]=="pcpassperniv"||$_GET["s"]=="all") {
			include('freq_stats_passchart_pcpassperniv.php');
		}
	}
	if($_GET["s"]=="passperdiv"||$_GET["s"]=="pcpassperdiv"||$_GET["s"]=="all") {
		// passages par division 
		$tabNbPerDiv = array();
		$tabPercentPerDiv = array();
		$liste_division = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves WHERE categ = '1' GROUP BY division ORDER BY division ASC");
		while($listediv = mysqli_fetch_assoc($liste_division)) {
			$nbeleves = 0;
			$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND division = '".$listediv['division']."' AND passage = '1' ".$filtre_activs."");
			while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
				$nbeleves += $elegrap['nombre'];
			}
			array_push($tabNbPerDiv, $nbeleves);
			$percent = round(($nbeleves * 100 / $totalpass_nb),2);
			array_push($tabPercentPerDiv, $percent);
		}
		if($_GET["s"]=="passperdiv"||$_GET["s"]=="all") {
			include('freq_stats_passchart_passperdiv.php');
		}
		if($_GET["s"]=="pcpassperdiv"||$_GET["s"]=="all") {
			include('freq_stats_passchart_pcpassperdiv.php');
		}
	}
	if($_GET["s"]=="passperweek"||$_GET["s"]=="passperweekperniv"||$_GET["s"]=="all") {
		$tabWeeks = array();
		$tabNbPerWeek = array();

		foreach (new DatePeriod($jourP1date, new DateInterval('P7D') /* pas de sept jours */, $jourP2date) as $dt) {
			$date = $dt->format('Y-m-d');
			$week = $dt->format('oW');
			$datetoend = strtotime(date("Y-m-d", strtotime($date)) . " +6 day"); // ajouter 6 jours
			$dateend = date('Y-m-d',$datetoend);
			$nbeleves = 0;
			$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$dt->format('Y-m-d')."' AND date_freq <= '".$dateend."' AND passage = '1' ".$filtre_activs."");
			while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
				$nbeleves += $elegrap['nombre'];
			}
			array_push($tabNbPerWeek, $nbeleves);
			array_push($tabWeeks, $week);
		}
		if($_GET["s"]=="passperweek"||$_GET["s"]=="all") {
			// fréquentation par semaine :
			include('freq_stats_passchart_passperweek.php');	
		}
		if($_GET["s"]=="passperweekperniv"||$_GET["s"]=="all") {
			// fréquentation par semaine et par niveau :
			include('freq_stats_passchart_passperweekperniv.php');
		}
	}
	if($_GET["s"]=="passperday"||$_GET["s"]=="passperdayperniv"||$_GET["s"]=="all") {
		if(count($tabWeeks)<6) {
			$tabDays = array();
			$tabNbPerDay = array();

			foreach (new DatePeriod($jourP1date, new DateInterval('P1D') /* pas d'un jour */, $jourP2date) as $dt) {
				$date = $dt->format('Y-m-d');
				$nbeleves = 0;
				$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq = '".$dt->format('Y-m-d')."' AND passage = '1' ".$filtre_activs."");
				while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
					$nbeleves += $elegrap['nombre'];
				}
				array_push($tabNbPerDay, $nbeleves);
				array_push($tabDays, $date);
			}
		}
		if(($_GET["s"]=="passperday"||$_GET["s"]=="all")&&count($tabWeeks)<6) {
			// fréquentation par jour (-36)
			include('freq_stats_passchart_passperday.php');
		}
		if(($_GET["s"]=="passperdayperniv"||$_GET["s"]=="all")&&count($tabWeeks)<6) {
			// fréquentation par jour et niveau (-36)
			include('freq_stats_passchart_passperdayperniv.php');
		}
	}
}
?>
