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

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