Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
ReviewFormDAO.inc.php
1 <?php
2 
18 import('lib.pkp.classes.reviewForm.ReviewForm');
19 
20 class ReviewFormDAO extends DAO {
21 
25  function ReviewFormDAO() {
26  parent::DAO();
27  }
28 
36  function getById($reviewFormId, $assocType = null, $assocId = null) {
37  $params = array((int) $reviewFormId);
38  if ($assocType) {
39  $params[] = (int) $assocType;
40  $params[] = (int) $assocId;
41  }
42 
43  $result = $this->retrieve (
44  'SELECT rf.*,
45  COUNT(rac.review_id) AS complete_count,
46  COUNT(rai.review_id) AS incomplete_count
47  FROM review_forms rf
48  LEFT JOIN review_assignments rac ON (
49  rac.review_form_id = rf.review_form_id AND
50  rac.date_confirmed IS NOT NULL
51  )
52  LEFT JOIN review_assignments rai ON (
53  rai.review_form_id = rf.review_form_id AND
54  rai.date_notified IS NOT NULL AND
55  rai.date_confirmed IS NULL
56  )
57  WHERE rf.review_form_id = ? AND rf.assoc_type = ? AND rf.assoc_id = ?
58  GROUP BY rf.assoc_type, rf.assoc_id, rf.review_form_id, rf.seq, rf.is_active',
59  $params
60  );
61 
62  $returner = null;
63  if ($result->RecordCount() != 0) {
64  $returner = $this->_fromRow($result->GetRowAssoc(false));
65  }
66 
67  $result->Close();
68  return $returner;
69  }
70 
78  function getUseCounts($assocType, $assocId, $completion = null) {
79  $params = array((int) $assocType, (int) $assocId);
80 
81  $result = $this->retrieve (
82  'SELECT rf.review_form_id AS review_form_id,
83  count(ra.review_form_id) AS rf_count
84  FROM review_forms rf
85  LEFT JOIN review_assignments ra ON (
86  ra.review_form_id = rf.review_form_id' .
87  ($completion === true?' AND ra.date_confirmed IS NOT NULL':'') .
88  ($completion === false?' AND ra.date_notified IS NOT NULL AND ra.date_confirmed IS NULL':'') . '
89  )
90  WHERE rf.assoc_type = ? AND rf.assoc_id = ?
91  GROUP BY rf.review_form_id',
92  $params
93  );
94 
95  $returner = array();
96  while (!$result->EOF) {
97  $row = $result->getRowAssoc(false);
98  $returner[$row['review_form_id']] = $row['rf_count'];
99  $result->MoveNext();
100  }
101  $result->Close();
102  return $returner;
103  }
104 
109  function newDataObject() {
110  return new ReviewForm();
111  }
112 
118  function _fromRow($row) {
119  $reviewForm = $this->newDataObject();
120  $reviewForm->setId($row['review_form_id']);
121  $reviewForm->setAssocType($row['assoc_type']);
122  $reviewForm->setAssocId($row['assoc_id']);
123  $reviewForm->setSequence($row['seq']);
124  $reviewForm->setActive($row['is_active']);
125  $reviewForm->setCompleteCount($row['complete_count']);
126  $reviewForm->setIncompleteCount($row['incomplete_count']);
127 
128  $this->getDataObjectSettings('review_form_settings', 'review_form_id', $row['review_form_id'], $reviewForm);
129 
130  HookRegistry::call('ReviewFormDAO::_fromRow', array(&$reviewForm, &$row));
131 
132  return $reviewForm;
133  }
134 
142  function reviewFormExists($reviewFormId, $assocType, $assocId) {
143  $result = $this->retrieve(
144  'SELECT COUNT(*) FROM review_forms WHERE review_form_id = ? AND assoc_type = ? AND assoc_id = ?',
145  array((int) $reviewFormId, (int) $assocType, (int) $assocId)
146  );
147  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
148 
149  $result->Close();
150  return $returner;
151  }
152 
157  function getLocaleFieldNames() {
158  return array('title', 'description');
159  }
160 
165  function updateLocaleFields(&$reviewForm) {
166  $this->updateDataObjectSettings('review_form_settings', $reviewForm, array(
167  'review_form_id' => $reviewForm->getId()
168  ));
169  }
170 
175  function insertObject($reviewForm) {
176  $this->update(
177  'INSERT INTO review_forms
178  (assoc_type, assoc_id, seq, is_active)
179  VALUES
180  (?, ?, ?, ?)',
181  array(
182  (int) $reviewForm->getAssocType(),
183  (int) $reviewForm->getAssocId(),
184  $reviewForm->getSequence() == null ? 0 : (float) $reviewForm->getSequence(),
185  $reviewForm->getActive()?1:0
186  )
187  );
188 
189  $reviewForm->setId($this->getInsertId());
190  $this->updateLocaleFields($reviewForm);
191 
192  return $reviewForm->getId();
193  }
194 
199  function updateObject($reviewForm) {
200  $returner = $this->update(
201  'UPDATE review_forms
202  SET
203  assoc_type = ?,
204  assoc_id = ?,
205  seq = ?,
206  is_active = ?
207  WHERE review_form_id = ?',
208  array(
209  (int) $reviewForm->getAssocType(),
210  (int) $reviewForm->getAssocId(),
211  (float) $reviewForm->getSequence(),
212  $reviewForm->getActive()?1:0,
213  (int) $reviewForm->getId()
214  )
215  );
216 
217  $this->updateLocaleFields($reviewForm);
218 
219  return $returner;
220  }
221 
226  function deleteObject($reviewForm) {
227  return $this->deleteById($reviewForm->getId());
228  }
229 
234  function deleteById($reviewFormId) {
235  $reviewFormElementDao = DAORegistry::getDAO('ReviewFormElementDAO');
236  $reviewFormElementDao->deleteByReviewFormId($reviewFormId);
237 
238  $this->update('DELETE FROM review_form_settings WHERE review_form_id = ?', (int) $reviewFormId);
239  $this->update('DELETE FROM review_forms WHERE review_form_id = ?', (int) $reviewFormId);
240  }
241 
247  function deleteByAssoc($assocType, $assocId) {
248  $reviewForms = $this->getByAssocId($assocType, $assocId);
249 
250  while ($reviewForm = $reviewForms->next()) {
251  $this->deleteById($reviewForm->getId());
252  }
253  }
254 
262  function getByAssocId($assocType, $assocId, $rangeInfo = null) {
263  $result = $this->retrieveRange(
264  'SELECT rf.review_form_id,
265  rf.assoc_type,
266  rf.assoc_id,
267  rf.seq,
268  rf.is_active,
269  COUNT(rac.review_id) AS complete_count,
270  COUNT(rai.review_id) AS incomplete_count
271  FROM review_forms rf
272  LEFT JOIN review_assignments rac ON (
273  rac.review_form_id = rf.review_form_id AND
274  rac.date_confirmed IS NOT NULL
275  )
276  LEFT JOIN review_assignments rai ON (
277  rai.review_form_id = rf.review_form_id AND
278  rai.date_notified IS NOT NULL AND
279  rai.date_confirmed IS NULL
280  )
281  WHERE rf.assoc_type = ? AND rf.assoc_id = ?
282  GROUP BY rf.assoc_type, rf.assoc_id, rf.review_form_id, rf.seq, rf.is_active
283  ORDER BY rf.seq',
284  array((int) $assocType, (int) $assocId), $rangeInfo
285  );
286 
287  return new DAOResultFactory($result, $this, '_fromRow');
288  }
289 
297  function getActiveByAssocId($assocType, $assocId, $rangeInfo = null) {
298  $result = $this->retrieveRange(
299  'SELECT rf.*,
300  COUNT(rac.review_id) AS complete_count,
301  COUNT(rai.review_id) AS incomplete_count
302  FROM review_forms rf
303  LEFT JOIN review_assignments rac ON (
304  rac.review_form_id = rf.review_form_id AND
305  rac.date_confirmed IS NOT NULL
306  )
307  LEFT JOIN review_assignments rai ON (
308  rai.review_form_id = rf.review_form_id AND
309  rai.date_notified IS NOT NULL AND
310  rai.date_confirmed IS NULL
311  )
312  WHERE rf.assoc_type = ? AND rf.assoc_id = ? AND rf.is_active = 1
313  GROUP BY rf.assoc_type, rf.assoc_id, rf.review_form_id, rf.seq, rf.is_active
314  ORDER BY rf.seq',
315  array((int) $assocType, (int) $assocId), $rangeInfo
316  );
317 
318  return new DAOResultFactory($result, $this, '_fromRow');
319  }
320 
328  function getUsedByAssocId($assocType, $assocId, $rangeInfo = null) {
329  $result = $this->retrieveRange(
330  'SELECT rf.*
331  FROM review_forms rf
332  LEFT JOIN review_assignments rac ON (
333  rac.review_form_id = rf.review_form_id AND
334  rac.date_confirmed IS NOT NULL
335  )
336  LEFT JOIN review_assignments rai ON (
337  rai.review_form_id = rf.review_form_id AND
338  rai.date_notified IS NOT NULL AND
339  rai.date_confirmed IS NULL
340  )
341  WHERE rf.assoc_type = ? AND rf.assoc_id = ? AND rf.is_active = 1
342  GROUP BY rf.assoc_type, rf.assoc_id, rf.review_form_id, rf.seq, rf.is_active
343  HAVING COUNT(rac.review_id) > 0 OR COUNT(rai.review_id) > 0
344  ORDER BY rf.seq',
345  array((int) $assocType, (int) $assocId), $rangeInfo
346  );
347 
348  return new DAOResultFactory($result, $this, '_fromRow');
349  }
350 
358  function getUnusedByAssocId($assocType, $assocId, $rangeInfo = null) {
359  $result = $this->retrieveRange(
360  'SELECT rf.*
361  FROM review_forms rf
362  LEFT JOIN review_assignments rac ON (
363  rac.review_form_id = rf.review_form_id AND
364  rac.date_confirmed IS NOT NULL
365  )
366  LEFT JOIN review_assignments rai ON (
367  rai.review_form_id = rf.review_form_id AND
368  rai.date_notified IS NOT NULL AND
369  rai.date_confirmed IS NULL
370  )
371  WHERE rf.assoc_type = ? AND rf.assoc_id = ?
372  GROUP BY rf.assoc_type, rf.assoc_id, rf.review_form_id, rf.seq, rf.is_active
373  HAVING COUNT(rac.review_id) = 0 AND COUNT(rai.review_id) = 0
374  ORDER BY rf.seq',
375  array((int) $assocType, (int) $assocId), $rangeInfo
376  );
377 
378  return new DAOResultFactory($result, $this, '_fromRow');
379  }
380 
388  function getTitlesByAssocId($assocType, $assocId, $used) {
389  $reviewFormTitles = array();
390 
391  if ($used) {
392  $reviewForms = $this->getUsedByAssocId($assocType, $assocId);
393  } else {
394  $reviewForms = $this->getUnusedByAssocId($assocType, $assocId);
395  }
396  while ($reviewForm = $reviewForms->next()) {
397  $reviewFormTitles[$reviewForm->getId()] = $reviewForm->getLocalizedTitle();
398  }
399 
400  return $reviewFormTitles;
401  }
402 
410  function unusedReviewFormExists($reviewFormId, $assocType = null, $assocId = null) {
411  $params = array((int) $reviewFormId);
412  if ($assocType) {
413  $params[] = (int) $assocType;
414  $params[] = (int) $assocId;
415  }
416 
417  $result = $this->retrieve (
418  'SELECT rf.review_form_id,
419  COUNT(rac.review_id) AS complete_count,
420  COUNT(rai.review_id) AS incomplete_count
421  FROM review_forms rf
422  LEFT JOIN review_assignments rac ON (
423  rac.review_form_id = rf.review_form_id AND
424  rac.date_confirmed IS NOT NULL
425  )
426  LEFT JOIN review_assignments rai ON (
427  rai.review_form_id = rf.review_form_id AND
428  rai.date_notified IS NOT NULL AND
429  rai.date_confirmed IS NULL
430  )
431  WHERE rf.review_form_id = ?' . ($assocType?' AND rf.assoc_type = ? AND rf.assoc_id = ?':'') . '
432  GROUP BY rf.review_form_id
433  HAVING COUNT(rac.review_id) = 0 AND COUNT(rai.review_id) = 0',
434  $params
435  );
436 
437  $returner = $result->RecordCount() != 0;
438 
439  $result->Close();
440  return $returner;
441  }
442 
448  function resequenceReviewForms($assocType, $assocId) {
449  $result = $this->retrieve(
450  'SELECT review_form_id FROM review_forms WHERE assoc_type = ? AND assoc_id = ? ORDER BY seq',
451  array((int) $assocType, (int) $assocId)
452  );
453 
454  for ($i=1; !$result->EOF; $i++) {
455  list($reviewFormId) = $result->fields;
456  $this->update(
457  'UPDATE review_forms SET seq = ? WHERE review_form_id = ?',
458  array(
459  $i,
460  $reviewFormId
461  )
462  );
463 
464  $result->MoveNext();
465  }
466  $result->Close();
467  }
468 
473  function getInsertId() {
474  return $this->_getInsertId('review_forms', 'review_form_id');
475  }
476 }
477 
478 ?>
static & getDAO($name, $dbconn=null)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
unusedReviewFormExists($reviewFormId, $assocType=null, $assocId=null)
getUsedByAssocId($assocType, $assocId, $rangeInfo=null)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
getByAssocId($assocType, $assocId, $rangeInfo=null)
updateDataObjectSettings($tableName, &$dataObject, $idArray)
Definition: DAO.inc.php:457
deleteByAssoc($assocType, $assocId)
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:174
Basic class describing a review form.
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
getUnusedByAssocId($assocType, $assocId, $rangeInfo=null)
insertObject($reviewForm)
static call($hookName, $args=null)
Operations for retrieving and modifying ReviewForm objects.
updateLocaleFields(&$reviewForm)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
getById($reviewFormId, $assocType=null, $assocId=null)
reviewFormExists($reviewFormId, $assocType, $assocId)
updateObject($reviewForm)
getActiveByAssocId($assocType, $assocId, $rangeInfo=null)
deleteObject($reviewForm)
resequenceReviewForms($assocType, $assocId)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
deleteById($reviewFormId)
getUseCounts($assocType, $assocId, $completion=null)
getTitlesByAssocId($assocType, $assocId, $used)