1 | package Vote::Model::Vote; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base 'Catalyst::Model'; |
---|
6 | use Vote; |
---|
7 | use DBI; |
---|
8 | |
---|
9 | =head1 NAME |
---|
10 | |
---|
11 | Vote::Model::Vote - Catalyst Model |
---|
12 | |
---|
13 | =head1 DESCRIPTION |
---|
14 | |
---|
15 | Catalyst Model. |
---|
16 | |
---|
17 | =cut |
---|
18 | |
---|
19 | sub new { |
---|
20 | my ($class) = @_; |
---|
21 | my $db = DBI->connect( |
---|
22 | 'dbi:Pg:' . Vote->config->{db}, |
---|
23 | undef, undef, |
---|
24 | { |
---|
25 | RaiseError => 0, |
---|
26 | AutoCommit => 0, |
---|
27 | PrintWarn => 0, |
---|
28 | PrintError => 1, |
---|
29 | } |
---|
30 | ) or return; |
---|
31 | |
---|
32 | bless { |
---|
33 | db => $db, |
---|
34 | }, $class; |
---|
35 | } |
---|
36 | |
---|
37 | sub db { $_[0]->{db} } |
---|
38 | |
---|
39 | sub list_comming_vote { |
---|
40 | my ($self) = @_; |
---|
41 | |
---|
42 | my $sth = $self->db->prepare_cached( |
---|
43 | q{ |
---|
44 | select id from poll where |
---|
45 | (start > now() and "end" > now()) or |
---|
46 | "end" is null or start is null |
---|
47 | } |
---|
48 | ); |
---|
49 | |
---|
50 | $sth->execute; |
---|
51 | my @id; |
---|
52 | while(my $res = $sth->fetchrow_hashref) { |
---|
53 | push(@id, $res->{id}); |
---|
54 | } |
---|
55 | |
---|
56 | @id |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | sub list_running_vote { |
---|
61 | my ($self) = @_; |
---|
62 | |
---|
63 | my $sth = $self->db->prepare_cached( |
---|
64 | q{ |
---|
65 | select id from poll where start < now() and "end" > now() |
---|
66 | } |
---|
67 | ); |
---|
68 | |
---|
69 | $sth->execute; |
---|
70 | my @id; |
---|
71 | while(my $res = $sth->fetchrow_hashref) { |
---|
72 | push(@id, $res->{id}); |
---|
73 | } |
---|
74 | |
---|
75 | @id |
---|
76 | } |
---|
77 | |
---|
78 | sub list_closed_vote { |
---|
79 | my ($self) = @_; |
---|
80 | |
---|
81 | my $sth = $self->db->prepare_cached( |
---|
82 | q{ |
---|
83 | select id from poll where |
---|
84 | start < now() and "end" < now() |
---|
85 | } |
---|
86 | ); |
---|
87 | |
---|
88 | $sth->execute; |
---|
89 | my @id; |
---|
90 | while(my $res = $sth->fetchrow_hashref) { |
---|
91 | push(@id, $res->{id}); |
---|
92 | } |
---|
93 | |
---|
94 | @id |
---|
95 | } |
---|
96 | |
---|
97 | sub vote_status { |
---|
98 | my ($self, $id) = @_; |
---|
99 | |
---|
100 | my $sth = $self->db->prepare_cached( |
---|
101 | q{ |
---|
102 | select start > now() as before, |
---|
103 | "end" < now() as after |
---|
104 | from poll |
---|
105 | where id = ? |
---|
106 | } |
---|
107 | ); |
---|
108 | $sth->execute($id); |
---|
109 | my $res = $sth->fetchrow_hashref; |
---|
110 | $sth->finish; |
---|
111 | $res or return; |
---|
112 | if ($res->{before}) { |
---|
113 | return 'BEFORE'; |
---|
114 | } elsif ($res->{after}) { |
---|
115 | return 'AFTER'; |
---|
116 | } else { |
---|
117 | return 'RUNNING'; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | sub vote_info { |
---|
122 | my ($self, $id) = @_; |
---|
123 | |
---|
124 | my $sth = $self->db->prepare_cached( |
---|
125 | q{ |
---|
126 | select * from poll where id = ? |
---|
127 | } |
---|
128 | ); |
---|
129 | |
---|
130 | $sth->execute($id); |
---|
131 | my $res = $sth->fetchrow_hashref; |
---|
132 | $sth->finish; |
---|
133 | $res |
---|
134 | } |
---|
135 | |
---|
136 | sub vote_signing { |
---|
137 | my ($self, $id) = @_; |
---|
138 | |
---|
139 | my $sth = $self->db->prepare_cached( |
---|
140 | q{ |
---|
141 | select * from voting left join signing |
---|
142 | on signing.key = voting.key |
---|
143 | where poll = ? order by voting.id |
---|
144 | } |
---|
145 | ); |
---|
146 | $sth->execute($id); |
---|
147 | my @people; |
---|
148 | while (my $res = $sth->fetchrow_hashref) { |
---|
149 | push(@people, $res); |
---|
150 | } |
---|
151 | @people |
---|
152 | } |
---|
153 | |
---|
154 | sub vote_signing_count { |
---|
155 | my ($self, $id) = @_; |
---|
156 | |
---|
157 | my $sth = $self->db->prepare_cached( |
---|
158 | q{ |
---|
159 | select count(*) from voting |
---|
160 | where poll = ? |
---|
161 | } |
---|
162 | ); |
---|
163 | $sth->execute($id); |
---|
164 | my $res = $sth->fetchrow_hashref; |
---|
165 | $sth->finish; |
---|
166 | $res->{count} |
---|
167 | } |
---|
168 | |
---|
169 | sub vote_choices { |
---|
170 | my ($self, $id) = @_; |
---|
171 | |
---|
172 | my $sth = $self->db->prepare_cached( |
---|
173 | q{ |
---|
174 | select * from choice where poll = ? |
---|
175 | order by label |
---|
176 | } |
---|
177 | ); |
---|
178 | $sth->execute($id); |
---|
179 | my @ch; |
---|
180 | while (my $res = $sth->fetchrow_hashref) { |
---|
181 | push(@ch, $res); |
---|
182 | } |
---|
183 | @ch |
---|
184 | } |
---|
185 | |
---|
186 | sub voting_info_id { |
---|
187 | my ($self, $id, $voteid) = @_; |
---|
188 | |
---|
189 | my $sth = $self->db->prepare_cached( |
---|
190 | q{ |
---|
191 | select * from voting where id = ? and poll = ? |
---|
192 | } |
---|
193 | ); |
---|
194 | $sth->execute($id, $voteid); |
---|
195 | my $res = $sth->fetchrow_hashref(); |
---|
196 | $sth->finish; |
---|
197 | $res |
---|
198 | } |
---|
199 | |
---|
200 | sub _register_signing { |
---|
201 | my ($self, $vid, $voteid, $referal) = @_; |
---|
202 | |
---|
203 | my $vinfo = $self->voting_info_id($vid, $voteid) or return; |
---|
204 | |
---|
205 | my $sth = $self->db->prepare_cached( |
---|
206 | q{ |
---|
207 | insert into signing (key, referal) values (?,?) |
---|
208 | } |
---|
209 | ); |
---|
210 | $sth->execute($vinfo->{key}, $referal) or do { |
---|
211 | $self->db->rollback; |
---|
212 | return; |
---|
213 | }; |
---|
214 | |
---|
215 | 1; |
---|
216 | } |
---|
217 | |
---|
218 | sub gen_uid { |
---|
219 | unpack("H*", join("", map { chr(rand(256)) } (0..15))) |
---|
220 | } |
---|
221 | |
---|
222 | sub _register_ballot { |
---|
223 | my ($self, $voteid, $choice, $fchoice) = @_; |
---|
224 | |
---|
225 | my $addb = $self->db->prepare_cached( |
---|
226 | q{ |
---|
227 | insert into ballot (id, poll, invalid) values (?,?,?) |
---|
228 | } |
---|
229 | ); |
---|
230 | my $uid = gen_uid; |
---|
231 | $addb->execute($uid, $voteid, scalar(@{$fchoice || []}) ? undef : 'f') or do { |
---|
232 | self->db->rollback; |
---|
233 | return; |
---|
234 | }; |
---|
235 | |
---|
236 | my $addbc = $self->db->prepare_cached( |
---|
237 | q{ |
---|
238 | insert into ballot_item (id, value, fromlist) values (?,?,?) |
---|
239 | } |
---|
240 | ); |
---|
241 | foreach (@{ $choice || []}) { |
---|
242 | $addbc->execute($uid, $_, 't') or do { |
---|
243 | $self->db->rollback; |
---|
244 | return; |
---|
245 | }; |
---|
246 | } |
---|
247 | foreach (@{ $fchoice || []}) { |
---|
248 | $addbc->execute($uid, $_, 'f') or do { |
---|
249 | $self->db->rollback; |
---|
250 | return; |
---|
251 | }; |
---|
252 | } |
---|
253 | |
---|
254 | $uid; |
---|
255 | } |
---|
256 | |
---|
257 | sub register_ballot { |
---|
258 | my ($self, $vid, $voteid, $choice, $fchoice, $referal) = @_; |
---|
259 | |
---|
260 | # First we register voting has voted |
---|
261 | $self->_register_signing($vid, $voteid, $referal) or return; # TODO error ? |
---|
262 | |
---|
263 | # registring choices |
---|
264 | my $uid = $self->_register_ballot($voteid, $choice, $fchoice) or return; |
---|
265 | |
---|
266 | # everything went fine, saving! |
---|
267 | $self->db->commit; |
---|
268 | |
---|
269 | $uid |
---|
270 | } |
---|
271 | |
---|
272 | sub signing_count { |
---|
273 | my ($self, $voteid) = @_; |
---|
274 | |
---|
275 | my $sth = $self->db->prepare_cached( |
---|
276 | q{ |
---|
277 | select count(*) from signing join voting |
---|
278 | on voting.key = signing.key where poll = ? |
---|
279 | } |
---|
280 | ); |
---|
281 | |
---|
282 | $sth->execute($voteid); |
---|
283 | my $res = $sth->fetchrow_hashref; |
---|
284 | $sth->finish; |
---|
285 | $res->{count} |
---|
286 | } |
---|
287 | |
---|
288 | sub ballot_count { |
---|
289 | my ($self, $voteid) = @_; |
---|
290 | |
---|
291 | my $sth = $self->db->prepare_cached( |
---|
292 | q{ |
---|
293 | select count(*) from ballot where poll = ? |
---|
294 | } |
---|
295 | ); |
---|
296 | |
---|
297 | $sth->execute($voteid); |
---|
298 | my $res = $sth->fetchrow_hashref; |
---|
299 | $sth->finish; |
---|
300 | $res->{count} |
---|
301 | } |
---|
302 | |
---|
303 | sub ballot_count_nonull { |
---|
304 | my ($self, $voteid) = @_; |
---|
305 | |
---|
306 | my $sth = $self->db->prepare_cached( |
---|
307 | q{ |
---|
308 | select count(*) from ballot where poll = ? |
---|
309 | and id in (select id from ballot_item) |
---|
310 | } |
---|
311 | ); |
---|
312 | |
---|
313 | $sth->execute($voteid); |
---|
314 | my $res = $sth->fetchrow_hashref; |
---|
315 | $sth->finish; |
---|
316 | warn $res->{count}; |
---|
317 | $res->{count} |
---|
318 | } |
---|
319 | |
---|
320 | sub auth_voting { |
---|
321 | my ($self, $poll, $user, $password) = @_; |
---|
322 | my $userinfo = $self->voting_info_id($user, $poll) or return; |
---|
323 | |
---|
324 | $userinfo->{passwd} or return; |
---|
325 | if (crypt($password, $userinfo->{passwd} || '') eq $userinfo->{passwd}) { |
---|
326 | return 1; |
---|
327 | } else { |
---|
328 | return 0; |
---|
329 | } |
---|
330 | } |
---|
331 | |
---|
332 | sub voting_has_sign { |
---|
333 | my ($self, $poll, $user) = @_; |
---|
334 | |
---|
335 | my $sth = $self->db->prepare_cached( |
---|
336 | q{ |
---|
337 | select date from signing join voting |
---|
338 | on voting.key = signing.key |
---|
339 | where poll = ? and id = ? |
---|
340 | } |
---|
341 | ); |
---|
342 | |
---|
343 | $sth->execute($poll, $user); |
---|
344 | my $res = $sth->fetchrow_hashref; |
---|
345 | $sth->finish; |
---|
346 | return $res->{date} |
---|
347 | } |
---|
348 | |
---|
349 | # Requete de decompte des voix: |
---|
350 | |
---|
351 | sub vote_results_count { |
---|
352 | my ($self, $voteid) = @_; |
---|
353 | |
---|
354 | my $sth = $self->db->prepare( |
---|
355 | q{ |
---|
356 | select count(ballot.id), value from ballot left join ballot_item |
---|
357 | on ballot.id = ballot_item.id where ballot.poll = ? and invalid = 'false' |
---|
358 | group by value |
---|
359 | order by count |
---|
360 | } |
---|
361 | ); |
---|
362 | $sth->execute($voteid); |
---|
363 | my @results; |
---|
364 | while (my $res = $sth->fetchrow_hashref) { |
---|
365 | push(@results, $res); |
---|
366 | } |
---|
367 | @results; |
---|
368 | } |
---|
369 | |
---|
370 | sub vote_results_nonull { |
---|
371 | my ($self, $voteid) = @_; |
---|
372 | |
---|
373 | my $sth = $self->db->prepare( |
---|
374 | q{ |
---|
375 | select count(ballot.id), value from ballot join ballot_item |
---|
376 | on ballot.id = ballot_item.id where ballot.poll = ? and invalid = 'false' |
---|
377 | group by value |
---|
378 | order by count desc |
---|
379 | } |
---|
380 | ); |
---|
381 | $sth->execute($voteid); |
---|
382 | my @results; |
---|
383 | while (my $res = $sth->fetchrow_hashref) { |
---|
384 | push(@results, $res); |
---|
385 | } |
---|
386 | \@results; |
---|
387 | } |
---|
388 | |
---|
389 | =head1 AUTHOR |
---|
390 | |
---|
391 | Thauvin Olivier |
---|
392 | |
---|
393 | =head1 LICENSE |
---|
394 | |
---|
395 | This library is free software, you can redistribute it and/or modify |
---|
396 | it under the same terms as Perl itself. |
---|
397 | |
---|
398 | =cut |
---|
399 | |
---|
400 | 1; |
---|