Changeset 214
- Timestamp:
- 04/17/09 15:32:47 (15 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Vote/DB/Poll.pm
r213 r214 513 513 } 514 514 515 sub voting_count {516 my ($self) = @_;517 518 my $sth = $self->db->prepare_cached(519 q{520 select count(*) from voting521 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 voting536 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 546 515 sub is_crypted { 547 516 my ($self) = @_; 548 517 return $self->info->{public_key} ? 1 : 0; 549 }550 551 sub ballot_count {552 my ($self) = @_;553 return $self->is_crypted554 ? $self->ballot_count_crypt555 : $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}582 518 } 583 519 … … 890 826 $privkey 891 827 } 828 829 ######### 830 # Count # 831 ######### 832 833 sub ballot_count { 834 my ($self) = @_; 835 return $self->is_crypted 836 ? $self->ballot_count_crypt 837 : $self->ballot_count_clear; 838 } 839 840 sub 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 853 sub 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 866 sub 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 881 sub 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 897 sub 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 892 912 =head1 AUTHOR 893 913 -
trunk/lib/Vote/DB/Poll/Results.pm
r213 r214 27 27 sub absolute_majority { 28 28 my ($self) = @_; 29 my $ballot_count = $self-> ballot_count_nonull;29 my $ballot_count = $self->voices_ballot_count; 30 30 return int($ballot_count / 2) + 1; 31 31 } … … 48 48 } 49 49 50 sub ballot_count_nonull { 50 sub ballot_count_nonull {not_empty_ballot_count(@_)} 51 52 sub 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 61 sub 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 78 sub not_empty_ballot_count { 51 79 my ($self) = @_; 52 80 … … 75 103 } 76 104 105 sub results { 106 my ($self) = @_; 107 $self->_results( 108 $self->info('empty_ballot_has_voice') 109 ? 0 110 : 1 111 ); 112 } 113 77 114 sub _results { 78 115 my ($self, $nonull) = @_; … … 85 122 union 86 123 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 89 125 on ballot.id = ballot_item.id 90 126 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 48 48 49 49 [%- IF poll.ballot_count_nonull -%] 50 [%- FOREACH res = poll.results _nonull-%]50 [%- FOREACH res = poll.results -%] 51 51 [%- IF res.count != loop.prev.count && loop.count > 0 -%] 52 52 [%- mycount = loop.count -%] -
trunk/root/templates/includes/poll_results.tt
r207 r214 26 26 27 27 [% IF poll.ballot_count_nonull %] 28 [% FOREACH res = poll.results _nonull%]28 [% FOREACH res = poll.results %] 29 29 [% IF loop.first %] 30 30 <p>Score:</p> -
trunk/t/10Vote_DB.t
r213 r214 60 60 a => { sbal => [ 'ch1' ] }, 61 61 b => { sbal => [ 'ch1', 'ch10' ] }, 62 d => { sbal => [ 'ch1', 'ch2' ] },62 d => { sbal => [ 'ch1', 'ch2', 'ch4' ] }, 63 63 f => {}, 64 64 }, 65 test_count => 9,65 test_count => 10, 66 66 tests => sub { 67 67 my ($r) = @_; … … 69 69 is($r->absolute_majority, 2, "Can get absolute_majority"); 70 70 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"); 72 73 is($r->voting_count, 6, "can get voting count"); 73 74 is($r->signing_count, 4, "can get signing count"); 74 my $res = $r->results _nonull;75 my $res = $r->results; 75 76 is($res->[0]->{value}, 'ch1', "winner is ch1"); 76 77 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"); 79 110 } 80 111 },
Note: See TracChangeset
for help on using the changeset viewer.