ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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
146 protected function isVisibleToUserBasedOnLanguage(ilObjUser $ilObjUser): bool
147 {
148 if (!$this->hasLanguageLimitation()) {
149 return true;
150 }
151
152 return in_array($ilObjUser->getLanguage(), $this->getLimitedToLanguages(), true);
153 }
154
155
156 protected function isVisibleRoleUserRoles(ilObjUser $ilObjUser): bool
157 {
158 if (!$this->isLimitToRoles()) {
159 return true;
160 }
161 global $DIC;
162
163 if ($ilObjUser->getId() === 0 && in_array(0, $this->getLimitedToRoleIds())) {
164 return true;
165 }
166
167 return $DIC->rbac()->review()->isAssignedToAtLeastOneGivenRole(
168 $ilObjUser->getId(),
169 $this->getLimitedToRoleIds()
170 );
171 }
172
181 protected ?int $id = null;
187 protected string $title = '';
192 protected string $body = '';
198 protected ?\DateTimeImmutable $event_start = null;
204 protected ?\DateTimeImmutable $event_end = null;
210 protected ?\DateTimeImmutable $display_start = null;
216 protected ?\DateTimeImmutable $display_end = null;
222 protected int $type = self::TYPE_INFO;
234 protected bool $dismissable = true;
240 protected bool $permanent = true;
246 protected array $allowed_users = [0, 6, 13];
252 protected ?int $parent_id = null;
258 protected ?\DateTimeImmutable $create_date = null;
264 protected \DateTimeImmutable $last_update;
270 protected ?int $created_by = null;
276 protected ?int $last_update_by = null;
282 protected bool $active = true;
288 protected array $limited_to_languages = [];
294 protected bool $has_language_limitation = false;
300 protected array $limited_to_role_ids = [];
306 protected bool $limit_to_roles = false;
312 protected bool $interruptive = false;
318 protected string $link = '';
330 protected string $link_target = '_top';
331
337 public function wakeUp($field_name, $field_value)
338 {
339 switch ($field_name) {
340 case 'event_start':
341 case 'event_end':
342 case 'display_end':
343 case 'display_start':
344 case 'create_date':
345 case 'last_update':
346 return (new DateTimeImmutable())->setTimestamp((int) $field_value);
347
348 case 'allowed_users':
349 if ($field_value === null) {
350 $array_unique = self::$allowed_user_ids;
351 } else {
352 $json_decode = json_decode((string) $field_value, true);
353 if (!is_array($json_decode)) {
354 $json_decode = self::$allowed_user_ids;
355 }
356 $array_unique = array_unique($json_decode);
357 }
358
359 sort($array_unique);
360
361 return $array_unique;
362 case 'limited_to_languages':
363 case 'limited_to_role_ids':
364 return json_decode((string) $field_value, true);
365 }
366 return null;
367 }
368
373 public function sleep($field_name)
374 {
375 switch ($field_name) {
376 case 'event_start':
377 case 'event_end':
378 case 'display_end':
379 case 'display_start':
380 case 'create_date':
381 case 'last_update':
385 $datetime = $this->{$field_name} ?? new DateTimeImmutable();
386 return $datetime->getTimestamp();
387 case 'allowed_users':
389 foreach ($this->allowed_users as $user_id) {
391 }
392
393 return json_encode(array_unique($allowed_users), JSON_THROW_ON_ERROR);
394 case 'limited_to_languages':
395 case 'limited_to_role_ids':
396 return json_encode($this->{$field_name}, JSON_THROW_ON_ERROR);
397 }
398 return null;
399 }
400
401 #[\Override]
402 public function create(): void
403 {
404 global $DIC;
405 $this->setCreateDate(new DateTimeImmutable());
406 $this->setCreatedBy($DIC->user()->getId());
407 parent::create();
408 }
409
410 public function setBody(string $body): void
411 {
412 $this->body = $body;
413 }
414
415 public function getBody(): string
416 {
417 return $this->body;
418 }
419
420 public function setDisplayEnd(DateTimeImmutable $display_end): void
421 {
422 $this->display_end = $display_end;
423 }
424
425 public function getDisplayEnd(): DateTimeImmutable
426 {
427 return $this->display_end ?? new DateTimeImmutable();
428 }
429
430 public function setDisplayStart(DateTimeImmutable $display_start): void
431 {
432 $this->display_start = $display_start;
433 }
434
435 public function getDisplayStart(): DateTimeImmutable
436 {
437 return $this->display_start ?? new DateTimeImmutable();
438 }
439
440 public function setEventEnd(DateTimeImmutable $event_end): void
441 {
442 $this->event_end = $event_end;
443 }
444
445 public function getEventEnd(): DateTimeImmutable
446 {
447 return $this->event_end ?? new DateTimeImmutable();
448 }
449
450 public function setEventStart(DateTimeImmutable $event_start): void
451 {
452 $this->event_start = $event_start;
453 }
454
455 public function getEventStart(): DateTimeImmutable
456 {
457 return $this->event_start ?? new DateTimeImmutable();
458 }
459
460 public function setId(int $id): void
461 {
462 $this->id = $id;
463 }
464
465 public function getId(): int
466 {
467 return (int) $this->id;
468 }
469
470 public function setTitle(string $title): void
471 {
472 $this->title = $title;
473 }
474
475 public function getTitle(): string
476 {
477 return $this->title;
478 }
479
480 public function setType(int $type): void
481 {
482 $this->type = $type;
483 }
484
485 public function getType(): int
486 {
487 return $this->type;
488 }
489
490 public function setTypeDuringEvent(int $type_during_event): void
491 {
492 $this->type_during_event = $type_during_event;
493 }
494
495 public function getTypeDuringEvent(): int
496 {
497 return in_array(
498 $this->type_during_event,
499 [self::TYPE_WARNING, self::TYPE_ERROR, self::TYPE_INFO]
500 ) ? $this->type_during_event : self::TYPE_INFO;
501 }
502
503 public function setDismissable(bool $dismissable): void
504 {
505 $this->dismissable = $dismissable;
506 }
507
508 public function getDismissable(): bool
509 {
510 return $this->dismissable;
511 }
512
513 protected function hasEventStarted(): bool
514 {
515 return $this->getTime() > $this->getEventStart();
516 }
517
518 protected function hasDisplayStarted(): bool
519 {
520 return $this->getTime() > $this->getDisplayStart();
521 }
522
523 protected function hasEventEnded(): bool
524 {
525 return $this->getTime() > $this->getEventEnd();
526 }
527
528 protected function hasDisplayEnded(): bool
529 {
530 return $this->getTime() > $this->getDisplayEnd();
531 }
532
533 public function setPermanent(bool $permanent): void
534 {
535 $this->permanent = $permanent;
536 }
537
538 public function isPermanent(): bool
539 {
540 return $this->permanent;
541 }
542
543 public function isDuringEvent(): bool
544 {
545 return $this->hasEventStarted() && !$this->hasEventEnded();
546 }
547
548 public function setCreateDate(DateTimeImmutable $create_date): void
549 {
550 $this->create_date = $create_date;
551 }
552
553 public function getCreateDate(): DateTimeImmutable
554 {
555 return $this->create_date ?? new DateTimeImmutable();
556 }
557
558 public function setCreatedBy(int $created_by): void
559 {
560 $this->created_by = $created_by;
561 }
562
563 public function getCreatedBy(): int
564 {
565 return (int) $this->created_by;
566 }
567
568 protected function getTime(): DateTimeImmutable
569 {
570 return new DateTimeImmutable();
571 }
572
573 public function isActive(): bool
574 {
575 return $this->active;
576 }
577
578 public function setActive(bool $active): void
579 {
580 $this->active = $active;
581 }
582
583 public function getLimitedToLanguages(): array
584 {
586 }
587
588 public function setLimitedToLanguages(array $limited_to_languages): void
589 {
590 $this->limited_to_languages = $limited_to_languages;
591 }
592
593 public function hasLanguageLimitation(): bool
594 {
596 }
597
599 {
600 $this->has_language_limitation = $has_language_limitation;
601 }
602
603 public function getLimitedToRoleIds(): array
604 {
606 }
607
608 public function setLimitedToRoleIds(array $limited_to_role_ids): void
609 {
610 $this->limited_to_role_ids = $limited_to_role_ids;
611 }
612
613 public function isLimitToRoles(): bool
614 {
616 }
617
618 public function setLimitToRoles(bool $limit_to_roles): void
619 {
620 $this->limit_to_roles = $limit_to_roles;
621 }
622}
$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)
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)
isVisibleToUserBasedOnLanguage(ilObjUser $ilObjUser)
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