<?php require_once(dirname(__DIR__, 1) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][0]!="2")
	{
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="../index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		?><style type="text/css">
		#mev3 { background-color: #fff; }
		#mev3 td { border: 3px #089608 solid; }
		#mev2 td { border:0; }
		</style><?php
		echo '<h1>'.EVALC13.'</h1>';
		$verif_tabseq = mysqli_query($bdd,"SELECT * FROM ".$prefix."_sequences");
		$nb_seq = mysqli_num_rows($verif_tabseq);
		if($nb_seq==0) {
			echo '<h2>'.SEQNO.' <a href="edit.php?page=creation">'.SEQCREA.'</a>.</h2>';
		}
		else {
		if(isset($_POST['seqchoix'])||isset($_SESSION['divevals'])||isset($_SESSION['seqch'])) {
			if(isset($_POST['seqchoix'])) {
				$diveval = $_POST['division'];
				$seqch = $_POST['seqch'];
			}
			else {
				$diveval = $_SESSION['divevals'];
				$seqch = $_SESSION['seqch'];
			}
			$_SESSION['divevals'] = $diveval;
			$_SESSION['seqch'] = $seqch;
		}
		echo '<div class="div-loading"><img class="ajax-loading" src="img/div/loadingImage.gif" alt="Loading" /></div>';
		echo '<h2>'.EVALC14.'</h2>
		<form method="POST" action="eval.php?page=sequence" class="padd2">
			<table class="noborder">
			<tr><td>'.DIV.DOTS.'</td><td><div class="box"><select name="division" id="division" onchange=\'div()\'><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($_SESSION['divevals'])) {
						if($_SESSION['divevals']==$liste['division']) {
							echo ' selected ';
						}
					}
					echo '>'.$liste['division'].'</option>';
				}
				$liste_groupe = mysqli_query($bdd,"SELECT groupe_nom FROM ".$prefix."_groupes WHERE groupe_actif = 1 ORDER BY groupe_nom ASC");
				while($liste = mysqli_fetch_assoc($liste_groupe)) {
					echo '<option';
					if(isset($_SESSION['divevals'])) {
						if($_SESSION['divevals']==$liste['groupe_nom']) {
							echo ' selected ';
						}
					}
					echo '>'.$liste['groupe_nom'].'</option>';
				}
			echo '</select></div></td></tr>
				<tr><td>'.SEQU2.'</td><td><div id=\'seqch\' style=\'display:inline\'>
				<div class="box"><select name=\'seqch\'>';
					if(isset($_SESSION['divevals'])) {
						$seqniv = substr($_SESSION['divevals'], 0, 1);
						if($seqniv== "6"||$seqniv=="5"||$seqniv=="4"||$seqniv=="3"||$seqniv=="2"||$seqniv=="1"||$seqniv=="T") {
							$liste_seq = mysqli_query($bdd,"SELECT seq_id, seq_nom, progression FROM ".$prefix."_sequences WHERE niveau LIKE '".$seqniv."%' ORDER BY progression ASC, seq_nom ASC");
						}
						else {
							$liste_seq = mysqli_query($bdd,"SELECT seq_id, seq_nom, progression FROM ".$prefix."_sequences WHERE niveau LIKE 'a%' ORDER BY progression ASC, seq_nom ASC");
						}
						while($liste = mysqli_fetch_assoc($liste_seq)) {
							echo '<option value="'.$liste['seq_id'].'"';
							if(isset($_SESSION['seqch'])) {
								if($_SESSION['seqch']==$liste['seq_id']) {
									echo ' selected ';
								}
							}
							echo '>'; 
							if($liste['progression']!="1000") { echo $liste['progression'].'. '; }
							echo $liste['seq_nom'];
							echo '</option>';
						}
					}
				echo '</select></div>
				</div></td></tr>
			<tr><td colspan="2" style="text-align:right;"><input class="bouton boutev ajax-load" type="submit" value="'.EVALC15.'" name="seqchoix" /></td></tr>
			</table></form>';
		
			if(isset($_POST['seqchoix'])||isset($_SESSION['divevals'])||isset($_SESSION['seqch'])) {
				$_SESSION['divevals'] = $diveval;
				$_SESSION['seqch'] = $seqch;
				$seq = mysqli_query($bdd,"SELECT * FROM ".$prefix."_sequences WHERE seq_id = '$seqch'");
				$seq_info = mysqli_fetch_array($seq);
				//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 '</article><article id="artleft">';
				echo '<h2>'.$diveval.' ('.$nbeleves.' '.ELEVS1.') - '.SEQU2.' '.$seq_info['seq_nom'].'</h2>';
				include('db/proc/eval_select_sequp.php');	
				//sortie PDF
				$_SESSION['eval_pdf'] = "sequence";
				echo '<p><a onclick="submitpdf();" id="preview-pdf"><img src="img/pdf-icon.png" title="'.PDF_EXTRACT.'"/></a>';
				// Envoi par mail
				if(@$_SESSION['auth_mail']==1) {
					if(isset($_POST['mail_send'])) {
						include('./db/proc/mailing/mailing_eval.php');
					}
					echo '&nbsp; &nbsp; <a href="javascript:;clearTimeout(timer);" id="mail" onmousedown="toggleDiv(\'divmail\');"><img src="img/div/mail.png" title="'.EVALC4.'" name="noprint" class="printer" /></a>';
					echo '<div name="noprint" id="divmail" style="display:none;">
					<br>'.EVALC5.'
					<form method="POST" action="" name="mailing">';
					$mails = mysqli_query($bdd,"SELECT * FROM ".$prefix."_user WHERE mail <> ''");
					if(mysqli_num_rows($mails)>0) {
						while($mail=mysqli_fetch_array($mails)) {
							echo '<input type="checkbox" name="mail[]" id="'.$mail['mail'].'" value="'.$mail['mail'].'" checked /><label for="'.$mail['mail'].'"> '.$mail['login'].' : '.$mail['mail'].'</label><br>';
						}
					}
					echo MAILOTH.' (1)'.DOTS.' <input type="email" name="mail1" /><br>
					'.MAILOTH.' (2)'.DOTS.' <input type="email" name="mail2" /><br>
					<input type="hidden" name="concerned" value="'.$diveval.' - '.$seq_info['seq_nom'].'" />
					<input class="bouton boutev paddvalid1" type="submit" name="mail_send" value="'.SEND.'" /></form>';
					echo '</div>';
				}
				if(isset($_POST['havepdf'])) {
					include('./include/pdf_loaders/evaluations.php');
				}
			    echo '<form id="loadpdf" action="./include/pdf_loaders/evaluations.php" method="post"><input type="hidden" name="havepdf" value="1" /></form>';

			    echo '<script type="text/javascript">
			    function submitpdf(){
			    	$("#loadpdf").submit()
			    }
			    </script></p>';
			}
		}
	}
}
else {
	include('include/noaccess.php');
	echo '<br><a href="../index.php">'.ACCUEIL.'</a></p>';
}
?>
