<?php
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][2]!=2) {
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="./index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		?><style type="text/css">
		#mdt4 { background-color: #fff; }
		#mdt4 td { border: 3px #E77015 solid; }
		#mdt3 td { border: 0; }
		</style><?php
		// Gestion du calendrier pour l'année en cours
		require_once('./db/proc/caloffsco.php');
		//vérification et cas échéant suppression des événements n-6 et nettoyage des briques :
		$year6 = date("Y")-5;
		if(isset($_POST['supprevent'])) {
			if(isset($_POST['input_save'])) {
				$_SESSION['save_auto'] = '2';
				include('./db/backup.php');
			}
			if($acadexist=="fnc"||$acadexist=="civ") {
				$checkvide = mysqli_query($bdd, "SELECT uid FROM ".$prefix."_edtevent WHERE week < '".$year6."01'");
				mysqli_query($bdd,"DELETE FROM ".$prefix."_calendrier WHERE LEFT(date,7) < '".$year6."-01'");
			}
			else {
				$checkvide = mysqli_query($bdd, "SELECT uid FROM ".$prefix."_edtevent WHERE week < '".($year6-1)."33'");
				mysqli_query($bdd,"DELETE FROM ".$prefix."_calendrier WHERE LEFT(date,7) < '".($year6-1)."-08'");
			}
			foreach($checkvide as $checkf) {
				mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$checkf['uid']."'");
				mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$checkf['uid']."'");
				mysqli_query($bdd,"DELETE FROM ".$prefix."_edtevent WHERE uid = '".$checkf['uid']."'");
			}
			$brik_select = "SELECT edtbrik.brik_id, edtbrik.activ_brik FROM ".$prefix."_edtbrik edtbrik 
			LEFT OUTER JOIN ".$prefix."_edtevent edtevent 
			ON edtevent.brik_id = edtbrik.brik_id
			WHERE edtevent.brik_id IS NULL AND edtbrik.activ_brik = 2";
			$brik = mysqli_query($bdd,$brik_select);
			foreach($brik as $brikdel) {
				mysqli_query($bdd,"DELETE FROM ".$prefix."_edtbrik WHERE brik_id = '".$brikdel['brik_id']."'");
			}
			$seq_select = "SELECT seq.seq_id, seq.seq_actif FROM ".$prefix."_sequences seq 
			LEFT OUTER JOIN ".$prefix."_edtevent edtevent 
			ON edtevent.seq_id = seq.seq_id
			WHERE edtevent.seq_id IS NULL AND seq.seq_actif = 0";
			$seq = mysqli_query($bdd,$seq_select);
			foreach($seq as $seqdel) {
				mysqli_query($bdd,"DELETE FROM ".$prefix."_sequences WHERE seq_id = '".$seqdel['seq_id']."'");
				mysqli_query($bdd,"DELETE FROM ".$prefix."_seqsav WHERE seq_id = '".$seqdel['seq_id']."'");
				//sélection et suppression des documents
				$imgdel = mysqli_query($bdd,"SELECT * FROM ".$prefix."_seqdocs WHERE seq_id = '".$seqdel['seq_id']."'");
				$rep_img = 'seqfichiers/'.$prefix;
				$rep_img_open = opendir($rep_img);
				while($imgdel_infos=mysqli_fetch_assoc($imgdel)) {
					$chemin = $rep_img."/".$imgdel_infos['docurl'];
					unlink($chemin);
				}
				closedir($rep_img_open);
				mysqli_query($bdd,"DELETE FROM ".$prefix."_seqdocs WHERE seq_id = '".$seqdel['seq_id']."'");
			}
			echo '<p>'.EDTC_ELEV1.'</p>';
		}
		if($acadexist=="fnc"||$acadexist=="civ") {
			$event6_clean = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent WHERE week < '".$year6."01'");	
		}
		else {
			$event6_clean = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent WHERE week < '".($year6-1)."33'");
		}
		if(mysqli_num_rows($event6_clean)>0) {
			echo '<p class="signal maxwidth">'.EDTC_ELEV2.substr($rentree,8,2).'/'.substr($rentree,5,2).'/'.substr($rentree,0,4).EDTC_ELEV3.mysqli_num_rows($event6_clean).EDTC_ELEV4.'</p><form action="" method="POST" name="supprevent"><input type="checkbox" name="input_save" id="input_save" checked /><label for="input_save"> '.DATAC_VIDE5.'</label><br><span class="ecart"><input class="bouton boutedt" type="submit" value="'.CONFIRM.'" name="supprevent" /></span>';
			$result_tables = mysqli_query($bdd,"SHOW TABLE STATUS LIKE '".$prefix."_%'");
			$_SESSION['nb_tables'] = mysqli_num_rows($result_tables);
			if($result_tables && $result_tables->num_rows) {
				while($obj_table = mysqli_fetch_assoc($result_tables)) {
					echo '<input type="hidden" id="'.$obj_table['Name'].'" name="'.$obj_table['Name'].'" checked />';
				}
			}
			echo '<input type="hidden" name="hidden_save" id="input_save" checked />';
			echo '</form>';
		}
		
		echo '<div class="edt_padding"><h1>'.EDTNAV3.'</h1>
		<h2>'.EDTC_ELEV5.'</h2>
		<form method="GET" action="" class="padd2">
		<input type="hidden" name="page" value="eleves" />
		'.DATAEXP1.' <div class="box"><select name="division" value=""><option></option>';
		$liste_division = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves WHERE categ = '1' GROUP BY division ORDER BY division ASC");
		while($liste = mysqli_fetch_assoc($liste_division)) {
			echo '<option';
			if(isset($_GET['division'])) {
				if($_GET['division']==$liste['division']) {
					echo ' selected ';
				}
			}
			echo '>'.$liste['division'].'</option>';
		}
		$liste_groupe = mysqli_query($bdd,"SELECT groupe_nom FROM ".$prefix."_groupes ORDER BY groupe_nom ASC");
		while($liste = mysqli_fetch_assoc($liste_groupe)) {
			echo '<option';
			if(isset($_GET['division'])) {
				if($_GET['division']==$liste['groupe_nom']) {
					echo ' selected ';
				}
			}
			echo '>'.$liste['groupe_nom'].'</option>';
		}
		echo '</select></div>&nbsp; 
		<input class="bouton boutedt" type="submit" value="'.CHOOSE.'" />
		</form>';

		if(!isset($_GET['division'])) {
			
			// Affichage des séances pour toutes les divisions
			$eventdiv = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent event
			INNER JOIN ".$prefix."_edtbrik brik
			ON event.brik_id = brik.brik_id
			WHERE brik.division != '' AND event.week >= $renweek");
			$eventdiv_nball = mysqli_num_rows($eventdiv);
			
			$div = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves WHERE categ = '1' GROUP BY division ORDER BY division");
			$groupes = mysqli_query($bdd,"SELECT groupe_nom FROM ".$prefix."_groupes");
			
			if($eventdiv_nball==0) {
				echo EDTC_ELEV6;
			}
			else {
				$edt_suivi = '<h2>'.SEAN4.'</h2>';
				$edt_suivi_pdfnb = '<h2>'.SEAN4.'</h2>';
				$edt_suivi_pdfcolor = '<h2>'.SEAN4.'</h2>';
				foreach($div as $division) {
					$diveval = $division['division'];
					$anneesuiv = 'now';
					include('db/proc/edt_suivi_seances.php');
				}
				foreach($groupes as $division) {
					$diveval = $division['groupe_nom'];
					$anneesuiv = 'now';
					include('db/proc/edt_suivi_seances.php');
				}
				echo $edt_suivi;
				$_SESSION['edt_suivi_pdfnb'] = $edt_suivi_pdfnb;
				$_SESSION['edt_suivi_pdfcolor'] = $edt_suivi_pdfcolor;
			}
			if($eventdiv_nball>0) {
				echo '<p class="center"><a onclick="submitpdf();" id="preview-pdf"><img src="img/pdf-icon.png" title="'.PDF_EXTRACT.PDF_COL.'"/></a> <a onclick="submitpdfnb();" id="preview-pdfnb"><img src="img/pdf-icon-nb.png" title="'.PDF_EXTRACT.PDF_NOI.'"/></a></p>';
				if(isset($_POST['havepdf'])) {
					include('./include/pdf_loaders/edt_suivi.php');
				}
				if(isset($_POST['havepdfnb'])) {
					include('./include/pdf_loaders/edt_suivi_nb.php');
				}
			    echo '<form id="loadpdf" action="./include/pdf_loaders/edt_suivi.php" method="post"><input type="hidden" name="havepdf" value="1" /></form>';
			    echo '<script type="text/javascript">
			    function submitpdf(){
			    	$("#loadpdf").submit()
			    }
			    </script>';
			    echo '<form id="loadpdfnb" action="./include/pdf_loaders/edt_suivi_nb.php" method="post"><input type="hidden" name="havepdfnb" value="1" /></form>';
			    echo '<script type="text/javascript">
			    function submitpdfnb(){
			    	$("#loadpdfnb").submit()
			    }
			    </script>';
			}
			// Années précédentes
			$lastyears_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent WHERE week < $renweek"));
			if($lastyears_nb>0) {
				echo '<h2 id="years">'.STATS_YEAR11.'</h2>';
				echo '<form method="POST" name="lastyear" action="#years" class="padd2">'.STATS_YEAR2.' <div class="box"><select name="lastyear"><option></option>';
				for($i=2040;$i>2015;$i--) {
					if(substr($rentree,0,4)!=$i) {
						$checkyear = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent WHERE dtstart > '".$i."0801T000000' AND dtstart < '".($i+1)."0731T240000'");
						$checkyear_nb = mysqli_num_rows($checkyear);
						if($checkyear_nb!=0) {
							if($acadexist=="fnc"||$acadexist=='civ') {
								echo '<option value="'.$i.'">'.$i.'</option>';
							}
							else {
								echo '<option value="'.$i.''.($i+1).'">'.$i.'/'.($i+1).'</option>';
							}
						}
					}
				}
				echo '</select></div> <input class="bouton boutedt" type="submit" name="choixyear" value="'.CONSULT.'" /></form>';
				if(isset($_POST['choixyear'])) {
					if($acadexist=="fnc"||$acadexist=='civ') {
						$i1=$_POST['lastyear'];
					}
					else {
						$i1=substr($_POST['lastyear'],0,4);
						$i2=substr($_POST['lastyear'],4,4);
					}
					if(isset(${$codepays.'_all_'.$_POST['lastyear'].'_cles'}[7])) {
						$date1=${$codepays.'_all_'.$_POST['lastyear'].'_cles'}[7];
						$date2=${$codepays.'_all_'.$_POST['lastyear'].'_cles'}[8];
					}
					else {
						if($acadexist=="La Réunion") {
							$date1=substr($_POST['lastyear'],0,4).'0815T000000'; 
							$date2=substr($_POST['lastyear'],4,4).'0707T240000';
						}
						else if($acadexist=="swi") {
							$date1=substr($_POST['lastyear'],0,4).'0819T000000'; 
							$date2=substr($_POST['lastyear'],4,4).'0629T240000';
						}
						else { 
							$date1=substr($_POST['lastyear'],0,4).'0901T000000'; 
							$date2=substr($_POST['lastyear'],4,4).'0707T240000'; 
						}
					}
					// Affichage des séances pour toutes les divisions
					$eventdiv = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent
					WHERE seq_id != 0 AND dtstart > '$date1' AND dtstart < '$date2'");
					$eventdiv_nb = mysqli_num_rows($eventdiv);
					
					$div = mysqli_query($bdd,"SELECT division FROM ".$prefix."_edtbrik GROUP BY division ORDER BY division");
					$groupes = mysqli_query($bdd,"SELECT groupe_nom FROM ".$prefix."_groupes");
					if($eventdiv_nb==0) {
						echo EDTC_ELEV6;
					}
					else {
						if($acadexist=="fnc"||$acadexist=='civ') {
							$infoannees = '('.$i1.')';
						}
						else {
							$infoannees = '('.$i1.'/'.$i2.')';
						}
						$edt_suivi = '<h3>'.SEAN4.' '.$infoannees.'</h3>';
						$edt_suivi_pdfnb = '<h3>'.SEAN4.' '.$infoannees.'</h3>';
						$edt_suivi_pdfcolor = '<h3>'.SEAN4.' '.$infoannees.'</h3>';
						foreach($div as $division) {
							if($division['division']!='') {
								$diveval = $division['division'];
								$anneesuiv = 'past';
								include('db/proc/edt_suivi_seances.php'); 
							}
						}
						foreach($groupes as $division) {
							if($division['groupe_nom']!='') {
								$diveval = $division['groupe_nom'];
								$anneesuiv = 'past';
								include('db/proc/edt_suivi_seances.php');
							}
						}
						echo $edt_suivi;
						$_SESSION['edt_suivi_pdf_pastnb'] = $edt_suivi_pdfnb;
						$_SESSION['edt_suivi_pdf_pastcolor'] = $edt_suivi_pdfcolor;
						if($acadexist=="fnc"||$acadexist=='civ') {
							$_SESSION['year_suivi'] = $i1;
						}
						else {
							$_SESSION['year_suivi'] = $i1.'-'.$i2;
						}
					}
				}
			}
			if(@$eventdiv_nb>0) {
				echo '<p class="center"><a onclick="submitpdfpast();" id="preview-pdf-past"><img src="img/pdf-icon.png" title="'.PDF_EXTRACT.PDF_COL.'"/></a> <a onclick="submitpdfpastnb();" id="preview-pdf-pastnb"><img src="img/pdf-icon-nb.png" title="'.PDF_EXTRACT.PDF_NOI.'"/></a></p>';
				if(isset($_POST['havepdf_past'])) {
					include('./include/pdf_loaders/edt_suivi_past.php');
				}
			    echo '<form id="loadpdf_past" action="./include/pdf_loaders/edt_suivi_past.php" method="post"><input type="hidden" name="havepdf_past" value="1" /></form>';

			    echo '<script type="text/javascript">
			    function submitpdfpast(){
			    	$("#loadpdf_past").submit()
			    }
			    </script>';
			    if(isset($_POST['havepdf_pastnb'])) {
					include('./include/pdf_loaders/edt_suivi_past_nb.php');
				}
			    echo '<form id="loadpdf_pastnb" action="./include/pdf_loaders/edt_suivi_past_nb.php" method="post"><input type="hidden" name="havepdf_pastnb" value="1" /></form>';
			    echo '<script type="text/javascript">
			    function submitpdfpastnb(){
			    	$("#loadpdf_pastnb").submit()
			    }
			    </script>';
			}
		}
		echo '</div>';
		if(isset($_GET['division'])) {
			$diveval = $_GET['division'];
			$_SESSION['diveval'] = $diveval;
			//groupe ou division :
			$divgr = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves WHERE division = '$diveval'");
			$divgr_nb = mysqli_num_rows($divgr);
			if($divgr_nb==0) {
				$nbeleves_diveval_select = "SELECT * FROM ".$prefix."_eleves eleves
				INNER JOIN ".$prefix."_groupele groupele
				ON groupele.eleves_id = eleves.eleves_id
				INNER JOIN ".$prefix."_groupes groupes
				ON groupele.groupe_id = groupes.groupe_id 
				WHERE groupe_nom = '$diveval'";
				$nbeleves_diveval = mysqli_query($bdd,$nbeleves_diveval_select);
				$nbeleves = mysqli_num_rows($nbeleves_diveval);
				$divougr = "groupe";
			}
			else {
				$nbeleves_diveval = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE division = '$diveval'");
				$nbeleves = mysqli_num_rows($nbeleves_diveval);
				$divougr = "division";
			}
			//gestion des affichages :
			$deb_diveval = substr("$diveval", 0, 1);
			echo '<h2>'.$diveval.' ('.$nbeleves.' '.ELEVS1.')</h2>';
			echo '<form method="POST" name="upeval" action="">';
			include('db/proc/edt_suivi_div.php');	
			echo '</form>';
			echo '</div>';
		}
	}
}
else {
	include('include/noaccess.php');
	echo '<br><a href="./index.php>'.ACCUEIL.'</a></p>';
}
?>