source: trunk/lib/Vote/DB.pm @ 198

Last change on this file since 198 was 198, checked in by nanardon, 15 years ago
  • use new commit() and rollback()
  • add add standalone create_poll function
  • Property svn:keywords set to Id Rev
File size: 7.5 KB
Line 
1package Vote::DB;
2
3# $Id$
4
5use strict;
6use warnings;
7use base 'Vote::DB::common';
8use Vote::DB::Poll;
9use Vote::DB::Poll::Results;
10use Vote::DB::Ballot; # see ballot()
11use Vote::DB::Voting; # see delete_voting()
12use Vote::DB::Choice;
13
14our $VERSION = '1.90';
15
16=head1 NAME
17
18Vote::Model::Vote - Catalyst Model
19
20=head1 DESCRIPTION
21
22Catalyst Model.
23
24=cut
25
26sub new {
27    my ($class, $dbstring) = @_;
28   
29    bless {
30        dbstring => $dbstring,
31        db => Vote::DB::common::_newdb($dbstring),
32    }, $class;
33}
34
35sub 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
55sub 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
73sub 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
92sub poll {
93    my ($self, $pollid) = @_;
94    Vote::DB::Poll->new($self->{dbstring}, $pollid);
95}
96
97sub results {
98    my ($self, $pollid) = @_;
99    Vote::DB::Poll::Results->new($self->{dbstring}, $pollid);
100}
101
102sub 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
108sub vote_param {
109    my ($self, $voteid, %attr) = @_;
110    warn "# vote_param";
111    $self->poll($voteid)->param(%attr);
112}
113
114sub vote_status {
115    my ($self, $id) = @_;
116    warn "# vote_status";
117    $self->poll($id)->status;
118}
119
120sub vote_info {
121    my ($self, $id) = @_;
122    $self->poll($id)->info;
123}
124
125sub vote_set_settings {
126    my ($self, $poll, $var, $val) = @_;
127    $self->poll($poll)->set_settings($var, $val);
128}
129
130sub vote_signing {
131    my ($self, $id) = @_;
132    $self->poll($id)->signing;
133}
134
135sub vote_voting {
136    my ($self, $voteid) = @_;
137    $self->poll($voteid)->voting_keys;
138}
139
140sub voting_info {
141    my ($self, $id) = @_;
142    $self->poll($id)->voting_info;
143}
144
145sub vote_choices {
146    my ($self, $id) = @_;
147    $self->poll($id)->choices;
148}
149
150sub vote_add_choice {
151    my ($self, $voteid, $label) = @_;
152    $self->poll($voteid)->add_choice($label);
153}
154
155sub choice_info {
156    my ($self, $chid) = @_;
157    Vote::DB::Choice->new($self->{dbstring}, $chid)->info;
158}
159
160sub 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
170sub voting_info_id {
171    my ($self, $mail, $voteid) = @_;
172    $self->poll($voteid)->voting_from_mail($mail)->info;
173}
174
175sub register_ballot {
176    my ($self, $vid, $voteid, $choice, $fchoice, $referal) = @_;
177    $self->poll($voteid)->register_ballot($vid, $choice, $fchoice, $referal);
178}
179
180sub vote_voting_count {
181    my ($self, $id) = @_;
182    $self->poll($id)->voting_count;
183}
184
185sub signing_count { vote_signing_count(@_) }
186
187sub vote_signing_count {
188    my ($self, $voteid) = @_;
189    $self->poll($voteid)->signing_count;
190}
191
192sub ballot_count { vote_ballot_count(@_) }
193
194sub vote_ballot_count {
195    my ($self, $voteid) = @_;
196    $self->poll($voteid)->ballot_count;
197}
198
199sub ballot_count_nonull { vote_ballot_count_nonull(@_) }
200
201sub vote_ballot_count_nonull {
202    my ($self, $voteid) = @_;
203    $self->results($voteid)->ballot_count_nonull;
204}
205
206sub auth_voting {
207    my ($self, $poll, $mail, $password) = @_;
208    $self->poll($poll)->auth_voting($mail, $password);
209}
210
211sub auth_poll {
212    my ($self, $voteid, $passwd) = @_;
213    $self->poll($voteid)->auth_poll($passwd);
214}
215
216sub 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
224sub vote_results_count {
225    my ($self, $voteid) = @_;
226    $self->results($voteid)->results_count;
227}
228
229sub vote_results_nonull {
230    my ($self, $voteid) = @_;
231    $self->results($voteid)->results_nonull;
232}
233
234sub list_vote_ballot {
235    my ($self, $voteid) = @_;
236    $self->poll($voteid)->list_ballot;
237}
238
239sub list_vote_ballot_needvalid {
240    my ($self, $voteid) = @_;
241    $self->poll($voteid)->list_ballot_needvalid;
242}
243
244sub ballot_info {
245    my ($self, $ballotid) = @_;
246    $self->ballot($ballotid)->info;
247}
248
249sub mark_ballot_invalid {
250    my ($self, $ballotid, $invalid) = @_;
251    $self->ballot($ballotid)->mark_invalid($invalid);
252}
253
254sub ballot_items {
255    my ($self, $ballotid) = @_;
256    $self->ballot($ballotid)->items;
257}
258
259sub vote_ballot_untrusted_values {
260    my ($self, $voteid) = @_;
261    $self->poll($voteid)->ballot_untrusted_values;
262}
263
264sub vote_ballot_values {
265    my ($self, $voteid) = @_;
266    $self->poll($voteid)->ballot_values;
267}
268
269sub vote_map_value {
270    my ($self, $voteid, $from, $to) = @_;
271    $self->poll($voteid)->map_value($from, $to);
272}
273
274sub addupd_voting {
275    my ($self, $voteid, $mail, $id) = @_;
276    $self->poll($voteid)->addupd_voting($mail, $id);
277}
278
279sub 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
290sub voting_from_file {
291    my ($self, $voteid, $fh, $delete) = @_;
292    $self->poll($voteid)->voting_from_file($fh, $delete);
293}
294
295sub 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
301sub 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
314sub poll_from_request {
315    my ($self, $rid, $passwd) = @_;
316    my $rinfo = $self->poll_request_info($rid) or return;
317
318    $self->create_poll($rinfo->{mail}, $rinfo->{label}, $passwd);
319   
320    my $delreq = $self->db->prepare_cached(
321        q{delete from poll_request where id = ?}
322    );
323
324    $delreq->execute($rid);
325    $self->commit;
326}
327
328sub create_poll {
329    my ($self, $mail, $label, $passwd) = @_;
330
331    my $encpasswd = $self->gen_enc_passwd($passwd);
332
333    my $getpollid = $self->db->prepare_cached(
334        q{select nextval('poll_id_seq')}
335    );
336    $getpollid->execute();
337    my $newpollid = $getpollid->fetchrow_hashref->{nextval};
338   
339    my $newpoll = $self->db->prepare_cached(
340        q{insert into poll (id, label, owner, password) values (?,?,?,?)}
341    );
342
343    $newpoll->execute($newpollid, $label, $mail, $encpasswd);
344    # set some default
345    $self->poll($newpollid)->setup() or do {
346        $self->rollback;
347        return;
348    };
349
350
351    $newpollid
352}
353
354
355sub create_poll_request {
356    my ($self, %info) = @_;
357
358    $info{mail} or return;
359    my $addreq = $self->db->prepare_cached(
360        q{insert into poll_request (id, label, mail) values (?,?,?)}
361    );
362
363    my $reqid = Vote::DB::common::gen_uid();
364
365    $addreq->execute($reqid, $info{label}, $info{mail});
366    $self->commit;
367    $reqid;
368}
369
370=head1 AUTHOR
371
372Thauvin Olivier
373
374=head1 LICENSE
375
376This library is free software, you can redistribute it and/or modify
377it under the same terms as Perl itself or CeCILL.
378
379=cut
380
3811;
Note: See TracBrowser for help on using the repository browser.