1 | package Vote::Model::Vote; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base 'Catalyst::Model'; |
---|
6 | use DBI; |
---|
7 | |
---|
8 | =head1 NAME |
---|
9 | |
---|
10 | Vote::Model::Vote - Catalyst Model |
---|
11 | |
---|
12 | =head1 DESCRIPTION |
---|
13 | |
---|
14 | Catalyst Model. |
---|
15 | |
---|
16 | =cut |
---|
17 | |
---|
18 | sub new { |
---|
19 | my ($class) = @_; |
---|
20 | my $db = DBI->connect( |
---|
21 | 'dbi:Pg:' . Vote->config->{db}, |
---|
22 | undef, undef, |
---|
23 | { |
---|
24 | RaiseError => 0, |
---|
25 | AutoCommit => 0, |
---|
26 | PrintWarn => 0, |
---|
27 | PrintError => 1, |
---|
28 | } |
---|
29 | ) or return; |
---|
30 | |
---|
31 | bless { |
---|
32 | db => $db, |
---|
33 | }, $class; |
---|
34 | } |
---|
35 | |
---|
36 | sub db { $_[0]->{db} } |
---|
37 | |
---|
38 | sub list_running_vote { |
---|
39 | my ($self) = @_; |
---|
40 | |
---|
41 | my $sth = $self->db->prepare_cached( |
---|
42 | q{ |
---|
43 | select id from poll where start < now() and "end" > now() |
---|
44 | } |
---|
45 | ); |
---|
46 | |
---|
47 | $sth->execute; |
---|
48 | my @id; |
---|
49 | while(my $res = $sth->fetchrow_hashref) { |
---|
50 | push(@id, $res->{id}); |
---|
51 | } |
---|
52 | |
---|
53 | @id |
---|
54 | } |
---|
55 | |
---|
56 | sub vote_status { |
---|
57 | my ($self, $id) = @_; |
---|
58 | |
---|
59 | my $sth = $self->db->prepare_cached( |
---|
60 | q{ |
---|
61 | select start < now() as before |
---|
62 | "end" > now() as after |
---|
63 | where id = ? |
---|
64 | } |
---|
65 | ); |
---|
66 | $sth->execute($id); |
---|
67 | my $res = $sth->fetchrow_hashref; |
---|
68 | $sth->finish; |
---|
69 | $res or return; |
---|
70 | if ($res->{before}) { |
---|
71 | return 'BEFORE'; |
---|
72 | } elsif ($res->{after}) { |
---|
73 | return 'AFTER'; |
---|
74 | } else { |
---|
75 | return 'RUNNING'; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | sub vote_info { |
---|
80 | my ($self, $id) = @_; |
---|
81 | |
---|
82 | my $sth = $self->db->prepare_cached( |
---|
83 | q{ |
---|
84 | select * from poll where id = ? |
---|
85 | } |
---|
86 | ); |
---|
87 | |
---|
88 | $sth->execute($id); |
---|
89 | my $res = $sth->fetchrow_hashref; |
---|
90 | $sth->finish; |
---|
91 | $res |
---|
92 | } |
---|
93 | |
---|
94 | sub vote_signing { |
---|
95 | my ($self, $id) = @_; |
---|
96 | |
---|
97 | my $sth = $self->db->prepare_cached( |
---|
98 | q{ |
---|
99 | select * from voting left join signing |
---|
100 | on signing.key = voting.key |
---|
101 | where poll = ? order by voting.id |
---|
102 | } |
---|
103 | ); |
---|
104 | $sth->execute($id); |
---|
105 | my @people; |
---|
106 | while (my $res = $sth->fetchrow_hashref) { |
---|
107 | push(@people, $res); |
---|
108 | } |
---|
109 | @people |
---|
110 | } |
---|
111 | |
---|
112 | sub vote_choices { |
---|
113 | my ($self, $id) = @_; |
---|
114 | |
---|
115 | my $sth = $self->db->prepare_cached( |
---|
116 | q{ |
---|
117 | select * from choice where poll = ? |
---|
118 | order by label |
---|
119 | } |
---|
120 | ); |
---|
121 | $sth->execute($id); |
---|
122 | my @ch; |
---|
123 | while (my $res = $sth->fetchrow_hashref) { |
---|
124 | push(@ch, $res); |
---|
125 | } |
---|
126 | @ch |
---|
127 | } |
---|
128 | |
---|
129 | sub voting_info_id { |
---|
130 | my ($self, $id, $voteid) = @_; |
---|
131 | |
---|
132 | my $sth = $self->db->prepare_cached( |
---|
133 | q{ |
---|
134 | select * from voting where id = ? and poll = ? |
---|
135 | } |
---|
136 | ); |
---|
137 | $sth->execute($id, $voteid); |
---|
138 | my $res = $sth->fetchrow_hashref(); |
---|
139 | $sth->finish; |
---|
140 | $res |
---|
141 | } |
---|
142 | |
---|
143 | sub _register_signing { |
---|
144 | my ($self, $vid, $voteid, $referal) = @_; |
---|
145 | |
---|
146 | my $vinfo = $self->voting_info_id($vid, $voteid) or return; |
---|
147 | |
---|
148 | my $sth = $self->db->prepare_cached( |
---|
149 | q{ |
---|
150 | insert into signing (key, referal) values (?,?) |
---|
151 | } |
---|
152 | ); |
---|
153 | $sth->execute($vinfo->{key}, $referal) or do { |
---|
154 | $self->db->rollback; |
---|
155 | return; |
---|
156 | }; |
---|
157 | |
---|
158 | 1; |
---|
159 | } |
---|
160 | |
---|
161 | sub gen_uid { |
---|
162 | unpack("H*", join("", map { chr(rand(256)) } (0..15))) |
---|
163 | } |
---|
164 | |
---|
165 | sub _register_ballot { |
---|
166 | my ($self, $voteid, $choice) = @_; |
---|
167 | |
---|
168 | my $addb = $self->db->prepare_cached( |
---|
169 | q{ |
---|
170 | insert into ballot (id, poll) values (?,?) |
---|
171 | } |
---|
172 | ); |
---|
173 | my $uid = gen_uid; |
---|
174 | $addb->execute($uid, $voteid) or do { |
---|
175 | self->db->rollback; |
---|
176 | return; |
---|
177 | }; |
---|
178 | |
---|
179 | my $addbc = $self->db->prepare_cached( |
---|
180 | q{ |
---|
181 | insert into ballot_item (id, value) values (?,?) |
---|
182 | } |
---|
183 | ); |
---|
184 | foreach (@{ $choice || []}) { |
---|
185 | $addbc->execute($uid, $_) or do { |
---|
186 | $self->db->rollback; |
---|
187 | return; |
---|
188 | }; |
---|
189 | } |
---|
190 | |
---|
191 | $uid; |
---|
192 | } |
---|
193 | |
---|
194 | sub register_ballot { |
---|
195 | my ($self, $vid, $voteid, $choice, $referal) = @_; |
---|
196 | |
---|
197 | # First we register voting has voted |
---|
198 | $self->_register_signing($vid, $voteid, $referal) or return; # TODO error ? |
---|
199 | |
---|
200 | # registring choices |
---|
201 | my $uid = $self->_register_ballot($voteid, $choice) or return; |
---|
202 | |
---|
203 | # everything went fine, saving! |
---|
204 | $self->db->commit; |
---|
205 | |
---|
206 | $uid |
---|
207 | } |
---|
208 | |
---|
209 | sub signing_count { |
---|
210 | my ($self, $voteid) = @_; |
---|
211 | |
---|
212 | my $sth = $self->db->prepare_cached( |
---|
213 | q{ |
---|
214 | select count(*) from signing join voting |
---|
215 | on voting.key = signing.key where poll = ? |
---|
216 | } |
---|
217 | ); |
---|
218 | |
---|
219 | $sth->execute($voteid); |
---|
220 | my $res = $sth->fetchrow_hashref; |
---|
221 | $sth->finish; |
---|
222 | $res->{count} |
---|
223 | } |
---|
224 | |
---|
225 | sub ballot_count { |
---|
226 | my ($self, $voteid) = @_; |
---|
227 | |
---|
228 | my $sth = $self->db->prepare_cached( |
---|
229 | q{ |
---|
230 | select count(*) from ballot where poll = ? |
---|
231 | } |
---|
232 | ); |
---|
233 | |
---|
234 | $sth->execute($voteid); |
---|
235 | my $res = $sth->fetchrow_hashref; |
---|
236 | $sth->finish; |
---|
237 | $res->{count} |
---|
238 | } |
---|
239 | |
---|
240 | sub auth_voting { |
---|
241 | my ($self, $poll, $user, $password) = @_; |
---|
242 | my $userinfo = $self->voting_info_id($user, $poll) or return; |
---|
243 | |
---|
244 | $userinfo->{passwd} or return; |
---|
245 | if (crypt($password, $userinfo->{passwd} || '') eq $userinfo->{passwd}) { |
---|
246 | return 1; |
---|
247 | } else { |
---|
248 | return 0; |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | # Requete de decompte des voix: |
---|
253 | |
---|
254 | # select count(ballot.id), value from ballot left join ballot_item |
---|
255 | # on ballot.id = ballot_item.id where ballot.poll = 1 and invalid = 'false' |
---|
256 | # group by value |
---|
257 | # order by count |
---|
258 | |
---|
259 | =head1 AUTHOR |
---|
260 | |
---|
261 | Thauvin Olivier |
---|
262 | |
---|
263 | =head1 LICENSE |
---|
264 | |
---|
265 | This library is free software, you can redistribute it and/or modify |
---|
266 | it under the same terms as Perl itself. |
---|
267 | |
---|
268 | =cut |
---|
269 | |
---|
270 | 1; |
---|