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

Last change on this file since 213 was 213, checked in by nanardon, 15 years ago
  • more tests and the fix need to have it passing
  • Property svn:keywords set to Id Rev
File size: 2.4 KB
RevLine 
[193]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->ballot_count_nonull;
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 {
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
67sub results_count {
68    my ($self) = @_;
[195]69    $self->_results();
[193]70}
71
72sub results_nonull {
73    my ($self) = @_;
[195]74    $self->_results(1);
75}
[193]76
[195]77sub _results {
78    my ($self, $nonull) = @_;
79
[193]80    my $sth = $self->db->prepare(
81        q{
82        select count(ballot.id), value from
[213]83        (
84        select NULL as id, label as value from choice where poll = ?
85        union
86        select ballot.id, coalesce(corrected, ballot_map.to, value) as "value" from ballot
[195]87        } . ($nonull ? '' : ' left ') . q{
88        join ballot_item
89        on ballot.id = ballot_item.id
90        left join ballot_map on ballot.poll = ballot_map.poll and ballot_map.from = ballot_item.value
[213]91        where ballot.poll = ? and (invalid = 'false' or invalid is null
92        )
[195]93        group by ballot.id, coalesce(corrected, ballot_map.to, value)) as ballot
[193]94        group by value
95        order by count desc, value
96        }
97    );
[213]98    $sth->execute($self->voteid, $self->voteid);
[193]99    my @results;
100    while (my $res = $sth->fetchrow_hashref) {
101        push(@results, $res);
102    }
103    \@results;
104}
105
106=head1 AUTHOR
107
108Thauvin Olivier
109
110=head1 LICENSE
111
112This library is free software, you can redistribute it and/or modify
113it under the same terms as Perl itself or CeCILL.
114
115=cut
116
1171;
Note: See TracBrowser for help on using the repository browser.