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

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