ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilUserPrivacySettingsGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
10 {
11  const PROP_ENABLE_OSC = 'chat_osc_accept_msg';
12  const PROP_ENABLE_BROWSER_NOTIFICATIONS = 'chat_osc_browser_notifications';
13  const PROP_ENABLE_SOUND = 'play_invitation_sound';
14 
18  protected $lng;
19 
23  protected $ctrl;
24 
28  protected $main_tpl;
29 
34 
38  protected $user;
39 
43  protected $settings;
44 
48  protected $request;
49 
53  protected $checklist_status;
54 
58  protected $profile_mode;
59 
61  private $uiFactory;
62 
64  private $uiRenderer;
65 
67  private $refinery;
68 
70  protected $chatSettings = array();
71 
73  protected $notificationSettings = array();
74 
76  protected $event;
77 
81  public function __construct()
82  {
83  global $DIC;
84 
85  $this->main_tpl = $DIC->ui()->mainTemplate();
86  $this->lng = $DIC->language();
87  $this->ctrl = $DIC->ctrl();
88  $this->lng->loadLanguageModule("user");
89  $this->user = $DIC->user();
90  $this->refinery = $DIC->refinery();
91  $this->uiFactory = $DIC->ui()->factory();
92  $this->uiRenderer = $DIC->ui()->renderer();
93  $this->chatSettings = new ilSetting('chatroom');
94  $this->notificationSettings = new ilSetting('notifications');
95  $this->event = $DIC->event();
96 
97  $this->request = $DIC->http()->request();
98 
99  $this->user_settings_config = new ilUserSettingsConfig();
100  $this->settings = $DIC->settings();
101  $this->checklist_status = new ilProfileChecklistStatus();
102  $this->profile_mode = new ilPersonalProfileMode($this->user, $this->settings);
103  }
104 
108  public function executeCommand()
109  {
110  $next_class = $this->ctrl->getNextClass();
111 
112  switch ($next_class) {
113  default:
114  $cmd = $this->ctrl->getCmd("showPrivacySettings");
115  $this->$cmd();
116  break;
117  }
118  $this->main_tpl->printToStdout();
119  }
120 
121 
122  //
123  //
124  // GENERAL SETTINGS FORM
125  //
126  //
127 
132  public function workWithUserSetting(string $setting) : bool
133  {
134  return $this->user_settings_config->isVisibleAndChangeable($setting);
135  }
136 
141  public function userSettingVisible(string $setting) : bool
142  {
143  return $this->user_settings_config->isVisible($setting);
144  }
145 
150  public function showPrivacySettings($form = null)
151  {
153  $user = $this->user;
154  $lng = $this->lng;
155 
156  $html = "";
157  if ($this->checklist_status->anyVisibilitySettings()
158  && ($this->isAwarnessSettingVisible()
159  || $this->isContactSettingVisible()
160  || $this->shouldDisplayChatSection())) {
161  if (is_null($form)) {
162  $form = $this->initPrivacySettingsForm();
163  }
164  $html = $this->uiRenderer->render([$form]);
165  }
166 
167  $pub_profile = new ilPublicUserProfileGUI($user->getId());
168  if ($this->profile_mode->isEnabled()) {
169  $pub_profile_legacy = $this->uiFactory->legacy($pub_profile->getEmbeddable());
170  $html .= $this->uiRenderer->render($this->uiFactory->panel()->standard(
171  $this->lng->txt('user_profile_preview'),
172  $pub_profile_legacy
173  ));
174  } else {
175  if (!$this->checklist_status->anyVisibilitySettings()) {
176  $html .= $this->uiRenderer->render(
177  [$this->uiFactory->messageBox()->info($lng->txt("usr_public_profile_disabled"))]
178  );
179  }
180  }
181 
182 
183  $chat_tpl = $this->appendChatJsToTemplate($main_tpl);
184 
185  $main_tpl->setContent($html . $chat_tpl->get());
186  }
187 
192  protected function isAwarnessSettingVisible() : bool
193  {
194  $awrn_set = new ilSetting("awrn");
195  if ($awrn_set->get("awrn_enabled", false) && $this->userSettingVisible("hide_own_online_status")) {
196  return true;
197  }
198  return false;
199  }
200 
205  protected function isContactSettingVisible() : bool
206  {
207  if (ilBuddySystem::getInstance()->isEnabled() && $this->userSettingVisible('bs_allow_to_contact_me')) {
208  return true;
209  }
210  return false;
211  }
212 
217  public function initPrivacySettingsForm()
218  {
219  $sections = [];
220 
221  $this->populateWithAwarenessSettingsSection($sections);
222  $this->populateWithContactsSettingsSection($sections);
223  $this->populateWithChatSettingsSection($sections);
224 
225  $form_action = $this->ctrl->getLinkTarget($this, "savePrivacySettings");
226 
227  return $this->uiFactory->input()
228  ->container()
229  ->form()
230  ->standard($form_action, $sections)
231  ->withAdditionalTransformation($this->refinery->custom()->transformation(static function ($values) : array {
232  return call_user_func_array('array_merge', $values);
233  }));
234  }
235 
239  public function shouldDisplayChatSection() : bool
240  {
241  return (
242  $this->chatSettings->get('chat_enabled', false) && (
244  )
245  );
246  }
247 
251  private function shouldShowNotificationOptions() : bool
252  {
253  return (
254  $this->notificationSettings->get('enable_osd', false) &&
255  $this->chatSettings->get('play_invitation_sound', false)
256  );
257  }
258 
262  private function shouldShowOnScreenChatOptions() : bool
263  {
264  return (
265  $this->chatSettings->get('enable_osc', false) &&
266  !(bool) $this->settings->get('usr_settings_hide_chat_osc_accept_msg', false)
267  );
268  }
269 
273  protected function populateWithAwarenessSettingsSection(array &$formSections) : void
274  {
275  if (!$this->isAwarnessSettingVisible()) {
276  return;
277  }
278 
279  $this->lng->loadLanguageModule("awrn");
280 
281  $default = ($this->settings->get('hide_own_online_status') == "n")
282  ? $this->lng->txt("user_awrn_show")
283  : $this->lng->txt("user_awrn_hide");
284 
285  $options = array(
286  "x" => $this->lng->txt("user_awrn_default") . " (" . $default . ")",
287  "n" => $this->lng->txt("user_awrn_show"),
288  "y" => $this->lng->txt("user_awrn_hide")
289  );
290  $val = $this->user->prefs["hide_own_online_status"];
291  if ($val == "") {
292  $val = "x";
293  }
294 
295  $fields["hide_own_online_status"] = $this->uiFactory->input()
296  ->field()
297  ->select(
298  $this->lng->txt("awrn_user_show"),
299  $options,
300  $this->lng->txt("awrn_hide_from_awareness_info")
301  )
302  ->withValue($val)
303  ->withRequired(true)
304  ->withDisabled(
305  $this->settings->get('usr_settings_disable_hide_own_online_status', '0') === '1' ? true : false
306  );
307 
308  $formSections['awrn_sec'] = $this->uiFactory->input()->field()->section($fields, $this->lng->txt('obj_awra'));
309  }
310 
314  protected function populateWithContactsSettingsSection(array &$formSections) : void
315  {
316  if (!$this->isContactSettingVisible()) {
317  return;
318  }
319 
320  $this->lng->loadLanguageModule('buddysystem');
321  $fields["bs_allow_to_contact_me"] = $this->uiFactory->input()
322  ->field()
323  ->checkbox(
324  $this->lng->txt("buddy_allow_to_contact_me"),
325  $this->lng->txt("buddy_allow_to_contact_me_info")
326  )
327  ->withValue($this->user->prefs['bs_allow_to_contact_me'] == 'y')
328  ->withDisabled(
329  $this->settings->get('usr_settings_disable_bs_allow_to_contact_me', '0') === '1' ? true : false
330  );
331 
332  $formSections['contacts_sec'] = $this->uiFactory->input()->field()->section($fields, $this->lng->txt('mm_contacts'));
333  }
334 
338  protected function populateWithChatSettingsSection(array &$formSections) : void
339  {
340  if (!$this->shouldDisplayChatSection()) {
341  return;
342  }
343 
344  $fieldFactory = $this->uiFactory->input()->field();
345  $fields = [];
346 
347  $this->lng->loadLanguageModule('chatroom_adm');
348  $checkboxStateToBooleanTrafo = $this->refinery->custom()->transformation(static function ($v) {
349  if (is_array($v)) {
350  return $v;
351  }
352 
353  if (is_bool($v)) {
354  return $v;
355  }
356 
357  return $v === 'checked';
358  });
359 
360  if ($this->shouldShowOnScreenChatOptions()) {
361  $oscAvailable = $this->settings->get('usr_settings_disable_chat_osc_accept_msg', '0') === '1' ? true : false;
362  $oscSubFormGroup = [];
363 
364  if ($this->chatSettings->get('enable_browser_notifications', false)) {
365  $enabledBrowserNotifications = $fieldFactory
366  ->checkbox(
367  $this->lng->txt('osc_enable_browser_notifications_label'),
368  sprintf(
369  $this->lng->txt('osc_enable_browser_notifications_info'),
370  (int) $this->chatSettings->get('conversation_idle_state_in_minutes')
371  )
372  )
373  ->withAdditionalTransformation($checkboxStateToBooleanTrafo)
374  ->withDisabled($oscAvailable);
375 
376  $oscSubFormGroup[self::PROP_ENABLE_BROWSER_NOTIFICATIONS] = $enabledBrowserNotifications;
377 
378  $groupValue = null;
379  if (ilUtil::yn2tf($this->user->getPref('chat_osc_accept_msg'))) {
380  $groupValue = [
381  self::PROP_ENABLE_BROWSER_NOTIFICATIONS => ilUtil::yn2tf($this->user->getPref('chat_osc_browser_notifications')),
382  ];
383  }
384  $enabledOsc = $fieldFactory
385  ->optionalGroup(
386  $oscSubFormGroup,
387  $this->lng->txt('chat_osc_accept_msg'),
388  $this->lng->txt('chat_osc_accept_msg_info')
389  )
390  ->withAdditionalTransformation($checkboxStateToBooleanTrafo)
391  ->withDisabled($oscAvailable)
392  ->withValue($groupValue);
393  } else {
394  $enabledOsc = $fieldFactory
395  ->checkbox(
396  $this->lng->txt('chat_osc_accept_msg'),
397  $this->lng->txt('chat_osc_accept_msg_info')
398  )
399  ->withAdditionalTransformation($checkboxStateToBooleanTrafo)
400  ->withDisabled($oscAvailable)
401  ->withValue(ilUtil::yn2tf($this->user->getPref('chat_osc_accept_msg')));
402  }
403 
404  $fields[self::PROP_ENABLE_OSC] = $enabledOsc;
405  }
406 
407  if ($this->shouldShowNotificationOptions()) {
408  $fields[self::PROP_ENABLE_SOUND] = $fieldFactory
409  ->checkbox($this->lng->txt('play_invitation_sound'), $this->lng->txt('play_invitation_sound_info'))
410  ->withAdditionalTransformation($checkboxStateToBooleanTrafo)
411  ->withValue((bool) $this->user->getPref('chat_play_invitation_sound'));
412  }
413 
414  $formSections['chat_sec'] = $this->uiFactory->input()->field()->section($fields, $this->lng->txt('chat_settings'));
415  }
416 
420  public function savePrivacySettings()
421  {
423  $form = $this->initPrivacySettingsForm();
424  $lng = $this->lng;
425  $user = $this->user;
426  $ctrl = $this->ctrl;
427 
428  if ($request->getMethod() == "POST") {
429  $form = $form->withRequest($request);
430  $formData = $form->getData();
431 
432  if ($this->isAwarnessSettingVisible() && $this->workWithUserSetting("hide_own_online_status")) {
433  $val = $formData["hide_own_online_status"] ?? 'x';
434  if ($val == "x") {
435  $val = "";
436  }
437  $user->setPref(
438  "hide_own_online_status",
439  $val
440  );
441  }
442  if ($this->isContactSettingVisible() && $this->workWithUserSetting("bs_allow_to_contact_me")) {
443  if ($formData["bs_allow_to_contact_me"]) {
444  $user->setPref("bs_allow_to_contact_me", "y");
445  } else {
446  $user->setPref("bs_allow_to_contact_me", "n");
447  }
448  }
449 
450  $user->update();
451 
452  if ($this->shouldDisplayChatSection()) {
453  $preferencesUpdated = false;
454 
455  if ($this->shouldShowNotificationOptions()) {
456  $oldPlaySoundValue = (int) $this->user->getPref('chat_play_invitation_sound');
457  $playASound = (int) ($formData[self::PROP_ENABLE_SOUND] ?? 0);
458 
459  if ($oldPlaySoundValue !== $playASound) {
460  $this->user->setPref('chat_play_invitation_sound', $playASound);
461  $preferencesUpdated = true;
462  }
463  }
464 
465  if ($this->shouldShowOnScreenChatOptions()) {
466  $oldEnableOscValue = ilUtil::yn2tf($this->user->getPref('chat_osc_accept_msg'));
467  $enableOsc = $formData[self::PROP_ENABLE_OSC] ?? null;
468  if (!is_bool($enableOsc)) {
469  $enableOsc = is_array($enableOsc);
470  }
471 
472  if ($this->settings->get('usr_settings_disable_chat_osc_accept_msg', '0') !== '1') {
473  $preferencesUpdated = true;
474  if ($oldEnableOscValue !== $enableOsc) {
475  $this->user->setPref('chat_osc_accept_msg', ilUtil::tf2yn($enableOsc));
476  $preferencesUpdated = true;
477  }
478  }
479 
480  if ($this->chatSettings->get('enable_browser_notifications', false) && $enableOsc) {
481  $oldBrowserNotificationValue = ilUtil::yn2tf($this->user->getPref('chat_osc_browser_notifications'));
482 
483  $sendBrowserNotifications = false;
484  if (is_array($formData[self::PROP_ENABLE_OSC])) {
485  if (true === $formData[self::PROP_ENABLE_OSC][self::PROP_ENABLE_BROWSER_NOTIFICATIONS]) {
486  $sendBrowserNotifications = true;
487  }
488  }
489 
490  if ($oldBrowserNotificationValue !== $sendBrowserNotifications) {
491  $this->user->setPref(
492  'chat_osc_browser_notifications',
493  ilUtil::tf2yn($sendBrowserNotifications)
494  );
495  $preferencesUpdated = true;
496  }
497  }
498  }
499 
500  if ($preferencesUpdated) {
501  $this->user->writePrefs();
502 
503  $this->event->raise(
504  'Modules/Chatroom',
505  'chatSettingsChanged',
506  [
507  'user' => $this->user
508  ]
509  );
510  }
511  }
512 
513  $this->checklist_status->saveStepSucess(ilProfileChecklistStatus::STEP_VISIBILITY_OPTIONS);
514  ilUtil::sendSuccess($lng->txt('msg_obj_modified'), true);
515  $ctrl->redirect($this, '');
516  }
517 
518  $this->showPrivacySettings($form);
519  }
520 
525  protected function appendChatJsToTemplate(ilGlobalPageTemplate $pageTemplate) : ilTemplate
526  {
527  $tpl = new ilTemplate('tpl.personal_chat_settings_form.html', true, true, 'Modules/Chatroom');
528  if ($this->shouldShowOnScreenChatOptions() && $this->chatSettings->get('enable_browser_notifications', false)) {
529  $pageTemplate->addJavascript('./Services/Notifications/js/browser_notifications.js');
530 
531  $tpl->setVariable('ALERT_IMAGE_SRC', ilUtil::getImagePath('icon_alert.svg'));
532  $tpl->setVariable('BROWSER_NOTIFICATION_TOGGLE_LABEL', $this->lng->txt('osc_enable_browser_notifications_label'));
533 
534  $this->lng->toJSMap([
535  'osc_browser_noti_no_permission_error' => $this->lng->txt('osc_browser_noti_no_permission_error'),
536  'osc_browser_noti_no_support_error' => $this->lng->txt('osc_browser_noti_no_support_error'),
537  'osc_browser_noti_req_permission_error' => $this->lng->txt('osc_browser_noti_req_permission_error'),
538  ], $pageTemplate);
539  }
540 
541  return $tpl;
542  }
543 }
Personal profile publishing mode of a iser.
static tf2yn($a_tf)
convert true/false to "y"/"n"
populateWithContactsSettingsSection(array &$formSections)
settings()
Definition: settings.php:2
user()
Definition: user.php:4
isAwarnessSettingVisible()
Is awareness tool setting visible.
populateWithAwarenessSettingsSection(array &$formSections)
GUI class for public user profile presentation.
Class ilGlobalPageTemplate.
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
global $DIC
Definition: goto.php:24
savePrivacySettings()
Save privacy settings.
showPrivacySettings($form=null)
General settings form.
populateWithChatSettingsSection(array &$formSections)
User settings configuration (what preferences can be visible/changed/...)
User privacy settings (currently located under "Profile and Privacy")
static yn2tf($a_yn)
convert "y"/"n" to true/false
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
isContactSettingVisible()
Is contact setting visible.
appendChatJsToTemplate(ilGlobalPageTemplate $pageTemplate)