283 lines
8.4 KiB
PHP
283 lines
8.4 KiB
PHP
<?php
|
|
//test if not directly called
|
|
if ( !isset($site) )
|
|
{
|
|
require_once( "config.inc.php" );
|
|
header( 'Location: ' . $accueil );
|
|
}
|
|
|
|
require_once "config.inc.php";
|
|
require_once "log.php";
|
|
|
|
class dbDolibarr extends dbcore
|
|
{
|
|
protected $server = "192.168.1.250";
|
|
protected $port = 3306;
|
|
protected $user = "dolibarr";
|
|
protected $passwd = "mysql_dolibarr";
|
|
protected $database = "dolibarr";
|
|
}
|
|
|
|
class db extends dbcore
|
|
{
|
|
protected $server = "localhost";
|
|
protected $port = 3306;
|
|
protected $user = "votation";
|
|
protected $passwd = "Lalis_votation";
|
|
protected $database = "votations";
|
|
}
|
|
|
|
class dbcore
|
|
{
|
|
public $connect;
|
|
public $result;
|
|
public $id;
|
|
public $error;
|
|
|
|
function __construct()
|
|
{
|
|
$this->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 . "<br />" . __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 . "<br />" . __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"] . "</h4>";
|
|
$listeAdherents = listeAdherents();
|
|
print (' <hr class="star-primary">
|
|
</div>
|
|
</div>
|
|
<div class="row">');
|
|
|
|
$query='SELECT * FROM liste_votes WHERE idVotation="' .$this->protect( $idVotation) .'"';
|
|
$result = $this->query($query);
|
|
$votes = $this->result->fetch_all(MYSQLI_ASSOC);
|
|
foreach ($votes as $vote)
|
|
{
|
|
switch ($vote["methode"])
|
|
{
|
|
case 1:
|
|
$total = 0;
|
|
echo "<div><h5>" . $vote["libelle"] . "</h5><br>";
|
|
$query='SELECT * FROM liste_candidats WHERE idVotation=' . $idVotation . ' AND idVote=' . $vote["id"];
|
|
$result = $this->query($query);
|
|
$candidats = $this->result->fetch_all(MYSQLI_ASSOC);
|
|
echo "<pre>";
|
|
//$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 . '<br>';
|
|
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"] . '<br>';
|
|
$resultatCandidats[$candidat['candidat']] = $resultat["n"];
|
|
$total += $resultat["n"];
|
|
}
|
|
array_multisort($resultatCandidats, SORT_DESC, SORT_NUMERIC);
|
|
reset($resultatCandidats);
|
|
if ($total != 0)
|
|
{
|
|
if ($total >= $blanc)
|
|
{
|
|
print("<h5>Remporte le vote : " . key($resultatCandidats));
|
|
unset($resultatCandidats);
|
|
print("</h5>");
|
|
}else
|
|
{
|
|
print("<h5>Aucun candidat n'emporte le vote, car il y a plus de 50% de votes blancs.</h5>");
|
|
}
|
|
listeVotants($idVotation, $vote["id"], $listeAdherents);
|
|
}else
|
|
{
|
|
print("<h5>Aucun votant, le vote est annulé.</h5>");
|
|
}
|
|
print("</pre></div><br>" . EOLH);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}else
|
|
{
|
|
print "Bonjour, vous pourrez consulter les résultats à partir du " . formattedate('fr', $votation['tf'] , "Europe/Paris") . "</h4>";
|
|
}
|
|
}
|
|
}
|
|
|
|
function liste_votants($idVotation)
|
|
{
|
|
$query = 'SELECT DISTINCT idVotant FROM liste_votes WHERE idVotation=' . $idVotation;
|
|
$this->query($query);
|
|
$votants = $this->result->fetch_all(MYSQLI_ASSOC);
|
|
$doliDB = new dbDolibarr();
|
|
foreach($votants as $votant)
|
|
{
|
|
$query ="SELECT login, firstname, lastname FROM llx_adherent WHERE login='" . $votant . "'";
|
|
$dolibarr->query($query);
|
|
$r = mysqli_fetch_array();
|
|
$listeVotants[$votant]["nom"] = $r["lastname"];
|
|
$listeVotants[$votant]["prenom"] = $r["firstname"];
|
|
}
|
|
return $listeVotants;
|
|
}
|
|
|
|
function queryVotationList()
|
|
{
|
|
$query = "SELECT * FROM liste_votations";
|
|
$this->query($query);
|
|
$votationLine = $this->result->fetch_all(MYSQLI_ASSOC);
|
|
foreach ($votationLine as $line)
|
|
{
|
|
$votationList[$line["id"]] = $line;
|
|
}
|
|
return $votationList;
|
|
}
|
|
|
|
function queryVotationName($idVotation) :string
|
|
{
|
|
$query = "SELECT libelle FROM liste_votations WHERE id='" . $idVotation . "'";
|
|
$this->query($query);
|
|
$votationLine = $this->result->fetch_assoc();
|
|
return $votationLine["libelle"];
|
|
}
|
|
|
|
function queryVoteList($idVotation)
|
|
{
|
|
$query = "SELECT * FROM liste_votes WHERE idVotation = " . idVotation;
|
|
$this->query($query);
|
|
$votes = $this->result->fetch_all(MYSQLI_ASSOC);
|
|
foreach ($votes as $line)
|
|
{
|
|
$votesList[$line["id"]] = $line;
|
|
}
|
|
return $votesList;
|
|
}
|
|
|
|
function queryVoteName($idVote)
|
|
{
|
|
$query = "SELECT libelle FROM liste_votes WHERE id='" . $idVote . "'";
|
|
$this->query($query);
|
|
$voteLine = $this->result->fetch_assoc();
|
|
return $voteLine["libelle"];
|
|
}
|
|
|
|
function queryVotes($idVotation)
|
|
{
|
|
$query = "SELECT lv.id, lv.libelle, ms.libelle as methode FROM liste_votes as lv LEFT JOIN methods as ms ON lv.methode=ms.id WHERE idVotation='" . $idVotation . "'";
|
|
$this->query($query);
|
|
$votes = $this->result->fetch_all(MYSQLI_ASSOC);
|
|
return $votes;
|
|
}
|
|
|
|
function queryCandidatsList($idVotation, $idVote)
|
|
{
|
|
$query = "SELECT * FROM liste_candidats WHERE idVotation='" . $idVotation . "' AND idVote='" . $idVote . "'";
|
|
$this->query($query);
|
|
$candidats = $this->result->fetch_all(MYSQLI_ASSOC);
|
|
foreach ($candidats as $line)
|
|
{
|
|
$candidatsList[$line["id"]] = $line;
|
|
}
|
|
return $candidatsList;
|
|
}
|
|
}
|
|
|
|
function queryCandidatName($idCandidat)
|
|
{
|
|
$query = "SELECT libelle FROM liste_candidats where id='" . $idCandidat . "'";
|
|
$this->query($query);
|
|
$line = $this->result->fetch_assoc();
|
|
return $line["candidat"];
|
|
}
|
|
?>
|