source: trunk/lib/Vote/DB/Poll/Results.pm @ 217

Last change on this file since 217 was 217, checked in by nanardon, 15 years ago
  • make difference between settings and their raw value
  • simplify template
  • allow to have a different number of elected people than choice in ballot
  • Property svn:keywords set to Id Rev
File size: 4.0 KB
Line 
1package Vote::DB::Poll::Results;
2
3# $Id$
4
5use strict;
6use warnings;
7use base 'Vote::DB::Poll';
8
9=head1 NAME
10
11Vote::Model::Vote - Catalyst Model
12
13=head1 DESCRIPTION
14
15Catalyst Model.
16
17=cut
18
19sub new {
20    my ($class, %args) = @_;
21
22    my $res = $class->SUPER::new(%args);
23   
24    return bless $res, $class;
25}
26
27sub absolute_majority {
28    my ($self) = @_;
29    my $ballot_count = $self->voices_ballot_count;
30    return int($ballot_count / 2) + 1;
31}   
32
33sub abstention {
34    my ($self) = @_;
35   
36    my $sth = $self->db->prepare_cached(
37        q{
38        select count(*) from ballot where poll = ?
39        and id not in (select id from ballot_item) and
40        (invalid = 'false' or invalid is null)
41        }
42    );
43
44    $sth->execute($self->voteid);
45    my $res = $sth->fetchrow_hashref;
46    $sth->finish;
47    $res->{count}
48}
49
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 {
79    my ($self) = @_;
80
81    my $sth = $self->db->prepare_cached(
82        q{
83        select count(*) from ballot where poll = ?
84        and id in (select id from ballot_item) and
85        (invalid = 'false' or invalid is null)
86        }
87    );
88
89    $sth->execute($self->voteid);
90    my $res = $sth->fetchrow_hashref;
91    $sth->finish;
92    $res->{count}
93}
94
95sub results_count {
96    my ($self) = @_;
97    $self->_results();
98}
99
100sub results_nonull {
101    my ($self) = @_;
102    $self->_results(1);
103}
104
105sub results {
106    my ($self) = @_;
107    $self->_results(
108        $self->info('empty_ballot_has_voice')
109        ? 0
110        : 1
111    );
112}
113
114sub _results {
115    my ($self, $nonull) = @_;
116
117    my $sth = $self->db->prepare(
118        q{
119        select count(ballot.id), value from
120        (
121        select NULL as id, label as value from choice where poll = ?
122        union
123        select ballot.id, coalesce(corrected, ballot_map.to, value) as "value" from ballot
124        } . ($nonull ? '' : ' left ') . q{ join ballot_item
125        on ballot.id = ballot_item.id
126        left join ballot_map on ballot.poll = ballot_map.poll and ballot_map.from = ballot_item.value
127        where ballot.poll = ? and (invalid = 'false' or invalid is null
128        )
129        group by ballot.id, coalesce(corrected, ballot_map.to, value)) as ballot
130        group by value
131        order by count desc, value
132        }
133    );
134    $sth->execute($self->voteid, $self->voteid);
135    my @results;
136    my $abs_maj = $self->absolute_majority;
137    my $wanted_count = $self->info('elected_count');
138    my $voice_count = $self->voices_ballot_count;
139    while (my $res = $sth->fetchrow_hashref) {
140        if ($res->{count} >= $abs_maj) {
141            $res->{abs_maj} = 1;
142        }
143        if (scalar(@results) < $wanted_count) {
144            $res->{elected} = 1 if ($res->{count});
145        } elsif (scalar(@results)) {
146            $res->{elected} = $results[-1]->{elected}
147                if($res->{count} == $results[-1]->{count});
148        }
149        if (scalar(@results)) {
150            $res->{order} = ($res->{count} == $results[-1]->{count}
151                ? $results[-1]->{order}
152                : scalar(@results) + 1);
153        } else { $res->{order} = 1 }
154        $res->{voices_percent} = $res->{count} / $voice_count * 100
155            if($voice_count); # avoid divide by 0
156
157        push(@results, $res);
158    }
159    \@results;
160}
161
162=head1 AUTHOR
163
164Thauvin Olivier
165
166=head1 LICENSE
167
168This library is free software, you can redistribute it and/or modify
169it under the same terms as Perl itself or CeCILL.
170
171=cut
172
1731;
Note: See TracBrowser for help on using the repository browser.