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

Last change on this file since 17 was 17, checked in by nanardon, 15 years ago
  • check count of selected items
File size: 2.5 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    my $uid = $c->session->{uid} || $c->req->param('uid');
34    my $password = $c->session->{password} || $c->req->param('password');
35
36    if (!$c->model('Vote')->auth_voting($id, $uid, $password)) {
37        $c->delete_session('invalid user/pass');
38        $c->stash->{template} = 'ballot/login.tt';
39        return;
40    }
41
42    $c->session->{uid} = $uid;
43    $c->session->{password} = $password;
44
45    # login succeed, but those this user has already voted
46    if (my $date = $c->model('Vote')->voting_has_sign($id, $uid)) {
47        $c->stash->{uid} = $c->session->{uid};
48        $c->stash->{template} = 'ballot/signed.tt';
49        $c->stash->{signed_date} = $date;
50        $c->delete_session('invalid user/pass');
51        return;
52    }
53
54    my $vote = $c->model('Vote');
55    my %choices;
56    foreach ($vote->vote_choices($id)) {
57        $choices{$_->{key}} = $_->{label};
58    }
59    $c->stash->{choices} = { %choices };
60    $c->stash->{sbal} = { map { $_ => 1 } $c->req->param('sbal') };
61    $c->stash->{fsbal} = [ map {
62        s/^\s+//;
63        s/\s+$//;
64        s/\s+/ /g;
65        $_
66    } ($c->req->param('fsbal')) ];
67
68    my @torecord = grep { $_ } (
69        (map { $choices{$_} } $c->req->param('sbal')),
70        @{ $c->stash->{fsbal} }
71    );
72   
73    warn $vote->vote_info($id)->{choice_count};
74    warn scalar(@torecord);
75    warn $_ foreach(@torecord);
76    if (scalar(@torecord) > $vote->vote_info($id)->{choice_count}) {
77        $c->req->param('ballot', '');
78        $c->stash->{vote_error} = 'Seulement ' .
79            $vote->vote_info($id)->{choice_count} . ' choix possible';
80        return;
81    }
82
83    if ($c->req->param('confirm')) {
84        $vote->register_ballot(
85            $uid,
86            $id,
87            [ @torecord ],
88            $c->req->address,
89        ); # TODO trap error
90        $c->forward('done');
91    }
92}
93
94sub done : Private {
95    my ( $self, $c ) = @_;
96    $c->response->body('Vote réussi.');
97    $c->delete_session('Vote terminé');
98}
99
100=head1 AUTHOR
101
102Thauvin Olivier
103
104=head1 LICENSE
105
106This library is free software, you can redistribute it and/or modify
107it under the same terms as Perl itself.
108
109=cut
110
1111;
Note: See TracBrowser for help on using the repository browser.