source: tags/2.00/lib/Epoll/DB.pm @ 478

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