Changeset 15 for trunk/lib


Ignore:
Timestamp:
03/07/09 16:30:29 (15 years ago)
Author:
nanardon
Message:
  • show vote results
File:
1 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 
Note: See TracChangeset for help on using the changeset viewer.