source: trunk/lib/Vote/Controller/Ballot.pm @ 19

Last change on this file since 19 was 19, checked in by nanardon, 15 years ago
  • check double value in ballot
File size: 2.8 KB
Line 
1package Vote::Controller::Ballot;
2
3use strict;
4use warnings;
5use base 'Catalyst::Controller';
6
7=head1 NAME
8
9Vote::Controller::Ballot - Catalyst Controller
10
11=head1 DESCRIPTION
12
13Catalyst Controller.
14
15=head1 METHODS
16
17=cut
18
19
20=head2 index
21
22=cut
23
24sub index : Private {
25    my ( $self, $c ) = @_;
26}
27
28sub default : Private {
29    my ( $self, $c, undef, $id ) = @_;
30
31    $c->stash->{voteid} = $id;
32
33    if ($c->model('Vote')->vote_status($id) ne 'RUNNING') {
34        $c->stash->{template} = 'ballot/closed.tt';
35        return;
36    }
37
38    my $uid = $c->session->{uid} || $c->req->param('uid');
39    my $password = $c->session->{password} || $c->req->param('password');
40
41    if (!$c->model('Vote')->auth_voting($id, $uid, $password)) {
42        $c->delete_session('invalid user/pass');
43        $c->stash->{template} = 'ballot/login.tt';
44        return;
45    }
46
47    $c->session->{uid} = $uid;
48    $c->session->{password} = $password;
49
50    # login succeed, but those this user has already voted
51    if (my $date = $c->model('Vote')->voting_has_sign($id, $uid)) {
52        $c->stash->{uid} = $c->session->{uid};
53        $c->stash->{template} = 'ballot/signed.tt';
54        $c->stash->{signed_date} = $date;
55        $c->delete_session('invalid user/pass');
56        return;
57    }
58
59    my $vote = $c->model('Vote');
60    my %choices;
61    foreach ($vote->vote_choices($id)) {
62        $choices{$_->{key}} = $_->{label};
63    }
64    $c->stash->{choices} = { %choices };
65    $c->stash->{sbal} = { map { $_ => 1 } $c->req->param('sbal') };
66    $c->stash->{fsbal} = [ map {
67        s/^\s+//;
68        s/\s+$//;
69        s/\s+/ /g;
70        lc($_)
71    } ($c->req->param('fsbal')) ];
72
73    my @torecord = grep { $_ } (
74        (map { $choices{$_} } $c->req->param('sbal')),
75        @{ $c->stash->{fsbal} }
76    );
77   
78    if (scalar(@torecord) > $vote->vote_info($id)->{choice_count}) {
79        $c->req->param('ballot', '');
80        $c->stash->{vote_error} = 'Seulement ' .
81            $vote->vote_info($id)->{choice_count} . ' choix possible';
82        return;
83    }
84    {
85        my %uniq;
86        $uniq{lc($_)} foreach(@torecord);
87        if (scalar(keys %uniq) != scalar(@torecord)) {
88            $c->req->param('ballot', '');
89            $c->stash->{vote_error} = 'Une valeur est en double';
90            return;
91        }
92    }
93
94    if ($c->req->param('confirm')) {
95        $vote->register_ballot(
96            $uid,
97            $id,
98            [ @torecord ],
99            $c->req->address,
100        ); # TODO trap error
101        $c->forward('done');
102    }
103}
104
105sub done : Private {
106    my ( $self, $c ) = @_;
107    $c->response->body('Vote réussi.');
108    $c->delete_session('Vote terminé');
109}
110
111=head1 AUTHOR
112
113Thauvin Olivier
114
115=head1 LICENSE
116
117This library is free software, you can redistribute it and/or modify
118it under the same terms as Perl itself.
119
120=cut
121
1221;
Note: See TracBrowser for help on using the repository browser.