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