ILIAS  release_7 Revision v7.30-3-g800a261c036
OnScreenChatNotificationProvider.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
3
5
15
21{
26
34 {
36 $dic->language()->loadLanguageModule('chatroom');
37
38 if (null === $conversationRepo) {
39 $conversationRepo = new Conversation($dic->database(), $dic->user());
40 }
41 $this->conversationRepo = $conversationRepo;
42
43 if (null === $subscriberRepo) {
44 $subscriberRepo = new Subscriber($dic->database(), $dic->user());
45 }
46 $this->subscriberRepo = $subscriberRepo;
47 }
48
52 public function getNotifications() : array
53 {
54 $id = function (string $id) : IdentificationInterface {
55 return $this->if->identifier($id);
56 };
57
58 if (0 === (int) $this->dic->user()->getId() || $this->dic->user()->isAnonymous()) {
59 return [];
60 }
61
62 $chatSettings = new \ilSetting('chatroom');
63 $isEnabled = $chatSettings->get('chat_enabled') && $chatSettings->get('enable_osc');
64 if (!$isEnabled) {
65 return [];
66 }
67
68 $factory = $this->globalScreen()->notifications()->factory();
69
70 $showAcceptMessageChange = (
71 !\ilUtil::yn2tf($this->dic->user()->getPref('chat_osc_accept_msg')) &&
72 !(bool) $this->dic->settings()->get('usr_settings_hide_chat_osc_accept_msg', false) &&
73 !(bool) $this->dic->settings()->get('usr_settings_disable_chat_osc_accept_msg', false)
74 );
75
76 $description = $this->dic->language()->txt('chat_osc_nc_no_conv');
77 if ($showAcceptMessageChange) {
78 $description = sprintf(
79 $this->dic->language()->txt('chat_osc_dont_accept_msg'),
80 $this->dic->ui()->renderer()->render(
81 $this->dic->ui()->factory()
82 ->link()
83 ->standard(
84 $this->dic->language()->txt('chat_osc_dont_accept_msg_link_txt'),
85 $this->dic->ctrl()->getLinkTargetByClass(
86 ['ilDashboardGUI', 'ilPersonalProfileGUI', 'ilUserPrivacySettingsGUI'],
87 'showPrivacySettings'
88 )
89 )
90 ->withOpenInNewViewport(true)
91 )
92 );
93 }
94
95 $icon = $this->dic->ui()->factory()
96 ->symbol()
97 ->icon()
98 ->standard(Standard::CHTA, 'conversations')->withIsOutlined(true);
99 $title = $this->dic->language()->txt('chat_osc_conversations');
100
101 $notificationItem = $this->dic->ui()->factory()
102 ->item()
103 ->notification($title, $icon)
104 ->withDescription($description);
105 if ($showAcceptMessageChange) {
106 /*$notificationItem = $notificationItem->withProperties([
107 '' => $this->dic->language()->txt('chat_osc_nc_no_conv')
108 ]);*/
109 } else {
110 $notificationItem = $notificationItem
111 ->withAdditionalOnLoadCode(
112 function ($id) {
113 return "
114 il.OnScreenChat.setNotificationItemId('$id');
115 ";
116 }
117 );
118 }
119
120 $group = $factory
121 ->standardGroup($id('chat_bucket_group'))
122 ->withTitle('Chat')
123 ->addNotification(
124 $factory->standard($id('chat_bucket'))
125 ->withNotificationItem($notificationItem)
126 ->withNewAmount(0)
127 );
128
129 return [
130 $group,
131 ];
132 }
133
140 public function getAsyncItem(
141 string $conversationIds,
142 bool $withAggregates
143 ) : array {
144 $conversationIds = array_filter(explode(',', $conversationIds));
145
146 $icon = $this->dic->ui()->factory()
147 ->symbol()
148 ->icon()
149 ->standard(Standard::CHTA, 'conversations')->withIsOutlined(true);
150
151 $title = $this->dic->language()->txt('chat_osc_conversations');
152 if ($withAggregates && count($conversationIds) > 0) {
153 $title = $this->dic->ui()->factory()
154 ->link()
155 ->standard($title, '#');
156 }
157 $notificationItem = $this->dic->ui()->factory()
158 ->item()
159 ->notification($title, $icon)
160 ->withDescription($this->dic->language()->txt('chat_osc_nc_no_conv'))
161 ->withAdditionalOnLoadCode(
162 function ($id) {
163 $tsInfo = json_encode(new \stdClass());
164 return "
165 il.OnScreenChat.setConversationMessageTimes($tsInfo);
166 il.OnScreenChat.setNotificationItemId('$id');
167 ";
168 }
169 );
170
171 if (
172 0 === count($conversationIds) ||
173 !$withAggregates ||
174 (!$this->dic->user()->getId() || $this->dic->user()->isAnonymous())
175 ) {
176 return [$notificationItem];
177 }
178
179 $conversations = $this->conversationRepo->findByIds($conversationIds);
180 if (0 === count($conversations)) {
181 return [$notificationItem];
182 }
183
184 $allUsrIds = [];
185 array_walk($conversations, function (ConversationDto $conversation) use (&$allUsrIds) {
186 $allUsrIds = array_unique(array_merge($conversation->getSubscriberUsrIds(), $allUsrIds));
187 });
188 $allUsrData = $this->subscriberRepo->getDataByUserIds($allUsrIds);
189
190 $messageTimesByConversation = [];
191
192 $aggregatedItems = [];
193 $latestMessageTimeStamp = null;
194 foreach ($conversations as $conversation) {
195 $convUsrData = array_filter($allUsrData, function ($key) use ($conversation) {
196 return in_array($key, $conversation->getSubscriberUsrIds());
197 }, ARRAY_FILTER_USE_KEY);
198
199 $convUsrNames = array_map(function ($value) {
200 return $value['public_name'];
201 }, $convUsrData);
202
203 $name = implode(', ', $convUsrNames);
204 $message = $conversation->getLastMessage()->getMessage();
205 $timestamp = (int) ($conversation->getLastMessage()->getCreatedTimestamp() / 1000);
207
208 $messageTimesByConversation[$conversation->getId()] = [
209 'ts' => $conversation->getLastMessage()->getCreatedTimestamp(),
210 'formatted' => $formattedDateTime
211 ];
212
213 $aggregateTitle = $this->dic->ui()->factory()
214 ->button()
215 ->shy(
216 $name,
217 ''
218 ) // Important: Do not pass any action here, otherwise there will be onClick/return false;
219 ->withAdditionalOnLoadCode(
220 function ($id) use ($conversation) {
221 return "
222 $('#$id').attr('data-onscreenchat-menu-item', '');
223 $('#$id').attr('data-onscreenchat-conversation', '{$conversation->getId()}');
224 ";
225 }
226 );
227 $aggregatedItems[] = $this->dic->ui()->factory()
228 ->item()
229 ->notification($aggregateTitle, $icon)
230 ->withDescription($message)
231 ->withAdditionalOnLoadCode(
232 function ($id) use ($conversation) {
233 return "
234 il.OnScreenChat.addConversationToUiIdMapping('{$conversation->getId()}', '$id');
235
236 $('#$id').find('.il-item-description').html(
237 il.OnScreenChat.getMessageFormatter().format(
238 $('#$id').find('.il-item-description').html()
239 )
240 );
241 $('#$id').find('button.close')
242 .attr('data-onscreenchat-menu-remove-conversation', '')
243 .attr('data-onscreenchat-conversation', '{$conversation->getId()}');
244 ";
245 }
246 )
247 ->withProperties([
248 $this->dic->language()->txt('chat_osc_nc_prop_time') => $formattedDateTime,
249 ])
250 ->withCloseAction('#'); // Important: The # prevents the default onClick handler is triggered
251
252 if ($timestamp > $latestMessageTimeStamp) {
253 $latestMessageTimeStamp = $timestamp;
254 }
255 }
256
257 $description = sprintf($this->dic->language()->txt('chat_osc_nc_conv_x_p'), count($aggregatedItems));
258 if (1 === count($aggregatedItems)) {
259 $description = $this->dic->language()->txt('chat_osc_nc_conv_x_s');
260 }
261
262 $notificationItem = $notificationItem
263 ->withAggregateNotifications($aggregatedItems)
264 ->withDescription($description)
265 ->withAdditionalOnLoadCode(
266 function ($id) use ($messageTimesByConversation) {
267 $tsInfo = json_encode($messageTimesByConversation);
268 return "
269 il.OnScreenChat.setConversationMessageTimes($tsInfo);
270 ";
271 }
272 )
273 ->withProperties([
274 $this->dic->language()->txt('chat_osc_nc_prop_time') => \ilDatePresentation::formatDate(
275 new \ilDateTime($latestMessageTimeStamp, IL_CAL_UNIX)
276 )
277 ]);
278
279 return [$notificationItem];
280 }
281}
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
An exception for terminatinating execution or to throw for unit testing.
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:19
__construct(Container $dic, Conversation $conversationRepo=null, Subscriber $subscriberRepo=null)
OnScreenChatNotificationProvider constructor.
const IL_CAL_UNIX
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date @access public.
@classDescription Date and time handling
static yn2tf($a_yn)
convert "y"/"n" to true/false
This describes the specific behavior of an ILIAS standard icon.
Definition: Standard.php:11
$factory
Definition: metadata.php:58
if($format !==null) $name
Definition: metadata.php:230
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$message
Definition: xapiexit.php:14