ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilNotificationOSDHandler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Notifications;
22
27
32{
34 private readonly ClockInterface $clock;
35
37 {
38 if ($repo === null) {
40 }
41 $this->repo = $repo;
42
43 if ($clock === null) {
44 $clock = (new \ILIAS\Data\Factory())->clock()->utc();
45 }
46 $this->clock = $clock;
47 }
48
49 public function notify(ilNotificationObject $notification): void
50 {
51 $this->repo->createOSDNotification($notification->user->getId(), $notification);
52 }
53
58 int $user_id,
59 bool $append_osd_id_to_link = true,
60 int $max_age_seconds = 0,
61 string $type = ''
62 ): array {
63 $notifications = $this->repo->getOSDNotificationsByUser($user_id, $max_age_seconds, $type);
64
65 foreach ($notifications as $notification) {
66 if ($append_osd_id_to_link) {
67 foreach ($notification->getObject()->links as $link) {
68 $link->setUrl($this->appendParamToLink($link->getUrl(), 'osd_id', $notification->getId()));
69 }
70 }
71 }
72
73 return $notifications;
74 }
75
79 public function deleteStaleNotificationsForUserAndType(int $user_id, string $type): void
80 {
81 $this->repo->deleteStaleOSDNotificationsForUserAndType($type, $user_id, $this->clock->now()->getTimestamp());
82 }
83
84 public function deleteStaleOSDNotificationsForUser(string $provider, int $user_id): void
85 {
86 $this->repo->deleteStaleOSDNotificationsForUserAndType($provider, $user_id, $this->clock->now()->getTimestamp());
87 }
88
89 public function removeOSDNotification(int $notification_osd_id): bool
90 {
91 return $this->repo->deleteOSDNotificationById($notification_osd_id);
92 }
93
94 public function removeOSDNotificationByIdentification(string $provider, string $identification, int $user_id = 0): bool
95 {
96 return $this->repo->deleteOSDNotificationByIdentification(
98 $identification,
100 );
101 }
102
103 private function appendParamToLink(string $link, string $param, int $value): string
104 {
105 if (str_contains($link, '?')) {
106 $link .= '&' . $param . '=' . $value;
107 } else {
108 $link .= '?' . $param . '=' . $value;
109 }
110
111 return $link;
112 }
113
114 public function clear(): void
115 {
116 $this->repo->deleteAllOSDNotifications();
117 }
118}
readonly ilNotificationOSDRepository $repo
getOSDNotificationsForUser(int $user_id, bool $append_osd_id_to_link=true, int $max_age_seconds=0, string $type='')
deleteStaleNotificationsForUserAndType(int $user_id, string $type)
__construct(?ilNotificationOSDRepository $repo=null, ?ClockInterface $clock=null)
removeOSDNotificationByIdentification(string $provider, string $identification, int $user_id=0)
deleteStaleOSDNotificationsForUser(string $provider, int $user_id)
appendParamToLink(string $link, string $param, int $value)
$provider
Definition: ltitoken.php:80
$param
Definition: xapitoken.php:46