<?php
#=
/****************************************************************************
Copyright 2002, Daniel Deycard, Jacques Messines, Alain Pomirol
(CATICE Académie de Bordeaux)

This file is part of Gibii/Melia.

Gibii/Melia is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

Gibii/Melia is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Gibii/Melia; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

****************************************************************************/

include("../include/vars_adm.php");
include("../include/util.inc.php");
include("../include/get.inc.php");
include("../include/eleve.inc.php");
include("../include/utiljs.php");
include("phpmygibii.inc.php");
connecte($host,$user,$mdp,$db);
//opensession("Admin",false);
recuprequest();

entetehtml("Résultat de la requête");


function joker($valchampform,$nomchampbase) {
//jojers acceptés : %, _, *, ?
$valchampform=trim($valchampform);
while ($valchampform && ($valchampform[strlen($valchampform)-1]=='*' ||
                         $valchampform[strlen($valchampform)-1]=='?'))
       $valchampform=substr($valchampform,0,-1);
if ($valchampform) {
    $p1=strpos($valchampform,'?');
    $p2=strpos($valchampform,'*');
    if ($p1!==false) $valchampform=str_replace('?','_',$valchampform);
    if ($p2!==false) $valchampform=str_replace('*','%',$valchampform);
    if ($valchampform[strlen($valchampform)-1]!='%') $valchampform.='%';
    return "AND $nomchampbase LIKE '$valchampform'";
    }
else return '';
}


?>

<form name='form1' method='post' action='reponse.php' >
<table border = "0" cellpadding="0" cellspacing="0" width="100%">

<?php
//print "<h3>_POST = </h3>";print_r($_POST);print "<hr />";
debformulaire('Requête');
if (!isset($obtenuschk)) $obtenuschk=array(); //pas de case cochée dans le formulaire
checkboxes($obtenuschk,'obtenuschk');
lignevide();
boutons("Relancer la requête","Remise à zéro");
print "</table>";
print "<hr style=\"width:100%;\" />";
set_time_limit(300);
$select=   "eleves.eleve_id AS eid,
        eleves.nom AS unom, prenom,
        dnaiss, annee,
        eleves.classe_id AS clid, divcod,
        eleves.compte_id AS cid, login,
        eleves.etab_id AS etab,
        etablissements.nom AS etnom, ville, rne, codepostal, nbj_vie_demande,
        count(eleves.eleve_id) AS nbacquis,
        resultat_id"; //permet de savoir si l'élève n'a pas eu d'item

$from="eleves
   LEFT JOIN etablissements ON eleves.etab_id=etablissements.etab_id
   LEFT JOIN comptes ON eleves.eleve_id=comptes.eleve_id
   LEFT JOIN classes ON eleves.classe_id=classes.classe_id
   LEFT JOIN resultats ON eleves.eleve_id=resultats.eleve_id ";

$where="";
//func joker(valeur du champ formulaire , nom du champ de la base)
$where.=joker($etnom,'etablissements.nom');
$where.=joker($ville,'ville');
$where.=joker($codepostal,'codepostal');
$where.=joker($etab,'eleves.etab_id');
$where.=joker($unom,'eleves.nom');
$where.=joker($prenom,'eleves.prenom');
$where.=joker($divcod,'divcod');
$where.=joker($login,'login');
$where.=joker($civil,'civil');
$where.=joker($dnaiss,'dnaiss');
$where.=joker($annee,'annee');

if (!$where)
    alert("Contraintes inexistantes dans les champs textes.");
else {
    $where='1 '.$where;             //à cause du premier AND
    $sql="SELECT  $select
            FROM  $from
            WHERE $where
            GROUP BY eleves.eleve_id
            ORDER BY eleves.etab_id,eleves.nom,eleves.prenom";

    //print($sql);
    $res=query($sql);
    $tablehtml=mysql_num_rows($res);
    if ($tablehtml) {
        print "<h2>Résultats</h2>\n";
        print "<table border=\"1\" cellpadding=\"2\" cellspacing=\"1\">\n";
        print "<th width=\"100\"> nom </th>\n<th> prénom </th>\n<th> naissance </th>\n<th> login </th>\n
                <th> classe </th>\n<th> RNE </th>\n<th> nom établissement </th>\n<th> ville </th>\n
                <th> CP </th>\n<th> acquis </th>\n";
        }
    else print "<br /><br /><br /><h3>Pas de résultat !</h3>\n";
    while ($row=mysql_fetch_array($res)) {
        $eid=$row['eid'];
        $clid=$row['clid'];
        $etab=$row['etab'];

        if (isset($obtenuschk) && count($obtenuschk) && !possede($eid,$obtenuschk))
            continue; //l'élève trouvé ne possède pas tous les items de $obtenuschk
        print "<tr>\n";
        $nomwoq=stripslashes($row['unom']);      //without quotes
        $prenomwoq=stripslashes($row['prenom']);
        if ($row['nbacquis']==1 && $row['resultat_id']==NULL) $row['nbacquis']=0;
        $i=0;

        print "<td> <a href=\"javascript:void(0);\" onclick=\"ouvrir($eid,$etab,$clid);\"> $nomwoq </a> </td>\n";
        print "<td> ".substr($prenomwoq,0,16)." </td>\n";
        print "<td> ".$row['dnaiss']." </td>\n";
        print "<td> ".$row['login']." </td>\n";
        print "<td> ".$row['divcod']." </td>\n";
        print "<td> ".$row['rne']." </td>\n";
        print "<td> ".substr($row['etnom'],0,25)." </td>\n";
        print "<td> ".$row['ville']." </td>\n";
        print "<td> ".$row['codepostal']." </td>\n";
        print "<td align=\"right\"> ".$row['nbacquis']." </td>\n"; //on n'affiche pas les items ici
        print "</tr>\n";
        }
    if ($tablehtml) print "</table>\n";
    }
?>
</form>
</BODY>
</HTML>