Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
StageAssignmentDAO.inc.php
1 <?php
2 
17 import('lib.pkp.classes.stageAssignment.StageAssignment');
18 
19 class StageAssignmentDAO extends DAO {
23  function StageAssignmentDAO() {
24  parent::DAO();
25  }
26 
32  function getById($stageAssignmentId) {
33  $result = $this->retrieve(
35  . 'WHERE stage_assignment_id = ?',
36  (int) $stageAssignmentId
37  );
38  return $this->_fromRow($result->GetRowAssoc(false));
39  }
40 
49  function getBySubmissionAndStageId($submissionId, $stageId = null, $userGroupId = null, $userId = null) {
50  return $this->_getByIds($submissionId, $stageId, $userGroupId, $userId);
51  }
52 
61  function getBySubmissionAndRoleId($submissionId, $roleId, $stageId = null, $userId = null) {
62  return $this->_getByIds($submissionId, $stageId, null, $userId, $roleId);
63  }
64 
69  function getByUserId($userId) {
70  return $this->_getByIds(null, null, null, $userId);
71  }
72 
79  function getEditorsAssignedToStage($submissionId, $stageId) {
80  $managerAssignmentFactory = $this->getBySubmissionAndRoleId($submissionId, ROLE_ID_MANAGER, $stageId);
81  $subEditorAssignmentFactory = $this->getBySubmissionAndRoleId($submissionId, ROLE_ID_SUB_EDITOR, $stageId);
82  return array_merge($managerAssignmentFactory->toArray(), $subEditorAssignmentFactory->toArray());
83  }
84 
93  function editorAssignedToStage($submissionId, $stageId = null) {
94  $params = array((int) $submissionId, ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR);
95  if ($stageId) $params[] = (int) $stageId;
96  $result = $this->retrieve(
97  'SELECT COUNT(*)
98  FROM stage_assignments sa
99  JOIN user_groups ug ON (sa.user_group_id = ug.user_group_id)
100  JOIN user_group_stage ugs ON (ug.user_group_id = ugs.user_group_id)
101  WHERE sa.submission_id = ? AND
102  ug.role_id IN (?, ?)' .
103  ($stageId?' AND ugs.stage_id = ?':''),
104  $params
105  );
106  $returner = isset($result->fields[0]) && $result->fields[0] > 0 ? true : false;
107 
108  $result->Close();
109  return $returner;
110  }
111 
119  function build($submissionId, $userGroupId, $userId) {
120 
121  // If one exists, fetch and return.
122  $stageAssignment = $this->getBySubmissionAndStageId($submissionId, null, $userGroupId, $userId);
123  if (!$stageAssignment->wasEmpty()) return $stageAssignment;
124 
125  // Otherwise, build one.
126  $stageAssignment = $this->newDataObject();
127  $stageAssignment->setSubmissionId($submissionId);
128  $stageAssignment->setUserGroupId($userGroupId);
129  $stageAssignment->setUserId($userId);
130  $this->insertObject($stageAssignment);
131  $stageAssignment->setId($this->getInsertId());
132  return $stageAssignment;
133  }
134 
139  function newDataObject() {
140  return new StageAssignment();
141  }
142 
148  function _fromRow($row) {
149  $stageAssignment = $this->newDataObject();
150 
151  $stageAssignment->setId($row['stage_assignment_id']);
152  $stageAssignment->setSubmissionId($row['submission_id']);
153  $stageAssignment->setUserId($row['user_id']);
154  $stageAssignment->setUserGroupId($row['user_group_id']);
155  $stageAssignment->setDateAssigned($row['date_assigned']);
156  $stageAssignment->setStageId($row['stage_id']);
157 
158  return $stageAssignment;
159  }
160 
166  function insertObject($stageAssignment) {
167  return $this->update(
168  sprintf(
169  'INSERT INTO stage_assignments
170  (submission_id, user_group_id, user_id, date_assigned)
171  VALUES
172  (?, ?, ?, %s)',
174  ),
175  array(
176  $stageAssignment->getSubmissionId(),
177  $this->nullOrInt($stageAssignment->getUserGroupId()),
178  $this->nullOrInt($stageAssignment->getUserId())
179  )
180  );
181  }
182 
188  function deleteObject($stageAssignment) {
189  return $this->deleteByAll(
190  $stageAssignment->getSubmissionId(),
191  $stageAssignment->getUserGroupId(),
192  $stageAssignment->getUserId()
193  );
194  }
195 
203  function deleteByAll($submissionId, $userGroupId, $userId) {
204  return $this->update(
205  'DELETE FROM stage_assignments
206  WHERE submission_id = ?
207  AND user_group_id = ?
208  AND user_id = ?',
209  array((int) $submissionId, (int) $userGroupId, (int) $userId)
210  );
211  }
212 
217  function getInsertId() {
218  return $this->_getInsertId('stage_assignments', 'stage_assignment_id');
219  }
220 
233  function _getByIds($submissionId = null, $stageId = null, $userGroupId = null, $userId = null, $roleId = null, $single = false) {
234  $conditions = array();
235  $params = array();
236  if (isset($submissionId)) {
237  $conditions[] = 'sa.submission_id = ?';
238  $params[] = (int) $submissionId;
239  }
240  if (isset($stageId)) {
241  $conditions[] = 'ugs.stage_id = ?';
242  $params[] = (int) $stageId;
243  }
244  if (isset($userGroupId)) {
245  $conditions[] = 'sa.user_group_id = ?';
246  $params[] = (int) $userGroupId;
247  }
248  if (isset($userId)) {
249  $conditions[] = 'sa.user_id = ?';
250  $params[] = (int) $userId;
251  }
252 
253  if (isset($roleId)) {
254  $conditions[] = 'ug.role_id = ?';
255  $params[] = (int) $roleId;
256  }
257 
258  $result = $this->retrieve(
260  (isset($roleId)?' LEFT JOIN user_groups ug ON sa.user_group_id = ug.user_group_id ':'') .
261  'WHERE ' . (implode(' AND ', $conditions)),
262  $params
263  );
264 
265  $returner = null;
266  if ( $single ) {
267  // all four parameters must be specified for a single record to be returned
268  if (!$submissionId && !$stageId && !$userGroupId && !$userId) return false;
269  // no matches were found.
270  if ($result->RecordCount() == 0) return false;
271  $returner = $this->_fromRow($result->GetRowAssoc(false));
272  $result->Close();
273  } else {
274  // In any other case, return a list of all assignments
275  $returner = new DAOResultFactory($result, $this, '_fromRow');
276  }
277  return $returner;
278  }
279 
285  return 'SELECT ugs.stage_id AS stage_id, sa.* FROM stage_assignments sa
286  JOIN user_group_stage ugs ON sa.user_group_id = ugs.user_group_id ';
287  }
288 }
289 
290 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
getById($stageAssignmentId)
_getByIds($submissionId=null, $stageId=null, $userGroupId=null, $userId=null, $roleId=null, $single=false)
insertObject($stageAssignment)
_getInsertId($table= '', $id= '')
Definition: DAO.inc.php:249
Basic class describing a Stage Assignment.
Operations for retrieving and modifying StageAssignment objects.
getEditorsAssignedToStage($submissionId, $stageId)
Wrapper around ADORecordSet providing &quot;factory&quot; features for generating objects from DAOs...
getBySubmissionAndRoleId($submissionId, $roleId, $stageId=null, $userId=null)
deleteObject($stageAssignment)
getBySubmissionAndStageId($submissionId, $stageId=null, $userGroupId=null, $userId=null)
datetimeToDB($dt)
Definition: DAO.inc.php:293
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
build($submissionId, $userGroupId, $userId)
editorAssignedToStage($submissionId, $stageId=null)
static getCurrentDate($ts=null)
Definition: Core.inc.php:95
deleteByAll($submissionId, $userGroupId, $userId)