Open Monograph Press  1.1
 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 &getReviewForm($reviewFormId, $assocType = null, $assocId = null) {
37  $params = array((int) $reviewFormId);
38  if ($assocType !== null && $assocId !== null) {
39  $params[] = (int) $assocType;
40  $params[] = (int) $assocId;
41  } else {
42  $assocType = $assocId = null;
43  }
44 
45  $result = $this->retrieve (
46  'SELECT rf.*
47  FROM review_forms rf
48  WHERE rf.review_form_id = ? ' . (($assocType !== null) ? 'AND rf.assoc_type = ? AND rf.assoc_id = ?' : ''),
49  $params
50  );
51 
52  $returner = null;
53  if ($result->RecordCount() != 0) {
54  $returner =& $this->_returnReviewFormFromRow($result->GetRowAssoc(false));
55  }
56 
57  $result->Close();
58  return $returner;
59  }
60 
68  function getUseCounts($assocType, $assocId, $completion = null) {
69  $params = array((int) $assocType, (int) $assocId);
70 
71  $result = $this->retrieve (
72  'SELECT rf.review_form_id AS review_form_id,
73  count(ra.review_form_id) AS rf_count
74  FROM review_forms rf
75  LEFT JOIN review_assignments ra ON (
76  ra.review_form_id = rf.review_form_id' .
77  ($completion === true?' AND ra.date_confirmed IS NOT NULL':'') .
78  ($completion === false?' AND ra.date_notified IS NOT NULL AND ra.date_confirmed IS NULL':'') . '
79  )
80  WHERE rf.assoc_type = ? AND rf.assoc_id = ?
81  GROUP BY rf.review_form_id',
82  $params
83  );
84 
85  $returner = array();
86  while (!$result->EOF) {
87  $row = $result->getRowAssoc(false);
88  $returner[$row['review_form_id']] = $row['rf_count'];
89  $result->MoveNext();
90  }
91  $result->Close();
92  return $returner;
93  }
94 
99  function newDataObject() {
100  return new ReviewForm();
101  }
102 
108  function &_returnReviewFormFromRow($row) {
109  $reviewForm = $this->newDataObject();
110  $reviewForm->setId($row['review_form_id']);
111  $reviewForm->setAssocType($row['assoc_type']);
112  $reviewForm->setAssocId($row['assoc_id']);
113  $reviewForm->setSequence($row['seq']);
114  $reviewForm->setActive($row['is_active']);
115 
116  $this->getDataObjectSettings('review_form_settings', 'review_form_id', $row['review_form_id'], $reviewForm);
117 
118  HookRegistry::call('ReviewFormDAO::_returnReviewFormFromRow', array(&$reviewForm, &$row));
119 
120  return $reviewForm;
121  }
122 
130  function reviewFormExists($reviewFormId, $assocType, $assocId) {
131  $result = $this->retrieve(
132  'SELECT COUNT(*) FROM review_forms WHERE review_form_id = ? AND assoc_type = ? AND assoc_id = ?',
133  array($reviewFormId, $assocType, $assocId)
134  );
135  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
136 
137  $result->Close();
138  return $returner;
139  }
140 
145  function getLocaleFieldNames() {
146  return array('title', 'description');
147  }
148 
153  function updateLocaleFields(&$reviewForm) {
154  $this->updateDataObjectSettings('review_form_settings', $reviewForm, array(
155  'review_form_id' => $reviewForm->getId()
156  ));
157  }
158 
163  function insertObject(&$reviewForm) {
164  $this->update(
165  'INSERT INTO review_forms
166  (assoc_type, assoc_id, seq, is_active)
167  VALUES
168  (?, ?, ?, ?)',
169  array(
170  $reviewForm->getAssocType(),
171  $reviewForm->getAssocId(),
172  $reviewForm->getSequence() == null ? 0 : $reviewForm->getSequence(),
173  $reviewForm->getActive() ? 1 : 0
174  )
175  );
176 
177  $reviewForm->setId($this->getInsertId());
178  $this->updateLocaleFields($reviewForm);
179 
180  return $reviewForm->getId();
181  }
182 
187  function updateObject(&$reviewForm) {
188  $returner = $this->update(
189  'UPDATE review_forms
190  SET
191  assoc_type = ?,
192  assoc_id = ?,
193  seq = ?,
194  is_active = ?
195  WHERE review_form_id = ?',
196  array(
197  $reviewForm->getAssocType(),
198  $reviewForm->getAssocId(),
199  $reviewForm->getSequence(),
200  $reviewForm->getActive(),
201  $reviewForm->getId()
202  )
203  );
204 
205  $this->updateLocaleFields($reviewForm);
206 
207  return $returner;
208  }
209 
214  function deleteObject(&$reviewForm) {
215  return $this->deleteById($reviewForm->getId());
216  }
217 
222  function deleteById($reviewFormId) {
223  $reviewFormElementDao = DAORegistry::getDAO('ReviewFormElementDAO');
224  $reviewFormElementDao->deleteByReviewFormId($reviewFormId);
225 
226  $this->update('DELETE FROM review_form_settings WHERE review_form_id = ?', array($reviewFormId));
227  return $this->update('DELETE FROM review_forms WHERE review_form_id = ?', array($reviewFormId));
228  }
229 
235  function deleteByAssoc($assocType, $assocId) {
236  $reviewForms = $this->getByAssocId($assocType, $assocId);
237 
238  while ($reviewForm = $reviewForms->next()) {
239  $this->deleteById($reviewForm->getId());
240  }
241  }
242 
250  function getByAssocId($assocType, $assocId, $rangeInfo = null) {
251  $result = $this->retrieveRange(
252  'SELECT *
253  FROM review_forms
254  WHERE assoc_type = ? AND assoc_id = ?
255  ORDER BY seq',
256  array((int) $assocType, (int) $assocId), $rangeInfo
257  );
258 
259  return new DAOResultFactory($result, $this, '_returnReviewFormFromRow');
260  }
261 
269  function getActiveByAssocId($assocType, $assocId, $rangeInfo = null) {
270  $result = $this->retrieveRange(
271  'SELECT rf.*
272  FROM review_forms rf
273  LEFT JOIN review_assignments rac ON (
274  rac.review_form_id = rf.review_form_id AND
275  rac.date_confirmed IS NOT NULL
276  )
277  LEFT JOIN review_assignments rai ON (
278  rai.review_form_id = rf.review_form_id AND
279  rai.date_notified IS NOT NULL AND
280  rai.date_confirmed IS NULL
281  )
282  WHERE rf.assoc_type = ? AND assoc_id = ? AND rf.is_active = 1
283  GROUP BY rf.assoc_type, rf.assoc_id, rf.review_form_id, rf.seq, rf.is_active
284  ORDER BY rf.seq',
285  array((int) $assocType, (int) $assocId), $rangeInfo
286  );
287 
288  return new DAOResultFactory($result, $this, '_returnReviewFormFromRow');
289  }
290 
298  function getUsedByAssocId($assocType, $assocId, $rangeInfo = null) {
299  $result = $this->retrieveRange(
300  'SELECT rf.*
301  FROM review_forms rf
302  LEFT JOIN review_assignments rac ON (
303  rac.review_form_id = rf.review_form_id AND
304  rac.date_confirmed IS NOT NULL
305  )
306  LEFT JOIN review_assignments rai ON (
307  rai.review_form_id = rf.review_form_id AND
308  rai.date_notified IS NOT NULL AND
309  rai.date_confirmed IS NULL
310  )
311  WHERE rf.assoc_type = ? AND rf.assoc_id = ? AND rf.is_active = 1
312  GROUP BY rf.assoc_type, rf.assoc_id, rf.review_form_id, rf.seq, rf.is_active
313  HAVING COUNT(rac.review_id) > 0 OR COUNT(rai.review_id) > 0
314  ORDER BY rf.seq',
315  array((int) $assocType, (int) $assocId), $rangeInfo
316  );
317 
318  return new DAOResultFactory($result, $this, '_returnReviewFormFromRow');
319  }
320 
328  function getUnusedByAssocId($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 = ?
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 AND 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, '_returnReviewFormFromRow');
349  }
350 
358  function getTitlesByAssocId($assocType, $assocId, $used) {
359  $reviewFormTitles = array();
360 
361  if ($used) {
362  $reviewForms = $this->getUsedByAssocId($assocType, $assocId);
363  } else {
364  $reviewForms = $this->getUnusedByAssocId($assocType, $assocId);
365  }
366  while ($reviewForm = $reviewForms->next()) {
367  $reviewFormTitles[$reviewForm->getId()] = $reviewForm->getLocalizedTitle();
368  }
369 
370  return $reviewFormTitles;
371  }
372 
380  function unusedReviewFormExists($reviewFormId, $assocType = null, $assocId = null) {
381  $params = array((int) $reviewFormId);
382  if ($assocType !== null && $assocId !== null) {
383  $params[] = (int) $assocType;
384  $params[] = (int) $assocId;
385  } else {
386  $assocType = $assocId = null;
387  }
388 
389  $result = $this->retrieve (
390  'SELECT rf.review_form_id,
391  COUNT(rac.review_id) AS complete_count,
392  COUNT(rai.review_id) AS incomplete_count
393  FROM review_forms rf
394  LEFT JOIN review_assignments rac ON (
395  rac.review_form_id = rf.review_form_id AND
396  rac.date_confirmed IS NOT NULL
397  )
398  LEFT JOIN review_assignments rai ON (
399  rai.review_form_id = rf.review_form_id AND
400  rai.date_notified IS NOT NULL AND
401  rai.date_confirmed IS NULL
402  )
403  WHERE rf.review_form_id = ?' . ($assocType !== null ? ' AND rf.assoc_type = ? AND rf.assoc_id = ?':'') . '
404  GROUP BY rf.review_form_id
405  HAVING COUNT(rac.review_id) = 0 AND COUNT(rai.review_id) = 0',
406  $params
407  );
408 
409  $returner = $result->RecordCount() != 0;
410 
411  $result->Close();
412  return $returner;
413  }
414 
420  function resequenceReviewForms($assocType, $assocId) {
421  $result = $this->retrieve(
422  'SELECT review_form_id FROM review_forms WHERE assoc_type = ? AND assoc_id = ? ORDER BY seq',
423  array((int) $assocType, (int) $assocId)
424  );
425 
426  for ($i=1; !$result->EOF; $i++) {
427  list($reviewFormId) = $result->fields;
428  $this->update(
429  'UPDATE review_forms SET seq = ? WHERE review_form_id = ?',
430  array(
431  $i,
432  $reviewFormId
433  )
434  );
435 
436  $result->MoveNext();
437  }
438  $result->Close();
439  }
440 
445  function getInsertId() {
446  return $this->_getInsertId('review_forms', 'review_form_id');
447  }
448 }
449 
450 ?>
static & getDAO($name, $dbconn=null)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
updateObject(&$reviewForm)
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
deleteObject(&$reviewForm)
Basic class describing a review form.
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
getUnusedByAssocId($assocType, $assocId, $rangeInfo=null)
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...
insertObject(&$reviewForm)
reviewFormExists($reviewFormId, $assocType, $assocId)
& _returnReviewFormFromRow($row)
& getReviewForm($reviewFormId, $assocType=null, $assocId=null)
getActiveByAssocId($assocType, $assocId, $rangeInfo=null)
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)