diff --git a/Kinza/lib/Kinza.pm b/Kinza/lib/Kinza.pm index 49a2b37..5000c14 100644 --- a/Kinza/lib/Kinza.pm +++ b/Kinza/lib/Kinza.pm @@ -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')) { @@ -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'); @@ -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, }; }; diff --git a/Kinza/lib/Kinza/Schema/Result/Year.pm b/Kinza/lib/Kinza/Schema/Result/Year.pm index 67975c1..4fd5a1f 100644 --- a/Kinza/lib/Kinza/Schema/Result/Year.pm +++ b/Kinza/lib/Kinza/Schema/Result/Year.pm @@ -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; diff --git a/Kinza/views/index.tt b/Kinza/views/index.tt index 0a910d7..e17ca5d 100644 --- a/Kinza/views/index.tt +++ b/Kinza/views/index.tt @@ -32,14 +32,15 @@ [% t.name %] [% END -%] -[% FOREACH c IN student.allowed_courses -%] +[% SET year = student.form.year; + FOREACH c IN year.get_allowed_courses -%] [% c.title %][% IF c.description %]
[% c.description %][% END %] [% 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 -%] 1; THEN %] colspan=[% p.number_of_terms; END %] title="[% t.name %]"> [% SET skip_terms = p.number_of_terms - 1;