<?php

/*
Name: vote.php
Author: NicoSoft
Création: 16/08/01
MAJ      : 16/08/01
*/
require_once "include/standard.inc.php";

$FileName = "modules/sondages/$File";
$Cpt = 0;
$totalnbvotes = 0;

// si le fichier n'existe pas, on le crée:
if (!(file_exists($FileName)))
{
        $fp = fopen($FileName,"w+");
        fclose($fp);
}

// lecture des données
$fp = fopen($FileName,"r");

$data = fgetcsv($fp, 1000, ",");
$question = $data[0];

while ($data = fgetcsv($fp, 1000, ","))
{
        $proposition[$Cpt] = stripcslashes($data[0]);
        $nbvotes[$Cpt] = $data[1];
        $totalnbvotes += $nbvotes[$Cpt++];
}

fclose($fp);

// *************** gestion des votes ***************
if ($vote)
{

if (!isset($question) || !isset($proposition) || !isset($nbvotes))
        die("erreur");

        $fp = fopen($FileName,"w+");
        fwrite($fp,"\"".$question."\"\n");
             for($i=0;$i<=$Cpt-1;$i++)
                {
                        if ($choix == $proposition[$i])
                                $nbvotes[$i]++;
				
                        fwrite($fp,"\"$proposition[$i]\",\"$nbvotes[$i]\"\n");
                }
        fclose($fp);

}

// ******************* affichage des résultats *******************
if (isset($affresults))
{
        echo "<p><b>Question:</b> &quot;<i>".$question."</i>&quot;</p>";
        echo "<table border=0 valign=middle align=center>";
	echo "<tr><td><b>Réponses:</b></td><tr>";
	
        for ($i = 0; $i < count($proposition); $i++)
        {
		if (($nbvotes[$i] != 0) || ($totalnbvotes != 0))
			$percent = round(($nbvotes[$i]*100)/$totalnbvotes);
		else
			$percent = 0;

                echo "<tr><td><i>".addslashes($proposition[$i])."</i></td>\n";
		echo "<td width=".$echelle."><img src=\"modules/share/or.gif\" width=".($percent+1)."% height=10 alt=':'></td>\n";
		echo "<td>".$percent."%</td>\n";
		
		if ($nbvotes[$i] == 0)
			$nbvotes[$i] = "aucun";
			
		echo "<td>(".$nbvotes[$i]." vote";
		
		if ($nbvotes[$i]  > 1)
			echo "s";
			
		echo ")</td></tr>\n";
	}
        echo "<tr><td><b>Total</b></td><td width=".$echelle."><img src=\"\" width=".$echelle." height=5 alt=':'></td><td>100%</td><td>(".$totalnbvotes." votes)</td></tr>";
        echo "</div></table>";

	$footerbox = 1;
        $footerboxtext = "Retour";
        $footerlien = "/";

}
else
{

// ************* affichage des questions ******************
        if ($Cpt <= 0)
        {
                echo "<b>Aucun sondage disponible</b><br />";
        }
        else
        {

                echo $question."<br />";
                echo "<br />
                    <form method=post action='".caller()."'>\n";
		    
                for($i=0;$i<=$Cpt-1;$i++)
                       echo "<input type=radio name=\"choix\" value=\"$proposition[$i]\"> $proposition[$i]<br />\n";
		       
                echo "<br /><input name='param' value='post' type=hidden>
                      <div align=center><input type=submit name='vote' value='Voter!'></div>
                        <input type=hidden name='echelle' value='1'>
                        </form>\n";

                $footerbox = 1;
                $footerboxtext = "Résultats";
		$footertitle = "Voir les résultats de ce sondage";
                $footerlien = "?modulepage=sondage&amp;affresults=1&amp;echelle=200&amp;File=$File";
}
}
unset($FileName);
unset($param);
unset($choix);
unset($vote);
unset($affresults);

?>