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
| class vote {
# променливи
var $NAME;
var $SL=1;
var $IL=1;
var $IP;
var $Q="Въпрос ?";
var $A="Отг.1|Отг.2|Отг.3|Отг.4";
# функции
function vote($N) {
mysql_query("create table if not exists `vote` (name tinyint UNSIGNED,ip char(15),vote int UNSIGNED);");
global $_CONF;
$this->NAME=$N;
$this->IP=$_SERVER['REMOTE_ADDR'];
$this->SL=$_CONF['VOTEPOOL']['SL'];
$this->IL=$_CONF['VOTEPOOL']['IL'];
$this->Q=$_CONF['VOTEPOOL'][$N]['Q'];
$this->A=$_CONF['VOTEPOOL'][$N]['A'];
return true;
}
function Post($V) {
if (!is_numeric($V)) return 0;
$A=explode('|',$this->A);
if ($V>count($A)||$V<1) return 0;
if (!isset($_SESSION['VOTEPOOL'][$this->NAME]))$_SESSION['VOTEPOOL'][$this->NAME]=0;
$_SESSION['VOTEPOOL'][$this->NAME]++;
if ($_SESSION['VOTEPOOL'][$this->NAME] >= $this->SL) return -1;
$query=mysql_query("select 1 from `vote` where ip='{$this->IP}' limit {$this->IL};");
if (mysql_num_rows($query)>=$this->IL) return -2;
mysql_query("insert into `vote` (name,ip,vote) values('{$this->NAME}','{$this->IP}','$V');");
return 1;
}
function Result() {
$query="select vote,count(*) as COUNT from `vote` where name='{$this->NAME}' group by vote;";
$query=mysql_query($query);
$ret=explode('|',$this->A);
$ret=array_fill(1,count($ret),0);
if (mysql_num_rows($query))
while($row=mysql_fetch_array($query,MYSQL_ASSOC)) $ret[$row['vote']]=$row['COUNT'];
$ret['TOTAL']=array_sum($ret);
return $ret;
}
function Graph() {
$a=$this->Result();
echo "
<div class=\"votegraph\">
<h1>{$this->Q}</h1>";
foreach(explode('|',$this->A) as $k=>$v) echo "
<div>
<span>".$a[$k+1]." за $v</span>
<hr style=\"width:".round(($a[$k+1]/$a['TOTAL'])*100,0)."%;\" title=\"".$a[$k+1]." за $v\" />
</div>";
echo "
<p><strong>Общо:</strong> {$a['TOTAL']}</p>
</div>";
}
function HTML_Form($S=FALSE) {
if (isset($_GET['viewvote'])) { $this->Graph(); return; }
if ($S===FALSE&&isset($_POST['votename'])&&isset($_POST['vote'])&&isset($_POST['submit'])&&is_numeric($_POST['votename'])&&is_numeric($_POST['vote'])) {
$ret=$this->Post($_POST['vote']);
if ($ret==1) { echo "<div class=\"vote_msg\">Благодаря, твоят глас беше отчетен.</div>"; $this-Graph(); }
elseif ($ret==-1) echo "<div class=\"vote_msg\">Лимитът за гласувания на сесия е изчерпан.</div>";
elseif ($ret==-2) echo "<div class=\"vote_msg\">Лимитът за гласувания на IP е изчерпан.</div>";
else {
echo "<div class=\"vote_msg\">Възникна непознат проблем.</div>";
$this->HTML_Form(1);
}
}
else {
echo "
<form action=\"{$_SERVER['PHP_SELF']}?".htmlentities($_SERVER['QUERY_STRING'])."\" method=\"post\" class=\"voteform\">
<input type=\"hidden\" name=\"votename\" value=\"{$this->NAME}\" />
<h1>{$this->Q}</h1>";
foreach(explode('|',$this->A) as $k=>$v) echo "
<label from=\"vote\"><input type=\"radio\" name=\"vote\" value=\"".($k+1)."\" /> $v</label>";
echo "
<input type=\"submit\" name=\"submit\" value=\"Гласувай!\" />
<input type=\"button\" name=\"viewresult\" value=\"Резултати\" onclick=\"window.location.href='{$_SERVER['PHP_SELF']}?".htmlentities($_SERVER['QUERY_STRING']).(isset($_GET['viewvote'])?'':'&viewvote')."';\" />
</form>
";
}
}
} |