open();
}
function open()
{
if ( !$this->connect )
{
$this->connect = new mysqli( $this->server, $this->user, $this->passwd, $this->database );
if ( $this->connect->connect_errno )
{
log_error( "Échec de la connexion : => " . $this->connect->connect_error . "
" . __file__ . ' ligne ' . __line__, false,false);
return false;
}
}
$this->connect->set_charset("utf8");
return true;
}
function close()
{
$this->connect->close();
$this->connect = 0;
}
function protect( $string )
{
return $this->connect->real_escape_string( $string );
}
function query( $string )
{
$this->error = 0;
if ( empty( $this->connect ) ) $this->open();
$this->result = $this->connect->query( $string ) ;
$this->error = $this->connect->error;
if ( $this->connect->errno > 0 ) log_error( "Échec de la commande query => " . $this->error . "
" . __file__ . ' ligne ' . __line__ . "\n" . $string, true, false);
return $this->error;
}
function vote($idVotation, $idVotant, $idVote, $idCandidat)
{
$flag = 0;
/*$query='SELECT IF(identifiant="' . $idVotant . '" AND idVotation="' . $idVotation .'" AND INSTR(idVote, "' . $idVote .'"),TRUE,FALSE) as r FROM liste_votants';
$this->query($query);
if ( ($r = $this->result->fetch_array(MYSQLI_ASSOC)))
{*/
$query='SELECT IF ( id="' . $idVotation .'",TRUE,FALSE) as r FROM liste_votations';
$this->query($query);
echo "resultat " . $r . EOLH;
if ( ($r = $this->result->fetch_array(MYSQLI_ASSOC)))
{
$query='SELECT IF( idVotation="' . $idVotation .'" AND id="' . $idVote .'",TRUE,FALSE) as r FROM liste_votes';
$this->query($query);
if ( ($r = $this->result->fetch_array(MYSQLI_ASSOC)))
{
$query='SELECT IF(id="' . $idCandidat . '" AND idVotation="' . $idVotation .'" AND idVote="' . $idVote .'",TRUE,FALSE) FROM liste_candidats';
$this->query($query);
if ( ($r = $this->result->fetch_array(MYSQLI_ASSOC)))
{
$query='INSERT INTO votes (idVotant, idVotation, idVote, idCandidat) VALUES ("' . $this->protect($idVotant) .'", "' . $this->protect($idVotation) .'", "' . $this->protect($idVote) .'", "'. $this->protect($idCandidat) .'")';
$error = $this->query($query);
return $error;
}else
{
$error = "Le candidat n'existe pas" . EOLH;
}
}else
{
$error= "La votation ou le vote n'existe pas" . EOLH;
}
}
//}
return $error;
}
function resultats($idVotation)
{
$query = 'SELECT *, UNIX_TIMESTAMP(DATE_ADD(dateFin, INTERVAL 1 DAY)) as tf FROM liste_votations WHERE id = ' . $this->protect($idVotation);
$result = $this->query($query);
$votation = $this->result->fetch_array(MYSQLI_ASSOC);
if ($result === FALSE)
{
print "Votation non trouvée" . EOLH;
}else
{
$now = date('Y-m-d');
if ( time() > $votation['tf'] )
{
echo $votation["libelle"] . "";
$listeAdherents = listeAdherents();
print ('
"; //$resultatCandidats = ''; $query='SELECT COUNT(*) as n FROM votes WHERE idVotation=' . $this->protect($idVotation) . ' AND idVote=' . $vote["id"] .' AND idCandidat=0'; $result = $this->query($query); $resultat = $this->result->fetch_array(MYSQLI_ASSOC); $blanc = $resultat["n"]; echo 'Votes blancs : ' . $blanc . '
'; foreach($candidats as $candidat) { $query='SELECT COUNT(*) as n FROM votes WHERE idVotation=' . $this->protect($idVotation) . ' AND idVote=' . $vote["id"] .' AND idCandidat=' . $candidat["id"]; $result = $this->query($query); $resultat = $this->result->fetch_array(MYSQLI_ASSOC); echo $candidat["candidat"] . ' :' . $resultat["n"] . '
'; $resultatCandidats[$candidat['candidat']] = $resultat["n"]; $total += $resultat["n"]; } array_multisort($resultatCandidats, SORT_DESC, SORT_NUMERIC); reset($resultatCandidats); if ($total != 0) { if ($total >= $blanc) { print("Remporte le vote : " . key($resultatCandidats)); unset($resultatCandidats); print("
"); }else { print("Aucun candidat n'emporte le vote, car il y a plus de 50% de votes blancs.
"); } listeVotants($idVotation, $vote["id"], $listeAdherents); }else { print("Aucun votant, le vote est annulé.
"); } print("