ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilCustomUserFieldsGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
31 {
34  private Language $lng;
38 
39  private int $ref_id = 0;
40  private bool $confirm_change = false;
41  private int $field_id = 0;
45  private array $field_definition = [];
47 
48  public function __construct(
49  int $ref_id,
50  int $requested_field_id
51  ) {
52  global $DIC;
53  $this->main_tpl = $DIC->ui()->mainTemplate();
54  $this->ctrl = $DIC['ilCtrl'];
55  $this->lng = $DIC['lng'];
56  $this->ui_factory = $DIC['ui.factory'];
57  $this->toolbar = $DIC['ilToolbar'];
58 
59  $this->lng->loadLanguageModule('user');
60  $this->lng->loadLanguageModule('administration');
61 
62  $this->ref_id = $ref_id;
63 
64  $this->field_id = $requested_field_id;
65  $this->ctrl->saveParameter($this, 'field_id', $this->field_id);
66 
67  if ($this->field_id) {
68  $user_field_definitions = ilUserDefinedFields::_getInstance();
69  $this->field_definition = $user_field_definitions->getDefinition($this->field_id);
70  }
71 
72  $this->permissions = ilUDFPermissionHelper::getInstance(
73  $DIC->user()->getId(),
74  $ref_id
75  );
76  $this->request = new UserGUIRequest(
77  $DIC->http(),
78  $DIC->refinery()
79  );
80  }
81 
83  {
84  return $this->permissions;
85  }
86 
87  public function executeCommand(): void
88  {
89  $next_class = $this->ctrl->getNextClass($this);
90  $cmd = $this->ctrl->getCmd();
91 
92  switch ($next_class) {
93  default:
94  if (!$cmd) {
95  $cmd = 'listUserDefinedFields';
96  }
97  $this->$cmd();
98  break;
99  }
100  }
101 
102  public function listUserDefinedFields(): void
103  {
104  if ($this->getPermissions()->hasPermission(
106  (string) $this->ref_id,
108  )) {
109  $this->toolbar->addComponent(
110  $this->ui_factory->link()->standard(
111  $this->lng->txt('add_user_defined_field'),
112  $this->ctrl->getLinkTarget($this, 'addField')
113  )
114  );
115  }
116 
117  $tab = new ilCustomUserFieldSettingsTableGUI($this, 'listUserDefinedFields', $this->getPermissions());
118  if ($this->confirm_change) {
119  $tab->setConfirmChange();
120  }
121  $this->main_tpl->setContent($tab->getHTML());
122  }
123 
124  public function addField(?ilPropertyFormGUI $a_form = null): void
125  {
126  if (!$a_form) {
127  $a_form = $this->initForm('create');
128  }
129 
130  $this->main_tpl->setContent($a_form->getHTML());
131  }
132 
137  public function getAccessOptions(): array
138  {
139  $opts = [];
140  $opts['visible'] = $this->lng->txt('user_visible_in_profile');
141  $opts['visib_reg'] = $this->lng->txt('visible_registration');
142  $opts['visib_lua'] = $this->lng->txt('usr_settings_visib_lua');
143  $opts['course_export'] = $this->lng->txt('course_export');
144  $opts['group_export'] = $this->lng->txt('group_export');
145  $opts["prg_export"] = $this->lng->txt("prg_export");
146  $opts['changeable'] = $this->lng->txt('changeable');
147  $opts['changeable_lua'] = $this->lng->txt('usr_settings_changeable_lua');
148  $opts['required'] = $this->lng->txt('required_field');
149  $opts['export'] = $this->lng->txt('export');
150  $opts['searchable'] = $this->lng->txt('header_searchable');
151  $opts['certificate'] = $this->lng->txt('certificate');
152  return $opts;
153  }
154 
158  public static function getAccessPermissions(): array
159  {
160  return [
173  ];
174  }
175 
176  protected function initFieldDefinition(): array // Missing array type.
177  {
179  $this->lng->loadLanguageModule('ps');
180  $this->main_tpl->setOnScreenMessage('info', $this->lng->txt('ps_warning_modify'));
181  }
182 
183  $perms = [];
184  if ($this->field_definition) {
185  $perms = $this->permissions->hasPermissions(
187  (string) $this->field_definition['field_id'],
188  [
189  [
192  ],
193  [
196  ],
197  [
200  ],
201  [
204  ],
205  [
208  ],
209  [
212  ],
213  [
216  ],
217  [
220  ],
221  [
224  ],
225  [
228  ],
229  [
232  ],
233  [
236  ],
237  [
240  ],
241  [
244  ]
245  ]
246  );
247  }
248  return $perms;
249  }
250 
251  protected function initForm(string $a_mode = 'create'): ilPropertyFormGUI
252  {
253  $perms = [];
254  $se_mu = null;
255  $perm_map = [];
256  $udf_type = null;
257 
259  $this->lng->loadLanguageModule('ps');
260  $this->main_tpl->setOnScreenMessage('info', $this->lng->txt('ps_warning_modify'));
261  }
262 
263  if ($this->field_definition) {
264  $perms = $this->initFieldDefinition();
265  $perm_map = self::getAccessPermissions();
266  }
267 
268  $form = new ilPropertyFormGUI();
269  $form->setFormAction($this->ctrl->getFormAction($this));
270 
271  $name = new ilTextInputGUI($this->lng->txt('field_name'), 'name');
272  $name->setRequired(true);
273  $form->addItem($name);
274 
276  $name->setDisabled(true);
277  }
278 
279  // type
280  $radg = new ilRadioGroupInputGUI($this->lng->txt('field_type'), 'field_type');
281  $radg->setRequired(true);
282  foreach (ilCustomUserFieldsHelper::getInstance()->getUDFTypes() as $udf_type => $udf_name) {
283  $op = new ilRadioOption($udf_name, (string) $udf_type);
284  $radg->addOption($op);
285 
286  switch ($udf_type) {
287  case UDF_TYPE_TEXT:
288  case UDF_TYPE_WYSIWYG:
289  // do nothing
290  break;
291  case UDF_TYPE_SELECT:
292  // select values
293  $se_mu = new ilTextWizardInputGUI($this->lng->txt('value'), 'selvalue');
294  $se_mu->setRequired(true);
295  $se_mu->setSize(32);
296  $se_mu->setMaxLength(128);
297  $se_mu->setValues(['']);
298  $op->addSubItem($se_mu);
299  break;
300 
301  default:
302  $plugin = ilCustomUserFieldsHelper::getInstance()->getPluginForType($udf_type);
303  if ($plugin instanceof ilUDFDefinitionPlugin) {
304  $plugin->addDefinitionTypeOptionsToRadioOption($op, $this->field_id);
305  }
306  break;
307  }
308  }
309 
310  $form->addItem($radg);
311 
313  $se_mu->setDisabled(true);
314  $se_mu->setRequired(false);
315  }
316 
317 
318  // access
319  $acc = new ilCheckboxGroupInputGUI($this->lng->txt('access'), 'access');
320 
321  $acc_values = [];
322  foreach ($this->getAccessOptions() as $id => $caption) {
323  $opt = new ilCheckboxOption($caption, $id);
324  $acc->addOption($opt);
325 
326  if ($this->field_definition && $this->field_definition[$id]) {
327  $acc_values[] = $id;
328  }
329 
330  if ($perms && !$perms[ilUDFPermissionHelper::ACTION_FIELD_EDIT_ACCESS][$perm_map[$id]]) {
331  $opt->setDisabled(true);
332  }
333  }
334 
335  $form->addItem($acc);
336 
337 
338  if ($a_mode == 'create') {
339  $radg->setValue((string) UDF_TYPE_TEXT);
340  $form->setTitle($this->lng->txt('add_new_user_defined_field'));
341  $form->addCommandButton('create', $this->lng->txt('save'));
342  } else {
343  $name->setValue($this->field_definition['field_name']);
344  $radg->setValue((string) $this->field_definition['field_type']);
345  $radg->setDisabled(true);
346  $acc->setValue($acc_values);
347 
348  switch ($this->field_definition['field_type']) {
349  case UDF_TYPE_SELECT:
350  $values = $this->field_definition['field_values'];
351  if (!is_array($values) || $values === []) {
352  $values = [''];
353  }
354  $se_mu->setValue($values);
355  $form->setTitle($this->lng->txt('udf_update_select_field'));
356  break;
357 
358  case UDF_TYPE_TEXT:
359  $form->setTitle($this->lng->txt('udf_update_text_field'));
360  break;
361 
362  case UDF_TYPE_WYSIWYG:
363  $form->setTitle($this->lng->txt('udf_update_wysiwyg_field'));
364  break;
365 
366  default:
367  $plugin = ilCustomUserFieldsHelper::getInstance()->getPluginForType($udf_type);
368  if ($plugin instanceof ilUDFDefinitionPlugin) {
369  $form->setTitle($plugin->getDefinitionUpdateFormTitle());
370  }
371  break;
372  }
373  $form->addCommandButton('update', $this->lng->txt('save'));
374  }
375  $form->addCommandButton('listUserDefinedFields', $this->lng->txt('cancel'));
376  return $form;
377  }
378 
379  protected function validateForm(
380  ilPropertyFormGUI $form,
381  ilUserDefinedFields $user_field_definitions,
382  array &$access,
383  ?array $a_field_permissions = null
384  ): bool {
385  $perm_map = [];
386 
387  if ($form->checkInput()) {
388  $valid = true;
389 
390  $incoming = (array) $form->getInput('access');
391 
392  if ($a_field_permissions) {
393  $perm_map = self::getAccessPermissions();
394  }
395 
396  $access = [];
397  foreach (array_keys($this->getAccessOptions()) as $id) {
398  $access[$id] = in_array($id, $incoming);
399 
400  // disabled fields
401  if ($a_field_permissions && !$a_field_permissions[ilUDFPermissionHelper::ACTION_FIELD_EDIT_ACCESS][$perm_map[$id]]) {
402  $access[$id] = $this->field_definition[$id];
403  }
404  }
405 
406  if ($access['required'] && !$access['visib_reg']) {
407  $this->confirm_change = true;
408  $form->getItemByPostVar('access')->setAlert($this->lng->txt('udf_required_requires_visib_reg'));
409  $valid = false;
410  }
411 
412  if (!$this->field_id && $user_field_definitions->nameExists($form->getInput('name'))) {
413  $form->getItemByPostVar('name')->setAlert($this->lng->txt('udf_name_already_exists'));
414  $valid = false;
415  }
416 
417  if ($form->getInput('field_type') == UDF_TYPE_SELECT &&
419  $user_field_definitions->setFieldValues($form->getInput('selvalue'));
420  if ($error = $user_field_definitions->validateValues()) {
421  switch ($error) {
423  $form->getItemByPostVar('selvalue')->setAlert($this->lng->txt('udf_duplicate_entries'));
424  $valid = false;
425  break;
426  }
427  }
428  }
429 
430  if (!$valid) {
431  $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
432  }
433  return $valid;
434  }
435 
436  return false;
437  }
438 
439  public function create(): void
440  {
441  $user_field_definitions = ilUserDefinedFields::_getInstance();
442  $user_field_definitions->setFieldType(
443  $this->request->getFieldType()
444  );
445 
446  $access = [];
447  $form = $this->initForm('create');
448  if ($this->validateForm($form, $user_field_definitions, $access)) {
449  $user_field_definitions->setFieldName($form->getInput('name'));
450  $user_field_definitions->enableVisible($access['visible']);
451  $user_field_definitions->enableVisibleRegistration((bool) $access['visib_reg']);
452  $user_field_definitions->enableVisibleLocalUserAdministration($access['visib_lua']);
453  $user_field_definitions->enableCourseExport($access['course_export']);
454  $user_field_definitions->enableGroupExport($access['group_export']);
455  $user_field_definitions->enablePrgExport($access['prg_export']);
456  $user_field_definitions->enableChangeable($access['changeable']);
457  $user_field_definitions->enableChangeableLocalUserAdministration($access['changeable_lua']);
458  $user_field_definitions->enableRequired($access['required']);
459  $user_field_definitions->enableExport($access['export']);
460  $user_field_definitions->enableSearchable($access['searchable']);
461  $user_field_definitions->enableCertificate($access['certificate']);
462  $new_id = $user_field_definitions->add();
463 
464  if ($user_field_definitions->isPluginType()) {
465  $plugin = ilCustomUserFieldsHelper::getInstance()->getPluginForType($user_field_definitions->getFieldType());
466  if ($plugin instanceof ilUDFDefinitionPlugin) {
467  $plugin->updateDefinitionFromForm($form, $new_id);
468  }
469  }
470  if ($access['course_export']) {
472  }
473 
474  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt('udf_added_field'), true);
475  $this->ctrl->redirect($this);
476  }
477 
478  $form->setValuesByPost();
479  $this->addField($form);
480  }
481 
482  public function edit(?ilPropertyFormGUI $a_form = null): void
483  {
484  if (!$a_form) {
485  $a_form = $this->initForm('edit');
486  }
487 
488  $this->main_tpl->setContent($a_form->getHTML());
489  }
490 
491  public function update(): void
492  {
493  $perms = [];
494 
495  $user_field_definitions = ilUserDefinedFields::_getInstance();
496  $user_field_definitions->setFieldType($this->field_definition['field_type']);
497 
498  // gather old select options
499  $old_options = null;
500  if ($this->field_id) {
501  $old_values = $user_field_definitions->getDefinition($this->field_id);
502  if ($old_values['field_type'] == UDF_TYPE_SELECT) {
503  $old_options = $old_values['field_values'];
504  }
505 
506  $perms = $this->permissions->hasPermissions(
508  (string) $this->field_id,
509  [
510  [
513  ],
514  [
517  ],
518  [
521  ],
522  [
525  ],
526  [
529  ],
530  [
533  ],
534  [
537  ],
538  [
541  ],
542  [
545  ],
546  [
549  ],
550  [
553  ],
554  [
557  ],
558  [
561  ],
562  [
565  ]
566  ]
567  );
568  }
569 
570  $access = [];
571  $form = $this->initForm('edit');
572  if ($this->validateForm($form, $user_field_definitions, $access, $perms) && $this->field_id) {
573  // field values are set in validateForm()...
574 
576  // diff old select options against new to handle deleted values properly
577  if (is_array($old_options)) {
578  foreach ($old_options as $old_option) {
579  if (!in_array($old_option, $user_field_definitions->getFieldValues())) {
580  ilUserDefinedData::deleteFieldValue($this->field_id, $old_option);
581  }
582  }
583  }
584  }
585  // disabled fields
586  elseif (is_array($old_options)) {
587  $user_field_definitions->setFieldValues($old_options);
588  }
589 
591  $user_field_definitions->setFieldName($form->getInput('name'));
592  } else {
593  $user_field_definitions->setFieldName($this->field_definition['field_name']);
594  }
595 
596  $user_field_definitions->enableVisible($access['visible']);
597  $user_field_definitions->enableVisibleRegistration($access['visib_reg']);
598  $user_field_definitions->enableVisibleLocalUserAdministration($access['visib_lua']);
599  $user_field_definitions->enableCourseExport($access['course_export']);
600  $user_field_definitions->enableGroupExport($access['group_export']);
601  $user_field_definitions->enablePrgExport($access['prg_export']);
602  $user_field_definitions->enableChangeable($access['changeable']);
603  $user_field_definitions->enableChangeableLocalUserAdministration($access['changeable_lua']);
604  $user_field_definitions->enableRequired($access['required']);
605  $user_field_definitions->enableExport($access['export']);
606  $user_field_definitions->enableSearchable($access['searchable']);
607  $user_field_definitions->enableCertificate($access['certificate']);
608  $user_field_definitions->update($this->field_id);
609 
610  if ($user_field_definitions->isPluginType()) {
611  $plugin = ilCustomUserFieldsHelper::getInstance()->getPluginForType($user_field_definitions->getFieldType());
612  if ($plugin instanceof ilUDFDefinitionPlugin) {
613  $plugin->updateDefinitionFromForm($form, $this->field_id);
614  }
615  }
616 
617  if ($access['course_export']) {
619  }
620 
621  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
622  $this->ctrl->redirect($this);
623  }
624 
625  $form->setValuesByPost();
626  $this->edit($form);
627  }
628 
629  public function askDeleteField(): bool
630  {
631  $fields = $this->request->getFields();
632  if (count($fields) == 0) {
633  $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('select_one'));
634  $this->listUserDefinedFields();
635  return false;
636  }
637 
638  $confirmation_gui = new ilConfirmationGUI();
639  $confirmation_gui->setFormAction($this->ctrl->getFormAction($this));
640  $confirmation_gui->setHeaderText($this->lng->txt('udf_delete_sure'));
641  $confirmation_gui->setCancel($this->lng->txt('cancel'), 'listUserDefinedFields');
642  $confirmation_gui->setConfirm($this->lng->txt('delete'), 'deleteField');
643 
644  $user_field_definitions = ilUserDefinedFields::_getInstance();
645  foreach ($fields as $id) {
646  $definition = $user_field_definitions->getDefinition($id);
647  $confirmation_gui->addItem('fields[]', (string) $id, $definition['field_name']);
648  }
649 
650  $this->main_tpl->setContent($confirmation_gui->getHTML());
651 
652  return true;
653  }
654 
655  public function deleteField(): void
656  {
657  $user_field_definitions = ilUserDefinedFields::_getInstance();
658  $fields = $this->request->getFields();
659 
660  // all fields have to be deletable
661  $fail = [];
662  foreach ($fields as $id) {
663  if (!$this->getPermissions()->hasPermission(
665  (string) $id,
667  )) {
668  $field = $user_field_definitions->getDefinition($id);
669  $fail[] = $field['field_name'];
670  }
671  }
672  if ($fail) {
673  $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('msg_no_perm_delete') . ' ' . implode(', ', $fail), true);
674  $this->ctrl->redirect($this, 'listUserDefinedFields');
675  }
676 
677  foreach ($fields as $id) {
678  $user_field_definitions->delete($id);
679  }
680 
681  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt('udf_field_deleted'), true);
682  $this->ctrl->redirect($this);
683  }
684 
688  public function updateFields(string $action = ''): void
689  {
690  $user_field_definitions = ilUserDefinedFields::_getInstance();
691  $a_fields = $user_field_definitions->getDefinitions();
692 
693  $checked = $this->request->getChecked();
694 
695  $perm_map = self::getAccessPermissions();
696 
697  foreach ($a_fields as $field_id => $definition) {
698  $perms = $this->permissions->hasPermissions(
700  (string) $field_id,
701  [
702  [
705  ],
706  [
709  ],
710  [
713  ],
714  [
717  ],
718  [
721  ],
722  [
725  ],
726  [
729  ],
730  [
733  ],
734  [
737  ],
738  [
741  ],
742  [
745  ],
746  [
749  ]
750  ]
751  );
752 
753  // disabled field
754  foreach ($perm_map as $prop => $perm) {
756  $checked[$prop . '_' . $field_id] = $definition[$prop];
757  }
758  }
759  }
760 
761  $valid = true;
762  foreach ($a_fields as $field_id => $definition) {
763  if (isset($checked['required_' . $field_id]) && (int) $checked['required_' . $field_id] &&
764  (!isset($checked['visib_reg_' . $field_id]) || !(int) $checked['visib_reg_' . $field_id])) {
765  $this->confirm_change = true;
766 
767  $this->main_tpl->setOnScreenMessage('failure', $this->lng->txt('invalid_visible_required_options_selected'));
768  $valid = false;
769  }
770  }
771 
772  if (!$valid) {
773  $this->listUserDefinedFields();
774  return;
775  }
776 
777  foreach ($a_fields as $field_id => $definition) {
778  $user_field_definitions->setFieldName($definition['field_name']);
779  $user_field_definitions->setFieldType($definition['field_type']);
780  $user_field_definitions->setFieldValues($definition['field_values']);
781  $user_field_definitions->enableVisible((bool) ($checked['visible_' . $field_id] ?? false));
782  $user_field_definitions->enableChangeable((bool) ($checked['changeable_' . $field_id] ?? false));
783  $user_field_definitions->enableRequired((bool) ($checked['required_' . $field_id] ?? false));
784  $user_field_definitions->enableSearchable((bool) ($checked['searchable_' . $field_id] ?? false));
785  $user_field_definitions->enableExport((bool) ($checked['export_' . $field_id] ?? false));
786  $user_field_definitions->enableCourseExport((bool) ($checked['course_export_' . $field_id] ?? false));
787  $user_field_definitions->enableVisibleLocalUserAdministration((bool) ($checked['visib_lua_' . $field_id] ?? false));
788  $user_field_definitions->enableChangeableLocalUserAdministration((bool) ($checked['changeable_lua_' . $field_id] ?? false));
789  $user_field_definitions->enableGroupExport((bool) ($checked['group_export_' . $field_id] ?? false));
790  $user_field_definitions->enableVisibleRegistration((bool) ($checked['visib_reg_' . $field_id] ?? false));
791  $user_field_definitions->enableCertificate((bool) ($checked['certificate_' . $field_id] ?? false));
792  $user_field_definitions->enablePrgExport((bool) ($checked['prg_export_' . $field_id] ?? false));
793 
794  $user_field_definitions->update($field_id);
795  }
796 
797  $this->main_tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
798  $this->ctrl->redirect($this);
799  }
800 }
const UDF_TYPE_SELECT
This class represents an option in a radio group.
validateForm(ilPropertyFormGUI $form, ilUserDefinedFields $user_field_definitions, array &$access, ?array $a_field_permissions=null)
This class represents an option in a checkbox group.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
edit(?ilPropertyFormGUI $a_form=null)
getItemByPostVar(string $a_post_var)
Additional user data fields definition.
ilGlobalTemplateInterface $main_tpl
ilClaimingPermissionHelper $permissions
$valid
addField(?ilPropertyFormGUI $a_form=null)
static _reset()
Reset all.
getAccessOptions()
Get all access options, order is kept in forms.
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Class ilCustomUserFieldsGUI.
This class represents a property in a property form.
TableGUI class for custom defined user fields.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
nameExists(string $a_field_name)
global $DIC
Definition: shib_login.php:22
const UDF_TYPE_TEXT
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(int $ref_id, int $requested_field_id)
This class represents a property in a property form.
static _hasAgreements()
Check if there is any user agreement.
setRequired(bool $a_required)
const UDF_TYPE_WYSIWYG
static deleteFieldValue(int $a_field_id, string $a_value)
Delete data of particular value of a (selection) field.
initForm(string $a_mode='create')
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static getInstance(int $a_user_id, int $a_ref_id)
This class represents a text wizard property in a property form.
const UDF_DUPLICATE_VALUES
updateFields(string $action='')
Update custom fields properties (from table gui)