ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilStudyProgrammeAssignmentDBRepository.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
4 {
8  protected $db;
9 
10  const TABLE = 'prg_usr_assignments';
11 
12  const FIELD_ID = 'id';
13  const FIELD_USR_ID = 'usr_id';
14  const FIELD_ROOT_PRG_ID = 'root_prg_id';
15  const FIELD_LAST_CHANGE = 'last_change';
16  const FIELD_LAST_CHANGE_BY = 'last_change_by';
17  const FIELD_RESTART_DATE = 'restart_date';
18  const FIELD_RESTARTED_ASSIGNMENT_ID = 'restarted_assignment_id';
19  const FIELD_RESTART_MAIL = 'restart_mail_send';
20 
21  public function __construct(ilDBInterface $db)
22  {
23  $this->db = $db;
24  }
25 
31  public function createFor(int $root_prg_id, int $usr_id, int $assigning_usr_id) : ilStudyProgrammeAssignment
32  {
33  if (ilObject::_lookupType($usr_id) != "usr") {
34  throw new ilException("ilStudyProgrammeAssignment::createFor: '$usr_id' "
35  . "is no id of a user.");
36  }
37  if (ilObject::_lookupType($root_prg_id) != "prg") {
38  throw new ilException("ilStudyProgrammeAssignment::createFor: '$root_prg_id' "
39  . "is no id of a prg.");
40  }
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);
53  }
54 
59  public function get(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 getByUsrId(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 getByPrgId(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 getByUsrIdAndPrgId(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 getDueToRestart() : array
114  {
115  $return = [];
116  foreach ($this->loadDueToRestart() as $row) {
117  $return[] = $this->assignmentByRow($row);
118  }
119  return $return;
120  }
121 
125  public function getDueToRestartAndMail() : array
126  {
127  $return = [];
128  foreach ($this->loadDueToRestartAndMail() as $row) {
129  $return[] = $this->assignmentByRow($row);
130  }
131  return $return;
132  }
133 
134  protected function loadDueToRestart()
135  {
136  $q = $this->getDueToRestartBaseSQL();
137  $res = $this->db->query($q);
138  while ($rec = $this->db->fetchAssoc($res)) {
139  yield $rec;
140  }
141  }
142 
143  protected function loadDueToRestartAndMail()
144  {
145  $q = $this->getDueToRestartBaseSQL();
146  $q .= ' AND ' . self::FIELD_RESTART_MAIL . ' IS NULL';
147 
148  $res = $this->db->query($q);
149  while ($rec = $this->db->fetchAssoc($res)) {
150  yield $rec;
151  }
152  }
153 
154  protected function getSQLHeader() : string
155  {
156  return 'SELECT ' . self::FIELD_ID
157  . ', ' . self::FIELD_USR_ID
158  . ', ' . self::FIELD_ROOT_PRG_ID
159  . ', ' . self::FIELD_LAST_CHANGE
160  . ', ' . self::FIELD_LAST_CHANGE_BY
161  . ', ' . self::FIELD_RESTART_DATE
162  . ', ' . self::FIELD_RESTARTED_ASSIGNMENT_ID . PHP_EOL
163  . ' FROM ' . self::TABLE . PHP_EOL;
164  }
165 
166  protected function getDueToRestartBaseSQL() : string
167  {
168  return $this->getSQLHeader()
169  . ' WHERE ' . self::FIELD_RESTARTED_ASSIGNMENT_ID
170  . ' = ' . $this->db->quote(
172  'integer'
173  ) . PHP_EOL
174  . ' AND ' . self::FIELD_RESTART_DATE . ' IS NOT NULL' . PHP_EOL
175  . ' AND DATE(' . self::FIELD_RESTART_DATE . ') <= '
176  . $this->db->quote(
177  (new DateTime())->format(
179  ),
180  'text'
181  );
182  }
183 
188  public function getDueToManuelRestart(int $days_before_end) : array
189  {
190  $return = [];
191  foreach ($this->loadDueToManuelRestart($days_before_end) as $row) {
192  $return[] = $this->assignmentByRow($row);
193  }
194  return $return;
195  }
196 
200  protected function loadDueToManuelRestart(int $days_before_end)
201  {
202  $date = new DateTime();
203  $date->sub(new DateInterval('P' . $days_before_end . 'D'));
204  $q = $this->getSQLHeader()
205  . ' WHERE ' . self::FIELD_RESTARTED_ASSIGNMENT_ID
206  . ' = ' . $this->db->quote(
208  'integer'
209  ) . PHP_EOL
210  . ' AND ' . self::FIELD_RESTART_DATE . ' IS NOT NULL' . PHP_EOL
211  . ' AND DATE(' . self::FIELD_RESTART_DATE . ') <= '
212  . $this->db->quote(
213  $date->format(
215  ),
216  'text'
217  );
218  $res = $this->db->query($q);
219  while ($rec = $this->db->fetchAssoc($res)) {
220  yield $rec;
221  }
222  }
223 
227  public function update(ilStudyProgrammeAssignment $assignment)
228  {
229  $row = [
230  self::FIELD_ID => $assignment->getId(),
231  self::FIELD_USR_ID => $assignment->getUserId(),
232  self::FIELD_ROOT_PRG_ID => $assignment->getRootId(),
233  self::FIELD_LAST_CHANGE_BY => $assignment->getLastChangeBy(),
234  self::FIELD_LAST_CHANGE => $assignment->getLastChange()->format(ilStudyProgrammeAssignment::DATE_TIME_FORMAT),
235  self::FIELD_RESTART_DATE => $assignment->getRestartDate() ? $assignment->getRestartDate()->format(ilStudyProgrammeAssignment::DATE_TIME_FORMAT) : null,
236  self::FIELD_RESTARTED_ASSIGNMENT_ID => $assignment->getRestartedAssignmentId()
237  ];
238  $this->updatedRowDB($row);
239  }
240 
244  public function delete(ilStudyProgrammeAssignment $assignment)
245  {
246  $this->deleteDB($assignment->getId());
247  }
248 
249  public function reminderSendFor(int $assignment_id) : void
250  {
251  $where = [
252  self::FIELD_ID => [
253  'integer',
254  $assignment_id
255  ]
256  ];
257 
258  $values = [
259  self::FIELD_RESTART_MAIL => [
260  'timestamp',
261  date('Y-m-d H:i:s')
262  ]
263  ];
264 
265  $this->db->update(self::TABLE, $values, $where);
266  }
267 
271  public function getDashboardInstancesforUser(int $usr_id)
272  {
273  global $DIC;
274  $db = $DIC['ilDB'];
275  $q = 'SELECT ' . self::FIELD_ID
276  . ', ' . self::FIELD_USR_ID
277  . ', ' . self::FIELD_ROOT_PRG_ID
278  . ', ' . self::FIELD_LAST_CHANGE
279  . ', ' . self::FIELD_LAST_CHANGE_BY
280  . ', ' . self::FIELD_RESTART_DATE
281  . ', ' . self::FIELD_RESTARTED_ASSIGNMENT_ID
282  . ' FROM ' . self::TABLE
283  . ' WHERE ' . self::FIELD_USR_ID . ' = ' . $usr_id
284  . ' ORDER BY ' . self::FIELD_ROOT_PRG_ID . ', ' . self::FIELD_ID
285  ;
286  $ret = [];
287  $assignments = [];
288  $res = $db->query($q);
289  $prg = 0;
290  while ($row = $db->fetchAssoc($res)) {
291  if ($prg == 0) {
292  $prg = $row['root_prg_id'];
293  }
294  if ($prg != $row['root_prg_id']) {
295  $ret[$prg] = $assignments;
296  $prg = $row['root_prg_id'];
297  $assignments = [];
298  }
299  $assignments[(int) $row['id']] = $this->assignmentByRow($row);
300  }
301  if (count($assignments) > 0) {
302  $ret[$prg] = $assignments;
303  }
304 
305  return $ret;
306  }
307 
311  protected function assignmentByRow(array $row) : ilStudyProgrammeAssignment
312  {
313  return (new ilStudyProgrammeAssignment((int) $row[self::FIELD_ID]))
314  ->withRootId((int) $row[self::FIELD_ROOT_PRG_ID])
315  ->withUserId((int) $row[self::FIELD_USR_ID])
316  ->withLastChange(
317  (int) $row[self::FIELD_LAST_CHANGE_BY],
318  DateTimeImmutable::createFromFormat(
320  $row[self::FIELD_LAST_CHANGE]
321  )
322  )
323  ->withRestarted(
324  (int) $row[self::FIELD_RESTARTED_ASSIGNMENT_ID],
325  $row[self::FIELD_RESTART_DATE] ?
326  DateTimeImmutable::createFromFormat(ilStudyProgrammeAssignment::DATE_TIME_FORMAT, $row[self::FIELD_RESTART_DATE]) :
327  null
328  );
329  }
330 
331  protected function loadByFilterDB(array $filter)
332  {
333  $q = 'SELECT ' . self::FIELD_ID
334  . ' ,' . self::FIELD_USR_ID
335  . ' ,' . self::FIELD_ROOT_PRG_ID
336  . ' ,' . self::FIELD_LAST_CHANGE
337  . ' ,' . self::FIELD_LAST_CHANGE_BY
338  . ' ,' . self::FIELD_RESTART_DATE
339  . ' ,' . self::FIELD_RESTARTED_ASSIGNMENT_ID
340  . ' FROM ' . self::TABLE
341  . ' WHERE TRUE';
342  foreach ($filter as $field => $value) {
343  $q .= ' AND ' . $field . ' = ' . $this->db->quote($value, 'text');
344  }
345  $res = $this->db->query($q);
346  while ($rec = $this->db->fetchAssoc($res)) {
347  yield $rec;
348  }
349  }
350 
351  protected function insertRowDB(array $row)
352  {
353  $this->db->insert(
354  self::TABLE,
355  [
356  self::FIELD_ID => ['integer', $row[self::FIELD_ID]]
357  , self::FIELD_USR_ID => ['integer', $row[self::FIELD_USR_ID]]
358  , self::FIELD_ROOT_PRG_ID => ['integer', $row[self::FIELD_ROOT_PRG_ID]]
359  , self::FIELD_LAST_CHANGE => ['text', $row[self::FIELD_LAST_CHANGE]]
360  , self::FIELD_LAST_CHANGE_BY => ['integer', $row[self::FIELD_LAST_CHANGE_BY]]
361  , self::FIELD_RESTART_DATE => ['timestamp', $row[self::FIELD_RESTART_DATE]]
362  , self::FIELD_RESTARTED_ASSIGNMENT_ID => ['integer', $row[self::FIELD_RESTARTED_ASSIGNMENT_ID]]
363  ]
364  );
365  }
366 
367  protected function updatedRowDB(array $values)
368  {
369  $q = 'UPDATE ' . self::TABLE
370  . ' SET'
371  . ' ' . self::FIELD_USR_ID . ' = ' . $this->db->quote($values[self::FIELD_USR_ID], 'integer')
372  . ' ,' . self::FIELD_ROOT_PRG_ID . ' = ' . $this->db->quote($values[self::FIELD_ROOT_PRG_ID], 'integer')
373  . ' ,' . self::FIELD_LAST_CHANGE . ' = ' . $this->db->quote($values[self::FIELD_LAST_CHANGE], 'text')
374  . ' ,' . self::FIELD_LAST_CHANGE_BY . ' = ' . $this->db->quote($values[self::FIELD_LAST_CHANGE_BY], 'integer')
375  . ' ,' . self::FIELD_RESTART_DATE . ' = ' . $this->db->quote($values[self::FIELD_RESTART_DATE], 'timestamp')
376  . ' ,' . self::FIELD_RESTARTED_ASSIGNMENT_ID . ' = ' . $this->db->quote($values[self::FIELD_RESTARTED_ASSIGNMENT_ID], 'integer')
377  . ' WHERE ' . self::FIELD_ID . ' = ' . $this->db->quote($values[self::FIELD_ID], 'integer');
378  $this->db->manipulate($q);
379  }
380 
381  protected function deleteDB(int $id)
382  {
383  $this->db->manipulate('DELETE FROM ' . self::TABLE . ' WHERE ' . self::FIELD_ID . ' = ' . $this->db->quote($id, 'integer'));
384  }
385 
386  protected function nextId()
387  {
388  return $this->db->nextId(self::TABLE);
389  }
390 
391 
392  public function deleteAllAssignmentsForProgrammeId(int $prg_obj_id) : void
393  {
394  $query = 'DELETE FROM ' . self::TABLE . PHP_EOL
395  . 'WHERE ' . self::FIELD_ROOT_PRG_ID . '=' . $this->db->quote($prg_obj_id, 'integer');
396  $this->db->manipulate($query);
397  }
398 
399  public function getTableAndFieldOfAssignmentIds() : array
400  {
401  return [self::TABLE, self::FIELD_ID];
402  }
403 
404 
411  public function getInstanceById(int $id)
412  {
413  return $this->get($id);
414  }
415 
416  public function getInstanceByModel(\ilStudyProgrammeAssignment $assignment)
417  {
418  return $assignment;
419  }
420 
421  public function getInstancesOfUser(int $user_id)
422  {
423  global $DIC;
424  $tree = $DIC['tree'];
425 
426  $assignments = $this->getByUsrId($user_id);
427 
428  //if parent object is deleted or in trash
429  //the assignment for the user should not be returned
430  $ret = [];
431  foreach ($assignments as $ass) {
432  foreach (ilObject::_getAllReferences($ass->getRootId()) as $value) {
433  if ($tree->isInTree($value)) {
434  $ret[] = $ass;
435  continue 2;
436  }
437  }
438  }
439  return $ret;
440  }
441 }
update(ilStudyProgrammeAssignment $assignment)
Update settings belonging to a SP-Object.Will throw if the record does not exist yet.
getDueToRestart()
Get all assignments due to restart and not restrted yet.ilStudyProgrammeAssignment[] ...
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 ...
getInstanceById(int $id)
Backport ilStudyProgrammeUserAssignmentDB
static now()
Return current timestamp in Y-m-d H:i:s format.
static _getAllReferences($a_id)
get all reference ids of object
foreach($_POST as $key=> $value) $res
global $DIC
Definition: goto.php:24
Represents one assignment of the user to a program tree.
$query
static _lookupType($a_id, $a_reference=false)
lookup object type
getDueToManuelRestart(int $days_before_end)
Get all assignments due to restart and not restrted yet.
$ret
Definition: parser.php:6