ILIAS  release_8 Revision v8.24
ilNotificationSystem.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21namespace ILIAS\Notifications;
22
25use ilObjUser;
26use ilRbacReview;
27
32{
36 private array $handler = [];
37 private string $defaultLanguage = 'en';
39
40 public function __construct(ilRbacReview $rbacReview = null)
41 {
42 $this->addHandler('osd', new ilNotificationOSDHandler());
43 $this->addHandler('mail', new ilNotificationMailHandler());
44 if ($rbacReview === null) {
45 global $DIC;
46 $rbacReview = $DIC->rbac()->review();
47 }
48 $this->rbacReview = $rbacReview;
49 }
50
51
52 private function addHandler(string $channel, ilNotificationHandler $handler): void
53 {
54 if (!array_key_exists($channel, $this->handler) || !is_array($this->handler[$channel])) {
55 $this->handler[$channel] = [];
56 }
57
58 $this->handler[$channel][] = $handler;
59 }
60
64 public function toUsers(ilNotificationConfig $notification, array $users, bool $processAsync = false): void
65 {
66 if ($processAsync === false) {
68 $usersWithCustomConfig = ilNotificationDatabaseHandler::getUsersWithCustomConfig($users);
72
73 $user_by_handler = [];
74 if (isset($types[$notification->getType()]['config_type'])) {
75 if ($types[$notification->getType()]['config_type'] === 'set_by_user') {
76 $it = new ilNotificationUserIterator($notification->getType(), $users);
77 $channelsByAdmin = false;
78 foreach ($it as $usr_id => $data) {
79 if (!isset($channels['channel']) || !$channels[$data['channel']]) {
80 continue;
81 }
82 if (!isset($user_by_handler[$data['channel']]) || !$user_by_handler[$data['channel']]) {
83 $user_by_handler[$data['channel']] = [];
84 }
85 $user_by_handler[$data['channel']][] = $usr_id;
86 }
87 } elseif ($types[$notification->getType()]['config_type'] !== 'disabled') {
88 $channelsByAdmin = true;
89 if (isset($adminConfig[$notification->getType()])) {
90 foreach ($adminConfig[$notification->getType()] as $channel) {
91 if (!isset($channels[$channel]) || !$channels[$channel]) {
92 continue;
93 }
94 $user_by_handler[$channel] = $users;
95 }
96 }
97 }
98 }
99
100 $userCache = [];
101
102 foreach ($user_by_handler as $handler => $h_users) {
103 $handler = $this->handler[$handler];
104 foreach ($h_users as $userId) {
105 if (!isset($userCache[$userId]) || !$userCache[$userId]) {
106 $user = ilObjectFactory::getInstanceByObjId($userId, false);
107 if (!($user instanceof ilObjUser)) {
108 continue;
109 }
110 $userCache[$userId] = $user;
111 }
112 $user = $userCache[$userId];
113
114 $instance = $notification->getUserInstance($user, $lang, $this->defaultLanguage);
115 foreach ($handler as $h) {
116 $h->notify($instance);
117 }
118 }
119 }
120 } else {
122 }
123 }
124
125 private function toListeners(ilNotificationConfig $notification, int $ref_id, bool $processAsync = false): void
126 {
127 if ($processAsync === false) {
129 if ($notification->hasDisableAfterDeliverySet()) {
131 }
132 } else {
134 }
135 }
136
140 private function toRoles(ilNotificationConfig $notification, array $roles, bool $processAsync = false): void
141 {
142 $users = [];
143 foreach ($roles as $role) {
144 $users[] = $this->rbacReview->assignedUsers($role);
145 }
146 $users = array_unique(array_merge(...$users));
147
148 $this->toUsers($notification, $users, $processAsync);
149 }
150
155 public static function sendNotificationToUsers(ilNotificationConfig $notification, array $users, bool $processAsync = false): void
156 {
157 global $DIC;
158 $DIC->notifications()->system()->toUsers($notification, $users, $processAsync);
159 }
160
161 public static function sendNotificationToListeners(ilNotificationConfig $notification, int $ref_id, bool $processAsync = false): void
162 {
163 global $DIC;
164 $DIC->notifications()->system()->toListeners($notification, $ref_id, $processAsync);
165 }
166
170 public static function sendNotificationToRoles(ilNotificationConfig $notification, array $roles, bool $processAsync = false): void
171 {
172 global $DIC;
173 $DIC->notifications()->system()->toRoles($notification, $roles, $processAsync);
174 }
175
176 public static function enableListeners(string $module, int $ref_id): void
177 {
179 }
180
181 public static function enableUserListeners(string $module, int $ref_id, array $users): void
182 {
183 if ($users) {
185 }
186 }
187
188 public function clear(string $channel = ''): void
189 {
190 $channels = $this->handler;
191 if ($channel !== '') {
192 $channels = [$this->handler[$channel]] ?? [];
193 }
194 foreach ($channels as $c) {
195 foreach ($c as $handler) {
196 $handler->clear();
197 }
198 }
199 }
200}
getUserInstance(ilObjUser $user, array $languageVars, string $defaultLanguage)
static enableListeners(string $module, $sender_id, array $users=[])
static getAvailableChannels(array $config_types=[], bool $includeDisabled=false)
static enqueueByUsers(ilNotificationConfig $notification, array $userids)
static enqueueByListener(ilNotificationConfig $notification, int $ref_id)
static sendNotificationToUsers(ilNotificationConfig $notification, array $users, bool $processAsync=false)
toListeners(ilNotificationConfig $notification, int $ref_id, bool $processAsync=false)
__construct(ilRbacReview $rbacReview=null)
static sendNotificationToRoles(ilNotificationConfig $notification, array $roles, bool $processAsync=false)
toUsers(ilNotificationConfig $notification, array $users, bool $processAsync=false)
static sendNotificationToListeners(ilNotificationConfig $notification, int $ref_id, bool $processAsync=false)
static enableUserListeners(string $module, int $ref_id, array $users)
addHandler(string $channel, ilNotificationHandler $handler)
static enableListeners(string $module, int $ref_id)
toRoles(ilNotificationConfig $notification, array $roles, bool $processAsync=false)
User class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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: cli.php:38
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$lang
Definition: xapiexit.php:26