refactor(web/group/assignment): more simple db query
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Baoshuo Ren 2022-10-22 08:07:41 +08:00
parent 49994cdc65
commit a28e34e7e9
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -78,23 +78,16 @@
]; ];
$cond = "submitter = '{$user['username']}' AND unix_timestamp(submit_time) <= " . $submission_end_time->getTimestamp(); $cond = "submitter = '{$user['username']}' AND unix_timestamp(submit_time) <= " . $submission_end_time->getTimestamp();
$cond_problem = "problem_id IN (".implode(',', $problem_ids).")";
$query = DB::query("SELECT MAX(id) as id, problem_id, MAX(score) as score FROM submissions WHERE (problem_id, score) IN (SELECT problem_id, MAX(score) FROM submissions WHERE $cond AND $cond_problem GROUP BY problem_id) AND $cond GROUP BY problem_id");
while ($_row = DB::fetch($query)) {
$scores[$_row['problem_id']] = [
'submission_id' => $_row['id'],
'score' => $_row['score'],
];
}
foreach ($problem_ids as $problem_id) { foreach ($problem_ids as $problem_id) {
if ($scores[$problem_id]) { $submission = DB::selectFirst("SELECT id, score FROM submissions WHERE problem_id = $problem_id AND $cond ORDER BY score DESC, id DESC");
if ($submission) {
$row['scores'][] = [ $row['scores'][] = [
'submission_id' => $scores[$problem_id]['submission_id'], 'submission_id' => $submission['id'],
'score' => intval($scores[$problem_id]['score']), 'score' => intval($submission['score']),
]; ];
$row['total_score'] += $scores[$problem_id]['score']; $row['total_score'] += $submission['score'];
} else { } else {
$row['scores'][] = null; $row['scores'][] = null;
} }