/
ReviewFormDAO.inc.php
304 lines (267 loc) · 8.61 KB
/
ReviewFormDAO.inc.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/**
* @file classes/reviewForm/ReviewFormDAO.inc.php
*
* Copyright (c) 2014-2018 Simon Fraser University
* Copyright (c) 2000-2018 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class ReviewFormDAO
* @ingroup reviewForm
* @see ReviewerForm
*
* @brief Operations for retrieving and modifying ReviewForm objects.
*
*/
import('lib.pkp.classes.reviewForm.ReviewForm');
class ReviewFormDAO extends DAO {
/**
* Retrieve a review form by ID.
* @param $reviewFormId int
* @param $assocType int optional
* @param $assocId int optional
* @return ReviewForm
*/
function getById($reviewFormId, $assocType = null, $assocId = null) {
$params = array((int) $reviewFormId);
if ($assocType) {
$params[] = (int) $assocType;
$params[] = (int) $assocId;
}
$isSqlServer = Config::getVar('database', 'ms_sql');
$result = $this->retrieve (
'SELECT rf.*,
SUM(CASE WHEN ra.date_completed IS NOT NULL THEN 1 ELSE 0 END) AS complete_count,
SUM(CASE WHEN ra.review_id IS NOT NULL AND ra.date_completed IS NULL THEN 1 ELSE 0 END) AS incomplete_count
FROM review_forms rf
LEFT JOIN review_assignments ra ON (ra.review_form_id = rf.review_form_id)
WHERE rf.review_form_id = ? AND rf.assoc_type = ? AND rf.assoc_id = ?
GROUP BY rf.review_form_id' . ($isSqlServer ? ', rf.seq, rf.assoc_type, rf.assoc_id, rf.is_active' : ''),
$params
);
$returner = null;
if ($result->RecordCount() != 0) {
$returner = $this->_fromRow($result->GetRowAssoc(false));
}
$result->Close();
return $returner;
}
/**
* Construct a new data object corresponding to this DAO.
* @return ReviewForm
*/
function newDataObject() {
return new ReviewForm();
}
/**
* Internal function to return a ReviewForm object from a row.
* @param $row array
* @return ReviewForm
*/
function _fromRow($row) {
$reviewForm = $this->newDataObject();
$reviewForm->setId($row['review_form_id']);
$reviewForm->setAssocType($row['assoc_type']);
$reviewForm->setAssocId($row['assoc_id']);
$reviewForm->setSequence($row['seq']);
$reviewForm->setActive($row['is_active']);
$reviewForm->setCompleteCount($row['complete_count']);
$reviewForm->setIncompleteCount($row['incomplete_count']);
$this->getDataObjectSettings('review_form_settings', 'review_form_id', $row['review_form_id'], $reviewForm);
HookRegistry::call('ReviewFormDAO::_fromRow', array(&$reviewForm, &$row));
return $reviewForm;
}
/**
* Check if a review form exists with the specified ID.
* @param $reviewFormId int
* @param $assocType int
* @param $assocId int
* @return boolean
*/
function reviewFormExists($reviewFormId, $assocType, $assocId) {
$result = $this->retrieve(
'SELECT COUNT(*) FROM review_forms WHERE review_form_id = ? AND assoc_type = ? AND assoc_id = ?',
array((int) $reviewFormId, (int) $assocType, (int) $assocId)
);
$returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
$result->Close();
return $returner;
}
/**
* Get the list of fields for which data can be localized.
* @return array
*/
function getLocaleFieldNames() {
return array('title', 'description');
}
/**
* Update the localized fields for this table
* @param $reviewForm object
*/
function updateLocaleFields(&$reviewForm) {
$this->updateDataObjectSettings('review_form_settings', $reviewForm, array(
'review_form_id' => $reviewForm->getId()
));
}
/**
* Insert a new review form.
* @param $reviewForm ReviewForm
*/
function insertObject($reviewForm) {
$this->update(
'INSERT INTO review_forms
(assoc_type, assoc_id, seq, is_active)
VALUES
(?, ?, ?, ?)',
array(
(int) $reviewForm->getAssocType(),
(int) $reviewForm->getAssocId(),
$reviewForm->getSequence() == null ? 0 : (float) $reviewForm->getSequence(),
$reviewForm->getActive()?1:0
)
);
$reviewForm->setId($this->getInsertId());
$this->updateLocaleFields($reviewForm);
return $reviewForm->getId();
}
/**
* Update an existing review form.
* @param $reviewForm ReviewForm
*/
function updateObject($reviewForm) {
$returner = $this->update(
'UPDATE review_forms
SET
assoc_type = ?,
assoc_id = ?,
seq = ?,
is_active = ?
WHERE review_form_id = ?',
array(
(int) $reviewForm->getAssocType(),
(int) $reviewForm->getAssocId(),
(float) $reviewForm->getSequence(),
$reviewForm->getActive()?1:0,
(int) $reviewForm->getId()
)
);
$this->updateLocaleFields($reviewForm);
return $returner;
}
/**
* Delete a review form.
* @param $reviewForm ReviewForm
*/
function deleteObject($reviewForm) {
return $this->deleteById($reviewForm->getId());
}
/**
* Delete a review form by Id.
* @param $reviewFormId int
*/
function deleteById($reviewFormId) {
$reviewFormElementDao = DAORegistry::getDAO('ReviewFormElementDAO');
$reviewFormElementDao->deleteByReviewFormId($reviewFormId);
$this->update('DELETE FROM review_form_settings WHERE review_form_id = ?', (int) $reviewFormId);
$this->update('DELETE FROM review_forms WHERE review_form_id = ?', (int) $reviewFormId);
}
/**
* Delete all review forms by assoc Id.
* @param $assocType int
* @param $assocId int
*/
function deleteByAssoc($assocType, $assocId) {
$reviewForms = $this->getByAssocId($assocType, $assocId);
while ($reviewForm = $reviewForms->next()) {
$this->deleteById($reviewForm->getId());
}
}
/**
* Get all review forms by assoc id.
* @param $assocType int
* @param $assocId int
* @param $rangeInfo RangeInfo (optional)
* @return DAOResultFactory containing matching ReviewForms
*/
function getByAssocId($assocType, $assocId, $rangeInfo = null) {
$isSqlServer = Config::getVar('database', 'ms_sql');
$result = $this->retrieveRange(
'SELECT rf.*,
SUM(CASE WHEN ra.date_completed IS NOT NULL THEN 1 ELSE 0 END) AS complete_count,
SUM(CASE WHEN ra.review_id IS NOT NULL AND ra.date_completed IS NULL THEN 1 ELSE 0 END) AS incomplete_count
FROM review_forms rf
LEFT JOIN review_assignments ra ON (ra.review_form_id = rf.review_form_id)
WHERE rf.assoc_type = ? AND rf.assoc_id = ?
GROUP BY rf.review_form_id' . ($isSqlServer ? ', rf.seq, rf.assoc_type, rf.assoc_id, rf.is_active' : '') . '
ORDER BY rf.seq',
array((int) $assocType, (int) $assocId), $rangeInfo
);
return new DAOResultFactory($result, $this, '_fromRow');
}
/**
* Get active review forms for an associated object.
* @param $assocType int
* @param $assocId int
* @param $rangeInfo object RangeInfo object (optional)
* @return DAOResultFactory containing matching ReviewForms
*/
function getActiveByAssocId($assocType, $assocId, $rangeInfo = null) {
$isSqlServer = Config::getVar('database', 'ms_sql');
$result = $this->retrieveRange(
'SELECT rf.*,
SUM(CASE WHEN ra.date_completed IS NOT NULL THEN 1 ELSE 0 END) AS complete_count,
SUM(CASE WHEN ra.review_id IS NOT NULL AND ra.date_completed IS NULL THEN 1 ELSE 0 END) AS incomplete_count
FROM review_forms rf
LEFT JOIN review_assignments ra ON (ra.review_form_id = rf.review_form_id)
WHERE rf.assoc_type = ? AND rf.assoc_id = ? AND rf.is_active = 1
GROUP BY rf.review_form_id' . ($isSqlServer ? ', rf.seq, rf.assoc_type, rf.assoc_id, rf.is_active' : '') . '
ORDER BY rf.seq',
array((int) $assocType, (int) $assocId), $rangeInfo
);
return new DAOResultFactory($result, $this, '_fromRow');
}
/**
* Check if a review form exists with the specified ID.
* @param $reviewFormId int
* @param $assocType int optional
* @param $assocId int optional
* @return boolean
*/
function unusedReviewFormExists($reviewFormId, $assocType = null, $assocId = null) {
$reviewForm = $this->getById($reviewFormId, $assocType, $assocId);
if (!$reviewForm) return false;
if ($reviewForm->getCompleteCount()!=0 || $reviewForm->getIncompleteCount()!=0) return false;
return true;
}
/**
* Sequentially renumber review form in their sequence order.
* @param $assocType int
* @param $assocId int
*/
function resequenceReviewForms($assocType, $assocId) {
$result = $this->retrieve(
'SELECT review_form_id FROM review_forms WHERE assoc_type = ? AND assoc_id = ? ORDER BY seq',
array((int) $assocType, (int) $assocId)
);
for ($i=1; !$result->EOF; $i++) {
list($reviewFormId) = $result->fields;
$this->update(
'UPDATE review_forms SET seq = ? WHERE review_form_id = ?',
array(
$i,
$reviewFormId
)
);
$result->MoveNext();
}
$result->Close();
}
/**
* Get the ID of the last inserted review form.
* @return int
*/
function getInsertId() {
return $this->_getInsertId('review_forms', 'review_form_id');
}
}
?>