source: trunk/lib/Vote/Model/Vote.pm @ 23

Last change on this file since 23 was 23, checked in by nanardon, 15 years ago
  • admin page is fully working
File size: 11.9 KB
Line 
1package Vote::Model::Vote;
2
3use strict;
4use warnings;
5use base 'Catalyst::Model';
6use Vote;
7use DBI;
8
9=head1 NAME
10
11Vote::Model::Vote - Catalyst Model
12
13=head1 DESCRIPTION
14
15Catalyst Model.
16
17=cut
18
19sub new {
20    my ($class) = @_;
21    my $db = DBI->connect(
22        'dbi:Pg:' . Vote->config->{db},
23        undef, undef,
24        {
25            RaiseError => 0,
26            AutoCommit => 0,
27            PrintWarn => 0,
28            PrintError => 1,
29        }
30    ) or return;
31   
32    bless {
33        db => $db,
34    }, $class;
35}
36
37sub db { $_[0]->{db} }
38
39sub list_comming_vote {
40    my ($self) = @_;
41
42    my $sth = $self->db->prepare_cached(
43        q{
44        select id from poll where
45        (start > now() and "end" > now()) or
46        "end" is null or start is null
47        }
48    );
49
50    $sth->execute;
51    my @id;
52    while(my $res = $sth->fetchrow_hashref) {
53        push(@id, $res->{id});
54    }
55
56    @id
57}
58
59
60sub list_running_vote {
61    my ($self) = @_;
62
63    my $sth = $self->db->prepare_cached(
64        q{
65        select id from poll where start < now() and "end" > now()
66        }
67    );
68
69    $sth->execute;
70    my @id;
71    while(my $res = $sth->fetchrow_hashref) {
72        push(@id, $res->{id});
73    }
74
75    @id
76}
77
78sub list_closed_vote {
79    my ($self) = @_;
80
81    my $sth = $self->db->prepare_cached(
82        q{
83        select id from poll where
84        start < now() and "end" < now()
85        }
86    );
87
88    $sth->execute;
89    my @id;
90    while(my $res = $sth->fetchrow_hashref) {
91        push(@id, $res->{id});
92    }
93
94    @id
95}
96
97sub vote_param {
98    my ($self, $voteid, %attr) = @_;
99
100    keys %attr or return;
101
102    my $sth = $self->db->prepare_cached(
103        q{update poll set } .
104        join(',', map { qq("$_" = ?) } sort keys %attr) .
105        q{ where id = ?}
106    );
107    $sth->execute((map { $attr{$_} } sort keys %attr), $voteid)
108        or $self->db->rollback;
109}
110
111sub vote_status {
112    my ($self, $id) = @_;
113   
114    my $sth = $self->db->prepare_cached(
115        q{
116        select start > now() as before,
117               "end" < now() as after
118        from poll
119        where id = ?
120        }
121    );
122    $sth->execute($id);
123    my $res = $sth->fetchrow_hashref;
124    $sth->finish;
125    $res or return;
126    if ($res->{before}) {
127        return 'BEFORE';
128    } elsif ($res->{after}) {
129        return 'AFTER';
130    } else {
131        return 'RUNNING';
132    }
133}
134
135sub vote_info {
136    my ($self, $id) = @_;
137
138    my $sth = $self->db->prepare_cached(
139        q{
140        select * from poll where id = ?
141        }
142    );
143
144    $sth->execute($id);
145    my $res = $sth->fetchrow_hashref;
146    $sth->finish;
147    $res
148}
149
150sub vote_signing {
151    my ($self, $id) = @_;
152
153    my $sth = $self->db->prepare_cached(
154        q{
155        select *, voting.key as vkey from voting left join signing
156        on signing.key = voting.key
157        where poll = ? order by voting.id
158        }
159    );
160    $sth->execute($id);
161    my @people;
162    while (my $res = $sth->fetchrow_hashref) {
163        push(@people, $res);
164    }
165    @people
166}
167
168sub vote_voting {
169    my ($self, $id) = @_;
170
171    my $sth = $self->db->prepare_cached(
172        q{
173        select key from voting
174        where poll = ? order by voting.id
175        }
176    );
177    $sth->execute($id);
178    my @people;
179    while (my $res = $sth->fetchrow_hashref) {
180        push(@people, $res->{key});
181    }
182    @people
183}
184
185sub voting_info {
186    my ($self, $id) = @_;
187
188    my $sth = $self->db->prepare_cached(
189        q{
190        select *, voting.key as vkey from voting left join signing
191        on signing.key = voting.key
192        where voting.key = ?
193        }
194    );
195    $sth->execute($id);
196   
197    my $res = $sth->fetchrow_hashref;
198    $sth->finish;
199    $res
200}
201
202sub vote_signing_count {
203    my ($self, $id) = @_;
204
205    my $sth = $self->db->prepare_cached(
206        q{
207        select count(*) from voting
208        where poll = ?
209        }
210    );
211    $sth->execute($id);
212    my $res = $sth->fetchrow_hashref;
213    $sth->finish;
214    $res->{count}
215}
216
217sub vote_choices {
218    my ($self, $id) = @_;
219
220    my $sth = $self->db->prepare_cached(
221        q{
222        select key from choice where poll = ?
223        order by label
224        }
225    );
226    $sth->execute($id);
227    my @ch;
228    while (my $res = $sth->fetchrow_hashref) {
229        push(@ch, $res->{key});
230    }
231    @ch
232}
233
234sub choice_info {
235    my ($self, $chid) = @_;
236    my $sth = $self->db->prepare_cached(
237        q{select * from choice where key = ?}
238    );
239    $sth->execute($chid);
240    my $res = $sth->fetchrow_hashref;
241    $sth->finish;
242    $res
243}
244
245sub vote_add_choice {
246    my ($self, $voteid, $label) = @_;
247
248    my $sth = $self->db->prepare_cached(
249        q{insert into choice (poll, label) values (?,?)}
250    );
251
252    $sth->execute($voteid, $label) or do {
253        $self->db->rollback;
254        return;
255    };
256
257    1
258}
259
260sub modify_choice {
261    my ($self, $chid, $label) = @_;
262
263    my $sth = $self->db->prepare_cached(
264        q{update choice set label = ? where key = ?}
265    );
266    $sth->execute($label, $chid);
267}
268
269sub delete_choice {
270    my ($self, $chid) = @_;
271
272    my $sth = $self->db->prepare_cached(
273        q{delete from choice where key = ?}
274    );
275
276    $sth->execute($chid);
277}
278
279sub voting_info_id {
280    my ($self, $id, $voteid) = @_;
281
282    my $sth = $self->db->prepare_cached(
283        q{
284        select * from voting where id = ? and poll = ?
285        }
286    );
287    $sth->execute($id, $voteid);
288    my $res = $sth->fetchrow_hashref();
289    $sth->finish;
290    $res
291}
292
293sub _register_signing {
294    my ($self, $vid, $voteid, $referal) = @_;
295
296    my $vinfo = $self->voting_info_id($vid, $voteid) or return;
297
298    my $sth = $self->db->prepare_cached(
299        q{
300        insert into signing (key, referal) values (?,?)
301        }
302    );
303    $sth->execute($vinfo->{key}, $referal) or do {
304        $self->db->rollback;
305        return;
306    };
307
308    1;
309}
310
311sub gen_uid {
312    unpack("H*", join("", map { chr(rand(256)) } (0..15)))
313}
314
315sub _register_ballot {
316    my ($self, $voteid, $choice, $fchoice) = @_;
317
318    my $addb = $self->db->prepare_cached(
319        q{
320        insert into ballot (id, poll, invalid) values (?,?,?)
321        }
322    );
323    my $uid = gen_uid;
324    $addb->execute($uid, $voteid, scalar(@{$fchoice || []}) ? undef : 'f') or do {
325        self->db->rollback;
326        return;
327    };
328
329    my $addbc = $self->db->prepare_cached(
330        q{
331        insert into ballot_item (id, value, fromlist) values (?,?,?)
332        }
333    );
334    foreach (@{ $choice || []}) {
335        $addbc->execute($uid, $_, 't') or do {
336            $self->db->rollback;
337            return;
338        };
339    }
340    foreach (@{ $fchoice || []}) {
341        $addbc->execute($uid, $_, 'f') or do {
342            $self->db->rollback;
343            return;
344        };
345    }
346
347    $uid;
348}
349
350sub register_ballot {
351    my ($self, $vid, $voteid, $choice, $fchoice, $referal) = @_;
352
353    # First we register voting has voted
354    $self->_register_signing($vid, $voteid, $referal) or return; # TODO error ?
355
356    # registring choices
357    my $uid = $self->_register_ballot($voteid, $choice, $fchoice) or return;
358
359    # everything went fine, saving!
360    $self->db->commit;
361
362    $uid
363}
364
365sub signing_count {
366    my ($self, $voteid) = @_;
367
368    my $sth = $self->db->prepare_cached(
369        q{
370        select count(*) from signing join voting
371        on voting.key = signing.key where poll = ?
372        }
373    );
374
375    $sth->execute($voteid);
376    my $res = $sth->fetchrow_hashref;
377    $sth->finish;
378    $res->{count}
379}
380
381sub ballot_count {
382    my ($self, $voteid) = @_;
383
384    my $sth = $self->db->prepare_cached(
385        q{
386        select count(*) from ballot where poll = ?
387        }
388    );
389
390    $sth->execute($voteid);
391    my $res = $sth->fetchrow_hashref;
392    $sth->finish;
393    $res->{count}
394}
395
396sub ballot_count_nonull {
397    my ($self, $voteid) = @_;
398
399    my $sth = $self->db->prepare_cached(
400        q{
401        select count(*) from ballot where poll = ?
402        and id in (select id from ballot_item) and invalid = 'false'
403        }
404    );
405
406    $sth->execute($voteid);
407    my $res = $sth->fetchrow_hashref;
408    $sth->finish;
409    warn $res->{count};
410    $res->{count}
411}
412
413sub auth_voting {
414    my ($self, $poll, $user, $password) = @_;
415    my $userinfo = $self->voting_info_id($user, $poll) or return;
416
417    $userinfo->{passwd} or return;
418    if (crypt($password, $userinfo->{passwd} || '') eq $userinfo->{passwd}) {
419        return 1;
420    } else {
421        return 0;
422    }
423}
424
425sub voting_has_sign {
426    my ($self, $poll, $user) = @_;
427
428    my $sth = $self->db->prepare_cached(
429        q{
430        select date from signing join voting
431        on voting.key = signing.key
432        where poll = ? and id = ?
433        }
434    );
435
436    $sth->execute($poll, $user);
437    my $res = $sth->fetchrow_hashref;
438    $sth->finish;
439    return $res->{date}
440}
441
442# Requete de decompte des voix:
443
444sub vote_results_count {
445    my ($self, $voteid) = @_;
446
447    my $sth = $self->db->prepare(
448        q{
449        select count(ballot.id), value from ballot left join ballot_item
450        on ballot.id = ballot_item.id where ballot.poll = ? and invalid = 'false'
451        group by value
452        order by count
453        }
454    );
455    $sth->execute($voteid);
456    my @results;
457    while (my $res = $sth->fetchrow_hashref) {
458        push(@results, $res);
459    }
460    @results;
461}
462
463sub vote_results_nonull {
464    my ($self, $voteid) = @_;
465
466    my $sth = $self->db->prepare(
467        q{
468        select count(ballot.id), value from ballot join ballot_item
469        on ballot.id = ballot_item.id where ballot.poll = ? and invalid = 'false'
470        group by value
471        order by count desc
472        }
473    );
474    $sth->execute($voteid);
475    my @results;
476    while (my $res = $sth->fetchrow_hashref) {
477        push(@results, $res);
478    }
479    \@results;
480}
481
482sub list_vote_ballot {
483    my ($self, $voteid) = @_;
484
485    my $sth = $self->db->prepare_cached(
486        q{
487        select id from ballot where poll = ?
488        order by id
489        }
490    );
491    $sth->execute($voteid);
492    my @ids;
493    while (my $res = $sth->fetchrow_hashref) {
494        push(@ids, $res->{id});
495    }
496    @ids
497}
498
499sub list_vote_ballot_needvalid {
500    my ($self, $voteid) = @_;
501
502    my $sth = $self->db->prepare_cached(
503        q{
504        select id from ballot where poll = ?
505        and invalid is null order by id
506        }
507    );
508    $sth->execute($voteid);
509    my @ids;
510    while (my $res = $sth->fetchrow_hashref) {
511        push(@ids, $res->{id});
512    }
513    @ids
514}
515
516sub ballot_info {
517    my ($self, $ballotid) = @_;
518
519    my $sth = $self->db->prepare_cached(
520        q{ select * from ballot where id = ? }
521    );
522
523    $sth->execute($ballotid);
524    my $res = $sth->fetchrow_hashref;
525    $sth->finish;
526    $res
527}
528
529sub ballot_items {
530    my ($self, $ballotid) = @_;
531
532    my $sth = $self->db->prepare_cached(
533        q{select *, value as v from ballot_item where id = ?}
534    );
535    $sth->execute($ballotid);
536    my @ids;
537    while (my $res = $sth->fetchrow_hashref) {
538        push(@ids, $res);
539    }
540    @ids
541}
542
543sub addupd_voting {
544    my ($self, $voteid, $id, $mail) = @_;
545
546    my $upd = $self->db->prepare_cached(
547        q{
548        update voting set mail = ? where poll = ? and id = ?
549        }
550    );
551
552    if ($upd->execute($mail, $voteid, $id) == 0) {
553        my $add = $self->db->prepare_cached(q{
554            insert into voting (poll, id, mail) values (?,?,?)
555        });
556
557        $add->execute($voteid, $id, $mail);
558    }
559}
560
561sub delete_voting {
562    my ($self, $key) = @_;
563
564    my $sth = $self->db->prepare_cached(
565        q{delete from voting where key = ?}
566    );
567
568    $sth->execute($key);
569}
570
571sub voting_from_file {
572    my ($self, $voteid, $fh, $delete) = @_;
573
574    if ($delete) {
575        my $sth = $self->db->prepare(q{delete from voting where poll = ?});
576        $sth->execute($voteid);
577    }
578
579    while (my $line = <$fh>) {
580        chomp($line);
581        warn $line;
582        my ($id, $mail) = split(';', $line);
583        $id && $mail or do {
584            $self->db->rollback;
585            return;
586        };
587        $self->addupd_voting($voteid, $id, $mail);
588    }
589    1;
590}
591
592
593=head1 AUTHOR
594
595Thauvin Olivier
596
597=head1 LICENSE
598
599This library is free software, you can redistribute it and/or modify
600it under the same terms as Perl itself.
601
602=cut
603
6041;
Note: See TracBrowser for help on using the repository browser.