<?php

echo '<h2>'.FSTATS_TIT3.'</h2>';

if($grappes_check==1) {
	echo FSTATS_AVERT1;
}

include('freq_stats_form.php');

// Gestion des années et dates
$an = array();
$today = date('Y/m/d');
if($acadexist=="fnc"||$acadexist=="civ") {
	$back = $year.'/01/01';
	$next = $year.'/12/31';
}
else {
	$back = $year.'/08/01';
	$next = ($year+1).'/08/01';
}
if($today>$back&&$today<$next) { 
	if($acadexist=="fnc"||$acadexist=="civ") {
		$an[1] = $year; 
		$an[2] = ($year-1); 
	}
	else {
		$an[1] = $year.'/'.($year+1); 
		$an[2] = ($year-1).'/'.($year); 
	}
	if(isset($_SESSION['nb_year'])) {
		if($_SESSION['nb_year']!="2a") {
			for($i=3;$i<(substr($_SESSION['nb_year'],0,1)+1);$i++) {
				if($acadexist=="fnc"||$acadexist=="civ") {
					$an[$i] = ($year-$i+1);
				}
				else {
					$an[$i] = ($year-$i+1).'/'.($year-$i+2);
				}
			}
		}
	}
}
else { 
	if($acadexist=="fnc"||$acadexist=="civ") {
		$an[1] = ($year-1); 
		$an[2] = ($year-2); 
	}
	else {
		$an[1] = ($year-1).'/'.$year; 
		$an[2] = ($year-2).'/'.($year-1); 
	}
	if(isset($_SESSION['nb_year'])) {
		if($_SESSION['nb_year']!="2a") {
			for($i=3;$i<(substr($_SESSION['nb_year'],0,1)+1);$i++) {
				if($acadexist=="fnc"||$acadexist=="civ") {
					$an[$i] = ($year-$i);
				}
				else {
					$an[$i] = ($year-$i).'/'.($year-$i+1);
				}
			}
		}
	}
}

// Tableau du début de période
$firstday = array();
if((substr($affday3,3,2).'/'.substr($affday3,0,2)<'12/31'&&substr($affday3,3,2).'/'.substr($affday3,0,2)>'08/01')||$acadexist=="fnc"||$acadexist=="civ") {
	$firstday[1] = substr($an[1],0,4).'/'.substr($affday3,3,2).'/'.substr($affday3,0,2);
	$firstday[2] = substr($an[2],0,4).'/'.substr($affday3,3,2).'/'.substr($affday3,0,2);
	if(isset($_SESSION['nb_year'])) {
		if($_SESSION['nb_year']!="2a") {
			for($i=3;$i<(substr($_SESSION['nb_year'],0,1)+1);$i++) {
				$firstday[$i] = substr($an[$i],0,4).'/'.substr($affday3,3,2).'/'.substr($affday3,0,2);
			}
		}
	}
}
else {
	$firstday[1] = substr($an[1],5,4).'/'.substr($affday3,3,2).'/'.substr($affday3,0,2);
	$firstday[2] = substr($an[2],5,4).'/'.substr($affday3,3,2).'/'.substr($affday3,0,2);
	if(isset($_SESSION['nb_year'])) {
		if($_SESSION['nb_year']!="2a") {
			for($i=3;$i<(substr($_SESSION['nb_year'],0,1)+1);$i++) {
				$firstday[$i] = substr($an[$i],5,4).'/'.substr($affday3,3,2).'/'.substr($affday3,0,2);
			}
		}
	}
}
// Tableau de fin de période
$lastday = array();
if((substr($affday4,3,2).'/'.substr($affday4,0,2)<'12/31'&&substr($affday4,3,2).'/'.substr($affday4,0,2)>'08/01')||$acadexist=="fnc"||$acadexist=="civ") {
	$lastday[1] = substr($an[1],0,4).'/'.substr($affday4,3,2).'/'.substr($affday4,0,2);
	$lastday[2] = substr($an[2],0,4).'/'.substr($affday4,3,2).'/'.substr($affday4,0,2);
	if(isset($_SESSION['nb_year'])) {
		if($_SESSION['nb_year']!="2a") {
			for($i=3;$i<(substr($_SESSION['nb_year'],0,1)+1);$i++) {
				$lastday[$i] = substr($an[$i],0,4).'/'.substr($affday4,3,2).'/'.substr($affday4,0,2);
			}
		}
	}
}
else {
	$lastday[1] = substr($an[1],5,4).'/'.substr($affday4,3,2).'/'.substr($affday4,0,2);
	$lastday[2] = substr($an[2],5,4).'/'.substr($affday4,3,2).'/'.substr($affday4,0,2);
	if(isset($_SESSION['nb_year'])) {
		if($_SESSION['nb_year']!="2a") {
			for($i=3;$i<(substr($_SESSION['nb_year'],0,1)+1);$i++) {
				$lastday[$i] = substr($an[$i],5,4).'/'.substr($affday4,3,2).'/'.substr($affday4,0,2);
			}
		}
	}
}

echo '<h2>'.FROM21.' '.$affday3.' '.TO2.' '.$affday4.' ('.FROM.' ';
if(isset($an[5])) { echo $an[5]; }
else if(isset($an[4])) { echo $an[4]; }
else if(isset($an[3])) { echo $an[3]; }
else { echo $an[2]; }
echo ' '.TO.' ';
if(substr($_SESSION['nb_year'],1,1)=='a') { echo $an[1].')</h2>'; }
else { echo $an[2].')</h2>'; }

// Gestion des semaines pour plusieurs années
if(isset($an[5])) { $i=5; }
else if(isset($an[4])) { $i=4; }
else if(isset($an[3])) { $i=3; }
else { $i=2; }
$jourP1date_evol = array();
$jourP2date_evol = array();
$jourP1_evol = array();
if(substr($_SESSION['nb_year'],1,1)=='a') { $nb_sans=0; } else { $nb_sans=1; }
for($i;$i>$nb_sans;$i--) {
	$dayP1 = strtotime($firstday[$i]);
	$dayname1 = date('D',$dayP1);
	if($dayname1=="Mon") { 
		$jourP1_evol[$i] = date("Y-m-d", $dayP1);
	}
	else {
		$jourP1_evol[$i] = date("Y-m-d", strtotime("Previous Monday", $dayP1));
	}
	$jourP1date_evol[$i] = new DateTime($jourP1_evol[$i]);
	$dayP2 = strtotime($lastday[$i]);
	$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_evol[$i] = new DateTime($jourP2);
}

// Calcul initial
$i=1;
$freq_nb = array();
foreach($an as $eachyear) {
	if(substr($_SESSION['nb_year'],1,1)=='p'&&$eachyear[$i]==0) { $i++; }
	else {
		$freq = mysqli_query($bdd,"SELECT freq.freq_id FROM ".$prefix."_freq freq WHERE freq.date_freq >= '".$firstday[$i]."' AND freq.date_freq <= '".$lastday[$i]."' ".$filtre_activs."");
		$freq_nb[$i] = mysqli_num_rows($freq);
		if($grappes_check==1) {
			$elegrap_nb = mysqli_query($bdd,"SELECT nombre FROM ".$prefix."_freqgrappes WHERE date_freq >= '".$firstday[$i]."' AND date_freq <= '".$lastday[$i]."' AND passage = '0' ".$filtre_activs."");
			while($elegrap = mysqli_fetch_assoc($elegrap_nb)) {
				$freq_nb[$i] += $elegrap['nombre'];
			}
		}
	$i++;
	}
}
$totalfreq_nb = 0;
$i=1;
foreach($freq_nb as $addfreq) {
	$totalfreq_nb += $addfreq;
	if(substr($_SESSION['nb_year'],1,1)=='p') { echo '<p><b>'.$an[$i+1].' : '; }
	else { echo '<p><b>'.$an[$i].' : '; }
	if($addfreq==0) { echo FREQ_NOVISIT1.'.</b></p>'; }
	else { 
		echo $addfreq.' '.FREQ_VISIT;
		if($addfreq>1) { echo PLURIEL; }
		echo '.</b></p>'; }
	$i++;
}
//récupération des niveaux et couleurs par niveau
include('freq_stats_nivcolors.php');

if($totalfreq_nb==0) {
	echo FREQ_NOVISIT2;
}
else {
	echo '<p><b>'.FSTATS_TIT2.' ';
	echo $totalfreq_nb;
	echo ' '.FREQ_VISIT.PLURIEL.'.</b></p>';

	echo '<form name="statscform" method="GET" action="">
	<input type="hidden" name="page" value="stats" />
	<input type="hidden" name="stat" value="annees" />
	<div class="box"><select name="s" id="statschoix">';
		$stcode = array('zero','nbperyearperniv','nbperyearperdiv','propperyearperniv','propperyearperdiv','nbperweekperyear','evolpercren','evolexclus','all');
		$stname = $stname2;
		for($sno=0;$sno<count($stcode);$sno++) {
			if($stcode[$i]==5) {
				$excluopt_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_excluopt'"));
				if($excluopt_info['valeur']==1) {
					echo '<option value="'.$stcode[$sno].'"';
					if(isset($_GET['s'])) {
						if($_GET['s']==$stcode[$sno]) {
							echo ' selected ';
						}
					}
					echo '>'.$stname[$sno].'</option>';
				}
			}
			else {
				echo '<option value="'.$stcode[$sno].'"';
				if(isset($_GET['s'])) {
					if($_GET['s']==$stcode[$sno]) {
						echo ' selected ';
					}
				}
				echo '>'.$stname[$sno].'</option>';
			}
		}
	echo '</select></div> <input type="submit" class="bouton boutfreq" value="'.CHOOSE.'" />
	</form>';

	echo '</article><article id="artleft" class="freqsep">';

	include('stats/freq_stats_evol_div.php');

} ?>

	