Open Journal Systems  3.0.0
 All Data Structures 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 
70  function getByUserId($userId) {
71  return $this->_getByIds(null, null, null, $userId);
72  }
73 
80  function getEditorsAssignedToStage($submissionId, $stageId) {
81  $managerAssignmentFactory = $this->getBySubmissionAndRoleId($submissionId, ROLE_ID_MANAGER, $stageId);
82  $subEditorAssignmentFactory = $this->getBySubmissionAndRoleId($submissionId, ROLE_ID_SUB_EDITOR, $stageId);
83  return array_merge($managerAssignmentFactory->toArray(), $subEditorAssignmentFactory->toArray());
84  }
85 
94  function editorAssignedToStage($submissionId, $stageId = null) {
95  $params = array((int) $submissionId, ROLE_ID_MANAGER, ROLE_ID_SUB_EDITOR);
96  if ($stageId) $params[] = (int) $stageId;
97  $result = $this->retrieve(
98  'SELECT COUNT(*)
99  FROM stage_assignments sa
100  JOIN user_groups ug ON (sa.user_group_id = ug.user_group_id)
101  JOIN user_group_stage ugs ON (ug.user_group_id = ugs.user_group_id)
102  WHERE sa.submission_id = ? AND
103  ug.role_id IN (?, ?)' .
104  ($stageId?' AND ugs.stage_id = ?':''),
105  $params
106  );
107  $returner = isset($result->fields[0]) && $result->fields[0] > 0 ? true : false;
108 
109  $result->Close();
110  return $returner;
111  }
112 
120  function build($submissionId, $userGroupId, $userId) {
121 
122  // If one exists, fetch and return.
123  $stageAssignment = $this->getBySubmissionAndStageId($submissionId, null, $userGroupId, $userId);
124  if (!$stageAssignment->wasEmpty()) return $stageAssignment;
125 
126  // Otherwise, build one.
127  $stageAssignment = $this->newDataObject();
128  $stageAssignment->setSubmissionId($submissionId);
129  $stageAssignment->setUserGroupId($userGroupId);
130  $stageAssignment->setUserId($userId);
131  $this->insertObject($stageAssignment);
132  $stageAssignment->setId($this->getInsertId());
133  return $stageAssignment;
134  }
135 
140  function newDataObject() {
141  return new StageAssignment();
142  }
143 
149  function _fromRow($row) {
150  $stageAssignment = $this->newDataObject();
151 
152  $stageAssignment->setId($row['stage_assignment_id']);
153  $stageAssignment->setSubmissionId($row['submission_id']);
154  $stageAssignment->setUserId($row['user_id']);
155  $stageAssignment->setUserGroupId($row['user_group_id']);
156  $stageAssignment->setDateAssigned($row['date_assigned']);
157  $stageAssignment->setStageId($row['stage_id']);
158 
159  return $stageAssignment;
160  }
161 
166  function insertObject($stageAssignment) {
167  $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 
187  function updateObject($stageAssignment) {
188  $this->update(
189  sprintf(
190  'UPDATE stage_assignments SET
191  submission_id = ?,
192  user_group_id = ?,
193  user_id = ?,
194  date_assigned = %s
195  WHERE stage_assignment_id = ?',
197  ),
198  array(
199  (int) $stageAssignment->getSubmissionId(),
200  $this->nullOrInt($stageAssignment->getUserGroupId()),
201  $this->nullOrInt($stageAssignment->getUserId()),
202  (int) $stageAssignment->getId(),
203  )
204  );
205  }
206 
211  function deleteObject($stageAssignment) {
212  $this->deleteByAll(
213  $stageAssignment->getSubmissionId(),
214  $stageAssignment->getUserGroupId(),
215  $stageAssignment->getUserId()
216  );
217  }
218 
225  function deleteByAll($submissionId, $userGroupId, $userId) {
226  $this->update(
227  'DELETE FROM stage_assignments
228  WHERE submission_id = ?
229  AND user_group_id = ?
230  AND user_id = ?',
231  array((int) $submissionId, (int) $userGroupId, (int) $userId)
232  );
233  }
234 
239  function getInsertId() {
240  return $this->_getInsertId('stage_assignments', 'stage_assignment_id');
241  }
242 
255  function _getByIds($submissionId = null, $stageId = null, $userGroupId = null, $userId = null, $roleId = null, $single = false) {
256  $conditions = array();
257  $params = array();
258  if (isset($submissionId)) {
259  $conditions[] = 'sa.submission_id = ?';
260  $params[] = (int) $submissionId;
261  }
262  if (isset($stageId)) {
263  $conditions[] = 'ugs.stage_id = ?';
264  $params[] = (int) $stageId;
265  }
266  if (isset($userGroupId)) {
267  $conditions[] = 'sa.user_group_id = ?';
268  $params[] = (int) $userGroupId;
269  }
270  if (isset($userId)) {
271  $conditions[] = 'sa.user_id = ?';
272  $params[] = (int) $userId;
273  }
274 
275  if (isset($roleId)) {
276  $conditions[] = 'ug.role_id = ?';
277  $params[] = (int) $roleId;
278  }
279 
280  $result = $this->retrieve(
282  (isset($roleId)?' LEFT JOIN user_groups ug ON sa.user_group_id = ug.user_group_id ':'') .
283  'WHERE ' . (implode(' AND ', $conditions)),
284  $params
285  );
286 
287  if ($single) {
288  // all four parameters must be specified for a single record to be returned
289  if (!$submissionId && !$stageId && !$userGroupId && !$userId) return false;
290  // no matches were found.
291  if ($result->RecordCount() == 0) return false;
292  $returner = $this->_fromRow($result->GetRowAssoc(false));
293  $result->Close();
294  return $returner;
295  } else {
296  // In any other case, return a list of all assignments
297  return new DAOResultFactory($result, $this, '_fromRow');
298  }
299  }
300 
306  return 'SELECT ugs.stage_id AS stage_id, sa.* FROM stage_assignments sa
307  JOIN user_group_stage ugs ON sa.user_group_id = ugs.user_group_id ';
308  }
309 }
310 
311 ?>
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
updateObject($stageAssignment)
nullOrInt($value)
Definition: DAO.inc.php:430
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)