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

Last change on this file since 195 was 195, checked in by nanardon, 15 years ago
  • start new value mapping method
  • Property svn:keywords set to Id Rev
File size: 2.3 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->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) = @_;
69    $self->_results();
70}
71
72sub results_nonull {
73    my ($self) = @_;
74    $self->_results(1);
75}
76
77sub _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
104Thauvin Olivier
105
106=head1 LICENSE
107
108This library is free software, you can redistribute it and/or modify
109it under the same terms as Perl itself or CeCILL.
110
111=cut
112
1131;
Note: See TracBrowser for help on using the repository browser.