ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilADNNotification.php
Go to the documentation of this file.
1 <?php
24 {
25  public const POS_TOP = 1;
26  public const POS_RIGHT = 2;
27  public const POST_LEFT = 3;
28  public const POS_BOTTOM = 4;
29  public const DATE_FORMAT = 'd.m.Y';
30  public const TIME_FORMAT = 'H:i';
31  public const DATE_TIME_FORMAT = 'd.m.Y H:i';
32  public const TYPE_INFO = 1;
33  public const TYPE_WARNING = 2;
34  public const TYPE_ERROR = 3;
35  public const TABLE_NAME = 'il_adn_notifications';
36  public const LINK_TYPE_NONE = 0;
37  public const LINK_TYPE_REF_ID = 1;
38  public const LINK_TYPE_URL = 2;
39  protected static array $allowed_user_ids = array(0, 13, 6);
40 
41  public function getConnectorContainerName(): string
42  {
43  return self::TABLE_NAME;
44  }
45 
49  public static function returnDbTableName(): string
50  {
51  return self::TABLE_NAME;
52  }
53 
54  public function dismiss(ilObjUser $ilObjUser): void
55  {
56  if ($this->isUserAllowedToDismiss($ilObjUser)) {
57  ilADNDismiss::dismiss($ilObjUser, $this);
58  }
59  }
60 
61  protected function hasUserDismissed(ilObjUser $ilObjUser): bool
62  {
63  if (!$this->getDismissable()) {
64  return false;
65  }
66 
67  return ilADNDismiss::hasDimissed($ilObjUser, $this);
68  }
69 
70  public function resetForAllUsers(): void
71  {
72  foreach (ilADNDismiss::where(array('notification_id' => $this->getId()))->get() as $not) {
73  $not->delete();
74  }
75  }
76 
77  public function getFullTimeFormated(): string
78  {
79  if ($this->getEventStart() == 0 && $this->getEventEnd() == 0) {
80  return '';
81  }
82  if (date(self::DATE_FORMAT, $this->getEventStart()) === date(self::DATE_FORMAT, $this->getEventEnd())) {
83  return date(self::DATE_FORMAT, $this->getEventEnd()) . ', ' . date(
84  self::TIME_FORMAT,
85  $this->getEventStart()
86  ) . " - "
87  . date(self::TIME_FORMAT, $this->getEventEnd());
88  } else {
89  return date(self::DATE_TIME_FORMAT, $this->getEventStart()) . ' - ' . date(
90  self::DATE_TIME_FORMAT,
91  $this->getEventEnd()
92  );
93  }
94  }
95 
96  public function isUserAllowedToDismiss(ilObjUser $user): bool
97  {
98  return ($this->getDismissable() && $user->getId() !== 0 && $user->getId() !== ANONYMOUS_USER_ID);
99  }
100 
101  public function getActiveType(): int
102  {
103  if ($this->isPermanent()) {
104  return $this->getType();
105  }
106  if ($this->hasEventStarted() && !$this->hasEventEnded()) {
107  return $this->getTypeDuringEvent();
108  }
109  if ($this->hasDisplayStarted() && !$this->hasDisplayEnded()) {
110  return $this->getType();
111  }
112  return self::TYPE_INFO;
113  }
114 
115 
116  protected function isVisible(): bool
117  {
118  if ($this->isPermanent()) {
119  return true;
120  }
121  $hasEventStarted = $this->hasEventStarted();
122  $hasDisplayStarted = $this->hasDisplayStarted();
123  $hasEventEnded = !$this->hasEventEnded();
124  $hasDisplayEnded = !$this->hasDisplayEnded();
125 
126  return ($hasEventStarted || $hasDisplayStarted)
127  && ($hasEventEnded || $hasDisplayEnded);
128  }
129 
130  public function isVisibleForUser(ilObjUser $ilObjUser): bool
131  {
132  if ($ilObjUser->getId() === 0 && $this->interruptive) {
133  return false;
134  }
135  if (!$this->isVisible()) {
136  return false;
137  }
138  if ($this->hasUserDismissed($ilObjUser)) {
139  return false;
140  }
141  return ($this->isVisibleToUserBasedOnLanguage($ilObjUser) && $this->isVisibleRoleUserRoles($ilObjUser));
142  }
143 
144 
145  protected function isVisibleToUserBasedOnLanguage(ilObjUser $ilObjUser): bool
146  {
147  if (!$this->hasLanguageLimitation()) {
148  return true;
149  }
150 
151  return in_array($ilObjUser->getLanguage(), $this->getLimitedToLanguages(), true);
152  }
153 
154 
155  protected function isVisibleRoleUserRoles(ilObjUser $ilObjUser): bool
156  {
157  if (!$this->isLimitToRoles()) {
158  return true;
159  }
160  global $DIC;
161 
162  if ($ilObjUser->getId() === 0 && in_array(0, $this->getLimitedToRoleIds())) {
163  return true;
164  }
165 
166  return $DIC->rbac()->review()->isAssignedToAtLeastOneGivenRole(
167  $ilObjUser->getId(),
168  $this->getLimitedToRoleIds()
169  );
170  }
171 
180  protected ?int $id = null;
186  protected string $title = '';
191  protected string $body = '';
197  protected ?\DateTimeImmutable $event_start = null;
203  protected ?\DateTimeImmutable $event_end = null;
209  protected ?\DateTimeImmutable $display_start = null;
215  protected ?\DateTimeImmutable $display_end = null;
221  protected int $type = self::TYPE_INFO;
227  protected int $type_during_event = self::TYPE_ERROR;
233  protected bool $dismissable = true;
239  protected bool $permanent = true;
245  protected array $allowed_users = array(0, 6, 13);
251  protected ?int $parent_id = null;
257  protected ?\DateTimeImmutable $create_date = null;
263  protected \DateTimeImmutable $last_update;
269  protected ?int $created_by = null;
275  protected ?int $last_update_by = null;
281  protected bool $active = true;
287  protected array $limited_to_languages = [];
293  protected bool $has_language_limitation = false;
299  protected array $limited_to_role_ids = [];
305  protected bool $limit_to_roles = false;
311  protected bool $interruptive = false;
317  protected string $link = '';
323  protected int $link_type = self::LINK_TYPE_NONE;
329  protected string $link_target = '_top';
330 
336  public function wakeUp($field_name, $field_value)
337  {
338  switch ($field_name) {
339  case 'event_start':
340  case 'event_end':
341  case 'display_end':
342  case 'display_start':
343  case 'create_date':
344  case 'last_update':
345  return (new DateTimeImmutable())->setTimestamp((int) $field_value);
346 
347  case 'allowed_users':
348  if ($field_value === null) {
349  $array_unique = self::$allowed_user_ids;
350  } else {
351  $json_decode = json_decode($field_value, true);
352  if (!is_array($json_decode)) {
353  $json_decode = self::$allowed_user_ids;
354  }
355  $array_unique = array_unique($json_decode);
356  }
357 
358  sort($array_unique);
359 
360  return $array_unique;
361  case 'limited_to_languages':
362  case 'limited_to_role_ids':
363  return json_decode($field_value, true);
364  }
365  }
366 
371  public function sleep($field_name)
372  {
373  switch ($field_name) {
374  case 'event_start':
375  case 'event_end':
376  case 'display_end':
377  case 'display_start':
378  case 'create_date':
379  case 'last_update':
383  $datetime = $this->{$field_name} ?? new DateTimeImmutable();
384  return $datetime->getTimestamp();
385  case 'allowed_users':
386  $allowed_users = self::$allowed_user_ids;
387  foreach ($this->allowed_users as $user_id) {
388  $allowed_users[] = (int) $user_id;
389  }
390 
391  return json_encode(array_unique($allowed_users), JSON_THROW_ON_ERROR);
392  case 'limited_to_languages':
393  case 'limited_to_role_ids':
394  return json_encode($this->{$field_name}, JSON_THROW_ON_ERROR);
395  }
396  }
397 
398  public function create(): void
399  {
400  global $DIC;
401  $this->setCreateDate(new DateTimeImmutable());
402  $this->setCreatedBy($DIC->user()->getId());
403  parent::create();
404  }
405 
406  public function setBody(string $body): void
407  {
408  $this->body = $body;
409  }
410 
411  public function getBody(): string
412  {
413  return $this->body;
414  }
415 
416  public function setDisplayEnd(DateTimeImmutable $display_end): void
417  {
418  $this->display_end = $display_end;
419  }
420 
421  public function getDisplayEnd(): DateTimeImmutable
422  {
423  return $this->display_end ?? new DateTimeImmutable();
424  }
425 
426  public function setDisplayStart(DateTimeImmutable $display_start): void
427  {
428  $this->display_start = $display_start;
429  }
430 
432  {
433  return $this->display_start ?? new DateTimeImmutable();
434  }
435 
436  public function setEventEnd(DateTimeImmutable $event_end): void
437  {
438  $this->event_end = $event_end;
439  }
440 
441  public function getEventEnd(): DateTimeImmutable
442  {
443  return $this->event_end ?? new DateTimeImmutable();
444  }
445 
446  public function setEventStart(DateTimeImmutable $event_start): void
447  {
448  $this->event_start = $event_start;
449  }
450 
451  public function getEventStart(): DateTimeImmutable
452  {
453  return $this->event_start ?? new DateTimeImmutable();
454  }
455 
456  public function setId(int $id): void
457  {
458  $this->id = $id;
459  }
460 
461  public function getId(): int
462  {
463  return (int) $this->id;
464  }
465 
466  public function setTitle(string $title): void
467  {
468  $this->title = $title;
469  }
470 
471  public function getTitle(): string
472  {
473  return $this->title;
474  }
475 
476  public function setType(int $type): void
477  {
478  $this->type = $type;
479  }
480 
481  public function getType(): int
482  {
483  return $this->type;
484  }
485 
486  public function setTypeDuringEvent(int $type_during_event): void
487  {
488  $this->type_during_event = $type_during_event;
489  }
490 
491  public function getTypeDuringEvent(): int
492  {
493  return in_array(
494  $this->type_during_event,
495  [self::TYPE_WARNING, self::TYPE_ERROR, self::TYPE_INFO]
496  ) ? $this->type_during_event : self::TYPE_INFO;
497  }
498 
499  public function setDismissable(bool $dismissable): void
500  {
501  $this->dismissable = $dismissable;
502  }
503 
504  public function getDismissable(): bool
505  {
506  return $this->dismissable;
507  }
508 
509  protected function hasEventStarted(): bool
510  {
511  return $this->getTime() > $this->getEventStart();
512  }
513 
514  protected function hasDisplayStarted(): bool
515  {
516  return $this->getTime() > $this->getDisplayStart();
517  }
518 
519  protected function hasEventEnded(): bool
520  {
521  return $this->getTime() > $this->getEventEnd();
522  }
523 
524  protected function hasDisplayEnded(): bool
525  {
526  return $this->getTime() > $this->getDisplayEnd();
527  }
528 
529  public function setPermanent(bool $permanent): void
530  {
531  $this->permanent = $permanent;
532  }
533 
534  public function isPermanent(): bool
535  {
536  return $this->permanent;
537  }
538 
539  public function isDuringEvent(): bool
540  {
541  return $this->hasEventStarted() && !$this->hasEventEnded();
542  }
543 
544  public function setCreateDate(DateTimeImmutable $create_date): void
545  {
546  $this->create_date = $create_date;
547  }
548 
549  public function getCreateDate(): DateTimeImmutable
550  {
551  return $this->create_date ?? new DateTimeImmutable();
552  }
553 
554  public function setCreatedBy(int $created_by): void
555  {
556  $this->created_by = $created_by;
557  }
558 
559  public function getCreatedBy(): int
560  {
561  return (int) $this->created_by;
562  }
563 
564  protected function getTime(): DateTimeImmutable
565  {
566  return new DateTimeImmutable();
567  }
568 
569  public function isActive(): bool
570  {
571  return $this->active;
572  }
573 
574  public function setActive(bool $active): void
575  {
576  $this->active = $active;
577  }
578 
579  public function getLimitedToLanguages(): array
580  {
582  }
583 
584  public function setLimitedToLanguages(array $limited_to_languages): void
585  {
586  $this->limited_to_languages = $limited_to_languages;
587  }
588 
589  public function hasLanguageLimitation(): bool
590  {
592  }
593 
594  public function setHasLanguageLimitation(bool $has_language_limitation): void
595  {
596  $this->has_language_limitation = $has_language_limitation;
597  }
598 
599  public function getLimitedToRoleIds(): array
600  {
602  }
603 
604  public function setLimitedToRoleIds(array $limited_to_role_ids): void
605  {
606  $this->limited_to_role_ids = $limited_to_role_ids;
607  }
608 
609  public function isLimitToRoles(): bool
610  {
611  return $this->limit_to_roles;
612  }
613 
614  public function setLimitToRoles(bool $limit_to_roles): void
615  {
616  $this->limit_to_roles = $limit_to_roles;
617  }
618 }
DateTimeImmutable $display_end
true integer 8
array $limited_to_languages
true text 256
setHasLanguageLimitation(bool $has_language_limitation)
bool $dismissable
true integer 1
static hasDimissed(ilObjUser $ilObjUser, ilADNNotification $ilADNNotification)
array $limited_to_role_ids
true text 256
wakeUp($field_name, $field_value)
static dismiss(ilObjUser $ilObjUser, ilADNNotification $ilADNNotification)
const ANONYMOUS_USER_ID
Definition: constants.php:27
setEventEnd(DateTimeImmutable $event_end)
dismiss(ilObjUser $ilObjUser)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
bool $has_language_limitation
true integer 1
bool $permanent
true integer 1
int $type_during_event
true integer 1
int $id
true true true true integer 8
$datetime
sleep($field_name)
DateTimeImmutable $last_update
true integer 8
string $title
true text 256
hasUserDismissed(ilObjUser $ilObjUser)
static where($where, $operator=null)
setCreateDate(DateTimeImmutable $create_date)
isUserAllowedToDismiss(ilObjUser $user)
bool $interruptive
true integer 1
isVisibleToUserBasedOnLanguage(ilObjUser $ilObjUser)
global $DIC
Definition: feed.php:28
int $type
true integer 1
setDisplayEnd(DateTimeImmutable $display_end)
int $parent_id
true integer 8
DateTimeImmutable $event_start
true integer 8
setLimitedToLanguages(array $limited_to_languages)
DateTimeImmutable $create_date
true integer 8
isVisibleRoleUserRoles(ilObjUser $ilObjUser)
DateTimeImmutable $display_start
true integer 8
setLimitedToRoleIds(array $limited_to_role_ids)
setPermanent(bool $permanent)
bool $active
true integer 1
array $allowed_users
true text 256
string $link_target
true text 256
int $link_type
true integer 1
string $link
true text 256
isVisibleForUser(ilObjUser $ilObjUser)
int $created_by
true integer 8
setLimitToRoles(bool $limit_to_roles)
bool $limit_to_roles
true integer 1
DateTimeImmutable $event_end
true integer 8
setCreatedBy(int $created_by)
setTypeDuringEvent(int $type_during_event)
setDisplayStart(DateTimeImmutable $display_start)
setEventStart(DateTimeImmutable $event_start)
int $last_update_by
true integer 8
setDismissable(bool $dismissable)