Changeset 214


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
Files:
5 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 
  • trunk/root/latex/poll_report.tt

    r207 r214  
    4848 
    4949[%- IF poll.ballot_count_nonull -%] 
    50 [%- FOREACH res = poll.results_nonull -%] 
     50[%- FOREACH res = poll.results -%] 
    5151[%- IF res.count != loop.prev.count && loop.count > 0 -%] 
    5252[%- mycount = loop.count -%] 
  • trunk/root/templates/includes/poll_results.tt

    r207 r214  
    2626 
    2727[% IF poll.ballot_count_nonull %] 
    28 [% FOREACH res = poll.results_nonull %] 
     28[% FOREACH res = poll.results %] 
    2929[% IF loop.first %] 
    3030<p>Score:</p> 
  • trunk/t/10Vote_DB.t

    r213 r214  
    6060            a => { sbal => [ 'ch1' ] }, 
    6161            b => { sbal => [ 'ch1', 'ch10' ] }, 
    62             d => { sbal => [ 'ch1', 'ch2' ] }, 
     62            d => { sbal => [ 'ch1', 'ch2', 'ch4' ] }, 
    6363            f => {}, 
    6464        }, 
    65         test_count => 9, 
     65        test_count => 10, 
    6666        tests => sub { 
    6767            my ($r) = @_; 
     
    6969            is($r->absolute_majority, 2, "Can get absolute_majority"); 
    7070            is($r->ballot_count, 4, "can get ballot count"); 
    71             is($r->ballot_count_nonull, 3, "can get ballot count"); 
     71            is($r->not_empty_ballot_count, 3, "can get ballot count"); 
     72            is($r->empty_ballot_count, 1, "can get empty ballot count"); 
    7273            is($r->voting_count, 6, "can get voting count"); 
    7374            is($r->signing_count, 4, "can get signing count"); 
    74             my $res = $r->results_nonull; 
     75            my $res = $r->results; 
    7576            is($res->[0]->{value}, 'ch1', "winner is ch1"); 
    7677            is($res->[0]->{count}, 3, "winner has 3 voices"); 
    77             is(@$res, 3, "count of winners"); 
    78             is($res->[2]->{count}, 0, "last has 0 voices"); 
     78            is(@$res, 4, "count of winners"); 
     79            is($res->[3]->{count}, 0, "last has 0 voices"); 
     80        } 
     81    }, 
     82    { 
     83        poll => { 
     84            label => 'empty ballot count', 
     85            empty_ballot_has_voice => 1, 
     86        }, 
     87        choices => [ qw(ch1 ch2 ch3) ], 
     88        voting => [ qw(a b c d e f) ], 
     89        ballot => { 
     90            a => { sbal => [ 'ch1' ] }, 
     91            b => { sbal => [ 'ch1', 'ch2' ] }, 
     92            d => { sbal => [ 'ch1', 'ch2' ] }, 
     93            e => {}, 
     94            f => {}, 
     95        }, 
     96        test_count => 10, 
     97        tests => sub { 
     98            my ($r) = @_; 
     99            is($r->absolute_majority, 3, "Can get absolute_majority"); 
     100            is($r->ballot_count, 5, "can get ballot count"); 
     101            is($r->not_empty_ballot_count, 3, "can get ballot count"); 
     102            is($r->empty_ballot_count, 2, "can get empty ballot count"); 
     103            is($r->voting_count, 6, "can get voting count"); 
     104            is($r->signing_count, 5, "can get signing count"); 
     105            my $res = $r->results; 
     106            is($res->[0]->{value}, 'ch1', "winner is ch1"); 
     107            is($res->[0]->{count}, 3, "winner has 3 voices"); 
     108            is(@$res, 4, "count of winners"); 
     109            is($res->[3]->{count}, 0, "last has 0 voices"); 
    79110        } 
    80111    }, 
Note: See TracChangeset for help on using the changeset viewer.