<?php
if(isset($_GET["s"])){
	$_SESSION['bctabs']='';
	if($_GET["s"]=="nbpermonth"||$_GET["s"]=="nbpermonthperniv"||$_GET["s"]=="all") {
		$bctab1 = '<table style="text-align:right;">
		<thead><tr><td colspan="3" style="text-align:center;">'.FDIR_STATS_PER1.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th>'.MONTH.'</th><th>'.NUMBER.'</th></tr></thead><tbody>';
		$tabMonths = array();
		$tabNbPerMonth = array();
		foreach(${$codepays.'_months'} as $month) {
			if($codepays=='fnc'&&$month=='01') {
				$y = $year+1;
			}
			if(($codepays=='fra'||$codepays=='bel'||$codepays=='aut')&&($month>='01'&&$month<='07')) {
				$y = substr($year,4,4);
			}
			else {
				$y = substr($year,0,4);
			}
			$nbeleves_niv = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE freq.date_freq LIKE '".$y."-".$month."%' ".$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 LIKE '".$y."-".$month."%' ".$filtre_activs."");
				while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
					$nbeleves += $elegrap['nombre'];
				}
			}
			array_push($tabNbPerMonth, $nbeleves);
			array_push($tabMonths, $month.'/'.$y);
			$bctab1 .= '<tr><td>'.$month.'/'.$y.'</td><td>'.$nbeleves.'</td></tr>';
		}
		$bctab1 .= '<tr><td>'.TOTAL.'</td><td>'.$totalfreq_nb.'</td></tr></tbody></table>';
	}
	if($_GET["s"]=="nbpermonth"||$_GET["s"]=="all") {
		// fréquentation par mois :
		include('freq_stats_perchart_nbpermonth.php');	
		echo '<br> <br>'.$bctab1;
		$_SESSION['bctabs'] .= '<table></table>'.$bctab1;
	}
	if($_GET["s"]=="nbpermonthperniv"||$_GET["s"]=="all") {
		// fréquentation par mois et par niveau :
		include('freq_stats_perchart_nbpermonthperniv.php');
		echo '<br> <br>'.$bctab2;
		$_SESSION['bctabs'] .= '<table></table>'.$bctab2;
	}
	if($_GET["s"]=="nbpersession"||$_GET["s"]=="nbpersessionperniv"||$_GET["s"]=="all") {
		$bctab3 = '<table style="text-align:right;">
		<thead><tr><td colspan="3" style="text-align:center;">'.FDIR_STATS_PER2.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th>'.SESSION.'</th><th>'.NUMBER.'</th></tr></thead><tbody>';
		$tabSessions = array();
		$tabNbPerSession = array();
		$rack=0;
		for($z=0;$z<(count(${$codepays.'_'.$zoneacad.'_'.$year.'_periodsclose'})/2);$z++) {
			if($z==0) {
				$nbeleves_niv = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE freq.date_freq >= '".${$codepays.'_all_'.$year.'_cles'}[0]."' AND freq.date_freq <= '".${$codepays.'_'.$zoneacad.'_'.$year.'_periodsclose'}[0]."' ".$filtre_activs."");
				$nbeleves = mysqli_num_rows($nbeleves_niv);
				if($grappes_check==1) {
					$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes freq WHERE freq.date_freq >= '".${$codepays.'_all_'.$year.'_cles'}[0]."' AND freq.date_freq <= '".${$codepays.'_'.$zoneacad.'_'.$year.'_periodsclose'}[0]."' ".$filtre_activs."");
					while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
						$nbeleves += $elegrap['nombre'];
					}
				}
				$dates = ${$codepays.'_all_'.$year.'_cles'}[0].' '.TO2.' '.${$codepays.'_'.$zoneacad.'_'.$year.'_periodsclose'}[0];
			}
			else if(isset(${$codepays.'_'.$zoneacad.'_'.$year.'_periodsclose'}[($rack)])) {
				$nbeleves_niv = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE freq.date_freq > '".${$codepays.'_'.$zoneacad.'_'.$year.'_periodsclose'}[($rack-1)]."' AND freq.date_freq <= '".${$codepays.'_'.$zoneacad.'_'.$year.'_periodsclose'}[$rack]."' ".$filtre_activs."");
				$nbeleves = mysqli_num_rows($nbeleves_niv);
				if($grappes_check==1) {
					$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes freq WHERE freq.date_freq >= '".${$codepays.'_'.$zoneacad.'_'.$year.'_periodsclose'}[($rack-1)]."' AND freq.date_freq <= '".${$codepays.'_'.$zoneacad.'_'.$year.'_periodsclose'}[$rack]."' ".$filtre_activs."");
					while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
						$nbeleves += $elegrap['nombre'];
					}
				}
				$dates = date('Y-m-d', strtotime(date("Y-m-d", strtotime(${$codepays.'_'.$zoneacad.'_'.$year.'_periodsclose'}[($rack-1)])) . " +1 day")).' '.TO2.' '.${$codepays.'_'.$zoneacad.'_'.$year.'_periodsclose'}[$rack];
			}
			array_push($tabNbPerSession, $nbeleves);
			array_push($tabSessions, SESSION.' '.($z+1).' '.FROM2.' '.$dates);
			$bctab3 .= '<tr><td>'.SESSION.' '.($z+1).' '.FROM2.' '.$dates.'</td><td>'.$nbeleves.'</td></tr>';
			$rack+=2;
		}
		$bctab3 .= '<tr><td>'.TOTAL.'</td><td>'.$totalfreq_nb.'</td></tr></tbody></table>';
	}
	if($_GET["s"]=="nbpersession"||$_GET["s"]=="all") {
		// fréquentation par session :
		include('freq_stats_perchart_nbpersession.php');
		echo '<br> <br>'.$bctab3;
		$_SESSION['bctabs'] .= '<table></table>'.$bctab3;	
	}
	if($_GET["s"]=="nbpersessionperniv"||$_GET["s"]=="all") {
		// fréquentation par session et par niveau :
		include('freq_stats_perchart_nbpersessionperniv.php');
		echo '<br> <br>'.$bctab4;
		$_SESSION['bctabs'] .= '<table></table>'.$bctab4;	
	}
	
	$per_check = mysqli_query($bdd,"SELECT * FROM ".$prefix."_periodes WHERE anneesco = '".$year."'");
	if(mysqli_num_rows($per_check)>0&&($_GET["s"]=="nbperperiode"||$_GET["s"]=="nbperperiodeperniv"||$_GET["s"]=="all")) {
		$bctab5 = '<table style="text-align:right;">
		<thead><tr><td colspan="3" style="text-align:center;">'.FDIR_STATS_PER3.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th>'.PERIOD.'</th><th>'.NUMBER.'</th></tr></thead><tbody>';
		$per_infos = mysqli_fetch_assoc($per_check);
		$per_pv = explode(";", $per_infos['periodes']);
		$per_nom = explode(";", $per_infos['nom']);		
		$tabPeriodes = array();
		$tabNbPerPeriode = array();
		$rack=0;
		for($z=0;$z<((count($per_pv)/2)-1);$z++) {
			if(isset($per_pv[$rack])) {
				$nbeleves_niv = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE freq.date_freq > '".$per_pv[$rack]."' AND freq.date_freq <= '".$per_pv[($rack+1)]."' ".$filtre_activs."");
				$nbeleves = mysqli_num_rows($nbeleves_niv);
				if($grappes_check==1) {
					$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes freq WHERE freq.date_freq > '".$per_pv[$rack]."' AND freq.date_freq <= '".$per_pv[($rack+1)]."' ".$filtre_activs."");
					while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
						$nbeleves += $elegrap['nombre'];
					}
				}
				$dates = "";
				if($per_nom[$z]!="") {
					$dates .= $per_nom[$z].' : ';
				}
				$dates .= FROM2.' '.$per_pv[$rack].' '.TO2.' '.$per_pv[($rack+1)];
			}
			array_push($tabNbPerPeriode, $nbeleves);
			array_push($tabPeriodes, $dates);
			$bctab5 .= '<tr><td>'.$dates.'</td><td>'.$nbeleves.'</td></tr>';
			$rack+=2;
		}
		$bctab5 .= '<tr><td>'.TOTAL.'</td><td>'.$totalfreq_nb.'</td></tr></tbody></table>';
	}
	if(mysqli_num_rows($per_check)>0&&($_GET["s"]=="nbperperiode"||$_GET["s"]=="all")) {
		// fréquentation par session :
		include('freq_stats_perchart_nbperperiode.php');
		echo '<br> <br>'.$bctab5;
		$_SESSION['bctabs'] .= '<table></table>'.$bctab5;	
	}
	if(mysqli_num_rows($per_check)>0&&($_GET["s"]=="nbperperiodeperniv"||$_GET["s"]=="all")) {
		// fréquentation par session et par niveau :
		include('freq_stats_perchart_nbperperiodeperniv.php');
		echo '<br> <br>'.$bctab6;
		$_SESSION['bctabs'] .= '<table></table>'.$bctab6;	
	}

	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>';
}
?>
