ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilUserPrivacySettingsGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 use ILIAS\User\Profile\Mode as ProfileMode;
23 
26 
32 {
33  private const PROP_ENABLE_OSC = 'chat_osc_accept_msg';
34  private const PROP_ENABLE_BROWSER_NOTIFICATIONS = 'chat_osc_browser_notifications';
35  private const PROP_ENABLE_SOUND = 'osd_play_sound';
36  private const PROP_ENABLE_BROADCAST_TYPING = 'chat_broadcast_typing';
37 
38  protected Language $lng;
39  protected ilCtrl $ctrl;
42  protected ilObjUser $user;
43  protected ilSetting $settings;
44  protected \Psr\Http\Message\RequestInterface $request;
46  protected ProfileMode $profile_mode;
47  private \ILIAS\UI\Factory $uiFactory;
48  private \ILIAS\UI\Renderer $uiRenderer;
49  private \ILIAS\Refinery\Factory $refinery;
53 
54  public function __construct()
55  {
56  global $DIC;
57 
58  $this->main_tpl = $DIC['tpl'];
59  $this->lng = $DIC['lng'];
60  $this->ctrl = $DIC['ilCtrl'];
61  $this->user = $DIC['ilUser'];
62  $this->refinery = $DIC['refinery'];
63  $this->uiFactory = $DIC['ui.factory'];
64  $this->uiRenderer = $DIC['ui.renderer'];
65  $this->event = $DIC['ilAppEventHandler'];
66  $this->request = $DIC->http()->request();
67 
68  $this->chatSettings = new ilSetting('chatroom');
69  $this->notificationSettings = new ilSetting('notifications');
70 
71  $this->user_settings_config = new ilUserSettingsConfig();
72  $this->settings = $DIC->settings();
73  $this->profile_mode = new ProfileMode($this->lng, $this->settings, $this->user);
74  $this->checklist_status = new ChecklistStatus(
75  $this->lng,
76  $this->settings,
77  $this->user,
78  $this->profile_mode
79  );
80 
81  $this->lng->loadLanguageModule('user');
82  }
83 
84  public function executeCommand(): void
85  {
86  $next_class = $this->ctrl->getNextClass();
87 
88  switch ($next_class) {
89  default:
90  $cmd = $this->ctrl->getCmd("showPrivacySettings");
91  $this->$cmd();
92  break;
93  }
94  $this->main_tpl->printToStdout();
95  }
96 
97 
98  //
99  //
100  // GENERAL SETTINGS FORM
101  //
102  //
103 
104  public function workWithUserSetting(string $setting): bool
105  {
106  return $this->user_settings_config->isVisibleAndChangeable($setting);
107  }
108 
109  public function userSettingVisible(string $setting): bool
110  {
111  return $this->user_settings_config->isVisible($setting);
112  }
113 
114  public function showPrivacySettings(
115  \ILIAS\UI\Component\Input\Container\Form\Standard $form = null
116  ): void {
117  $main_tpl = $this->main_tpl;
118  $user = $this->user;
119  $lng = $this->lng;
120 
121  $html = "";
122  if ($this->checklist_status->anyVisibilitySettings()
123  && ($this->isAwarnessSettingVisible()
124  || $this->isContactSettingVisible()
125  || $this->shouldDisplayChatSection())) {
126  if (is_null($form)) {
127  $form = $this->initPrivacySettingsForm();
128  }
129  $html = $this->uiRenderer->render([$form]);
130  }
131 
132  $pub_profile = new ilPublicUserProfileGUI($user->getId());
133  if ($this->profile_mode->isEnabled()) {
134  $pub_profile_legacy = $this->uiFactory->legacy($pub_profile->getEmbeddable());
135  $html .= $this->uiRenderer->render($this->uiFactory->panel()->standard(
136  $this->lng->txt('user_profile_preview'),
137  $pub_profile_legacy
138  ));
139  } elseif (!$this->checklist_status->anyVisibilitySettings()) {
140  $html .= $this->uiRenderer->render(
141  [$this->uiFactory->messageBox()->info($lng->txt("usr_public_profile_disabled"))]
142  );
143  }
144 
145 
146  $chat_tpl = $this->appendChatJsToTemplate($main_tpl);
147 
148  $main_tpl->setContent($html . $chat_tpl->get());
149  }
150 
154  protected function isAwarnessSettingVisible(): bool
155  {
156  $awrn_set = new ilSetting("awrn");
157 
158  return $awrn_set->get("awrn_enabled", '0') && $this->userSettingVisible("hide_own_online_status");
159  }
160 
164  protected function isContactSettingVisible(): bool
165  {
166  return ilBuddySystem::getInstance()->isEnabled() && $this->userSettingVisible('bs_allow_to_contact_me');
167  }
168 
172  public function initPrivacySettingsForm(): \ILIAS\UI\Component\Input\Container\Form\Standard
173  {
174  $sections = [];
175 
176  $this->populateWithAwarenessSettingsSection($sections);
177  $this->populateWithContactsSettingsSection($sections);
178  $this->populateWithChatSettingsSection($sections);
179  $this->populateWithNotificationSettingsSection($sections);
180 
181  $form_action = $this->ctrl->getLinkTarget($this, "savePrivacySettings");
182 
183  return $this->uiFactory->input()
184  ->container()
185  ->form()
186  ->standard($form_action, $sections)
187  ->withAdditionalTransformation($this->refinery->custom()->transformation(static function (array $values): array {
188  return array_merge(...array_values($values));
189  }));
190  }
191 
192  private function shouldShowOnScreenChatOptions(): bool
193  {
194  return (
195  $this->chatSettings->get('enable_osc', '0') &&
196  !$this->settings->get('usr_settings_hide_chat_osc_accept_msg', '0')
197  );
198  }
199 
200  private function shouldShowChatTypingBroadcastOption(): bool
201  {
202  return (
203  !$this->settings->get('usr_settings_hide_chat_broadcast_typing', '0')
204  );
205  }
206 
207  public function shouldDisplayChatSection(): bool
208  {
209  return (bool) $this->chatSettings->get('chat_enabled', '0');
210  }
211 
212  private function shouldShowNotificationOptions(): bool
213  {
214  return (bool) $this->notificationSettings->get('osd_play_sound', '0');
215  }
216 
217  public function shouldDisplayNotificationSection(): bool
218  {
219  return (bool) $this->notificationSettings->get('enable_osd', '0');
220  }
221 
223  array &$formSections
224  ): void {
225  if (!$this->isAwarnessSettingVisible()) {
226  return;
227  }
228 
229  $this->lng->loadLanguageModule("awrn");
230 
231  $default = ($this->settings->get('hide_own_online_status') === "n")
232  ? $this->lng->txt("user_awrn_show")
233  : $this->lng->txt("user_awrn_hide");
234 
235  $options = [
236  "x" => $this->lng->txt("user_awrn_default") . " (" . $default . ")",
237  "n" => $this->lng->txt("user_awrn_show"),
238  "y" => $this->lng->txt("user_awrn_hide")
239  ];
240  $val = $this->user->prefs["hide_own_online_status"] ?? "";
241  if ($val == "") {
242  $val = "x";
243  }
244 
245  $fields["hide_own_online_status"] = $this->uiFactory->input()
246  ->field()
247  ->select(
248  $this->lng->txt("awrn_user_show"),
249  $options,
250  $this->lng->txt("awrn_hide_from_awareness_info")
251  )
252  ->withValue($val)
253  ->withRequired(true)
254  ->withDisabled(
255  $this->settings->get('usr_settings_disable_hide_own_online_status', '0') === '1' ? true : false
256  );
257 
258  $formSections['awrn_sec'] = $this->uiFactory->input()->field()->section($fields, $this->lng->txt('obj_awra'));
259  }
260 
262  array &$formSections
263  ): void {
264  if (!$this->isContactSettingVisible()) {
265  return;
266  }
267 
268  $this->lng->loadLanguageModule('buddysystem');
269  $bs_allow_contact_me = isset($this->user->prefs['bs_allow_to_contact_me']) ?
270  $this->user->prefs['bs_allow_to_contact_me'] === 'y' : false;
271  $fields["bs_allow_to_contact_me"] = $this->uiFactory->input()
272  ->field()
273  ->checkbox(
274  $this->lng->txt("buddy_allow_to_contact_me"),
275  $this->lng->txt("buddy_allow_to_contact_me_info")
276  )
277  ->withValue($bs_allow_contact_me)
278  ->withDisabled(
279  $this->settings->get('usr_settings_disable_bs_allow_to_contact_me', '0') === '1' ? true : false
280  );
281 
282  $formSections['contacts_sec'] = $this->uiFactory->input()->field()->section($fields, $this->lng->txt('mm_contacts'));
283  }
284 
288  protected function populateWithNotificationSettingsSection(array &$formSections): void
289  {
290  if (!$this->shouldDisplayNotificationSection()) {
291  return;
292  }
293 
294  $fields = [];
295 
296  if ($this->shouldShowNotificationOptions()) {
297  $this->lng->loadLanguageModule('notifications_adm');
298  $fields[self::PROP_ENABLE_SOUND] = $this->uiFactory->input()->field()
299  ->checkbox($this->lng->txt('osd_play_sound'), $this->lng->txt('osd_play_sound_desc'))
300  ->withValue((bool) $this->user->getPref('osd_play_sound'));
301  }
302 
303  if ($fields !== []) {
304  $formSections['notification_sec'] = $this->uiFactory->input()->field()->section(
305  $fields,
306  $this->lng->txt('notification_settings')
307  );
308  }
309  }
310 
312  array &$formSections
313  ): void {
314  if (!$this->shouldDisplayChatSection()) {
315  return;
316  }
317 
318  $fieldFactory = $this->uiFactory->input()->field();
319  $fields = [];
320 
321  $this->lng->loadLanguageModule('chatroom_adm');
322  $checkboxStateToBooleanTrafo = $this->refinery->custom()->transformation(static function ($v) {
323  if (is_array($v)) {
324  return $v;
325  }
326 
327  if (is_bool($v)) {
328  return $v;
329  }
330 
331  return $v === 'checked';
332  });
333 
334  if ($this->shouldShowOnScreenChatOptions()) {
335  $oscAvailable = $this->settings->get('usr_settings_disable_chat_osc_accept_msg', '0') === '1' ? true : false;
336  $oscSubFormGroup = [];
337 
338  if ($this->chatSettings->get('enable_browser_notifications', '0')) {
339  $enabledBrowserNotifications = $fieldFactory
340  ->checkbox(
341  $this->lng->txt('osc_enable_browser_notifications_label'),
342  sprintf(
343  $this->lng->txt('osc_enable_browser_notifications_info'),
344  (int) $this->chatSettings->get('conversation_idle_state_in_minutes')
345  )
346  )
347  ->withAdditionalTransformation($checkboxStateToBooleanTrafo)
348  ->withDisabled($oscAvailable);
349 
350  $oscSubFormGroup[self::PROP_ENABLE_BROWSER_NOTIFICATIONS] = $enabledBrowserNotifications;
351 
352  $groupValue = null;
353  if (ilUtil::yn2tf((string) $this->user->getPref('chat_osc_accept_msg'))) {
354  $groupValue = [
355  self::PROP_ENABLE_BROWSER_NOTIFICATIONS => ilUtil::yn2tf((string) $this->user->getPref('chat_osc_browser_notifications')),
356  ];
357  }
358  $enabledOsc = $fieldFactory
359  ->optionalGroup(
360  $oscSubFormGroup,
361  $this->lng->txt('chat_osc_accept_msg'),
362  $this->lng->txt('chat_osc_accept_msg_info')
363  )
364  ->withAdditionalTransformation($checkboxStateToBooleanTrafo)
365  ->withDisabled($oscAvailable)
366  ->withValue($groupValue);
367  } else {
368  $enabledOsc = $fieldFactory
369  ->checkbox(
370  $this->lng->txt('chat_osc_accept_msg'),
371  $this->lng->txt('chat_osc_accept_msg_info')
372  )
373  ->withAdditionalTransformation($checkboxStateToBooleanTrafo)
374  ->withDisabled($oscAvailable)
375  ->withValue(ilUtil::yn2tf((string) $this->user->getPref('chat_osc_accept_msg')));
376  }
377 
378  $fields[self::PROP_ENABLE_OSC] = $enabledOsc;
379  }
380 
382  $fields[self::PROP_ENABLE_BROADCAST_TYPING] = $fieldFactory
383  ->checkbox($this->lng->txt('chat_broadcast_typing'), $this->lng->txt('chat_broadcast_typing_info'))
384  ->withAdditionalTransformation($checkboxStateToBooleanTrafo)
385  ->withValue(ilUtil::yn2tf((string) $this->user->getPref('chat_broadcast_typing')));
386  }
387 
388  if ($fields !== []) {
389  $formSections['chat_sec'] = $this->uiFactory->input()->field()->section(
390  $fields,
391  $this->lng->txt('chat_settings')
392  );
393  }
394  }
395 
396  public function savePrivacySettings(): void
397  {
398  $request = $this->request;
399  $form = $this->initPrivacySettingsForm();
400  $lng = $this->lng;
401  $user = $this->user;
402  $ctrl = $this->ctrl;
403 
404  if ($request->getMethod() === "POST") {
405  $form = $form->withRequest($request);
406  $formData = $form->getData();
407 
408  if ($this->isAwarnessSettingVisible() && $this->workWithUserSetting("hide_own_online_status")) {
409  $val = $formData["hide_own_online_status"] ?? 'x';
410  if ($val == "x") {
411  $val = "";
412  }
413  $user->setPref(
414  "hide_own_online_status",
415  $val
416  );
417  }
418  if ($this->isContactSettingVisible() && $this->workWithUserSetting("bs_allow_to_contact_me")) {
419  if ($formData["bs_allow_to_contact_me"]) {
420  $user->setPref("bs_allow_to_contact_me", "y");
421  } else {
422  $user->setPref("bs_allow_to_contact_me", "n");
423  }
424  }
425 
426  $user->update();
427 
428  if ($this->shouldDisplayNotificationSection()) {
429  if ($this->shouldShowNotificationOptions()) {
430  $oldPlaySoundValue = (int) $this->user->getPref('osd_play_sound');
431  $playASound = (int) ($formData[self::PROP_ENABLE_SOUND] ?? 0);
432 
433  if ($oldPlaySoundValue !== $playASound) {
434  $this->user->setPref('osd_play_sound', (string) $playASound);
435  }
436  }
437  }
438 
439  if ($this->shouldDisplayChatSection()) {
440  $preferencesUpdated = false;
441 
442  if ($this->shouldShowOnScreenChatOptions()) {
443  $oldEnableOscValue = ilUtil::yn2tf((string) $this->user->getPref('chat_osc_accept_msg'));
444  $enableOsc = $formData[self::PROP_ENABLE_OSC] ?? null;
445  if (!is_bool($enableOsc)) {
446  $enableOsc = is_array($enableOsc);
447  }
448 
449  if ($this->settings->get('usr_settings_disable_chat_osc_accept_msg', '0') !== '1') {
450  $preferencesUpdated = true;
451  if ($oldEnableOscValue !== $enableOsc) {
452  $this->user->setPref('chat_osc_accept_msg', ilUtil::tf2yn($enableOsc));
453  $preferencesUpdated = true;
454  }
455  }
456 
457  if ($enableOsc && $this->chatSettings->get('enable_browser_notifications', '0')) {
458  $oldBrowserNotificationValue = ilUtil::yn2tf((string) $this->user->getPref('chat_osc_browser_notifications'));
459 
460  $sendBrowserNotifications = false;
461  if (is_array($formData[self::PROP_ENABLE_OSC]) &&
462  true === $formData[self::PROP_ENABLE_OSC][self::PROP_ENABLE_BROWSER_NOTIFICATIONS]) {
463  $sendBrowserNotifications = true;
464  }
465 
466  if ($oldBrowserNotificationValue !== $sendBrowserNotifications) {
467  $this->user->setPref(
468  'chat_osc_browser_notifications',
469  ilUtil::tf2yn($sendBrowserNotifications)
470  );
471  $preferencesUpdated = true;
472  }
473  }
474  }
475 
477  $oldBroadcastTypingValue = ilUtil::yn2tf((string) $this->user->getPref('chat_broadcast_typing'));
478  $broadcastTyping = (bool) ($formData[self::PROP_ENABLE_BROADCAST_TYPING] ?? false);
479 
480  if ($oldBroadcastTypingValue !== $broadcastTyping) {
481  $this->user->setPref('chat_broadcast_typing', ilUtil::tf2yn($broadcastTyping));
482  $preferencesUpdated = true;
483  }
484  }
485 
486  if ($preferencesUpdated) {
487  $this->user->writePrefs();
488 
489  $this->event->raise(
490  'components/ILIAS/Chatroom',
491  'chatSettingsChanged',
492  [
493  'user' => $this->user
494  ]
495  );
496  }
497  }
498 
499  $this->checklist_status->saveStepSucess(ChecklistStatus::STEP_VISIBILITY_OPTIONS);
500  $this->main_tpl->setOnScreenMessage('success', $lng->txt('msg_obj_modified'), true);
501  $ctrl->redirect($this, '');
502  }
503 
504  $this->showPrivacySettings($form);
505  }
506 
507  protected function appendChatJsToTemplate(
508  ilGlobalTemplateInterface $pageTemplate
509  ): ilTemplate {
510  $tpl = new ilTemplate('tpl.personal_chat_settings_form.html', true, true, 'components/ILIAS/Chatroom');
511  if ($this->shouldShowOnScreenChatOptions() && $this->chatSettings->get('enable_browser_notifications', '0')) {
512  $pageTemplate->addJavaScript('assets/js/browser_notifications.js');
513 
514  $tpl->setVariable('ALERT_IMAGE_SRC', ilUtil::getImagePath('standard/icon_alert.svg'));
515  $tpl->setVariable('BROWSER_NOTIFICATION_TOGGLE_LABEL', $this->lng->txt('osc_enable_browser_notifications_label'));
516 
517  $this->lng->toJSMap([
518  'osc_browser_noti_no_permission_error' => $this->lng->txt('osc_browser_noti_no_permission_error'),
519  'osc_browser_noti_no_support_error' => $this->lng->txt('osc_browser_noti_no_support_error'),
520  'osc_browser_noti_req_permission_error' => $this->lng->txt('osc_browser_noti_req_permission_error'),
521  ], $pageTemplate);
522  }
523 
524  return $tpl;
525  }
526 }
Global event handler.
populateWithContactsSettingsSection(array &$formSections)
appendChatJsToTemplate(ilGlobalTemplateInterface $pageTemplate)
Personal profile publishing mode of a user.
Definition: Mode.php:29
redirect(object $a_gui_obj, string $a_cmd=null, string $a_anchor=null, bool $is_async=false)
Interface Observer Contains several chained tasks and infos about them.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
populateWithNotificationSettingsSection(array &$formSections)
static tf2yn(bool $a_tf)
showPrivacySettings(\ILIAS\UI\Component\Input\Container\Form\Standard $form=null)
isAwarnessSettingVisible()
Is awareness tool setting visible.
populateWithAwarenessSettingsSection(array &$formSections)
GUI class for public user profile presentation.
txt(string $a_topic, string $a_default_lang_fallback_mod="")
setContent(string $a_html)
Sets content for standard template.
global $DIC
Definition: shib_login.php:25
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
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.
setPref(string $a_keyword, ?string $a_value)
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59
populateWithChatSettingsSection(array &$formSections)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static yn2tf(string $a_yn)
isContactSettingVisible()
Is contact setting visible.
Psr Http Message RequestInterface $request