Changeset 269 for trunk/lib/Epoll


Ignore:
Timestamp:
12/12/09 19:40:32 (15 years ago)
Author:
nanardon
Message:
  • allow to delete ballot after poll (then keeping only results in DB)
Location:
trunk/lib/Epoll
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Epoll/Controller/Admin.pm

    r267 r269  
    296296} 
    297297 
     298sub delete: LocalRegex('^(\w+)/delete$') { 
     299    my ($self, $c) = @_; 
     300    ($c->stash->{voteid}) = @{ $c->req->snippets || [] }; 
     301    my $poll = $c->model('Vote')->results($c->stash->{voteid}); 
     302 
     303    $poll->info or do { 
     304        $c->res->redirect($c->uri_for('/')); 
     305        return; 
     306    }; 
     307 
     308    $c->forward('auth') or return; 
     309 
     310    if ($c->req->param('delete_ballot')) { 
     311        $c->stash->{template} = 'admin/delete_ballot_confirm.tt'; 
     312        return; 
     313    } 
     314 
     315    if ($c->req->param('delete_ballot_confirm')) { 
     316        $poll->delete_ballots; 
     317        $c->response->redirect($c->uri_for($poll->uid)); 
     318    } 
     319} 
     320 
    298321sub ajaxsettings: LocalRegex('^(\w+)/ajaxsettings$') { 
    299322    my ($self, $c) = @_; 
  • trunk/lib/Epoll/DB/Poll.pm

    r267 r269  
    997997} 
    998998 
     999################# 
     1000# CLEANING DATA # 
     1001################# 
     1002 
     1003sub _delete_ballot { 
     1004    my ($self) = @_; 
     1005 
     1006    foreach ( 
     1007        q{delete from ballot_item where id in (select id from ballot where 
     1008        ballot.poll = ?)}, 
     1009        q{delete from ballot where poll = ?}, 
     1010        q{delete from ballot_enc where poll = ?},) { 
     1011        my $req = $self->db->prepare($_); 
     1012        $req->execute($self->voteid) or return; 
     1013    } 
     1014 
     1015    return 1; 
     1016} 
     1017 
    9991018=head1 AUTHOR 
    10001019 
  • trunk/lib/Epoll/DB/Poll/Results.pm

    r250 r269  
    66use warnings; 
    77use base 'Epoll::DB::Poll'; 
     8use XML::Simple; 
     9use vars qw($AUTOLOAD); 
    810 
    911=head1 NAME 
     
    1820 
    1921sub new { 
    20     my ($class, %args) = @_; 
    21  
    22     my $res = $class->SUPER::new(%args); 
    23      
    24     return bless $res, $class; 
    25 } 
    26  
    27 sub absolute_majority { 
     22    my ($class, @args) = @_; 
     23 
     24    my $res = $class->SUPER::new(@args); 
     25 
     26    $res = bless $res, $class; 
     27    $res->load_static_data; 
     28    $res 
     29} 
     30 
     31sub AUTOLOAD { 
     32    my ($self, @args) = @_; 
     33    my ($constname, $sub) = $AUTOLOAD =~ m/(.*)::([^:]+)/; 
     34    $sub =~ /^__(.*)/ and die "No sub $1"; 
     35    if ($self->{static_results}) { 
     36        return $self->{static_results}{$sub}; 
     37    } else { 
     38        my $realsub = "__$sub"; 
     39        return $self->$realsub(@args); 
     40    } 
     41} 
     42 
     43sub DESTROY {} 
     44 
     45sub load_static_data { 
     46    my ($self) = @_; 
     47    if (my $res = $self->info('static_results')) { 
     48        $self->{static_results} = XMLin( 
     49            $res, 
     50            ForceArray => ['results'] 
     51        ); 
     52    } 
     53} 
     54 
     55sub ballot_count { 
     56    my ($self) = @_; 
     57    if ($self->{static_results}) { 
     58        return $self->{static_results}{ballot_count}; 
     59    } else { 
     60        return $self->SUPER::ballot_count(); 
     61    } 
     62} 
     63 
     64sub __absolute_majority { 
    2865    my ($self) = @_; 
    2966    my $ballot_count = $self->voices_ballot_count; 
     
    3168}     
    3269 
    33 sub abstention { 
     70sub __abstention { 
    3471    my ($self) = @_; 
    3572     
     
    4885} 
    4986 
    50 sub ballot_count_nonull {not_empty_ballot_count(@_)} 
    51  
    52 sub voices_ballot_count { 
     87sub __ballot_count_nonull {not_empty_ballot_count(@_)} 
     88 
     89sub __voices_ballot_count { 
    5390    my ($self) = @_; 
    5491    return $self->info('empty_ballot_has_voice') 
     
    5794} 
    5895 
    59 sub valid_ballot_count { 
     96sub __valid_ballot_count { 
    6097    my ($self) = @_; 
    6198 
     
    73110} 
    74111 
    75 sub invalid_ballot_count { 
     112sub __invalid_ballot_count { 
    76113    my ($self) = @_; 
    77114 
     
    89126} 
    90127 
    91 sub empty_ballot_count { 
     128sub __empty_ballot_count { 
    92129    my ($self) = @_; 
    93130 
     
    106143} 
    107144 
    108 sub not_empty_ballot_count { 
     145sub __not_empty_ballot_count { 
    109146    my ($self) = @_; 
    110147 
     
    133170} 
    134171 
    135 sub results { 
     172sub __results { 
    136173    my ($self) = @_; 
    137174    $self->_results( 
     
    190227} 
    191228 
     229sub dump_results { 
     230    my ($self) = @_; 
     231    my $xml = XML::Simple->new(ForceArray => 1, RootName => 'results'); 
     232    my %results; 
     233 
     234    foreach my $val (qw( 
     235        ballot_count 
     236        ), 
     237        map { s/^__//; $_ } grep { /^__/ } keys %{Epoll::DB::Poll::Results::} 
     238        ) { 
     239        $results{$val} = $self->$val(); 
     240    } 
     241    $xml->XMLout({ 
     242        %results 
     243    }); 
     244} 
     245 
     246sub delete_ballots { 
     247    my ($self) = @_; 
     248 
     249    $self->param('static_results' => $self->dump_results); 
     250    $self->_delete_ballot; 
     251    $self->commit; 
     252    $self->{static_results} = XMLin( 
     253        $self->info('static_results'), 
     254        ForceArray => ['results'] 
     255    ); 
     256} 
     257 
    192258=head1 AUTHOR 
    193259 
Note: See TracChangeset for help on using the changeset viewer.