ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilChatroomFormFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  public const PROP_TITLE_AND_DESC = 'title_and_desc';
24  public const PROP_ONLINE_STATUS = 'online_status';
25  public const PROP_TILE_IMAGE = 'tile_image';
26  public const PROP_DISPLAY_PAST_MSG = 'display_past_msgs';
27  public const PROP_ENABLE_HISTORY = 'enable_history';
28  public const PROP_ALLOW_ANONYMOUS = 'allow_anonymous';
29  public const PROP_ALLOW_CUSTOM_NAMES = 'allow_custom_usernames';
30  public const PROP_AUTOGEN_USERNAMES = 'autogen_usernames';
31 
32  protected ilLanguage $lng;
33  protected ilObjUser $user;
34  protected \ILIAS\HTTP\Services $http;
35  protected \ILIAS\UI\Factory $ui_factory;
36  protected \ILIAS\Refinery\Factory $refinery;
37 
38  public function __construct()
39  {
40  global $DIC;
41 
42  $this->lng = $DIC->language();
43  $this->user = $DIC->user();
44  $this->http = $DIC->http();
45  $this->ui_factory = $DIC->ui()->factory();
46  $this->refinery = $DIC->refinery();
47  }
48 
52  public static function applyValues(ilPropertyFormGUI $form, array $values): void
53  {
54  $form->setValuesByArray(
55  array_map(
56  static fn($value) => is_int($value) ? (string) $value : $value,
57  $values
58  )
59  );
60  }
61 
68  {
69  $form = new ilPropertyFormGUI();
70  $title = new ilTextInputGUI($this->lng->txt('title'), 'title');
71  $title->setRequired(true);
72  $form->addItem($title);
73 
74  $description = new ilTextAreaInputGUI($this->lng->txt('description'), 'desc');
75  $form->addItem($description);
76 
77  return $this->addDefaultBehaviour($form);
78  }
79 
84  {
85  $form->addCommandButton('create-save', $this->lng->txt('create'));
86  $form->addCommandButton('cancel', $this->lng->txt('cancel'));
87 
88  return $form;
89  }
90 
91  private function mergeValuesTrafo(): \ILIAS\Refinery\Transformation
92  {
93  return $this->refinery->custom()->transformation(static fn(array $values): array => array_merge(...$values));
94  }
95 
96  private function saniziteArrayElementsTrafo(): \ILIAS\Refinery\Transformation
97  {
98  $sanitize = static function (array $data) use (&$sanitize): array {
99  foreach ($data as $key => $value) {
100  if (is_array($value)) {
101  $data[$key] = $sanitize($value);
102  } elseif (is_string($value)) {
103  $data[$key] = ilUtil::stripSlashes($value);
104  }
105  }
106 
107  return $data;
108  };
109 
110  return $this->refinery->custom()->transformation($sanitize);
111  }
112 
113  public function getSettingsForm(
114  ilChatroomObjectGUI $gui,
115  ilCtrlInterface $ctrl,
116  ?array $values = null
117  ): \ILIAS\UI\Component\Input\Container\Form\Form {
118  $this->lng->loadLanguageModule('obj');
119  $this->lng->loadLanguageModule('rep');
120 
121  $field_factory = $this->ui_factory->input()->field();
122 
123  $title_and_description = $gui->getObject()->getObjectProperties()->getPropertyTitleAndDescription();
124  $general_settings_fields = [
125  self::PROP_TITLE_AND_DESC => $title_and_description->toForm(
126  $this->lng,
127  $field_factory,
128  $this->refinery
129  )
130  ];
131 
132  $online_status = $gui->getObject()->getObjectProperties()->getPropertyIsOnline();
133  $availability_fields = [
134  self::PROP_ONLINE_STATUS => $online_status->toForm(
135  $this->lng,
136  $field_factory,
137  $this->refinery
138  )->withByline($this->lng->txt('chtr_activation_online_info')),
139  ];
140 
141  $tile_image = $gui->getObject()->getObjectProperties()->getPropertyTileImage();
142  $presentation_fields = [
143  self::PROP_TILE_IMAGE => $tile_image->toForm(
144  $this->lng,
145  $field_factory,
146  $this->refinery
147  ),
148  self::PROP_DISPLAY_PAST_MSG => $field_factory->numeric(
149  $this->lng->txt('display_past_msgs'),
150  $this->lng->txt('hint_display_past_msgs')
151  )->withRequired(
152  true
153  )->withAdditionalTransformation(
154  $this->refinery->logical()->parallel([
155  $this->refinery->int()->isGreaterThanOrEqual(0),
156  $this->refinery->int()->isLessThanOrEqual(100)
157  ])
158  )->withValue(
159  $values['display_past_msgs'] ?? 0
160  ),
161  self::PROP_ENABLE_HISTORY => $field_factory->checkbox(
162  $this->lng->txt('chat_enable_history'),
163  $this->lng->txt('chat_enable_history_info')
164  )->withValue((bool) ($values['enable_history'] ?? false)),
165  ];
166 
167  $function_fields = [
168  self::PROP_ALLOW_ANONYMOUS => $field_factory->checkbox(
169  $this->lng->txt('allow_anonymous'),
170  $this->lng->txt('anonymous_hint')
171  )->withValue((bool) ($values['allow_anonymous'] ?? false)),
172  self::PROP_ALLOW_CUSTOM_NAMES => $field_factory->optionalGroup(
173  [
174  self::PROP_AUTOGEN_USERNAMES => $field_factory->text(
175  $this->lng->txt('autogen_usernames'),
176  $this->lng->txt('autogen_usernames_info')
177  )->withRequired(true),
178  ],
179  $this->lng->txt('allow_custom_usernames')
180  )->withValue(
181  ($values['allow_custom_usernames'] ?? false) ? [self::PROP_AUTOGEN_USERNAMES => $values['autogen_usernames'] ?? ''] : null
182  ),
183  ];
184 
185  $sections = [
186  $field_factory->section(
187  $general_settings_fields,
188  $this->lng->txt('settings_title'),
189  ''
190  ),
191  $field_factory->section(
192  $availability_fields,
193  $this->lng->txt('rep_activation_availability'),
194  ''
195  ),
196  $field_factory->section(
197  $presentation_fields,
198  $this->lng->txt('settings_presentation_header'),
199  ''
200  ),
201  $field_factory->section(
202  $function_fields,
203  $this->lng->txt('chat_settings_functions_header'),
204  ''
205  ),
206  ];
207 
208  return $this->ui_factory->input()
209  ->container()
210  ->form()
211  ->standard(
212  $ctrl->getFormAction($gui, 'settings-saveGeneral'),
213  $sections
214  )
217  }
218 
219  public function getPeriodForm(): ilPropertyFormGUI
220  {
221  $form = new ilPropertyFormGUI();
222  $form->setPreventDoubleSubmission(false);
223 
224  $duration = new ilDateDurationInputGUI($this->lng->txt('period'), 'timeperiod');
225 
226  $duration->setStartText($this->lng->txt('duration_from'));
227  $duration->setEndText($this->lng->txt('duration_to'));
228  $duration->setShowTime(true);
229  $duration->setRequired(true);
230  $form->addItem($duration);
231 
232  return $form;
233  }
234 
239  public function getUserChatNameSelectionForm(array $name_options): ilPropertyFormGUI
240  {
241  $form = new ilPropertyFormGUI();
242 
243  $radio = new ilRadioGroupInputGUI($this->lng->txt('select_custom_username'), 'custom_username_radio');
244 
245  foreach ($name_options as $key => $option) {
246  $opt = new ilRadioOption($option, $key);
247  $radio->addOption($opt);
248  }
249 
250  $custom_opt = new ilRadioOption($this->lng->txt('custom_username'), 'custom_username');
251  $radio->addOption($custom_opt);
252 
253  $txt = new ilTextInputGUI($this->lng->txt('preferred_chatname'), 'custom_username_text');
254  $custom_opt->addSubItem($txt);
255  $form->addItem($radio);
256 
257  if ($this->user->isAnonymous()) {
258  $radio->setValue('anonymousName');
259  } else {
260  $radio->setValue('fullname');
261  }
262 
263  return $form;
264  }
265 
269  public function getSessionForm(array $sessions): ilPropertyFormGUI
270  {
271  $form = new ilPropertyFormGUI();
272  $form->setPreventDoubleSubmission(false);
273  $list = new ilSelectInputGUI($this->lng->txt('session'), 'session');
274 
275  $options = [];
276 
277  foreach ($sessions as $session) {
278  $start = new ilDateTime($session['connected'], IL_CAL_UNIX);
279  $end = new ilDateTime($session['disconnected'], IL_CAL_UNIX);
280 
281  $options[$session['connected'] . ',' .
282  $session['disconnected']] = ilDatePresentation::formatPeriod($start, $end);
283  }
284 
285  $list->setOptions($options);
286  $list->setRequired(true);
287 
288  $form->addItem($list);
289 
290  return $form;
291  }
292 
294  {
295  $form = new ilPropertyFormGUI();
296 
297  $enable_chat = new ilCheckboxInputGUI($this->lng->txt('chat_enabled'), 'chat_enabled');
298  $form->addItem($enable_chat);
299 
300  $enable_osc = new ilCheckboxInputGUI($this->lng->txt('chatroom_enable_osc'), 'enable_osc');
301  $enable_osc->setInfo($this->lng->txt('chatroom_enable_osc_info'));
302  $enable_chat->addSubItem($enable_osc);
303 
304  $oscBrowserNotificationStatus = new ilCheckboxInputGUI(
305  $this->lng->txt('osc_adm_browser_noti_label'),
306  'enable_browser_notifications'
307  );
308  $oscBrowserNotificationStatus->setInfo($this->lng->txt('osc_adm_browser_noti_info'));
309  $oscBrowserNotificationStatus->setValue('1');
310  $enable_osc->addSubItem($oscBrowserNotificationStatus);
311 
312  $oscBrowserNotificationIdleTime = new ilNumberInputGUI(
313  $this->lng->txt('osc_adm_conv_idle_state_threshold_label'),
314  'conversation_idle_state_in_minutes'
315  );
316  $oscBrowserNotificationIdleTime->allowDecimals(false);
317  $oscBrowserNotificationIdleTime->setSuffix($this->lng->txt('minutes'));
318  $oscBrowserNotificationIdleTime->setMinValue(1);
319  $oscBrowserNotificationIdleTime->setSize(5);
320  $oscBrowserNotificationIdleTime->setInfo($this->lng->txt('osc_adm_conv_idle_state_threshold_info'));
321  $enable_osc->addSubItem($oscBrowserNotificationIdleTime);
322 
323  $name = new ilTextInputGUI($this->lng->txt('chatroom_client_name'), 'client_name');
324  $name->setInfo($this->lng->txt('chatroom_client_name_info'));
325  $name->setRequired(true);
326  $name->setMaxLength(100);
327  $enable_chat->addSubItem($name);
328 
329  $auth = new ilChatroomAuthInputGUI(
330  $this->lng->txt('chatroom_auth'),
331  'auth',
333  );
334  $auth->setInfo($this->lng->txt('chat_auth_token_info'));
335  $auth->setCtrlPath(
336  [
337  ilAdministrationGUI::class,
338  ilObjChatroomGUI::class,
339  ilPropertyFormGUI::class,
340  ilFormPropertyDispatchGUI::class,
341  ilChatroomAuthInputGUI::class,
342  ]
343  );
344  $auth->setRequired(true);
345  $enable_chat->addSubItem($auth);
346 
347  return $form;
348  }
349 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getSessionForm(array $sessions)
Returns session form with period set by given $sessions.
This class represents a selection list property in a property form.
Class ChatMainBarProvider .
getFormAction(object $a_gui_obj, string $a_fallback_cmd=null, string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
Returns a form action link for the given information.
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_CAL_UNIX
$duration
global $DIC
Definition: feed.php:28
getSettingsForm(ilChatroomObjectGUI $gui, ilCtrlInterface $ctrl, ?array $values=null)
static http()
Fetches the global http state from ILIAS.
This class represents a property in a property form.
string $key
Consumer key/client ID value.
Definition: System.php:193
getUserChatNameSelectionForm(array $name_options)
Returns chatname selection form.
Class ilChatroomAuthInputGUI.
setValuesByArray(array $a_values, bool $a_restrict_to_value_keys=false)
$txt
Definition: error.php:14
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:58
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
getCreationForm()
Instantiates and returns ilPropertyFormGUI containing ilTextInputGUI and ilTextAreaInputGUI.
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
This class represents a text area property in a property form.
static applyValues(ilPropertyFormGUI $form, array $values)
Applies given values to field in given form.
ILIAS Refinery Factory $refinery
addDefaultBehaviour(ilPropertyFormGUI $form)
Adds &#39;create-save&#39; and &#39;cancel&#39; button to given $form and returns it.