<?php
if($_POST['ordredeb']!=$_POST['ordrefin']) {
	if($_POST['ordrefin']=="div") {
		$ordre = "ORDER BY eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
	}
	else if($_POST['ordrefin']=="nom") {
		$ordre = "ORDER BY eleves.nom ASC, eleves.prenom ASC";
	}
	else if($_POST['ordrefin']=="hor") {
		$ordre = "ORDER BY GREATEST (coalesce(freq.h_inscription2,0), coalesce(freq.h_inscription3,0), coalesce(freq.h_inscription,0)) DESC";
	}
	else if($_POST['ordrefin']=="act") {
		$ordre = "ORDER BY freq.code_activ ASC";
	}
	else if($_POST['ordrefin']=="esp") {
		$ordre = "ORDER BY espaces.espace_nom ASC, eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
	}
	$inscrits_select = "SELECT freq.freq_id, freq.eleves_id, freq.date_freq, freq.hor_name, freq.code_activ, freq.cas, freq.h_inscription, freq.h_inscription2, freq.h_inscription3, freq.h_sortie, freq.h_sortie_2, freq.h_sortie_3, freq.espace_id, espaces.espace_nom, eleves.nom, eleves.prenom, eleves.division, eleves.naissance 
		FROM ".$prefix."_freq freq 
		INNER JOIN ".$prefix."_eleves eleves
		ON freq.eleves_id = eleves.eleves_id
		LEFT JOIN ".$prefix."_espaces espaces
		ON espaces.espace_id = freq.espace_id
		WHERE freq.date_freq = '".$_SESSION['ccren_day']."' AND freq.hor_name = '".$_SESSION['ccren_cren']."' AND freq.cas != 'sor' AND freq.cas != 'exc'
		".$_SESSION['filtre_consult']."
		".$ordre."";
	$inscrits = mysqli_query($bdd,$inscrits_select);
	$nb_inscrits = mysqli_num_rows($inscrits);
	$mail_inscrits = '<style> table { border-collapse:collapse; } td { border:1px solid grey; padding: 5px; } </style>';
	$mail_inscrits .= '<h2>'.MAILLIST.' '.date("d/m/Y").' '.IN.' '.$_SESSION['creneau'].' ('.@$_SESSION['crenpdf'].')</h2>';
	$mail_inscrits .= '<h3>'.FREQ_INF3.' ('.$nb_inscrits.')'.DOTS.'</h3>';
	$mail_inscrits .= '<table>';
	foreach($inscrits as $liste) {
		$activ = mysqli_query($bdd,"SELECT nom_activ FROM ".$prefix."_freqactiv WHERE code_activ = '".$liste['code_activ']."'");
		$activ_info = mysqli_fetch_assoc($activ);
		$mail_inscrits .= '<tr>';
		if($_POST['ordrefin']=="hor") {
			if(strlen($liste['h_inscription3'])==15) { $hi = '3'; }
			else if(strlen($liste['h_inscription2'])==15) { $hi = '2'; }
			else { $hi = ''; }
			$mail_inscrits .= '<td>'.substr($liste['h_inscription'.$hi],9,2).':'.substr($liste['h_inscription'.$hi],11,2).'</td>';
		}
		$mail_inscrits .= '<td>'.$liste['division'].' - '.$liste['nom'].' '.$liste['prenom'].'</td>';
		if($_SESSION['droits']==3&&$check_consultactiv_opt['valeur']==0) { }
		else {
			if($activ_info['nom_activ']=="") {
				$mail_inscrits .= '<td></td>';
			}
			else {
				$mail_inscrits .= '<td>'.$activ_info['nom_activ'].'</td>';
			}
		}
		if($_SESSION['espopt']==1) {
			if($liste['espace_id']!=0) { 
				$mail_inscrits .= '<td>'.$liste['espace_nom'].'</td>';
			}
			else { 
				$mail_inscrits .= '<td>'.$_SESSION['esp0'].'</td>'; 
			}
		}									
		$mail_inscrits .= '</tr>';						
	}
	$mail_inscrits .= '</table>';
	$_SESSION['freq_liste'] = $mail_inscrits;
}
?>