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