1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
<?php
// L'internaute a-t-il déjà voté ?
function VoteEffectue($question_id, $bdd)
{
$vote_effectue = $bdd->prepare('SELECT count(*) as nb_votes FROM votes WHERE qid = :qid AND INET_NTOA(ipadress) = :ip');
$vote_effectue->bindValue(':qid', $question_id, PDO::PARAM_INT);
$vote_effectue->bindValue(':ip', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
$vote_effectue->execute();
$nombre_de_votes = $vote_effectue->fetch();
$vote_effectue->closeCursor();
return ($nombre_de_votes['nb_votes'] == 0) ? false : true;
}
// Le formulaire
function AfficherFormulaire($question_id, $bdd)
{
global $titre_site;
// La question
$afficher_formulaire_question = $bdd->prepare('SELECT * FROM questions WHERE id = :qid');
$afficher_formulaire_question->bindValue(':qid', $question_id, PDO::PARAM_INT);
$afficher_formulaire_question->execute();
$afficher_formulaire_question_retour = $afficher_formulaire_question->fetch();
$afficher_formulaire_question->closeCursor();
// Le thème
$afficher_formulaire_theme = $bdd->prepare('SELECT * FROM themes WHERE id = :tid');
$afficher_formulaire_theme->bindValue(':tid', $afficher_formulaire_question_retour['tid'], PDO::PARAM_INT);
$afficher_formulaire_theme->execute();
$afficher_formulaire_theme_retour = $afficher_formulaire_theme->fetch();
$afficher_formulaire_theme->closeCursor();
if($afficher_formulaire_theme_retour['bouton_voter'] == 1)
{
$bouton_voter = '<input type="submit" value="Voter"/>';
}
else // $theme2['bouton_voter'] vaut 2
{
$bouton_voter = '<input type="submit" value="Voter"/>';
}
// Les réponses
$afficher_formulaire_reponses = $bdd->prepare('SELECT * FROM reponses WHERE qid = :qid');
$afficher_formulaire_reponses->bindValue(':qid', $question_id, PDO::PARAM_INT);
$afficher_formulaire_reponses->execute();
$formulaire = 'document.write(\'<div id="div_cadre" class="1000sondages" style="width: '.htmlspecialchars(utf8_encode($afficher_formulaire_theme_retour['largeur_sondage'])).'px; border: '.htmlspecialchars(utf8_encode($afficher_formulaire_theme_retour['largeur_bordure'])).'px solid #'.htmlspecialchars(utf8_encode($afficher_formulaire_theme_retour['couleur_bordure'])).' ; background-color: #'.htmlspecialchars(utf8_encode($afficher_formulaire_theme_retour['couleur_fond'])).' ; padding-top: 10px; padding-left: 10px; padding-right: 10px; padding-bottom: 10px; font-family: Arial, Helvetica, sans-serif; font-size: 13px; color: #'.htmlspecialchars(utf8_encode($afficher_formulaire_theme_retour['couleur_reponses'])).';"> <span style="font-weight: bold;">'.htmlspecialchars($afficher_formulaire_question_retour['question']).'</span><br/><br/><form class="1000sondages" method="post" action="#" target="_blank">';
while($afficher_formulaire_reponses2 = $afficher_formulaire_reponses->fetch())
{
$formulaire .= '<input type="radio" name="reponse" value="'.htmlspecialchars(utf8_encode($afficher_formulaire_reponses2['id'])).'" id="form-'.$titre_site.'-'.htmlspecialchars(utf8_encode($afficher_formulaire_reponses2['id'])).'" class="1000sondages"/><label for="form-'.$titre_site.'-'.htmlspecialchars(utf8_encode($afficher_formulaire_reponses2['id'])).'">'.htmlspecialchars($afficher_formulaire_reponses2['texte']).'</label><br/><br/>';
}
$formulaire .= '<input class="1000sondages" type="hidden" name="question" value="'.htmlspecialchars(utf8_encode($afficher_formulaire_question_retour['id'])).'"/>'.utf8_encode($bouton_voter).' <br/><br/> <span style="font-size: 11px; color: #'.htmlspecialchars(utf8_encode($afficher_formulaire_theme_retour['couleur_reponses'])).';"><a style="text-decoration: none; color: #'.htmlspecialchars(utf8_encode($afficher_formulaire_theme_retour['couleur_reponses'])).';" href="http://www.1000sondages.com" alt="Creer sondage gratuit" title="Créer un sondage gratuit">'.$titre_site.'</a></span></form></div>\');';
return $formulaire ;
}
// Les résultats
function AfficherResultats($question_id, $bdd)
{
global $titre_site;
// La question
$afficher_resultats_question = $bdd->prepare('SELECT * FROM questions WHERE id = :qid');
$afficher_resultats_question->bindValue(':qid', $question_id, PDO::PARAM_INT);
$afficher_resultats_question->execute();
$afficher_resultats_question_retour = $afficher_resultats_question->fetch();
$afficher_resultats_question->closeCursor();
// Le thème
$afficher_resultats_theme = $bdd->prepare('SELECT * FROM themes WHERE id = :tid');
$afficher_resultats_theme->bindValue(':tid', $afficher_resultats_question_retour['tid'], PDO::PARAM_INT);
$afficher_resultats_theme->execute();
$afficher_resultats_theme_retour = $afficher_resultats_theme->fetch();
$afficher_resultats_theme->closeCursor();
$resultats = 'document.write(\'<div id="div_cadre" style="width: '.htmlspecialchars(utf8_encode($afficher_resultats_theme_retour['largeur_sondage'])).'px; border: '.htmlspecialchars(utf8_encode($afficher_resultats_theme_retour['largeur_bordure'])).'px solid #'.htmlspecialchars(utf8_encode($afficher_resultats_theme_retour['couleur_bordure'])).' ; background-color: #'.htmlspecialchars(utf8_encode($afficher_resultats_theme_retour['couleur_fond'])).' ; padding-top: 10px; padding-left: 10px; padding-right: 10px; padding-bottom: 10px; font-family: Arial, Helvetica, sans-serif; font-size: 13px; color: #'.htmlspecialchars(utf8_encode($afficher_resultats_theme_retour['couleur_reponses'])).';"> <span style="font-weight: bold; font-size: 13px;">'.htmlspecialchars($afficher_resultats_question_retour['question']).'</span><br/><br/>';
// Les réponses et résultats
$reponses = $bdd->prepare('SELECT * FROM reponses WHERE qid = :qid');
$reponses->bindValue(':qid', $question_id, PDO::PARAM_INT);
$reponses->execute();
while($reponses_retour = $reponses->fetch())
{
// Le nombre de votes par réponse
$votes = $bdd->prepare('SELECT count(*) as rep_votes FROM votes WHERE rid = :rid');
$votes->bindValue(':rid', $reponses_retour['id'], PDO::PARAM_INT);
$votes->execute();
$votes_retour = $votes->fetch();
// Le total de votes
$total = $bdd->prepare('SELECT count(*) as nb_votes FROM votes WHERE qid = :qid');
$total->bindValue(':qid', $question_id, PDO::PARAM_INT);
$total->execute();
$total_retour = $total->fetch();
$total->closeCursor();
$un_pour_cent = ($theme_retour['largeur_sondage'] - 20) / 100;
$pourcentage = ($votes_retour['rep_votes'] / $total_retour['nb_votes']) * 100;
$largeur_div = $un_pour_cent * $pourcentage;
if($largeur_div == 0)
{
$largeur_div = 5;
}
$resultats .= '<strong>'.htmlspecialchars($reponses_retour['texte']).' </strong>'.htmlspecialchars(utf8_encode(round($pourcentage))).'%<br/>';
$resultats .= '<div style="width: '.htmlspecialchars(utf8_encode($largeur_div)).'px; height: 12px; background-color: #CCCCCC ;"></div><br/>';
}
$votes->closeCursor();
$reponses->closeCursor();
$resultats .= '<span style="font-size: 11px; color: #'.htmlspecialchars(utf8_encode($afficher_resultats_theme_retour['couleur_reponses'])).';"><a style="text-decoration: none; color: #'.htmlspecialchars(utf8_encode($afficher_resultats_theme_retour['couleur_reponses'])).';" href="http://www.1000sondages.com" alt="Creer sondage gratuit" title="Créer un sondage gratuit">'.$titre_site.'</a></span><input type="text" name="qid" value="'.htmlspecialchars(utf8_encode($qid)).'" style="display: none;"/></form></div>\');';
return $resultats;
}
function EnregistrerVote($question_id, $bdd)
{
// On vérifie que l'internaute a bien validé le formulaire
if(isset($_GET['reponse']) AND !empty($_GET['reponse']))
{
// On enregistre
$bdd->beginTransaction();
try
{
$insertion = $bdd->prepare('INSERT INTO votes (qid, rid, ipadress) VALUES (:qid, :rid, INET_ATON(:ipadress))');
$insertion->bindValue(':qid', $question_id, PDO::PARAM_INT);
$insertion->bindValue(':rid', $_GET['reponse'], PDO::PARAM_INT);
$insertion->bindValue(':ipadress', $_SERVER['REMOTE_ADDR'], PDO::PARAM_STR);
$insertion->execute();
$insertion->closeCursor();
$bdd->commit();
}
catch(PDOException $e)
{
$insertion->closeCursor();
$bdd->rollBack();
throw $e;
}
}
}
?> |
Partager