[242] | 1 | package Epoll::DB::Ballot; |
---|
[160] | 2 | |
---|
| 3 | # $Id$ |
---|
| 4 | |
---|
| 5 | use strict; |
---|
| 6 | use warnings; |
---|
[242] | 7 | use base 'Epoll::DB::common'; |
---|
| 8 | use Epoll::DB::Poll; |
---|
[160] | 9 | |
---|
| 10 | =head1 NAME |
---|
| 11 | |
---|
[242] | 12 | Epoll::Model::Epoll - Catalyst Model |
---|
[160] | 13 | |
---|
| 14 | =head1 DESCRIPTION |
---|
| 15 | |
---|
| 16 | Catalyst Model. |
---|
| 17 | |
---|
| 18 | =cut |
---|
| 19 | |
---|
| 20 | sub new { |
---|
| 21 | my ($class, $dbstring, $id) = @_; |
---|
| 22 | |
---|
| 23 | bless { |
---|
| 24 | id => $id, |
---|
| 25 | dbstring => $dbstring, |
---|
[242] | 26 | db => Epoll::DB::common::_newdb($dbstring), |
---|
[160] | 27 | }, $class; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | sub ballotid { $_[0]->{id} } |
---|
| 31 | |
---|
| 32 | sub poll { |
---|
| 33 | my ($self) = @_; |
---|
[242] | 34 | Epoll::DB::Poll->new($self->{dbstring}, $self->info->{poll}); |
---|
[160] | 35 | } |
---|
| 36 | |
---|
| 37 | sub info { |
---|
| 38 | my ($self) = @_; |
---|
| 39 | |
---|
| 40 | my $sth = $self->db->prepare_cached( |
---|
[171] | 41 | q{ select * from |
---|
| 42 | ballot left join |
---|
| 43 | ballot_enc on ballot.id = ballot_enc.id |
---|
| 44 | where ballot.id = ? } |
---|
[160] | 45 | ); |
---|
| 46 | |
---|
| 47 | $sth->execute($self->ballotid); |
---|
| 48 | my $res = $sth->fetchrow_hashref; |
---|
| 49 | $sth->finish; |
---|
| 50 | $res |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | sub 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 | |
---|
| 63 | sub items { |
---|
| 64 | my ($self) = @_; |
---|
| 65 | |
---|
| 66 | my $sth = $self->db->prepare_cached( |
---|
[195] | 67 | q{ |
---|
| 68 | select ballot_item.*, value as v, |
---|
[319] | 69 | ballot_map.to as corrected |
---|
[195] | 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 | } |
---|
[160] | 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 | |
---|
| 86 | Thauvin Olivier |
---|
| 87 | |
---|
| 88 | =head1 LICENSE |
---|
| 89 | |
---|
| 90 | This library is free software, you can redistribute it and/or modify |
---|
| 91 | it under the same terms as Perl itself or CeCILL. |
---|
| 92 | |
---|
| 93 | =cut |
---|
| 94 | |
---|
| 95 | 1; |
---|