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

Last change on this file since 16 was 16, checked in by nanardon, 15 years ago
  • add admin interface
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
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
62    if ($c->req->param('confirm')) {
63        $vote->register_ballot(
64            $uid,
65            $id,
66            [ grep { $_ } (
67              map { $choices{$_} } $c->req->param('sbal'),
68              $c->req->param('fsbal'),
69            ) ],
70            $c->req->address,
71        ); # TODO trap error
72        $c->forward('done');
73    }
74}
75
76sub done : Private {
77    my ( $self, $c ) = @_;
78    $c->response->body('Vote réussi.');
79    $c->delete_session('Vote terminé');
80}
81
82=head1 AUTHOR
83
84Thauvin Olivier
85
86=head1 LICENSE
87
88This library is free software, you can redistribute it and/or modify
89it under the same terms as Perl itself.
90
91=cut
92
931;
Note: See TracBrowser for help on using the repository browser.