Line | |
---|
1 | package Vote::Model::Vote; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base 'Catalyst::Model'; |
---|
6 | use DBI; |
---|
7 | |
---|
8 | =head1 NAME |
---|
9 | |
---|
10 | Vote::Model::Vote - Catalyst Model |
---|
11 | |
---|
12 | =head1 DESCRIPTION |
---|
13 | |
---|
14 | Catalyst Model. |
---|
15 | |
---|
16 | =cut |
---|
17 | |
---|
18 | sub new { |
---|
19 | my ($class) = @_; |
---|
20 | my $db = DBI->connect( |
---|
21 | 'dbi:Pg:' . Vote->config->{db}, |
---|
22 | undef, undef, |
---|
23 | { |
---|
24 | RaiseError => 0, |
---|
25 | AutoCommit => 0, |
---|
26 | PrintWarn => 0, |
---|
27 | PrintError => 1, |
---|
28 | } |
---|
29 | ) or return; |
---|
30 | |
---|
31 | bless { |
---|
32 | db => $db, |
---|
33 | }, $class; |
---|
34 | } |
---|
35 | |
---|
36 | sub db { $_[0]->{db} } |
---|
37 | |
---|
38 | sub list_running_vote { |
---|
39 | my ($self) = @_; |
---|
40 | |
---|
41 | my $sth = $self->db->prepare_cached( |
---|
42 | q{ |
---|
43 | select id from poll where start < now() and "end" > now() |
---|
44 | } |
---|
45 | ); |
---|
46 | |
---|
47 | $sth->execute; |
---|
48 | my @id; |
---|
49 | while(my $res = $sth->fetchrow_hashref) { |
---|
50 | push(@id, $res->{id}); |
---|
51 | } |
---|
52 | |
---|
53 | @id |
---|
54 | } |
---|
55 | |
---|
56 | sub vote_status { |
---|
57 | my ($self, $id) = @_; |
---|
58 | |
---|
59 | my $sth = $self->db->prepare_cached( |
---|
60 | q{ |
---|
61 | select start < now() as before |
---|
62 | "end" > now() as after |
---|
63 | where id = ? |
---|
64 | } |
---|
65 | ); |
---|
66 | $sth->execute($id); |
---|
67 | my $res = $sth->fetchrow_hashref; |
---|
68 | $sth->finish; |
---|
69 | $res or return; |
---|
70 | if ($res->{before}) { |
---|
71 | return 'BEFORE'; |
---|
72 | } elsif ($res->{after}) { |
---|
73 | return 'AFTER'; |
---|
74 | } else { |
---|
75 | return 'RUNNING'; |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | sub vote_info { |
---|
80 | my ($self, $id) = @_; |
---|
81 | |
---|
82 | my $sth = $self->db->prepare_cached( |
---|
83 | q{ |
---|
84 | select * from poll where id = ? |
---|
85 | } |
---|
86 | ); |
---|
87 | |
---|
88 | $sth->execute($id); |
---|
89 | my $res = $sth->fetchrow_hashref; |
---|
90 | $sth->finish; |
---|
91 | $res |
---|
92 | } |
---|
93 | |
---|
94 | sub vote_signing { |
---|
95 | my ($self, $id) = @_; |
---|
96 | |
---|
97 | my $sth = $self->db->prepare_cached( |
---|
98 | q{ |
---|
99 | select * from voting left join signing |
---|
100 | on signing.key = voting.key |
---|
101 | where poll = ? order by voting.id |
---|
102 | } |
---|
103 | ); |
---|
104 | $sth->execute($id); |
---|
105 | my @people; |
---|
106 | while (my $res = $sth->fetchrow_hashref) { |
---|
107 | push(@people, $res); |
---|
108 | } |
---|
109 | @people |
---|
110 | } |
---|
111 | |
---|
112 | sub vote_choices { |
---|
113 | my ($self, $id) = @_; |
---|
114 | |
---|
115 | my $sth = $self->db->prepare_cached( |
---|
116 | q{ |
---|
117 | select * from choice where poll = ? |
---|
118 | order by label |
---|
119 | } |
---|
120 | ); |
---|
121 | $sth->execute($id); |
---|
122 | my @ch; |
---|
123 | while (my $res = $sth->fetchrow_hashref) { |
---|
124 | push(@ch, $res); |
---|
125 | } |
---|
126 | @ch |
---|
127 | } |
---|
128 | |
---|
129 | =head1 AUTHOR |
---|
130 | |
---|
131 | Thauvin Olivier |
---|
132 | |
---|
133 | =head1 LICENSE |
---|
134 | |
---|
135 | This library is free software, you can redistribute it and/or modify |
---|
136 | it under the same terms as Perl itself. |
---|
137 | |
---|
138 | =cut |
---|
139 | |
---|
140 | 1; |
---|
Note: See
TracBrowser
for help on using the repository browser.