Changeset 15


Ignore:
Timestamp:
03/07/09 16:30:29 (15 years ago)
Author:
nanardon
Message:
  • show vote results
Location:
trunk
Files:
9 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Vote/Model/Vote.pm

    r14 r15  
    44use warnings; 
    55use base 'Catalyst::Model'; 
     6use Vote; 
    67use DBI; 
    78 
     
    3637sub db { $_[0]->{db} } 
    3738 
    38 sub list_running_vote { 
     39sub list_comming_vote { 
    3940    my ($self) = @_; 
    4041 
    4142    my $sth = $self->db->prepare_cached( 
    4243        q{ 
    43         select id from poll where start < now() and "end" > now() 
     44        select id from poll where 
     45        (start > now() and "end" > now()) or 
     46        "end" is null or start is null 
    4447        } 
    4548    ); 
     
    5457} 
    5558 
     59 
     60sub 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 
     78sub 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 
    5697sub vote_status { 
    5798    my ($self, $id) = @_; 
     
    59100    my $sth = $self->db->prepare_cached( 
    60101        q{ 
    61         select start < now() as before 
    62                "end"   > now() as after 
     102        select start > now() as before, 
     103               "end" < now() as after 
     104        from poll 
    63105        where id = ? 
    64106        } 
     
    110152} 
    111153 
     154sub 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 
    112169sub vote_choices { 
    113170    my ($self, $id) = @_; 
     
    238295} 
    239296 
     297sub ballot_count_nonull { 
     298    my ($self, $voteid) = @_; 
     299 
     300    my $sth = $self->db->prepare_cached( 
     301        q{ 
     302        select count(*) from ballot where poll = ? 
     303        and id in (select id from ballot_item) 
     304        } 
     305    ); 
     306 
     307    $sth->execute($voteid); 
     308    my $res = $sth->fetchrow_hashref; 
     309    $sth->finish; 
     310    warn $res->{count}; 
     311    $res->{count} 
     312} 
     313 
    240314sub auth_voting { 
    241315    my ($self, $poll, $user, $password) = @_; 
     
    269343# Requete de decompte des voix: 
    270344 
    271 # select count(ballot.id), value from ballot left join ballot_item 
    272 # on ballot.id = ballot_item.id where ballot.poll = 1 and invalid = 'false' 
    273 # group by value 
    274 # order by count 
     345sub vote_results_count { 
     346    my ($self, $voteid) = @_; 
     347 
     348    my $sth = $self->db->prepare( 
     349        q{ 
     350        select count(ballot.id), value from ballot left join ballot_item 
     351        on ballot.id = ballot_item.id where ballot.poll = ? and invalid = 'false' 
     352        group by value 
     353        order by count 
     354        } 
     355    ); 
     356    $sth->execute($voteid); 
     357    my @results; 
     358    while (my $res = $sth->fetchrow_hashref) { 
     359        push(@results, $res); 
     360    } 
     361    @results; 
     362} 
     363 
     364sub vote_results_nonull { 
     365    my ($self, $voteid) = @_; 
     366 
     367    my $sth = $self->db->prepare( 
     368        q{ 
     369        select count(ballot.id), value from ballot join ballot_item 
     370        on ballot.id = ballot_item.id where ballot.poll = ? and invalid = 'false' 
     371        group by value 
     372        order by count desc 
     373        } 
     374    ); 
     375    $sth->execute($voteid); 
     376    my @results; 
     377    while (my $res = $sth->fetchrow_hashref) { 
     378        push(@results, $res); 
     379    } 
     380    \@results; 
     381} 
    275382 
    276383=head1 AUTHOR 
  • trunk/root/templates/ballot/default.tt

    r12 r15  
    11[% vote = c.model('Vote') %] 
    22 
    3 [% thisvote = vote.vote_info(voteid) %] 
    4 [% thisvote.label | html %] 
    5 [% signing = [vote.vote_signing(voteid)] %] 
    6 Ont voté: [% vote.signing_count(voteid) %] /  [% signing.size %]<br> 
    7 Bulletins: [% vote.ballot_count(voteid) %]<br> 
    8 <hr> 
     3[% INLCUDE 'includes/poll.tt' %] 
    94 
    105[% IF c.req.param('ballot') %] 
  • trunk/root/templates/default.tt

    r3 r15  
    22[% vote = c.model('Vote') %] 
    33 
     4<p>Vote en cours</p> 
     5 
    46[% FOREACH id = vote.list_running_vote %] 
     7<p><a href="[% c.uri_for( 'vote', id ) %]">[% vote.vote_info(id).label | html %]</a> 
     8<a href="[% c.uri_for( 'ballot', id ) %]">Voter</a></p> 
     9[% END %] 
     10 
     11<p>Vote à venir</p> 
     12 
     13[% FOREACH id = vote.list_comming_vote %] 
    514<p><a href="[% c.uri_for( 'vote', id ) %]">[% vote.vote_info(id).label | html %]</a></p> 
    615[% END %] 
     16 
     17<p>Vote Fermé</p> 
     18 
     19[% FOREACH id = vote.list_closed_vote %] 
     20<p><a href="[% c.uri_for( 'vote', id ) %]">[% vote.vote_info(id).label | html %]</a></p> 
     21[% END %] 
  • trunk/root/templates/vote/default.tt

    r12 r15  
    11[% vote = c.model('Vote') %] 
    22 
    3 [% thisvote = vote.vote_info(voteid) %] 
    4 [% thisvote.label | html %]<br> 
     3[% INCLUDE 'includes/poll.tt' %] 
     4 
     5[% SWITCH vote.vote_status(voteid) %] 
     6[% CASE 'RUNNING' %] 
     7<p>en cours</p> 
     8[% CASE 'BEFORE' %] 
     9<p>Avant</p> 
     10[% CASE 'AFTER' %] 
     11<p>Resultats:</p> 
     12Nombre de bulletin exprimés: [% vote.ballot_count_nonull(voteid) %] 
     13([% vote.ballot_count_nonull(voteid) * 100 / vote.ballot_count(voteid) %]%) 
     14<br> 
     15 
     16<table border="1"> 
     17<tr><th>choix</th><th>Nb voix</th><th>%</th><th></th></tr> 
     18[% FOREACH res = vote.vote_results_nonull(voteid) %] 
     19<tr> 
     20<td>[% res.value | html %]</td> 
     21<td>[% res.count %]</td> 
     22<td>[% res.count * 100 / vote.ballot_count(voteid) %]</td> 
     23<td><img src="[% c.uri_for('/static', 'images', 'green-h.png') %]"  
     24height="10px" width="[% res.count * 300 / vote.ballot_count(voteid) %]px"></td> 
     25</tr> 
     26[% END %] 
     27</table> 
     28[% END %] 
     29 
    530[% signing = [ vote.vote_signing(voteid) ] %] 
    6 Ont voté: [% vote.signing_count(voteid) %] /  [% signing.size %]<br> 
    7 Bulletins: [% vote.ballot_count(voteid) %]<br> 
    8  
    931[% FOREACH voting = signing %] 
    1032[% voting.id | html %] [% voting.date %]<br> 
Note: See TracChangeset for help on using the changeset viewer.