ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilNotificationSystem.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Notifications;
22
25use ilObjUser;
26use ilRbacReview;
27use ilLogger;
28
33{
35 private array $handler = [];
36 private string $defaultLanguage = 'en';
37 private readonly ilRbacReview $rbacReview;
38 private readonly ilLogger $logger;
39
40 public function __construct(?ilRbacReview $rbacReview = null, ?ilLogger $logger = null)
41 {
42 $this->addHandler('osd', new ilNotificationOSDHandler());
43 $this->addHandler('mail', new ilNotificationMailHandler());
44
45 if ($rbacReview === null) {
46 global $DIC;
47 $rbacReview = $DIC->rbac()->review();
48 }
49 $this->rbacReview = $rbacReview;
50
51 if ($logger === null) {
52 global $DIC;
53 $logger = $DIC->logger()->nota();
54 }
55 $this->logger = $logger;
56 }
57
58 private function addHandler(string $channel, ilNotificationHandler $handler): void
59 {
60 if (!array_key_exists($channel, $this->handler)) {
61 $this->handler[$channel] = [];
62 }
63
64 $this->handler[$channel][] = $handler;
65 }
66
70 public function toUsers(ilNotificationConfig $notification, array $users, bool $process_async = false): void
71 {
72 $this->logger->debug(
73 'Sending notification to users {users}: {notification_type} / {id} / {id_type}',
74 [
75 'users' => $users,
76 'notification_type' => $notification->getType(),
77 'id' => (string) $notification->getIdentification(),
78 'id_type' => $notification->getIdentification()->getType(),
79 ]
80 );
81
82 if ($process_async === false) {
84 $usersWithCustomConfig = ilNotificationDatabaseHandler::getUsersWithCustomConfig($users);
88 $notification->getLanguageParameters()
89 );
90
91 $user_by_handler = [];
92 if (isset($types[$notification->getType()]['config_type'])) {
93 if ($types[$notification->getType()]['config_type'] === 'set_by_user') {
94 $it = new ilNotificationUserIterator($notification->getType(), $users);
95 $channelsByAdmin = false;
96 foreach ($it as $usr_id => $data) {
97 if (!isset($channels[$data['channel']])) {
98 continue;
99 }
100 if (!isset($user_by_handler[$data['channel']])) {
101 $user_by_handler[$data['channel']] = [];
102 }
103 $user_by_handler[$data['channel']][] = $usr_id;
104 }
105 } elseif ($types[$notification->getType()]['config_type'] !== 'disabled') {
106 $channelsByAdmin = true;
107 if (isset($adminConfig[$notification->getType()])) {
108 foreach ($adminConfig[$notification->getType()] as $channel) {
109 if (!isset($channels[$channel]) || !$channels[$channel]) {
110 continue;
111 }
112 $user_by_handler[$channel] = $users;
113 }
114 }
115 }
116 }
117
118 $this->logger->debug(
119 'User by handler: {user_by_handler}',
120 [
121 'user_by_handler' => $user_by_handler
122 ]
123 );
124
125 $userCache = [];
126
127 foreach ($user_by_handler as $handler => $h_users) {
128 $handler = $this->handler[$handler];
129 foreach ($h_users as $userId) {
130 if (!isset($userCache[$userId])) {
131 $user = ilObjectFactory::getInstanceByObjId($userId, false);
132 if (!($user instanceof ilObjUser)) {
133 continue;
134 }
135 $userCache[$userId] = $user;
136 }
137 $user = $userCache[$userId];
138
139 $instance = $notification->getUserInstance($user, $lang, $this->defaultLanguage);
140 foreach ($handler as $h) {
141 $this->logger->debug(
142 'Notify {user} by calling handler {handler}',
143 [
144 'user' => $user->getId(),
145 'handler' => get_class($h),
146 ]
147 );
148 $h->notify($instance);
149 }
150 }
151 }
152 } else {
154 }
155 }
156
157 private function toListeners(ilNotificationConfig $notification, int $ref_id, bool $process_async = false): void
158 {
159 if ($process_async === false) {
161 if ($notification->hasDisableAfterDeliverySet()) {
163 }
164 } else {
166 }
167 }
168
172 private function toRoles(ilNotificationConfig $notification, array $roles, bool $process_async = false): void
173 {
174 $users = [];
175 foreach ($roles as $role) {
176 $users[] = $this->rbacReview->assignedUsers($role);
177 }
178 $users = array_unique(array_merge(...$users));
179
180 $this->toUsers($notification, $users, $process_async);
181 }
182
187 public static function sendNotificationToUsers(
188 ilNotificationConfig $notification,
189 array $users,
190 bool $processAsync = false
191 ): void {
192 global $DIC;
193 $DIC->notifications()->system()->toUsers($notification, $users, $processAsync);
194 }
195
196 public static function sendNotificationToListeners(
197 ilNotificationConfig $notification,
198 int $ref_id,
199 bool $processAsync = false
200 ): void {
201 global $DIC;
202 $DIC->notifications()->system()->toListeners($notification, $ref_id, $processAsync);
203 }
204
208 public static function sendNotificationToRoles(
209 ilNotificationConfig $notification,
210 array $roles,
211 bool $processAsync = false
212 ): void {
213 global $DIC;
214 $DIC->notifications()->system()->toRoles($notification, $roles, $processAsync);
215 }
216
217 public static function enableListeners(string $module, int $ref_id): void
218 {
219 ilNotificationDatabaseHandler::enableListeners($module, $ref_id);
220 }
221
225 public static function enableUserListeners(string $module, int $ref_id, array $users): void
226 {
227 if ($users) {
228 ilNotificationDatabaseHandler::enableListeners($module, $ref_id, $users);
229 }
230 }
231
232 public function clear(string $channel = ''): void
233 {
234 $channels = $this->handler;
235 if ($channel !== '') {
236 $channels = [$this->handler[$channel]] ?? [];
237 }
238 foreach ($channels as $c) {
239 foreach ($c as $handler) {
240 $handler->clear();
241 }
242 }
243 }
244}
getUserInstance(ilObjUser $user, array $languageVars, string $defaultLanguage)
static getAvailableChannels(array $config_types=[], bool $include_disabled=false)
static enqueueByListener(ilNotificationConfig $notification, int $ref_id)
static enqueueByUsers(ilNotificationConfig $notification, array $usr_ids)
static sendNotificationToUsers(ilNotificationConfig $notification, array $users, bool $processAsync=false)
toListeners(ilNotificationConfig $notification, int $ref_id, bool $process_async=false)
static sendNotificationToRoles(ilNotificationConfig $notification, array $roles, bool $processAsync=false)
static sendNotificationToListeners(ilNotificationConfig $notification, int $ref_id, bool $processAsync=false)
__construct(?ilRbacReview $rbacReview=null, ?ilLogger $logger=null)
static enableUserListeners(string $module, int $ref_id, array $users)
toRoles(ilNotificationConfig $notification, array $roles, bool $process_async=false)
addHandler(string $channel, ilNotificationHandler $handler)
static enableListeners(string $module, int $ref_id)
toUsers(ilNotificationConfig $notification, array $users, bool $process_async=false)
Component logger with individual log levels by component id.
User class.
Class ilObjectFactory This class offers methods to get instances of the type-specific object classes ...
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
class ilRbacReview Contains Review functions of core Rbac.
$c
Definition: deliver.php:25
$ref_id
Definition: ltiauth.php:66
$handler
Definition: oai.php:29
global $DIC
Definition: shib_login.php:26
$lang
Definition: xapiexit.php:25