source: trunk/lib/Vote/DB/Ballot.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: 1.7 KB
Line 
1package Vote::DB::Ballot;
2
3# $Id$
4
5use strict;
6use warnings;
7use base 'Vote::DB::common';
8use Vote::DB::Poll;
9
10=head1 NAME
11
12Vote::Model::Vote - Catalyst Model
13
14=head1 DESCRIPTION
15
16Catalyst Model.
17
18=cut
19
20sub new {
21    my ($class, $dbstring, $id) = @_;
22   
23    bless {
24        id => $id,
25        dbstring => $dbstring,
26        db => Vote::DB::common::_newdb($dbstring),
27    }, $class;
28}
29
30sub ballotid { $_[0]->{id} }
31
32sub poll {
33    my ($self) = @_;
34    Vote::DB::Poll->new($self->{dbstring}, $self->info->{poll});
35}
36
37sub info {
38    my ($self) = @_;
39
40    my $sth = $self->db->prepare_cached(
41        q{ select * from
42           ballot left join
43           ballot_enc on ballot.id = ballot_enc.id
44           where ballot.id = ? }
45    );
46
47    $sth->execute($self->ballotid);
48    my $res = $sth->fetchrow_hashref;
49    $sth->finish;
50    $res
51}
52
53sub mark_invalid {
54    my ($self, $invalid) = @_;
55
56    my $sth = $self->db->prepare_cached(
57        q{update ballot set invalid = ? where id = ?}
58    );
59
60    $sth->execute($invalid ? 't' : 'f', $self->ballotid);
61}
62
63sub items {
64    my ($self) = @_;
65
66    my $sth = $self->db->prepare_cached(
67        q{
68        select ballot_item.*, value as v,
69        coalesce(ballot_map.to, corrected) as corrected
70        from ballot_item join ballot on ballot.id = ballot_item.id
71        left join ballot_map on ballot_map.poll = ballot.poll and
72        ballot_map.from = ballot_item.value
73        where ballot.id = ?
74        }
75    );
76    $sth->execute($self->ballotid);
77    my @ids;
78    while (my $res = $sth->fetchrow_hashref) {
79        push(@ids, $res);
80    }
81    \@ids
82}
83
84=head1 AUTHOR
85
86Thauvin Olivier
87
88=head1 LICENSE
89
90This library is free software, you can redistribute it and/or modify
91it under the same terms as Perl itself or CeCILL.
92
93=cut
94
951;
Note: See TracBrowser for help on using the repository browser.