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

Last change on this file since 207 was 207, checked in by nanardon, 15 years ago
  • cleanup old functions and switch everything to new objects
  • Property svn:keywords set to Id Rev
File size: 5.8 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;
10
11our $VERSION = '1.90';
12
13=head1 NAME
14
15Vote::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 => Vote::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    Vote::DB::Poll->new($self->{dbstring}, $self->poll_id_from_uid($pollid));
92}
93
94sub results {
95    my ($self, $pollid) = @_;
96    Vote::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    $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}
169
170sub create_poll {
171    my ($self, $mail, $label, $passwd) = @_;
172
173    my $encpasswd = $self->gen_enc_passwd($passwd);
174
175    my $getpollid = $self->db->prepare_cached(
176        q{select nextval('poll_id_seq')}
177    );
178    $getpollid->execute();
179    my $newpollid = $getpollid->fetchrow_hashref->{nextval};
180   
181    my $newpoll = $self->db->prepare_cached(
182        q{insert into poll (id, label, owner, password) values (?,?,?,?)}
183    );
184
185    $newpoll->execute($newpollid, $label, $mail, $encpasswd);
186    # set some default
187    $self->poll($newpollid)->setup() or do {
188        $self->rollback;
189        return;
190    };
191
192
193    $newpollid
194}
195
196
197sub create_poll_request {
198    my ($self, %info) = @_;
199
200    $info{mail} or return;
201    my $addreq = $self->db->prepare_cached(
202        q{insert into poll_request (id, label, mail) values (?,?,?)}
203    );
204
205    my $reqid = Vote::DB::common::gen_uid();
206
207    $addreq->execute($reqid, $info{label}, $info{mail});
208    $self->commit;
209    $reqid;
210}
211
212=head1 AUTHOR
213
214Thauvin Olivier
215
216=head1 LICENSE
217
218This library is free software, you can redistribute it and/or modify
219it under the same terms as Perl itself or CeCILL.
220
221=cut
222
2231;
Note: See TracBrowser for help on using the repository browser.