<?php
if(!isset($nbannees)) {
	// Nombre de semaines à calculer (et des demi-jounées fermées) :
	// La variable $dayhour pemet l'activation du comptage jours et heures
	$nb_week=0;
	$nb_close_week = 0;
	$nb_jclose=0;
	$nb_fullhour=0;
	$nb_briks_open=0;
	$nb_briks_open_noseq=0;
	$nb_briks_open_seance=0;
	$nb_briks_close=0;
	$ferme=0;
	$tabWeeks = array();
	$etude_classe = array('étude','séance');
	$nb_week_etude = array();
	$nb_week_classe = array();
	$tabday = array('di','lu','ma','me','je','ve','sa');
	foreach(new DatePeriod($jourP1date, new DateInterval('P7D') /* pas de 7 jours */, $jourP2date) as $dt) {
		//comptage du nombre de briques ouvertes et de briques fermées dans la semaine
		$briques_nb=0;
		$briques_noseq_nb=0;
		$briques_seance_nb=0;
		$briques_close_nb=0;
		if(isset($dayhour)) {
			$week = $dt->format('oW');
			array_push($tabWeeks,$week);
			$briques = 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 event.week = '".$week."' AND uevent.user_id=0 AND espevent.espace_id ='0'");
			foreach ($briques as $brique) {
				if($brique['duration']==5) { $duree = 0.5; }
				else { $duree = substr($brique['duration'],0,1).'.'.substr($brique['duration'],1,1); }
				$briques_nb += $duree;
				if($brique['close']==0&&$brique['seq_id']==0&&$brique['seance']==1) {
					$briques_noseq_nb += $duree;
				}
				if($brique['close']==0&&$brique['seance']==1) {
					$briques_seance_nb += $duree;
				}
				if($brique['close']==1) {
					$briques_close_nb += $duree;
				}
			}
			$nb_briks_open += ($briques_nb-$briques_close_nb);
			$nb_briks_open_noseq += $briques_noseq_nb;
			$nb_briks_open_seance += $briques_seance_nb;
			$nb_briks_close += $briques_close_nb;
			$nb_fullet = 0;
		}
		//comptages semaines, journées, heures
		$nb_week++;
		$datecheck=0;
		$date = $dt->format('Y-m-d');
		//on supprime l'addition si la semaine est fermée et on compte les demi-journées fermées
		$fermeture_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".$date."' AND statut = 0"));
		$ouverture_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".$date."' AND statut = 1"));
		if($fermeture_nb==1) {
			$ferme=1;
			
			if(isset($dayhour)) {
				$houropen=0;
				include('calweeknb_demij.php');
			}

			//gestion de semaines fermées
			for($k = 1; $k < 7; $k++) {
				$fermeture_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".date("Y-m-d", strtotime($date." +".$k." days"))."' AND statut = 0"));
				if($fermeture_nb==1) {
					$ferme++;
					if(isset($dayhour)) {
						$houropen=0;
						$datecheck=1;
						include('calweeknb_demij.php');
					}
				}
				else {
					$houropen=1;
					$datecheck=1;
					$daylc = date("D", strtotime($date." +".$k." days"));
					include('calweeknb_demij.php');
				}
			}
			if($ferme==7) {
				$nb_week--;
				$nb_close_week++;
				if(isset($dayhour)) { $nb_jclose_cumul=0; }
			}
			if(isset($dayhour)) { $nb_jclose+=$nb_jclose_cumul; }
		}
		if($ouverture_nb==1) {
			if(isset($dayhour)) {
				$houropen=1;
				$datecheck=0;
				$daylc = 'Mon';
				include('calweeknb_demij.php');
				for($k = 1; $k < 7; $k++) {
					$fermeture_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".date("Y-m-d", strtotime($date." +".$k." days"))."' AND statut = 0"));
					if($fermeture_nb==1) {
						$ferme++;
						$houropen=0;
						$datecheck=1;
						include('calweeknb_demij.php');
					}
					else {
						$houropen=1;
						$datecheck=1;
						$daylc = date("D", strtotime($date." +".$k." days"));
						include('calweeknb_demij.php');
					}
				}
				if(isset($nb_jclose_cumul)) { $nb_jclose+=$nb_jclose_cumul; }
			}
		}
		if(isset($dayhour)) {
			array_push($nb_week_etude,($nb_fullet-$briques_close_nb-$briques_seance_nb));
			array_push($nb_week_classe,$briques_seance_nb);
		}
	}
}

else if(isset($nbannees)) {
	$ferme=0;
	$nb_close_week=0;
	for($i=1;$i<($nbannees+1);$i++) {
		if($acadexist=="fnc"||$acadexist=="civ") {
			$_POST['fullyear'] = substr($day1,0,4);
		}
		else {
			$_POST['fullyear'] = substr($day1,0,4).(substr($day1,0,4)+1);
		}
		if(substr($_SESSION['nb_year'],1,1)=='p'&&$i==1) { }
		else {
			if($i>1) {
				if($acadexist=="fnc"||$acadexist=="civ") {
					$_POST['fullyear'] = ($_POST['fullyear']-1);
				}
				else {
					$_POST['fullyear'] = (substr($_POST['fullyear'],0,4)-1).(substr($_POST['fullyear'],0,4));
				}
			}
			require_once('caloffsco.php');
			include('calfullyear.php');
			if($acadexist=="fnc"||$acadexist=="civ") {
				$anneesco = $_POST['fullyear'];
			}
			else {
				$anneesco = substr($_POST['fullyear'],0,4).'/'.substr($_POST['fullyear'],4,4);
			}
			$day1 = date('Y-m-d', strtotime(str_replace("/","-",$_SESSION['datepickstats1'])));
			$day2 = date('Y-m-d', strtotime(str_replace("/","-",$_SESSION['datepickstats2'])));
			
			$dayP1 = strtotime($day1);
			$dayname1 = date('D',$dayP1);
			if($dayname1=="Mon") { 
				$jourP1 = date("Y-m-d", $dayP1);
			}
			else {
				$jourP1 = date("Y-m-d", strtotime("Previous Monday", $dayP1));
			}
			$jourP1date = new DateTime($jourP1);
			$dayP2 = strtotime($day2);
			$dayname2 = date('D',$dayP2);
			if($dayname2=="Mon") {
				$jourP2to = strtotime(date("Y-m-d",$dayP2) . " +7 day");
				$jourP2last = strtotime(date("Y-m-d",$dayP2) . " +6 day");
			}
			else {
				$jourP2to = strtotime(date("Y-m-d",strtotime("Previous Monday", $dayP2)) . " +7 day");
				$jourP2last = strtotime(date("Y-m-d",strtotime("Previous Monday", $dayP2)) . " +6 day");
			}
			$jourP2 = date('Y-m-d',$jourP2to);
			$jourP2dim = date('Y-m-d',$jourP2last);
			$jourP2date = new DateTime($jourP2);
			
			$eventdiv_nb = 0;
			$eventdivs = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_userevent uevent ON event.uid = uevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid INNER JOIN ".$prefix."_edtbrik brik ON event.brik_id = brik.brik_id WHERE (brik.close = 1 OR brik.seance = 1) AND event.week >= '".substr($anneesco,0,4).$w1."' AND event.week <= '".substr($anneesco,-4).substr($w2,-2)."' AND uevent.user_id=0 AND espevent.espace_id ='0'");
			foreach ($eventdivs as $eventdiv) {
				if($eventdiv['duration']==5) { $duree = 0.5; }
				else { $duree = substr($eventdiv['duration'],0,1).'.'.substr($eventdiv['duration'],1,1); }
				$eventdiv_nb += $duree;
			}
			
			// CALCULS
			$nb_week=0;
			$nb_jclose=0;
			$nb_fullhour=0;
			$nb_briks_open=0;
			$nb_briks_open_noseq=0;
			$nb_briks_open_seance=0;
			$nb_briks_close=0;
			${'tabWeeks'.$i} = array();
			${'etude_classe'.$i} = array('étude','séance');
			${'nb_week_etude'.$i} = array();
			${'nb_week_classe'.$i} = array();
			$tabday = array('di','lu','ma','me','je','ve','sa');
			foreach(new DatePeriod($jourP1date, new DateInterval('P7D') /* pas de 7 jours */, $jourP2date) as $dt) {
				$briques_nb=0;
				$briques_noseq_nb=0;
				$briques_seance_nb=0;
				$briques_close_nb=0;
				//comptage du nombre de briques ouvertes et de briques fermées dans la semaine
				$week = $dt->format('oW');
				array_push(${'tabWeeks'.$i},$week);
				$briques = 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 event.week = '".$week."' AND uevent.user_id=0 AND espevent.espace_id ='0'");
				foreach ($briques as $brique) {
					if($brique['duration']==5) { $duree = 0.5; }
					else { $duree = substr($brique['duration'],0,1).'.'.substr($brique['duration'],1,1); }
					$briques_nb += $duree;
					if($brique['close']==0&&$brique['seq_id']==0&&$brique['seance']==1) {
						$briques_noseq_nb += $duree;
					}
					if($brique['close']==0&&$brique['seance']==1) {
						$briques_seance_nb += $duree;
					}
					if($brique['close']==1) {
						$briques_close_nb += $duree;
					}
				}
				$nb_briks_open += ($briques_nb-$briques_close_nb);
				$nb_briks_open_noseq += $briques_noseq_nb;
				$nb_briks_open_seance += $briques_seance_nb;
				$nb_briks_close += $briques_close_nb;
				$nb_fullet = 0;
				//comptages semaines, journées, heures
				$nb_week++;
				$datecheck=0;
				$date = $dt->format('Y-m-d');
				//on supprime l'addition si la semaine est fermée et on compte les demi-journées fermées
				$fermeture_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".$date."' AND statut = 0"));
				$ouverture_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".$date."' AND statut = 1"));
				if($fermeture_nb==1) {
					$ferme=1;
					
					if(isset($dayhour)) {
						$houropen=0;
						include('calweeknb_demij.php');
					}
					
					//gestion de semaines fermées
					for($k = 1; $k < 7; $k++) {
						$fermeture_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".date("Y-m-d", strtotime($date." +".$k." days"))."' AND statut = 0"));
						if($fermeture_nb==1) {
							$ferme++;
							if(isset($dayhour)) {
								$houropen=0;
								$datecheck=1;
								include('calweeknb_demij.php');
							}
						}
						else {
							$houropen=1;
							$datecheck=1;
							include('calweeknb_demij.php');
						}
					}
					if($ferme==7) {
						$nb_week--;
						$nb_close_week++;
						if(isset($dayhour)) { $nb_jclose_cumul=0; }
					}
					//if(isset($nb_jclose_cumul)) { $nb_jclose+=$nb_jclose_cumul; }
					if(isset($dayhour)) { $nb_jclose+=$nb_jclose_cumul; }
				}
				if($ouverture_nb==1) {
					$houropen=1;
					$datecheck=0;
					include('calweeknb_demij.php');
					for($k = 1; $k < 7; $k++) {
						$fermeture_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_calendrier WHERE date = '".date("Y-m-d", strtotime($date." +".$k." days"))."' AND statut = 0"));
						if($fermeture_nb==1) {
							$ferme++;
							$houropen=0;
							$datecheck=1;
							include('calweeknb_demij.php');
						}
						else {
							$houropen=1;
							$datecheck=1;
							include('calweeknb_demij.php');
						}
					}
					if(isset($nb_jclose_cumul)) { $nb_jclose+=$nb_jclose_cumul; }
				}
				array_push(${'nb_week_etude'.$i},($nb_fullet-$briques_close_nb-$briques_seance_nb));
				array_push(${'nb_week_classe'.$i},$briques_seance_nb);
			}
			if($nb_week==0) {
				$no_annees=1;
			}
			else {
				$no_annees=0;
				array_push($tabAnnees, $anneesco);
				array_push($tabEtudeTotal,($nb_fullhour-$eventdiv_nb));
				array_push($tabEtudeWeek,round((($nb_fullhour-$eventdiv_nb)/$nb_week),1));
				array_push($tabSeancesTotal,$nb_briks_open_seance);
				array_push($tabSeancesWeek,round(($nb_briks_open_seance/$nb_week),1));
				array_push($nb_weeks,$nb_week);				
			}
		}
	}
}

?>