19declare(strict_types=1);
39 $this->gui->switchToVisibleMode();
45 $custom_username =
false;
52 $custom_username =
true;
61 $username = $chat_user->{
68 if (!$failure && trim($username) !==
'') {
69 if (!$room->isSubscribed($chat_user->getUserId())) {
70 $chat_user->setUsername($chat_user->buildUniqueUsername($username));
71 $chat_user->setProfilePictureVisible(!$custom_username);
76 $this->mainTpl->setOnScreenMessage(
'failure', $this->ilLng->txt(
'no_username_given'));
87 $this->mainTpl->addJavaScript(
'assets/js/socket.io.min.js');
88 $this->mainTpl->addJavaScript(
'assets/js/Chatroom.min.js');
90 $this->mainTpl->setPermanentLink($this->gui->getObject()->getType(), $this->gui->getObject()->getRefId());
98 $this->redirectIfNoPermission(
'read');
102 $ref_id = $this->getRequestValue(
'ref_id', $this->
refinery->kindlyTo()->int());
110 $this->cancelJoin($this->ilLng->txt(
'banned'));
115 $connector = $this->gui->getConnector();
119 $this->mainTpl->setOnScreenMessage(
'failure', $this->ilLng->txt(
'unable_to_connect'),
true);
129 $this->mainTpl->setOnScreenMessage(
'failure', $this->ilLng->txt(
'unable_to_connect'),
true);
133 $messages = $room->
getSetting(
'display_past_msgs') ? array_reverse(array_filter(
135 fn($entry) => $entry->type !==
'notice'
139 $show_auto_messages = !$this->ilUser->getPref(
'chat_hide_automsg_' . $room->
getRoomId());
141 $build = $this->buildChat($room, $connector->
getSettings());
143 $room_tpl = $build->template(
false, $build->initialData(
146 $this->ilCtrl->getLinkTarget($this->gui,
'view-lostConnection',
'',
false),
148 'moderator' => $is_moderator,
155 ), $this->panel($this->ilLng->txt(
'write_message'), $this->sendMessageForm()), $this->panel($this->ilLng->txt(
'messages'), $this->legacy(
'<div id="chat_messages"></div>')));
157 $this->mainTpl->setContent($room_tpl->get());
158 $this->mainTpl->setRightContent($this->userList() . $this->chatFunctions($show_auto_messages, $is_moderator));
163 $build = $this->buildChat($room, $this->gui->getConnector()->
getSettings());
165 return $build->template(
true, $build->initialData([],
true,
null, [
166 'moderator' =>
false,
169 'broadcast_typing' =>
false,
170 ], $messages), $this->panel($this->ilLng->txt(
'messages'), $this->legacy(
'<div id="chat_messages"></div>')),
'');
175 $template =
new ilTemplate(
'tpl.chatroom_send_message_form.html',
true,
true,
'components/ILIAS/Chatroom');
176 $this->renderSendMessageBox($template);
178 return $this->
legacy($template->get());
183 $roomRightTpl =
new ilTemplate(
'tpl.chatroom_right.html',
true,
true,
'components/ILIAS/Chatroom');
184 $this->renderRightUsersBlock($roomRightTpl);
186 return $this->panel($this->ilLng->txt(
'users'), $this->legacy($roomRightTpl->get()));
189 private function chatFunctions(
bool $show_auto_messages,
bool $is_moderator): string
191 $txt = $this->ilLng->txt(...);
192 $js_escape = json_encode(...);
193 $format = fn($format, ...$args) => sprintf($format, ...array_map($js_escape, $args));
194 $register = fn($name,
$c) =>
$c->withOnLoadCode(fn(
$id) => $format(
195 'il.Chatroom.bus.send(%s, document.getElementById(%s));',
200 $b = $this->uiFactory->button();
201 $toggle = fn($label, $enabled) =>
$b->toggle($label,
'#',
'#', $enabled)->withAriaLabel($label);
203 $bind = fn($key, $m) => $m->withAdditionalOnLoadCode(fn(
string $id) => $format(
204 '$(() => il.Chatroom.bus.send(%s, {
205 node: document.getElementById(%s),
206 showModal: () => $(document).trigger(%s, {}),
207 closeModal: () => $(document).trigger(%s, {})
211 $m->getShowSignal()->getId(),
212 $m->getCloseSignal()->getId()
215 $interrupt = fn($key, $label,
$text, $button =
null) => $bind($key, $this->uiFactory->modal()->interruptive(
219 ))->withActionButtonLabel($button ?? $label);
221 $auto_scroll = $register(
'auto-scroll-toggle', $toggle($txt(
'auto_scroll'),
true));
222 $messages = $register(
'system-messages-toggle', $toggle($txt(
'chat_show_auto_messages'), $show_auto_messages));
224 $invite = $bind(
'invite-modal', $this->uiFactory->modal()->roundtrip($txt(
'chat_invite'), $this->
legacy($txt(
'invite_to_private_room')), [
225 $this->uiFactory->input()->field()->text($txt(
'chat_invite')),
226 ])->withSubmitLabel($txt(
'chat_invite')));
229 $buttons[] = $register(
'invite-button',
$b->shy($txt(
'invite_to_private_room'),
''));
231 $buttons[] = $register(
'clear-history-button',
$b->shy($txt(
'clear_room_history'),
''));
234 return $this->panel($txt(
'chat_functions'), [
235 $this->
legacy(
'<div id="chat_function_list">'),
238 $interrupt(
'kick-modal', $txt(
'chat_kick'), $txt(
'kick_question')),
239 $interrupt(
'ban-modal', $txt(
'chat_ban'), $txt(
'ban_question')),
240 $interrupt(
'clear-history-modal', $txt(
'clear_room_history'), $txt(
'clear_room_history_question')),
242 $this->
legacy(sprintf(
'<div>%s%s</div>', $this->checkbox($auto_scroll), $this->checkbox($messages))),
248 return sprintf(
'<div class="chatroom-centered-checkboxes">%s</div>', $this->uiRenderer->render($component));
253 return $this->uiFactory->legacy()->content($html);
259 private function panel(
string $title, $body): string
261 if (is_array($body)) {
262 $body = $this->uiFactory->legacy()->content(join(
'', array_map($this->uiRenderer->render(...), $body)));
264 $panel = $this->uiFactory->panel()->secondary()->legacy($title, $body);
266 return $this->uiRenderer->render($panel);
271 $this->redirectIfNoPermission(
'read');
276 if ($this->
http->wrapper()->post()->has(
'state')) {
277 $state = $this->
http->wrapper()->post()->retrieve(
'state', $this->
refinery->kindlyTo()->int());
280 $this->ilUser->writePref(
281 'chat_hide_automsg_' . $room->getRoomId(),
282 (
string) ((
int) (!(
bool) $state))
285 $this->
http->saveResponse(
286 $this->
http->response()
287 ->withHeader(ResponseHeader::CONTENT_TYPE,
'application/json')
288 ->withBody(Streams::ofString(json_encode([
'success' =>
true], JSON_THROW_ON_ERROR)))
290 $this->
http->sendResponse();
291 $this->
http->close();
299 $this->mainTpl->setOnScreenMessage(
'failure', $message);
304 $roomTpl->
setVariable(
'PLACEHOLDER', $this->ilLng->txt(
'chat_osc_write_a_msg'));
305 $roomTpl->
setVariable(
'LBL_SEND', $this->ilLng->txt(
'send'));
310 $roomTpl->
setVariable(
'LBL_NO_FURTHER_USERS', $this->ilLng->txt(
'no_further_users'));
317 $selectionForm = $formFactory->getUserChatNameSelectionForm($name_options);
321 $selectionForm->addCommandButton(
'view-joinWithCustomName', $this->ilLng->txt(
'enter'));
322 $selectionForm->setFormAction(
326 $this->mainTpl->setVariable(
'ADM_CONTENT', $selectionForm->getHTML());
337 $this->redirectIfNoPermission(
'read');
339 $this->gui->switchToVisibleMode();
340 $this->setupTemplate();
342 $chatSettings =
new ilSetting(
'chatroom');
343 if (!$chatSettings->get(
'chat_enabled',
'0')) {
349 if (!$room->getSetting(
'allow_anonymous') && $this->ilUser->isAnonymous()) {
350 $this->cancelJoin($this->ilLng->txt(
'chat_anonymous_not_allowed'));
356 if ($room->getSetting(
'allow_custom_usernames')) {
357 if ($room->isSubscribed($chat_user->getUserId())) {
358 $chat_user->setUsername($chat_user->getUsername());
359 $this->showRoom($room, $chat_user);
361 $this->showNameSelection($chat_user);
364 $chat_user->setUsername($this->ilUser->getPublicName());
365 $chat_user->setProfilePictureVisible(
true);
366 $this->showRoom($room, $chat_user);
372 $pid = $this->tree->getParentId($this->gui->getRefId());
379 if ($this->
http->wrapper()->query()->has(
'msg')) {
380 match ($this->
http->wrapper()->query()->retrieve(
'msg', $this->refinery->kindlyTo()->string())) {
381 'kicked' => $this->mainTpl->setOnScreenMessage(
'failure', $this->ilLng->txt(
'kicked'),
true),
382 'banned' => $this->mainTpl->setOnScreenMessage(
'failure', $this->ilLng->txt(
'banned'),
true),
383 default => $this->mainTpl->setOnScreenMessage(
'failure', $this->ilLng->txt(
'lost_connection'),
true),
386 $this->mainTpl->setOnScreenMessage(
'failure', $this->ilLng->txt(
'lost_connection'),
true);
392 public function getUserProfileImages(): void
398 $request = json_decode($this->
http->request()->getBody()->getContents(),
true, 512, JSON_THROW_ON_ERROR);
403 $this->refinery->kindlyTo()->recordOf([
404 'id' => $this->refinery->kindlyTo()->int(),
405 'username' => $this->refinery->kindlyTo()->string(),
406 'profile_picture_visible' => $this->refinery->kindlyTo()->bool(),
408 $this->refinery->kindlyTo()->recordOf([
409 'id' => $this->refinery->kindlyTo()->int(),
410 'username' => $this->refinery->kindlyTo()->string(),
412 ]))->transform($request[
'profiles'] ?? []);
414 $user_ids = array_column($users,
'id');
418 foreach ($users as $user) {
419 if ($user[
'profile_picture_visible'] ??
false) {
420 $public_image = $public_data[$user[
'id']][
'img'] ??
'';
423 $avatar =
$DIC[
"user.avatar.factory"]->avatar(
'xsmall');
426 $public_image = $avatar->getUrl();
429 $response[json_encode($user, JSON_THROW_ON_ERROR)] = $public_image;
439 $kindly = $this->
refinery->kindlyTo();
440 $s = $kindly->string();
441 $int = $kindly->int();
442 $get = $this->
http->wrapper()->query()->retrieve(...);
443 $get_or = fn($k, $t,
$d =
null) => $get($k, $this->
refinery->byTrying([$t, $this->refinery->always(
$d)]));
445 $ref_id = $get(
'ref_id', $int);
447 $username = $get(
'username', $s);
448 $actions = $get_or(
'actions', $kindly->dictOf($s), []);
450 $avatar =
$DIC[
"user.avatar.factory"]->avatar(
'xsmall');
453 $public_image = $avatar->getUrl();
454 $item = $this->uiFactory->item()->standard($username)->withLeadImage($this->uiFactory->image()->standard(
456 'Profile image of ' . $username
460 $item = $item->withProperties([
461 $this->ilLng->txt(
'role') => $this->ilLng->txt(
'il_chat_moderator'),
464 $item = $item->withActions($this->uiFactory->dropdown()->standard($this->buildUserActions(
$user_id, $actions)));
467 $this->sendResponse($this->uiRenderer->renderAsync($item),
'text/html');
476 $chat_settings =
new ilSetting(
'chatroom');
477 $osc_enabled = $chat_settings->get(
'chat_enabled') && $chat_settings->get(
'enable_osc');
479 'kick' => $this->ilLng->txt(
'chat_kick'),
480 'ban' => $this->ilLng->txt(
'chat_ban'),
484 $translations[
'chat'] = $this->ilLng->txt(
'start_private_chat');
488 foreach ($actions as $key => $bus_id) {
489 $label = $translations[$key] ??
false;
491 $buttons[] = $this->uiFactory->button()->shy($label,
'')->withAdditionalOnLoadCode(fn(
string $id):
string => (
492 'il.Chatroom.bus.send(' . json_encode(
495 ) .
', document.getElementById(' . json_encode(
$id, JSON_THROW_ON_ERROR) .
'));'
505 return new BuildChat($this->
ilCtrl, $this->ilLng, $this->gui, $room, $settings, $this->ilUser, $this->uiFactory, $this->uiRenderer);
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
setVariable($variable, $value='')
Sets a variable value.
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Class ilChatroomGUIHandler.
hasRequestValue(string $key)
redirectIfNoPermission($permission)
Checks for requested permissions and redirects if the permission check failed.
getRequestValue(string $key, Transformation $trafo, $default=null)
Class ilChatroomServerSettings.
isProfilePictureVisible()
getChatNameSuggestions()
Returns an array of chat-name suggestions.
getUserId()
Returns Ilias User ID.
getUsername()
Returns username from Object or SESSION.
readOnlyChatWindow(ilChatroom $room, array $messages)
buildChat(ilChatroom $room, ilChatroomServerSettings $settings)
panel(string $title, $body)
renderRightUsersBlock(ilTemplate $roomTpl)
cancelJoin(string $message)
Calls ilUtil::sendFailure method using given $message as parameter.
checkbox(Component $component)
setupTemplate()
Adds CSS and JavaScript files that should be included in the header.
showNameSelection(ilChatroomUser $chat_user)
buildUserActions(int $user_id, array $actions)
toggleAutoMessageDisplayState()
executeDefault(string $requestedMethod)
Chatroom and Chatuser get prepared before $this->showRoom method is called.
chatFunctions(bool $show_auto_messages, bool $is_moderator)
showRoom(ilChatroom $room, ilChatroomUser $chat_user)
Prepares and displays chatroom and connects user to it.
renderSendMessageBox(ilTemplate $roomTpl)
getConnectedUsers(bool $only_data=true)
static checkPermissionsOfUser(int $usr_id, $permissions, int $ref_id)
Checks user permissions in question for a given user id in relation to a given ref_id.
connectUser(ilChatroomUser $user)
static checkUserPermissions($permissions, int $ref_id, bool $send_info=true)
Checks user permissions by given array and ref_id.
isUserBanned(int $user_id)
isSubscribed(int $chat_userid)
getLastMessages(int $number, ilChatroomUser $chatuser)
static byObjectId(int $object_id)
Class ilCtrl provides processing control methods.
redirectByClass( $a_class, ?string $a_cmd=null, ?string $a_anchor=null, bool $is_async=false)
@inheritDoc
getFormAction(object $a_gui_obj, ?string $a_fallback_cmd=null, ?string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
@inheritDoc
saveParameter(object $a_gui_obj, $a_parameter)
@inheritDoc
redirect(object $a_gui_obj, ?string $a_cmd=null, ?string $a_anchor=null, bool $is_async=false)
@inheritDoc
setParameterByClass(string $a_class, string $a_parameter, $a_value)
@inheritDoc
getLinkTargetByClass( $a_class, ?string $a_cmd=null, ?string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
@inheritDoc
static initLinkify(?ilGlobalTemplateInterface $a_tpl=null)
static _lookupPref(int $a_usr_id, string $a_keyword)
static subStr(string $a_str, int $a_start, ?int $a_length=null)
special template class to simplify handling of ITX/PEAR
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link='', bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path=null)
Default behaviour is:
static setTokenMaxLifetimeInSeconds(int $token_max_lifetime_in_seconds)
A component is the most general form of an entity in the UI.
static http()
Fetches the global http state from ILIAS.