forked from pkp/pkp-lib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DAO.php
199 lines (179 loc) · 5.56 KB
/
DAO.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* @file classes/submission/reviewAssignment/DAO.php
*
* Copyright (c) 2014-2023 Simon Fraser University
* Copyright (c) 2000-2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class DAO
*
* @brief Read and write review assignments to the database.
*/
namespace PKP\submission\reviewAssignment;
use APP\facades\Repo;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\LazyCollection;
use PKP\core\EntityDAO;
/**
* @template T of ReviewAssignment
*
* @extends EntityDAO<T>
*/
class DAO extends EntityDAO
{
/** @copydoc EntityDAO::$schema */
public $schema = \PKP\services\PKPSchemaService::SCHEMA_REVIEW_ASSIGNMENT;
/** @copydoc EntityDAO::$table */
public $table = 'review_assignments';
/** @copydoc EntityDAO::$primaryKeyColumn */
public $primaryKeyColumn = 'review_id';
/** @copydoc EntityDAO::$primaryTableColumns */
public $primaryTableColumns = [
'id' => 'review_id',
'submissionId' => 'submission_id',
'reviewerId' => 'reviewer_id',
'competingInterests' => 'competing_interests',
'recommendation' => 'recommendation',
'dateAssigned' => 'date_assigned',
'dateNotified' => 'date_notified',
'dateConfirmed' => 'date_confirmed',
'dateCompleted' => 'date_completed',
'dateAcknowledged' => 'date_acknowledged',
'dateDue' => 'date_due',
'dateResponseDue' => 'date_response_due',
'lastModified' => 'last_modified',
'reminderWasAutomatic' => 'reminder_was_automatic',
'declined' => 'declined',
'cancelled' => 'cancelled',
'dateRated' => 'date_rated',
'dateReminded' => 'date_reminded',
'quality' => 'quality',
'reviewRoundId' => 'review_round_id',
'stageId' => 'stage_id',
'reviewMethod' => 'review_method',
'round' => 'round',
'step' => 'step',
'reviewFormId' => 'review_form_id',
'considered' => 'considered',
'requestResent' => 'request_resent',
];
/**
* Instantiate a new DataObject
*/
public function newDataObject(): ReviewAssignment
{
return app(ReviewAssignment::class);
}
/**
* Check if a review assignment exists
*/
public function exists(int $id, ?int $submissionId): bool
{
return DB::table($this->table)
->where($this->primaryKeyColumn, $id)
->when($submissionId !== null, fn (Builder $query) => $query->where('submission_id', $submissionId))
->exists();
}
/**
* Get a review assignment
*/
public function get(int $id, ?int $submissionId = null): ?ReviewAssignment
{
$row = DB::table($this->table)
->where($this->primaryKeyColumn, $id)
->when($submissionId !== null, fn (Builder $query) => $query->where('submission_id', $submissionId))
->first();
return $row ? $this->fromRow($row) : null;
}
/**
* Get the number of review assignments matching the configured query
*/
public function getCount(Collector $query): int
{
return $query
->getQueryBuilder()
->get('ra.' . $this->primaryKeyColumn)
->count();
}
/**
* Get a list of ids matching the configured query
*
* @return Collection<int,int>
*/
public function getIds(Collector $query): Collection
{
return $query
->getQueryBuilder()
->pluck('ra.' . $this->primaryKeyColumn);
}
/**
* Get a collection of review assignments matching the configured query
*
* @return LazyCollection<int,T>
*/
public function getMany(Collector $query): LazyCollection
{
$rows = $query
->getQueryBuilder()
->get();
return LazyCollection::make(function () use ($rows) {
foreach ($rows as $row) {
yield $row->review_id => $this->fromRow($row);
}
});
}
/**
* @copydoc EntityDAO::fromRow()
*/
public function fromRow(object $row): ReviewAssignment
{
$reviewAssignment = parent::fromRow($row);
$reviewAssignment->setData(
'reviewerFullName',
Repo::user()->get($reviewAssignment->getReviewerId())->getFullName()
);
return $reviewAssignment;
}
/**
* @copydoc EntityDAO::insert()
*/
public function insert(ReviewAssignment $reviewAssignment): int
{
return parent::_insert($reviewAssignment);
}
/**
* @copydoc EntityDAO::update()
*/
public function update(ReviewAssignment $reviewAssignment)
{
parent::_update($reviewAssignment);
}
/**
* @copydoc EntityDAO::delete()
*/
public function delete(ReviewAssignment $reviewAssignment)
{
parent::_delete($reviewAssignment);
}
/**
* Get IDs of the reviewers that have completed a reivew for the given context in the given year.
*
* @return Collection<int,int>
*/
public function getCompletedByYear(int $contextId, string $year): Collection
{
return DB::table($this->table)
->whereIn(
'submission_id',
fn (Builder $q) => $q
->select('s.submission_id')
->from('submissions as s')
->where('s.context_id', $contextId)
)
->whereYear('date_completed', $year)
->pluck('reviewer_id');
}
}