ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilStudyProgrammeAssignmentDBRepository.php
Go to the documentation of this file.
1 <?php
2 
3 
5 {
9  protected $db;
10 
11  const TABLE = 'prg_usr_assignments';
12 
13  const FIELD_ID = 'id';
14  const FIELD_USR_ID = 'usr_id';
15  const FIELD_ROOT_PRG_ID = 'root_prg_id';
16  const FIELD_LAST_CHANGE = 'last_change';
17  const FIELD_LAST_CHANGE_BY = 'last_change_by';
18  const FIELD_RESTART_DATE = 'restart_date';
19  const FIELD_RESTARTED_ASSIGNMENT_ID = 'restarted_assignment_id';
20  const FIELD_RESTART_MAIL = 'restart_mail_send';
21 
22  public function __construct(ilDBInterface $db)
23  {
24  $this->db = $db;
25  }
26 
32  public function createFor(int $root_prg_id, int $usr_id, int $assigning_usr_id) : ilStudyProgrammeAssignment
33  {
34  if (ilObject::_lookupType($usr_id) != "usr") {
35  throw new ilException("ilStudyProgrammeAssignment::createFor: '$usr_id' "
36  . "is no id of a user.");
37  }
38  if (ilObject::_lookupType($root_prg_id) != "prg") {
39  throw new ilException("ilStudyProgrammeAssignment::createFor: '$root_prg_id' "
40  . "is no id of a prg.");
41  }
42  $row = [
43  self::FIELD_ID => $this->nextId(),
44  self::FIELD_USR_ID => $usr_id,
45  self::FIELD_ROOT_PRG_ID => $root_prg_id,
46  self::FIELD_LAST_CHANGE_BY => $assigning_usr_id,
47  self::FIELD_LAST_CHANGE => ilUtil::now(),
48  self::FIELD_RESTART_DATE => null,
49  self::FIELD_RESTARTED_ASSIGNMENT_ID => ilStudyProgrammeAssignment::NO_RESTARTED_ASSIGNMENT
50  ];
51  $this->insertRowDB($row);
52  return $this->assignmentByRow($row)->updateLastChange();
53  }
54 
59  public function read(int $id)
60  {
61  foreach ($this->loadByFilterDB([self::FIELD_ID => $id]) as $row) {
62  return $this->assignmentByRow($row);
63  }
64  return null;
65  }
66 
71  public function readByUsrId(int $usr_id) : array
72  {
73  $return = [];
74  foreach ($this->loadByFilterDB([self::FIELD_USR_ID => $usr_id]) as $row) {
75  $return[] = $this->assignmentByRow($row);
76  }
77  return $return;
78  }
79 
84  public function readByPrgId(int $prg_id) : array
85  {
86  $return = [];
87  foreach ($this->loadByFilterDB([self::FIELD_ROOT_PRG_ID => $prg_id]) as $row) {
88  $return[] = $this->assignmentByRow($row);
89  }
90  return $return;
91  }
92 
97  public function readByUsrIdAndPrgId(int $usr_id, int $prg_id)
98  {
99  $return = [];
100  foreach ($this->loadByFilterDB(
101  [self::FIELD_USR_ID => $usr_id
102  , self::FIELD_ROOT_PRG_ID => $prg_id]
103  ) as $row) {
104  $return[] = $this->assignmentByRow($row);
105  }
106  return $return;
107  }
108 
113  public function readDueToRestart() : array
114  {
115  $return = [];
116  foreach ($this->loadDueToRestart() as $row) {
117  $return[] = $this->assignmentByRow($row);
118  }
119  return $return;
120  }
121 
122  public function readDueToRestartAndMail() : array
123  {
124  $return = [];
125  foreach ($this->loadDueToRestartAndMail() as $row) {
126  $return[] = $this->assignmentByRow($row);
127  }
128  return $return;
129  }
130 
131  protected function loadDueToRestart()
132  {
133  $q = $this->getDueToRestartBaseSQL();
134  $res = $this->db->query($q);
135  while ($rec = $this->db->fetchAssoc($res)) {
136  yield $rec;
137  }
138  }
139 
140  protected function loadDueToRestartAndMail()
141  {
142  $q = $this->getDueToRestartBaseSQL();
143  $q .= ' AND ' . self::FIELD_RESTART_MAIL . ' IS NULL';
144 
145  $res = $this->db->query($q);
146  while ($rec = $this->db->fetchAssoc($res)) {
147  yield $rec;
148  }
149  }
150 
151  protected function getSQLHeader() : string
152  {
153  return 'SELECT ' . self::FIELD_ID
154  . ', ' . self::FIELD_USR_ID
155  . ', ' . self::FIELD_ROOT_PRG_ID
156  . ', ' . self::FIELD_LAST_CHANGE
157  . ', ' . self::FIELD_LAST_CHANGE_BY
158  . ', ' . self::FIELD_RESTART_DATE
159  . ', ' . self::FIELD_RESTARTED_ASSIGNMENT_ID . PHP_EOL
160  . ' FROM ' . self::TABLE . PHP_EOL;
161  }
162 
163  protected function getDueToRestartBaseSQL() : string
164  {
165  return $this->getSQLHeader()
166  . ' WHERE ' . self::FIELD_RESTARTED_ASSIGNMENT_ID
167  . ' = ' . $this->db->quote(
169  'integer'
170  ) . PHP_EOL
171  . ' AND ' . self::FIELD_RESTART_DATE . ' IS NOT NULL' . PHP_EOL
172  . ' AND DATE(' . self::FIELD_RESTART_DATE . ') <= '
173  . $this->db->quote(
174  (new DateTime())->format(
176  ),
177  'text'
178  );
179  }
180 
185  public function readDueToManuelRestart(int $days_before_end) : array
186  {
187  $return = [];
188  foreach ($this->loadDueToManuelRestart($days_before_end) as $row) {
189  $return[] = $this->assignmentByRow($row);
190  }
191  return $return;
192  }
193 
197  protected function loadDueToManuelRestart(int $days_before_end)
198  {
199  $date = new DateTime();
200  $date->sub(new DateInterval('P' . $days_before_end . 'D'));
201  $q = $this->getSQLHeader()
202  . ' WHERE ' . self::FIELD_RESTARTED_ASSIGNMENT_ID
203  . ' = ' . $this->db->quote(
205  'integer'
206  ) . PHP_EOL
207  . ' AND ' . self::FIELD_RESTART_DATE . ' IS NOT NULL' . PHP_EOL
208  . ' AND DATE(' . self::FIELD_RESTART_DATE . ') <= '
209  . $this->db->quote(
210  $date->format(
212  ),
213  'text'
214  );
215  $res = $this->db->query($q);
216  while ($rec = $this->db->fetchAssoc($res)) {
217  yield $rec;
218  }
219  }
220 
224  public function update(ilStudyProgrammeAssignment $assignment)
225  {
226  $row = [
227  self::FIELD_ID => $assignment->getId(),
228  self::FIELD_USR_ID => $assignment->getUserId(),
229  self::FIELD_ROOT_PRG_ID => $assignment->getRootId(),
230  self::FIELD_LAST_CHANGE_BY => $assignment->getLastChangeBy(),
231  self::FIELD_LAST_CHANGE => $assignment->getLastChange()->format(ilStudyProgrammeAssignment::DATE_TIME_FORMAT),
232  self::FIELD_RESTART_DATE => $assignment->getRestartDate() ? $assignment->getRestartDate()->format(ilStudyProgrammeAssignment::DATE_TIME_FORMAT) : null,
233  self::FIELD_RESTARTED_ASSIGNMENT_ID => $assignment->getRestartedAssignmentId()
234  ];
235  $this->updatedRowDB($row);
236  }
237 
241  public function delete(ilStudyProgrammeAssignment $assignment)
242  {
243  $this->deleteDB($assignment->getId());
244  }
245 
246  public function reminderSendFor(int $assignment_id) : void
247  {
248  $where = [
249  self::FIELD_ID => [
250  'integer',
251  $assignment_id
252  ]
253  ];
254 
255  $values = [
256  self::FIELD_RESTART_MAIL => [
257  'timestamp',
258  date('Y-m-d H:i:s')
259  ]
260  ];
261 
262  $this->db->update(self::TABLE, $values, $where);
263  }
264 
268  public function getDashboardInstancesforUser(int $usr_id)
269  {
270  global $DIC;
271  $db = $DIC['ilDB'];
272  $q = 'SELECT ' . self::FIELD_ID
273  . ', ' . self::FIELD_USR_ID
274  . ', ' . self::FIELD_ROOT_PRG_ID
275  . ', ' . self::FIELD_LAST_CHANGE
276  . ', ' . self::FIELD_LAST_CHANGE_BY
277  . ', ' . self::FIELD_RESTART_DATE
278  . ', ' . self::FIELD_RESTARTED_ASSIGNMENT_ID
279  . ' FROM ' . self::TABLE
280  . ' WHERE ' . self::FIELD_USR_ID . ' = ' . $usr_id
281  . ' ORDER BY ' . self::FIELD_ROOT_PRG_ID . ', ' . self::FIELD_ID
282  ;
283  $ret = [];
284  $assignments = [];
285  $res = $db->query($q);
286  $prg = 0;
287  while ($row = $db->fetchAssoc($res)) {
288  if ($prg == 0) {
289  $prg = $row['root_prg_id'];
290  }
291  if ($prg != $row['root_prg_id']) {
292  $ret[$prg] = $assignments;
293  $prg = $row['root_prg_id'];
294  $assignments = [];
295  }
296  $assignments[(int) $row['id']] = $this->assignmentByRow($row);
297  }
298  if (count($assignments) > 0) {
299  $ret[$prg] = $assignments;
300  }
301  return $ret;
302  }
303 
307  protected function assignmentByRow(array $row) : ilStudyProgrammeAssignment
308  {
309  return (new ilStudyProgrammeAssignment($row[self::FIELD_ID]))
310  ->setRootId($row[self::FIELD_ROOT_PRG_ID])
311  ->setUserId($row[self::FIELD_USR_ID])
312  ->setLastChangeBy($row[self::FIELD_LAST_CHANGE_BY])
313  ->setLastChange(DateTime::createFromFormat(
315  $row[self::FIELD_LAST_CHANGE]
316  ))
317  ->setRestartDate(
318  $row[self::FIELD_RESTART_DATE] ?
319  DateTime::createFromFormat(ilStudyProgrammeAssignment::DATE_TIME_FORMAT, $row[self::FIELD_RESTART_DATE]) :
320  null
321  )
322  ->setRestartedAssignmentId($row[self::FIELD_RESTARTED_ASSIGNMENT_ID]);
323  }
324 
325  protected function loadByFilterDB(array $filter)
326  {
327  $q = 'SELECT ' . self::FIELD_ID
328  . ' ,' . self::FIELD_USR_ID
329  . ' ,' . self::FIELD_ROOT_PRG_ID
330  . ' ,' . self::FIELD_LAST_CHANGE
331  . ' ,' . self::FIELD_LAST_CHANGE_BY
332  . ' ,' . self::FIELD_RESTART_DATE
333  . ' ,' . self::FIELD_RESTARTED_ASSIGNMENT_ID
334  . ' FROM ' . self::TABLE
335  . ' WHERE TRUE';
336  foreach ($filter as $field => $value) {
337  $q .= ' AND ' . $field . ' = ' . $this->db->quote($value, 'text');
338  }
339  $res = $this->db->query($q);
340  while ($rec = $this->db->fetchAssoc($res)) {
341  yield $rec;
342  }
343  }
344 
345  protected function insertRowDB(array $row)
346  {
347  $this->db->insert(
348  self::TABLE,
349  [
350  self::FIELD_ID => ['interger', $row[self::FIELD_ID]]
351  , self::FIELD_USR_ID => ['interger', $row[self::FIELD_USR_ID]]
352  , self::FIELD_ROOT_PRG_ID => ['interger', $row[self::FIELD_ROOT_PRG_ID]]
353  , self::FIELD_LAST_CHANGE => ['interger', $row[self::FIELD_LAST_CHANGE]]
354  , self::FIELD_LAST_CHANGE_BY => ['interger', $row[self::FIELD_LAST_CHANGE_BY]]
355  , self::FIELD_RESTART_DATE => ['timestamp', $row[self::FIELD_RESTART_DATE]]
356  , self::FIELD_RESTARTED_ASSIGNMENT_ID => ['integer', $row[self::FIELD_RESTARTED_ASSIGNMENT_ID]]
357  ]
358  );
359  }
360 
361  protected function updatedRowDB(array $values)
362  {
363  $q = 'UPDATE ' . self::TABLE
364  . ' SET'
365  . ' ' . self::FIELD_USR_ID . ' = ' . $this->db->quote($values[self::FIELD_USR_ID], 'integer')
366  . ' ,' . self::FIELD_ROOT_PRG_ID . ' = ' . $this->db->quote($values[self::FIELD_ROOT_PRG_ID], 'integer')
367  . ' ,' . self::FIELD_LAST_CHANGE . ' = ' . $this->db->quote($values[self::FIELD_LAST_CHANGE], 'text')
368  . ' ,' . self::FIELD_LAST_CHANGE_BY . ' = ' . $this->db->quote($values[self::FIELD_LAST_CHANGE_BY], 'integer')
369  . ' ,' . self::FIELD_RESTART_DATE . ' = ' . $this->db->quote($values[self::FIELD_RESTART_DATE], 'timestamp')
370  . ' ,' . self::FIELD_RESTARTED_ASSIGNMENT_ID . ' = ' . $this->db->quote($values[self::FIELD_RESTARTED_ASSIGNMENT_ID], 'integer')
371  . ' WHERE ' . self::FIELD_ID . ' = ' . $this->db->quote($values[self::FIELD_ID], 'integer');
372  $this->db->manipulate($q);
373  }
374 
375  protected function deleteDB(int $id)
376  {
377  $this->db->manipulate('DELETE FROM ' . self::TABLE . ' WHERE ' . self::FIELD_ID . ' = ' . $this->db->quote($id, 'integer'));
378  }
379 
380  protected function nextId()
381  {
382  return $this->db->nextId(self::TABLE);
383  }
384 }
update(ilStudyProgrammeAssignment $assignment)
Update settings belonging to a SP-Object.Will throw if the record does not exist yet.
getUserId()
Get the id of the user who is assigned.
getRestartedAssignmentId()
Get the id of the assignment which was intiated due to expiring progress of this assignment.
createFor(int $root_prg_id, int $usr_id, int $assigning_usr_id)
Create a record corresponding to a SP-Object and return represending settings.Will throw if a record ...
static now()
Return current timestamp in Y-m-d H:i:s format.
read(int $id)
Load settings belonging to a SP-Object.Will throw if the record does not exist yet.ilStudyProgrammeAssignment | null
getLastChange()
Get the timestamp of the last change on this program or a sub program.
Interface ilDBInterface.
foreach($_POST as $key=> $value) $res
readDueToRestart()
Get all assignments due to restart and not restrted yet.
Class ilStudyProgrammeAssignment.
getRestartDate()
Get the date, at which the user is to be reassigned to the programme.
static _lookupType($a_id, $a_reference=false)
lookup object type
$ret
Definition: parser.php:6
$DIC
Definition: xapitoken.php:46
getLastChangeBy()
Get the id of the user who did the last change on this assignment.
getRootId()
Get the object id of the program the user was assigned to.
readDueToManuelRestart(int $days_before_end)
Get all assignments due to restart and not restrted yet.
getId()
Get the id of the assignment.