ILIAS  release_8 Revision v8.24
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->isVisibleRoleUserRoles($ilObjUser);
142 }
143
144
145 protected function isVisibleRoleUserRoles(ilObjUser $ilObjUser): bool
146 {
147 if (!$this->isLimitToRoles()) {
148 return true;
149 }
150 global $DIC;
151
152 if ($ilObjUser->getId() === 0 && in_array(0, $this->getLimitedToRoleIds())) {
153 return true;
154 }
155
156 return $DIC->rbac()->review()->isAssignedToAtLeastOneGivenRole(
157 $ilObjUser->getId(),
158 $this->getLimitedToRoleIds()
159 );
160 }
161
170 protected ?int $id = null;
176 protected string $title = '';
181 protected string $body = '';
187 protected ?\DateTimeImmutable $event_start = null;
193 protected ?\DateTimeImmutable $event_end = null;
199 protected ?\DateTimeImmutable $display_start = null;
205 protected ?\DateTimeImmutable $display_end = null;
211 protected int $type = self::TYPE_INFO;
223 protected bool $dismissable = true;
229 protected bool $permanent = true;
235 protected array $allowed_users = array(0, 6, 13);
241 protected ?int $parent_id = null;
247 protected ?\DateTimeImmutable $create_date = null;
253 protected \DateTimeImmutable $last_update;
259 protected ?int $created_by = null;
265 protected ?int $last_update_by = null;
271 protected bool $active = true;
277 protected array $limited_to_role_ids = [];
283 protected bool $limit_to_roles = false;
289 protected bool $interruptive = false;
295 protected string $link = '';
307 protected string $link_target = '_top';
308
314 public function wakeUp($field_name, $field_value)
315 {
316 switch ($field_name) {
317 case 'event_start':
318 case 'event_end':
319 case 'display_end':
320 case 'display_start':
321 case 'create_date':
322 case 'last_update':
323 return (new DateTimeImmutable())->setTimestamp((int) $field_value);
324
325 case 'allowed_users':
326 if ($field_value === null) {
327 $array_unique = self::$allowed_user_ids;
328 } else {
329 $json_decode = json_decode($field_value, true);
330 if (!is_array($json_decode)) {
331 $json_decode = self::$allowed_user_ids;
332 }
333 $array_unique = array_unique($json_decode);
334 }
335
336 sort($array_unique);
337
338 return $array_unique;
339 case 'limited_to_role_ids':
340 return json_decode($field_value, true);
341 }
342 }
343
348 public function sleep($field_name)
349 {
350 switch ($field_name) {
351 case 'event_start':
352 case 'event_end':
353 case 'display_end':
354 case 'display_start':
355 case 'create_date':
356 case 'last_update':
360 $datetime = $this->{$field_name} ?? new DateTimeImmutable();
361 return $datetime->getTimestamp();
362 case 'allowed_users':
364 foreach ($this->allowed_users as $user_id) {
365 $allowed_users[] = (int) $user_id;
366 }
367
368 return json_encode(array_unique($allowed_users), JSON_THROW_ON_ERROR);
369 case 'limited_to_role_ids':
370 return json_encode($this->{$field_name}, JSON_THROW_ON_ERROR);
371 }
372 }
373
374 public function create(): void
375 {
376 global $DIC;
377 $this->setCreateDate(new DateTimeImmutable());
378 $this->setCreatedBy($DIC->user()->getId());
379 parent::create();
380 }
381
382 public function setBody(string $body): void
383 {
384 $this->body = $body;
385 }
386
387 public function getBody(): string
388 {
389 return $this->body;
390 }
391
392 public function setDisplayEnd(DateTimeImmutable $display_end): void
393 {
394 $this->display_end = $display_end;
395 }
396
397 public function getDisplayEnd(): DateTimeImmutable
398 {
399 return $this->display_end ?? new DateTimeImmutable();
400 }
401
402 public function setDisplayStart(DateTimeImmutable $display_start): void
403 {
404 $this->display_start = $display_start;
405 }
406
407 public function getDisplayStart(): DateTimeImmutable
408 {
409 return $this->display_start ?? new DateTimeImmutable();
410 }
411
412 public function setEventEnd(DateTimeImmutable $event_end): void
413 {
414 $this->event_end = $event_end;
415 }
416
417 public function getEventEnd(): DateTimeImmutable
418 {
419 return $this->event_end ?? new DateTimeImmutable();
420 }
421
422 public function setEventStart(DateTimeImmutable $event_start): void
423 {
424 $this->event_start = $event_start;
425 }
426
427 public function getEventStart(): DateTimeImmutable
428 {
429 return $this->event_start ?? new DateTimeImmutable();
430 }
431
432 public function setId(int $id): void
433 {
434 $this->id = $id;
435 }
436
437 public function getId(): int
438 {
439 return (int) $this->id;
440 }
441
442 public function setTitle(string $title): void
443 {
444 $this->title = $title;
445 }
446
447 public function getTitle(): string
448 {
449 return $this->title;
450 }
451
452 public function setType(int $type): void
453 {
454 $this->type = $type;
455 }
456
457 public function getType(): int
458 {
459 return $this->type;
460 }
461
462 public function setTypeDuringEvent(int $type_during_event): void
463 {
464 $this->type_during_event = $type_during_event;
465 }
466
467 public function getTypeDuringEvent(): int
468 {
469 return in_array(
470 $this->type_during_event,
471 [self::TYPE_WARNING, self::TYPE_ERROR, self::TYPE_INFO]
472 ) ? $this->type_during_event : self::TYPE_INFO;
473 }
474
475 public function setDismissable(bool $dismissable): void
476 {
477 $this->dismissable = $dismissable;
478 }
479
480 public function getDismissable(): bool
481 {
482 return $this->dismissable;
483 }
484
485 protected function hasEventStarted(): bool
486 {
487 return $this->getTime() > $this->getEventStart();
488 }
489
490 protected function hasDisplayStarted(): bool
491 {
492 return $this->getTime() > $this->getDisplayStart();
493 }
494
495 protected function hasEventEnded(): bool
496 {
497 return $this->getTime() > $this->getEventEnd();
498 }
499
500 protected function hasDisplayEnded(): bool
501 {
502 return $this->getTime() > $this->getDisplayEnd();
503 }
504
505 public function setPermanent(bool $permanent): void
506 {
507 $this->permanent = $permanent;
508 }
509
510 public function isPermanent(): bool
511 {
512 return $this->permanent;
513 }
514
515 public function isDuringEvent(): bool
516 {
517 return $this->hasEventStarted() && !$this->hasEventEnded();
518 }
519
520 public function setCreateDate(DateTimeImmutable $create_date): void
521 {
522 $this->create_date = $create_date;
523 }
524
525 public function getCreateDate(): DateTimeImmutable
526 {
527 return $this->create_date ?? new DateTimeImmutable();
528 }
529
530 public function setCreatedBy(int $created_by): void
531 {
532 $this->created_by = $created_by;
533 }
534
535 public function getCreatedBy(): int
536 {
537 return (int) $this->created_by;
538 }
539
540 protected function getTime(): DateTimeImmutable
541 {
542 return new DateTimeImmutable();
543 }
544
545 public function isActive(): bool
546 {
547 return $this->active;
548 }
549
550 public function setActive(bool $active): void
551 {
552 $this->active = $active;
553 }
554
555 public function getLimitedToRoleIds(): array
556 {
558 }
559
560 public function setLimitedToRoleIds(array $limited_to_role_ids): void
561 {
562 $this->limited_to_role_ids = $limited_to_role_ids;
563 }
564
565 public function isLimitToRoles(): bool
566 {
568 }
569
570 public function setLimitToRoles(bool $limit_to_roles): void
571 {
572 $this->limit_to_roles = $limit_to_roles;
573 }
574}
Class ActiveRecord.
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
string $link_target
@con_has_field true @con_fieldtype text @con_length 256
setCreateDate(DateTimeImmutable $create_date)
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
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)
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)
User class.
const ANONYMOUS_USER_ID
Definition: constants.php:27
global $DIC
Definition: feed.php:28