ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilNotificationOSDRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
32
37{
38 private const UNIQUE_TYPES = [
39 'who_is_online'
40 ];
41
42 private readonly ilDBInterface $database;
43
44 public function __construct(?ilDBInterface $database = null)
45 {
46 if ($database === null) {
47 global $DIC;
48 $database = $DIC->database();
49 }
50 $this->database = $database;
51 }
52
53 private function getCurrentUnixTimestamp(): int
54 {
55 return time(); // Should be replace by the consumption of the `Clock` interface
56 }
57
59 {
61 $base = $object->baseNotification;
62 $now = $this->getCurrentUnixTimestamp();
63
64 $notification = new ilOSDNotificationObject(
65 $id,
67 $object,
68 $now,
69 $base->getValidForSeconds() ? $base->getValidForSeconds() + $now : 0,
70 $base->getVisibleForSeconds(),
71 $base->getType(),
72 $base->getIdentification()
73 );
74
75 if (in_array($notification->getType(), self::UNIQUE_TYPES)) {
76 $this->deleteOSDNotificationByUserAndType($user_id, $notification->getType());
77 }
78
79 $affected = $this->database->insert(
81 [
82 'notification_osd_id' => [ilDBConstants::T_INTEGER, $notification->getId()],
83 'usr_id' => [ilDBConstants::T_INTEGER, $notification->getUser()],
84 'serialized' => [ilDBConstants::T_TEXT, serialize($notification->getObject())],
85 'valid_until' => [ilDBConstants::T_INTEGER, $notification->getValidUntil()],
86 'visible_for' => [ilDBConstants::T_INTEGER, $notification->getVisibleFor()],
87 'type' => [ilDBConstants::T_TEXT, $notification->getType()],
88 'time_added' => [ilDBConstants::T_INTEGER, $notification->getTimeAdded()],
89 'identification' => [ilDBConstants::T_TEXT, (string) $notification->getIdentification()]
90 ]
91 );
92
93 return ($affected === 1) ? $notification : null;
94 }
95
96 public function ifOSDNotificationExistsById(int $id): bool
97 {
98 $query = 'SELECT count(*) AS count FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE notification_osd_id = %s';
99 $result = $this->database->queryF($query, [ilDBConstants::T_INTEGER], [$id]);
100 $row = $this->database->fetchAssoc($result);
101
102 return ((int) ($row['count'] ?? 0)) === 1;
103 }
104
105 public function getOSDNotificationsByUser(int $user_id, int $max_age_seconds = 0, string $type = ''): array
106 {
107 $now = $this->getCurrentUnixTimestamp();
108
109 if ($max_age_seconds === 0) {
110 $max_age_seconds = $now;
111 }
112
113 $query =
115 ' WHERE usr_id = %s AND (valid_until = 0 OR valid_until > %s) AND time_added > %s';
116
118 $values = [$user_id, $now, $now - $max_age_seconds];
119
120 if ($type !== '') {
121 $query .= ' AND type = %s';
122 $types[] = ilDBConstants::T_TEXT;
123 $values[] = $type;
124 }
125
126 $rset = $this->database->queryF($query, $types, $values);
127 $notifications = [];
128
129 while ($row = $this->database->fetchAssoc($rset)) {
130 $object = unserialize(
131 $row['serialized'],
132 ['allowed_classes' => [ilNotificationObject::class, ilNotificationLink::class, ilNotificationParameter::class]]
133 );
134 if (isset($object->handlerParams[''], $object->handlerParams['osd'])) {
135 $object->handlerParams = [
136 'general' => $object->handlerParams[''],
137 'osd' => $object->handlerParams['osd']
138 ];
139 }
140 $notification = new ilOSDNotificationObject(
141 (int) $row['notification_osd_id'],
142 (int) $row['usr_id'],
143 $object,
144 (int) $row['time_added'],
145 (int) $row['valid_until'],
146 (int) $row['visible_for'],
147 $row['type'],
148 new NotificationIdentification($row['type'], $row['identification'])
149 );
150
151 $notifications[] = $notification;
152 }
153
154 return $notifications;
155 }
156
157 public function deleteOSDNotificationById(int $id): bool
158 {
159 if ($this->ifOSDNotificationExistsById($id)) {
160 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE notification_osd_id = %s';
161 return 1 === $this->database->manipulateF($query, [ilDBConstants::T_INTEGER], [$id]);
162 }
163
164 return false;
165 }
166
167 private function deleteOSDNotificationByUserAndType(int $user_id, string $type): void
168 {
169 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE usr_id = %s AND type = %s';
170 $this->database->manipulateF(
171 $query,
173 [$user_id, $type]
174 );
175 }
176
177 public function deleteOSDNotificationByIdentification(string $povider_type, string $identification, int $user_id = 0): bool
178 {
179 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE type = %s AND identification = %s';
181 $values = [$povider_type, $identification];
182 if ($user_id > 0) {
183 $query .= ' AND user_id = %s';
184 $keys[] = ilDBConstants::T_INTEGER;
185 $values[] = $user_id;
186 }
187 return (1 === $this->database->manipulateF($query, $keys, $values));
188 }
189
190 public function deleteStaleOSDNotificationsForUserAndType(string $povider_type, int $user_id, int $until_timestamp): void
191 {
192 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE type = %s AND usr_id = %s AND time_added < %s';
193 $this->database->manipulateF(
194 $query,
196 [$povider_type, $user_id, $until_timestamp]
197 );
198 }
199
200 public function deleteAllOSDNotifications(): void
201 {
202 $this->database->manipulate('TRUNCATE TABLE ' . ilNotificationSetupHelper::$tbl_notification_osd_handler);
203 }
204}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
description of a localized parameter this information is used locate translations while processing no...
getOSDNotificationsByUser(int $user_id, int $max_age_seconds=0, string $type='')
createOSDNotification(int $user_id, ilNotificationObject $object)
deleteOSDNotificationByIdentification(string $povider_type, string $identification, int $user_id=0)
deleteStaleOSDNotificationsForUserAndType(string $povider_type, int $user_id, int $until_timestamp)
Class ilDBConstants.
Interface ilDBInterface.
global $DIC
Definition: shib_login.php:26