@@ -282,17 +282,24 @@ public function orderBySubmissionId(string $direction = self::ORDER_DIR_ASC): st
282
282
public function getQueryBuilder (): Builder
283
283
{
284
284
$ q = DB ::table ($ this ->dao ->table . ' as ra ' )
285
- // Aggregate the latest review round number for the given assignment
285
+ // Aggregating data regarding latest review round and stage. For OMP the latest round isn't equal to the round with the highest number per submission
286
286
->leftJoinSub (
287
- DB ::table ('review_rounds ' , 'rr ' )
288
- ->select ('rr.submission_id ' )
289
- ->selectRaw ('MAX(rr.round) as current_round ' )
290
- ->selectRaw ('MAX(rr.stage_id) as current_stage ' )
291
- ->groupBy ('rr.submission_id ' ),
292
- 'agrr ' ,
293
- fn (JoinClause $ join ) => $ join ->on ('ra.submission_id ' , '= ' , 'agrr.submission_id ' )
294
- )
295
- ->select (['ra.* ' ]);
287
+ DB ::table ('review_assignments as rr ' )
288
+ ->select (['rr.submission_id ' , 'rr.stage_id ' ])
289
+ ->selectRaw ('MAX(rr.round) as latest_round ' )
290
+ ->groupBy ('rr.submission_id ' , 'rr.stage_id ' )
291
+ ->leftJoinSub (
292
+ DB ::table ('review_assignments as rs ' )
293
+ ->select ('rs.submission_id ' )
294
+ ->selectRaw ('MAX(rs.stage_id) as latest_stage ' )
295
+ ->groupBy ('rs.submission_id ' ),
296
+ 'rsmax ' ,
297
+ fn (JoinClause $ join ) => $ join ->on ('rr.submission_id ' , '= ' , 'rsmax.submission_id ' )
298
+ )
299
+ ->whereColumn ('rr.stage_id ' , 'rsmax.latest_stage ' ), // Take the highest round only from the latest stage
300
+ 'rrmax ' ,
301
+ fn (JoinClause $ join ) => $ join ->on ('ra.submission_id ' , '= ' , 'rrmax.submission_id ' )
302
+ );
296
303
297
304
$ q ->when (
298
305
$ this ->contextIds !== null ,
@@ -314,8 +321,8 @@ public function getQueryBuilder(): Builder
314
321
315
322
$ q ->when ($ this ->isLastReviewRound || $ this ->isIncomplete , function (Builder $ q ) {
316
323
$ q
317
- ->whereColumn ('ra.round ' , '= ' , 'agrr.current_round ' ) // assignments from the last review round only
318
- ->whereColumn ('ra.stage_id ' , '= ' , 'agrr.current_stage ' ) // assignments for the current review stage only (for OMP)
324
+ ->whereColumn ('ra.round ' , '= ' , 'rrmax.latest_round ' ) // assignments from the last review round only
325
+ ->whereColumn ('ra.stage_id ' , '= ' , 'rrmax.stage_id ' ) // assignments for the current review stage only (for OMP)
319
326
->when (
320
327
$ this ->isIncomplete ,
321
328
fn (Builder $ q ) => $ q
@@ -450,19 +457,33 @@ public function getQueryBuilder(): Builder
450
457
->when (
451
458
$ this ->isLastReviewRoundByReviewer ,
452
459
fn (Builder $ q ) => $ q
453
- // Aggregate the latest review round number for the given assignment and reviewer
454
460
->leftJoinSub (
461
+ // Determine the last review round the reviewer has assignments in
455
462
DB ::table ('review_assignments as ramax ' )
456
- ->select (['ramax.submission_id ' , 'ramax.reviewer_id ' ])
463
+ ->select (['ramax.submission_id ' , 'ramax.reviewer_id ' , ' ramax.stage_id ' ])
457
464
->selectRaw ('MAX(ramax.round) as latest_round ' )
458
- ->selectRaw ('MAX(ramax.stage_id) as latest_stage ' )
459
- ->whereIn ('ramax.reviewer_id ' , $ this ->reviewerIds )
460
- ->groupBy (['ramax.submission_id ' , 'ramax.reviewer_id ' ]),
461
- 'agrmax ' ,
462
- fn (JoinClause $ join ) => $ join ->on ('ra.submission_id ' , '= ' , 'agrmax.submission_id ' )
465
+ ->groupBy (['ramax.submission_id ' , 'ramax.reviewer_id ' , 'ramax.stage_id ' ])
466
+ /*
467
+ * Reviewers might have assignments in the Internal but not External review stage.
468
+ * Must aggregate data regarding the last review stage the reviewer has assignments in
469
+ */
470
+ ->leftJoinSub (
471
+ DB ::table ('review_assignments as rsmax ' )
472
+ ->select (['rsmax.submission_id ' , 'rsmax.reviewer_id ' ])
473
+ ->selectRaw ('MAX(rsmax.stage_id) as latest_stage ' )
474
+ ->groupBy (['rsmax.submission_id ' , 'rsmax.reviewer_id ' ])
475
+ ->where ('rsmax.reviewer_id ' , $ this ->reviewerIds ),
476
+ 'rssmax ' ,
477
+ fn (JoinClause $ join ) => $ join ->on ('ramax.submission_id ' , '= ' , 'rssmax.submission_id ' )
478
+ )
479
+ ->whereColumn ('ramax.reviewer_id ' , 'rssmax.reviewer_id ' ) // Take only selected reviewers
480
+ ->whereColumn ('ramax.stage_id ' , 'rssmax.latest_stage ' ), // Take only the current stage
481
+ 'raamax ' ,
482
+ fn (JoinClause $ join ) => $ join ->on ('ra.submission_id ' , '= ' , 'raamax.submission_id ' )
463
483
)
464
- ->whereColumn ('ra.round ' , 'agrmax.latest_round ' ) // assignments from the last review round per reviewer only
465
- ->whereColumn ('ra.stage_id ' , 'agrmax.latest_stage ' ) // assignments for the current review stage only (for OMP)
484
+ ->whereColumn ('ra.reviewer_id ' , 'raamax.reviewer_id ' ) // Finally fitler by reviewers
485
+ ->whereColumn ('ra.stage_id ' , 'raamax.stage_id ' ) // Finally filter by the latest review stage
486
+ ->whereColumn ('ra.round ' , 'raamax.latest_round ' ) // Finally filter by the latest review round
466
487
)
467
488
);
468
489
0 commit comments