source: trunk/lib/Epoll/DB.pm @ 272

Last change on this file since 272 was 272, checked in by nanardon, 15 years ago
  • allow poll deletion
  • Property svn:keywords set to Id Rev
File size: 7.8 KB
Line 
1package Epoll::DB;
2
3# $Id$
4
5use strict;
6use warnings;
7use base 'Epoll::DB::common';
8use Epoll::DB::Poll;
9use Epoll::DB::Poll::Results;
10
11our $VERSION = '1.90';
12
13=head1 NAME
14
15Epoll::Model::Vote - Catalyst Model
16
17=head1 DESCRIPTION
18
19Catalyst Model.
20
21=cut
22
23sub new {
24    my ($class, $dbstring) = @_;
25   
26    bless {
27        dbstring => $dbstring,
28        db => Epoll::DB::common::_newdb($dbstring),
29    }, $class;
30}
31
32sub 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
52sub 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
70sub 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
89sub poll {
90    my ($self, $pollid) = @_;
91    Epoll::DB::Poll->new($self->{dbstring}, $self->poll_id_from_uid($pollid));
92}
93
94sub results {
95    my ($self, $pollid) = @_;
96    Epoll::DB::Poll::Results->new($self->{dbstring}, $self->poll_id_from_uid($pollid));
97}
98
99# This part is keep to track error
100sub ballot {die "# ballot" . join(' ',caller());}
101sub vote_param { die "# vote_param" . join(' ',caller());}
102sub vote_status {die "# vote_status" . join(' ',caller());}
103sub vote_info {die "# vote_info" . join(' ',caller());}
104sub vote_set_settings {die "# old functions" . join(' ',caller());}
105sub vote_signing {die "# old functions" . join(' ',caller());}
106sub vote_voting {die "# old functions" . join(' ',caller());}
107sub voting_info {die "# old functions" . join(' ',caller());}
108sub vote_choices {die "# old functions" . join(' ',caller());}
109sub vote_add_choice {die "# old functions" . join(' ',caller());}
110sub choice_info {die "# old functions" . join(' ',caller());}
111sub delete_choice {die "# old functions" . join(' ',caller());}
112sub voting_info_id {die "# old functions" . join(' ',caller());}
113sub register_ballot {die "# old functions" . join(' ',caller());}
114sub vote_voting_count {die "# old functions" . join(' ',caller());}
115sub signing_count { vote_signing_count(@_) }
116sub vote_signing_count {die "# old functions" . join(' ',caller());}
117sub ballot_count { vote_ballot_count(@_) }
118sub vote_ballot_count {die "# old functions" . join(' ',caller());}
119sub ballot_count_nonull { vote_ballot_count_nonull(@_) }
120sub vote_ballot_count_nonull {die "# old functions" . join(' ',caller());}
121sub auth_voting {die "# old functions" . join(' ',caller());}
122sub auth_poll {die "# old functions" . join(' ',caller());}
123sub voting_has_sign {die "# old functions" . join(' ',caller());}
124sub vote_results_count {die "# old functions" . join(' ',caller());}
125sub vote_results_nonull {die "# old functions" . join(' ',caller());}
126sub list_vote_ballot {die "# old functions" . join(' ',caller());}
127sub list_vote_ballot_needvalid {die "# old functions" . join(' ',caller());}
128sub ballot_info {die "# vote_status" . join(' ',caller());}
129sub mark_ballot_invalid {die "# old functions" . join(' ',caller());}
130sub ballot_items {die "# old functions" . join(' ',caller());}
131sub vote_ballot_untrusted_values {die "# old functions" . join(' ',caller());}
132sub vote_ballot_values {die "# old functions" . join(' ',caller());}
133sub vote_map_value {die "# old functions" . join(' ',caller());}
134sub addupd_voting {die "# old functions" . join(' ',caller());}
135sub delete_voting {die "# old functions" . join(' ',caller());}
136sub voting_from_file {die "# old functions" . join(' ',caller());}
137
138sub clean_old_poll_request {
139    my ($self) = @_;
140    $self->db->do(q{delete from poll_request where "create" < now() - '30 days'::interval});
141}
142
143sub 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
156sub poll_from_request {
157    my ($self, $rid, $passwd) = @_;
158    my $rinfo = $self->poll_request_info($rid) or return;
159
160    my $pollid = $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    $pollid
169}
170
171sub create_poll {
172    my ($self, $mail, $label, $passwd) = @_;
173
174    my $encpasswd = $self->gen_enc_passwd($passwd);
175
176    my $getpollid = $self->db->prepare_cached(
177        q{select nextval('poll_id_seq')}
178    );
179    $getpollid->execute();
180    my $newpollid = $getpollid->fetchrow_hashref->{nextval};
181    $getpollid->finish;
182   
183    my $newpoll = $self->db->prepare_cached(
184        q{insert into poll (id, label, owner, password) values (?,?,?,?)}
185    );
186
187    $newpoll->execute($newpollid, $label, $mail, $encpasswd);
188    # set some default
189    $self->poll($newpollid)->setup() or do {
190        $self->rollback;
191        return;
192    };
193
194
195    $newpollid
196}
197
198
199sub create_poll_request {
200    my ($self, %info) = @_;
201
202    $info{mail} or return;
203    my $addreq = $self->db->prepare_cached(
204        q{insert into poll_request (id, label, mail) values (?,?,?)}
205    );
206
207    my $reqid = Epoll::DB::common::gen_uid();
208
209    $addreq->execute($reqid, $info{label}, $info{mail});
210    $self->commit;
211    $reqid;
212}
213
214sub find_poll_by_voting {
215    my ($self, $mail) = @_;
216    $mail or return;
217    my $finder = $self->db->prepare_cached(
218        q{select poll, date from voting left join signing
219          on voting.key = signing.key
220          where mail = lower($1)
221          order by date desc
222        }
223    );
224    $finder->execute($mail);
225    my @results;
226    while (my $res = $finder->fetchrow_hashref) {
227        push(@results, $res);
228    }
229    @results
230}
231
232sub find_poll_by_notyet_voting {
233    my ($self, $mail) = @_;
234    $mail or return;
235    my $finder = $self->db->prepare_cached(
236        q{select poll from voting where
237          key not in (select key from signing)
238            and
239          poll in (select id from poll where "end" > now())
240            and
241          mail = lower($1)
242        }
243    );
244    $finder->execute($mail);
245    my @results;
246    while (my $res = $finder->fetchrow_hashref) {
247        push(@results, $res);
248    }
249    @results
250}
251
252sub find_poll_by_owner {
253    my ($self, $mail) = @_;
254    $mail or return;
255    my $finder = $self->db->prepare_cached(
256        q{select id, "end" > now() as terminated from poll where owner = lower($1)
257          order by "end" desc}
258    );
259    $finder->execute($mail);
260    my @results;
261    while (my $res = $finder->fetchrow_hashref) {
262        push(@results, $res);
263    }
264    @results
265}
266
267sub delete_poll {
268    my ($self, $id) = @_;
269    {
270        my $poll = $self->poll($id);
271        $poll->_delete_ballot;
272    }
273
274    foreach (
275        q{delete from settings where poll = ?},
276        q{delete from signing where key in (select key from voting where poll = ?)},
277        q{delete from voting where poll = ?},
278        q{delete from choice where poll = ?},
279        q{delete from ballot_map where poll = ?},
280        q{delete from poll where id = ?},
281    ) {
282        my $req = $self->db->prepare($_);
283        $req->execute($id) or do {
284            $self->rollback;
285            return;
286        };
287    }
288
289    $self->commit;
290    return 1;
291}
292
293=head1 AUTHOR
294
295Thauvin Olivier
296
297=head1 LICENSE
298
299This library is free software, you can redistribute it and/or modify
300it under the same terms as Perl itself or CeCILL.
301
302=cut
303
3041;
Note: See TracBrowser for help on using the repository browser.