Skip to content

Commit

Permalink
Moved reports to a separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Aug 9, 2015
1 parent a18e67e commit 29728d2
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 70 deletions.
75 changes: 5 additions & 70 deletions Kinza/lib/Kinza.pm
Expand Up @@ -6,8 +6,12 @@ use Dancer2::Plugin::Email;
use Dancer2::Plugin::Passphrase;
use DateTime;

use Kinza::Reports;

our $VERSION = '0.1';

prefix undef;

$ENV{KZ_USER} && $ENV{KZ_PASS}
or die 'Must set KZ_USER and KZ_PASS';

Expand Down Expand Up @@ -157,76 +161,7 @@ get '/dummies' => sub {
};

get '/reports' => sub {
template 'reports';
};

get '/reports/form' => sub {
content_type 'text/csv';
header 'Content-Disposition' => 'attachment; filename="form.csv"';

my $csv;
my $terms = join ',', map { $_->name } $rs{Term}->all;

foreach my $y (schema->resultset('Year')->search({}, {
order_by => 'id',
})) {
$csv .= $y->name . "\n";

foreach my $f ($y->forms->search({}, { order_by => 'id' })) {
$csv .= $f->name . "\n";
$csv .= "Name,$terms\n";

foreach my $s ($f->students->search({}, { order_by => 'name' })) {
$csv .= $s->name;
foreach my $a ($s->sorted_attendances) {
$csv .= ',"' . $a->presentation->course->title . '"';
}
$csv .= "\n";
}
$csv .= "\n";
}
}

return $csv;
};

get '/reports/course' => sub {
content_type 'text/csv';
header 'Content-Disposition' => 'attachment; filename="course.csv"';

my $csv;

foreach my $p ($rs{Presentation}->search({}, { order_by => 'id' })) {
$csv .= '"' . $p->course->title . '/' . $p->term->name . qq["\n];
foreach my $a ($p->attendances) {
$csv .= $a->student->name. "\n";
}
$csv .= "\n";
}

return $csv;
};

get '/reports/numbers' => sub {
content_type 'text/csv';
header 'Content-Disposition' => 'attachment; filename="numbers.csv"';

my @terms = $rs{Term}->all;
my $csv = 'Course,' . join(',', map { $_->name} @terms) . "\n";

foreach my $c ($rs{Course}->search({}, { order_by => 'title' })) {
$csv .= '"' . $c->title . '"';
foreach my $t (@terms) {
if (my $p = $t->presentations->find({ course_id => $c->id })) {
$csv .= ',' . $p->attendances->count;
} else {
$csv .= ',';
}
}
$csv .= "\n";
}

return $csv;
return redirect '/reports/';
};

get '/register' => sub {
Expand Down
94 changes: 94 additions & 0 deletions Kinza/lib/Kinza/Reports.pm
@@ -0,0 +1,94 @@
package Kinza;
use 5.010;
use Dancer2 appname => 'Kinza';
use Dancer2::Plugin::DBIC;
use Dancer2::Plugin::Email;
use Dancer2::Plugin::Passphrase;
use DateTime;

our $VERSION = '0.1';

prefix '/reports';

my $cfg = config->{plugins};
$cfg->{DBIC}{default}{user} = $ENV{KZ_USER};
$cfg->{DBIC}{default}{password} = $ENV{KZ_PASS};

my %rs = map {
$_ => schema()->resultset($_)
} qw[Student Term Course Presentation PasswordReset];

get '/' => sub {
template 'reports';
};

get '/form' => sub {
content_type 'text/csv';
header 'Content-Disposition' => 'attachment; filename="form.csv"';

my $csv;
my $terms = join ',', map { $_->name } $rs{Term}->all;

foreach my $y (schema->resultset('Year')->search({}, {
order_by => 'id',
})) {
$csv .= $y->name . "\n";

foreach my $f ($y->forms->search({}, { order_by => 'id' })) {
$csv .= $f->name . "\n";
$csv .= "Name,$terms\n";

foreach my $s ($f->students->search({}, { order_by => 'name' })) {
$csv .= $s->name;
foreach my $a ($s->sorted_attendances) {
$csv .= ',"' . $a->presentation->course->title . '"';
}
$csv .= "\n";
}
$csv .= "\n";
}
}

return $csv;
};

get '/course' => sub {
content_type 'text/csv';
header 'Content-Disposition' => 'attachment; filename="course.csv"';

my $csv;

foreach my $p ($rs{Presentation}->search({}, { order_by => 'id' })) {
$csv .= '"' . $p->course->title . '/' . $p->term->name . qq["\n];
foreach my $a ($p->attendances) {
$csv .= $a->student->name. "\n";
}
$csv .= "\n";
}

return $csv;
};

get '/numbers' => sub {
content_type 'text/csv';
header 'Content-Disposition' => 'attachment; filename="numbers.csv"';

my @terms = $rs{Term}->all;
my $csv = 'Course,' . join(',', map { $_->name} @terms) . "\n";

foreach my $c ($rs{Course}->search({}, { order_by => 'title' })) {
$csv .= '"' . $c->title . '"';
foreach my $t (@terms) {
if (my $p = $t->presentations->find({ course_id => $c->id })) {
$csv .= ',' . $p->attendances->count;
} else {
$csv .= ',';
}
}
$csv .= "\n";
}

return $csv;
};

true;

0 comments on commit 29728d2

Please sign in to comment.