ILIAS  release_8 Revision v8.25-1-g13de6a5eca6
class.ilOnScreenChatGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25use Psr\Http\Message\ResponseInterface;
26
34{
35 protected static bool $frontend_initialized = false;
36
41
42 public function __construct()
43 {
44 global $DIC;
45
46 $this->dic = $DIC;
47 $this->http = $DIC->http();
48 $this->ctrl = $DIC->ctrl();
49 $this->actor = $DIC->user();
50 }
51
52 private function getResponseWithText(string $body): ResponseInterface
53 {
54 return $this->dic->http()->response()->withBody(Streams::ofString($body));
55 }
56
57 protected static function isOnScreenChatAccessible(ilSetting $chatSettings): bool
58 {
59 global $DIC;
60
61 return (
62 $chatSettings->get('chat_enabled', '0') &&
63 $chatSettings->get('enable_osc', '0') &&
64 $DIC->user() && !$DIC->user()->isAnonymous()
65 );
66 }
67
72 protected static function getEmoticons(ilChatroomServerSettings $chatSettings): array
73 {
74 $smileys = [];
75
76 if ($chatSettings->getSmiliesEnabled()) {
77 $smileys_array = ilChatroomSmilies::_getSmilies();
78 foreach ($smileys_array as $smiley_array) {
79 $new_keys = [];
80 $new_val = '';
81 foreach ($smiley_array as $key => $value) {
82 if ($key === 'smiley_keywords') {
83 $new_keys = explode("\n", $value);
84 }
85
86 if ($key === 'smiley_fullpath') {
87 $new_val = $value;
88 }
89 }
90
91 if (!$new_keys || !$new_val) {
92 continue;
93 }
94
95 foreach ($new_keys as $new_key) {
96 $smileys[$new_key] = $new_val;
97 }
98 }
99 }
100
101 return $smileys;
102 }
103
104 public function executeCommand(): void
105 {
106 $cmd = $this->ctrl->getCmd();
107 switch ($cmd) {
108 case 'getUserProfileData':
109 $response = $this->getUserProfileData();
110 break;
111
112 case 'verifyLogin':
113 $response = $this->verifyLogin();
114 break;
115
116 case 'getRenderedConversationItems':
118 $this->dic,
119 new Conversation($this->dic->database(), $this->dic->user()),
120 new Subscriber($this->dic->database(), $this->dic->user())
121 );
122
123 $conversationIds = (string) ($this->dic->http()->request()->getQueryParams()['ids'] ?? '');
124 $noAggregates = ($this->dic->http()->request()->getQueryParams()['no_aggregates'] ?? '');
125
127 $this->dic->ui()->renderer()->renderAsync($provider->getAsyncItem(
128 $conversationIds,
129 $noAggregates !== 'true'
130 ))
131 );
132 break;
133
134 case 'getUserlist':
135 default:
136 $response = $this->getUserList();
137 }
138
139 if ($this->ctrl->isAsynch()) {
140 $this->http->saveResponse($response);
141 $this->http->sendResponse();
142 $this->http->close();
143 }
144 }
145
146 private function verifyLogin(): ResponseInterface
147 {
149
150 return $this->getResponseWithText(json_encode([
151 'loggedIn' => $this->actor->getId() && !$this->actor->isAnonymous()
152 ], JSON_THROW_ON_ERROR));
153 }
154
155 private function getUserList(): ResponseInterface
156 {
157 if (!$this->actor->getId() || $this->actor->isAnonymous()) {
158 return $this->getResponseWithText(json_encode([], JSON_THROW_ON_ERROR));
159 }
160
162 $auto->setUser($this->actor);
164 if (isset($this->http->request()->getQueryParams()['fetchall'])) {
165 $auto->setLimit(ilUserAutoComplete::MAX_ENTRIES);
166 }
167 $auto->setMoreLinkAvailable(true);
168 $auto->setSearchFields(['firstname', 'lastname']);
169 $auto->setResultField('login');
170 $auto->enableFieldSearchableCheck(true);
171
172 return $this->getResponseWithText($auto->getList($this->http->request()->getQueryParams()['term'] ?? ''));
173 }
174
175 private function getUserProfileData(): ResponseInterface
176 {
177 if (!$this->actor->getId() || $this->actor->isAnonymous()) {
178 return $this->getResponseWithText(json_encode([], JSON_THROW_ON_ERROR));
179 }
180
181 $usrIds = (string) ($this->http->request()->getQueryParams()['usr_ids'] ?? '');
182 if ($usrIds === '') {
183 return $this->getResponseWithText(json_encode([], JSON_THROW_ON_ERROR));
184 }
185
186 $this->dic->language()->loadLanguageModule('user');
187 $subscriberRepo = new Subscriber($this->dic->database(), $this->dic->user());
188 $data = $subscriberRepo->getDataByUserIds(explode(',', $usrIds));
189
191
192 return $this->getResponseWithText(json_encode($data, JSON_THROW_ON_ERROR));
193 }
194
195 public static function initializeFrontend(ilGlobalTemplateInterface $page): void
196 {
197 global $DIC;
198
199 if (!self::$frontend_initialized) {
200 $clientSettings = new ilSetting('chatroom');
201
202 if (!self::isOnScreenChatAccessible($clientSettings)) {
203 self::$frontend_initialized = true;
204 return;
205 }
206
208
209 $DIC->language()->loadLanguageModule('chatroom');
210 $DIC->language()->loadLanguageModule('user');
211
212 $renderer = $DIC->ui()->renderer();
213 $factory = $DIC->ui()->factory();
214
215 $chatWindowTemplate = new ilTemplate('tpl.chat-window.html', false, false, 'Services/OnScreenChat');
216 $chatWindowTemplate->setVariable('SUBMIT_ACTION', $renderer->render(
217 $factory->button()->standard($DIC->language()->txt('chat_osc_send'), 'onscreenchat-submit')
218 ));
219 $chatWindowTemplate->setVariable('ADD_ACTION', $renderer->render(
220 $factory->symbol()->glyph()->add('addUser')
221 ));
222 $chatWindowTemplate->setVariable('MINIMIZE_ACTION', $renderer->render(
223 $factory->button()->minimize()
224 ));
225 $chatWindowTemplate->setVariable('CONVERSATION_ICON', ilUtil::img(ilUtil::getImagePath('icon_pcht.svg')));
226
227 $subscriberRepo = new Subscriber($DIC->database(), $DIC->user());
228
229 $guiConfig = [
230 'chatWindowTemplate' => $chatWindowTemplate->get(),
231 'messageTemplate' => (new ilTemplate(
232 'tpl.chat-message.html',
233 false,
234 false,
235 'Services/OnScreenChat'
236 ))->get(),
237 'modalTemplate' => (new ilTemplate(
238 'tpl.chat-add-user.html',
239 false,
240 false,
241 'Services/OnScreenChat'
242 ))->get(),
243 'userId' => $DIC->user()->getId(),
244 'username' => $DIC->user()->getLogin(),
245 'userListURL' => $DIC->ctrl()->getLinkTargetByClass(
246 'ilonscreenchatgui',
247 'getUserList',
248 '',
249 true,
250 false
251 ),
252 'userProfileDataURL' => $DIC->ctrl()->getLinkTargetByClass(
253 'ilonscreenchatgui',
254 'getUserProfileData',
255 '',
256 true,
257 false
258 ),
259 'verifyLoginURL' => $DIC->ctrl()->getLinkTargetByClass(
260 'ilonscreenchatgui',
261 'verifyLogin',
262 '',
263 true,
264 false
265 ),
266 'renderConversationItemsURL' => $DIC->ctrl()->getLinkTargetByClass(
267 'ilonscreenchatgui',
268 'getRenderedConversationItems',
269 '',
270 true,
271 false
272 ),
273 'loaderImg' => ilUtil::getImagePath('loader.svg'),
274 'emoticons' => self::getEmoticons($settings),
275 'locale' => $DIC->language()->getLangKey(),
276 'initialUserData' => $subscriberRepo->getInitialUserProfileData(),
277 'enabledBrowserNotifications' => (
278 $clientSettings->get('enable_browser_notifications', '0') &&
279 ilUtil::yn2tf((string) $DIC->user()->getPref('chat_osc_browser_notifications'))
280 ),
281 'broadcast_typing' => (
282 ilUtil::yn2tf((string) $DIC->user()->getPref('chat_broadcast_typing'))
283 ),
284 'notificationIconPath' => ilUtil::getImagePath('icon_chta.png'),
285 ];
286
287 $chatConfig = [
288 'url' => $settings->generateClientUrl() . '/' . $settings->getInstance() . '-im',
289 'subDirectory' => $settings->getSubDirectory() . '/socket.io',
290 'userId' => $DIC->user()->getId(),
291 'username' => $DIC->user()->getLogin(),
292 ];
293
294 $DIC->language()->toJS([
295 'chat_osc_no_usr_found',
296 'chat_osc_emoticons',
297 'chat_osc_write_a_msg',
298 'autocomplete_more',
299 'chat_osc_minimize',
300 'chat_osc_invite_to_conversation',
301 'chat_osc_user',
302 'chat_osc_add_user',
303 'chat_osc_subs_rej_msgs',
304 'chat_osc_subs_rej_msgs_p',
305 'chat_osc_self_rej_msgs',
306 'chat_osc_search_modal_info',
307 'chat_osc_head_grp_x_persons',
308 'osc_noti_title',
309 'chat_osc_conversations',
310 'chat_osc_sure_to_leave_grp_conv',
311 'chat_osc_user_left_grp_conv',
312 'confirm',
313 'cancel',
314 'chat_osc_leave_grp_conv',
315 'chat_osc_no_conv',
316 'chat_osc_nc_conv_x_p',
317 'chat_osc_nc_conv_x_s',
318 'chat_osc_nc_no_conv',
319 'chat_user_x_is_typing',
320 'chat_users_are_typing',
321 'today',
322 'yesterday',
323 ], $page);
324
328
329 $page->addJavaScript('./node_modules/jquery-outside-events/jquery.ba-outside-events.js');
330 $page->addJavaScript('./node_modules/@andxor/jquery-ui-touch-punch-fix/jquery.ui.touch-punch.js');
331 $page->addJavascript('./Services/UIComponent/Modal/js/Modal.js');
332 $page->addJavascript('./node_modules/moment/min/moment-with-locales.min.js');
333 $page->addJavascript('./Services/Notifications/js/browser_notifications.js');
334 $page->addJavascript('./Services/OnScreenChat/js/onscreenchat-notifications.js');
335 $page->addJavascript('./Services/OnScreenChat/js/moment.js');
336 $page->addJavascript('./Modules/Chatroom/chat/node_modules/socket.io-client/dist/socket.io.js');
337 $page->addJavascript('./Services/OnScreenChat/js/chat.js');
338 $page->addJavascript('./Services/OnScreenChat/js/onscreenchat.js');
339 $page->addOnLoadCode("il.Chat.setConfig(" . json_encode($chatConfig, JSON_THROW_ON_ERROR) . ");");
340 $page->addOnLoadCode("il.OnScreenChat.setConfig(" . json_encode($guiConfig, JSON_THROW_ON_ERROR) . ");");
341 $page->addOnLoadCode("il.OnScreenChat.init();");
342 $page->addOnLoadCode('il.OnScreenChatNotifications.init(' . json_encode([
343 'conversationIdleTimeThreshold' => max(
344 1,
345 (int) $clientSettings->get('conversation_idle_state_in_minutes', '1')
346 ),
347 'logLevel' => $DIC['ilLoggerFactory']->getSettings()->getLevelByComponent('osch'),
348 ], JSON_THROW_ON_ERROR) . ');');
349
350 self::$frontend_initialized = true;
351 }
352 }
353}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
Class Services.
Definition: Services.php:38
Class ilChatroomServerSettings.
static initLinkify(?ilGlobalTemplateInterface $a_tpl=null)
User class.
Class ilOnScreenChatGUI.
static initializeFrontend(ilGlobalTemplateInterface $page)
static getEmoticons(ilChatroomServerSettings $chatSettings)
getResponseWithText(string $body)
ILIAS HTTP Services $http
ILIAS DI Container $dic
static isOnScreenChatAccessible(ilSetting $chatSettings)
static enableWebAccessWithoutSession(bool $enable_web_access_without_session)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
get(string $a_keyword, ?string $a_default_value=null)
get setting
special template class to simplify handling of ITX/PEAR
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
static yn2tf(string $a_yn)
static img(string $a_src, ?string $a_alt=null, $a_width="", $a_height="", $a_border=0, $a_id="", $a_class="")
Build img tag.
static initjQueryUI(ilGlobalTemplateInterface $a_tpl=null)
inits and adds the jQuery-UI JS-File to the global template (see included_components....
static initjQuery(ilGlobalTemplateInterface $a_tpl=null)
inits and adds the jQuery JS-File to the global or a passed template
global $DIC
Definition: feed.php:28
Interface ilCtrlBaseClassInterface describes ilCtrl base classes.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addJavaScript(string $a_js_file, bool $a_add_version_parameter=true, int $a_batch=2)
Add a javascript file that should be included in the header.
addOnLoadCode(string $a_code, int $a_batch=2)
Add on load code.
$provider
Definition: ltitoken.php:83
$factory
Definition: metadata.php:75
static http()
Fetches the global http state from ILIAS.
string $key
Consumer key/client ID value.
Definition: System.php:193
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
$response