Open Monograph Press  3.3.0
QueryDAO.inc.php
1 <?php
2 
18 import('lib.pkp.classes.query.Query');
19 
20 class QueryDAO extends DAO {
21 
29  function getById($queryId, $assocType = null, $assocId = null) {
30  $params = array((int) $queryId);
31  if ($assocType) {
32  $params[] = (int) $assocType;
33  $params[] = (int) $assocId;
34  }
35  $result = $this->retrieve(
36  'SELECT *
37  FROM queries
38  WHERE query_id = ?'
39  . ($assocType?' AND assoc_type = ? AND assoc_id = ?':''),
40  $params
41  );
42 
43  $returner = null;
44  if ($result->RecordCount() != 0) {
45  $returner = $this->_fromRow($result->GetRowAssoc(false));
46  }
47 
48  $result->Close();
49  return $returner;
50  }
51 
60  function getByAssoc($assocType, $assocId, $stageId = null, $userId = null) {
61  $params = array();
62  $params[] = (int) ASSOC_TYPE_QUERY;
63  if ($userId) $params[] = (int) $userId;
64  $params[] = (int) $assocType;
65  $params[] = (int) $assocId;
66  if ($stageId) $params[] = (int) $stageId;
67  if ($userId) $params[] = (int) $userId;
68 
69  return new DAOResultFactory(
70  $this->retrieve(
71  'SELECT DISTINCT q.*
72  FROM queries q
73  LEFT JOIN notes n ON n.assoc_type = ? AND n.assoc_id = q.query_id
74  ' . ($userId?'INNER JOIN query_participants qp ON (q.query_id = qp.query_id AND qp.user_id = ?)':'') . '
75  WHERE q.assoc_type = ? AND q.assoc_id = ?
76  ' . ($stageId?' AND q.stage_id = ?':'') .
77  ($userId?'
78  AND (n.user_id = ? OR n.title IS NOT NULL
79  OR n.contents IS NOT NULL)':'') . '
80  ORDER BY q.seq',
81  $params
82  ),
83  $this, '_fromRow'
84  );
85  }
86 
92  function _fromRow($row) {
93  $query = $this->newDataObject();
94  $query->setId($row['query_id']);
95  $query->setAssocType($row['assoc_type']);
96  $query->setAssocId($row['assoc_id']);
97  $query->setStageId($row['stage_id']);
98  $query->setIsClosed($row['closed']);
99  $query->setSequence($row['seq']);
100 
101  HookRegistry::call('QueryDAO::_fromRow', array(&$query, &$row));
102  return $query;
103  }
104 
109  function newDataObject() {
110  return new Query();
111  }
112 
118  function insertObject($query) {
119  $this->update(
120  'INSERT INTO queries (assoc_type, assoc_id, stage_id, closed, seq)
121  VALUES (?, ?, ?, ?, ?)',
122  array(
123  (int) $query->getAssocType(),
124  (int) $query->getAssocId(),
125  (int) $query->getStageId(),
126  (int) $query->getIsClosed(),
127  (float) $query->getSequence(),
128  )
129  );
130  $query->setId($this->getInsertId());
131  return $query->getId();
132  }
133 
139  function insertParticipant($queryId, $userId) {
140  $this->update(
141  'INSERT INTO query_participants
142  (query_id, user_id)
143  VALUES
144  (?, ?)',
145  array(
146  (int) $queryId,
147  (int) $userId,
148  )
149  );
150  }
151 
157  function removeParticipant($queryId, $userId) {
158  $this->update(
159  'DELETE FROM query_participants WHERE query_id = ? AND user_id = ?',
160  array((int) $queryId, (int) $userId)
161  );
162  }
163 
168  function removeAllParticipants($queryId) {
169  $this->update(
170  'DELETE FROM query_participants WHERE query_id = ?',
171  (int) $queryId
172  );
173  }
174 
181  function getParticipantIds($queryId, $userId = null) {
182  $params = array((int) $queryId);
183  if ($userId) $params[] = (int) $userId;
184  $result = $this->retrieve(
185  'SELECT user_id
186  FROM query_participants
187  WHERE query_id = ?' .
188  ($userId?' AND user_id = ?':''),
189  $params
190  );
191  $userIds = array();
192  while (!$result->EOF) {
193  $row = $result->getRowAssoc(false);
194  $userIds[] = (int) $row['user_id'];
195  $result->MoveNext();
196  }
197  $result->Close();
198  return $userIds;
199  }
200 
205  function updateObject($query) {
206  $this->update(
207  'UPDATE queries
208  SET assoc_type = ?,
209  assoc_id = ?,
210  stage_id = ?,
211  closed = ?,
212  seq = ?
213  WHERE query_id = ?',
214  array(
215  (int) $query->getAssocType(),
216  (int) $query->getAssocId(),
217  (int) $query->getStageId(),
218  (int) $query->getIsClosed(),
219  (float) $query->getSequence(),
220  (int) $query->getId()
221  )
222  );
223  }
224 
229  function deleteObject($query) {
230  $this->deleteById($query->getId());
231  }
232 
239  function deleteById($queryId, $assocType = null, $assocId = null) {
240  $params = array((int) $queryId);
241  if ($assocType) {
242  $params[] = (int) $assocType;
243  $params[] = (int) $assocId;
244  }
245  $this->update(
246  'DELETE FROM queries WHERE query_id = ?' .
247  ($assocType?' AND assoc_type = ? AND assoc_id = ?':''),
248  $params
249  );
250  if ($this->getAffectedRows()) {
251  $this->update('DELETE FROM query_participants WHERE query_id = ?', (int) $queryId);
252 
253  // Remove associated notes
254  $noteDao = DAORegistry::getDAO('NoteDAO'); /* @var $noteDao NoteDAO */
255  $noteDao->deleteByAssoc(ASSOC_TYPE_QUERY, $queryId);
256 
257  // Remove associated notifications
258  $notificationDao = DAORegistry::getDAO('NotificationDAO'); /* @var $notificationDao NotificationDAO */
259  $notifications = $notificationDao->getByAssoc(ASSOC_TYPE_QUERY, $queryId);
260  while ($notification = $notifications->next()) {
261  $notificationDao->deleteObject($notification);
262  }
263 
264  }
265  }
266 
272  function resequence($assocType, $assocId) {
273  $result = $this->retrieve(
274  'SELECT query_id FROM queries WHERE assoc_type = ? AND assoc_id = ? ORDER BY seq',
275  array((int) $assocType, (int) $assocId)
276  );
277 
278  for ($i=1; !$result->EOF; $i++) {
279  list($queryId) = $result->fields;
280  $this->update(
281  'UPDATE queries SET seq = ? WHERE query_id = ?',
282  array(
283  $i,
284  $queryId
285  )
286  );
287 
288  $result->MoveNext();
289  }
290  $result->Close();
291  }
292 
297  function getInsertId() {
298  return $this->_getInsertId('queries', 'query_id');
299  }
300 
306  function deleteByAssoc($assocType, $assocId) {
307  $queries = $this->getByAssoc($assocType, $assocId);
308  while ($query = $queries->next()) {
309  $this->deleteObject($query);
310  }
311  }
312 }
313 
314 
QueryDAO\getByAssoc
getByAssoc($assocType, $assocId, $stageId=null, $userId=null)
Definition: QueryDAO.inc.php:60
QueryDAO\newDataObject
newDataObject()
Definition: QueryDAO.inc.php:109
QueryDAO\insertObject
insertObject($query)
Definition: QueryDAO.inc.php:118
DAOResultFactory
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
Definition: DAOResultFactory.inc.php:21
QueryDAO\getById
getById($queryId, $assocType=null, $assocId=null)
Definition: QueryDAO.inc.php:29
QueryDAO\deleteById
deleteById($queryId, $assocType=null, $assocId=null)
Definition: QueryDAO.inc.php:239
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
QueryDAO\insertParticipant
insertParticipant($queryId, $userId)
Definition: QueryDAO.inc.php:139
DAO\getAffectedRows
getAffectedRows()
Definition: DAO.inc.php:264
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
QueryDAO\getParticipantIds
getParticipantIds($queryId, $userId=null)
Definition: QueryDAO.inc.php:181
QueryDAO\removeAllParticipants
removeAllParticipants($queryId)
Definition: QueryDAO.inc.php:168
QueryDAO\getInsertId
getInsertId()
Definition: QueryDAO.inc.php:297
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
QueryDAO\deleteObject
deleteObject($query)
Definition: QueryDAO.inc.php:229
QueryDAO\_fromRow
_fromRow($row)
Definition: QueryDAO.inc.php:92
QueryDAO\resequence
resequence($assocType, $assocId)
Definition: QueryDAO.inc.php:272
QueryDAO\deleteByAssoc
deleteByAssoc($assocType, $assocId)
Definition: QueryDAO.inc.php:306
Query
Class for Query.
Definition: Query.inc.php:19
QueryDAO
Operations for retrieving and modifying Query objects.
Definition: QueryDAO.inc.php:20
HookRegistry\call
static call($hookName, $args=null)
Definition: HookRegistry.inc.php:86
QueryDAO\updateObject
updateObject($query)
Definition: QueryDAO.inc.php:205
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31
QueryDAO\removeParticipant
removeParticipant($queryId, $userId)
Definition: QueryDAO.inc.php:157