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 | use Vote::DB::Ballot; # see ballot() |
---|
11 | use Vote::DB::Voting; # see delete_voting() |
---|
12 | use Vote::DB::Choice; |
---|
13 | |
---|
14 | our $VERSION = '1.90'; |
---|
15 | |
---|
16 | =head1 NAME |
---|
17 | |
---|
18 | Vote::Model::Vote - Catalyst Model |
---|
19 | |
---|
20 | =head1 DESCRIPTION |
---|
21 | |
---|
22 | Catalyst Model. |
---|
23 | |
---|
24 | =cut |
---|
25 | |
---|
26 | sub new { |
---|
27 | my ($class, $dbstring) = @_; |
---|
28 | |
---|
29 | bless { |
---|
30 | dbstring => $dbstring, |
---|
31 | db => Vote::DB::common::_newdb($dbstring), |
---|
32 | }, $class; |
---|
33 | } |
---|
34 | |
---|
35 | sub list_comming_vote { |
---|
36 | my ($self) = @_; |
---|
37 | |
---|
38 | my $sth = $self->db->prepare_cached( |
---|
39 | q{ |
---|
40 | select id from poll where |
---|
41 | (start > now() and "end" > now()) or |
---|
42 | "end" is null or start is null |
---|
43 | } |
---|
44 | ); |
---|
45 | |
---|
46 | $sth->execute; |
---|
47 | my @id; |
---|
48 | while(my $res = $sth->fetchrow_hashref) { |
---|
49 | push(@id, $res->{id}); |
---|
50 | } |
---|
51 | |
---|
52 | @id |
---|
53 | } |
---|
54 | |
---|
55 | sub list_running_vote { |
---|
56 | my ($self) = @_; |
---|
57 | |
---|
58 | my $sth = $self->db->prepare_cached( |
---|
59 | q{ |
---|
60 | select id from poll where start < now() and "end" > now() |
---|
61 | } |
---|
62 | ); |
---|
63 | |
---|
64 | $sth->execute; |
---|
65 | my @id; |
---|
66 | while(my $res = $sth->fetchrow_hashref) { |
---|
67 | push(@id, $res->{id}); |
---|
68 | } |
---|
69 | |
---|
70 | @id |
---|
71 | } |
---|
72 | |
---|
73 | sub list_closed_vote { |
---|
74 | my ($self) = @_; |
---|
75 | |
---|
76 | my $sth = $self->db->prepare_cached( |
---|
77 | q{ |
---|
78 | select id from poll where |
---|
79 | start < now() and "end" < now() |
---|
80 | } |
---|
81 | ); |
---|
82 | |
---|
83 | $sth->execute; |
---|
84 | my @id; |
---|
85 | while(my $res = $sth->fetchrow_hashref) { |
---|
86 | push(@id, $res->{id}); |
---|
87 | } |
---|
88 | |
---|
89 | @id |
---|
90 | } |
---|
91 | |
---|
92 | sub poll { |
---|
93 | my ($self, $pollid) = @_; |
---|
94 | Vote::DB::Poll->new($self->{dbstring}, $pollid); |
---|
95 | } |
---|
96 | |
---|
97 | sub results { |
---|
98 | my ($self, $pollid) = @_; |
---|
99 | Vote::DB::Poll::Results->new($self->{dbstring}, $pollid); |
---|
100 | } |
---|
101 | |
---|
102 | sub ballot { |
---|
103 | my ($self, $ballotid) = @_; |
---|
104 | # TODO must die, ballot are attached to a poll |
---|
105 | Vote::DB::Ballot->new($self->{dbstring}, $ballotid); |
---|
106 | } |
---|
107 | |
---|
108 | sub vote_param { |
---|
109 | my ($self, $voteid, %attr) = @_; |
---|
110 | warn "# vote_param"; |
---|
111 | $self->poll($voteid)->param(%attr); |
---|
112 | } |
---|
113 | |
---|
114 | sub vote_status { |
---|
115 | my ($self, $id) = @_; |
---|
116 | warn "# vote_status"; |
---|
117 | $self->poll($id)->status; |
---|
118 | } |
---|
119 | |
---|
120 | sub vote_info { |
---|
121 | my ($self, $id) = @_; |
---|
122 | $self->poll($id)->info; |
---|
123 | } |
---|
124 | |
---|
125 | sub vote_set_settings { |
---|
126 | my ($self, $poll, $var, $val) = @_; |
---|
127 | $self->poll($poll)->set_settings($var, $val); |
---|
128 | } |
---|
129 | |
---|
130 | sub vote_signing { |
---|
131 | my ($self, $id) = @_; |
---|
132 | $self->poll($id)->signing; |
---|
133 | } |
---|
134 | |
---|
135 | sub vote_voting { |
---|
136 | my ($self, $voteid) = @_; |
---|
137 | $self->poll($voteid)->voting_keys; |
---|
138 | } |
---|
139 | |
---|
140 | sub voting_info { |
---|
141 | my ($self, $id) = @_; |
---|
142 | $self->poll($id)->voting_info; |
---|
143 | } |
---|
144 | |
---|
145 | sub vote_choices { |
---|
146 | my ($self, $id) = @_; |
---|
147 | $self->poll($id)->choices; |
---|
148 | } |
---|
149 | |
---|
150 | sub vote_add_choice { |
---|
151 | my ($self, $voteid, $label) = @_; |
---|
152 | $self->poll($voteid)->add_choice($label); |
---|
153 | } |
---|
154 | |
---|
155 | sub choice_info { |
---|
156 | my ($self, $chid) = @_; |
---|
157 | Vote::DB::Choice->new($self->{dbstring}, $chid)->info; |
---|
158 | } |
---|
159 | |
---|
160 | sub delete_choice { |
---|
161 | my ($self, $chid) = @_; |
---|
162 | |
---|
163 | my $sth = $self->db->prepare_cached( |
---|
164 | q{delete from choice where key = ?} |
---|
165 | ); |
---|
166 | |
---|
167 | $sth->execute($chid); |
---|
168 | } |
---|
169 | |
---|
170 | sub voting_info_id { |
---|
171 | my ($self, $mail, $voteid) = @_; |
---|
172 | $self->poll($voteid)->voting_from_mail($mail)->info; |
---|
173 | } |
---|
174 | |
---|
175 | sub register_ballot { |
---|
176 | my ($self, $vid, $voteid, $choice, $fchoice, $referal) = @_; |
---|
177 | $self->poll($voteid)->register_ballot($vid, $choice, $fchoice, $referal); |
---|
178 | } |
---|
179 | |
---|
180 | sub vote_voting_count { |
---|
181 | my ($self, $id) = @_; |
---|
182 | $self->poll($id)->voting_count; |
---|
183 | } |
---|
184 | |
---|
185 | sub signing_count { vote_signing_count(@_) } |
---|
186 | |
---|
187 | sub vote_signing_count { |
---|
188 | my ($self, $voteid) = @_; |
---|
189 | $self->poll($voteid)->signing_count; |
---|
190 | } |
---|
191 | |
---|
192 | sub ballot_count { vote_ballot_count(@_) } |
---|
193 | |
---|
194 | sub vote_ballot_count { |
---|
195 | my ($self, $voteid) = @_; |
---|
196 | $self->poll($voteid)->ballot_count; |
---|
197 | } |
---|
198 | |
---|
199 | sub ballot_count_nonull { vote_ballot_count_nonull(@_) } |
---|
200 | |
---|
201 | sub vote_ballot_count_nonull { |
---|
202 | my ($self, $voteid) = @_; |
---|
203 | $self->results($voteid)->ballot_count_nonull; |
---|
204 | } |
---|
205 | |
---|
206 | sub auth_voting { |
---|
207 | my ($self, $poll, $mail, $password) = @_; |
---|
208 | $self->poll($poll)->auth_voting($mail, $password); |
---|
209 | } |
---|
210 | |
---|
211 | sub auth_poll { |
---|
212 | my ($self, $voteid, $passwd) = @_; |
---|
213 | $self->poll($voteid)->auth_poll($passwd); |
---|
214 | } |
---|
215 | |
---|
216 | sub voting_has_sign { |
---|
217 | my ($self, $poll, $user) = @_; |
---|
218 | warn $user; |
---|
219 | $self->poll($poll)->voting_has_sign($user); |
---|
220 | } |
---|
221 | |
---|
222 | # Requete de decompte des voix: |
---|
223 | |
---|
224 | sub vote_results_count { |
---|
225 | my ($self, $voteid) = @_; |
---|
226 | $self->results($voteid)->results_count; |
---|
227 | } |
---|
228 | |
---|
229 | sub vote_results_nonull { |
---|
230 | my ($self, $voteid) = @_; |
---|
231 | $self->results($voteid)->results_nonull; |
---|
232 | } |
---|
233 | |
---|
234 | sub list_vote_ballot { |
---|
235 | my ($self, $voteid) = @_; |
---|
236 | $self->poll($voteid)->list_ballot; |
---|
237 | } |
---|
238 | |
---|
239 | sub list_vote_ballot_needvalid { |
---|
240 | my ($self, $voteid) = @_; |
---|
241 | $self->poll($voteid)->list_ballot_needvalid; |
---|
242 | } |
---|
243 | |
---|
244 | sub ballot_info { |
---|
245 | my ($self, $ballotid) = @_; |
---|
246 | $self->ballot($ballotid)->info; |
---|
247 | } |
---|
248 | |
---|
249 | sub mark_ballot_invalid { |
---|
250 | my ($self, $ballotid, $invalid) = @_; |
---|
251 | $self->ballot($ballotid)->mark_invalid($invalid); |
---|
252 | } |
---|
253 | |
---|
254 | sub ballot_items { |
---|
255 | my ($self, $ballotid) = @_; |
---|
256 | $self->ballot($ballotid)->items; |
---|
257 | } |
---|
258 | |
---|
259 | sub vote_ballot_untrusted_values { |
---|
260 | my ($self, $voteid) = @_; |
---|
261 | $self->poll($voteid)->ballot_untrusted_values; |
---|
262 | } |
---|
263 | |
---|
264 | sub vote_ballot_values { |
---|
265 | my ($self, $voteid) = @_; |
---|
266 | $self->poll($voteid)->ballot_values; |
---|
267 | } |
---|
268 | |
---|
269 | sub vote_map_value { |
---|
270 | my ($self, $voteid, $from, $to) = @_; |
---|
271 | $self->poll($voteid)->map_value($from, $to); |
---|
272 | } |
---|
273 | |
---|
274 | sub addupd_voting { |
---|
275 | my ($self, $voteid, $mail, $id) = @_; |
---|
276 | $self->poll($voteid)->addupd_voting($mail, $id); |
---|
277 | } |
---|
278 | |
---|
279 | sub delete_voting { |
---|
280 | my ($self, $key) = @_; |
---|
281 | # must die, voting are attached to a poll |
---|
282 | Vote::DB::Voting->new($self->{dbstring}, $key)->has_sign and return; |
---|
283 | my $sth = $self->db->prepare_cached( |
---|
284 | q{delete from voting where key = ?} |
---|
285 | ); |
---|
286 | |
---|
287 | $sth->execute($key); |
---|
288 | } |
---|
289 | |
---|
290 | sub voting_from_file { |
---|
291 | my ($self, $voteid, $fh, $delete) = @_; |
---|
292 | $self->poll($voteid)->voting_from_file($fh, $delete); |
---|
293 | } |
---|
294 | |
---|
295 | sub clean_old_poll_request { |
---|
296 | my ($self) = @_; |
---|
297 | |
---|
298 | $self->db->do(q{delete from poll_request where "create" < now() - '30 days'::interval}); |
---|
299 | } |
---|
300 | |
---|
301 | sub poll_request_info { |
---|
302 | my ($self, $rid) = @_; |
---|
303 | |
---|
304 | my $sth = $self->db->prepare_cached( |
---|
305 | q{select * from poll_request where id = ?} |
---|
306 | ); |
---|
307 | |
---|
308 | $sth->execute($rid); |
---|
309 | my $res = $sth->fetchrow_hashref; |
---|
310 | $sth->finish; |
---|
311 | $res |
---|
312 | } |
---|
313 | |
---|
314 | sub poll_from_request { |
---|
315 | my ($self, $rid, $passwd) = @_; |
---|
316 | my $rinfo = $self->poll_request_info($rid) or return; |
---|
317 | |
---|
318 | my $encpasswd = $self->gen_enc_passwd($passwd); |
---|
319 | |
---|
320 | my $getpollid = $self->db->prepare_cached( |
---|
321 | q{select nextval('poll_id_seq')} |
---|
322 | ); |
---|
323 | $getpollid->execute(); |
---|
324 | my $newpollid = $getpollid->fetchrow_hashref->{nextval}; |
---|
325 | |
---|
326 | my $newpoll = $self->db->prepare_cached( |
---|
327 | q{insert into poll (id, label, owner, password) values (?,?,?,?)} |
---|
328 | ); |
---|
329 | |
---|
330 | $newpoll->execute($newpollid, $rinfo->{label}, $rinfo->{mail}, $encpasswd); |
---|
331 | # set some default |
---|
332 | $self->poll($newpollid)->setup() or do { |
---|
333 | $self->db->rollback; |
---|
334 | return; |
---|
335 | }; |
---|
336 | |
---|
337 | my $delreq = $self->db->prepare_cached( |
---|
338 | q{delete from poll_request where id = ?} |
---|
339 | ); |
---|
340 | |
---|
341 | $delreq->execute($rid); |
---|
342 | $self->db->commit; |
---|
343 | |
---|
344 | $newpollid |
---|
345 | } |
---|
346 | |
---|
347 | sub create_poll_request { |
---|
348 | my ($self, %info) = @_; |
---|
349 | |
---|
350 | $info{mail} or return; |
---|
351 | my $addreq = $self->db->prepare_cached( |
---|
352 | q{insert into poll_request (id, label, mail) values (?,?,?)} |
---|
353 | ); |
---|
354 | |
---|
355 | my $reqid = Vote::DB::common::gen_uid(); |
---|
356 | |
---|
357 | $addreq->execute($reqid, $info{label}, $info{mail}); |
---|
358 | $self->db->commit; |
---|
359 | $reqid; |
---|
360 | } |
---|
361 | |
---|
362 | =head1 AUTHOR |
---|
363 | |
---|
364 | Thauvin Olivier |
---|
365 | |
---|
366 | =head1 LICENSE |
---|
367 | |
---|
368 | This library is free software, you can redistribute it and/or modify |
---|
369 | it under the same terms as Perl itself or CeCILL. |
---|
370 | |
---|
371 | =cut |
---|
372 | |
---|
373 | 1; |
---|