ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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 = [0, 13, 6];
40 
41  #[\Override]
42  public function getConnectorContainerName(): string
43  {
44  return self::TABLE_NAME;
45  }
46 
50  public static function returnDbTableName(): string
51  {
52  return self::TABLE_NAME;
53  }
54 
55  public function dismiss(ilObjUser $ilObjUser): void
56  {
57  if ($this->isUserAllowedToDismiss($ilObjUser)) {
58  ilADNDismiss::dismiss($ilObjUser, $this);
59  }
60  }
61 
62  protected function hasUserDismissed(ilObjUser $ilObjUser): bool
63  {
64  if (!$this->getDismissable()) {
65  return false;
66  }
67 
68  return ilADNDismiss::hasDimissed($ilObjUser, $this);
69  }
70 
71  public function resetForAllUsers(): void
72  {
73  foreach (ilADNDismiss::where(['notification_id' => $this->getId()])->get() as $not) {
74  $not->delete();
75  }
76  }
77 
78  public function getFullTimeFormated(): string
79  {
80  if ($this->getEventStart() == 0 && $this->getEventEnd() == 0) {
81  return '';
82  }
83  if (date(self::DATE_FORMAT, $this->getEventStart()) === date(self::DATE_FORMAT, $this->getEventEnd())) {
84  return date(self::DATE_FORMAT, $this->getEventEnd()) . ', ' . date(
85  self::TIME_FORMAT,
86  $this->getEventStart()
87  ) . " - "
88  . date(self::TIME_FORMAT, $this->getEventEnd());
89  }
90  return date(self::DATE_TIME_FORMAT, $this->getEventStart()) . ' - ' . date(
91  self::DATE_TIME_FORMAT,
92  $this->getEventEnd()
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 = [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((string) $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((string) $field_value, true);
364  }
365  return null;
366  }
367 
372  public function sleep($field_name)
373  {
374  switch ($field_name) {
375  case 'event_start':
376  case 'event_end':
377  case 'display_end':
378  case 'display_start':
379  case 'create_date':
380  case 'last_update':
384  $datetime = $this->{$field_name} ?? new DateTimeImmutable();
385  return $datetime->getTimestamp();
386  case 'allowed_users':
387  $allowed_users = self::$allowed_user_ids;
388  foreach ($this->allowed_users as $user_id) {
389  $allowed_users[] = (int) $user_id;
390  }
391 
392  return json_encode(array_unique($allowed_users), JSON_THROW_ON_ERROR);
393  case 'limited_to_languages':
394  case 'limited_to_role_ids':
395  return json_encode($this->{$field_name}, JSON_THROW_ON_ERROR);
396  }
397  return null;
398  }
399 
400  #[\Override]
401  public function create(): void
402  {
403  global $DIC;
404  $this->setCreateDate(new DateTimeImmutable());
405  $this->setCreatedBy($DIC->user()->getId());
406  parent::create();
407  }
408 
409  public function setBody(string $body): void
410  {
411  $this->body = $body;
412  }
413 
414  public function getBody(): string
415  {
416  return $this->body;
417  }
418 
419  public function setDisplayEnd(DateTimeImmutable $display_end): void
420  {
421  $this->display_end = $display_end;
422  }
423 
424  public function getDisplayEnd(): DateTimeImmutable
425  {
426  return $this->display_end ?? new DateTimeImmutable();
427  }
428 
429  public function setDisplayStart(DateTimeImmutable $display_start): void
430  {
431  $this->display_start = $display_start;
432  }
433 
435  {
436  return $this->display_start ?? new DateTimeImmutable();
437  }
438 
439  public function setEventEnd(DateTimeImmutable $event_end): void
440  {
441  $this->event_end = $event_end;
442  }
443 
444  public function getEventEnd(): DateTimeImmutable
445  {
446  return $this->event_end ?? new DateTimeImmutable();
447  }
448 
449  public function setEventStart(DateTimeImmutable $event_start): void
450  {
451  $this->event_start = $event_start;
452  }
453 
454  public function getEventStart(): DateTimeImmutable
455  {
456  return $this->event_start ?? new DateTimeImmutable();
457  }
458 
459  public function setId(int $id): void
460  {
461  $this->id = $id;
462  }
463 
464  public function getId(): int
465  {
466  return (int) $this->id;
467  }
468 
469  public function setTitle(string $title): void
470  {
471  $this->title = $title;
472  }
473 
474  public function getTitle(): string
475  {
476  return $this->title;
477  }
478 
479  public function setType(int $type): void
480  {
481  $this->type = $type;
482  }
483 
484  public function getType(): int
485  {
486  return $this->type;
487  }
488 
489  public function setTypeDuringEvent(int $type_during_event): void
490  {
491  $this->type_during_event = $type_during_event;
492  }
493 
494  public function getTypeDuringEvent(): int
495  {
496  return in_array(
497  $this->type_during_event,
498  [self::TYPE_WARNING, self::TYPE_ERROR, self::TYPE_INFO]
499  ) ? $this->type_during_event : self::TYPE_INFO;
500  }
501 
502  public function setDismissable(bool $dismissable): void
503  {
504  $this->dismissable = $dismissable;
505  }
506 
507  public function getDismissable(): bool
508  {
509  return $this->dismissable;
510  }
511 
512  protected function hasEventStarted(): bool
513  {
514  return $this->getTime() > $this->getEventStart();
515  }
516 
517  protected function hasDisplayStarted(): bool
518  {
519  return $this->getTime() > $this->getDisplayStart();
520  }
521 
522  protected function hasEventEnded(): bool
523  {
524  return $this->getTime() > $this->getEventEnd();
525  }
526 
527  protected function hasDisplayEnded(): bool
528  {
529  return $this->getTime() > $this->getDisplayEnd();
530  }
531 
532  public function setPermanent(bool $permanent): void
533  {
534  $this->permanent = $permanent;
535  }
536 
537  public function isPermanent(): bool
538  {
539  return $this->permanent;
540  }
541 
542  public function isDuringEvent(): bool
543  {
544  return $this->hasEventStarted() && !$this->hasEventEnded();
545  }
546 
547  public function setCreateDate(DateTimeImmutable $create_date): void
548  {
549  $this->create_date = $create_date;
550  }
551 
552  public function getCreateDate(): DateTimeImmutable
553  {
554  return $this->create_date ?? new DateTimeImmutable();
555  }
556 
557  public function setCreatedBy(int $created_by): void
558  {
559  $this->created_by = $created_by;
560  }
561 
562  public function getCreatedBy(): int
563  {
564  return (int) $this->created_by;
565  }
566 
567  protected function getTime(): DateTimeImmutable
568  {
569  return new DateTimeImmutable();
570  }
571 
572  public function isActive(): bool
573  {
574  return $this->active;
575  }
576 
577  public function setActive(bool $active): void
578  {
579  $this->active = $active;
580  }
581 
582  public function getLimitedToLanguages(): array
583  {
585  }
586 
587  public function setLimitedToLanguages(array $limited_to_languages): void
588  {
589  $this->limited_to_languages = $limited_to_languages;
590  }
591 
592  public function hasLanguageLimitation(): bool
593  {
595  }
596 
597  public function setHasLanguageLimitation(bool $has_language_limitation): void
598  {
599  $this->has_language_limitation = $has_language_limitation;
600  }
601 
602  public function getLimitedToRoleIds(): array
603  {
605  }
606 
607  public function setLimitedToRoleIds(array $limited_to_role_ids): void
608  {
609  $this->limited_to_role_ids = $limited_to_role_ids;
610  }
611 
612  public function isLimitToRoles(): bool
613  {
614  return $this->limit_to_roles;
615  }
616 
617  public function setLimitToRoles(bool $limit_to_roles): void
618  {
619  $this->limit_to_roles = $limit_to_roles;
620  }
621 }
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)
sort()
description: > Example for rendering a Sort Glyph.
Definition: sort.php:41
bool $interruptive
true integer 1
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
isVisibleToUserBasedOnLanguage(ilObjUser $ilObjUser)
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
global $DIC
Definition: shib_login.php:22
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)