ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilPersonalChatSettingsFormGUI.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
3 /* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
10 
17 {
18  const PROP_ENABLE_OSC = 'chat_osc_accept_msg';
19  const PROP_ENABLE_BROWSER_NOTIFICATIONS = 'chat_osc_browser_notifications';
20  const PROP_ENABLE_SOUND = 'play_invitation_sound';
21 
23  protected $lng;
24 
26  protected $ctrl;
27 
29  protected $user;
30 
32  protected $mainTpl;
33 
35  protected $settings;
36 
38  protected $chatSettings = array();
39 
41  protected $notificationSettings = array();
42 
44  protected $event;
45 
47  private $uiFactory;
48 
50  private $uiRenderer;
51 
53  private $httpRequest;
54 
56  private $refinery;
57 
61  public function __construct()
62  {
63  global $DIC;
64 
65  $this->user = $DIC->user();
66  $this->ctrl = $DIC->ctrl();
67  $this->settings = $DIC['ilSetting'];
68  $this->mainTpl = $DIC['tpl'];
69  $this->lng = $DIC['lng'];
70  $this->event = $DIC->event();
71  $this->uiFactory = $DIC->ui()->factory();
72  $this->uiRenderer = $DIC->ui()->renderer();
73  $this->httpRequest = $DIC->http()->request();
74  $this->refinery = $DIC->refinery();
75 
76  $this->lng->loadLanguageModule('chatroom');
77  $this->lng->loadLanguageModule('chatroom_adm');
78 
79  $this->chatSettings = new ilSetting('chatroom');
80  $this->notificationSettings = new ilSetting('notifications');
81  }
82 
86  public function executeCommand() : void
87  {
88  switch ($this->ctrl->getCmd()) {
89  case 'saveChatOptions':
90  $this->saveChatOptions();
91  break;
92 
93  case 'showChatOptions':
94  default:
95  $this->showChatOptions();
96  break;
97  }
98  }
99 
103  public function isAccessible() : bool
104  {
105  return (
106  $this->chatSettings->get('chat_enabled', false) && (
108  )
109  );
110  }
111 
115  private function shouldShowNotificationOptions() : bool
116  {
117  return (
118  $this->notificationSettings->get('enable_osd', false) &&
119  $this->chatSettings->get('play_invitation_sound', false)
120  );
121  }
122 
126  private function shouldShowOnScreenChatOptions() : bool
127  {
128  return (
129  $this->chatSettings->get('enable_osc', false) &&
130  !(bool) $this->settings->get('usr_settings_hide_chat_osc_accept_msg', false)
131  );
132  }
133 
137  private function buildForm() : Standard
138  {
139  $fieldFactory = $this->uiFactory->input()->field();
140 
141  $fields = [];
142 
143  $checkboxStateToBooleanTrafo = $this->refinery->custom()->transformation(function ($v) {
144  if (is_array($v)) {
145  return $v;
146  }
147 
148  if (is_bool($v)) {
149  return $v;
150  }
151 
152  return $v === 'checked';
153  });
154 
155  if ($this->shouldShowOnScreenChatOptions()) {
156  $oscAvailable = (bool) $this->settings->get('usr_settings_disable_chat_osc_accept_msg', false);
157  $oscSubFormGroup = [];
158 
159  if ($this->chatSettings->get('enable_browser_notifications', false)) {
160  $enabledBrowserNotifications = $fieldFactory
161  ->checkbox(
162  $this->lng->txt('osc_enable_browser_notifications_label'),
163  sprintf(
164  $this->lng->txt('osc_enable_browser_notifications_info'),
165  (int) $this->chatSettings->get('conversation_idle_state_in_minutes')
166  )
167  )
168  ->withAdditionalTransformation($checkboxStateToBooleanTrafo)
169  ->withDisabled($oscAvailable);
170 
171  $oscSubFormGroup[self::PROP_ENABLE_BROWSER_NOTIFICATIONS] = $enabledBrowserNotifications;
172 
173  $groupValue = null;
174  if (ilUtil::yn2tf($this->user->getPref('chat_osc_accept_msg'))) {
175  $groupValue = [
176  self::PROP_ENABLE_BROWSER_NOTIFICATIONS => ilUtil::yn2tf($this->user->getPref('chat_osc_browser_notifications')),
177  ];
178  }
179  $enabledOsc = $fieldFactory
180  ->optionalGroup(
181  $oscSubFormGroup,
182  $this->lng->txt('chat_osc_accept_msg'),
183  $this->lng->txt('chat_osc_accept_msg_info')
184  )
185  ->withAdditionalTransformation($checkboxStateToBooleanTrafo)
186  ->withDisabled($oscAvailable)
187  ->withValue($groupValue);
188  } else {
189  $enabledOsc = $fieldFactory
190  ->checkbox(
191  $this->lng->txt('chat_osc_accept_msg'),
192  $this->lng->txt('chat_osc_accept_msg_info')
193  )
194  ->withAdditionalTransformation($checkboxStateToBooleanTrafo)
195  ->withDisabled($oscAvailable)
196  ->withValue(ilUtil::yn2tf($this->user->getPref('chat_osc_accept_msg')));
197  }
198 
199  $fields[self::PROP_ENABLE_OSC] = $enabledOsc;
200  }
201 
202  if ($this->shouldShowNotificationOptions()) {
203  $fields[self::PROP_ENABLE_SOUND] = $fieldFactory
204  ->checkbox($this->lng->txt('play_invitation_sound'), $this->lng->txt('play_invitation_sound_info'))
205  ->withAdditionalTransformation($checkboxStateToBooleanTrafo)
206  ->withValue((bool) $this->user->getPref('chat_play_invitation_sound'));
207  }
208 
209  $section = $fieldFactory
210  ->section($fields, $this->lng->txt('chat_settings'), '');
211 
212  return $this->uiFactory->input()
213  ->container()
214  ->form()
215  ->standard(
216  $this->ctrl->getFormAction($this, 'saveChatOptions'),
217  [$section]
218  )
219  ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($values) {
220  return call_user_func_array('array_merge', $values);
221  }));
222  }
223 
228  public function showChatOptions(Standard $form = null) : void
229  {
230  if (!$this->isAccessible()) {
231  $this->ctrl->returnToParent($this);
232  }
233 
234  if (null === $form) {
235  $form = $this->buildForm();
236  }
237 
238  $tpl = new ilTemplate('tpl.personal_chat_settings_form.html', true, true, 'Modules/Chatroom');
239  if ($this->shouldShowOnScreenChatOptions() && $this->chatSettings->get('enable_browser_notifications', false)) {
240  $this->mainTpl->addJavascript('./Services/Notifications/js/browser_notifications.js');
241 
242  $tpl->setVariable('ALERT_IMAGE_SRC', ilUtil::getImagePath('icon_alert.svg'));
243 
244  $this->lng->toJSMap([
245  'osc_browser_noti_no_permission_error' => $this->lng->txt('osc_browser_noti_no_permission_error'),
246  'osc_browser_noti_no_support_error' => $this->lng->txt('osc_browser_noti_no_support_error'),
247  'osc_browser_noti_req_permission_error' => $this->lng->txt('osc_browser_noti_req_permission_error'),
248  ], $this->mainTpl);
249  }
250 
251  $this->mainTpl->setContent($this->uiRenderer->render([
252  $form,
253  $this->uiFactory->legacy($tpl->get())
254  ]));
255  $this->mainTpl->printToStdout();
256  }
257 
261  public function saveChatOptions() : void
262  {
263  if (!$this->isAccessible()) {
264  $this->ctrl->returnToParent($this);
265  }
266 
267  $form = $this->buildForm();
268 
269  if ('POST' === $this->httpRequest->getMethod()) {
270  $form = $form->withRequest($this->httpRequest);
271 
272  $formData = $form->getData();
273  $update_possible = !is_null($formData);
274  if ($update_possible) {
275  $this->saveFormData($formData);
276  }
277  }
278 
279  ilUtil::sendFailure($this->lng->txt('form_input_not_valid'));
280  $this->showChatOptions($form);
281  }
282 
283  private function saveFormData(array $formData) : void
284  {
285  $preferencesUpdated = false;
286 
287  if ($this->shouldShowNotificationOptions()) {
288  $oldPlaySoundValue = (int) $this->user->getPref('chat_play_invitation_sound');
289  $playASound = (int) ($formData[self::PROP_ENABLE_SOUND] ?? 0);
290 
291  if ($oldPlaySoundValue !== $playASound) {
292  $this->user->setPref('chat_play_invitation_sound', $playASound);
293  $preferencesUpdated = true;
294  }
295  }
296 
297  if ($this->shouldShowOnScreenChatOptions()) {
298  $oldEnableOscValue = ilUtil::yn2tf($this->user->getPref('chat_osc_accept_msg'));
299  $enableOsc = $formData[self::PROP_ENABLE_OSC] ?? null;
300  if (!is_bool($enableOsc)) {
301  $enableOsc = is_array($enableOsc);
302  }
303 
304  if (!(bool) $this->settings->get('usr_settings_disable_chat_osc_accept_msg', false)) {
305  $preferencesUpdated = true;
306  if ($oldEnableOscValue !== $enableOsc) {
307  $this->user->setPref('chat_osc_accept_msg', ilUtil::tf2yn($enableOsc));
308  $preferencesUpdated = true;
309  }
310  }
311 
312  if ($this->chatSettings->get('enable_browser_notifications', false) && $enableOsc) {
313  $oldBrowserNotificationValue = ilUtil::yn2tf($this->user->getPref('chat_osc_browser_notifications'));
314 
315  $sendBrowserNotifications = false;
316  if (is_array($formData[self::PROP_ENABLE_OSC])) {
317  if (true === $formData[self::PROP_ENABLE_OSC][self::PROP_ENABLE_BROWSER_NOTIFICATIONS]) {
318  $sendBrowserNotifications = true;
319  }
320  }
321 
322  if ($oldBrowserNotificationValue !== $sendBrowserNotifications) {
323  $this->user->setPref('chat_osc_browser_notifications', ilUtil::tf2yn($sendBrowserNotifications));
324  $preferencesUpdated = true;
325  }
326  }
327  }
328 
329  if ($preferencesUpdated) {
330  $this->user->writePrefs();
331 
332  $this->event->raise(
333  'Modules/Chatroom',
334  'chatSettingsChanged',
335  [
336  'user' => $this->user
337  ]
338  );
339  }
340 
341  ilUtil::sendSuccess($this->lng->txt('saved_successfully'), true);
342  $this->ctrl->redirect($this);
343  }
344 }
static tf2yn($a_tf)
convert true/false to "y"/"n"
settings()
Definition: settings.php:2
if(isset($_FILES['img_file']['size']) && $_FILES['img_file']['size'] > 0) $tpl
user()
Definition: user.php:4
$section
Definition: Utf8Test.php:83
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
This describes a standard form.
Definition: Standard.php:10
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
__construct()
ilPersonalChatSettingsFormGUI constructor.
$DIC
Definition: xapitoken.php:46
Class ilPersonalChatSettingsFormGUI.
static yn2tf($a_yn)
convert "y"/"n" to true/false