/
pollBooth.pl
executable file
·155 lines (128 loc) · 3.81 KB
/
pollBooth.pl
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
149
150
151
152
153
154
155
#!/usr/bin/perl -w
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2001 by Open Source Development Network. See README
# and COPYING for more information, or see http://slashcode.com/.
# $Id$
use strict;
use Slash;
use Slash::Display;
use Slash::Utility;
#################################################################
sub main {
my $slashdb = getCurrentDB();
my $form = getCurrentForm();
my $user = getCurrentUser();
my $op = $form->{op};
if (defined $form->{aid} && $form->{aid} !~ /^\-?\d$/) {
undef $form->{aid};
}
header(getData('title'), $form->{section});
if ($user->{seclev} > 99 && $op eq 'edit') {
editpoll($form->{qid});
} elsif ($user->{seclev} > 99 && $op eq 'save') {
savepoll();
} elsif (! defined $form->{qid}) {
listpolls();
} elsif (! defined $form->{aid}) {
print pollbooth($form->{qid}, 0, 1);
} else {
my $vote = vote($form->{qid}, $form->{aid});
printComments($form->{qid})
if $vote && ! $slashdb->getVar('nocomment', 'value');
}
writeLog($form->{qid});
footer();
}
#################################################################
sub editpoll {
my($qid) = @_;
my $slashdb = getCurrentDB();
my($currentqid) = $slashdb->getVar('currentqid', 'value');
my $question = $slashdb->getPollQuestion($qid, ['question', 'voters']);
$question->{voters} ||= 0;
my $answers = $slashdb->getPollAnswers($qid, ['answer', 'votes']);
slashDisplay('editpoll', {
checked => $currentqid eq $qid ? ' CHECKED' : '',
qid => $qid,
question => $question,
answers => $answers,
});
}
#################################################################
sub savepoll {
return unless getCurrentForm('qid');
my $slashdb = getCurrentDB();
slashDisplay('savepoll');
$slashdb->savePollQuestion();
}
#################################################################
sub vote {
my($qid, $aid) = @_;
return unless $qid;
my $user = getCurrentUser();
my $constants = getCurrentStatic();
my $slashdb = getCurrentDB();
my(%all_aid) = map { ($_->[0], 1) }
@{$slashdb->getPollAnswers($qid, ['aid'])};
if (! keys %all_aid) {
print getData('invalid');
# Non-zero denotes error condition and that comments
# should not be printed.
return;
}
my $question = $slashdb->getPollQuestion($qid, ['voters', 'question']);
my $notes = getData('display');
if ($user->{is_anon} && ! $constants->{allow_anonymous}) {
$notes = getData('anon');
} elsif ($aid > 0) {
my $id = $slashdb->getPollVoter($qid);
if ($id) {
$notes = getData('uid_voted');
} elsif (exists $all_aid{$aid}) {
$notes = getData('success', { aid => $aid });
$slashdb->createPollVoter($qid, $aid);
$question->{voters}++;
} else {
$notes = getData('reject', { aid => $aid });
}
}
my $answers = $slashdb->getPollAnswers($qid, ['answer', 'votes']);
my $maxvotes = $slashdb->getPollVotesMax($qid);
my @pollitems;
for (@$answers) {
my($answer, $votes) = @$_;
my $imagewidth = $maxvotes
? int(350 * $votes / $maxvotes) + 1
: 0;
my $percent = $question->{voters}
? int(100 * $votes / $question->{voters})
: 0;
push @pollitems, [$answer, $imagewidth, $votes, $percent];
}
slashDisplay('vote', {
qid => $qid,
width => '99%',
title => $question->{question},
voters => $question->{voters},
pollitems => \@pollitems,
notes => $notes
});
}
#################################################################
sub listpolls {
my $slashdb = getCurrentDB();
my $min = getCurrentForm('min') || 0;
my $questions = $slashdb->getPollQuestionList($min);
my $sitename = getCurrentStatic('sitename');
slashDisplay('listpolls', {
questions => $questions,
startat => $min + @$questions,
admin => getCurrentUser('seclev') >= 100,
title => "$sitename Polls",
width => '99%'
});
}
#################################################################
createEnvironment();
main();
1;