<?php

echo EDT_STATS5;
if ($nb_espedt>0) {
	echo '<p class="signal">'.EDT_STATS2.'</p> ';
}

$_SESSION['bctabs']='';
// récupération des divisions et groupes
$div = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves GROUP BY division ORDER BY division ASC");
$groupes = mysqli_query($bdd,"SELECT groupe_nom FROM ".$prefix."_groupes");

include('edt_stats_form.php');

// Stats
if($acadexist=="fnc") { $w1 = '07'; } 
else if($acadexist=="civ") { $w1 = '01'; } 
else { $w1 = '33'; }
if($acadexist=="fnc"&&isset($anneesco)) { $w2 = @$anneesco."50"; } 
else if($acadexist=="civ"&&isset($anneesco)) { $w2 = @$anneesco."52"; } 
else { $w2 = @substr($anneesco,5,4)."27"; }
if(isset($anneesco)) {
	echo '<h2>'.YEAR.' '.$anneesco.' '.$info_comp.'</h2>';
}
else if(isset($_SESSION['week1'])) {
	echo '<h2>'.WEEKS.' '.substr($_SESSION['week1'],0,4).'-'.substr($_SESSION['week1'],4,2).' '.TO.' '.substr($_SESSION['week2'],0,4).'-'.substr($_SESSION['week2'],4,2).'</h2><p>'.WEEKFROM.' '.date("d/m/Y",strtotime($day1)).' '.WEEKTO.' '.date("d/m/Y",strtotime($day2)).'.</p>';
}
if(isset($_SESSION['week1'])) {
	$eventseance = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".$_SESSION['week1']."' AND event.week <= '".$_SESSION['week2']."' AND uevent.user_id=0 AND espevent.espace_id ='0'");
}
else {
	$eventseance = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".substr($anneesco,0,4).$w1."' AND event.week <= '".$w2."' AND uevent.user_id=0 AND espevent.espace_id ='0'");
}
$eventseance_nb = 0;
foreach ($eventseance as $eventdiv) {
	if($eventdiv['duration']==5) { $duree = 0.5; }
	else { $duree = substr($eventdiv['duration'],0,1).'.'.substr($eventdiv['duration'],1,1); }
	$eventseance_nb += $duree;
}
if(isset($_SESSION['week1'])) {
	$eventseance_div = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".$_SESSION['week1']."' AND event.week <= '".$_SESSION['week2']."' AND brik.division <> '' AND uevent.user_id=0 AND espevent.espace_id ='0'");
}
else {
	$eventseance_div = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".substr($anneesco,0,4).$w1."' AND event.week <= '".$w2."' AND brik.division <> '' AND uevent.user_id=0 AND espevent.espace_id ='0'");
}
$eventseance_div_nb = 0;
foreach ($eventseance_div as $eventdiv) {
	if($eventdiv['duration']==5) { $duree = 0.5; }
	else { $duree = substr($eventdiv['duration'],0,1).'.'.substr($eventdiv['duration'],1,1); }
	$eventseance_div_nb += $duree;
}

if($eventseance_nb==0) {
	echo '<p>'.EDT_STATS6.'</p>';
}
else {

	echo '<p>'.$eventseance_nb;
	echo ' '.UNIT;
	if($eventseance_nb>1) {echo 's';}
	echo ' '.FROM.' '.SEAN;
	if($eventseance_nb>1) {echo 's';}
	echo ' ('.DONT.' '.$eventseance_div_nb.' '.EDT_STATS7.'</p>';

	//récupération des niveaux et couleurs par niveau
	include('freq_stats_nivcolors.php'); 
	//récupération des arrays spécifiques pour stats edt
	include('edt_stats_arrays.php'); 

	echo '</article><article id="artleft" class="edtsep">';
	//séances par niveau :
	$tabNbPerNiveau = array();
	$tabPercentPerNiveau = array();
	$bctab1 = '<table style="text-align:right;">
	<thead><tr><td colspan="3" style="text-align:center;">'.STATS_TIT16.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th>'.LEVEL.'</th><th>Nombre</th><th>%</th></tr></thead><tbody>';
	$nivi=0;
	foreach($tabDebniveau as $divdeb) {
		if(isset($anneesco)) {
			$nbseances_niv = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".substr($anneesco,0,4).$w1."' AND event.week <= '".$w2."' AND brik.division LIKE '".$divdeb."%' AND uevent.user_id=0 AND espevent.espace_id ='0'");
		}
		else {
			$nbseances_niv = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".$_SESSION['week1']."' AND event.week <= '".$_SESSION['week2']."' AND brik.division LIKE '".$divdeb."%' AND uevent.user_id=0 AND espevent.espace_id ='0'");
		}
		$nbseances = 0;
		foreach ($nbseances_niv as $eventdiv) {
			if($eventdiv['duration']==5) { $duree = 0.5; }
			else { $duree = substr($eventdiv['duration'],0,1).'.'.substr($eventdiv['duration'],1,1); }
			$nbseances += $duree;
		}
		array_push($tabNbPerNiveau, $nbseances);
		$percent = round(($nbseances * 100 / $eventseance_div_nb),2);
		array_push($tabPercentPerNiveau, $percent);
		$bctab1 .= '<tr><td>'.$tabNiveau[$nivi].'</td><td>'.$nbseances.'</td><td>'.number_format((float)$percent,2,'.','').'</td></tr>';
		$nivi++;
	}
	$bctab1 .= '<tr><td>'.TOTAL.'</td><td>'.$eventseance_div_nb.'</td><td>100.00</td></tr></tbody></table>';
	?>
	<div id="wrapper" class="statcadre">
	<canvas id="chart_nbsperniv" class="charts" width="600" height="300"></canvas>
	</div>
	<script type="text/javascript">
	(async function() {
	    
	    Chart.register(ChartDataLabels);
	    const labels = <?php echo json_encode($tabNiveau); ?>;
	    const data = {
	      labels: labels,
	      datasets: [{
	            label: '<?php echo HOURS;?>',
	            data: <?php echo json_encode($tabNbPerNiveau); ?>,
	            backgroundColor: <?php echo json_encode($tabBgcolor); ?>,
	            borderColor: <?php echo json_encode($tabBgcolor); ?>,
	            borderWidth: 1
	        }]
	    };
	new Chart(
	    document.getElementById('chart_nbsperniv'),
	    {
	    type: 'bar',
	    data: data,
	    options: {
	        plugins: {
	            title: {
	                display: true,
	                text: '<?php echo STATS_TIT16.STATS_DET1;?>'
	            },
	            legend: {
	    			display:false
	    		},
	            tooltip : {
	                mode: 'point',
	                intersect: false,
	              }  ,    
	            datalabels: {
	              display: true, // Affiche les étiquettes de données
	              color: '#878787', // Couleur du texte
	              align: 'center', // Alignement horizontal
	              anchor: 'center', // Alignement vertical
	              font: {
	                weight: 'bold', // Style de police
	              },
	              display: function(context) {
	                  return context.dataset.data[context.dataIndex] !== 0; // N'affiche pas si la valeur est égale à 0
	                },
	            },
	        },
	        responsive: true,
	        scales: {
	            yAxes: [{
	                ticks: {
	                    beginAtZero:true
	                }
	            }]
	        },
	      }
	    }
	);
	})();
	</script>

	<div id="wrapper" class="statcadre">
	<canvas id="chart_percentsperniv" class="charts" width="400" height="400"></canvas>
	</div>
	<script type="text/javascript">
	(async function() {
	    
	    Chart.register(ChartDataLabels);
	    const labels = <?php echo json_encode($tabNiveau); ?>;
	    const data = {
	      labels: labels,
	      datasets: [{
	            data: <?php echo json_encode($tabPercentPerNiveau); ?>,
	            backgroundColor: <?php echo json_encode($tabBgcolor); ?>,
	            borderColor: <?php echo json_encode($tabBgcolor); ?>,
	        }]
	    };
	new Chart(
	    document.getElementById('chart_percentsperniv'),
	    {
	    type: 'pie',
	    data : data,
	    options: {
	        plugins: {
	            title: {
	                display: true,
	                text: '<?php echo STATS_TIT18;?>'
	            },
	            legend: {
	                display:true
	            },
	            tooltip : {
	                mode: 'point',
	                intersect: false,
	              }  ,    
	            datalabels: {
	              display: true, // Affiche les étiquettes de données
	              formatter: function(value, context) {
	                    return value + ' %'; // Ajoute le symbole "%" après la valeur
	                },
	              color: '#878787', // Couleur du texte
	              align: 'center', // Alignement horizontal
	              anchor: 'center', // Alignement vertical
	              font: {
	                weight: 'bold', // Style de police
	              },
	              display: function(context) {
	                  return context.dataset.data[context.dataIndex] !== 0; // N'affiche pas si la valeur est égale à 0
	                },
	            },
	        },
	        responsive: true,
	      }
	    }
	);
	})();
	</script>

	<?php
	echo '<br> <br>'.$bctab1;
	$_SESSION['bctabs'] .= $bctab1;

	//séances par niveau :
	if(isset($anneesco)) {
		if($acadexist=="fnc"||$acadexist=="civ") {
			$numSemaine = 07;
			$annee = substr($anneesco,0,4);
			$numSemaine2 = 50;
			$annee2 = substr($anneesco,0,4);
		}
		else {
			$numSemaine = 33;
			$annee = substr($anneesco,0,4);
			$numSemaine2 = 27;
			$annee2 = substr($anneesco,5,4);
		}
	}
	else {
		$numSemaine = substr($_SESSION['week1'],4,2);
		$annee = substr($_SESSION['week1'],0,4);
		$numSemaine2 = substr($_SESSION['week2'],4,2);
		$annee2 = substr($_SESSION['week2'],0,4);
	}
	include('edt_weekday_convert.php');
	include('calweeknb.php');
	$tabNbPerNiveau = array();
	$tabPercentPerNiveau = array();
	if(isset($anneesco)) {
		$nbseances_niv = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".substr($anneesco,0,4).$w1."' AND event.week <= '".$w2."' AND brik.division <> '' AND uevent.user_id=0 AND espevent.espace_id ='0'");
	}
	else {
		$nbseances_niv = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".$_SESSION['week1']."' AND event.week <= '".$_SESSION['week2']."' AND brik.division <> '' AND uevent.user_id=0 AND espevent.espace_id ='0'");
	}
	$nbseances = 0;
	foreach ($nbseances_niv as $eventdiv) {
		if($eventdiv['duration']==5) { $duree = 0.5; }
		else { $duree = substr($eventdiv['duration'],0,1).'.'.substr($eventdiv['duration'],1,1); }
		$nbseances += $duree;
	}
	array_push($tabNbPerNiveau, round(($nbseances/$nb_week),1));
	$bctab2 = '<table style="text-align:right;">
	<thead><tr><td colspan="3" style="text-align:center;">'.STATS_TIT19.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th>'.LEVEL.'</th><th>'.MOYENNE.'</th></tr></thead><tbody>';
	$nivi=0;	
	foreach($tabDebniveau as $divdeb) {
		if(isset($anneesco)) {
			$nbseances_niv = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".substr($anneesco,0,4).$w1."' AND event.week <= '".$w2."' AND brik.division LIKE '".$divdeb."%' AND uevent.user_id=0 AND espevent.espace_id ='0'");
		}
		else {
			$nbseances_niv = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".$_SESSION['week1']."' AND event.week <= '".$_SESSION['week2']."' AND brik.division LIKE '".$divdeb."%' AND uevent.user_id=0 AND espevent.espace_id ='0'");
		}
		$nbseances = 0;
		foreach ($nbseances_niv as $eventdiv) {
			if($eventdiv['duration']==5) { $duree = 0.5; }
			else { $duree = substr($eventdiv['duration'],0,1).'.'.substr($eventdiv['duration'],1,1); }
			$nbseances += $duree;
		}
		array_push($tabNbPerNiveau, round(($nbseances/$nb_week),1));
		$bctab2 .= '<tr><td>'.$tabNiveau[$nivi].'</td><td>'.round(($nbseances/$nb_week),1).'</td></tr>';
		$nivi++;
	}
	$bctab2 .= '<tr><td>'.TOTAL.'</td><td>'.$tabNbPerNiveau[0].'</td></tr></tbody></table>';
	?>
	<div id="wrapper" class="statcadre">
	<canvas id="chart_moysperniv" class="charts" width="600" height="300"></canvas>
	</div>
	<script type="text/javascript">
	(async function() {
	    
	    Chart.register(ChartDataLabels);
	    const labels = <?php echo json_encode($taballNiveau); ?>;
	    const data = {
	      labels: labels,
	      datasets: [{
	            label: '<?php echo HOURS;?>',
	            data: <?php echo json_encode($tabNbPerNiveau); ?>,
	            backgroundColor: <?php echo json_encode($tabBgallcolor); ?>,
	            borderColor: <?php echo json_encode($tabBgallcolor); ?>,
	            borderWidth: 1
	        }]
	    };
	new Chart(
	    document.getElementById('chart_moysperniv'),
	    {
	    type: 'bar',
	    data: data,
	    options: {
	        plugins: {
	            title: {
	                display: true,
	                text: '<?php echo STATS_TIT19.STATS_DET1;?>'
	            },
	            legend: {
	    			display:false
	    		},
	            tooltip : {
	                mode: 'point',
	                intersect: false,
	              }  ,    
	            datalabels: {
	              display: true, // Affiche les étiquettes de données
	              color: function(context) {
			        var index = context.dataIndex;
			        var color = '#878787'; // Couleur par défaut

			        // Condition pour déterminer la couleur en fonction de l'index ou d'autres critères
			        if (index === 0) {
			            color = '#fff';
			        } 
			        return color; // Retourne la couleur déterminée
			    },
	              align: 'center', // Alignement horizontal
	              anchor: 'center', // Alignement vertical
	              font: {
	                weight: 'bold', // Style de police
	              },
	              display: function(context) {
	                  return context.dataset.data[context.dataIndex] !== 0; // N'affiche pas si la valeur est égale à 0
	                },
	            },
	        },
	        responsive: true,
	        scales: {
	            yAxes: [{
	                ticks: {
	                    beginAtZero:true
	                }
	            }]
	        },
	      }
	    }
	);
	})();
	</script>

	<?php
	echo '<br> <br>'.$bctab2;
	$_SESSION['bctabs'] .= '<table></table>'.$bctab2;

	// séances par division 
	$tabNbPerDiv = array();
	$tabPercentPerDiv = array();
	$liste_division = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves GROUP BY division ORDER BY division ASC");
	$bctab3 = '<table style="text-align:right;">
	<thead><tr><td colspan="3" style="text-align:center;">'.STATS_TIT17.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th>'.DIV.'</th><th>'.NUMBER.'</th><th>%</th></tr></thead><tbody>';
	foreach($liste_division as $listediv) {
		if(isset($anneesco)) {
			$nbseances_div = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".substr($anneesco,0,4).$w1."' AND event.week <= '".$w2."' AND brik.division = '".$listediv['division']."' AND uevent.user_id=0 AND espevent.espace_id ='0'");
		}
		else {
			$nbseances_div = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".$_SESSION['week1']."' AND event.week <= '".$_SESSION['week2']."' AND brik.division = '".$listediv['division']."' AND uevent.user_id=0 AND espevent.espace_id ='0'");
		}
		$nbseances = 0;
		foreach ($nbseances_div as $eventdiv) {
			if($eventdiv['duration']==5) { $duree = 0.5; }
			else { $duree = substr($eventdiv['duration'],0,1).'.'.substr($eventdiv['duration'],1,1); }
			$nbseances += $duree;
		}
		array_push($tabNbPerDiv, $nbseances);
		$percent = round(($nbseances * 100 / $eventseance_div_nb),2);
		array_push($tabPercentPerDiv, $percent);
		$bctab3 .= '<tr><td>'.$listediv['division'].'</td><td>'.$nbseances.'</td><td>'.number_format((float)$percent,2,'.','').'</td></tr>';
	}
	$bctab3 .= '<tr><td>'.TOTAL.'</td><td>'.$eventseance_div_nb.'</td><td>100.00</td></tr></tbody></table>';

	?>
	<div id="wrapper" class="statcadre">
	<canvas id="chart_nbsperdiv" class="charts" width="600" height="300"></canvas>
	</div>
	<script type="text/javascript">
	(async function() {
	    
	    Chart.register(ChartDataLabels);
	    const labels = <?php echo json_encode($tabDivisions); ?>;
	    const data = {
	      labels: labels,
	      datasets: [{
	            label: '<?php echo HOURS;?>',
	            data: <?php echo json_encode($tabNbPerDiv); ?>,
	            backgroundColor: <?php echo json_encode($tabDivColors); ?>,
	            borderColor: <?php echo json_encode($tabDivColors); ?>,
	            borderWidth: 1
	        }]
	    };
	new Chart(
	    document.getElementById('chart_nbsperdiv'),
	    {
	    type: 'bar',
	    data: data,
	    options: {
	        plugins: {
	            title: {
	                display: true,
	                text: '<?php echo STATS_TIT17.STATS_DET1;?>'
	            },
	            legend: {
	    			display:false
	    		},
	            tooltip : {
	                mode: 'point',
	                intersect: false,
	              }  ,    
	            datalabels: {
	              display: true, // Affiche les étiquettes de données
	              color : '#878787',
	              align: 'center', // Alignement horizontal
	              anchor: 'center', // Alignement vertical
	              font: {
	                weight: 'bold', // Style de police
	              },
	              display: function(context) {
	                  return context.dataset.data[context.dataIndex] !== 0; // N'affiche pas si la valeur est égale à 0
	                },
	            },
	        },
	        responsive: true,
	        scales: {
	            yAxes: [{
	                ticks: {
	                    beginAtZero:true
	                }
	            }]
	        },
	      }
	    }
	);
	})();
	</script>

	<div id="wrapper" class="statcadre">
	<canvas id="chart_percentsperdiv" class="charts" width="400" height="400"></canvas>
	</div>
	<script type="text/javascript">
	(async function() {
	    
	    Chart.register(ChartDataLabels);
	    const labels = <?php echo json_encode($tabDivisions); ?>;
	    const data = {
	      labels: labels,
	      datasets: [{
	            data: <?php echo json_encode($tabPercentPerDiv); ?>,
	            backgroundColor: <?php echo json_encode($tabDivColors); ?>,
	            borderColor: <?php echo json_encode($tabDivColors); ?>,
	        }]
	    };
	new Chart(
	    document.getElementById('chart_percentsperdiv'),
	    {
	    type: 'polarArea',
	    data : data,
	    options: {
	        plugins: {
	            title: {
	                display: true,
	                text: '<?php echo STATS_TIT20;?>'
	            },
	            legend: {
	                display:true
	            },
	            tooltip : {
	                mode: 'point',
	                intersect: false,
	              }  ,   
	            legend: {
	                display:false
	            }, 
	            datalabels: {
	              display: false, // Affiche les étiquettes de données
	              color: '#878787', // Couleur du texte
	              align: 'center', // Alignement horizontal
	              anchor: 'center', // Alignement vertical
	              font: {
	                weight: 'normal', // Style de police
	              },
	              display: function(context) {
	                return false; // Ne pas afficher les étiquettes de données
	                },
	            },
	        },
	        responsive: true,
	      }
	    }
	);
	})();
	</script>

	<?php
	echo '<br> <br>'.$bctab3;
	$_SESSION['bctabs'] .= '<table></table>'.$bctab3;

	// séances par groupe
	$tabNbPerGr = array();
	$tabGroupes = array();
	$tabGrColors = array();
	$liste_groupe = mysqli_query($bdd,"SELECT groupe_nom FROM ".$prefix."_groupes GROUP BY groupe_nom ORDER BY groupe_nom ASC");
	$groupes_stats = 0;
	$bctab4 = '<table style="text-align:right;">
	<thead><tr><td colspan="3" style="text-align:center;">'.STATS_TIT21.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th>'.GROUP.'</th><th>'.NUMBER.'</th></tr></thead><tbody>';
	foreach($liste_groupe as $listegr) {
		if(isset($annesco)) {
			$nbseances_div = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".substr($anneesco,0,4).$w1."' AND event.week <= '".$w2."' AND brik.division = '".$listegr['groupe_nom']."' AND uevent.user_id=0 AND espevent.espace_id ='0'");
		}
		else {
			$nbseances_div = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".$_SESSION['week1']."' AND event.week <= '".$_SESSION['week2']."' AND brik.division = '".$listegr['groupe_nom']."' AND uevent.user_id=0 AND espevent.espace_id ='0'");
		}
		$nbseances = 0;
		foreach ($nbseances_div as $eventdiv) {
			if($eventdiv['duration']==5) { $duree = 0.5; }
			else { $duree = substr($eventdiv['duration'],0,1).'.'.substr($eventdiv['duration'],1,1); }
			$nbseances += $duree;
		}
		if($nbseances>0) {
			$tabGroupes[]=$listegr['groupe_nom'];
			$colorniv = substr($listegr['groupe_nom'],0,1);
			$color = mysqli_query($bdd,"SELECT bgcolor FROM ".$prefix."_edtnivcolor WHERE niveau = '".$colorniv."'");
			$color_info = mysqli_fetch_assoc($color);
			array_push($tabGrColors, $color_info['bgcolor']);
			array_push($tabNbPerGr, $nbseances);
			$groupes_stats = 1;
		}
		$bctab4 .= '<tr><td>'.$listediv['division'].'</td><td>'.$nbseances.'</td></tr>';
	}
	$bctab4 .= '</tbody></table>';
	if($groupes_stats==1) {
		?>
		<div id="wrapper" class="statcadre">
		<canvas id="chart_nbspergroupe" class="charts" width="600" height="300"></canvas>
		</div>
		<script type="text/javascript">
		(async function() {
		    
		    Chart.register(ChartDataLabels);
		    const labels = <?php echo json_encode($tabGroupes); ?>;
		    const data = {
		      labels: labels,
		      datasets: [{
		            label: '<?php echo HOURS;?> ',
		            data: <?php echo json_encode($tabNbPerGr); ?>,
		            backgroundColor: <?php echo json_encode($tabGrColors); ?>,
		            borderColor: <?php echo json_encode($tabGrColors); ?>,
		            borderWidth: 1
		        }]
		    };
		new Chart(
		    document.getElementById('chart_nbspergroupe'),
		    {
		    type: 'bar',
		    data: data,
		    options: {
		        plugins: {
		            title: {
		                display: true,
		                text: '<?php echo STATS_TIT21.STATS_DET1;?>'
		            },
		            legend: {
		    			display:false
		    		},
		            tooltip : {
		                mode: 'point',
		                intersect: false,
		              }  ,    
		            datalabels: {
		              display: true, // Affiche les étiquettes de données
		              color: '#878787', // Couleur du texte
		              align: 'center', // Alignement horizontal
		              anchor: 'center', // Alignement vertical
		              font: {
		                weight: 'bold', // Style de police
		              },
		              display: function(context) {
		                  return context.dataset.data[context.dataIndex] !== 0; // N'affiche pas si la valeur est égale à 0
		                },
		            },
		        },
		        responsive: true,
		        scales: {
		            yAxes: [{
		                ticks: {
		                    beginAtZero:true
		                }
		            }]
		        },
		      }
		    }
		);
		})();
		</script>

	<?php
	echo '<br> <br>'.$bctab4;
	$_SESSION['bctabs'] .= '<table></table>'.$bctab4;
	}
	
	//séances par domaine :
	$tabNbPerCode = array();
	$tabPercentPerCode = array();
	$tabCodesSeq = array();
	$tabCodesSeqColors = array();
	$ict=0;
	$dom_stats=0;
	$liste_codes = mysqli_query($bdd,"SELECT brik.code, edtcode.code_id, edtcode.bgcolor_code FROM ".$prefix."_edtbrik brik INNER JOIN ".$prefix."_edtcode edtcode ON brik.code = edtcode.code GROUP BY brik.code ORDER BY edtcode.code_id ASC");
	$bctab5 = '<table style="text-align:right;">
	<thead><tr><td colspan="3" style="text-align:center;">'.STATS_TIT22.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th>'.DOMAIN.'</th><th>'.NUMBER.'</th><th>%</th></tr></thead><tbody>';
	foreach($liste_codes as $code) {
		if(isset($annesco)) {
			$nbseances_niv = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".substr($anneesco,0,4).$w1."' AND event.week <= '".$w2."' AND brik.code = '".$code['code']."' AND uevent.user_id=0 AND espevent.espace_id ='0'");
		}
		else {
			$nbseances_niv = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".$_SESSION['week1']."' AND event.week <= '".$_SESSION['week2']."' AND brik.code = '".$code['code']."' AND uevent.user_id=0 AND espevent.espace_id ='0'");
		}
		$nbseances = 0;
		foreach ($nbseances_niv as $eventdiv) {
			if($eventdiv['duration']==5) { $duree = 0.5; }
			else { $duree = substr($eventdiv['duration'],0,1).'.'.substr($eventdiv['duration'],1,1); }
			$nbseances += $duree;
		}
		if($nbseances>0) {
			$tabCodesSeq[]=$code['code'];
			$tabCodesSeqColors[]=$code['bgcolor_code'];
			array_push($tabNbPerCode, $nbseances);
			$percent = round(($nbseances * 100 / $eventseance_nb),2);
			array_push($tabPercentPerCode, $percent);
			$ict++;
			$dom_stats=1;
			$bctab5 .= '<tr><td>'.$code['code'].'</td><td>'.$nbseances.'</td><td>'.number_format((float)$percent,2,'.','').'</td></tr>';
		}		
	}
	$bctab5 .= '<tr><td>'.TOTAL.'</td><td>'.$eventseance_nb.'</td><td>100.00</td></tr></tbody></table>';
	if($dom_stats==1) {
		?>
		<div id="wrapper" class="statcadre">
		<canvas id="chart_nbspercode" class="charts" width="600" height="300"></canvas>
		</div>
		<script type="text/javascript">
		(async function() {
		    
		    Chart.register(ChartDataLabels);
		    const labels = <?php echo json_encode($tabCodesSeq); ?>;
		    const data = {
		      labels: labels,
		      datasets: [{
		            label: '<?php echo HOURS;?> ',
		            data: <?php echo json_encode($tabNbPerCode); ?>,
		            backgroundColor: <?php echo json_encode($tabCodesSeqColors); ?>,
		            borderColor: <?php echo json_encode($tabCodesSeqColors); ?>,
		            borderWidth: 1
		        }]
		    };
		new Chart(
		    document.getElementById('chart_nbspercode'),
		    {
		    type: 'bar',
		    data: data,
		    options: {
		        plugins: {
		            title: {
		                display: true,
		                text: '<?php echo STATS_TIT22.STATS_DET1;?>'
		            },
		            legend: {
		    			display:false
		    		},
		            tooltip : {
		                mode: 'point',
		                intersect: false,
		              }  ,    
		            datalabels: {
		              display: true, // Affiche les étiquettes de données
		              color: '#878787', // Couleur du texte
		              align: 'center', // Alignement horizontal
		              anchor: 'center', // Alignement vertical
		              font: {
		                weight: 'bold', // Style de police
		              },
		              display: function(context) {
		                  return context.dataset.data[context.dataIndex] !== 0; // N'affiche pas si la valeur est égale à 0
		                },
		            },
		        },
		        responsive: true,
		        scales: {
		            yAxes: [{
		                ticks: {
		                    beginAtZero:true
		                }
		            }]
		        },
		      }
		    }
		);
		})();
		</script>
		
		<div id="wrapper" class="statcadre">
		<canvas id="chart_percentspercode" class="charts" width="400" height="400"></canvas>
		</div>
		<script type="text/javascript">
		(async function() {
		    
		    Chart.register(ChartDataLabels);
		    const labels = <?php echo json_encode($tabCodesSeq); ?>;
		    const data = {
		      labels: labels,
		      datasets: [{
		            data: <?php echo json_encode($tabPercentPerCode); ?>,
		            backgroundColor: <?php echo json_encode($tabCodesSeqColors); ?>,
		            borderColor: <?php echo json_encode($tabCodesSeqColors); ?>
		        }]
		    };
		new Chart(
		    document.getElementById('chart_percentspercode'),
		    {
		    type: 'pie',
		    data : data,
		    options: {
		        plugins: {
		            title: {
		                display: true,
		                text: '<?php echo STATS_TIT23;?>'
		            },
		            legend: {
		                display:true
		            },
		            tooltip : {
		                mode: 'point',
		                intersect: false,
		              }  ,    
		            datalabels: {
		              display: true, // Affiche les étiquettes de données
		              formatter: function(value, context) {
		                    return value + ' %'; // Ajoute le symbole "%" après la valeur
		                },
		              color: '#878787', // Couleur du texte
		              align: 'center', // Alignement horizontal
		              anchor: 'center', // Alignement vertical
		              font: {
		                weight: 'bold', // Style de police
		              },
		              display: function(context) {
		                  return context.dataset.data[context.dataIndex] !== 0; // N'affiche pas si la valeur est égale à 0
		                },
		            },
		        },
		        responsive: true,
		        scales: {
		            yAxes: [{
		                ticks: {
		                    beginAtZero:true
		                }
		            }]
		        },
		      }
		    }
		);
		})();
		</script>

		<?php
		echo '<br> <br>'.$bctab5;
		$_SESSION['bctabs'] .= '<table></table>'.$bctab5;

		//séances par domaine et par niveau :
		$tabNbPerCode = array();
		$tabPercentPerCode = array();
		$i=1;
		foreach($tabDebniveau as $divdeb) {
			$tabCode[$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($tabCodesSeq as $code) {
				if(isset($anneesco)) {
					$nbseances_niv = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".substr($anneesco,0,4).$w1."' AND event.week <= '".$w2."' AND brik.code = '".$code."' AND brik.division LIKE '".$divdeb."%' AND uevent.user_id=0 AND espevent.espace_id ='0'");
				}
				else {
					$nbseances_niv = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE brik.seance = 1 AND event.week >= '".$_SESSION['week1']."' AND event.week <= '".$_SESSION['week2']."' AND brik.code = '".$code."' AND brik.division LIKE '".$divdeb."%' AND uevent.user_id=0 AND espevent.espace_id ='0'");
				}
				$nbseances = 0;
				foreach ($nbseances_niv as $eventdiv) {
					if($eventdiv['duration']==5) { $duree = 0.5; }
					else { $duree = substr($eventdiv['duration'],0,1).'.'.substr($eventdiv['duration'],1,1); }
					$nbseances += $duree;
				}
				array_push($tabCode[$i], $nbseances);
			}
			$i++;
		}
		$bctab6 = '<table style="text-align:right;">
		<thead><tr><td colspan="'.($i+1).'" style="text-align:center;">'.STATS_TIT24.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th></th>';
		$nivi=0;
		foreach($tabDebniveau as $divdeb) {
			$bctab6 .= '<th>'.$tabNiveau[$nivi].'</th>';
			$nivi++;
		}
		$bctab6 .= '<th>'.TOTAL.'</th></tr></thead><tbody>';
		$w=0;
		foreach($tabCodesSeq as $code) {
			$bctab6 .= '<tr><td>'.$code.'</td>';
			$m=1;$tot=0;
			foreach($tabDebniveau as $divdeb) {
				$bctab6 .= '<td>'.$tabCode[$m][$w].'</td>';
				$tot+=$tabCode[$m][$w];
				$m++;				
			}
			$bctab6 .= '<td>'.$tot.'</td></tr>';
			$w++;
		}	
		$bctab6 .= '</tbody></table>';
		?>
		<div id="wrapper" class="statcadre">
		<canvas id="chart_nbspercodeperniv" class="charts" width="600" height="300"></canvas>
		</div>
		<script type="text/javascript">
		(async function() {
		    
		    Chart.register(ChartDataLabels);
		    const labels = <?php echo json_encode($tabCodesSeq); ?>;
		    const data = {
		      labels: labels,
		      datasets: [ 
		        <?php $i=1;$j=1;
		        foreach($tabDebniveau as $divdeb) { ?>
		        {
		            label: <?php echo json_encode($tabNiveau[$i-1]); ?>,
		            data: <?php echo json_encode($tabCode[$i]); ?>,
		            backgroundColor: '<?php echo $tabColor[$i] ?>',
		            borderColor: '<?php echo $tabColor[$i] ?>',
		            borderWidth: 1
		        } <?php if($j<count($tabDebniveau)) { echo ','; } $i++; $j++;} ?> ]
		    };
		new Chart(
		    document.getElementById('chart_nbspercodeperniv'),
		    {
		    type: 'bar',
		    data: data,
		    options: {
		        plugins: {
		            title: {
		                display: true,
		                text: '<?php echo STATS_TIT24.STATS_DET1;?>'
		            },
		            legend: {
		    			display:false
		    		},
		            tooltip : {
		                mode: 'point',
		                intersect: false,
		              }  ,    
		            datalabels: {
		              display: true, // Affiche les étiquettes de données
		              color: '#878787', // Couleur du texte
		              align: 'center', // Alignement horizontal
		              anchor: 'center', // Alignement vertical
		              font: {
		                weight: 'bold', // Style de police
		              },
		              display: function(context) {
		                  return context.dataset.data[context.dataIndex] !== 0; // N'affiche pas si la valeur est égale à 0
		                },
		            },
		        },
		        responsive: true,
		        scales: {
		            yAxes: [{
		                ticks: {
		                    beginAtZero:true
		                }
		            }]
		        },
		      }
		    }
		);
		})();
		</script>
	
	<?php
		echo '<br> <br>'.$bctab6;
		$_SESSION['bctabs'] .= '<table></table>'.$bctab6;
	}

	// séances par semaine 
	$tabWeeks = array();
	$tabNbPerWeek = array();
	
	if(isset($anneesco)) {
		if($acadexist=="fnc"||$acadexist=="civ") {
			$numSemaine = 07;
			$annee = substr($anneesco,0,4);
			$numSemaine2 = 50;
			$annee2 = substr($anneesco,0,4);
		}
		else {
			$numSemaine = 33;
			$annee = substr($anneesco,0,4);
			$numSemaine2 = 27;
			$annee2 = ($annee+1);
		}
	}
	else {
		$numSemaine = substr($_SESSION['week1'],4,2);
		$annee = substr($_SESSION['week1'],0,4);
		$numSemaine2 = substr($_SESSION['week2'],4,2);
		$annee2 = substr($_SESSION['week2'],0,4);
	}
	include('edt_weekday_convert.php');

	$bctab7 = '<table style="text-align:right;">
	<thead><tr><td colspan="3" style="text-align:center;">'.STATS_TIT25.'<br>'.FROM2.' '.$affday1.' '.TO2.' '.$affday2.'</td></tr><tr><th>'.WEEK.'</th><th>'.NUMBER.'</th></tr></thead><tbody>';
	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);
		$nbseances_niv = mysqli_query($bdd,"SELECT event.uid, event.duration FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id LEFT JOIN ".$prefix."_espaces_event esp ON esp.uid = event.uid LEFT JOIN ".$prefix."_userevent uevent ON uevent.uid = event.uid WHERE brik.seance = 1 AND week = '".$week."' AND uevent.user_id=0 AND esp.espace_id ='0'");
		$nbseances = 0;
		foreach ($nbseances_niv as $eventdiv) {
			if($eventdiv['duration']==5) { $duree = 0.5; }
			else { $duree = substr($eventdiv['duration'],0,1).'.'.substr($eventdiv['duration'],1,1); }
			$nbseances += $duree;
		}
		array_push($tabNbPerWeek, $nbseances);
		array_push($tabWeeks, $week);
		$bctab7 .= '<tr><td>'.$week.'</td><td>'.$nbseances.'</td></tr>';
	}
	$bctab7 .= '</tbody></table>';
	?>

	<div id="wrapper" class="statcadre">
	<canvas id="chart_nbsperweek" class="charts" width="600" height="300"></canvas>
	</div>
	<script type="text/javascript">
	(async function() {
	    
	    Chart.register(ChartDataLabels);
	    const labels = <?php echo json_encode($tabWeeks); ?>;
	    const data = {
	      labels: labels,
	      datasets: [{
	            label: '<?php echo HOURS;?>',
	            data: <?php echo json_encode($tabNbPerWeek); ?>,
	            backgroundColor: 'grey',
	            borderColor: 'grey',
	            borderWidth: 1
	        }]
	    };
	new Chart(
	    document.getElementById('chart_nbsperweek'),
	    {
	    type: 'bar',
	    data: data,
	    options: {
	        plugins: {
	            title: {
	                display: true,
	                text: '<?php echo STATS_TIT25.STATS_DET1;?>'
	            },
	            legend: {
	    			display:false
	    		},
	            tooltip : {
	                mode: 'point',
	                intersect: false,
	              }  ,    
	            datalabels: {
	              display: true, // Affiche les étiquettes de données
	              color: '#fff', // Couleur du texte
	              align: 'center', // Alignement horizontal
	              anchor: 'center', // Alignement vertical
	              font: {
	                weight: 'bold', // Style de police
	              },
	              display: function(context) {
	                  return context.dataset.data[context.dataIndex] !== 0; // N'affiche pas si la valeur est égale à 0
	                },
	            },
	        },
	        responsive: true,
	        scales: {
	            yAxes: [{
	                ticks: {
	                    beginAtZero:true
	                }
	            }]
	        },
	      }
	    }
	);
	})();
	</script>

<?php 
	echo '<br> <br>'.$bctab7;
	$_SESSION['bctabs'] .= '<table></table>'.$bctab7;
} 
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>';
?>