1 | package Vote::DB::Poll::Results; |
---|
2 | |
---|
3 | # $Id$ |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | use base 'Vote::DB::Poll'; |
---|
8 | |
---|
9 | =head1 NAME |
---|
10 | |
---|
11 | Vote::Model::Vote - Catalyst Model |
---|
12 | |
---|
13 | =head1 DESCRIPTION |
---|
14 | |
---|
15 | Catalyst Model. |
---|
16 | |
---|
17 | =cut |
---|
18 | |
---|
19 | sub new { |
---|
20 | my ($class, %args) = @_; |
---|
21 | |
---|
22 | my $res = $class->SUPER::new(%args); |
---|
23 | |
---|
24 | return bless $res, $class; |
---|
25 | } |
---|
26 | |
---|
27 | sub absolute_majority { |
---|
28 | my ($self) = @_; |
---|
29 | my $ballot_count = $self->ballot_count_nonull; |
---|
30 | return int($ballot_count / 2) + 1; |
---|
31 | } |
---|
32 | |
---|
33 | sub 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 | |
---|
50 | sub ballot_count_nonull { |
---|
51 | my ($self) = @_; |
---|
52 | |
---|
53 | my $sth = $self->db->prepare_cached( |
---|
54 | q{ |
---|
55 | select count(*) from ballot where poll = ? |
---|
56 | and id in (select id from ballot_item) and |
---|
57 | (invalid = 'false' or invalid is null) |
---|
58 | } |
---|
59 | ); |
---|
60 | |
---|
61 | $sth->execute($self->voteid); |
---|
62 | my $res = $sth->fetchrow_hashref; |
---|
63 | $sth->finish; |
---|
64 | $res->{count} |
---|
65 | } |
---|
66 | |
---|
67 | sub results_count { |
---|
68 | my ($self) = @_; |
---|
69 | $self->_results(); |
---|
70 | } |
---|
71 | |
---|
72 | sub results_nonull { |
---|
73 | my ($self) = @_; |
---|
74 | $self->_results(1); |
---|
75 | } |
---|
76 | |
---|
77 | sub _results { |
---|
78 | my ($self, $nonull) = @_; |
---|
79 | |
---|
80 | my $sth = $self->db->prepare( |
---|
81 | q{ |
---|
82 | select count(ballot.id), value from |
---|
83 | (select ballot.id, coalesce(corrected, ballot_map.to, value) as "value" from ballot |
---|
84 | } . ($nonull ? '' : ' left ') . q{ |
---|
85 | join ballot_item |
---|
86 | on ballot.id = ballot_item.id |
---|
87 | left join ballot_map on ballot.poll = ballot_map.poll and ballot_map.from = ballot_item.value |
---|
88 | where ballot.poll = ? and (invalid = 'false' or invalid is null) |
---|
89 | group by ballot.id, coalesce(corrected, ballot_map.to, value)) as ballot |
---|
90 | group by value |
---|
91 | order by count desc, value |
---|
92 | } |
---|
93 | ); |
---|
94 | $sth->execute($self->voteid); |
---|
95 | my @results; |
---|
96 | while (my $res = $sth->fetchrow_hashref) { |
---|
97 | push(@results, $res); |
---|
98 | } |
---|
99 | \@results; |
---|
100 | } |
---|
101 | |
---|
102 | =head1 AUTHOR |
---|
103 | |
---|
104 | Thauvin Olivier |
---|
105 | |
---|
106 | =head1 LICENSE |
---|
107 | |
---|
108 | This library is free software, you can redistribute it and/or modify |
---|
109 | it under the same terms as Perl itself or CeCILL. |
---|
110 | |
---|
111 | =cut |
---|
112 | |
---|
113 | 1; |
---|