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 | |
---|
70 | my $sth = $self->db->prepare( |
---|
71 | q{ |
---|
72 | select count(ballot.id), value from |
---|
73 | (select ballot.id, coalesce(corrected, value) as "value" from ballot left join ballot_item |
---|
74 | on ballot.id = ballot_item.id where ballot.poll = ? and (invalid = 'false' or invalid is null) |
---|
75 | group by ballot.id, coalesce(corrected, value)) as ballot |
---|
76 | group by value |
---|
77 | order by count desc, value |
---|
78 | } |
---|
79 | ); |
---|
80 | $sth->execute($self->voteid); |
---|
81 | my @results; |
---|
82 | while (my $res = $sth->fetchrow_hashref) { |
---|
83 | push(@results, $res); |
---|
84 | } |
---|
85 | \@results; |
---|
86 | } |
---|
87 | |
---|
88 | sub results_nonull { |
---|
89 | my ($self) = @_; |
---|
90 | |
---|
91 | my $sth = $self->db->prepare( |
---|
92 | q{ |
---|
93 | select count(ballot.id), value from |
---|
94 | (select ballot.id, coalesce(corrected, value) as "value" from ballot join ballot_item |
---|
95 | on ballot.id = ballot_item.id where ballot.poll = ? and (invalid = 'false' or invalid is null) |
---|
96 | group by ballot.id, coalesce(corrected, value)) as ballot |
---|
97 | group by value |
---|
98 | order by count desc, value |
---|
99 | } |
---|
100 | ); |
---|
101 | $sth->execute($self->voteid); |
---|
102 | my @results; |
---|
103 | while (my $res = $sth->fetchrow_hashref) { |
---|
104 | push(@results, $res); |
---|
105 | } |
---|
106 | \@results; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | =head1 AUTHOR |
---|
111 | |
---|
112 | Thauvin Olivier |
---|
113 | |
---|
114 | =head1 LICENSE |
---|
115 | |
---|
116 | This library is free software, you can redistribute it and/or modify |
---|
117 | it under the same terms as Perl itself or CeCILL. |
---|
118 | |
---|
119 | =cut |
---|
120 | |
---|
121 | 1; |
---|