1 | package Vote::Model::Vote; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base 'Catalyst::Model'; |
---|
6 | use Vote; |
---|
7 | use DBI; |
---|
8 | use Mail::Mailer; |
---|
9 | |
---|
10 | =head1 NAME |
---|
11 | |
---|
12 | Vote::Model::Vote - Catalyst Model |
---|
13 | |
---|
14 | =head1 DESCRIPTION |
---|
15 | |
---|
16 | Catalyst Model. |
---|
17 | |
---|
18 | =cut |
---|
19 | |
---|
20 | sub new { |
---|
21 | my ($class) = @_; |
---|
22 | |
---|
23 | bless { |
---|
24 | db => _newdb(), |
---|
25 | }, $class; |
---|
26 | } |
---|
27 | |
---|
28 | sub _newdb { |
---|
29 | my $db = DBI->connect( |
---|
30 | 'dbi:Pg:' . Vote->config->{db}, |
---|
31 | undef, undef, |
---|
32 | { |
---|
33 | RaiseError => 0, |
---|
34 | AutoCommit => 0, |
---|
35 | PrintWarn => 0, |
---|
36 | PrintError => 1, |
---|
37 | } |
---|
38 | ) or return; |
---|
39 | $db->do(q{set DATESTYLE to 'DMY'}); |
---|
40 | return $db; |
---|
41 | } |
---|
42 | |
---|
43 | sub db { |
---|
44 | return $_[0]->{db} && $_[0]->{db}->ping |
---|
45 | ? $_[0]->{db} |
---|
46 | : $_[0]->_newdb(); |
---|
47 | } |
---|
48 | |
---|
49 | sub mail_header { |
---|
50 | return( |
---|
51 | 'Content-Type' => 'text/plain; charset=UTF-8; format=flowed', |
---|
52 | 'Content-Transfer-Encoding' => '8bit', |
---|
53 | 'X-Epoll-version' => $Vote::VERSION, |
---|
54 | ); |
---|
55 | } |
---|
56 | |
---|
57 | sub random_string { |
---|
58 | my $lenght = $_[-1] || 8; |
---|
59 | |
---|
60 | return join('', map { ('a'..'z', 'A'..'Z', 0..9)[rand 62] } (1..$lenght)); |
---|
61 | } |
---|
62 | |
---|
63 | sub gen_enc_passwd { |
---|
64 | my ($self, $passwd) = @_; |
---|
65 | |
---|
66 | $passwd ||= random_string(8); |
---|
67 | return(crypt($passwd, '$1$' . random_string(8) . '$')); |
---|
68 | } |
---|
69 | |
---|
70 | sub dbtime { |
---|
71 | my ($self) = @_; |
---|
72 | my $sth = $self->db->prepare( |
---|
73 | q{select to_char(now(), 'DD/MM/YYYY HH24:MI:SS') as d} |
---|
74 | ); |
---|
75 | |
---|
76 | $sth->execute(); |
---|
77 | my $res = $sth->fetchrow_hashref; |
---|
78 | $sth->finish; |
---|
79 | $res->{d}; |
---|
80 | } |
---|
81 | |
---|
82 | sub list_comming_vote { |
---|
83 | my ($self) = @_; |
---|
84 | |
---|
85 | my $sth = $self->db->prepare_cached( |
---|
86 | q{ |
---|
87 | select id from poll where |
---|
88 | (start > now() and "end" > now()) or |
---|
89 | "end" is null or start is null |
---|
90 | } |
---|
91 | ); |
---|
92 | |
---|
93 | $sth->execute; |
---|
94 | my @id; |
---|
95 | while(my $res = $sth->fetchrow_hashref) { |
---|
96 | push(@id, $res->{id}); |
---|
97 | } |
---|
98 | |
---|
99 | @id |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | sub list_running_vote { |
---|
104 | my ($self) = @_; |
---|
105 | |
---|
106 | my $sth = $self->db->prepare_cached( |
---|
107 | q{ |
---|
108 | select id from poll where start < now() and "end" > now() |
---|
109 | } |
---|
110 | ); |
---|
111 | |
---|
112 | $sth->execute; |
---|
113 | my @id; |
---|
114 | while(my $res = $sth->fetchrow_hashref) { |
---|
115 | push(@id, $res->{id}); |
---|
116 | } |
---|
117 | |
---|
118 | @id |
---|
119 | } |
---|
120 | |
---|
121 | sub list_closed_vote { |
---|
122 | my ($self) = @_; |
---|
123 | |
---|
124 | my $sth = $self->db->prepare_cached( |
---|
125 | q{ |
---|
126 | select id from poll where |
---|
127 | start < now() and "end" < now() |
---|
128 | } |
---|
129 | ); |
---|
130 | |
---|
131 | $sth->execute; |
---|
132 | my @id; |
---|
133 | while(my $res = $sth->fetchrow_hashref) { |
---|
134 | push(@id, $res->{id}); |
---|
135 | } |
---|
136 | |
---|
137 | @id |
---|
138 | } |
---|
139 | |
---|
140 | sub vote_param { |
---|
141 | my ($self, $voteid, %attr) = @_; |
---|
142 | |
---|
143 | keys %attr or return; |
---|
144 | my @online_f = qw(label start end owner password); |
---|
145 | |
---|
146 | if (grep { exists($attr{$_}) } @online_f) { |
---|
147 | my $sth = $self->db->prepare_cached( |
---|
148 | q{update poll set } . |
---|
149 | join(',', map { qq("$_" = ?) } grep { exists $attr{$_} } @online_f) . |
---|
150 | q{ where id = ?} |
---|
151 | ); |
---|
152 | $sth->execute((map { $attr{$_} } grep { exists $attr{$_} } @online_f), $voteid) |
---|
153 | or do { |
---|
154 | $self->db->rollback; |
---|
155 | return; |
---|
156 | }; |
---|
157 | } |
---|
158 | |
---|
159 | # vote settings in settings table |
---|
160 | foreach my $var (keys %attr) { |
---|
161 | grep { $var eq $_ } @online_f and next; |
---|
162 | $self->vote_set_settings($voteid, $var, $attr{$var}); |
---|
163 | } |
---|
164 | 1 |
---|
165 | } |
---|
166 | |
---|
167 | sub vote_status { |
---|
168 | my ($self, $id) = @_; |
---|
169 | |
---|
170 | my $sth = $self->db->prepare_cached( |
---|
171 | q{ |
---|
172 | select (start > now() or start is null) as before, |
---|
173 | "end" < now() as after |
---|
174 | from poll |
---|
175 | where id = ? |
---|
176 | } |
---|
177 | ); |
---|
178 | $sth->execute($id); |
---|
179 | my $res = $sth->fetchrow_hashref; |
---|
180 | $sth->finish; |
---|
181 | $res or return; |
---|
182 | if ($res->{before}) { |
---|
183 | return 'BEFORE'; |
---|
184 | } elsif ($res->{after}) { |
---|
185 | return 'AFTER'; |
---|
186 | } else { |
---|
187 | return 'RUNNING'; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | sub vote_info { |
---|
192 | my ($self, $id) = @_; |
---|
193 | |
---|
194 | my $sth = $self->db->prepare_cached( |
---|
195 | q{ |
---|
196 | select *, |
---|
197 | to_char("start", 'DD/MM/YYYY') as dstart, |
---|
198 | to_char("start", 'HH24:MI:SS') as hstart, |
---|
199 | to_char("end", 'DD/MM/YYYY') as dend, |
---|
200 | to_char("end", 'HH24:MI:SS') as hend |
---|
201 | from poll where id = ? |
---|
202 | } |
---|
203 | ); |
---|
204 | |
---|
205 | $sth->execute($id); |
---|
206 | my $res = $sth->fetchrow_hashref; |
---|
207 | $sth->finish; |
---|
208 | if ($res) { |
---|
209 | my $get = $self->db->prepare_cached( |
---|
210 | q{select var, val from settings where poll = ?} |
---|
211 | ); |
---|
212 | $get->execute($id); |
---|
213 | while (my $set = $get->fetchrow_hashref) { |
---|
214 | $res->{$set->{var}} = $set->{val}; |
---|
215 | } |
---|
216 | } |
---|
217 | $res->{free_choice} ||= 0; # avoiding undef |
---|
218 | $res |
---|
219 | } |
---|
220 | |
---|
221 | sub vote_set_settings { |
---|
222 | my ($self, $poll, $var, $val) = @_; |
---|
223 | |
---|
224 | my $upd = $self->db->prepare_cached( |
---|
225 | q{update settings set val = ? where poll = ? and var = ?} |
---|
226 | ); |
---|
227 | |
---|
228 | if ($upd->execute($val, $poll, $var) == 0) { |
---|
229 | my $add = $self->db->prepare_cached( |
---|
230 | q{insert into settings (poll, var, val) values (?,?,?)} |
---|
231 | ); |
---|
232 | |
---|
233 | $add->execute($poll, $var, $val); |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | sub vote_signing { |
---|
238 | my ($self, $id) = @_; |
---|
239 | |
---|
240 | my $sth = $self->db->prepare_cached( |
---|
241 | q{ |
---|
242 | select *, voting.key as vkey from voting left join signing |
---|
243 | on signing.key = voting.key |
---|
244 | where poll = ? order by voting.mail |
---|
245 | } |
---|
246 | ); |
---|
247 | $sth->execute($id); |
---|
248 | my @people; |
---|
249 | while (my $res = $sth->fetchrow_hashref) { |
---|
250 | push(@people, $res); |
---|
251 | } |
---|
252 | @people |
---|
253 | } |
---|
254 | |
---|
255 | sub vote_voting { |
---|
256 | my ($self, $voteid) = @_; |
---|
257 | |
---|
258 | my $sth = $self->db->prepare_cached( |
---|
259 | q{ |
---|
260 | select key from voting |
---|
261 | where poll = ? order by voting.mail |
---|
262 | } |
---|
263 | ); |
---|
264 | $sth->execute($voteid); |
---|
265 | my @people; |
---|
266 | while (my $res = $sth->fetchrow_hashref) { |
---|
267 | push(@people, $res->{key}); |
---|
268 | } |
---|
269 | @people |
---|
270 | } |
---|
271 | |
---|
272 | sub voting_info { |
---|
273 | my ($self, $id) = @_; |
---|
274 | |
---|
275 | my $sth = $self->db->prepare_cached( |
---|
276 | q{ |
---|
277 | select *, voting.key as vkey from voting left join signing |
---|
278 | on signing.key = voting.key |
---|
279 | where voting.key = ? |
---|
280 | } |
---|
281 | ); |
---|
282 | $sth->execute($id); |
---|
283 | |
---|
284 | my $res = $sth->fetchrow_hashref; |
---|
285 | $sth->finish; |
---|
286 | $res |
---|
287 | } |
---|
288 | |
---|
289 | sub vote_choices { |
---|
290 | my ($self, $id) = @_; |
---|
291 | |
---|
292 | my $sth = $self->db->prepare_cached( |
---|
293 | q{ |
---|
294 | select key from choice where poll = ? |
---|
295 | order by label |
---|
296 | } |
---|
297 | ); |
---|
298 | $sth->execute($id); |
---|
299 | my @ch; |
---|
300 | while (my $res = $sth->fetchrow_hashref) { |
---|
301 | push(@ch, $res->{key}); |
---|
302 | } |
---|
303 | @ch |
---|
304 | } |
---|
305 | |
---|
306 | sub choice_info { |
---|
307 | my ($self, $chid) = @_; |
---|
308 | my $sth = $self->db->prepare_cached( |
---|
309 | q{select * from choice where key = ?} |
---|
310 | ); |
---|
311 | $sth->execute($chid); |
---|
312 | my $res = $sth->fetchrow_hashref; |
---|
313 | $sth->finish; |
---|
314 | $res |
---|
315 | } |
---|
316 | |
---|
317 | sub vote_add_choice { |
---|
318 | my ($self, $voteid, $label) = @_; |
---|
319 | |
---|
320 | my $sth = $self->db->prepare_cached( |
---|
321 | q{insert into choice (poll, label) values (?,?)} |
---|
322 | ); |
---|
323 | |
---|
324 | $sth->execute($voteid, $label) or do { |
---|
325 | $self->db->rollback; |
---|
326 | return; |
---|
327 | }; |
---|
328 | |
---|
329 | 1 |
---|
330 | } |
---|
331 | |
---|
332 | sub modify_choice { |
---|
333 | my ($self, $chid, $label) = @_; |
---|
334 | |
---|
335 | my $sth = $self->db->prepare_cached( |
---|
336 | q{update choice set label = ? where key = ?} |
---|
337 | ); |
---|
338 | $sth->execute($label, $chid); |
---|
339 | } |
---|
340 | |
---|
341 | sub delete_choice { |
---|
342 | my ($self, $chid) = @_; |
---|
343 | |
---|
344 | my $sth = $self->db->prepare_cached( |
---|
345 | q{delete from choice where key = ?} |
---|
346 | ); |
---|
347 | |
---|
348 | $sth->execute($chid); |
---|
349 | } |
---|
350 | |
---|
351 | sub voting_info_id { |
---|
352 | my ($self, $mail, $voteid) = @_; |
---|
353 | |
---|
354 | my $sth = $self->db->prepare_cached( |
---|
355 | q{ |
---|
356 | select * from voting where mail = ? and poll = ? |
---|
357 | } |
---|
358 | ); |
---|
359 | $sth->execute($mail, $voteid); |
---|
360 | my $res = $sth->fetchrow_hashref(); |
---|
361 | $sth->finish; |
---|
362 | $res |
---|
363 | } |
---|
364 | |
---|
365 | sub _register_signing { |
---|
366 | my ($self, $mail, $voteid, $referal) = @_; |
---|
367 | |
---|
368 | my $vinfo = $self->voting_info_id($mail, $voteid) or return; |
---|
369 | |
---|
370 | my $sth = $self->db->prepare_cached( |
---|
371 | q{ |
---|
372 | insert into signing (key, referal) values (?,?) |
---|
373 | } |
---|
374 | ); |
---|
375 | $sth->execute($vinfo->{key}, $referal) or do { |
---|
376 | $self->db->rollback; |
---|
377 | return; |
---|
378 | }; |
---|
379 | |
---|
380 | 1; |
---|
381 | } |
---|
382 | |
---|
383 | sub gen_uid { |
---|
384 | unpack("H*", join("", map { chr(rand(256)) } (0..15))) |
---|
385 | } |
---|
386 | |
---|
387 | sub _register_ballot { |
---|
388 | my ($self, $voteid, $choice, $fchoice) = @_; |
---|
389 | |
---|
390 | my $addb = $self->db->prepare_cached( |
---|
391 | q{ |
---|
392 | insert into ballot (id, poll, invalid) values (?,?,?) |
---|
393 | } |
---|
394 | ); |
---|
395 | my $uid = gen_uid; |
---|
396 | $addb->execute($uid, $voteid, scalar(@{$fchoice || []}) ? undef : 'f') or do { |
---|
397 | self->db->rollback; |
---|
398 | return; |
---|
399 | }; |
---|
400 | |
---|
401 | my $addbc = $self->db->prepare_cached( |
---|
402 | q{ |
---|
403 | insert into ballot_item (id, value, fromlist) values (?,?,?) |
---|
404 | } |
---|
405 | ); |
---|
406 | foreach (@{ $choice || []}) { |
---|
407 | $addbc->execute($uid, $_, 't') or do { |
---|
408 | $self->db->rollback; |
---|
409 | return; |
---|
410 | }; |
---|
411 | } |
---|
412 | foreach (@{ $fchoice || []}) { |
---|
413 | $_ or next; |
---|
414 | $addbc->execute($uid, $_, 'f') or do { |
---|
415 | $self->db->rollback; |
---|
416 | return; |
---|
417 | }; |
---|
418 | } |
---|
419 | |
---|
420 | $uid; |
---|
421 | } |
---|
422 | |
---|
423 | sub register_ballot { |
---|
424 | my ($self, $vid, $voteid, $choice, $fchoice, $referal) = @_; |
---|
425 | |
---|
426 | my $uid; |
---|
427 | for (0..2) { # 3 try |
---|
428 | # First we register voting has voted |
---|
429 | $self->_register_signing($vid, $voteid, $referal) or return; # TODO error ? |
---|
430 | |
---|
431 | # registring choices |
---|
432 | $uid = $self->_register_ballot($voteid, $choice, $fchoice); |
---|
433 | defined($uid) and last; |
---|
434 | |
---|
435 | } |
---|
436 | # everything went fine, saving! |
---|
437 | $self->db->commit; |
---|
438 | |
---|
439 | |
---|
440 | $uid |
---|
441 | } |
---|
442 | |
---|
443 | sub mail_ballot_confirm { |
---|
444 | my ($self, $vid, $voteid, $info) = @_; |
---|
445 | my $voteinfo = $self->vote_info($voteid) or return; |
---|
446 | $info->{ballotid} or return; |
---|
447 | my $mailer = new Mail::Mailer 'smtp', Server => (Vote->config->{smtp} || 'localhost'); |
---|
448 | $mailer->open({ |
---|
449 | From => $vid, # TODO allow to configure this |
---|
450 | To => $vid, |
---|
451 | Subject => 'Confirmation de vote: ' . $voteinfo->{label}, |
---|
452 | mail_header(), |
---|
453 | }); |
---|
454 | print $mailer <<EOF; |
---|
455 | |
---|
456 | Vous venez de participer au vote: |
---|
457 | |
---|
458 | -------- |
---|
459 | $voteinfo->{label} |
---|
460 | -------- |
---|
461 | |
---|
462 | Votre bulletin est idéntifié sous le numéro: |
---|
463 | $info->{ballotid} |
---|
464 | |
---|
465 | Les résultats seront disponibles à cet url: |
---|
466 | $info->{url} |
---|
467 | |
---|
468 | Cordialement. |
---|
469 | EOF |
---|
470 | $mailer->close |
---|
471 | or warn "couldn't send whole message: $!\n"; |
---|
472 | |
---|
473 | } |
---|
474 | |
---|
475 | sub vote_voting_count { |
---|
476 | my ($self, $id) = @_; |
---|
477 | |
---|
478 | my $sth = $self->db->prepare_cached( |
---|
479 | q{ |
---|
480 | select count(*) from voting |
---|
481 | where poll = ? |
---|
482 | } |
---|
483 | ); |
---|
484 | $sth->execute($id); |
---|
485 | my $res = $sth->fetchrow_hashref; |
---|
486 | $sth->finish; |
---|
487 | $res->{count} |
---|
488 | } |
---|
489 | |
---|
490 | sub signing_count { vote_signing_count(@_) } |
---|
491 | |
---|
492 | sub vote_signing_count { |
---|
493 | my ($self, $voteid) = @_; |
---|
494 | |
---|
495 | my $sth = $self->db->prepare_cached( |
---|
496 | q{ |
---|
497 | select count(*) from signing join voting |
---|
498 | on voting.key = signing.key where poll = ? |
---|
499 | } |
---|
500 | ); |
---|
501 | |
---|
502 | $sth->execute($voteid); |
---|
503 | my $res = $sth->fetchrow_hashref; |
---|
504 | $sth->finish; |
---|
505 | $res->{count} |
---|
506 | } |
---|
507 | |
---|
508 | sub ballot_count { vote_ballot_count(@_) } |
---|
509 | |
---|
510 | sub vote_ballot_count { |
---|
511 | my ($self, $voteid) = @_; |
---|
512 | |
---|
513 | my $sth = $self->db->prepare_cached( |
---|
514 | q{ |
---|
515 | select count(*) from ballot where poll = ? |
---|
516 | } |
---|
517 | ); |
---|
518 | |
---|
519 | $sth->execute($voteid); |
---|
520 | my $res = $sth->fetchrow_hashref; |
---|
521 | $sth->finish; |
---|
522 | $res->{count} |
---|
523 | } |
---|
524 | |
---|
525 | sub ballot_count_nonull { vote_ballot_count_nonull(@_) } |
---|
526 | |
---|
527 | sub vote_ballot_count_nonull { |
---|
528 | my ($self, $voteid) = @_; |
---|
529 | |
---|
530 | my $sth = $self->db->prepare_cached( |
---|
531 | q{ |
---|
532 | select count(*) from ballot where poll = ? |
---|
533 | and id in (select id from ballot_item) and |
---|
534 | (invalid = 'false' or invalid is null) |
---|
535 | } |
---|
536 | ); |
---|
537 | |
---|
538 | $sth->execute($voteid); |
---|
539 | my $res = $sth->fetchrow_hashref; |
---|
540 | $sth->finish; |
---|
541 | $res->{count} |
---|
542 | } |
---|
543 | |
---|
544 | sub auth_voting { |
---|
545 | my ($self, $poll, $mail, $password) = @_; |
---|
546 | my $userinfo = $self->voting_info_id($mail, $poll) or return; |
---|
547 | |
---|
548 | $userinfo->{passwd} or return; |
---|
549 | if (crypt($password, $userinfo->{passwd} || '') eq $userinfo->{passwd}) { |
---|
550 | return 1; |
---|
551 | } else { |
---|
552 | return 0; |
---|
553 | } |
---|
554 | } |
---|
555 | |
---|
556 | sub auth_poll { |
---|
557 | my ($self, $voteid, $passwd) = @_; |
---|
558 | |
---|
559 | my $vinfo = $self->vote_info($voteid) or return; |
---|
560 | |
---|
561 | $vinfo->{password} or return; |
---|
562 | $passwd or return; |
---|
563 | if (crypt($passwd, $vinfo->{password} || '') eq $vinfo->{password}) { |
---|
564 | return 1; |
---|
565 | } else { |
---|
566 | return 0; |
---|
567 | } |
---|
568 | } |
---|
569 | |
---|
570 | sub voting_has_sign { |
---|
571 | my ($self, $poll, $user) = @_; |
---|
572 | |
---|
573 | my $sth = $self->db->prepare_cached( |
---|
574 | q{ |
---|
575 | select date from signing join voting |
---|
576 | on voting.key = signing.key |
---|
577 | where poll = ? and mail = ? |
---|
578 | } |
---|
579 | ); |
---|
580 | |
---|
581 | $sth->execute($poll, $user); |
---|
582 | my $res = $sth->fetchrow_hashref; |
---|
583 | $sth->finish; |
---|
584 | return $res->{date} |
---|
585 | } |
---|
586 | |
---|
587 | # Requete de decompte des voix: |
---|
588 | |
---|
589 | sub vote_results_count { |
---|
590 | my ($self, $voteid) = @_; |
---|
591 | |
---|
592 | my $sth = $self->db->prepare( |
---|
593 | q{ |
---|
594 | select count(ballot.id), value from ballot left join ballot_item |
---|
595 | on ballot.id = ballot_item.id where ballot.poll = ? and invalid = 'false' |
---|
596 | group by value |
---|
597 | order by count |
---|
598 | } |
---|
599 | ); |
---|
600 | $sth->execute($voteid); |
---|
601 | my @results; |
---|
602 | while (my $res = $sth->fetchrow_hashref) { |
---|
603 | push(@results, $res); |
---|
604 | } |
---|
605 | @results; |
---|
606 | } |
---|
607 | |
---|
608 | sub vote_results_nonull { |
---|
609 | my ($self, $voteid) = @_; |
---|
610 | |
---|
611 | my $sth = $self->db->prepare( |
---|
612 | q{ |
---|
613 | select count(ballot.id), coalesce(corrected, value) as value |
---|
614 | from ballot join ballot_item |
---|
615 | on ballot.id = ballot_item.id where ballot.poll = ? and |
---|
616 | (invalid = 'false' or invalid is null) |
---|
617 | group by coalesce(corrected, value) |
---|
618 | order by count desc |
---|
619 | } |
---|
620 | ); |
---|
621 | $sth->execute($voteid); |
---|
622 | my @results; |
---|
623 | while (my $res = $sth->fetchrow_hashref) { |
---|
624 | push(@results, $res); |
---|
625 | } |
---|
626 | \@results; |
---|
627 | } |
---|
628 | |
---|
629 | sub list_vote_ballot { |
---|
630 | my ($self, $voteid) = @_; |
---|
631 | |
---|
632 | my $sth = $self->db->prepare_cached( |
---|
633 | q{ |
---|
634 | select id from ballot where poll = ? |
---|
635 | order by id |
---|
636 | } |
---|
637 | ); |
---|
638 | $sth->execute($voteid); |
---|
639 | my @ids; |
---|
640 | while (my $res = $sth->fetchrow_hashref) { |
---|
641 | push(@ids, $res->{id}); |
---|
642 | } |
---|
643 | @ids |
---|
644 | } |
---|
645 | |
---|
646 | sub list_vote_ballot_needvalid { |
---|
647 | my ($self, $voteid) = @_; |
---|
648 | |
---|
649 | my $sth = $self->db->prepare_cached( |
---|
650 | q{ |
---|
651 | select id from ballot where poll = ? |
---|
652 | and invalid is null order by id |
---|
653 | } |
---|
654 | ); |
---|
655 | $sth->execute($voteid); |
---|
656 | my @ids; |
---|
657 | while (my $res = $sth->fetchrow_hashref) { |
---|
658 | push(@ids, $res->{id}); |
---|
659 | } |
---|
660 | @ids |
---|
661 | } |
---|
662 | |
---|
663 | sub ballot_info { |
---|
664 | my ($self, $ballotid) = @_; |
---|
665 | |
---|
666 | my $sth = $self->db->prepare_cached( |
---|
667 | q{ select * from ballot where id = ? } |
---|
668 | ); |
---|
669 | |
---|
670 | $sth->execute($ballotid); |
---|
671 | my $res = $sth->fetchrow_hashref; |
---|
672 | $sth->finish; |
---|
673 | $res |
---|
674 | } |
---|
675 | |
---|
676 | sub mark_ballot_invalid { |
---|
677 | my ($self, $ballotid, $invalid) = @_; |
---|
678 | |
---|
679 | my $sth = $self->db->prepare_cached( |
---|
680 | q{update ballot set invalid = ? where id = ?} |
---|
681 | ); |
---|
682 | |
---|
683 | $sth->execute($invalid ? 't' : 'f', $ballotid); |
---|
684 | } |
---|
685 | |
---|
686 | sub ballot_items { |
---|
687 | my ($self, $ballotid) = @_; |
---|
688 | |
---|
689 | my $sth = $self->db->prepare_cached( |
---|
690 | q{select *, value as v from ballot_item where id = ?} |
---|
691 | ); |
---|
692 | $sth->execute($ballotid); |
---|
693 | my @ids; |
---|
694 | while (my $res = $sth->fetchrow_hashref) { |
---|
695 | push(@ids, $res); |
---|
696 | } |
---|
697 | \@ids |
---|
698 | } |
---|
699 | |
---|
700 | sub vote_ballot_untrusted_values { |
---|
701 | my ($self, $voteid) = @_; |
---|
702 | |
---|
703 | my $getval = $self->db->prepare_cached( |
---|
704 | q{ |
---|
705 | select value from ballot join ballot_item |
---|
706 | on ballot.id = ballot_item.id |
---|
707 | where poll = ? and fromlist = false and corrected is null |
---|
708 | group by value order by value |
---|
709 | } |
---|
710 | ); |
---|
711 | $getval->execute($voteid); |
---|
712 | my @vals; |
---|
713 | while (my $res = $getval->fetchrow_hashref) { |
---|
714 | push(@vals, $res->{value}); |
---|
715 | } |
---|
716 | @vals |
---|
717 | } |
---|
718 | |
---|
719 | sub vote_ballot_values { |
---|
720 | my ($self, $voteid) = @_; |
---|
721 | |
---|
722 | my $getval = $self->db->prepare_cached( |
---|
723 | q{ |
---|
724 | select coalesce(corrected, value) as value from ballot join ballot_item |
---|
725 | on ballot.id = ballot_item.id |
---|
726 | where poll = ? |
---|
727 | group by coalesce(corrected, value) order by coalesce(corrected, value) |
---|
728 | } |
---|
729 | ); |
---|
730 | $getval->execute($voteid); |
---|
731 | my @vals; |
---|
732 | while (my $res = $getval->fetchrow_hashref) { |
---|
733 | push(@vals, $res->{value}); |
---|
734 | } |
---|
735 | @vals |
---|
736 | } |
---|
737 | |
---|
738 | sub vote_map_value { |
---|
739 | my ($self, $voteid, $from, $to) = @_; |
---|
740 | |
---|
741 | my $sth = $self->db->prepare_cached( |
---|
742 | q{ |
---|
743 | update ballot_item set corrected = ? where |
---|
744 | id in (select id from ballot where poll = ?) |
---|
745 | and (value = ? or corrected = ?) |
---|
746 | } |
---|
747 | ); |
---|
748 | |
---|
749 | $sth->execute($to, $voteid, $from, $from) or $self->db->rollback; |
---|
750 | $self->db->commit; |
---|
751 | } |
---|
752 | |
---|
753 | sub addupd_voting { |
---|
754 | my ($self, $voteid, $mail, $id) = @_; |
---|
755 | |
---|
756 | $mail =~ s/\s*$//; |
---|
757 | $mail =~ s/^\s*//; |
---|
758 | $id =~ s/\s*$//; |
---|
759 | $id =~ s/^\s//; |
---|
760 | my $upd = $self->db->prepare_cached( |
---|
761 | q{ |
---|
762 | update voting set label = ? where mail = ? and poll = ? |
---|
763 | } |
---|
764 | ); |
---|
765 | |
---|
766 | if ($upd->execute($id || '', $mail, $voteid) == 0) { |
---|
767 | my $add = $self->db->prepare_cached(q{ |
---|
768 | insert into voting (poll, label, mail) values (?,?,?) |
---|
769 | }); |
---|
770 | |
---|
771 | $add->execute($voteid, $id || '', $mail); |
---|
772 | } |
---|
773 | } |
---|
774 | |
---|
775 | sub delete_voting { |
---|
776 | my ($self, $key) = @_; |
---|
777 | |
---|
778 | $self->voting_has_sign($key) and return; |
---|
779 | my $sth = $self->db->prepare_cached( |
---|
780 | q{delete from voting where key = ?} |
---|
781 | ); |
---|
782 | |
---|
783 | $sth->execute($key); |
---|
784 | } |
---|
785 | |
---|
786 | sub voting_from_file { |
---|
787 | my ($self, $voteid, $fh, $delete) = @_; |
---|
788 | |
---|
789 | if ($delete) { |
---|
790 | my $sth = $self->db->prepare(q{delete from voting where poll = ?}); |
---|
791 | $sth->execute($voteid); |
---|
792 | } |
---|
793 | |
---|
794 | while (my $line = <$fh>) { |
---|
795 | chomp($line); |
---|
796 | my ($mail, $name) = split(';', $line); |
---|
797 | $mail or do { |
---|
798 | $self->db->rollback; |
---|
799 | return; |
---|
800 | }; |
---|
801 | $self->addupd_voting($voteid, $mail, $name || ''); |
---|
802 | } |
---|
803 | 1; |
---|
804 | } |
---|
805 | |
---|
806 | sub mail_passwd_ifnul { |
---|
807 | my ($self, $voteid, $mailinfo) = @_; |
---|
808 | |
---|
809 | my $list_voting = $self->db->prepare_cached( |
---|
810 | q{select key from voting where poll = ? and passwd is null or passwd = ''} |
---|
811 | ); |
---|
812 | |
---|
813 | $list_voting->execute($voteid); |
---|
814 | while (my $res = $list_voting->fetchrow_hashref) { |
---|
815 | $self->mail_voting_passwd($res->{key}, $mailinfo); |
---|
816 | } |
---|
817 | } |
---|
818 | |
---|
819 | sub mail_voting_passwd { |
---|
820 | my ($self, $id, $mailinfo) = @_; |
---|
821 | |
---|
822 | my $vinfo = $self->voting_info($id) or return; |
---|
823 | my $voteinfo = $self->vote_info($vinfo->{poll}); |
---|
824 | $voteinfo->{description} ||= ""; |
---|
825 | |
---|
826 | my $passwd = random_string(8); |
---|
827 | my $encpasswd = $self->gen_enc_passwd($passwd); |
---|
828 | |
---|
829 | my $upd_voting = $self->db->prepare_cached( |
---|
830 | q{update voting set passwd = ? where key = ?} |
---|
831 | ); |
---|
832 | |
---|
833 | $upd_voting->execute($encpasswd, $id); |
---|
834 | |
---|
835 | my $date = $voteinfo->{dstart} && $voteinfo->{dend} |
---|
836 | ? sprintf("\n" . 'Vous pourrez voter entre le %s %s et le %s %s' . "\n", |
---|
837 | $voteinfo->{dstart}, $voteinfo->{hstart}, $voteinfo->{dend}, $voteinfo->{hend}) |
---|
838 | : ''; |
---|
839 | |
---|
840 | # TODO complete this properly: |
---|
841 | my $mailer = new Mail::Mailer 'smtp', Server => (Vote->config->{smtp} || 'localhost'); |
---|
842 | $mailer->open({ |
---|
843 | From => $voteinfo->{owner}, |
---|
844 | To => $vinfo->{mail}, |
---|
845 | Subject => 'Invitation a voter: ' . $voteinfo->{label}, |
---|
846 | 'X-Epoll-poll' => $id, |
---|
847 | mail_header(), |
---|
848 | }); |
---|
849 | print $mailer <<EOF; |
---|
850 | Vous êtes convié à participer a ce vote: |
---|
851 | |
---|
852 | -------- |
---|
853 | $voteinfo->{label} |
---|
854 | -------- |
---|
855 | $voteinfo->{description} |
---|
856 | -------- |
---|
857 | |
---|
858 | Ã l'adresse: |
---|
859 | |
---|
860 | $mailinfo->{voteurl} |
---|
861 | $date |
---|
862 | Votre identifiant est: $vinfo->{mail} |
---|
863 | Votre mot de passe est: $passwd |
---|
864 | |
---|
865 | Conserver précieusement ces identifiants, il ne vous seront pas retransmit. |
---|
866 | |
---|
867 | Cordialement. |
---|
868 | EOF |
---|
869 | $mailer->close or warn "couldn't send whole message: $!\n"; |
---|
870 | |
---|
871 | $self->db->commit; |
---|
872 | } |
---|
873 | |
---|
874 | sub poll_request_info { |
---|
875 | my ($self, $rid) = @_; |
---|
876 | |
---|
877 | my $sth = $self->db->prepare_cached( |
---|
878 | q{select * from poll_request where id = ?} |
---|
879 | ); |
---|
880 | |
---|
881 | $sth->execute($rid); |
---|
882 | my $res = $sth->fetchrow_hashref; |
---|
883 | $sth->finish; |
---|
884 | $res |
---|
885 | } |
---|
886 | |
---|
887 | sub poll_from_request { |
---|
888 | my ($self, $rid, $passwd) = @_; |
---|
889 | my $rinfo = $self->poll_request_info($rid) or return; |
---|
890 | |
---|
891 | my $encpasswd = $self->gen_enc_passwd($passwd); |
---|
892 | |
---|
893 | my $getpollid = $self->db->prepare_cached( |
---|
894 | q{select nextval('poll_id_seq')} |
---|
895 | ); |
---|
896 | $getpollid->execute(); |
---|
897 | my $newpollid = $getpollid->fetchrow_hashref->{nextval}; |
---|
898 | |
---|
899 | my $newpoll = $self->db->prepare_cached( |
---|
900 | q{insert into poll (id, label, owner, password) values (?,?,?,?)} |
---|
901 | ); |
---|
902 | |
---|
903 | $newpoll->execute($newpollid, $rinfo->{label}, $rinfo->{mail}, $encpasswd); |
---|
904 | # set some default |
---|
905 | $self->vote_param($newpollid, |
---|
906 | free_choice => 0, |
---|
907 | choice_count => 1, |
---|
908 | ); |
---|
909 | |
---|
910 | my $delreq = $self->db->prepare_cached( |
---|
911 | q{delete from poll_request where id = ?} |
---|
912 | ); |
---|
913 | |
---|
914 | $delreq->execute($rid); |
---|
915 | $self->db->commit; |
---|
916 | |
---|
917 | $newpollid |
---|
918 | } |
---|
919 | |
---|
920 | sub create_poll_request { |
---|
921 | my ($self, %info) = @_; |
---|
922 | |
---|
923 | $info{mail} or return; |
---|
924 | my $addreq = $self->db->prepare_cached( |
---|
925 | q{insert into poll_request (id, label, mail) values (?,?,?)} |
---|
926 | ); |
---|
927 | |
---|
928 | my $reqid = gen_uid; |
---|
929 | |
---|
930 | $addreq->execute($reqid, $info{label}, $info{mail}); |
---|
931 | my $mailer = new Mail::Mailer 'smtp', Server => (Vote->config->{smtp} || 'localhost'); |
---|
932 | $mailer->open({ |
---|
933 | From => 'Voting system <nomail@nomail.com>', # TODO allow to configure this |
---|
934 | To => $info{mail}, |
---|
935 | Subject => 'Votre nouveau vote', |
---|
936 | mail_header(), |
---|
937 | }); |
---|
938 | print $mailer <<EOF; |
---|
939 | |
---|
940 | Vous avez demandez la création d'un nouveau vote: |
---|
941 | $info{label} |
---|
942 | |
---|
943 | Pour valider votre demande, veuiller allez visitez la page: |
---|
944 | $info{url}/$reqid |
---|
945 | |
---|
946 | A bientÃŽt |
---|
947 | EOF |
---|
948 | $mailer->close |
---|
949 | or warn "couldn't send whole message: $!\n"; |
---|
950 | $self->db->commit; |
---|
951 | 1; |
---|
952 | } |
---|
953 | |
---|
954 | =head1 AUTHOR |
---|
955 | |
---|
956 | Thauvin Olivier |
---|
957 | |
---|
958 | =head1 LICENSE |
---|
959 | |
---|
960 | This library is free software, you can redistribute it and/or modify |
---|
961 | it under the same terms as Perl itself or CeCILL. |
---|
962 | |
---|
963 | =cut |
---|
964 | |
---|
965 | 1; |
---|