Skip to content

Commit

Permalink
Fix opening time logic. Cut down on SQL queries to build selection page.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Sep 19, 2015
1 parent 4003601 commit bdc8900
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
34 changes: 26 additions & 8 deletions Kinza/lib/Kinza.pm
Expand Up @@ -38,11 +38,15 @@ my $now = DateTime->now(time_zone => 'Europe/London');
my $reg_live = $dt_p->parse_datetime($ENV{KZ_REG_OPEN});
my $sel_live = $dt_p->parse_datetime($ENV{KZ_SEL_OPEN});

my %private = map { $_ => 1 } qw[/submit];
my %open = map { $_ => 1 } qw[/closed /years /reports];
my %private = map { $_ => 1 } qw[/submit];
my %open = map { $_ => 1 } qw[/closed /years /reports];
my %reg_open = (%open, map { $_ => 1 } qw[/register]);

hook before => sub {
if (! $open{request->path_info}) {
if ($now < $reg_live && ! $open{request->path_info}) {
forward '/closed';
}
if (session('name') && $now < $sel_live && ! $open{request->path_info}) {
forward '/closed';
}
if ($private{request->path_info} and ! session('user')) {
Expand All @@ -67,13 +71,14 @@ hook before_template => sub {
};

get '/closed' => sub {
return template 'comingsoon';
if ($now < $reg_live) {
return template 'comingsoon';
} else {
return template 'sel_closed';
}
};

get '/' => sub {
if (session('name') and $now < $sel_live) {
return template 'sel_closed';
}
my $error = session('error');
session 'error' => undef;
my $choices = session('choices');
Expand All @@ -90,12 +95,25 @@ get '/' => sub {
$choices = $student->choices;
}

my @terms = $rs{Term}->search({}, {
prefetch => { presentations => 'attendances' },
order_by => 'seq',
})->all;

my %term_course;
foreach my $t (@terms) {
foreach my $p ($t->presentations) {
$term_course{$t->id}{$p->course_id} = $p;
}
}

template 'index', {
error => $error,
choices => $choices,
student => $student,
courses => [ $rs{Course}->all ],
terms => [ $rs{Term}->all ],
terms => \@terms,
term_course => \%term_course,
};
};

Expand Down
12 changes: 12 additions & 0 deletions Kinza/lib/Kinza/Schema/Result/Year.pm
Expand Up @@ -123,6 +123,18 @@ __PACKAGE__->many_to_many(
'course',
);

sub get_allowed_courses {
my $self = shift;

return $self->allowed_courses({}, {
prefetch => { presentations => 'term' },
join => {presentations => 'attendances' },
'+select' => { count => 'attendances.id' },
'+as' => [ 'att_count' ],
group_by => [ 'course.id', 'presentations.id' ],
});
}

# You can replace this text with custom code or comments, and it will be preserved on regeneration
__PACKAGE__->meta->make_immutable;
1;
5 changes: 3 additions & 2 deletions Kinza/views/index.tt
Expand Up @@ -32,14 +32,15 @@
<th width="22%">[% t.name %]</th>
[% END -%]
</thead>
[% FOREACH c IN student.allowed_courses -%]
[% SET year = student.form.year;
FOREACH c IN year.get_allowed_courses -%]
<tr>
<td>[% c.title %][% IF c.description %]<br><span class="small">[% c.description %]</span>[% END %]</td>
[% SET total_terms = terms.size;
SET skip_terms = 0;
FOREACH t IN terms;
IF ! skip_terms; THEN;
SET p = c.in_term(t.id);
SET p = term_course.${t.id}.${c.id};
IF p; THEN -%]
<td class="option"[% IF p.number_of_terms > 1; THEN %] colspan=[% p.number_of_terms; END %] title="[% t.name %]"><input type="radio" class="C[% c.id %][% FOREACH i IN [1 .. p.number_of_terms]; SET id = t.id+loop.index; ' T' _ id; END %]" name="T[% p.term.seq %]" value="[% p.id %]"[% IF choices.${p.id} %] checked[% ELSIF p.full %] disabled[% END %]></td>
[% SET skip_terms = p.number_of_terms - 1;
Expand Down

0 comments on commit bdc8900

Please sign in to comment.