Skip to content

Commit

Permalink
#8887 Review assignments count
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy-1 committed Oct 30, 2023
1 parent 9c06a4e commit b9cda09
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 39 deletions.
10 changes: 7 additions & 3 deletions api/v1/_submissions/PKPBackendSubmissionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,17 @@ public function getGroupRoutes(): void
Role::ROLE_ID_MANAGER,
Role::ROLE_ID_SUB_EDITOR,
Role::ROLE_ID_ASSISTANT,
Role::ROLE_ID_AUTHOR,
Role::ROLE_ID_REVIEWER,
])
]);

Route::get('reviewerAssignments', $this->getReviewAssignments(...))
->name('_submission.getReviewAssignments')
->middleware([
Role::ROLE_ID_REVIEWER,
self::roleAuthorizer([
Role::ROLE_ID_REVIEWER,
])
]);
}
}
Expand Down Expand Up @@ -328,7 +332,7 @@ public function reviews(Request $illuminateRequest): JsonResponse
}

/**
* Get a number of the submissions for each view
* Get a number of the submissions/review assignments for each view depending on a user role
*/
public function getViewsCount(Request $illuminateRequest): JsonResponse
{
Expand Down Expand Up @@ -363,7 +367,7 @@ public function getReviewAssignments(Request $illuminateRequest): JsonResponse
}
$currentUser = $request->getUser();
$collector = Repo::reviewAssignment()->getCollector()
->filterByReviewRoundIds([$currentUser->getId()])
->filterByReviewerIds([$currentUser->getId()])
->filterByContextIds([$context->getId()]);

foreach ($illuminateRequest->query() as $param => $val) {
Expand Down
17 changes: 14 additions & 3 deletions classes/submission/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@
/**
* @template T of Submission
*/
abstract class Collector implements CollectorInterface
abstract class Collector implements CollectorInterface, ViewsCount
{
use ViewsCount;

public const ORDERBY_DATE_PUBLISHED = 'datePublished';
public const ORDERBY_DATE_SUBMITTED = 'dateSubmitted';
public const ORDERBY_ID = 'id';
Expand Down Expand Up @@ -776,6 +774,19 @@ protected function buildReviewStageQueries(Builder $q): Builder
return $q;
}

public static function getViewsCountBuilder(Collection $keyCollectorPair): Builder
{
$q = DB::query();
$keyCollectorPair->each(function(AppCollector $collector, string $key) use ($q) {
// Get query builder from a collector instance, override a select statement to retrieve submissions count instead of submissions data
$subQuery = $collector->getQueryBuilder()->select([])->selectRaw(
'COUNT(s.submission_id)'
);
$q->selectSub($subQuery, $key);
});
return $q;
}

protected function getReviewStages(): array
{
return [WORKFLOW_STAGE_ID_EXTERNAL_REVIEW];
Expand Down
2 changes: 1 addition & 1 deletion classes/submission/DashboardView.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static function getTypes(): Collection
/**
* Get the collector with filters applied to retrieve submissions for the view
*/
public function getCollector(): Collector
public function getCollector(): SubmissionCollector|ReviewAssignmentCollector
{
return $this->submissionCollector;
}
Expand Down
34 changes: 23 additions & 11 deletions classes/submission/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use PKP\services\PKPSchemaService;
use PKP\stageAssignment\StageAssignmentDAO;
use PKP\submission\Collector as SubmissionCollector;
use PKP\submission\reviewAssignment\Collector as ReviewCollector;
use PKP\submissionFile\SubmissionFile;
use PKP\user\User;
use PKP\validation\ValidatorFactory;
Expand Down Expand Up @@ -789,7 +790,6 @@ public function getUrlSubmissionWizard(Context $context, ?int $submissionId = nu

/**
* Get all views, views count to be retrieved separately due to performance reasons
* TODO return views depending on a user role
*/
public function getDashboardViews(Context $context, User $user): Collection
{
Expand Down Expand Up @@ -1044,7 +1044,7 @@ public function getDashboardViews(Context $context, User $user): Collection

$filteredViews = $this->filterViewsByUserRoles($views, $roleIds);

return $this->getViewsCount($filteredViews);
return $this->setViewsCount($filteredViews);
}

protected function filterViewsByUserRoles(Collection $views, array $roleIds): Collection
Expand All @@ -1055,19 +1055,31 @@ protected function filterViewsByUserRoles(Collection $views, array $roleIds): Co
}

/**
* @param Collection [
* Dashboard view unique ID => Submission Collector with filters applied
* ]
* @param Collection<DashboardView> $dashboardViews
*
* Set the submissions/reviews count to the list of dashboard views
*/
protected function getViewsCount(Collection $dashboardViews): Collection
protected function setViewsCount(Collection $dashboardViews): Collection
{
$collectors = $dashboardViews->map(function (DashboardView $view) {
return $view->getCollector();
});
$submissionCollectors = collect();
$reviewCollectors = collect();
foreach ($dashboardViews as $id => $dashboardView) {
$collector = $dashboardView->getCollector();
is_a($collector, SubmissionCollector::class) ?
$submissionCollectors->put($id, $collector) :
$reviewCollectors->put($id, $collector);
}

$submissionsCount = $submissionCollectors->isNotEmpty() ?
get_object_vars(SubmissionCollector::getViewsCountBuilder($submissionCollectors)?->first() ?? []) :
[];

$reviewsCount = $reviewCollectors->isNotEmpty() ?
get_object_vars(ReviewCollector::getViewsCountBuilder($reviewCollectors)?->first() ?? []) :
[];

$viewCounts = SubmissionCollector::getViewsCountBuilder($collectors)->first();

foreach ($viewCounts as $viewId => $count) {
foreach (array_merge($submissionsCount, $reviewsCount) as $viewId => $count) {
$view = $dashboardViews->get($viewId); /** @var $view DashboardView */
$view->setCount($count);
}
Expand Down
22 changes: 5 additions & 17 deletions classes/submission/ViewsCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,22 @@
*
* @class ViewsCount
*
* @brief trait to use with a collector to build a query for counting submissions/reviewAssignments in a view
* @brief interface to use with a collector to build a query for counting submissions/reviewAssignments in a view
*/

namespace PKP\submission;

use APP\submission\Collector as AppCollector;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use PKP\core\interfaces\CollectorInterface;

trait ViewsCount
interface ViewsCount
{
/**
* Builds a single query to retrieve submissions count for all dashboard views
* @param Collection [
* @param Collection<string, CollectorInterface> $keyCollectorPair [
* Dashboard view unique ID => Submission Collector with filters applied
* ]
*/
public static function getViewsCountBuilder(Collection $keyCollectorPair): Builder
{
$q = DB::query();
$keyCollectorPair->each(function(AppCollector $collector, string $key) use ($q) {
// Get query builder from a collector instance, override a select statement to retrieve submissions count instead of submissions data
$subQuery = $collector->getQueryBuilder()->select([])->selectRaw(
'COUNT('. $this->dao->table . '.' . $this->dao->primaryKeyColumn . ')'
);
$q->selectSub($subQuery, $key);
});
return $q;
}
public static function getViewsCountBuilder(Collection $keyCollectorPair): Builder;
}
19 changes: 15 additions & 4 deletions classes/submission/reviewAssignment/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
/**
* @template T of ReviewAssignment
*/
class Collector implements CollectorInterface
class Collector implements CollectorInterface, ViewsCount
{
use ViewsCount;

public DAO $dao;
public ?array $contextIds = null;
public ?array $submissionIds = null;
Expand Down Expand Up @@ -225,7 +223,7 @@ public function getQueryBuilder(): Builder
->whereIn('ra.submission_id', fn(Builder $q) => $q
->select('s.submission_id')
->from('submissions AS s')
->where('s.stage_id', 'ra.stage_id')
->whereRaw('s.stage_id = ra.stage_id')
)
)
);
Expand Down Expand Up @@ -284,4 +282,17 @@ public function getQueryBuilder(): Builder

return $q;
}

public static function getViewsCountBuilder(Collection $keyCollectorPair): Builder
{
$q = DB::query();
$keyCollectorPair->each(function(Collector $collector, string $key) use ($q) {
// Get query builder from a collector instance, override a select statement to retrieve submissions count instead of submissions data
$subQuery = $collector->getQueryBuilder()->select([])->selectRaw(
'COUNT(ra.review_id)'
);
$q->selectSub($subQuery, $key);
});
return $q;
}
}

0 comments on commit b9cda09

Please sign in to comment.