Open Journal Systems  3.3.0
ReviewFormDAO.inc.php
1 <?php
2 
18 import('lib.pkp.classes.reviewForm.ReviewForm');
19 
20 class ReviewFormDAO extends DAO {
21 
29  function getById($reviewFormId, $assocType = null, $assocId = null) {
30  $params = array((int) $reviewFormId);
31  if ($assocType) {
32  $params[] = (int) $assocType;
33  $params[] = (int) $assocId;
34  }
35 
36  $result = $this->retrieve (
37  'SELECT rf.*,
38  SUM(CASE WHEN ra.date_completed IS NOT NULL THEN 1 ELSE 0 END) AS complete_count,
39  SUM(CASE WHEN ra.review_id IS NOT NULL AND ra.date_completed IS NULL THEN 1 ELSE 0 END) AS incomplete_count
40  FROM review_forms rf
41  LEFT JOIN review_assignments ra ON (ra.review_form_id = rf.review_form_id AND ra.declined<>1)
42  WHERE rf.review_form_id = ? AND rf.assoc_type = ? AND rf.assoc_id = ?
43  GROUP BY rf.review_form_id',
44  $params
45  );
46 
47  $returner = null;
48  if ($result->RecordCount() != 0) {
49  $returner = $this->_fromRow($result->GetRowAssoc(false));
50  }
51 
52  $result->Close();
53  return $returner;
54  }
55 
60  function newDataObject() {
61  return new ReviewForm();
62  }
63 
69  function _fromRow($row) {
70  $reviewForm = $this->newDataObject();
71  $reviewForm->setId($row['review_form_id']);
72  $reviewForm->setAssocType($row['assoc_type']);
73  $reviewForm->setAssocId($row['assoc_id']);
74  $reviewForm->setSequence($row['seq']);
75  $reviewForm->setActive($row['is_active']);
76  $reviewForm->setCompleteCount($row['complete_count']);
77  $reviewForm->setIncompleteCount($row['incomplete_count']);
78 
79  $this->getDataObjectSettings('review_form_settings', 'review_form_id', $row['review_form_id'], $reviewForm);
80 
81  HookRegistry::call('ReviewFormDAO::_fromRow', array(&$reviewForm, &$row));
82 
83  return $reviewForm;
84  }
85 
93  function reviewFormExists($reviewFormId, $assocType, $assocId) {
94  $result = $this->retrieve(
95  'SELECT COUNT(*) FROM review_forms WHERE review_form_id = ? AND assoc_type = ? AND assoc_id = ?',
96  array((int) $reviewFormId, (int) $assocType, (int) $assocId)
97  );
98  $returner = isset($result->fields[0]) && $result->fields[0] == 1 ? true : false;
99 
100  $result->Close();
101  return $returner;
102  }
103 
108  function getLocaleFieldNames() {
109  return array('title', 'description');
110  }
111 
116  function updateLocaleFields(&$reviewForm) {
117  $this->updateDataObjectSettings('review_form_settings', $reviewForm, array(
118  'review_form_id' => $reviewForm->getId()
119  ));
120  }
121 
126  function insertObject($reviewForm) {
127  $this->update(
128  'INSERT INTO review_forms
129  (assoc_type, assoc_id, seq, is_active)
130  VALUES
131  (?, ?, ?, ?)',
132  array(
133  (int) $reviewForm->getAssocType(),
134  (int) $reviewForm->getAssocId(),
135  $reviewForm->getSequence() == null ? 0 : (float) $reviewForm->getSequence(),
136  $reviewForm->getActive()?1:0
137  )
138  );
139 
140  $reviewForm->setId($this->getInsertId());
141  $this->updateLocaleFields($reviewForm);
142 
143  return $reviewForm->getId();
144  }
145 
150  function updateObject($reviewForm) {
151  $returner = $this->update(
152  'UPDATE review_forms
153  SET
154  assoc_type = ?,
155  assoc_id = ?,
156  seq = ?,
157  is_active = ?
158  WHERE review_form_id = ?',
159  array(
160  (int) $reviewForm->getAssocType(),
161  (int) $reviewForm->getAssocId(),
162  (float) $reviewForm->getSequence(),
163  $reviewForm->getActive()?1:0,
164  (int) $reviewForm->getId()
165  )
166  );
167 
168  $this->updateLocaleFields($reviewForm);
169 
170  return $returner;
171  }
172 
177  function deleteObject($reviewForm) {
178  return $this->deleteById($reviewForm->getId());
179  }
180 
185  function deleteById($reviewFormId) {
186  $reviewFormElementDao = DAORegistry::getDAO('ReviewFormElementDAO'); /* @var $reviewFormElementDao ReviewFormElementDAO */
187  $reviewFormElementDao->deleteByReviewFormId($reviewFormId);
188 
189  $this->update('DELETE FROM review_form_settings WHERE review_form_id = ?', (int) $reviewFormId);
190  $this->update('DELETE FROM review_forms WHERE review_form_id = ?', (int) $reviewFormId);
191  }
192 
198  function deleteByAssoc($assocType, $assocId) {
199  $reviewForms = $this->getByAssocId($assocType, $assocId);
200 
201  while ($reviewForm = $reviewForms->next()) {
202  $this->deleteById($reviewForm->getId());
203  }
204  }
205 
213  function getByAssocId($assocType, $assocId, $rangeInfo = null) {
214  $result = $this->retrieveRange(
215  'SELECT rf.*,
216  SUM(CASE WHEN ra.date_completed IS NOT NULL THEN 1 ELSE 0 END) AS complete_count,
217  SUM(CASE WHEN ra.review_id IS NOT NULL AND ra.date_completed IS NULL THEN 1 ELSE 0 END) AS incomplete_count
218  FROM review_forms rf
219  LEFT JOIN review_assignments ra ON (ra.review_form_id = rf.review_form_id AND ra.declined<>1)
220  WHERE rf.assoc_type = ? AND rf.assoc_id = ?
221  GROUP BY rf.review_form_id
222  ORDER BY rf.seq',
223  array((int) $assocType, (int) $assocId), $rangeInfo
224  );
225 
226  return new DAOResultFactory($result, $this, '_fromRow');
227  }
228 
236  function getActiveByAssocId($assocType, $assocId, $rangeInfo = null) {
237  $result = $this->retrieveRange(
238  'SELECT rf.*,
239  SUM(CASE WHEN ra.date_completed IS NOT NULL THEN 1 ELSE 0 END) AS complete_count,
240  SUM(CASE WHEN ra.review_id IS NOT NULL AND ra.date_completed IS NULL THEN 1 ELSE 0 END) AS incomplete_count
241  FROM review_forms rf
242  LEFT JOIN review_assignments ra ON (ra.review_form_id = rf.review_form_id AND ra.declined<>1)
243  WHERE rf.assoc_type = ? AND rf.assoc_id = ? AND rf.is_active = 1
244  GROUP BY rf.review_form_id
245  ORDER BY rf.seq',
246  array((int) $assocType, (int) $assocId), $rangeInfo
247  );
248 
249  return new DAOResultFactory($result, $this, '_fromRow');
250  }
251 
259  function unusedReviewFormExists($reviewFormId, $assocType = null, $assocId = null) {
260  $reviewForm = $this->getById($reviewFormId, $assocType, $assocId);
261  if (!$reviewForm) return false;
262  if ($reviewForm->getCompleteCount()!=0 || $reviewForm->getIncompleteCount()!=0) return false;
263  return true;
264  }
265 
271  function resequenceReviewForms($assocType, $assocId) {
272  $result = $this->retrieve(
273  'SELECT review_form_id FROM review_forms WHERE assoc_type = ? AND assoc_id = ? ORDER BY seq',
274  array((int) $assocType, (int) $assocId)
275  );
276 
277  for ($i=1; !$result->EOF; $i++) {
278  list($reviewFormId) = $result->fields;
279  $this->update(
280  'UPDATE review_forms SET seq = ? WHERE review_form_id = ?',
281  array(
282  $i,
283  $reviewFormId
284  )
285  );
286 
287  $result->MoveNext();
288  }
289  $result->Close();
290  }
291 
296  function getInsertId() {
297  return $this->_getInsertId('review_forms', 'review_form_id');
298  }
299 }
300 
301 
DAOResultFactory
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
Definition: DAOResultFactory.inc.php:21
DAO\retrieveRange
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:176
ReviewFormDAO\_fromRow
_fromRow($row)
Definition: ReviewFormDAO.inc.php:69
ReviewFormDAO\getById
getById($reviewFormId, $assocType=null, $assocId=null)
Definition: ReviewFormDAO.inc.php:29
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
ReviewFormDAO\deleteObject
deleteObject($reviewForm)
Definition: ReviewFormDAO.inc.php:177
ReviewFormDAO\getLocaleFieldNames
getLocaleFieldNames()
Definition: ReviewFormDAO.inc.php:108
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
ReviewFormDAO\updateLocaleFields
updateLocaleFields(&$reviewForm)
Definition: ReviewFormDAO.inc.php:116
ReviewFormDAO\getInsertId
getInsertId()
Definition: ReviewFormDAO.inc.php:296
ReviewFormDAO\resequenceReviewForms
resequenceReviewForms($assocType, $assocId)
Definition: ReviewFormDAO.inc.php:271
ReviewFormDAO\getActiveByAssocId
getActiveByAssocId($assocType, $assocId, $rangeInfo=null)
Definition: ReviewFormDAO.inc.php:236
ReviewFormDAO\getByAssocId
getByAssocId($assocType, $assocId, $rangeInfo=null)
Definition: ReviewFormDAO.inc.php:213
ReviewFormDAO\newDataObject
newDataObject()
Definition: ReviewFormDAO.inc.php:60
ReviewFormDAO\unusedReviewFormExists
unusedReviewFormExists($reviewFormId, $assocType=null, $assocId=null)
Definition: ReviewFormDAO.inc.php:259
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
DAO\_getInsertId
_getInsertId($table='', $id='')
Definition: DAO.inc.php:255
ReviewForm
Basic class describing a review form.
Definition: ReviewForm.inc.php:24
DAO\getDataObjectSettings
getDataObjectSettings($tableName, $idFieldName, $idFieldValue, $dataObject)
Definition: DAO.inc.php:582
ReviewFormDAO
Operations for retrieving and modifying ReviewForm objects.
Definition: ReviewFormDAO.inc.php:20
ReviewFormDAO\updateObject
updateObject($reviewForm)
Definition: ReviewFormDAO.inc.php:150
ReviewFormDAO\deleteById
deleteById($reviewFormId)
Definition: ReviewFormDAO.inc.php:185
DAO\updateDataObjectSettings
updateDataObjectSettings($tableName, $dataObject, $idArray)
Definition: DAO.inc.php:488
ReviewFormDAO\insertObject
insertObject($reviewForm)
Definition: ReviewFormDAO.inc.php:126
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
ReviewFormDAO\reviewFormExists
reviewFormExists($reviewFormId, $assocType, $assocId)
Definition: ReviewFormDAO.inc.php:93
ReviewFormDAO\deleteByAssoc
deleteByAssoc($assocType, $assocId)
Definition: ReviewFormDAO.inc.php:198
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31