<?php

if(isset($_GET["s"])){
	$_SESSION['bctabs']='';
	if($_GET["s"]=="nbperniv"||$_GET["s"]=="percentperniv"||$_GET["s"]=="all") {
		//fréquentation par niveau :
		$bctab1 = '<table style="text-align:right;">
		<thead><tr><td colspan="3" style="text-align:center;">'.FDIR_STATS_TIT13.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th></th><th>'.NUMBER.'</th><th>%</th></tr></thead><tbody>';
		$tabNbPerNiveau = array();
		$tabPercentPerNiveau = array();
		$nivi=0;
		foreach($tabDebniveau as $divdeb) {
			$nbeleves_niv = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND division LIKE '".$divdeb."%' ".$filtre_activs."");
			$nbeleves = mysqli_num_rows($nbeleves_niv);
			if($grappes_check==1) {
				$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND niveau = '".$divdeb."' AND passage = '0' ".$filtre_activs."");
				while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
					$nbeleves += $elegrap['nombre'];
				}
			}
			array_push($tabNbPerNiveau, $nbeleves);
			$percent = round(($nbeleves * 100 / $totalfreq_nb),2);
			array_push($tabPercentPerNiveau, $percent);
			$bctab1 .= '<tr><td>'.$tabNiveau[$nivi].'</td><td>'.$nbeleves.'</td><td>'.number_format((float)$percent,2,'.','').'</td></tr>';
			$nivi++;
		}
		if($_GET["s"]=="nbperniv"||$_GET["s"]=="all") {
			include('freq_stats_defchart_nbperniv.php');			
		}
		if($_GET["s"]=="percentperniv"||$_GET["s"]=="all") {
			include('freq_stats_defchart_percentperniv.php');
		}
		$bctab1 .= '<tr><td>'.TOTAL.'</td><td>'.$totalfreq_nb.'</td><td>100.00</td></tr></tbody></table>';
		echo '<br> <br>'.$bctab1;	
		$_SESSION['bctabs'] .= $bctab1;	
	}
	if($_GET["s"]=="nbperdiv"||$_GET["s"]=="percentperdiv"||$_GET["s"]=="all") {
		//fréquentation par division :
		$bctab2 = '<table style="text-align:right;">
		<thead><tr><td colspan="3" style="text-align:center;">'.FDIR_STATS_TIT14.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th></th><th>'.NUMBER.'</th><th>%</th></tr></thead><tbody>';
		$tabNbPerDiv = array();
		$tabPercentPerDiv = array();
		$liste_division = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves GROUP BY division ORDER BY division ASC");
		foreach($liste_division as $listediv) {
			$nbeleves_div = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND division = '".$listediv['division']."' ".$filtre_activs."");
			$nbeleves = mysqli_num_rows($nbeleves_div);
			if($grappes_check==1) {
				$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$day1."' AND date_freq <= '".$day2."' AND division = '".$listediv['division']."' AND passage = '0' ".$filtre_activs."");
				while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
					$nbeleves += $elegrap['nombre'];
				}
			}
			array_push($tabNbPerDiv, $nbeleves);
			$percent = round(($nbeleves * 100 / $totalfreq_nb),2);
			array_push($tabPercentPerDiv, $percent);
			$bctab2 .= '<tr><td>'.$listediv['division'].'</td><td>'.$nbeleves.'</td><td>'.number_format((float)$percent,2,'.','').'</td></tr>';
		}
		if($_GET["s"]=="nbperdiv"||$_GET["s"]=="all") {
			include('freq_stats_defchart_nbperdiv.php');
		}
		if($_GET["s"]=="percentperdiv"||$_GET["s"]=="all") {
			include('freq_stats_defchart_percentperdiv.php');
		}
		$bctab2 .= '<tr><td>'.TOTAL.'</td><td>'.$totalfreq_nb.'</td><td>100.00</td></tr></tbody></table>';
		echo '<br> <br>'.$bctab2;
		$_SESSION['bctabs'] .= '<table></table>'.$bctab2;	
	}
	if($_GET["s"]=="nbperweek"||$_GET["s"]=="nbperweekperniv"||$_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_niv = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE freq.date_freq >= '".$dt->format('Y-m-d')."' AND freq.date_freq <= '".$dateend."' ".$filtre_activs."");
			$nbeleves = mysqli_num_rows($nbeleves_niv);
			if($grappes_check==1) {
				$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$dt->format('Y-m-d')."' AND date_freq <= '".$dateend."' AND passage = '0' ".$filtre_activs."");
				while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
					$nbeleves += $elegrap['nombre'];
				}
			}
			array_push($tabNbPerWeek, $nbeleves);
			array_push($tabWeeks, $week);
		}
		$i=1;
		foreach($tabDebniveau as $divdeb) {
			$tab[$i] = array();
			$nivcolor = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$divdeb."'");
			$nivcolor_info = mysqli_fetch_assoc($nivcolor);
			$tabColor[$i] = $nivcolor_info['bgcolor'];
			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_niv = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE freq.date_freq >= '".$dt->format('Y-m-d')."' AND freq.date_freq <= '".$dateend."' AND division LIKE '".$divdeb."%' ".$filtre_activs."");
				$nbeleves = mysqli_num_rows($nbeleves_niv);
				if($grappes_check==1) {
					$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$dt->format('Y-m-d')."' AND date_freq <= '".$dateend."' AND niveau = '".$divdeb."' AND passage = '0' ".$filtre_activs."");
					while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
						$nbeleves += $elegrap['nombre'];
					}
				}
				array_push($tab[$i], $nbeleves);
			}
			$i++;
		}	
		$bctab3 = '<table style="text-align:right;">
		<thead><tr><td colspan="'.($i+1).'" style="text-align:center;">'.FDIR_STATS_TIT15.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th></th>';
		$nivi=0;
		foreach($tabDebniveau as $divdeb) {
			$bctab3 .= '<th>'.$tabNiveau[$nivi].'</th>';
			$nivi++;
		}
		$bctab3 .= '<th>'.TOTAL.'</th></tr></thead><tbody>';
		$w=0;
		foreach($tabWeeks as $weekname) {
			$bctab3 .= '<tr><td>'.$weekname.'</td>';
			$m=1;
			foreach($tabDebniveau as $divdeb) {
				$bctab3 .= '<td>'.$tab[$m][$w].'</td>';
				$m++;
			}
			$bctab3 .= '<td>'.$tabNbPerWeek[$w].'</td></tr>';
			$w++;
		}	
		$bctab3 .= '<tr><td>'.TOTAL.'</td><td colspan="'.($i-1).'"></td><td>'.$totalfreq_nb.'</td></tr></tbody></table>';
		if($_GET["s"]=="nbperweekperniv"||$_GET["s"]=="all") {
			// fréquentation par semaine et par niveau :
			include('freq_stats_defchart_nbperweekperniv.php');
		}		
	}
	if($_GET["s"]=="nbperweek"||$_GET["s"]=="all") {
		// fréquentation par semaine :
		include('freq_stats_defchart_nbperweek.php');	
	}	
	if($_GET["s"]=="nbperweek"||$_GET["s"]=="nbperweekperniv"||$_GET["s"]=="all") {
		echo '<br> <br>'.$bctab3;
		$_SESSION['bctabs'] .= '<table></table>'.$bctab3;
	}
	if($_GET["s"]=="nbperday"||$_GET["s"]=="nbperdayperniv"||$_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_niv = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE freq.date_freq = '".$dt->format('Y-m-d')."' ".$filtre_activs."");
				$nbeleves = mysqli_num_rows($nbeleves_niv);
				if($grappes_check==1) {
					$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq = '".$dt->format('Y-m-d')."' AND passage = '0' ".$filtre_activs."");
					while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
						$nbeleves += $elegrap['nombre'];
					}
				}
				array_push($tabNbPerDay, $nbeleves);
				array_push($tabDays, $date);
			}
			$i=1;
			foreach($tabDebniveau as $divdeb) {
				$tabDay[$i] = array();
				$nivcolor = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$divdeb."'");
				$nivcolor_info = mysqli_fetch_assoc($nivcolor);
				$tabColor[$i] = $nivcolor_info['bgcolor'];
				foreach (new DatePeriod($jourP1date, new DateInterval('P1D') /* pas d'un jour */, $jourP2date) as $dt) {
					$date = $dt->format('Y-m-d');
					$nbeleves_niv = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE freq.date_freq = '".$dt->format('Y-m-d')."' AND freq.division LIKE '".$divdeb."%' ".$filtre_activs."");
					$nbeleves = mysqli_num_rows($nbeleves_niv);
					if($grappes_check==1) {
						$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq = '".$dt->format('Y-m-d')."' AND niveau = '".$divdeb."' AND passage = '0' ".$filtre_activs."");
						while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
							$nbeleves += $elegrap['nombre'];
						}
					}
					array_push($tabDay[$i], $nbeleves);
				}
				$i++;
			}
			$bctab4 = '<table style="text-align:right;">
			<thead><tr><td colspan="'.($i+1).'" style="text-align:center;">'.FDIR_STATS_TIT16.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th></th>';
			$nivi=0;
			foreach($tabDebniveau as $divdeb) {
				$bctab4 .= '<th>'.$tabNiveau[$nivi].'</th>';
				$nivi++;
			}
			$bctab4 .= '<th>'.TOTAL.'</th></tr></thead><tbody>';
			$w=0;
			foreach($tabDays as $dayname) {
				$bctab4 .= '<tr><td>'.$dayname.'</td>';
				$m=1;
				foreach($tabDebniveau as $divdeb) {
					$bctab4 .= '<td>'.$tabDay[$m][$w].'</td>';
					$m++;
				}
				$bctab4 .= '<td>'.$tabNbPerDay[$w].'</td></tr>';
				$w++;
			}	
			$bctab4 .= '<tr><td>'.TOTAL.'</td><td colspan="'.($i-1).'"></td><td>'.$totalfreq_nb.'</td></tr></tbody></table>';
		}
		$affperday=0;
	}
	if(($_GET["s"]=="nbperday"||$_GET["s"]=="all")&&count($tabWeeks)<6) {
		// fréquentation par jour (-36)
		include('freq_stats_defchart_nbperday.php');
	}
	if(($_GET["s"]=="nbperdayperniv"||$_GET["s"]=="all")&&count($tabWeeks)<6) {
		// fréquentation par jour et niveau (-36)
		include('freq_stats_defchart_nbperdayperniv.php');		
	}	
	if($_GET["s"]=="nbperday"||$_GET["s"]=="nbperdayperniv"||$_GET["s"]=="all") {
		if(count($tabWeeks)<6) {
			echo '<br> <br>'.$bctab4;
			$_SESSION['bctabs'] .= '<table></table>'.$bctab4;
		}
	}

	echo '<br> <a onclick="submitods();" id="preview-ods"><i class="fa-solid fa-file-export" style="font-size:1.5em;"></i> '.STATS_DL1.'</a>';
	if(isset($_POST['haveods'])) {
		include('stats_ods_loader.php');
	}
    echo '<form id="loadods" action="db/proc/stats/stats_ods_loader.php" method="post"><input type="hidden" name="haveods" value="1" /></form>';

    echo '<script type="text/javascript">
	function submitods(){
		$("#loadods").submit()
	}
	</script>';
}
?>
