Changeset 214 for trunk/lib


Ignore:
Timestamp:
04/17/09 15:32:47 (15 years ago)
Author:
nanardon
Message:
  • more results function, support empty ballots in voices count
Location:
trunk/lib/Vote/DB
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Vote/DB/Poll.pm

    r213 r214  
    513513} 
    514514 
    515 sub voting_count { 
    516     my ($self) = @_; 
    517  
    518     my $sth = $self->db->prepare_cached( 
    519         q{ 
    520         select count(*) from voting 
    521         where poll = ? 
    522         } 
    523     ); 
    524     $sth->execute($self->voteid); 
    525     my $res = $sth->fetchrow_hashref; 
    526     $sth->finish; 
    527     $res->{count} 
    528 } 
    529  
    530 sub signing_count { 
    531     my ($self) = @_; 
    532  
    533     my $sth = $self->db->prepare_cached( 
    534         q{ 
    535         select count(*) from signing join voting 
    536         on voting.key = signing.key where poll = ? 
    537         } 
    538     ); 
    539  
    540     $sth->execute($self->voteid); 
    541     my $res = $sth->fetchrow_hashref; 
    542     $sth->finish; 
    543     $res->{count} 
    544 } 
    545  
    546515sub is_crypted { 
    547516    my ($self) = @_; 
    548517    return $self->info->{public_key} ? 1 : 0; 
    549 } 
    550  
    551 sub ballot_count { 
    552     my ($self) = @_; 
    553     return $self->is_crypted 
    554         ? $self->ballot_count_crypt 
    555         : $self->ballot_count_clear; 
    556 } 
    557  
    558 sub ballot_count_clear { 
    559     my ($self) = @_; 
    560  
    561     my $sth = $self->db->prepare_cached( 
    562         q{select count(*) from ballot where poll = ?} 
    563     ); 
    564  
    565     $sth->execute($self->voteid); 
    566     my $res = $sth->fetchrow_hashref; 
    567     $sth->finish; 
    568     $res->{count} 
    569 } 
    570  
    571 sub ballot_count_crypt { 
    572     my ($self) = @_; 
    573  
    574     my $sth = $self->db->prepare_cached( 
    575         q{select count(*) from ballot_enc where poll = ?} 
    576     ); 
    577  
    578     $sth->execute($self->voteid); 
    579     my $res = $sth->fetchrow_hashref; 
    580     $sth->finish; 
    581     $res->{count} 
    582518} 
    583519 
     
    890826    $privkey 
    891827} 
     828 
     829######### 
     830# Count # 
     831######### 
     832 
     833sub ballot_count { 
     834    my ($self) = @_; 
     835    return $self->is_crypted 
     836        ? $self->ballot_count_crypt 
     837        : $self->ballot_count_clear; 
     838} 
     839 
     840sub ballot_count_clear { 
     841    my ($self) = @_; 
     842 
     843    my $sth = $self->db->prepare_cached( 
     844        q{select count(*) from ballot where poll = ?} 
     845    ); 
     846 
     847    $sth->execute($self->voteid); 
     848    my $res = $sth->fetchrow_hashref; 
     849    $sth->finish; 
     850    $res->{count} 
     851} 
     852 
     853sub ballot_count_crypt { 
     854    my ($self) = @_; 
     855 
     856    my $sth = $self->db->prepare_cached( 
     857        q{select count(*) from ballot_enc where poll = ?} 
     858    ); 
     859 
     860    $sth->execute($self->voteid); 
     861    my $res = $sth->fetchrow_hashref; 
     862    $sth->finish; 
     863    $res->{count} 
     864} 
     865 
     866sub voting_count { 
     867    my ($self) = @_; 
     868 
     869    my $sth = $self->db->prepare_cached( 
     870        q{ 
     871        select count(*) from voting 
     872        where poll = ? 
     873        } 
     874    ); 
     875    $sth->execute($self->voteid); 
     876    my $res = $sth->fetchrow_hashref; 
     877    $sth->finish; 
     878    $res->{count} 
     879} 
     880 
     881sub signing_count { 
     882    my ($self) = @_; 
     883 
     884    my $sth = $self->db->prepare_cached( 
     885        q{ 
     886        select count(*) from signing join voting 
     887        on voting.key = signing.key where poll = ? 
     888        } 
     889    ); 
     890 
     891    $sth->execute($self->voteid); 
     892    my $res = $sth->fetchrow_hashref; 
     893    $sth->finish; 
     894    $res->{count} 
     895} 
     896 
     897sub not_signing_count { 
     898    my ($self) = @_; 
     899    my $sth = $self->db->prepare_cached( 
     900        q{ 
     901        select count(*) from voting where key 
     902        not in (select key from signing) 
     903        } 
     904    ); 
     905 
     906    $sth->execute($self->voteid); 
     907    my $res = $sth->fetchrow_hashref; 
     908    $sth->finish; 
     909    $res->{count} 
     910} 
     911 
    892912=head1 AUTHOR 
    893913 
  • trunk/lib/Vote/DB/Poll/Results.pm

    r213 r214  
    2727sub absolute_majority { 
    2828    my ($self) = @_; 
    29     my $ballot_count = $self->ballot_count_nonull; 
     29    my $ballot_count = $self->voices_ballot_count; 
    3030    return int($ballot_count / 2) + 1; 
    3131}     
     
    4848} 
    4949 
    50 sub ballot_count_nonull { 
     50sub ballot_count_nonull {not_empty_ballot_count(@_)} 
     51 
     52sub voices_ballot_count { 
     53    my ($self) = @_; 
     54    return $self->not_empty_ballot_count + 
     55        ($self->info('empty_ballot_has_voice') 
     56            ? $self->empty_ballot_count 
     57            : 0); 
     58} 
     59 
     60 
     61sub empty_ballot_count { 
     62    my ($self) = @_; 
     63 
     64    my $sth = $self->db->prepare_cached( 
     65        q{ 
     66        select count(*) from ballot where poll = ? 
     67        and id not in (select id from ballot_item) and 
     68        (invalid = 'false' or invalid is null) 
     69        } 
     70    ); 
     71 
     72    $sth->execute($self->voteid); 
     73    my $res = $sth->fetchrow_hashref; 
     74    $sth->finish; 
     75    $res->{count} 
     76} 
     77 
     78sub not_empty_ballot_count { 
    5179    my ($self) = @_; 
    5280 
     
    75103} 
    76104 
     105sub results { 
     106    my ($self) = @_; 
     107    $self->_results( 
     108        $self->info('empty_ballot_has_voice') 
     109        ? 0 
     110        : 1 
     111    ); 
     112} 
     113 
    77114sub _results { 
    78115    my ($self, $nonull) = @_; 
     
    85122        union 
    86123        select ballot.id, coalesce(corrected, ballot_map.to, value) as "value" from ballot 
    87         } . ($nonull ? '' : ' left ') . q{ 
    88         join ballot_item 
     124        } . ($nonull ? '' : ' left ') . q{ join ballot_item 
    89125        on ballot.id = ballot_item.id  
    90126        left join ballot_map on ballot.poll = ballot_map.poll and ballot_map.from = ballot_item.value 
Note: See TracChangeset for help on using the changeset viewer.