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

Last change on this file since 14 was 14, checked in by nanardon, 15 years ago
  • check already voted
File size: 2.0 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    $c->response->body('Matched Vote::Controller::Ballot in Vote.');
28}
29
30sub default : Private {
31    my ( $self, $c, undef, $id ) = @_;
32
33    $c->stash->{voteid} = $id;
34
35    my $uid = $c->session->{uid} || $c->req->param('uid');
36    my $password = $c->session->{password} || $c->req->param('password');
37
38    if (!$c->model('Vote')->auth_voting($id, $uid, $password)) {
39        $c->delete_session('invalid user/pass');
40        $c->stash->{template} = 'ballot/login.tt';
41        return;
42    }
43
44    $c->session->{uid} = $uid;
45    $c->session->{password} = $password;
46
47    # login succeed, but those this user has already voted
48    if (my $date = $c->model('Vote')->voting_has_sign($id, $uid)) {
49        $c->stash->{uid} = $c->session->{uid};
50        $c->stash->{template} = 'ballot/signed.tt';
51        $c->stash->{signed_date} = $date;
52        $c->delete_session('invalid user/pass');
53        return;
54    }
55
56    my $vote = $c->model('Vote');
57    my %choices;
58    foreach ($vote->vote_choices($id)) {
59        $choices{$_->{key}} = $_->{label};
60    }
61    $c->stash->{choices} = { %choices };
62    $c->stash->{sbal} = { map { $_ => 1 } $c->req->param('sbal') };
63
64    if ($c->req->param('confirm')) {
65        $vote->register_ballot(
66            $uid,
67            $id,
68            [ grep { $_ } (
69              map { $choices{$_} } $c->req->param('sbal'),
70              $c->req->param('fsbal'),
71            ) ],
72            $c->req->address,
73        ); # TODO trap error
74        $c->forward('done');
75    }
76}
77
78sub done : Private {
79    my ( $self, $c ) = @_;
80    $c->response->body('Vote réussi.');
81    $c->delete_session('Vote terminé');
82}
83
84=head1 AUTHOR
85
86Thauvin Olivier
87
88=head1 LICENSE
89
90This library is free software, you can redistribute it and/or modify
91it under the same terms as Perl itself.
92
93=cut
94
951;
Note: See TracBrowser for help on using the repository browser.