ILIAS  release_8 Revision v8.24
ilNotificationOSDRepository.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
22
31use ilLanguage;
33
38{
39 private const UNIQUE_TYPES = [
40 'who_is_online'
41 ];
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
54 {
56 $base = $object->baseNotification;
57 $now = time();
58
59 $notification = new ilOSDNotificationObject(
60 $id,
61 $user_id,
62 $object,
63 $now,
64 $base->getValidForSeconds() ? $base->getValidForSeconds() + $now : 0,
65 $base->getVisibleForSeconds(),
66 $base->getType(),
67 $base->getIdentification()
68 );
69
70 if (in_array($notification->getType(), self::UNIQUE_TYPES)) {
71 $this->deleteOSDNotificationByUserAndType($user_id, $notification->getType());
72 }
73
74 $affected = $this->database->insert(
76 [
77 'notification_osd_id' => [ilDBConstants::T_INTEGER, $notification->getId()],
78 'usr_id' => [ilDBConstants::T_INTEGER, $notification->getUser()],
79 'serialized' => [ilDBConstants::T_TEXT, serialize($notification->getObject())],
80 'valid_until' => [ilDBConstants::T_INTEGER, $notification->getValidUntil()],
81 'visible_for' => [ilDBConstants::T_INTEGER, $notification->getVisibleFor()],
82 'type' => [ilDBConstants::T_TEXT, $notification->getType()],
83 'time_added' => [ilDBConstants::T_INTEGER, $notification->getTimeAdded()],
84 'identification' => [ilDBConstants::T_TEXT, (string) $notification->getIdentification()]
85 ]
86 );
87
88 return ($affected === 1) ? $notification : null;
89 }
90
91 public function ifOSDNotificationExistsById(int $id): bool
92 {
93 $query = 'SELECT count(*) AS count FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE notification_osd_id = %s';
94 $result = $this->database->queryF($query, [ilDBConstants::T_INTEGER], [$id]);
95 $row = $this->database->fetchAssoc($result);
96 return ((int) ($row['count'] ?? 0)) === 1;
97 }
98
102 public function getOSDNotificationsByUser(int $user_id, int $max_age_seconds = 0, string $type = ''): array
103 {
104 $now = time();
105 if ($max_age_seconds === 0) {
106 $max_age_seconds = $now;
107 }
108 $query =
110 ' WHERE usr_id = %s AND (valid_until = 0 OR valid_until > %s) AND time_added > %s';
111
113 $values = [$user_id, $now, $now - $max_age_seconds];
114
115 if ($type !== '') {
116 $query .= ' AND type = %s';
117 $types[] = ilDBConstants::T_TEXT;
118 $values[] = $type;
119 }
120
121 $rset = $this->database->queryF($query, $types, $values);
122 $notifications = [];
123
124 while ($row = $this->database->fetchAssoc($rset)) {
125 $object = unserialize($row['serialized'], ['allowed_classes' => [ilNotificationObject::class, ilNotificationLink::class]]);
126 if (isset($object->handlerParams[''], $object->handlerParams['osd'])) {
127 $object->handlerParams = ['general' => $object->handlerParams[''], 'osd' => $object->handlerParams['osd']];
128 }
129 $notification = new ilOSDNotificationObject(
130 (int) $row['notification_osd_id'],
131 (int) $row['usr_id'],
132 $object,
133 (int) $row['time_added'],
134 (int) $row['valid_until'],
135 (int) $row['visible_for'],
136 $row['type'],
137 new NotificationIdentification($row['type'], $row['identification'])
138 );
139
140 $notifications[] = $notification;
141 }
142
143 return $notifications;
144 }
145
146 public function deleteOSDNotificationById(int $id): bool
147 {
148 if ($this->ifOSDNotificationExistsById($id)) {
149 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE notification_osd_id = %s';
150 return 1 === $this->database->manipulateF($query, [ilDBConstants::T_INTEGER], [$id]);
151 }
152 return false;
153 }
154
155 private function deleteOSDNotificationByUserAndType(int $user_id, string $type): void
156 {
157 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE usr_id = %s AND type = %s';
158 $this->database->manipulateF(
159 $query,
161 [$user_id, $type]
162 );
163 }
164
165 public function deleteOSDNotificationByIdentification(string $povider_type, string $identification, int $user_id = 0): bool
166 {
167 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE type = %s AND identification = %s';
169 $values = [$povider_type, $identification];
170 if ($user_id > 0) {
171 $query .= ' AND user_id = %s';
173 $values[] = $user_id;
174 }
175 return (1 === $this->database->manipulateF($query, $keys, $values));
176 }
177
178 public function deleteStaleOSDNotificationsForUserAndType(string $povider_type, int $user_id, int $until_timestamp): void
179 {
180 $query = 'DELETE FROM ' . ilNotificationSetupHelper::$tbl_notification_osd_handler . ' WHERE type = %s AND usr_id = %s AND time_added < %s';
181 $this->database->manipulateF(
182 $query,
184 [$povider_type, $user_id, $until_timestamp]
185 );
186 }
187
188 public function deleteAllOSDNotifications(): void
189 {
190 $this->database->manipulate('TRUNCATE TABLE ' . ilNotificationSetupHelper::$tbl_notification_osd_handler);
191 }
192}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
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)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$keys
Definition: metadata.php:204
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$base
Definition: index.php:4
$query
$type