<?php
if(isset($_GET["s"])){
	$_SESSION['bctabs']='';
	if($_GET["s"]=="propperniv"||$_GET["s"]=="all") {
		//proportion par niveau :
		include('freq_stats_propchart_propperniv.php');
	}
	if($_GET["s"]=="propperdiv"||$_GET["s"]=="all") {
		//proportion par niveau :
		include('freq_stats_propchart_propperdiv.php');
	}
	if($_GET["s"]=="propperweek"||$_GET["s"]=="propperweekperniv"||$_GET["s"]=="all") {
		$tabWeeks = array();
		$tabPropPerWeek = array();
		$elediv = mysqli_num_rows(mysqli_query($bdd,"SELECT eleves_id FROM ".$prefix."_eleves"));
		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 DISTINCT freq.eleves_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($elediv!='0') {
			   $prop = round(($nbeleves * 100 / $elediv),2);
		    }
		    else {
		        $prop = 0;
		    }
			if($prop>100) { $prop = 100; }
			array_push($tabPropPerWeek, $prop);
			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 DISTINCT freq.eleves_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);
		        $elediv = mysqli_num_rows(mysqli_query($bdd,"SELECT eleves_id FROM ".$prefix."_eleves WHERE division LIKE '".$divdeb."%'"));
		        if($elediv!='0') {
				   $prop = round(($nbeleves * 100 / $elediv),2);
			    }
			    else {
			        $prop = 0;
			    }
		        if($prop>100) { $prop = 100; }
		        array_push($tab[$i], $prop);
		    }
		    $i++;
		}
		$bctab3 = '<table style="text-align:right;">
		<thead><tr><td colspan="'.($i+1).'" style="text-align:center;">'.FDIR_STATS_PROP1.'<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>'.number_format((float)$tab[$m][$w],2,'.','').'</td>';
				$m++;
			}
			$bctab3 .= '<td>'.number_format((float)$tabPropPerWeek[$w],2,'.','').'</td></tr>';
			$w++;
		}	
		$bctab3 .= '</tbody></table>';
		//proportion par niveau :
		include('freq_stats_propchart_propperweek.php');
	}
	if($_GET["s"]=="propperweekperniv"||$_GET["s"]=="all") {
		//proportion par niveau :
		include('freq_stats_propchart_propperweekperniv.php');
	}
	if($_GET["s"]=="propperweek"||$_GET["s"]=="propperweekperniv"||$_GET["s"]=="all") {
		echo '<br> <br>'.$bctab3;
		$_SESSION['bctabs'] .= '<table></table>'.$bctab3;
	}
	if($_GET["s"]=="propperday"||$_GET["s"]=="propperdayperniv"||$_GET["s"]=="all") {
		if(count($tabWeeks)<6) {
			$tabDays = array();
			$tabPropPerDay = array();
			$elediv = mysqli_num_rows(mysqli_query($bdd,"SELECT eleves_id FROM ".$prefix."_eleves"));
			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 DISTINCT freq.eleves_id FROM ".$prefix."_freq freq WHERE freq.date_freq = '".$dt->format('Y-m-d')."' ".$filtre_activs."");
				$nbeleves = mysqli_num_rows($nbeleves_niv);
				$prop = round(($nbeleves * 100 / $elediv),2);
				if($prop>100) { $prop = 100; }
				array_push($tabPropPerDay, $prop);
				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 DISTINCT freq.eleves_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); 
				$elediv = mysqli_num_rows(mysqli_query($bdd,"SELECT eleves_id FROM ".$prefix."_eleves WHERE division LIKE '".$divdeb."%'"));
				$prop = round(($nbeleves * 100 / $elediv),2);
				if($prop>100) { $prop = 100; }
				array_push($tabDay[$i], $prop);
			}
			$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>'.number_format((float)$tabDay[$m][$w],2,'.','').'</td>';
				$m++;
			}
			$bctab4 .= '<td>'.number_format((float)$tabPropPerDay[$w],2,'.','').'</td></tr>';
			$w++;
		}	
		$bctab4 .= '</tbody></table>';
	}
	if(($_GET["s"]=="propperday"||$_GET["s"]=="all")&&count($tabWeeks)<6) {
		// fréquentation par jour (-36)
		include('freq_stats_propchart_propperday.php');
	}
	if(($_GET["s"]=="propperdayperniv"||$_GET["s"]=="all")&&count($tabWeeks)<6) {
		// fréquentation par jour et niveau (-36)
		include('freq_stats_propchart_propperdayperniv.php');
	}
	if($_GET["s"]=="propperday"||$_GET["s"]=="propperdayperniv"||$_GET["s"]=="all") {
		echo '<br> <br>'.$bctab4;
		$_SESSION['bctabs'] .= '<table></table>'.$bctab4;
	}
	if($_GET["s"]=="genre"||$_GET["s"]=="all") {
		// stats par genre
		include('freq_stats_propchart_genre.php');
	}

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