ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
class.ilForumPushProvider.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25
27{
28 public const string IDENTIFIER = 'forum';
29 public array $lang_cache = [];
30
32
33 public function getIdentifier(): string
34 {
35 return self::IDENTIFIER;
36 }
37
38 public function getName(ilLanguage $lng): string
39 {
40 $lng->loadLanguageModule('forum');
41
42 return $lng->txt('forum');
43 }
44
45 public function getDescription(ilLanguage $lng): string
46 {
47 $lng->loadLanguageModule('forum');
48
49 return $lng->txt('frm_forum_push_notification_info');
50 }
51
56 public function filterMailRecipients(
57 array $recipients,
59 int $notification_type,
61 ): array {
62 global $DIC;
63
64 if (
65 !$DIC->settings()->get('forum_notification', '0') ||
66 new ilSetting('notifications')->get('enable_push') !== '1'
67 ) {
68 return $recipients;
69 }
70
71 $mail_recipients = [];
72
73 foreach ($recipients as $recipient_id) {
74 if ($this->sendPush($recipient_id, $provider, $notification_type)) {
75 $logger->debug(sprintf('Push notification sent to user "%s".', $recipient_id));
76 } else {
77 $logger->debug(sprintf('Push notification not sent to user "%s". Fallback to mail if possible.', $recipient_id));
78 $mail_recipients[] = $recipient_id;
79 }
80 }
81
82 return $mail_recipients;
83 }
84
85 private function sendPush(
86 int $recipient_id,
88 int $notification_type
89 ): bool {
90 $user = ilObjectFactory::getInstanceByObjId($recipient_id, false);
91 if (!$user instanceof ilObjUser) {
92 return false;
93 }
94
95 if (isset($this->lang_cache[$user->getLanguage()])) {
96 $lng = $this->lang_cache[$user->getLanguage()];
97 } else {
99 $lng->loadLanguageModule('forum');
100 $this->lang_cache[$user->getLanguage()] = $lng;
101 }
102
103 $content = $this->buildPushContent($provider, $notification_type, $lng);
104 if ($content === null) {
105 return false;
106 }
107
108 return $this->getPushProvider()->push(
109 $user,
110 $content['title'],
111 $content['description'],
112 $this->buildPushLink($provider, $lng)
113 );
114 }
115
119 private function buildPushContent(
121 int $notification_type,
123 ): ?array {
124 $subject_key = $this->getSubjectLanguageKey($notification_type);
125 if ($subject_key === null) {
126 return null;
127 }
128
129 $container_text = '';
130 if ($provider->providesClosestContainer()) {
131 $container_text = ' (' .
132 $lng->txt('frm_noti_obj_' . $provider->closestContainer()->getType()) .
133 ' "' . $provider->closestContainer()->getTitle() . '")';
134 }
135
136 $title = sprintf(
137 $lng->txt($subject_key),
138 $provider->getForumTitle(),
139 $container_text,
140 $provider->getThreadTitle()
141 );
142
143 $description = match ($notification_type) {
145 $lng->txt('thread_deleted_by'),
146 $provider->getDeletedBy(),
147 $provider->getForumTitle()
148 ),
150 $lng->txt('frm_noti_new_post'),
151 $provider->getForumTitle()
152 ),
153 ilForumMailNotification::TYPE_POST_ACTIVATION => $lng->txt('forums_post_activation_mail'),
154 ilForumMailNotification::TYPE_POST_ANSWERED => $lng->txt('forum_post_replied'),
156 $lng->txt('post_updated_by'),
157 $provider->getPostUpdateUserName($lng),
158 $provider->getForumTitle()
159 ),
161 $lng->txt('post_censored_by'),
162 $provider->getPostUpdateUserName($lng),
163 $provider->getForumTitle()
164 ),
166 $lng->txt('post_uncensored_by'),
167 $provider->getPostUpdateUserName($lng)
168 ),
170 $lng->txt('post_deleted_by'),
171 $provider->getDeletedBy(),
172 $provider->getForumTitle()
173 ),
174 default => null,
175 };
176
177 if ($description === null) {
178 return null;
179 }
180
181 return [
182 'title' => $title,
183 'description' => $description,
184 ];
185 }
186
187 private function getSubjectLanguageKey(int $notification_type): ?string
188 {
189 return match ($notification_type) {
190 ilForumMailNotification::TYPE_THREAD_DELETED => 'frm_noti_subject_del_thread',
191 ilForumMailNotification::TYPE_POST_NEW => 'frm_noti_subject_new_post',
192 ilForumMailNotification::TYPE_POST_ACTIVATION => 'frm_noti_subject_act_post',
193 ilForumMailNotification::TYPE_POST_ANSWERED => 'frm_noti_subject_answ_post',
194 ilForumMailNotification::TYPE_POST_UPDATED => 'frm_noti_subject_upt_post',
195 ilForumMailNotification::TYPE_POST_CENSORED => 'frm_noti_subject_cens_post',
196 ilForumMailNotification::TYPE_POST_UNCENSORED => 'frm_noti_subject_uncens_post',
197 ilForumMailNotification::TYPE_POST_DELETED => 'frm_noti_subject_del_post',
198 default => null,
199 };
200 }
201
203 {
204 $url = rtrim(ilUtil::_getHttpPath(), '/') . '/goto.php?target=frm_' . implode('_', [
205 $provider->getRefId(),
206 $provider->getThreadId(),
207 $provider->getPostId(),
208 ]) . '&client_id=' . CLIENT_ID;
209
210 return new ilNotificationLink(
211 new ilNotificationParameter(sprintf($lng->txt('forums_notification_show_post'), $provider->getPostTitle()), [], 'forum'),
212 $url
213 );
214 }
215
217 {
218 return $this->push_provider ??= new NotificationsPushProvider($this);
219 }
220}
description of a localized parameter this information is used locate translations while processing no...
buildPushLink(ilForumNotificationMailData $provider, ilLanguage $lng)
buildPushContent(ilForumNotificationMailData $provider, int $notification_type, ilLanguage $lng)
getIdentifier()
Return a unique identifier.
getName(ilLanguage $lng)
Return the presentation name of the provider.
NotificationsPushProvider $push_provider
getDescription(ilLanguage $lng)
Return the description of the provider.
filterMailRecipients(array $recipients, ilForumNotificationMailData $provider, int $notification_type, ilLogger $logger)
sendPush(int $recipient_id, ilForumNotificationMailData $provider, int $notification_type)
getSubjectLanguageKey(int $notification_type)
static _getLanguageOfUser(int $a_usr_id)
Get language object of user.
language handling
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 ...
ILIAS Setting Class.
static _getHttpPath()
const CLIENT_ID
Definition: constants.php:41
Interface ilForumNotificationMailData.
getPostUpdateUserName(ilLanguage $user_lang)
global $lng
Definition: privfeed.php:26
global $DIC
Definition: shib_login.php:26