Skip to content

Commit

Permalink
Allow cheez to download a CSV of results (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keltena authored Aug 19, 2024
1 parent fd849ca commit c81c830
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
50 changes: 50 additions & 0 deletions IFComp/lib/IFComp/Controller/Admin.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ sub index : Chained( 'root' ) : PathPart('') : Args(0) {
sub ballotcsv : Chained( 'root' ) : Args(0) {
my ( $self, $c ) = @_;

unless ( $c->user
&& $c->check_any_user_role( 'curator', ) )
{
$c->detach('/error_403');
return;
}

my $current_comp = $c->model('IFCompDB::Comp')->current_comp;
my @entries = $current_comp->entries();

Expand Down Expand Up @@ -81,6 +88,49 @@ sub ballotcsv : Chained( 'root' ) : Args(0) {
$c->res->body($output);
}

sub resultscsv : Chained( 'root' ) : Args(0) {
my ( $self, $c ) = @_;

unless ( $c->user
&& $c->check_any_user_role( 'cheez', ) )
{
$c->detach('/error_403');
return;
}

my $current_comp = $c->model('IFCompDB::Comp')->current_comp;
my @entries = $current_comp->entries();

my $csv = Text::CSV::Encoded->new( { binary => 1, encoding => "utf8" } )
or die "2 unable to create csv: $!";

my $output = "";
open my $fh, ">:encoding(utf8)", \$output or die "open fail $!";
$csv->column_names(
"author", "title", "place", "MissC",
"average", "stddev", "total_votes"
);

for my $entry ( $current_comp->entries ) {
$csv->print(
$fh,
[ $entry->author->name, $entry->title,
$entry->place, $entry->miss_congeniality_place,
$entry->average_score, $entry->standard_deviation,
$entry->votes_cast
]
);
print $fh "\n";
}
close $fh;

$c->res->header(
'Content-Disposition' => qq{attachment; filename="results.csv"} );
$c->res->content_type('text/csv; charset=utf-8');
$c->res->code(200);
$c->res->body($output);
}

=encoding utf8
=head1 AUTHOR
Expand Down
1 change: 1 addition & 0 deletions IFComp/root/src/admin/index.tt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
[% END %]
[% IF c.check_user_roles( 'cheez' ) %]
<li><a href="[% c.uri_for('/admin/validation/') %]">User Validation</a></li>
<li><a href="[% c.uri_for_action('/admin/resultscsv') %]">Download CSV of Results</a></li>
[% END %]
[% IF c.check_user_roles( 'prizemanager' ) %]
<li><a href="[% c.uri_for('/admin/prizes/list') %]">Prize Management</a></li>
Expand Down

0 comments on commit c81c830

Please sign in to comment.