1 | package Vote::DB; |
---|
2 | |
---|
3 | # $Id$ |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | use base 'Vote::DB::common'; |
---|
8 | use Vote::DB::Poll; |
---|
9 | use Vote::DB::Poll::Results; |
---|
10 | |
---|
11 | our $VERSION = '1.90'; |
---|
12 | |
---|
13 | =head1 NAME |
---|
14 | |
---|
15 | Vote::Model::Vote - Catalyst Model |
---|
16 | |
---|
17 | =head1 DESCRIPTION |
---|
18 | |
---|
19 | Catalyst Model. |
---|
20 | |
---|
21 | =cut |
---|
22 | |
---|
23 | sub new { |
---|
24 | my ($class, $dbstring) = @_; |
---|
25 | |
---|
26 | bless { |
---|
27 | dbstring => $dbstring, |
---|
28 | db => Vote::DB::common::_newdb($dbstring), |
---|
29 | }, $class; |
---|
30 | } |
---|
31 | |
---|
32 | sub list_comming_vote { |
---|
33 | my ($self) = @_; |
---|
34 | |
---|
35 | my $sth = $self->db->prepare_cached( |
---|
36 | q{ |
---|
37 | select id from poll where |
---|
38 | (start > now() and "end" > now()) or |
---|
39 | "end" is null or start is null |
---|
40 | } |
---|
41 | ); |
---|
42 | |
---|
43 | $sth->execute; |
---|
44 | my @id; |
---|
45 | while(my $res = $sth->fetchrow_hashref) { |
---|
46 | push(@id, $res->{id}); |
---|
47 | } |
---|
48 | |
---|
49 | @id |
---|
50 | } |
---|
51 | |
---|
52 | sub list_running_vote { |
---|
53 | my ($self) = @_; |
---|
54 | |
---|
55 | my $sth = $self->db->prepare_cached( |
---|
56 | q{ |
---|
57 | select id from poll where start < now() and "end" > now() |
---|
58 | } |
---|
59 | ); |
---|
60 | |
---|
61 | $sth->execute; |
---|
62 | my @id; |
---|
63 | while(my $res = $sth->fetchrow_hashref) { |
---|
64 | push(@id, $res->{id}); |
---|
65 | } |
---|
66 | |
---|
67 | @id |
---|
68 | } |
---|
69 | |
---|
70 | sub list_closed_vote { |
---|
71 | my ($self) = @_; |
---|
72 | |
---|
73 | my $sth = $self->db->prepare_cached( |
---|
74 | q{ |
---|
75 | select id from poll where |
---|
76 | start < now() and "end" < now() |
---|
77 | } |
---|
78 | ); |
---|
79 | |
---|
80 | $sth->execute; |
---|
81 | my @id; |
---|
82 | while(my $res = $sth->fetchrow_hashref) { |
---|
83 | push(@id, $res->{id}); |
---|
84 | } |
---|
85 | |
---|
86 | @id |
---|
87 | } |
---|
88 | |
---|
89 | sub poll { |
---|
90 | my ($self, $pollid) = @_; |
---|
91 | Vote::DB::Poll->new($self->{dbstring}, $self->poll_id_from_uid($pollid)); |
---|
92 | } |
---|
93 | |
---|
94 | sub results { |
---|
95 | my ($self, $pollid) = @_; |
---|
96 | Vote::DB::Poll::Results->new($self->{dbstring}, $self->poll_id_from_uid($pollid)); |
---|
97 | } |
---|
98 | |
---|
99 | # This part is keep to track error |
---|
100 | sub ballot {die "# ballot" . join(' ',caller());} |
---|
101 | sub vote_param { die "# vote_param" . join(' ',caller());} |
---|
102 | sub vote_status {die "# vote_status" . join(' ',caller());} |
---|
103 | sub vote_info {die "# vote_info" . join(' ',caller());} |
---|
104 | sub vote_set_settings {die "# old functions" . join(' ',caller());} |
---|
105 | sub vote_signing {die "# old functions" . join(' ',caller());} |
---|
106 | sub vote_voting {die "# old functions" . join(' ',caller());} |
---|
107 | sub voting_info {die "# old functions" . join(' ',caller());} |
---|
108 | sub vote_choices {die "# old functions" . join(' ',caller());} |
---|
109 | sub vote_add_choice {die "# old functions" . join(' ',caller());} |
---|
110 | sub choice_info {die "# old functions" . join(' ',caller());} |
---|
111 | sub delete_choice {die "# old functions" . join(' ',caller());} |
---|
112 | sub voting_info_id {die "# old functions" . join(' ',caller());} |
---|
113 | sub register_ballot {die "# old functions" . join(' ',caller());} |
---|
114 | sub vote_voting_count {die "# old functions" . join(' ',caller());} |
---|
115 | sub signing_count { vote_signing_count(@_) } |
---|
116 | sub vote_signing_count {die "# old functions" . join(' ',caller());} |
---|
117 | sub ballot_count { vote_ballot_count(@_) } |
---|
118 | sub vote_ballot_count {die "# old functions" . join(' ',caller());} |
---|
119 | sub ballot_count_nonull { vote_ballot_count_nonull(@_) } |
---|
120 | sub vote_ballot_count_nonull {die "# old functions" . join(' ',caller());} |
---|
121 | sub auth_voting {die "# old functions" . join(' ',caller());} |
---|
122 | sub auth_poll {die "# old functions" . join(' ',caller());} |
---|
123 | sub voting_has_sign {die "# old functions" . join(' ',caller());} |
---|
124 | sub vote_results_count {die "# old functions" . join(' ',caller());} |
---|
125 | sub vote_results_nonull {die "# old functions" . join(' ',caller());} |
---|
126 | sub list_vote_ballot {die "# old functions" . join(' ',caller());} |
---|
127 | sub list_vote_ballot_needvalid {die "# old functions" . join(' ',caller());} |
---|
128 | sub ballot_info {die "# vote_status" . join(' ',caller());} |
---|
129 | sub mark_ballot_invalid {die "# old functions" . join(' ',caller());} |
---|
130 | sub ballot_items {die "# old functions" . join(' ',caller());} |
---|
131 | sub vote_ballot_untrusted_values {die "# old functions" . join(' ',caller());} |
---|
132 | sub vote_ballot_values {die "# old functions" . join(' ',caller());} |
---|
133 | sub vote_map_value {die "# old functions" . join(' ',caller());} |
---|
134 | sub addupd_voting {die "# old functions" . join(' ',caller());} |
---|
135 | sub delete_voting {die "# old functions" . join(' ',caller());} |
---|
136 | sub voting_from_file {die "# old functions" . join(' ',caller());} |
---|
137 | |
---|
138 | sub clean_old_poll_request { |
---|
139 | my ($self) = @_; |
---|
140 | $self->db->do(q{delete from poll_request where "create" < now() - '30 days'::interval}); |
---|
141 | } |
---|
142 | |
---|
143 | sub poll_request_info { |
---|
144 | my ($self, $rid) = @_; |
---|
145 | |
---|
146 | my $sth = $self->db->prepare_cached( |
---|
147 | q{select * from poll_request where id = ?} |
---|
148 | ); |
---|
149 | |
---|
150 | $sth->execute($rid); |
---|
151 | my $res = $sth->fetchrow_hashref; |
---|
152 | $sth->finish; |
---|
153 | $res |
---|
154 | } |
---|
155 | |
---|
156 | sub poll_from_request { |
---|
157 | my ($self, $rid, $passwd) = @_; |
---|
158 | my $rinfo = $self->poll_request_info($rid) or return; |
---|
159 | |
---|
160 | $self->create_poll($rinfo->{mail}, $rinfo->{label}, $passwd); |
---|
161 | |
---|
162 | my $delreq = $self->db->prepare_cached( |
---|
163 | q{delete from poll_request where id = ?} |
---|
164 | ); |
---|
165 | |
---|
166 | $delreq->execute($rid); |
---|
167 | $self->commit; |
---|
168 | } |
---|
169 | |
---|
170 | sub create_poll { |
---|
171 | my ($self, $mail, $label, $passwd) = @_; |
---|
172 | |
---|
173 | my $encpasswd = $self->gen_enc_passwd($passwd); |
---|
174 | |
---|
175 | my $getpollid = $self->db->prepare_cached( |
---|
176 | q{select nextval('poll_id_seq')} |
---|
177 | ); |
---|
178 | $getpollid->execute(); |
---|
179 | my $newpollid = $getpollid->fetchrow_hashref->{nextval}; |
---|
180 | $getpollid->finish; |
---|
181 | |
---|
182 | my $newpoll = $self->db->prepare_cached( |
---|
183 | q{insert into poll (id, label, owner, password) values (?,?,?,?)} |
---|
184 | ); |
---|
185 | |
---|
186 | $newpoll->execute($newpollid, $label, $mail, $encpasswd); |
---|
187 | # set some default |
---|
188 | $self->poll($newpollid)->setup() or do { |
---|
189 | $self->rollback; |
---|
190 | return; |
---|
191 | }; |
---|
192 | |
---|
193 | |
---|
194 | $newpollid |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | sub create_poll_request { |
---|
199 | my ($self, %info) = @_; |
---|
200 | |
---|
201 | $info{mail} or return; |
---|
202 | my $addreq = $self->db->prepare_cached( |
---|
203 | q{insert into poll_request (id, label, mail) values (?,?,?)} |
---|
204 | ); |
---|
205 | |
---|
206 | my $reqid = Vote::DB::common::gen_uid(); |
---|
207 | |
---|
208 | $addreq->execute($reqid, $info{label}, $info{mail}); |
---|
209 | $self->commit; |
---|
210 | $reqid; |
---|
211 | } |
---|
212 | |
---|
213 | =head1 AUTHOR |
---|
214 | |
---|
215 | Thauvin Olivier |
---|
216 | |
---|
217 | =head1 LICENSE |
---|
218 | |
---|
219 | This library is free software, you can redistribute it and/or modify |
---|
220 | it under the same terms as Perl itself or CeCILL. |
---|
221 | |
---|
222 | =cut |
---|
223 | |
---|
224 | 1; |
---|