ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilStudyProgrammeSettingsDBRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 {
23  private const TABLE = 'prg_settings';
24 
25  private const FIELD_OBJ_ID = 'obj_id';
26  private const FIELD_SUBTYPE_ID = 'subtype_id';
27  private const FIELD_STATUS = 'status';
28  private const FIELD_LP_MODE = 'lp_mode';
29  private const FIELD_POINTS = 'points';
30  private const FIELD_LAST_CHANGED = 'last_change';
31  private const FIELD_DEADLINE_PERIOD = 'deadline_period';
32  private const FIELD_DEADLINE_DATE = 'deadline_date';
33  private const FIELD_VALIDITY_QUALIFICATION_DATE = 'vq_date';
34  private const FIELD_VALIDITY_QUALIFICATION_PERIOD = 'vq_period';
35  private const FIELD_VQ_RESTART_PERIOD = 'vq_restart_period';
36  private const FIELD_VQ_RESTART_RECHECK = 'vq_restart_recheck';
37  private const FIELD_RM_NOT_RESTARTED_BY_USER_DAY = 'rm_nr_by_usr_days';
38  private const FIELD_PROC_ENDS_NOT_SUCCESSFUL = 'proc_end_no_success';
39  private const FIELD_SEND_RE_ASSIGNED_MAIL = "send_re_assigned_mail";
40  private const FIELD_SEND_INFO_TO_RE_ASSIGN_MAIL = "send_info_to_re_assign_mail";
41  private const FIELD_SEND_RISKY_TO_FAIL_MAIL = "send_risky_to_fail_mail";
42 
43  protected static array $cache = [];
44  protected ilDBInterface $db;
45 
46  public function __construct(ilDBInterface $db)
47  {
48  $this->db = $db;
49  }
50 
55  public function createFor(int $obj_id): ilStudyProgrammeSettings
56  {
57  $type_settings = new ilStudyProgrammeTypeSettings(
59  );
60  $assessment_settings = new ilStudyProgrammeAssessmentSettings(
63  );
64  $deadline_settings = new ilStudyProgrammeDeadlineSettings(null, null);
65  $validity_of_achieved_qualification_settings =
66  new \ilStudyProgrammeValidityOfAchievedQualificationSettings(null, null, null, false)
67  ;
68  $automail = new ilStudyProgrammeAutoMailSettings(false, null, null);
69 
70  $prg = new ilStudyProgrammeSettings(
71  $obj_id,
72  $type_settings,
73  $assessment_settings,
74  $deadline_settings,
75  $validity_of_achieved_qualification_settings,
76  $automail
77  );
78 
79  $this->insertDB(
80  $obj_id,
86  0,
89  null,
90  null,
91  null,
92  null
93  );
94 
95  $prg = $prg->setLPMode(ilStudyProgrammeSettings::MODE_UNDEFINED);
96  self::$cache[$obj_id] = $prg;
97  return $prg;
98  }
99 
104  public function get(int $obj_id): ilStudyProgrammeSettings
105  {
106  if (!array_key_exists($obj_id, self::$cache)) {
107  $type = ilObject::_lookupType($obj_id);
108  if ($type === 'prgr') {
109  $prg_reference = new ilObjStudyProgrammeReference($obj_id, false);
110  $obj_id = $prg_reference->getReferencedObject()->getId();
111  }
112  self::$cache[$obj_id] = $this->loadDB($obj_id);
113  }
114  return self::$cache[$obj_id];
115  }
116 
121  {
122  $deadline_period = $settings->getDeadlineSettings()->getDeadlinePeriod();
123  if (is_null($deadline_period)) {
124  $deadline_period = 0;
125  }
126 
127  $deadline_date = $settings->getDeadlineSettings()->getDeadlineDate();
128  if (!is_null($deadline_date)) {
129  $deadline_date = $deadline_date->format(ilStudyProgrammeSettings::DATE_TIME_FORMAT);
130  }
131 
132  $vq_date = $settings->getValidityOfQualificationSettings()->getQualificationDate();
133  if (!is_null($vq_date)) {
134  $vq_date = $vq_date->format(ilStudyProgrammeSettings::DATE_TIME_FORMAT);
135  }
136 
137  $qp = $settings->getValidityOfQualificationSettings()->getQualificationPeriod();
138  if (is_null($qp)) {
139  $qp = 0;
140  }
141 
142  $rp = $settings->getValidityOfQualificationSettings()->getRestartPeriod();
143  if (is_null($rp)) {
144  $rp = 0;
145  }
146 
147  $this->updateDB(
148  $settings->getObjId(),
149  $settings->getTypeSettings()->getTypeId(),
150  $settings->getAssessmentSettings()->getStatus(),
151  $settings->getLPMode(),
152  $settings->getAssessmentSettings()->getPoints(),
154  $deadline_period,
155  $qp,
156  $rp,
157  $deadline_date,
158  $vq_date,
159  $settings->getAutoMailSettings()->getReminderNotRestartedByUserDays(),
160  $settings->getAutoMailSettings()->getProcessingEndsNotSuccessfulDays(),
161  $settings->getAutoMailSettings()->getSendReAssignedMail(),
162  false,
163  false,
164  $settings->getValidityOfQualificationSettings()->getRestartRecheck()
165  );
166  self::$cache[$settings->getObjId()] = $settings;
167  }
168 
172  public function delete(ilStudyProgrammeSettings $settings): void
173  {
174  unset(self::$cache[$settings->getObjId()]);
175  $this->deleteDB($settings->getObjId());
176  }
177 
182  public function loadByType(int $type_id): array
183  {
184  $q = 'SELECT ' . self::FIELD_SUBTYPE_ID
185  . ' ,' . self::FIELD_STATUS
186  . ' ,' . self::FIELD_POINTS
187  . ' ,' . self::FIELD_LP_MODE
188  . ' ,' . self::FIELD_LAST_CHANGED
189  . ' ,' . self::FIELD_OBJ_ID
190  . ' ,' . self::FIELD_DEADLINE_PERIOD
191  . ' ,' . self::FIELD_DEADLINE_DATE
192  . ' ,' . self::FIELD_VALIDITY_QUALIFICATION_PERIOD
193  . ' ,' . self::FIELD_VALIDITY_QUALIFICATION_DATE
194  . ' ,' . self::FIELD_VQ_RESTART_PERIOD
195  . ', ' . self::FIELD_RM_NOT_RESTARTED_BY_USER_DAY
196  . ', ' . self::FIELD_PROC_ENDS_NOT_SUCCESSFUL
197  . ', ' . self::FIELD_SEND_RE_ASSIGNED_MAIL
198  . ', ' . self::FIELD_SEND_INFO_TO_RE_ASSIGN_MAIL
199  . ', ' . self::FIELD_SEND_RISKY_TO_FAIL_MAIL
200  . ', ' . self::FIELD_VQ_RESTART_RECHECK
201  . ' FROM ' . self::TABLE
202  . ' WHERE ' . self::FIELD_SUBTYPE_ID . ' = ' . $this->db->quote($type_id, 'integer');
203  $res = $this->db->query($q);
204  $return = [];
205  while ($rec = $this->db->fetchAssoc($res)) {
206  $return[] = $this->createByRow($rec);
207  }
208  return $return;
209  }
210 
211 
212  public function loadIdsByType(int $type_id): array
213  {
214  return [];
215  }
216 
217  protected function insertDB(
218  int $obj_id,
219  int $subtype_id,
220  int $status,
221  int $lp_mode,
222  int $points,
223  string $last_change,
224  int $deadline_period,
225  int $vq_period,
226  int $vq_restart_period,
227  string $deadline_date = null,
228  string $vq_date = null,
229  int $rm_nr_by_usr_days = null,
230  int $proc_end_no_success = null,
231  bool $send_re_assigned_mail = false,
232  bool $send_info_to_re_assign_mail = false,
233  bool $send_risky_to_fail_mail = false,
234  bool $vq_restart_recheck = false
235  ) {
236  $this->db->insert(
237  self::TABLE,
238  [
239  self::FIELD_OBJ_ID => ['integer', $obj_id],
240  self::FIELD_SUBTYPE_ID => ['integer', $subtype_id],
241  self::FIELD_STATUS => ['integer', $status],
242  self::FIELD_POINTS => ['integer', $points],
243  self::FIELD_LP_MODE => ['integer', $lp_mode],
244  self::FIELD_LAST_CHANGED => ['timestamp', $last_change],
245  self::FIELD_DEADLINE_PERIOD => ['integer', $deadline_period],
246  self::FIELD_DEADLINE_DATE => ['timestamp', $deadline_date],
247  self::FIELD_VALIDITY_QUALIFICATION_DATE => ['timestamp', $vq_date],
248  self::FIELD_VALIDITY_QUALIFICATION_PERIOD => ['integer', $vq_period],
249  self::FIELD_VQ_RESTART_PERIOD => ['integer', $vq_restart_period],
250  self::FIELD_RM_NOT_RESTARTED_BY_USER_DAY => ['integer', $rm_nr_by_usr_days],
251  self::FIELD_PROC_ENDS_NOT_SUCCESSFUL => ['integer', $proc_end_no_success],
252  self::FIELD_SEND_RE_ASSIGNED_MAIL => ['integer', $send_re_assigned_mail],
253  self::FIELD_SEND_INFO_TO_RE_ASSIGN_MAIL => ['integer', $send_info_to_re_assign_mail],
254  self::FIELD_SEND_RISKY_TO_FAIL_MAIL => ['integer', $send_risky_to_fail_mail],
255  self::FIELD_VQ_RESTART_RECHECK => ['integer', $vq_restart_recheck]
256  ]
257  );
258  }
259 
264  protected function loadDB(int $obj_id): ilStudyProgrammeSettings
265  {
266  $rec = $this->db->fetchAssoc(
267  $this->db->query(
268  'SELECT ' . self::FIELD_SUBTYPE_ID
269  . ', ' . self::FIELD_STATUS
270  . ', ' . self::FIELD_POINTS
271  . ', ' . self::FIELD_LP_MODE
272  . ', ' . self::FIELD_LAST_CHANGED
273  . ', ' . self::FIELD_OBJ_ID
274  . ', ' . self::FIELD_DEADLINE_PERIOD
275  . ', ' . self::FIELD_DEADLINE_DATE
276  . ', ' . self::FIELD_VALIDITY_QUALIFICATION_PERIOD
277  . ', ' . self::FIELD_VALIDITY_QUALIFICATION_DATE
278  . ', ' . self::FIELD_VQ_RESTART_PERIOD
279  . ', ' . self::FIELD_RM_NOT_RESTARTED_BY_USER_DAY
280  . ', ' . self::FIELD_PROC_ENDS_NOT_SUCCESSFUL
281  . ', ' . self::FIELD_SEND_RE_ASSIGNED_MAIL
282  . ', ' . self::FIELD_SEND_INFO_TO_RE_ASSIGN_MAIL
283  . ', ' . self::FIELD_SEND_RISKY_TO_FAIL_MAIL
284  . ', ' . self::FIELD_VQ_RESTART_RECHECK
285  . ' FROM ' . self::TABLE
286  . ' WHERE ' . self::FIELD_OBJ_ID . ' = ' . $this->db->quote($obj_id, 'integer')
287  )
288  );
289  if (!$rec) {
290  throw new LogicException('invalid obj_id to load: ' . $obj_id);
291  }
292  return $this->createByRow($rec);
293  }
294 
298  protected function createByRow(array $row): ilStudyProgrammeSettings
299  {
300  $type_settings = new ilStudyProgrammeTypeSettings(
302  );
303  $assessment_settings = new ilStudyProgrammeAssessmentSettings(
306  );
307  $deadline_settings = new ilStudyProgrammeDeadlineSettings(null, null);
308  $validity_of_achieved_qualification_settings =
309  new \ilStudyProgrammeValidityOfAchievedQualificationSettings(
310  null,
311  null,
312  null,
313  (bool) $row[self::FIELD_VQ_RESTART_RECHECK]
314  );
315  $automail = new \ilStudyProgrammeAutoMailSettings(false, null, null);
316 
317  $prg = new ilStudyProgrammeSettings(
318  (int) $row[self::FIELD_OBJ_ID],
319  $type_settings,
320  $assessment_settings,
321  $deadline_settings,
322  $validity_of_achieved_qualification_settings,
323  $automail
324  );
325 
326  $return = $prg
327  ->setLPMode((int) $row[self::FIELD_LP_MODE])
328  ->setLastChange(DateTime::createFromFormat(
330  $row[self::FIELD_LAST_CHANGED]
331  ))
332  ;
333 
334  $type = $return->getTypeSettings();
335  $type = $type->withTypeId((int) $row['subtype_id']);
336  $return = $return->withTypeSettings($type);
337 
338  $points = $return->getAssessmentSettings();
339  $points = $points->withPoints((int) $row['points'])->withStatus((int) $row['status']);
340  $return = $return->withAssessmentSettings($points);
341 
342  $deadline = $return->getDeadlineSettings();
343  if ($row[self::FIELD_DEADLINE_DATE] !== null) {
344  $deadline = $deadline->withDeadlineDate(DateTimeImmutable::createFromFormat(
346  $row[self::FIELD_DEADLINE_DATE]
347  ))
348  ;
349  } else {
350  $deadline_period = (int) $row[self::FIELD_DEADLINE_PERIOD];
351  if ($deadline_period === ilStudyProgrammeSettings::NO_DEADLINE) {
352  $deadline_period = null;
353  }
354  $deadline = $deadline->withDeadlinePeriod($deadline_period);
355  }
356  $return = $return->withDeadlineSettings($deadline);
357 
358  $vqs = $return->getValidityOfQualificationSettings();
359  if ($row[self::FIELD_VALIDITY_QUALIFICATION_DATE] !== null) {
360  $vqs = $vqs->withQualificationDate(
361  DateTimeImmutable::createFromFormat(
363  $row[self::FIELD_VALIDITY_QUALIFICATION_DATE]
364  )
365  );
366  } else {
367  $qualification_period = (int) $row[self::FIELD_VALIDITY_QUALIFICATION_PERIOD];
369  $qualification_period = null;
370  }
371  $vqs = $vqs->withQualificationPeriod($qualification_period);
372  }
373  $restart_period = (int) $row[self::FIELD_VQ_RESTART_PERIOD];
374  if ($restart_period === ilStudyProgrammeSettings::NO_RESTART) {
375  $restart_period = null;
376  }
377 
378  $vqs = $vqs
379  ->withRestartPeriod($restart_period)
380  ->withRestartRecheck((bool) $row[self::FIELD_VQ_RESTART_RECHECK]);
381 
382  $return = $return->withValidityOfQualificationSettings($vqs);
383 
384  $rm_nr_by_usr_days = $row[self::FIELD_RM_NOT_RESTARTED_BY_USER_DAY];
385  if (!is_null($rm_nr_by_usr_days)) {
386  $rm_nr_by_usr_days = (int) $rm_nr_by_usr_days;
387  }
388  $proc_end_no_success = $row[self::FIELD_PROC_ENDS_NOT_SUCCESSFUL];
389  if (!is_null($proc_end_no_success)) {
390  $proc_end_no_success = (int) $proc_end_no_success;
391  }
392 
393  return $return->withAutoMailSettings(
395  (bool) $row[self::FIELD_SEND_RE_ASSIGNED_MAIL],
396  $rm_nr_by_usr_days,
397  $proc_end_no_success
398  )
399  );
400  }
401 
405  protected function deleteDB(int $obj_id): void
406  {
407  if (!$this->checkExists($obj_id)) {
408  throw new LogicException('invaid obj_id to delete: ' . $obj_id);
409  }
410  $this->db->manipulate(
411  'DELETE FROM ' . self::TABLE
412  . ' WHERE ' . self::FIELD_OBJ_ID . ' = ' . $this->db->quote($obj_id, 'integer')
413  );
414  }
415 
419  protected function updateDB(
420  int $obj_id,
421  int $subtype_id,
422  int $status,
423  int $lp_mode,
424  int $points,
425  string $last_change,
426  int $deadline_period,
427  int $vq_period,
428  int $vq_restart_period,
429  string $deadline_date = null,
430  string $vq_date = null,
431  int $rm_nr_by_usr_days = null,
432  int $proc_end_no_success = null,
433  bool $send_re_assigned_mail = false,
434  bool $send_info_to_re_assign_mail = false,
435  bool $send_risky_to_fail_mail = false,
436  bool $vq_restart_recheck = false
437  ) {
438  if (!$this->checkExists($obj_id)) {
439  throw new LogicException('invalid obj_id to update: ' . $obj_id);
440  }
441  $where = [
442  self::FIELD_OBJ_ID => [
443  'integer',
444  $obj_id
445  ]
446  ];
447 
448  $values = [
449  self::FIELD_SUBTYPE_ID => [
450  'integer',
451  $subtype_id
452  ],
453  self::FIELD_STATUS => [
454  'integer',
455  $status
456  ],
457  self::FIELD_LP_MODE => [
458  'integer',
459  $lp_mode
460  ],
461  self::FIELD_POINTS => [
462  'integer',
463  $points
464  ],
465  self::FIELD_LAST_CHANGED => [
466  'timestamp',
467  $last_change
468  ],
469  self::FIELD_DEADLINE_PERIOD => [
470  'integer',
471  $deadline_period
472  ],
473  self::FIELD_DEADLINE_DATE => [
474  'timestamp',
475  $deadline_date
476  ],
477  self::FIELD_VALIDITY_QUALIFICATION_PERIOD => [
478  'integer',
479  $vq_period
480  ],
481  self::FIELD_VALIDITY_QUALIFICATION_DATE => [
482  'timestamp',
483  $vq_date
484  ],
485  self::FIELD_VQ_RESTART_PERIOD => [
486  'integer',
487  $vq_restart_period
488  ],
489  self::FIELD_RM_NOT_RESTARTED_BY_USER_DAY => [
490  'integer',
491  $rm_nr_by_usr_days
492  ],
493  self::FIELD_PROC_ENDS_NOT_SUCCESSFUL => [
494  'integer',
495  $proc_end_no_success
496  ],
497  self::FIELD_SEND_RE_ASSIGNED_MAIL => [
498  'integer',
499  $send_re_assigned_mail
500  ],
501  self::FIELD_SEND_INFO_TO_RE_ASSIGN_MAIL => [
502  'integer',
503  $send_info_to_re_assign_mail
504  ],
505  self::FIELD_SEND_RISKY_TO_FAIL_MAIL => [
506  'integer',
507  $send_risky_to_fail_mail
508  ],
509  self::FIELD_VQ_RESTART_RECHECK => [
510  'integer',
511  $vq_restart_recheck
512  ]
513  ];
514 
515  $this->db->update(self::TABLE, $values, $where);
516  }
517 
518  protected function checkExists(int $obj_id): bool
519  {
520  $rec = $this->db->fetchAssoc(
521  $this->db->query(
522  'SELECT ' . self::FIELD_OBJ_ID
523  . ' FROM ' . self::TABLE
524  . ' WHERE ' . self::FIELD_OBJ_ID . ' = ' . $this->db->quote($obj_id, 'integer')
525  )
526  );
527  if ($rec) {
528  return true;
529  }
530  return false;
531  }
532 
533  public static function clearCache(): void
534  {
535  self::$cache = [];
536  }
537 
544  public function getProgrammeIdsWithRiskyToFailSettings(): array
545  {
546  $query = 'SELECT '
547  . self::FIELD_OBJ_ID . ', '
548  . self::FIELD_PROC_ENDS_NOT_SUCCESSFUL
549  . ' FROM ' . self::TABLE . PHP_EOL
550  . ' WHERE ' . self::FIELD_STATUS . ' = ' . ilStudyProgrammeSettings::STATUS_ACTIVE
551  . ' AND ' . self::FIELD_PROC_ENDS_NOT_SUCCESSFUL . ' IS NOT NULL';
552 
553  $return = [];
554  $res = $this->db->query($query);
555  while ($rec = $this->db->fetchAssoc($res)) {
556  $return[$rec[self::FIELD_OBJ_ID]] = $rec[self::FIELD_PROC_ENDS_NOT_SUCCESSFUL];
557  }
558  return $return;
559  }
560 
567  {
568  $query = 'SELECT '
569  . self::FIELD_OBJ_ID . ', '
570  . self::FIELD_RM_NOT_RESTARTED_BY_USER_DAY
571  . ' FROM ' . self::TABLE . PHP_EOL
572  . ' WHERE ' . self::FIELD_STATUS . ' = ' . ilStudyProgrammeSettings::STATUS_ACTIVE
573  . ' AND ' . self::FIELD_RM_NOT_RESTARTED_BY_USER_DAY . ' IS NOT NULL';
574 
575  $return = [];
576  $res = $this->db->query($query);
577  while ($rec = $this->db->fetchAssoc($res)) {
578  $return[$rec[self::FIELD_OBJ_ID]] = $rec[self::FIELD_RM_NOT_RESTARTED_BY_USER_DAY];
579  }
580  return $return;
581  }
582 
589  {
590  $query = 'SELECT '
591  . self::FIELD_OBJ_ID . ', '
592  . self::FIELD_VQ_RESTART_PERIOD
593  . ' FROM ' . self::TABLE . PHP_EOL
594  . ' WHERE ' . self::FIELD_STATUS . ' = ' . ilStudyProgrammeSettings::STATUS_ACTIVE
595  . ' AND ' . self::FIELD_VQ_RESTART_PERIOD . ' >= 0';
596 
597  $return = [];
598  $res = $this->db->query($query);
599  while ($rec = $this->db->fetchAssoc($res)) {
600  $return[$rec[self::FIELD_OBJ_ID]] = $rec[self::FIELD_VQ_RESTART_PERIOD];
601  }
602  return $return;
603  }
604 }
$res
Definition: ltiservices.php:69
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
createFor(int $obj_id)
Create a record corresponding to a SP-Object and return representing settings.Will throw if a record ...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$type
getLastChange()
Get the timestamp of the last change on this program or a sub program.
loadByType(int $type_id)
Load SP settings by assigned type.ilStudyProgrammeSettings[]
loadIdsByType(int $type_id)
Load SP setting-ids by assigned type.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$query
getObjId()
Get the id of the study program.
updateDB(int $obj_id, int $subtype_id, int $status, int $lp_mode, int $points, string $last_change, int $deadline_period, int $vq_period, int $vq_restart_period, string $deadline_date=null, string $vq_date=null, int $rm_nr_by_usr_days=null, int $proc_end_no_success=null, bool $send_re_assigned_mail=false, bool $send_info_to_re_assign_mail=false, bool $send_risky_to_fail_mail=false, bool $vq_restart_recheck=false)
LogicException
update(ilStudyProgrammeSettings $settings)
Update settings belonging to a SP-Object.Will throw if the record does not exist yet.
getProgrammeIdsWithRiskyToFailSettings()
Programme must be active and have a setting to send mails if the user is at risk to fail completing t...
getProgrammeIdsWithReassignmentForExpiringValidity()
Programme must be active and have a setting to reassign users when validity expires.
insertDB(int $obj_id, int $subtype_id, int $status, int $lp_mode, int $points, string $last_change, int $deadline_period, int $vq_period, int $vq_restart_period, string $deadline_date=null, string $vq_date=null, int $rm_nr_by_usr_days=null, int $proc_end_no_success=null, bool $send_re_assigned_mail=false, bool $send_info_to_re_assign_mail=false, bool $send_risky_to_fail_mail=false, bool $vq_restart_recheck=false)
static _lookupType(int $id, bool $reference=false)
getProgrammeIdsWithMailsForExpiringValidity()
Programme must be active and have a setting to send mails for qualifications about to expire...