Fix pb and log
[gruntmaster-page.git] / lib / Gruntmaster / Page / Generic.pm
index fe9525dc44a1feb392f30f290adcdd9450d0896f..9c5687b2b705ab77622da9217b453e9df41f1e05 100644 (file)
@@ -31,13 +31,15 @@ sub makepkg {
 sub list {
        my ($thing, $lang, $env) = @_;
        my %thing = %$thing;
+       my %params;
        #debug $env => "Contest is $ct";
        $thing{makers} //= sub { db(shift)->resultset($thing{rsname}) };
        my $rs = $thing{makers}->($env);
        $rs = $rs->search(undef, {order_by => 'me.id'}) unless $rs->is_ordered;
        if (my $page = $env->{'gruntmaster.page'}) {
-               my $pages = $rs->count / PAGE_SIZE;
+               my $pages = int ($rs->count / PAGE_SIZE);
                $page = $pages if $page == -1;
+               @params{'page', 'pages'} = ($page, $pages);
                $rs = $rs->search(undef, {offset => ($page - 1) * PAGE_SIZE, ($page == $pages ? () : (rows => PAGE_SIZE))});
        }
        $rs = $rs->search(undef, {prefetch => $thing{prefetch}}) if exists $thing{prefetch};
@@ -45,7 +47,6 @@ sub list {
        @thing = map  { $thing{mangle}->(); $_ } @thing if exists $thing{mangle};
        @thing = grep { $thing{choose}->() } @thing if exists $thing{choose};
        @thing = sort { $thing{sortby}->() } @thing if exists $thing{sortby};
-       my %params;
        $thing{group} //= sub { $thing{id} };
        for (@thing) {
                my $group = $thing{group}->();
@@ -116,23 +117,23 @@ thing {
        makers {
                my $env = $_[0];
                my $db = db $env;
-               return $db->contest($env->{'gruntmaster.contest'})->problems->search(undef, {order_by => 'problem.id'}) if exists $env->{'gruntmaster.contest'};
                return $db->problems->search({owner => $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'};
-               $db->problems->search({private => 0});
+               return $db->problems->search({'contest_problems.contest' => $env->{'gruntmaster.contest'}}, {join => 'contest_problems'}) if exists $env->{'gruntmaster.contest'};
+               $db->problems->search({-or => ['contest_problems.contest' => undef, 'contest.stop' => {'<=', time}], 'me.private' => 0}, {join => {'contest_problems' => 'contest'}});
        };
        sortby { $a->{name} cmp $b->{name}};
        group { $_->{level} };
        mangle {
                my $env = shift;
                $_->{owner_name} = $_->{rs}->owner->name;
-               $_->{cansubmit} = $_->{contest} ? time < $_->{rs}->contest->stop : 1;
+               $_->{cansubmit} = $env->{'gruntmaster.contest'} ? time < db($env)->contest($env->{'gruntmaster.contest'})->stop : 1;
                eval {
                        db($env)->open->create({
-                               contest => $_->{contest},
+                               contest => $env->{'gruntmaster.contest'},
                                problem => $_->{id},
                                owner   => $env->{REMOTE_USER},
                        })
-               } if $_->{contest} && time >= $_->{rs}->contest->start;
+               } if $env->{'gruntmaster.contest'} && time >= db($env)->contest($env->{'gruntmaster.contest'})->start;
        };
 };
 
This page took 0.01162 seconds and 4 git commands to generate.