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

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