Correction bac2019
Le fichier JS
function alphanum(ch)
{
var i=0;
ch=ch.toUpperCase();
while( i< ch.length && (ch.charAt(i) >="A" && ch.charAt(i) <= "Z")||( ch.charAt(i) >="0" && ch.charAt(i) <= "9"))
i++;
return i== ch.length
}
function alpha(ch)
{
var i=0;
ch=ch.toUpperCase();
while( i< ch.length && ch.charAt(i) >="A" && ch.charAt(i) <= "Z")
i++;
return (i== ch.length) ;
}
function valide(ch)
{
var nbmaj=0; var nbmin=0; var nbch=0;
for( var i=0; i<ch.length;i++)
{
if (ch.charAt(i) >= "A" && ch.charAt(i)<="Z") nbmaj=nbmaj+1;
if (ch.charAt(i)>= "a" && ch.charAt(i) <= "z") nbmin=nbmin+1;
if (ch.charAt(i)>="0" && ch.charAt(i)<= "9") nbch=nbch+1;
}
return (nbmaj!=0 && nbmin!=0 && nbch!=0)
}
function test()
{
var mail=document.getElementById("mail").value;
var mp=document.getElementById("mp").value;
Page 1 sur 8
if (mail.length >50){ alert(" mail ne doit pas dépasser 50 caractères"); return false;}
if ( !(mail.indexOf("@")!=-1 && mail.indexOf(".")!=-1 && mail.indexOf("@") < mail.indexOf(".")))
{
alert(" mail doit être sous le format [email protected]");
return false;
}
var ch1=mail.substr(0,mail.indexOf("@"));
var ch2=mail .substr(mail.indexOf("@")+1 ,mail.indexOf(".")- mail.indexOf("@")+1 -1);
var ch3=mail.substr(mail.indexOf(".")+1,mail.length);
if(!(ch1.length >=3 && ch2.length>= 3 && ch3.length>=2 && ch3.length <= 4 && alphanum(ch1)
&& alphanum(ch2) && alpha(ch3)))
{
alert("ch1 et ch2 doivent être 2 chaines alphabétiques de longueur > = 3 et ch3 entre 2 et 4 lettres ");
return false;
}
if (!( mp.length ==6 && valide(mp)))
{
alert("mp de 6 caracteres comportant au moins une lettre en majuscule une lettre en miniscule et un chiffre ");
return false;
}
if( document.getElementById("genre").value==""){ alert("selection obligatoire du genre"); return false;}
if ( (document.getElementById("q11").checked==false && document.getElementById("q12").checked==false
&& document.getElementById("q13").checked==false)||(document.getElementById("q21").checked==false
&& document.getElementById("q22").checked==false && document.getElementById("q23").checked==false)
|| (document.getElementById("q31").checked==false && document.getElementById("q32").checked==false &&
document.getElementById("q33").checked==false) )
{alert("Sélection obligatoire d'une réponse pour chaque question "); return false;}
}
Page 2 sur 8
Sondage .php
<?php /* correction sondage.php bac2019 */
mysql_connect("127.0.0.1","root","");
mysql_select_db("bac2019");
$mail=$_POST['mail'];
$mp=$_POST['mp'];
$genre=$_POST['genre'];
$q1=$_POST['q1'];
$q2=$_POST['q2'];
$q3=$_POST['q3'];
//----------------Mail existe mais Mp incorrecte ------------
$r1="select *
from Participant
where Mail='$mail' and Mdp <>'$mp';";
$res1=mysql_query($r1);
if( mysql_num_rows($res1)!=0)
die(" Erreur d'authenfication");
//----mise à jour des réponses puisque le participant a participé au sondage actuelle ----
$r1="select *
from Participant P, Reponse R , Sondage S
where P.IdParticipant=R.IdParticipant and S.NumS=R.NumS
and Mail='$mail' and Mdp='$mp' and month(now())=month(DateDebut);";
$res1=mysql_query($r1);
$q1=substr($q1,1,1);//pour copier seulement la réponse O, N ou S
$q2=substr($q2,1,1);
Page 3 sur 8
$q3=substr($q3,1,1);
$enreg=mysql_fetch_array($res1); // pour chercher le numS du sondage actuelle et le Idparticipant
$nums=$enreg["NumS"];
$idparticipant=$enreg["IdParticipant"];
if (mysql_num_rows($res1)!=0)
{ // mise à jour des trois réponses
$r2="update Reponse
set Rep='$q1'
where NumQ=1 and NumS='$nums' and IdParticipant='$idparticipant';";
$res2=mysql_query($r2);
$r3="update Reponse
set Rep='$q2'
where NumQ=2 and NumS='$nums' and IdParticipant='$idparticipant';";
$res3=mysql_query($r3);
$r4="update Reponse
set Rep='$q3'
where NumQ=3 and NumS='$nums' and IdParticipant='$idparticipant';";
$res4=mysql_query($r4);
if($res2 and $res3 and $res4)
die("Mise à jour effectuée avec succès");
}
//-----------------------------------------------------------------
else{ // le participant n'a pas participer au sondage actuelle
$r2="insert into Reponse values (1,'$nums','$idparticipant','$q1'),
(2,'$nums','$idparticipant','$q2'),
(3,'$nums','$idparticipant','$q3');
"; // ajout des 3 réponses
$res2=mysql_query($r2);
if($res2) die("Participation au sondage effectuée avec succèes");
Page 4 sur 8
}
//----------------nouveau participant ------------------------
$r1="select *
from Participant
where Mail='$mail' and Mdp='$mp';";
$res1=mysql_query($r1);
if(mysql_num_rows($res1)==0)
{ // ajout du participant
$r2="insert into Participant
values('','$mail','$mp','$genre');";
$res2=mysql_query($r2);
if($res2)
{
$enreg=mysql_fetch_array($res2); // pour récuperer IdParticipant du nouveau participant
$idparticipant=$enreg["IdParticipant"];
// ajout de ses réponses
$r3="insert into Reponse values (1,'$nums','$idparticipant','$q1'),
(2,'$nums','$idparticipant','$q2'),
(3,'$nums','$idparticipant','$q3');
";
$res3=mysql_query($r3);
if($res3) die("Inscription et participation au sondage effectuée avec succèes");
}
}
statistique.php
<?php /* correction statistique.php bac2019 */
mysql_connect("127.0.0.1","root","");
mysql_select_db("bac2019");
$nums=$_POST['theme'];
Page 5 sur 8
/* on commence par vérifier si le sondage est lancé ou non */
$r1="select *
from Sondage
where NumS='$nums' and DateDebut < date(now()) ;";
$res1=mysql_query($r1);
if( mysql_num_rows($res1)==0)
die( " sondage non encore lancé! ") ;
/* vérifier s'il y a des participations au sondage sélectionné ou non */
$r1="select *
from Reponse
where NumS='$nums';";
$res1=mysql_query($r1);
if( mysql_num_rows($res1)==0)
die("aucune participation enregistrée à ce moment");
/* on compte le nopmbre total des participants au sondage */
$r1="select count(*) nbpart
from Reponse
where NumS='$nums'
group by IdParticipant;";
$res1=mysql_query($r1);
$enreg=mysql_fetch_array($res1);
$nbparticipant=$enreg['nbpart'];
/* on compte le nopmbre total de participants au sondage du genre femme*/
$r2="select count(*) nbpartf
from Participant P, Reponse R
where P.Genre='F' and P.IdParticipant=R.IdParticipant and R.NumS= '$nums'
group by IdParticipant;";
$res2=mysql_query($r2);
$enreg=mysql_fetch_array($res2);
$nbpartf=$enreg['nbpartf'];
/* on compte le nopmbre total de participants au sondage du genre homme */
Page 6 sur 8
$r3="select count(*) nbparth
from Participant P, Reponse R
where P.Genre='H' and P.IdParticipant=R.IdParticipant and R.NumS= '$nums'
group by IdParticipant;";
$res3=mysql_query($r3);
$enreg=mysql_fetch_array($res3);
$nbparth=$enreg['nbparth'];
/*l'affichage */
echo 'statistique du sondage '.'<br>'.'Nombre total des participants au sondage :' . $nbrepart.'<br>'.'Nombre des femmes: '.$nbpartf.'<br>'.
'Nombre des hommes : '.$nbparth.'<br>';
echo
'<table>
<tr>
<td colspan="2"> </td>
<td colspan="3" border="2"> Pourcentages </td>
</tr>
<tr>
<td> Nº </td>
<td> Question </td>
<td> "Oui" </td>
<td> "non" </td>
<td> "Sans avis" </td>
</tr>';
/*on commence par chercher le nombre de question du thème sélectionné */
$r4="select *
from Question
where NumS='$nums';";
$res4=mysql_query($r4);
$nbq=mysql_num_rows($res4);
/*on compte pour chaque question le nombre de réponse oui, non et sans avis */
for($i=1;$i<=$nbq;$i++)
{
Page 7 sur 8
$r5="select *
from Reponse
where NumQ='$i' and NumS=$nums and rep='O';";
$res5=mysql_query($r5);
$nbo=mysql_num_rows($res5);
$r6="select *
from Reponse
where NumQ='$i' and NumS=$nums and rep='N';";
$res6=mysql_query($r6);
$nbn=mysql_num_rows($res6);
$r7="select *
from Reponse
where NumQ='$i' and NumS=$nums and rep='S';";
$res7=mysql_query($r7);
$nbs=mysql_num_rows($res7);
/*pour déterminer le contenu du question */
$r8="select *
from Question
where NumS='$nums' and NumQ='$i';";
$res8=mysql_query($r8);
$enreg=mysql_fetch_array($res8);
$totalrep=$nbo+$nbn+$nbs;
echo '<tr>
<td> '. $i .'</td>
<td> '. $enreg["Contenu"] .'</td>
<td> '. ($nbo/$totalrep)*100 .'%'.'</td>
<td>' . ($nbn/$totalrep)*100. '%'.'</td>
<td> '. ($nbs/$totalrep)*100 .'%' . '</td>
</tr>';
}
echo '</table>';
?>
Page 8 sur 8