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

Last change on this file since 303 was 303, checked in by nanardon, 14 years ago
  • tag 1.01
  • Property svn:keywords set to Id Rev
File size: 7.9 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(q{
218        select poll.id, date from poll join
219        subpoll on poll.id = subpoll.pollid
220        join voters on voters.poll = poll.id
221        left join signing on
222          signing.subpoll = subpoll.id and
223          signing.voters = voters.key
224        where mail = lower($1)
225        group by poll.id, date
226        order by date desc
227        }
228    );
229    $finder->execute($mail);
230    my @results;
231    while (my $res = $finder->fetchrow_hashref) {
232        push(@results, $res);
233    }
234    @results
235}
236
237sub find_poll_by_notyet_voting {
238    my ($self, $mail) = @_;
239    $mail or return;
240    my $finder = $self->db->prepare_cached(
241        q{select poll from voting where
242          key not in (select key from signing)
243            and
244          poll in (select id from poll where "end" > now())
245            and
246          mail = lower($1)
247        }
248    );
249    $finder->execute($mail);
250    my @results;
251    while (my $res = $finder->fetchrow_hashref) {
252        push(@results, $res);
253    }
254    @results
255}
256
257sub find_poll_by_owner {
258    my ($self, $mail) = @_;
259    $mail or return;
260    my $finder = $self->db->prepare_cached(
261        q{select id, "end" > now() as terminated from poll where owner = lower($1)
262          order by "end" desc}
263    );
264    $finder->execute($mail);
265    my @results;
266    while (my $res = $finder->fetchrow_hashref) {
267        push(@results, $res);
268    }
269    @results
270}
271
272sub delete_poll {
273    my ($self, $id) = @_;
274    {
275        my $poll = $self->poll($id);
276        $poll->_delete_ballot;
277    }
278
279    foreach (
280        q{delete from settings where poll = ?},
281        q{delete from signing where key in (select key from voting where poll = ?)},
282        q{delete from voting where poll = ?},
283        q{delete from choice where poll = ?},
284        q{delete from ballot_map where poll = ?},
285        q{delete from poll where id = ?},
286    ) {
287        my $req = $self->db->prepare($_);
288        $req->execute($id) or do {
289            $self->rollback;
290            return;
291        };
292    }
293
294    $self->commit;
295    return 1;
296}
297
298=head1 AUTHOR
299
300Thauvin Olivier
301
302=head1 LICENSE
303
304This library is free software, you can redistribute it and/or modify
305it under the same terms as Perl itself or CeCILL.
306
307=cut
308
3091;
Note: See TracBrowser for help on using the repository browser.