ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilObjectCustomUserFieldsGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
31 {
32  protected const MODE_CREATE = 1;
33  protected const MODE_UPDATE = 2;
34 
36  private ilLanguage $lng;
43  protected ilObjUser $user;
45  protected Factory $refinery;
46  private int $obj_id;
47  private int $ref_id;
48 
49  public function __construct(int $a_obj_id)
50  {
51  global $DIC;
52 
53  $this->http = $DIC->http();
54  $this->refinery = $DIC->refinery();
55 
56  $this->lng = $DIC->language();
57  $this->lng->loadLanguageModule('ps');
58  $this->lng->loadLanguageModule(ilObject::_lookupType($a_obj_id));
59 
60  $this->tpl = $DIC->ui()->mainTemplate();
61  $this->ctrl = $DIC->ctrl();
62  $this->tabs_gui = $DIC->tabs();
63  $this->errorHandling = $DIC['ilErr'];
64  $this->accessHandler = $DIC->access();
65  $this->toolbarGUI = $DIC->toolbar();
66  $this->user = $DIC->user();
67  $this->obj_id = $a_obj_id;
68 
69  // Currently only supported for container objects
70  $refs = ilObject::_getAllReferences($this->obj_id);
71  $this->ref_id = end($refs);
72  }
73 
74  protected function initMemberIdFromQuery(): int
75  {
76  if ($this->http->wrapper()->query()->has('member_id')) {
77  return $this->http->wrapper()->query()->retrieve(
78  'member_id',
79  $this->refinery->kindlyTo()->int()
80  );
81  }
82  return 0;
83  }
84 
85  protected function initFielIdFromQuery(): int
86  {
87  if ($this->http->wrapper()->query()->has('field_id')) {
88  return $this->http->wrapper()->query()->retrieve(
89  'field_id',
90  $this->refinery->kindlyTo()->int()
91  );
92  }
93  return 0;
94  }
95 
96  protected function initRequiredStatusFromPost(): array
97  {
98  if ($this->http->wrapper()->post()->has('required')) {
99  return $this->http->wrapper()->post()->retrieve(
100  'required',
101  $this->refinery->kindlyTo()->dictOf(
102  $this->refinery->kindlyTo()->bool()
103  )
104  );
105  }
106  return [];
107  }
108 
109  public function executeCommand(): void
110  {
111  if (!$this->accessHandler->checkAccess('write', '', $this->ref_id)) {
112  $this->errorHandling->raiseError($this->lng->txt('permission_denied'), $this->errorHandling->WARNING);
113  }
114 
115  $cmd = $this->ctrl->getCmd();
116  switch ($next_class = $this->ctrl->getNextClass($this)) {
117  default:
118  if (!$cmd) {
119  $cmd = 'show';
120  }
121  $this->$cmd();
122  break;
123  }
124  }
125 
126  public function getObjId(): int
127  {
128  return $this->obj_id;
129  }
130 
131  protected function show(): void
132  {
134  $this->tpl->setOnScreenMessage('info', $this->lng->txt('ps_cdf_warning_modify'));
135  }
136  $this->listFields();
137  }
138 
139  protected function listFields(): void
140  {
141  $this->toolbarGUI->addButton(
142  $this->lng->txt('ps_cdf_add_field'),
143  $this->ctrl->getLinkTarget($this, 'addField')
144  );
145  $table = new ilObjectCustomUserFieldsTableGUI($this, 'listFields');
146  $table->parse(ilCourseDefinedFieldDefinition::_getFields($this->getObjId()));
147  $this->tpl->setContent($table->getHTML());
148  }
149 
150  protected function saveFields(): void
151  {
153  foreach ($fields as $field_obj) {
154  $field_obj->enableRequired($this->initRequiredStatusFromPost()[$field_obj->getId()] ?? false);
155  $field_obj->update();
156  }
157 
159  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'));
160  $this->listFields();
161  }
162 
163  protected function confirmDeleteFields(): void
164  {
165  $field_ids = [];
166  if ($this->http->wrapper()->post()->has('field_ids')) {
167  $field_ids = $this->http->wrapper()->post()->retrieve(
168  'field_ids',
169  $this->refinery->kindlyTo()->listOf(
170  $this->refinery->kindlyTo()->int()
171  )
172  );
173  }
174 
175  if (!count($field_ids)) {
176  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('ps_cdf_select_one'));
177  $this->listFields();
178  return;
179  }
180  $confirm = new ilConfirmationGUI();
181  $confirm->setFormAction($this->ctrl->getFormAction($this));
182  $confirm->setHeaderText($this->lng->txt('ps_cdf_delete_sure'));
183 
184  foreach ($field_ids as $field_id) {
185  $tmp_field = new ilCourseDefinedFieldDefinition($this->getObjId(), $field_id);
186 
187  $confirm->addItem('field_ids[]', (string) $field_id, $tmp_field->getName());
188  }
189 
190  $confirm->setConfirm($this->lng->txt('delete'), 'deleteFields');
191  $confirm->setCancel($this->lng->txt('cancel'), 'listFields');
192  $this->tpl->setContent($confirm->getHTML());
193  }
194 
195  protected function deleteFields(): void
196  {
197  $field_ids = [];
198  if ($this->http->wrapper()->post()->has('field_ids')) {
199  $field_ids = $this->http->wrapper()->post()->retrieve(
200  'field_ids',
201  $this->refinery->kindlyTo()->listOf(
202  $this->refinery->kindlyTo()->int()
203  )
204  );
205  }
206 
207  foreach ($field_ids as $field_id) {
208  $tmp_field = new ilCourseDefinedFieldDefinition($this->obj_id, $field_id);
209  $tmp_field->delete();
210  }
211 
212  ilMemberAgreement::_deleteByObjId($this->obj_id);
213 
214  $this->tpl->setOnScreenMessage('success', $this->lng->txt('ps_cdf_deleted'));
215  $this->listFields();
216  }
217 
218  protected function addField(): void
219  {
220  $this->initFieldForm(self::MODE_CREATE);
221  $this->form->getItemByPostVar('va')->setValues(array(''));
222  $this->tpl->setContent($this->form->getHTML());
223  }
224 
225  protected function saveField(): void
226  {
227  $this->initFieldForm(self::MODE_CREATE);
228  if ($this->form->checkInput()) {
229  $udf = new ilCourseDefinedFieldDefinition($this->getObjId());
230  $udf->setName((string) $this->form->getInput('na'));
231  $udf->setType((int) $this->form->getInput('ty'));
232  $udf->setValues($udf->prepareValues($this->form->getInput('va')));
233  $udf->setValueOptions($this->form->getItemByPostVar('va')->getOpenAnswerIndexes()); // #14720
234  $udf->enableRequired((bool) $this->form->getInput('re'));
235  $udf->save();
236 
237  $this->tpl->setOnScreenMessage('success', $this->lng->txt('ps_cdf_added_field'));
238  // reset agreements
240  $this->listFields();
241  return;
242  }
243  // not valid
244  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_check_input'));
245  $this->form->setValuesByPost();
246  $this->tpl->setContent($this->form->getHTML());
247  }
248 
249  protected function editField(): void
250  {
251  if (!$this->initFielIdFromQuery()) {
252  $this->listFields();
253  return;
254  }
255 
256  $this->initFieldForm(self::MODE_UPDATE);
257  $udf = new ilCourseDefinedFieldDefinition($this->getObjId(), $this->initFielIdFromQuery());
258  $this->form->getItemByPostVar('na')->setValue($udf->getName());
259  $this->form->getItemByPostVar('ty')->setValue((string) $udf->getType());
260  $this->form->getItemByPostVar('re')->setChecked($udf->isRequired());
261  $this->form->getItemByPostVar('va')->setValues($udf->getValues());
262  $this->form->getItemByPostVar('va')->setOpenAnswerIndexes($udf->getValueOptions());
263  $this->tpl->setContent($this->form->getHTML());
264  }
265 
266  protected function updateField(): void
267  {
268  $this->initFieldForm(self::MODE_UPDATE);
269  if ($this->form->checkInput()) {
270  $udf = new ilCourseDefinedFieldDefinition($this->getObjId(), $this->initFielIdFromQuery());
271  $udf->setName($this->form->getInput('na'));
272  $udf->setType((int) $this->form->getInput('ty'));
273  $prepared = $udf->prepareValues($this->form->getInput('va'));
274  $udf->setValues($prepared);
275  $udf->setValueOptions($this->form->getItemByPostVar('va')->getOpenAnswerIndexes());
276  $udf->enableRequired((bool) $this->form->getInput('re'));
277  $udf->update();
278 
279  // Finally reset member agreements
281  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'));
282  $this->listFields();
283  return;
284  }
285 
286  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_check_input'));
287  $this->form->setValuesByPost();
288  $this->tpl->setContent($this->form->getHTML());
289  }
290 
291  protected function initFieldForm(int $a_mode): ilPropertyFormGUI
292  {
293  if ($this->form instanceof ilPropertyFormGUI) {
294  return $this->form;
295  }
296  $this->form = new ilPropertyFormGUI();
297 
298  switch ($a_mode) {
299  case self::MODE_CREATE:
300  $this->form->setFormAction($this->ctrl->getFormAction($this));
301  $this->form->setTitle($this->lng->txt('ps_cdf_add_field'));
302  $this->form->addCommandButton('saveField', $this->lng->txt('save'));
303  $this->form->addCommandButton('listFields', $this->lng->txt('cancel'));
304  break;
305 
306  case self::MODE_UPDATE:
307  $this->ctrl->setParameter($this, 'field_id', $this->initFielIdFromQuery());
308  $this->form->setFormAction($this->ctrl->getFormAction($this));
309  $this->form->setTitle($this->lng->txt('ps_cdf_edit_field'));
310  $this->form->addCommandButton('updateField', $this->lng->txt('save'));
311  $this->form->addCommandButton('listFields', $this->lng->txt('cancel'));
312  break;
313  }
314 
315  // Name
316  $na = new ilTextInputGUI($this->lng->txt('ps_cdf_name'), 'na');
317  $na->setSize(32);
318  $na->setMaxLength(255);
319  $na->setRequired(true);
320  $this->form->addItem($na);
321 
322  // Type
323  $ty = new ilRadioGroupInputGUI($this->lng->txt('ps_field_type'), 'ty');
324  $ty->setRequired(true);
325  $this->form->addItem($ty);
326 
327  if ($a_mode === self::MODE_UPDATE) {
328  $ty->setDisabled(true); // #14888
329  }
330 
331  // Text type
332  $ty_te = new ilRadioOption($this->lng->txt('ps_type_txt_long'), (string) ilCourseDefinedFieldDefinition::IL_CDF_TYPE_TEXT);
333  $ty->addOption($ty_te);
334 
335  // Select Type
336  $ty_se = new ilRadioOption($this->lng->txt('ps_type_select_long'), (string) ilCourseDefinedFieldDefinition::IL_CDF_TYPE_SELECT);
337  $ty->addOption($ty_se);
338 
339  // Select Type Values
340  $ty_se_mu = new ilSelectBuilderInputGUI($this->lng->txt('ps_cdf_value'), 'va');
341  $ty_se_mu->setAllowMove(true);
342  $ty_se_mu->setRequired(true);
343  $ty_se_mu->setSize(32);
344  $ty_se_mu->setMaxLength(128);
345  $ty_se->addSubItem($ty_se_mu);
346 
347  // Required
348  $re = new ilCheckboxInputGUI($this->lng->txt('ps_cdf_required'), 're');
349  $re->setValue("1");
350  $this->form->addItem($re);
351  return $this->form;
352  }
353 
354  protected function editMember(?ilPropertyFormGUI $form = null): void
355  {
356  $member_id = $this->initMemberIdFromQuery();
357  $this->ctrl->saveParameter($this, 'member_id');
358 
359  $this->tabs_gui->clearTargets();
360  $this->tabs_gui->clearSubTabs();
361  $this->tabs_gui->setBackTarget(
362  $this->lng->txt('back'),
363  $this->ctrl->getLinkTarget($this, 'cancelEditMember')
364  );
365  if ($form instanceof ilPropertyFormGUI) {
366  $this->tpl->setContent($form->getHTML());
367  } else {
368  $form = $this->initMemberForm();
370  $form,
371  $this->getObjId(),
372  $member_id
373  );
374  }
375  $this->tpl->setContent($form->getHTML());
376  }
377 
378  protected function cancelEditMember(): void
379  {
380  $this->ctrl->returnToParent($this);
381  }
382 
383  protected function initMemberForm(): ilPropertyFormGUI
384  {
385  $member_id = $this->initMemberIdFromQuery();
386  $form = new ilPropertyFormGUI();
387  $form->setFormAction($this->ctrl->getFormAction($this));
388  $title = $this->lng->txt(ilObject::_lookupType($this->getObjId()) . '_cdf_edit_member');
389  $name = ilObjUser::_lookupName($member_id);
390  $title .= (': ' . $name['lastname'] . ', ' . $name['firstname']);
391  $form->setTitle($title);
392 
394  $form,
395  $this->getObjId(),
397  'edit'
398  );
399  $form->addCommandButton('saveMember', $this->lng->txt('save'));
400  $form->addCommandButton('cancelEditMember', $this->lng->txt('cancel'));
401  return $form;
402  }
403 
404  protected function saveMember(): void
405  {
406  $member_id = $this->initMemberIdFromQuery();
407  $this->ctrl->saveParameter($this, 'member_id');
408 
409  $form = $this->initMemberForm();
410  if ($form->checkInput()) {
411  // save history
412  $history = new ilObjectCustomUserFieldHistory($this->getObjId(), $member_id);
413  $history->setEditingTime(new ilDateTime(time(), IL_CAL_UNIX));
414  $history->setUpdateUser($this->user->getId());
415  $history->save();
416 
417  ilMemberAgreementGUI::saveCourseDefinedFields($form, $this->getObjId(), $member_id);
418  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
419  $this->ctrl->returnToParent($this);
420  return;
421  }
422 
423  $form->setValuesByPost();
424  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_check_input'));
425  $this->editMember($form);
426  }
427 }
This class represents an option in a radio group.
static _hasAgreementsByObjId(int $a_obj_id)
Check if there is any user agreement.
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...
static _getAllReferences(int $id)
get all reference ids for object ID
static _lookupName(int $a_user_id)
lookup user name
Editing history for object custom user fields.
static _getFields(int $a_container_id, $a_sort=self::IL_CDF_SORT_NAME)
Get all fields of a container.
const IL_CAL_UNIX
static addCustomFields(ilPropertyFormGUI $form, int $a_obj_id, string $a_type, string $a_mode='user')
Table presentation of course/group relevant user data fields
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static http()
Fetches the global http state from ILIAS.
This class represents a property in a property form.
setFormAction(string $a_formaction)
setAllowMove(bool $a_allow_move)
global $DIC
Definition: shib_login.php:22
static saveCourseDefinedFields(ilPropertyFormGUI $form, int $a_obj_id, int $a_usr_id=0)
static _deleteByObjId(int $a_obj_id)
Delete all entries by obj_id.
editMember(?ilPropertyFormGUI $form=null)
setRequired(bool $a_required)
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
Input GUI for the configuration of select input elements.
form( $class_path, string $cmd, string $submit_caption="")
static setCourseDefinedFieldValues(ilPropertyFormGUI $form, int $a_obj_id, int $a_usr_id=0)
static _lookupType(int $id, bool $reference=false)