ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilADNNotification.php
Go to the documentation of this file.
1<?php
9{
10
11 const POS_TOP = 1;
12 const POS_RIGHT = 2;
13 const POST_LEFT = 3;
14 const POS_BOTTOM = 4;
15 const DATE_FORMAT = 'd.m.Y';
16 const TIME_FORMAT = 'H:i';
17 const DATE_TIME_FORMAT = 'd.m.Y H:i';
18 const TYPE_INFO = 1;
19 const TYPE_WARNING = 2;
20 const TYPE_ERROR = 3;
21 const TABLE_NAME = 'il_adn_notifications';
22 const LINK_TYPE_NONE = 0;
24 const LINK_TYPE_URL = 2;
28 protected static $allowed_user_ids = array(0, 13, 6);
29
33 public function getConnectorContainerName()
34 {
35 return self::TABLE_NAME;
36 }
37
42 public static function returnDbTableName()
43 {
44 return self::TABLE_NAME;
45 }
46
50 public function dismiss(ilObjUser $ilObjUser)
51 {
52 if ($this->isUserAllowedToDismiss($ilObjUser)) {
53 ilADNDismiss::dismiss($ilObjUser, $this);
54 }
55 }
56
61 protected function hasUserDismissed(ilObjUser $ilObjUser)
62 {
63 if (!$this->getDismissable()) {
64 return false;
65 }
66
67 return ilADNDismiss::hasDimissed($ilObjUser, $this);
68 }
69
70 public function resetForAllUsers()
71 {
72 foreach (ilADNDismiss::where(array('notification_id' => $this->getId()))->get() as $not) {
73 $not->delete();
74 }
75 }
76
80 public function getFullTimeFormated() : string
81 {
82 if ($this->getEventStart() == 0 && $this->getEventEnd() == 0) {
83 return '';
84 }
85 if (date(self::DATE_FORMAT, $this->getEventStart()) == date(self::DATE_FORMAT, $this->getEventEnd())) {
86 return date(self::DATE_FORMAT, $this->getEventEnd()) . ', ' . date(self::TIME_FORMAT,
87 $this->getEventStart()) . " - "
88 . date(self::TIME_FORMAT, $this->getEventEnd());
89 } else {
90 return date(self::DATE_TIME_FORMAT, $this->getEventStart()) . ' - ' . date(self::DATE_TIME_FORMAT,
91 $this->getEventEnd());
92 }
93 }
94
100 {
101 return ($this->getDismissable() and $ilUser->getId() != 0 and $ilUser->getId() != ANONYMOUS_USER_ID);
102 }
103
107 public function getActiveType()
108 {
109 if ($this->isPermanent()) {
110 return $this->getType();
111 }
112 if ($this->hasEventStarted() and !$this->hasEventEnded()) {
113 return $this->getTypeDuringEvent();
114 }
115 if ($this->hasDisplayStarted() and !$this->hasDisplayEnded()) {
116 return $this->getType();
117 }
118 }
119
123 protected function isVisible()
124 {
125 if ($this->isPermanent()) {
126 return true;
127 }
128 $hasEventStarted = $this->hasEventStarted();
129 $hasDisplayStarted = $this->hasDisplayStarted();
130 $hasEventEnded = !$this->hasEventEnded();
131 $hasDisplayEnded = !$this->hasDisplayEnded();
132
133 return ($hasEventStarted or $hasDisplayStarted) and ($hasEventEnded or $hasDisplayEnded);
134 }
135
140 public function isVisibleForUser(ilObjUser $ilObjUser)
141 {
142 if ($ilObjUser->getId() == 0 && $this->isInterruptive()) {
143 return false;
144 }
145 if (!$this->isVisible()) {
146
147 return false;
148 }
149 if ($this->hasUserDismissed($ilObjUser)) {
150 return false;
151 }
152 if (!$this->isVisibleRoleUserRoles($ilObjUser)) {
153 return false;
154 }
155
156 return true;
157 }
158
163 protected function isVisibleRoleUserRoles(ilObjUser $ilObjUser)
164 {
165 if (!$this->isLimitToRoles()) {
166 return true;
167 }
168 global $DIC;
169
170 if ($ilObjUser->getId() === 0 && in_array(0, $this->getLimitedToRoleIds())) {
171 return true;
172 }
173
174 return $DIC->rbac()->review()->isAssignedToAtLeastOneGivenRole($ilObjUser->getId(),
175 $this->getLimitedToRoleIds());
176 }
177
187 protected $id;
194 protected $title = '';
200 protected $body = '';
207 protected $event_start;
214 protected $event_end;
221 protected $display_start;
228 protected $display_end;
249 protected $dismissable = true;
256 protected $permanent = true;
263 protected $allowed_users = array(0, 6, 13);
270 protected $parent_id = null;
277 protected $create_date;
284 protected $last_update;
291 protected $created_by = null;
298 protected $last_update_by = null;
305 protected $active = true;
312 protected $limited_to_role_ids = [];
319 protected $limit_to_roles = false;
326 protected $interruptive = false;
333 protected $link = '';
347 protected $link_target = '_top';
348
354 public function wakeUp($field_name, $field_value)
355 {
356 switch ($field_name) {
357 case 'event_start':
358 case 'event_end':
359 case 'display_end':
360 case 'display_start':
361 case 'create_date':
362 case 'last_update':
363 return (new DateTimeImmutable())->setTimestamp((int) $field_value);
364
365 case 'allowed_users':
366 if ($field_value === null) {
367 $array_unique = self::$allowed_user_ids;
368 } else {
369 $json_decode = json_decode($field_value, true);
370 if (!is_array($json_decode)) {
371 $json_decode = self::$allowed_user_ids;
372 }
373 $array_unique = array_unique($json_decode);
374 }
375
376 sort($array_unique);
377
378 return $array_unique;
379 break;
380 case 'limited_to_role_ids':
381 return json_decode($field_value, true);
382 break;
383 }
384 }
385
390 public function sleep($field_name)
391 {
392 switch ($field_name) {
393 case 'event_start':
394 case 'event_end':
395 case 'display_end':
396 case 'display_start':
397 case 'create_date':
398 case 'last_update':
402 $datetime = $this->{$field_name} ?? new DateTimeImmutable();
403 return $datetime->getTimestamp();
404 break;
405 case 'allowed_users':
407 foreach ($this->allowed_users as $user_id) {
408 $allowed_users[] = (int) $user_id;
409 }
410
411 return json_encode(array_unique($allowed_users));
412 break;
413 case 'limited_to_role_ids':
414 return json_encode($this->{$field_name});
415 break;
416 }
417 }
418
419 public function create()
420 {
421 global $DIC;
422 $this->setCreateDate(new DateTimeImmutable());
423 $this->setCreatedBy($DIC->user()->getId());
424 parent::create();
425 }
426
427 public function setBody(string $body) : void
428 {
429 $this->body = $body;
430 }
431
432 public function getBody() : string
433 {
434 return (string) $this->body;
435 }
436
437 public function setDisplayEnd(DateTimeImmutable $display_end) : void
438 {
439 $this->display_end = $display_end;
440 }
441
442 public function getDisplayEnd() : DateTimeImmutable
443 {
444 return $this->display_end ?? new DateTimeImmutable();
445 }
446
447 public function setDisplayStart(DateTimeImmutable $display_start) : void
448 {
449 $this->display_start = $display_start;
450 }
451
452 public function getDisplayStart() : DateTimeImmutable
453 {
454 return $this->display_start ?? new DateTimeImmutable();
455 }
456
457 public function setEventEnd(DateTimeImmutable $event_end) : void
458 {
459 $this->event_end = $event_end;
460 }
461
462 public function getEventEnd() : DateTimeImmutable
463 {
464 return $this->event_end ?? new DateTimeImmutable();
465 }
466
467 public function setEventStart(DateTimeImmutable $event_start) : void
468 {
469 $this->event_start = $event_start;
470 }
471
472 public function getEventStart() : DateTimeImmutable
473 {
474 return $this->event_start ?? new DateTimeImmutable();
475 }
476
477 public function setId(int $id) : void
478 {
479 $this->id = $id;
480 }
481
482 public function getId() : int
483 {
484 return (int) $this->id;
485 }
486
487 public function setTitle(string $title) : void
488 {
489 $this->title = $title;
490 }
491
492 public function getTitle() : string
493 {
494 return (string) $this->title;
495 }
496
497 public function setType(int $type) : void
498 {
499 $this->type = $type;
500 }
501
502 public function getType() : int
503 {
504 return (int) $this->type;
505 }
506
507 public function setTypeDuringEvent(int $type_during_event) : void
508 {
509 $this->type_during_event = $type_during_event;
510 }
511
512 public function getTypeDuringEvent() : int
513 {
514 return (int) $this->type_during_event;
515 }
516
517 public function setDismissable(bool $dismissable) : void
518 {
519 $this->dismissable = $dismissable;
520 }
521
522 public function getDismissable() : bool
523 {
524 return (bool) $this->dismissable;
525 }
526
527 protected function hasEventStarted() : bool
528 {
529 return $this->getTime() > $this->getEventStart();
530 }
531
532 protected function hasDisplayStarted() : bool
533 {
534 return $this->getTime() > $this->getDisplayStart();
535 }
536
537 protected function hasEventEnded() : bool
538 {
539 return $this->getTime() > $this->getEventEnd();
540 }
541
542 protected function hasDisplayEnded() : bool
543 {
544 return $this->getTime() > $this->getDisplayEnd();
545 }
546
547 public function setPermanent(bool $permanent) : void
548 {
549 $this->permanent = $permanent;
550 }
551
552 public function isPermanent() : bool
553 {
554 return (bool) $this->permanent;
555 }
556
557 public function isDuringEvent() : bool
558 {
559 return $this->hasEventStarted() && !$this->hasEventEnded();
560 }
561
562 public function setCreateDate(DateTimeImmutable $create_date) : void
563 {
564 $this->create_date = $create_date;
565 }
566
567 public function getCreateDate() : DateTimeImmutable
568 {
569 return $this->create_date ?? new DateTimeImmutable();
570 }
571
572 public function setCreatedBy(int $created_by) : void
573 {
574 $this->created_by = $created_by;
575 }
576
577 public function getCreatedBy() : int
578 {
579 return (int) $this->created_by;
580 }
581
582 protected function getTime() : DateTimeImmutable
583 {
584 return new DateTimeImmutable();
585 }
586
587 public function isActive() : bool
588 {
589 return $this->active;
590 }
591
592 public function setActive(bool $active) : void
593 {
594 $this->active = $active;
595 }
596
597 public function getLimitedToRoleIds() : array
598 {
599 return (array) $this->limited_to_role_ids;
600 }
601
602 public function setLimitedToRoleIds(array $limited_to_role_ids) : void
603 {
604 $this->limited_to_role_ids = $limited_to_role_ids;
605 }
606
607 public function isLimitToRoles() : bool
608 {
609 return (bool) $this->limit_to_roles;
610 }
611
612 public function setLimitToRoles(bool $limit_to_roles) : void
613 {
614 $this->limit_to_roles = $limit_to_roles;
615 }
616
617}
Class ActiveRecord.
static where($where, $operator=null)
sleep($field_name)
An exception for terminatinating execution or to throw for unit testing.
static dismiss(ilObjUser $ilObjUser, ilADNNotification $ilADNNotification)
static hasDimissed(ilObjUser $ilObjUser, ilADNNotification $ilADNNotification)
setCreateDate(DateTimeImmutable $create_date)
setEventEnd(DateTimeImmutable $event_end)
isUserAllowedToDismiss(ilObjUser $ilUser)
hasUserDismissed(ilObjUser $ilObjUser)
dismiss(ilObjUser $ilObjUser)
setPermanent(bool $permanent)
isVisibleForUser(ilObjUser $ilObjUser)
setDismissable(bool $dismissable)
setCreatedBy(int $created_by)
setLimitToRoles(bool $limit_to_roles)
setDisplayStart(DateTimeImmutable $display_start)
isVisibleRoleUserRoles(ilObjUser $ilObjUser)
setEventStart(DateTimeImmutable $event_start)
setDisplayEnd(DateTimeImmutable $display_end)
setTypeDuringEvent(int $type_during_event)
setLimitedToRoleIds(array $limited_to_role_ids)
wakeUp($field_name, $field_value)
getId()
get object id @access public
const ANONYMOUS_USER_ID
Definition: constants.php:25
global $DIC
Definition: goto.php:24
$ilUser
Definition: imgupload.php:18