ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilObjectCustomUserFieldsGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
24 include_once('Services/Membership/classes/class.ilMemberAgreement.php');
25 
36 {
37  const MODE_CREATE = 1;
38  const MODE_UPDATE = 2;
39 
40  private $form = null;
41 
42  private $lng;
43  private $tpl;
44  private $ctrl;
45  private $tabs_gui;
46 
47  private $obj_id;
48  private $ref_id;
49 
50  private $cdf;
51 
59  public function __construct($a_obj_id)
60  {
61  global $DIC;
62 
63  $lng = $DIC['lng'];
64  $tpl = $DIC['tpl'];
65  $ilCtrl = $DIC['ilCtrl'];
66  $ilTabs = $DIC['ilTabs'];
67 
68  $this->lng = $lng;
69  $this->lng->loadLanguageModule('ps');
70  $this->lng->loadLanguageModule(ilObject::_lookupType($a_obj_id));
71 
72  $this->tpl = $tpl;
73  $this->ctrl = $ilCtrl;
74  $this->tabs_gui = $ilTabs;
75 
76  $this->obj_id = $a_obj_id;
77 
78  // Currently only supported for container objects
79  $refs = ilObject::_getAllReferences($this->obj_id);
80  $this->ref_id = end($refs);
81  }
82 
89  public function executeCommand()
90  {
91  global $DIC;
92 
93  $ilErr = $DIC['ilErr'];
94  $ilAccess = $DIC['ilAccess'];
95  $lng = $DIC['lng'];
96 
97  if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
98  $ilErr->raiseError($lng->txt('permission_denied'), $ilErr->WARNING);
99  }
100 
101  $cmd = $this->ctrl->getCmd();
102 
103  switch ($next_class = $this->ctrl->getNextClass($this)) {
104  default:
105  if (!$cmd) {
106  $cmd = 'show';
107  }
108  $this->$cmd();
109  break;
110  }
111  }
112 
117  public function getObjId()
118  {
119  return $this->obj_id;
120  }
121 
126  protected function show()
127  {
129  ilUtil::sendInfo($this->lng->txt('ps_cdf_warning_modify'));
130  }
131  $this->listFields();
132  }
133 
138  protected function listFields()
139  {
140  global $DIC;
141 
142  $ilToolbar = $DIC['ilToolbar'];
143 
144  $ilToolbar->addButton(
145  $this->lng->txt('ps_cdf_add_field'),
146  $this->ctrl->getLinkTarget($this, 'addField')
147  );
148 
149  include_once './Services/Membership/classes/class.ilObjectCustomUserFieldsTableGUI.php';
150  $table = new ilObjectCustomUserFieldsTableGUI($this, 'listFields');
152  $this->tpl->setContent($table->getHTML());
153  }
154 
159  protected function saveFields()
160  {
162  foreach ($fields as $field_obj) {
163  $field_obj->enableRequired((bool) isset($_POST['required'][$field_obj->getId()]));
164  $field_obj->update();
165  }
166 
168  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
169  $this->listFields();
170  return true;
171  }
172 
177  protected function confirmDeleteFields()
178  {
179  if (!count($_POST['field_ids'])) {
180  ilUtil::sendFailure($this->lng->txt('ps_cdf_select_one'));
181  $this->listFields();
182  return false;
183  }
184  include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
185  $confirm = new ilConfirmationGUI();
186  $confirm->setFormAction($this->ctrl->getFormAction($this));
187  $confirm->setHeaderText($this->lng->txt('ps_cdf_delete_sure'));
188 
189  foreach ($_POST['field_ids'] as $field_id) {
190  $tmp_field = new ilCourseDefinedFieldDefinition($this->getObjId(), $field_id);
191 
192  $confirm->addItem('field_ids[]', $field_id, $tmp_field->getName());
193  }
194 
195  $confirm->setConfirm($this->lng->txt('delete'), 'deleteFields');
196  $confirm->setCancel($this->lng->txt('cancel'), 'listFields');
197  $this->tpl->setContent($confirm->getHTML());
198  }
199 
204  protected function deleteFields()
205  {
206  foreach ((array) $_POST['field_ids'] as $field_id) {
207  $tmp_field = new ilCourseDefinedFieldDefinition($this->obj_id, $field_id);
208  $tmp_field->delete();
209  }
210 
211  ilMemberAgreement::_deleteByObjId($this->obj_id);
212 
213  ilUtil::sendSuccess($this->lng->txt('ps_cdf_deleted'));
214  $this->listFields();
215  return true;
216  }
217 
222  protected function addField()
223  {
224  $this->initFieldForm(self::MODE_CREATE);
225 
226  $this->form->getItemByPostVar('va')->setValues(array(''));
227 
228  $this->tpl->setContent($this->form->getHTML());
229  }
230 
235  protected function saveField()
236  {
237  $GLOBALS['DIC']->logger()->mmbr()->dump($_POST);
238  $this->initFieldForm(self::MODE_CREATE);
239  if ($this->form->checkInput()) {
240  $udf = new ilCourseDefinedFieldDefinition($this->getObjId());
241  $udf->setName($this->form->getInput('na'));
242  $udf->setType($this->form->getInput('ty'));
243  $udf->setValues($udf->prepareValues($this->form->getInput('va')));
244  $udf->setValueOptions($this->form->getItemByPostVar('va')->getOpenAnswerIndexes()); // #14720
245  $udf->enableRequired($this->form->getInput('re'));
246  $udf->save();
247 
248  ilUtil::sendSuccess($this->lng->txt('ps_cdf_added_field'));
249  // reset agreements
251  $this->listFields();
252  return true;
253  }
254  // not valid
255  ilUtil::sendFailure($this->lng->txt('err_check_input'));
256  $this->form->setValuesByPost();
257  $this->tpl->setContent($this->form->getHTML());
258  return false;
259  }
260 
266  protected function editField()
267  {
268  if (!$_REQUEST['field_id']) {
269  $this->listFields();
270  return false;
271  }
272 
273  $this->initFieldForm(self::MODE_UPDATE);
274 
275  $udf = new ilCourseDefinedFieldDefinition($this->getObjId(), (int) $_REQUEST['field_id']);
276  $this->form->getItemByPostVar('na')->setValue($udf->getName());
277  $this->form->getItemByPostVar('ty')->setValue($udf->getType());
278  $this->form->getItemByPostVar('re')->setChecked($udf->isRequired());
279  $this->form->getItemByPostVar('va')->setValues($udf->getValues());
280  $this->form->getItemByPostVar('va')->setOpenAnswerIndexes($udf->getValueOptions());
281 
282  $this->tpl->setContent($this->form->getHTML());
283  }
284 
289  protected function updateField()
290  {
291  $this->initFieldForm(self::MODE_UPDATE);
292 
293  if ($this->form->checkInput()) {
294  $udf = new ilCourseDefinedFieldDefinition($this->getObjId(), (int) $_REQUEST['field_id']);
295  $udf->setName($this->form->getInput('na'));
296  $udf->setType($this->form->getInput('ty'));
297  $prepared = $udf->prepareValues($this->form->getInput('va'));
298  $udf->setValues($prepared);
299  $udf->setValueOptions($this->form->getItemByPostVar('va')->getOpenAnswerIndexes());
300  $udf->enableRequired($this->form->getInput('re'));
301  $udf->update();
302 
303  // Finally reset member agreements
305  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
306  $this->listFields();
307  return true;
308  }
309 
310  ilUtil::sendFailure($this->lng->txt('err_check_input'));
311  $this->form->setValuesByPost();
312  $this->tpl->setContent($this->form->getHTML());
313  return false;
314  }
315 
320  protected function initFieldForm($a_mode)
321  {
322  if ($this->form instanceof ilPropertyFormGUI) {
323  return true;
324  }
325  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
326  $this->form = new ilPropertyFormGUI();
327 
328  switch ($a_mode) {
329  case self::MODE_CREATE:
330  $this->form->setFormAction($this->ctrl->getFormAction($this));
331  $this->form->setTitle($this->lng->txt('ps_cdf_add_field'));
332  $this->form->addCommandButton('saveField', $this->lng->txt('save'));
333  $this->form->addCommandButton('listFields', $this->lng->txt('cancel'));
334  break;
335 
336  case self::MODE_UPDATE:
337  $this->ctrl->setParameter($this, 'field_id', (int) $_REQUEST['field_id']);
338  $this->form->setFormAction($this->ctrl->getFormAction($this));
339  $this->form->setTitle($this->lng->txt('ps_cdf_edit_field'));
340  $this->form->addCommandButton('updateField', $this->lng->txt('save'));
341  $this->form->addCommandButton('listFields', $this->lng->txt('cancel'));
342  break;
343  }
344 
345  // Name
346  $na = new ilTextInputGUI($this->lng->txt('ps_cdf_name'), 'na');
347  $na->setSize(32);
348  $na->setMaxLength(255);
349  $na->setRequired(true);
350  $this->form->addItem($na);
351 
352  // Type
353  $ty = new ilRadioGroupInputGUI($this->lng->txt('ps_field_type'), 'ty');
354  $ty->setRequired(true);
355  $this->form->addItem($ty);
356 
357  if ($a_mode == self::MODE_UPDATE) {
358  $ty->setDisabled(true); // #14888
359  }
360 
361  // Text type
362  $ty_te = new ilRadioOption($this->lng->txt('ps_type_txt_long'), IL_CDF_TYPE_TEXT);
363  $ty->addOption($ty_te);
364 
365  // Select Type
366  $ty_se = new ilRadioOption($this->lng->txt('ps_type_select_long'), IL_CDF_TYPE_SELECT);
367  $ty->addOption($ty_se);
368 
369  // Select Type Values
370  include_once './Services/Form/classes/class.ilSelectBuilderInputGUI.php';
371  $ty_se_mu = new ilSelectBuilderInputGUI($this->lng->txt('ps_cdf_value'), 'va');
372  $ty_se_mu->setAllowMove(true);
373  $ty_se_mu->setRequired(true);
374  $ty_se_mu->setSize(32);
375  $ty_se_mu->setMaxLength(128);
376  $ty_se->addSubItem($ty_se_mu);
377 
378  // Required
379  $re = new ilCheckboxInputGUI($this->lng->txt('ps_cdf_required'), 're');
380  $re->setValue(1);
381  $this->form->addItem($re);
382  }
383 
388  protected function editMember(ilPropertyFormGUI $form = null)
389  {
390  $GLOBALS['DIC']['ilCtrl']->saveParameter($this, 'member_id');
391 
392  $GLOBALS['DIC']['ilTabs']->clearTargets();
393  $GLOBALS['DIC']['ilTabs']->clearSubTabs();
394  $GLOBALS['DIC']['ilTabs']->setBackTarget($this->lng->txt('back'), $this->ctrl->getLinkTarget($this, 'cancelEditMember'));
395 
396 
397  if ($form instanceof ilPropertyFormGUI) {
398  $GLOBALS['DIC']['tpl']->setContent($form->getHTML());
399  } else {
400  $form = $this->initMemberForm();
401  ilMemberAgreementGUI::setCourseDefinedFieldValues($form, $this->getObjId(), (int) $_REQUEST['member_id']);
402  }
403 
404  $GLOBALS['DIC']['tpl']->setContent($form->getHTML());
405  }
406 
407 
411  protected function cancelEditMember()
412  {
413  $GLOBALS['DIC']['ilCtrl']->returnToParent($this);
414  }
415 
420  protected function initMemberForm()
421  {
422  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
423  $form = new ilPropertyFormGUI();
424  $form->setFormAction($GLOBALS['DIC']['ilCtrl']->getFormAction($this));
425  $title = $this->lng->txt(ilObject::_lookupType($this->getObjId()) . '_cdf_edit_member');
426  $name = ilObjUser::_lookupName((int) $_REQUEST['member_id']);
427  $title .= (': ' . $name['lastname'] . ', ' . $name['firstname']);
428  $form->setTitle($title);
429 
430  include_once './Services/Membership/classes/class.ilMemberAgreementGUI.php';
432 
433  $form->addCommandButton('saveMember', $this->lng->txt('save'));
434  $form->addCommandButton('cancelEditMember', $this->lng->txt('cancel'));
435 
436  return $form;
437  }
438 
439  protected function saveMember()
440  {
441  global $DIC;
442 
443  $ilUser = $DIC['ilUser'];
444 
445  $GLOBALS['DIC']['ilCtrl']->saveParameter($this, 'member_id');
446 
447  $form = $this->initMemberForm();
448  if ($form->checkInput()) {
449  // save history
450  include_once './Services/Membership/classes/class.ilObjectCustomUserFieldHistory.php';
451  $history = new ilObjectCustomUserFieldHistory($this->getObjId(), (int) $_REQUEST['member_id']);
452  $history->setEditingTime(new ilDateTime(time(), IL_CAL_UNIX));
453  $history->setUpdateUser($ilUser->getId());
454  $history->save();
455 
456  ilMemberAgreementGUI::saveCourseDefinedFields($form, $this->getObjId(), (int) $_REQUEST['member_id']);
457  ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
458  $GLOBALS['DIC']['ilCtrl']->returnToParent($this);
459  return true;
460  }
461 
462  $form->setValuesByPost();
463  ilUtil::sendFailure($this->lng->txt('err_check_input'));
464  return $this->editMember($form);
465  }
466 }
setAllowMove($a_allow_move)
Set allow move.
static _lookupName($a_user_id)
lookup user name
This class represents an option in a radio group.
editMember(ilPropertyFormGUI $form=null)
Edit Member.
This class represents a property form user interface.
global $DIC
Definition: saml.php:7
initFieldForm($a_mode)
Init/create property form for fields.
This class represents a checkbox property in a property form.
Editing history for object custom user fields.
static addCustomFields($form, $a_obj_id, $a_type, $a_mode='user')
Add custom course fields.
const IL_CAL_UNIX
static _getAllReferences($a_id)
get all reference ids of object
$ilErr
Definition: raiseError.php:18
Table presentation of course/group relevant user data fields
global $ilCtrl
Definition: ilias.php:18
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
saveFields()
Save Field settings (currently only required status)
This class represents a property in a property form.
setValue($a_value)
Set Value.
setSize($a_size)
Set Size.
This class represents a text property in a property form.
Date and time handling
$ilUser
Definition: imgupload.php:18
static _lookupType($a_id, $a_reference=false)
lookup object type
Input GUI for the configuration of select input elements.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static _hasAgreementsByObjId($a_obj_id)
Check if there is any user agreement.
static saveCourseDefinedFields(ilPropertyFormGUI $form, $a_obj_id, $a_usr_id=0)
Save course defined fields.
listFields()
List existing custom fields.
confirmDeleteFields()
Show delete confirmation screen.
static setCourseDefinedFieldValues(ilPropertyFormGUI $form, $a_obj_id, $a_usr_id=0)
if(empty($password)) $table
Definition: pwgen.php:24
static _deleteByObjId($a_obj_id)
Delete all entries by obj_id.
$_POST["username"]
setRequired($a_required)
Set Required.
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.
Confirmation screen class.
static _getFields($a_container_id, $a_sort=IL_CDF_SORT_NAME)
Get all fields of a container.