ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
AllowOnScreenChatConversations.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30
32{
33 private const string KEY_ENABLE_BROWSER_NOTIFICATIONS = 'chat_osc_browser_notifications';
34
35 public function getIdentifier(): string
36 {
37 return 'chat_osc_accept_msg';
38 }
39
40 public function isAvailable(): bool
41 {
42 return (new \ilSetting('chatroom'))->get('enable_osc', '0') === '1';
43 }
44
45 public function getLabel(Language $lng): string
46 {
47 return $lng->txt('chat_osc_accept_msg');
48 }
49
51 {
52 return AvailablePages::PrivacySettings;
53 }
54
55 public function getSection(): AvailableSections
56 {
57 return AvailableSections::Main;
58 }
59
60 public function getInput(
61 FieldFactory $field_factory,
64 \ilSetting $settings,
65 ?\ilObjUser $user = null
66 ): Input {
67 $chat_settings = new \ilSetting('chatroom');
68 $lng->loadLanguageModule('chatroom_adm');
69
70 if ($chat_settings->get('enable_browser_notifications', '0') !== '1') {
71 return $field_factory->checkbox(
72 $lng->txt('chat_osc_accept_msg'),
73 $lng->txt('chat_osc_accept_msg_info')
74 )->withValue(
75 $user !== null
76 ? $user->getPref('chat_osc_accept_msg') === 'y'
77 : $settings->get('chat_osc_accept_msg') === 'y'
78 );
79 }
80
81 return $field_factory->optionalGroup(
82 [
83 self::KEY_ENABLE_BROWSER_NOTIFICATIONS => $field_factory->checkbox(
84 $lng->txt('osc_enable_browser_notifications_label'),
85 \sprintf(
86 $lng->txt('osc_enable_browser_notifications_info'),
87 (int) $chat_settings->get('conversation_idle_state_in_minutes')
88 )
89 )->withDisabled(
90 $settings->get('usr_settings_disable_chat_osc_accept_msg', '0') === '1'
91 )
92 ],
93 $lng->txt('chat_osc_accept_msg'),
94 $lng->txt('chat_osc_accept_msg_info')
95 )->withValue(
96 $this->buildValueForNotificationGroup($settings, $user)
97 );
98 }
99
100 public function getLegacyInput(
102 \ilSetting $settings,
103 ?\ilObjUser $user = null
105 $lng->loadLanguageModule('chatroom');
106
107 $input = new \ilCheckboxInputGUI($lng->txt('chat_osc_accept_msg'));
108 $input->setChecked(
109 $user !== null
110 ? $user->getPref('chat_osc_accept_msg') === 'y'
111 : $settings->get('chat_osc_accept_msg') === 'y'
112 );
113
114 return $input;
115 }
116
119 \ilSetting $settings
120 ): string {
121 return $lng->txt('chat_osc_accepts_messages_no');
122 }
123
125 \ilSetting $settings,
126 \ilObjUser $user
127 ): bool {
128 $user_value = $this->retrieveValueFromUser($user);
129 return $user_value[$this->getIdentifier()] !== false;
130 }
131
132 public function persistUserInput(
133 \ilObjUser $user,
134 mixed $input
135 ): \ilObjUser {
136 if ($input === null) {
137 $user->deletePref($this->getIdentifier());
138 $user->deletePref(self::KEY_ENABLE_BROWSER_NOTIFICATIONS);
139 $this->updateChatServer($user->getId(), false);
140
141 return $user;
142 }
143
144 if (\is_bool($input)) {
145 $user->setPref($this->getIdentifier(), $input ? 'y' : 'n');
146 $this->updateChatServer($user->getId(), $input);
147
148 return $user;
149 }
150
151 if (\is_array($input)) {
152 $user->setPref($this->getIdentifier(), 'y');
153 $user->setPref(
154 self::KEY_ENABLE_BROWSER_NOTIFICATIONS,
155 $input[self::KEY_ENABLE_BROWSER_NOTIFICATIONS] ? 'y' : 'n'
156 );
157 $this->updateChatServer($user->getId(), true);
158 }
159
160 return $user;
161 }
162
163 public function retrieveValueFromUser(\ilObjUser $user): array
164 {
165 if ($user->getPref($this->getIdentifier()) !== 'y') {
166 return [
167 $this->getIdentifier() => false,
168 self::KEY_ENABLE_BROWSER_NOTIFICATIONS => false
169 ];
170 }
171
172 return [
173 $this->getIdentifier() => true,
174 self::KEY_ENABLE_BROWSER_NOTIFICATIONS => $user->getPref(self::KEY_ENABLE_BROWSER_NOTIFICATIONS) === 'y'
175 ];
176 }
177
179 \ilSetting $settings,
180 ?\ilObjUser $user
181 ): ?array {
182 $active = $settings->get($this->getIdentifier()) === 'y';
183 $notification = false;
184 if ($user !== null) {
185 $active = $user->getPref($this->getIdentifier()) === 'y';
186 $notification = $user->getPref(self::KEY_ENABLE_BROWSER_NOTIFICATIONS) === 'y';
187 }
188
189 if ($active === false) {
190 return null;
191 }
192
193 return [
194 self::KEY_ENABLE_BROWSER_NOTIFICATIONS => $notification
195 ];
196 }
197
198 private function updateChatServer(int $user_id, bool $osc_state): void
199 {
200 $message = [
201 $user_id => [
202 'acceptsMessages' => $osc_state,
203 ]
204 ];
205
206 $settings = \ilChatroomAdmin::getDefaultConfiguration()->getServerSettings();
207 $connector = new \ilChatroomServerConnector($settings);
208 $connector->sendUserConfigChange(json_encode($message, JSON_THROW_ON_ERROR));
209 }
210}
isAvailable()
If this function returns false the setting will not be shown, even if it's PropertyAttributes would a...
getLegacyInput(Language $lng, \ilSetting $settings, ?\ilObjUser $user=null)
You don't need to add a post_var to the input as the User will handle this for you,...
getInput(FieldFactory $field_factory, Language $lng, Refinery $refinery, \ilSetting $settings, ?\ilObjUser $user=null)
Builds data types.
Definition: Factory.php:36
static getDefaultConfiguration()
Instantiates and returns ilChatroomAdmin object using instance_id and settings from settingsTable.
This class represents a property in a property form.
User class.
setPref(string $a_keyword, ?string $a_value)
getPref(string $a_keyword)
deletePref(string $keyword)
ILIAS Setting Class.
get(string $a_keyword, ?string $a_default_value=null)
get setting
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This is what a factory for input fields looks like.
Definition: Factory.php:31
This describes commonalities between all inputs.
Definition: Input.php:47
get(string $class_name)
global $lng
Definition: privfeed.php:31
if(!file_exists('../ilias.ini.php'))
$message
Definition: xapiexit.php:31