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

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