Changes in / [20:30]


Ignore:
Location:
/trunk
Files:
2 added
8 edited

Legend:

Unmodified
Added
Removed
  • /trunk/lib/Vote/Controller/Admin.pm

    r20 r30  
    3030    my ( $self, $c, undef, $id ) = @_; 
    3131    $c->stash->{voteid} = $id; 
     32    my $vote = $c->model('Vote'); 
    3233 
    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 
    3576        $c->stash->{template} = 'admin/denied.tt'; 
    3677        return; 
    3778    } 
     79 
    3880} 
    3981 
  • /trunk/lib/Vote/Controller/Ballot.pm

    r20 r30  
    6060    my %choices; 
    6161    foreach ($vote->vote_choices($id)) { 
    62         $choices{$_->{key}} = $_->{label}; 
     62        $choices{$vote->choice_info($_)->{key}} = $vote->choice_info($_)->{label}; 
    6363    } 
    6464    $c->stash->{choices} = { %choices }; 
     
    9898            $c->req->address, 
    9999        ); # TODO trap error 
    100         $c->forward('done'); 
     100        $c->stash->{template} = 'ballot/done.tt'; 
     101        $c->delete_session('Vote terminé'); 
    101102    } 
    102 } 
    103  
    104 sub done : Private { 
    105     my ( $self, $c ) = @_; 
    106     $c->response->body('Vote réussi.'); 
    107     $c->delete_session('Vote terminé'); 
    108103} 
    109104 
  • /trunk/lib/Vote/Model/Vote.pm

    r20 r30  
    66use Vote; 
    77use DBI; 
     8use Mail::Mailer; 
    89 
    910=head1 NAME 
     
    3738sub db { $_[0]->{db} } 
    3839 
     40sub random_string { 
     41    my $lenght = $_[-1] || 8; 
     42 
     43    return join('', map { ('a'..'z', 'A'..'Z', 0..9)[rand 62] } (1..$lenght)); 
     44} 
     45 
     46sub gen_enc_passwd { 
     47    my ($self, $passwd) = @_; 
     48 
     49    $passwd ||= random_string(8); 
     50    return(crypt($passwd, '$1$' . random_string(8) . '$')); 
     51} 
     52 
    3953sub list_comming_vote { 
    4054    my ($self) = @_; 
     
    95109} 
    96110 
     111sub 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 
    97133sub vote_status { 
    98134    my ($self, $id) = @_; 
     
    100136    my $sth = $self->db->prepare_cached( 
    101137        q{ 
    102         select start > now() as before, 
     138        select (start > now() or start is null) as before, 
    103139               "end" < now() as after 
    104140        from poll 
     
    131167    my $res = $sth->fetchrow_hashref; 
    132168    $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    } 
    133178    $res 
     179} 
     180 
     181sub 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    } 
    134195} 
    135196 
     
    139200    my $sth = $self->db->prepare_cached( 
    140201        q{ 
    141         select * from voting left join signing 
     202        select *, voting.key as vkey from voting left join signing 
    142203        on signing.key = voting.key 
    143204        where poll = ? order by voting.id 
     
    152213} 
    153214 
     215sub 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 
     232sub 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 
    154249sub vote_signing_count { 
    155250    my ($self, $id) = @_; 
     
    172267    my $sth = $self->db->prepare_cached( 
    173268        q{ 
    174         select * from choice where poll = ? 
     269        select key from choice where poll = ? 
    175270        order by label 
    176271        } 
     
    179274    my @ch; 
    180275    while (my $res = $sth->fetchrow_hashref) { 
    181         push(@ch, $res); 
     276        push(@ch, $res->{key}); 
    182277    } 
    183278    @ch 
     279} 
     280 
     281sub 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 
     292sub 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 
     307sub 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 
     316sub 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); 
    184324} 
    185325 
     
    307447        q{ 
    308448        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' 
    310450        } 
    311451    ); 
     
    387527} 
    388528 
     529sub 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 
     546sub 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 
     563sub 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 
     576sub 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 
     586sub 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 
     600sub 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 
     618sub 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 
     628sub 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 
     649sub 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 
     662sub 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; 
     685Vous êtes convié à voter: 
     686$voteinfo->{label} 
     687 
     688Votre identifiant est: $vinfo->{id} 
     689Votre mot de passe est: $passwd 
     690 
     691Cordialement. 
     692EOF 
     693    $mailer->close; 
     694 
     695    $self->db->commit; 
     696} 
     697 
     698sub 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 
     711sub 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 
     739sub 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 
     758Vous avez demandez la création d'un nouveau vote: 
     759$info{label} 
     760 
     761Pour valider votre demande, veuiller allez visitez la page: 
     762$info{url}/$reqid 
     763 
     764A bientot 
     765EOF 
     766    $mailer->close; 
     767    $self->db->commit; 
     768    1; 
     769} 
     770 
    389771=head1 AUTHOR 
    390772 
  • /trunk/root/templates/admin/default.tt

    r20 r30  
    11[% vote = c.model('Vote') %] 
    22 
     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"> 
    38<form action="[% c.uri_for(voteid) %]"> 
    49 
     
    1116Debut du vote: 
    1217<input type="text" name="start" value="[% c.req.param('start') || thisvote.start | html %]"> 
     18<br> 
    1319Fin du vote: 
    1420<input type="text" name="end" value="[% c.req.param('end') || thisvote.end | html %]"> 
     
    2026<input type="text" name="free_choice" value="[% c.req.param('free_choice') || thisvote.free_choice | html %]"> 
    2127<br> 
    22  
    23  
    2428<input type="submit"> 
    2529</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) %]"> 
     41Ajouter 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) %]"> 
     67Login: <input type="text" name="id"><br> 
     68Mail: <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  
    2727<form action="[% c.uri_for(voteid) %]"> 
    2828[% 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> 
    3232[% END %] 
    3333 
  • /trunk/root/templates/ballot/done.tt

    r20 r30  
    11Vote réussi. 
     2 
     3Les résultats seront disponibles ici: 
     4<a href="[% c.uri_for('/vote', voteid) %]">Ici</a>. 
  • /trunk/root/templates/vote/default.tt

    r20 r30  
    1111<p>Resultats:</p> 
    1212Nombre de bulletin exprimés: [% vote.ballot_count_nonull(voteid) %] 
     13[% IF vote.ballot_count(voteid) %] 
    1314([% vote.ballot_count_nonull(voteid) * 100 / vote.ballot_count(voteid) %]%) 
     15[% END %] 
    1416<br> 
    1517 
     
    2022<td>[% res.value | html %]</td> 
    2123<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> 
    2325<td><img src="[% c.uri_for('/static', 'images', 'green-h.png') %]"  
    24 height="10px" width="[% res.count * 300 / vote.ballot_count(voteid) %]px"></td> 
     26height="10px" width="[% res.count * 300 / vote.ballot_count_nonull(voteid) %]px"></td> 
     27</tr> 
     28[% END %] 
     29</table> 
     30 
     31Liste 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> 
    2543</tr> 
    2644[% END %] 
Note: See TracChangeset for help on using the changeset viewer.