- Location:
- /trunk
- Files:
-
- 2 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
/trunk/lib/Vote/Controller/Admin.pm
r20 r30 30 30 my ( $self, $c, undef, $id ) = @_; 31 31 $c->stash->{voteid} = $id; 32 my $vote = $c->model('Vote'); 32 33 33 # can't modify vote after date 34 if ($c->model('Vote')->vote_status($id) ne 'BEFORE') { 34 if ($vote->vote_status($id) eq 'BEFORE') { 35 if (my ($upload) = $c->req->upload('votinglist')) { 36 $vote->voting_from_file( 37 $id, 38 $upload->fh, 39 $c->req->param('delete'), 40 ) and $vote->db->commit; 41 } elsif ($c->req->param('addch')) { 42 $vote->vote_add_choice($id, $c->req->param('addch')) 43 and $vote->db->commit; 44 } elsif ($c->req->param('delch')) { 45 $vote->delete_choice($c->req->param('delch')) 46 and $vote->db->commit; 47 } elsif ($c->req->param('label')) { 48 $vote->vote_param( 49 $id, 50 map { $_ => ($c->req->param($_) || undef) } 51 qw(label description start end choice_count free_choice) 52 ) and $vote->db->commit; 53 } elsif($c->req->param('delvoting')) { 54 $vote->delete_voting($c->req->param('delvoting')) 55 and $vote->db->commit; 56 } elsif ($c->req->param('mail')) { 57 $vote->addupd_voting($id, $c->req->param('id'), $c->req->param('mail')) 58 and $vote->db->commit; 59 } elsif($c->req->param('mailpasswd')) { 60 $vote->mail_passwd_ifnul($id); 61 } 62 } elsif ($vote->vote_status($id) eq 'AFTER') { 63 foreach my $bid ($vote->list_vote_ballot_needvalid($id)) { 64 if (!$c->req->param($bid)) { 65 next; 66 } elsif($c->req->param($bid) eq 'invalid') { 67 $vote->mark_ballot_invalid($bid, 1); 68 $vote->db->commit; 69 } elsif($c->req->param($bid) eq 'valid') { 70 $vote->mark_ballot_invalid($bid, 0); 71 $vote->db->commit; 72 } 73 } 74 } else { 75 # can't modify vote after date 35 76 $c->stash->{template} = 'admin/denied.tt'; 36 77 return; 37 78 } 79 38 80 } 39 81 -
/trunk/lib/Vote/Controller/Ballot.pm
r20 r30 60 60 my %choices; 61 61 foreach ($vote->vote_choices($id)) { 62 $choices{$ _->{key}} = $_->{label};62 $choices{$vote->choice_info($_)->{key}} = $vote->choice_info($_)->{label}; 63 63 } 64 64 $c->stash->{choices} = { %choices }; … … 98 98 $c->req->address, 99 99 ); # TODO trap error 100 $c->forward('done'); 100 $c->stash->{template} = 'ballot/done.tt'; 101 $c->delete_session('Vote terminé'); 101 102 } 102 }103 104 sub done : Private {105 my ( $self, $c ) = @_;106 $c->response->body('Vote réussi.');107 $c->delete_session('Vote terminé');108 103 } 109 104 -
/trunk/lib/Vote/Model/Vote.pm
r20 r30 6 6 use Vote; 7 7 use DBI; 8 use Mail::Mailer; 8 9 9 10 =head1 NAME … … 37 38 sub db { $_[0]->{db} } 38 39 40 sub random_string { 41 my $lenght = $_[-1] || 8; 42 43 return join('', map { ('a'..'z', 'A'..'Z', 0..9)[rand 62] } (1..$lenght)); 44 } 45 46 sub gen_enc_passwd { 47 my ($self, $passwd) = @_; 48 49 $passwd ||= random_string(8); 50 return(crypt($passwd, '$1$' . random_string(8) . '$')); 51 } 52 39 53 sub list_comming_vote { 40 54 my ($self) = @_; … … 95 109 } 96 110 111 sub vote_param { 112 my ($self, $voteid, %attr) = @_; 113 114 keys %attr or return; 115 my @online_f = qw(label start end owner password); 116 117 my $sth = $self->db->prepare_cached( 118 q{update poll set } . 119 join(',', map { qq("$_" = ?) } grep { exists $attr{$_} } @online_f) . 120 q{ where id = ?} 121 ); 122 $sth->execute((map { $attr{$_} } grep { exists $attr{$_} } @online_f), $voteid) 123 or $self->db->rollback; 124 125 # vote settings in settings table 126 foreach my $var (keys %attr) { 127 grep { $var eq $_ } @online_f and next; 128 $self->vote_set_settings($voteid, $var, $attr{$var}); 129 } 130 1 131 } 132 97 133 sub vote_status { 98 134 my ($self, $id) = @_; … … 100 136 my $sth = $self->db->prepare_cached( 101 137 q{ 102 select start > now() as before,138 select (start > now() or start is null) as before, 103 139 "end" < now() as after 104 140 from poll … … 131 167 my $res = $sth->fetchrow_hashref; 132 168 $sth->finish; 169 if ($res) { 170 my $get = $self->db->prepare_cached( 171 q{select var, val from settings where poll = ?} 172 ); 173 $get->execute($id); 174 while (my $set = $get->fetchrow_hashref) { 175 $res->{$set->{var}} = $set->{val}; 176 } 177 } 133 178 $res 179 } 180 181 sub vote_set_settings { 182 my ($self, $poll, $var, $val) = @_; 183 184 my $upd = $self->db->prepare_cached( 185 q{update settings set val = ? where poll = ? and var = ?} 186 ); 187 188 if ($upd->execute($val, $poll, $var) == 0) { 189 my $add = $self->db->prepare_cached( 190 q{insert into settings (poll, var, val) values (?,?,?)} 191 ); 192 193 $add->execute($poll, $var, $val); 194 } 134 195 } 135 196 … … 139 200 my $sth = $self->db->prepare_cached( 140 201 q{ 141 select * from voting left join signing202 select *, voting.key as vkey from voting left join signing 142 203 on signing.key = voting.key 143 204 where poll = ? order by voting.id … … 152 213 } 153 214 215 sub vote_voting { 216 my ($self, $id) = @_; 217 218 my $sth = $self->db->prepare_cached( 219 q{ 220 select key from voting 221 where poll = ? order by voting.id 222 } 223 ); 224 $sth->execute($id); 225 my @people; 226 while (my $res = $sth->fetchrow_hashref) { 227 push(@people, $res->{key}); 228 } 229 @people 230 } 231 232 sub voting_info { 233 my ($self, $id) = @_; 234 235 my $sth = $self->db->prepare_cached( 236 q{ 237 select *, voting.key as vkey from voting left join signing 238 on signing.key = voting.key 239 where voting.key = ? 240 } 241 ); 242 $sth->execute($id); 243 244 my $res = $sth->fetchrow_hashref; 245 $sth->finish; 246 $res 247 } 248 154 249 sub vote_signing_count { 155 250 my ($self, $id) = @_; … … 172 267 my $sth = $self->db->prepare_cached( 173 268 q{ 174 select *from choice where poll = ?269 select key from choice where poll = ? 175 270 order by label 176 271 } … … 179 274 my @ch; 180 275 while (my $res = $sth->fetchrow_hashref) { 181 push(@ch, $res );276 push(@ch, $res->{key}); 182 277 } 183 278 @ch 279 } 280 281 sub choice_info { 282 my ($self, $chid) = @_; 283 my $sth = $self->db->prepare_cached( 284 q{select * from choice where key = ?} 285 ); 286 $sth->execute($chid); 287 my $res = $sth->fetchrow_hashref; 288 $sth->finish; 289 $res 290 } 291 292 sub vote_add_choice { 293 my ($self, $voteid, $label) = @_; 294 295 my $sth = $self->db->prepare_cached( 296 q{insert into choice (poll, label) values (?,?)} 297 ); 298 299 $sth->execute($voteid, $label) or do { 300 $self->db->rollback; 301 return; 302 }; 303 304 1 305 } 306 307 sub modify_choice { 308 my ($self, $chid, $label) = @_; 309 310 my $sth = $self->db->prepare_cached( 311 q{update choice set label = ? where key = ?} 312 ); 313 $sth->execute($label, $chid); 314 } 315 316 sub delete_choice { 317 my ($self, $chid) = @_; 318 319 my $sth = $self->db->prepare_cached( 320 q{delete from choice where key = ?} 321 ); 322 323 $sth->execute($chid); 184 324 } 185 325 … … 307 447 q{ 308 448 select count(*) from ballot where poll = ? 309 and id in (select id from ballot_item) 449 and id in (select id from ballot_item) and invalid = 'false' 310 450 } 311 451 ); … … 387 527 } 388 528 529 sub list_vote_ballot { 530 my ($self, $voteid) = @_; 531 532 my $sth = $self->db->prepare_cached( 533 q{ 534 select id from ballot where poll = ? 535 order by id 536 } 537 ); 538 $sth->execute($voteid); 539 my @ids; 540 while (my $res = $sth->fetchrow_hashref) { 541 push(@ids, $res->{id}); 542 } 543 @ids 544 } 545 546 sub list_vote_ballot_needvalid { 547 my ($self, $voteid) = @_; 548 549 my $sth = $self->db->prepare_cached( 550 q{ 551 select id from ballot where poll = ? 552 and invalid is null order by id 553 } 554 ); 555 $sth->execute($voteid); 556 my @ids; 557 while (my $res = $sth->fetchrow_hashref) { 558 push(@ids, $res->{id}); 559 } 560 @ids 561 } 562 563 sub ballot_info { 564 my ($self, $ballotid) = @_; 565 566 my $sth = $self->db->prepare_cached( 567 q{ select * from ballot where id = ? } 568 ); 569 570 $sth->execute($ballotid); 571 my $res = $sth->fetchrow_hashref; 572 $sth->finish; 573 $res 574 } 575 576 sub mark_ballot_invalid { 577 my ($self, $ballotid, $invalid) = @_; 578 579 my $sth = $self->db->prepare_cached( 580 q{update ballot set invalid = ? where id = ?} 581 ); 582 583 $sth->execute($invalid ? 't' : 'f', $ballotid); 584 } 585 586 sub ballot_items { 587 my ($self, $ballotid) = @_; 588 589 my $sth = $self->db->prepare_cached( 590 q{select *, value as v from ballot_item where id = ?} 591 ); 592 $sth->execute($ballotid); 593 my @ids; 594 while (my $res = $sth->fetchrow_hashref) { 595 push(@ids, $res); 596 } 597 @ids 598 } 599 600 sub addupd_voting { 601 my ($self, $voteid, $id, $mail) = @_; 602 603 my $upd = $self->db->prepare_cached( 604 q{ 605 update voting set mail = ? where poll = ? and id = ? 606 } 607 ); 608 609 if ($upd->execute($mail, $voteid, $id) == 0) { 610 my $add = $self->db->prepare_cached(q{ 611 insert into voting (poll, id, mail) values (?,?,?) 612 }); 613 614 $add->execute($voteid, $id, $mail); 615 } 616 } 617 618 sub delete_voting { 619 my ($self, $key) = @_; 620 621 my $sth = $self->db->prepare_cached( 622 q{delete from voting where key = ?} 623 ); 624 625 $sth->execute($key); 626 } 627 628 sub voting_from_file { 629 my ($self, $voteid, $fh, $delete) = @_; 630 631 if ($delete) { 632 my $sth = $self->db->prepare(q{delete from voting where poll = ?}); 633 $sth->execute($voteid); 634 } 635 636 while (my $line = <$fh>) { 637 chomp($line); 638 warn $line; 639 my ($id, $mail) = split(';', $line); 640 $id && $mail or do { 641 $self->db->rollback; 642 return; 643 }; 644 $self->addupd_voting($voteid, $id, $mail); 645 } 646 1; 647 } 648 649 sub mail_passwd_ifnul { 650 my ($self, $voteid, $mailinfo) = @_; 651 652 my $list_voting = $self->db->prepare_cached( 653 q{select key from voting where poll = ? and passwd is null or passwd = ''} 654 ); 655 656 $list_voting->execute($voteid); 657 while (my $res = $list_voting->fetchrow_hashref) { 658 $self->mail_voting_passwd($res->{key}, $mailinfo); 659 } 660 } 661 662 sub mail_voting_passwd { 663 my ($self, $id, $mailinfo) = @_; 664 665 my $vinfo = $self->voting_info($id) or return; 666 my $voteinfo = $self->vote_info($vinfo->{poll}); 667 668 my $passwd = random_string(8); 669 my $encpasswd = $self->gen_enc_passwd($passwd); 670 671 my $upd_voting = $self->db->prepare_cached( 672 q{update voting set passwd = ? where key = ?} 673 ); 674 675 $upd_voting->execute($encpasswd, $id); 676 677 # TODO complete this properly: 678 my $mailer = new Mail::Mailer 'smtp', Server => 'mailhost'; 679 $mailer->open({ 680 From => 'Voting system <nomail@nomail.com>', 681 To => $vinfo->{mail}, 682 Subject => 'Vote passwd', 683 }); 684 print $mailer <<EOF; 685 Vous êtes convié à voter: 686 $voteinfo->{label} 687 688 Votre identifiant est: $vinfo->{id} 689 Votre mot de passe est: $passwd 690 691 Cordialement. 692 EOF 693 $mailer->close; 694 695 $self->db->commit; 696 } 697 698 sub poll_request_info { 699 my ($self, $rid) = @_; 700 701 my $sth = $self->db->prepare_cached( 702 q{select * from poll_request where id = ?} 703 ); 704 705 $sth->execute($rid); 706 my $res = $sth->fetchrow_hashref; 707 $sth->finish; 708 $res 709 } 710 711 sub poll_from_request { 712 my ($self, $rid, $passwd) = @_; 713 my $rinfo = $self->poll_request_info($rid) or return; 714 715 my $encpasswd = $self->gen_enc_passwd($passwd); 716 717 my $getpollid = $self->db->prepare_cached( 718 q{select nextval('poll_id_seq')} 719 ); 720 $getpollid->execute(); 721 my $newpollid = $getpollid->fetchrow_hashref->{nextval}; 722 723 my $newpoll = $self->db->prepare_cached( 724 q{insert into poll (id, label, owner, password) values (?,?,?,?)} 725 ); 726 727 $newpoll->execute($newpollid, $rinfo->{label}, $rinfo->{mail}, $encpasswd); 728 729 my $delreq = $self->db->prepare_cached( 730 q{delete from poll_request where id = ?} 731 ); 732 733 $delreq->execute($rid); 734 $self->db->commit; 735 736 $newpollid 737 } 738 739 sub create_poll_request { 740 my ($self, %info) = @_; 741 742 $info{mail} or return; 743 my $addreq = $self->db->prepare_cached( 744 q{insert into poll_request (id, label, mail) values (?,?,?)} 745 ); 746 747 my $reqid = gen_uid; 748 749 $addreq->execute($reqid, $info{label}, $info{mail}); 750 my $mailer = new Mail::Mailer 'smtp', Server => 'mailhost'; 751 $mailer->open({ 752 From => 'Voting system <nomail@nomail.com>', 753 To => $info{mail}, 754 Subject => 'Votre nouveau vote', 755 }); 756 print $mailer <<EOF; 757 758 Vous avez demandez la création d'un nouveau vote: 759 $info{label} 760 761 Pour valider votre demande, veuiller allez visitez la page: 762 $info{url}/$reqid 763 764 A bientot 765 EOF 766 $mailer->close; 767 $self->db->commit; 768 1; 769 } 770 389 771 =head1 AUTHOR 390 772 -
/trunk/root/templates/admin/default.tt
r20 r30 1 1 [% vote = c.model('Vote') %] 2 2 3 [% IF vote.vote_status(voteid) == 'BEFORE' %] 4 <table border="1"> 5 <tr><th>Vote</th><th>Possibilité de vote</th></tr> 6 <tr> 7 <td valign="TOP"> 3 8 <form action="[% c.uri_for(voteid) %]"> 4 9 … … 11 16 Debut du vote: 12 17 <input type="text" name="start" value="[% c.req.param('start') || thisvote.start | html %]"> 18 <br> 13 19 Fin du vote: 14 20 <input type="text" name="end" value="[% c.req.param('end') || thisvote.end | html %]"> … … 20 26 <input type="text" name="free_choice" value="[% c.req.param('free_choice') || thisvote.free_choice | html %]"> 21 27 <br> 22 23 24 28 <input type="submit"> 25 29 </form> 30 </td> 31 <td valign="TOP"> 32 [% FOREACH choice = vote.vote_choices(voteid) %] 33 <form action="[% c.uri_for(voteid) %]"> 34 [% loop.count %] - [% vote.choice_info(choice).label | html %] 35 <input type="hidden" name="delch" value="[% vote.choice_info(choice).key %]"> 36 <input type="submit" name="del" value="Effacer"> 37 </form> 38 <br> 39 [% END %] 40 <form action="[% c.uri_for(voteid) %]"> 41 Ajouter un choix:<br> 42 <input type="text" name="addch"> 43 <input type="submit"> 44 </form> 45 </td> 46 </tr> 47 </table> 48 49 <hr> 50 51 <table border="1"> 52 <tr> 53 <td valign="TOP"> 54 [% signing = vote.vote_voting(voteid) %] 55 [% FOREACH thisvoting = signing %] 56 [% voting = vote.voting_info(thisvoting) %] 57 <form action="[% c.uri_for(voteid) %]"> 58 <input type="hidden" name="delvoting" value="[% voting.vkey %]"> 59 [% voting.id | html %] [% voting.mail | html %] 60 [% IF voting.passwd %]<i>(pass envoyé)</i>[% END %] 61 <input type="submit" name="delete" value="delete"><br> 62 </form> 63 [% END %] 64 </td> 65 <td valign="TOP"> 66 <form action="[% c.uri_for(voteid) %]"> 67 Login: <input type="text" name="id"><br> 68 Mail: <input type="text" name="mail"><br> 69 <input type="submit"> 70 </form> 71 <hr> 72 <form method="POST" ENCTYPE="multipart/form-data" action="[% c.uri_for(voteid) %]"> 73 <input type="file" name="votinglist"><br> 74 <input type="checkbox" name="delete">Effacer la liste des votants<br> 75 <input type="submit"> 76 </form> 77 <hr> 78 <form action="[% c.uri_for(voteid) %]"> 79 <input type="submit" name="mailpasswd" value="Envoyer les mot de passe"> 80 </form> 81 </td> 82 </tr> 83 </table> 84 85 [% ELSE %] 86 87 [% FOREACH bid = vote.list_vote_ballot_needvalid(voteid) %] 88 [% IF loop.first %] 89 <form action="[% c.uri_for(voteid) %]"> 90 <table border="1"> 91 [% END %] 92 <tr> 93 <td>[% bid %]</td> 94 <td> 95 [% FOREACH item = [ vote.ballot_items(bid) ] %] 96 [% item.v %]<br> 97 [% END %] 98 </td> 99 <td> 100 <input type="radio" name="[% bid %]" value="valid">est valide<br> 101 <input type="radio" name="[% bid %]" value="invalid">n'est pas valide<br> 102 </td> 103 </tr> 104 [% IF loop.last %] 105 </table> 106 <input type="submit"> 107 </form> 108 [% END %] 109 [% END %] 110 [% END %] -
/trunk/root/templates/ballot/default.tt
r20 r30 27 27 <form action="[% c.uri_for(voteid) %]"> 28 28 [% FOREACH choice = vote.vote_choices(voteid) %] 29 [% key = choice.key %]30 <input type="checkbox" name="sbal" value="[% choice.key %]"[% " checked" IF sbal.$key %]>31 [% choice.label | html %]<br>29 [% key = vote.choice_info(choice).key %] 30 <input type="checkbox" name="sbal" value="[% key %]"[% " checked" IF sbal.$key %]> 31 [% vote.choice_info(choice).label | html %]<br> 32 32 [% END %] 33 33 -
/trunk/root/templates/ballot/done.tt
r20 r30 1 1 Vote réussi. 2 3 Les résultats seront disponibles ici: 4 <a href="[% c.uri_for('/vote', voteid) %]">Ici</a>. -
/trunk/root/templates/vote/default.tt
r20 r30 11 11 <p>Resultats:</p> 12 12 Nombre de bulletin exprimés: [% vote.ballot_count_nonull(voteid) %] 13 [% IF vote.ballot_count(voteid) %] 13 14 ([% vote.ballot_count_nonull(voteid) * 100 / vote.ballot_count(voteid) %]%) 15 [% END %] 14 16 <br> 15 17 … … 20 22 <td>[% res.value | html %]</td> 21 23 <td>[% res.count %]</td> 22 <td>[% res.count * 100 / vote.ballot_count (voteid) %]</td>24 <td>[% res.count * 100 / vote.ballot_count_nonull(voteid) %]</td> 23 25 <td><img src="[% c.uri_for('/static', 'images', 'green-h.png') %]" 24 height="10px" width="[% res.count * 300 / vote.ballot_count(voteid) %]px"></td> 26 height="10px" width="[% res.count * 300 / vote.ballot_count_nonull(voteid) %]px"></td> 27 </tr> 28 [% END %] 29 </table> 30 31 Liste des bulletins: 32 <table border="1"> 33 <tr><th>numéro</th><th>Id</th><th>contenu</th></tr> 34 [% FOREACH id = vote.list_vote_ballot(voteid) %] 35 <tr> 36 <td>[% loop.count %]</td> 37 <td>[% id | html %]</td> 38 <td> 39 [% FOREACH item = [ vote.ballot_items(id) ] %] 40 [% item.v %]<br> 41 [% END %] 42 </td> 25 43 </tr> 26 44 [% END %]
Note: See TracChangeset
for help on using the changeset viewer.