ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 $lng,$tpl,$ilCtrl,$ilTabs;
62 
63  $this->lng = $lng;
64  $this->lng->loadLanguageModule('ps');
65  $this->lng->loadLanguageModule(ilObject::_lookupType($a_obj_id));
66 
67  $this->tpl = $tpl;
68  $this->ctrl = $ilCtrl;
69  $this->tabs_gui = $ilTabs;
70 
71  $this->obj_id = $a_obj_id;
72 
73  // Currently only supported for container objects
74  $refs = ilObject::_getAllReferences($this->obj_id);
75  $this->ref_id = end($refs);
76  }
77 
84  public function executeCommand()
85  {
86  global $ilErr, $ilAccess, $lng;
87 
88  if (!$ilAccess->checkAccess('write', '', $this->ref_id)) {
89  $ilErr->raiseError($lng->txt('permission_denied'), $ilErr->WARNING);
90  }
91 
92  $cmd = $this->ctrl->getCmd();
93 
94  switch ($next_class = $this->ctrl->getNextClass($this)) {
95  default:
96  if (!$cmd) {
97  $cmd = 'show';
98  }
99  $this->$cmd();
100  break;
101  }
102  }
103 
108  public function getObjId()
109  {
110  return $this->obj_id;
111  }
112 
117  protected function show()
118  {
120  ilUtil::sendInfo($this->lng->txt('ps_cdf_warning_modify'));
121  }
122  $this->listFields();
123  }
124 
129  protected function listFields()
130  {
131  global $ilToolbar;
132 
133  $ilToolbar->addButton(
134  $this->lng->txt('ps_cdf_add_field'),
135  $this->ctrl->getLinkTarget($this, 'addField')
136  );
137 
138  include_once './Services/Membership/classes/class.ilObjectCustomUserFieldsTableGUI.php';
139  $table = new ilObjectCustomUserFieldsTableGUI($this, 'listFields');
141  $this->tpl->setContent($table->getHTML());
142  }
143 
148  protected function saveFields()
149  {
151  foreach ($fields as $field_obj) {
152  $field_obj->enableRequired((bool) isset($_POST['required'][$field_obj->getId()]));
153  $field_obj->update();
154  }
155 
157  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
158  $this->listFields();
159  return true;
160  }
161 
166  protected function confirmDeleteFields()
167  {
168  if (!count($_POST['field_ids'])) {
169  ilUtil::sendFailure($this->lng->txt('ps_cdf_select_one'));
170  $this->listFields();
171  return false;
172  }
173  include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
174  $confirm = new ilConfirmationGUI();
175  $confirm->setFormAction($this->ctrl->getFormAction($this));
176  $confirm->setHeaderText($this->lng->txt('ps_cdf_delete_sure'));
177 
178  foreach ($_POST['field_ids'] as $field_id) {
179  $tmp_field = new ilCourseDefinedFieldDefinition($this->getObjId(), $field_id);
180 
181  $confirm->addItem('field_ids[]', $field_id, $tmp_field->getName());
182  }
183 
184  $confirm->setConfirm($this->lng->txt('delete'), 'deleteFields');
185  $confirm->setCancel($this->lng->txt('cancel'), 'listFields');
186  $this->tpl->setContent($confirm->getHTML());
187  }
188 
193  protected function deleteFields()
194  {
195  foreach ((array) $_POST['field_ids'] as $field_id) {
196  $tmp_field = new ilCourseDefinedFieldDefinition($this->obj_id, $field_id);
197  $tmp_field->delete();
198  }
199 
200  ilMemberAgreement::_deleteByObjId($this->obj_id);
201 
202  ilUtil::sendSuccess($this->lng->txt('ps_cdf_deleted'));
203  $this->listFields();
204  return true;
205  }
206 
211  protected function addField()
212  {
213  $this->initFieldForm(self::MODE_CREATE);
214 
215  $this->form->getItemByPostVar('va')->setValues(array(''));
216 
217  $this->tpl->setContent($this->form->getHTML());
218  }
219 
224  protected function saveField()
225  {
226  $GLOBALS['ilLog']->write(__METHOD__ . ': ' . print_r($_POST, true));
227 
228  $this->initFieldForm(self::MODE_CREATE);
229  if ($this->form->checkInput()) {
230  $udf = new ilCourseDefinedFieldDefinition($this->getObjId());
231  $udf->setName($this->form->getInput('na'));
232  $udf->setType($this->form->getInput('ty'));
233  $udf->setValues($udf->prepareValues($this->form->getInput('va')));
234  $udf->setValueOptions($this->form->getItemByPostVar('va')->getOpenAnswerIndexes()); // #14720
235  $udf->enableRequired($this->form->getInput('re'));
236  $udf->save();
237 
238  ilUtil::sendSuccess($this->lng->txt('ps_cdf_added_field'));
239  // reset agreements
241  $this->listFields();
242  return true;
243  }
244  // not valid
245  ilUtil::sendFailure($this->lng->txt('err_check_input'));
246  $this->form->setValuesByPost();
247  $this->tpl->setContent($this->form->getHTML());
248  return false;
249  }
250 
256  protected function editField()
257  {
258  if (!$_REQUEST['field_id']) {
259  $this->listFields();
260  return false;
261  }
262 
263  $this->initFieldForm(self::MODE_UPDATE);
264 
265  $udf = new ilCourseDefinedFieldDefinition($this->getObjId(), (int) $_REQUEST['field_id']);
266  $this->form->getItemByPostVar('na')->setValue($udf->getName());
267  $this->form->getItemByPostVar('ty')->setValue($udf->getType());
268  $this->form->getItemByPostVar('re')->setChecked($udf->isRequired());
269  $this->form->getItemByPostVar('va')->setValues($udf->getValues());
270  $this->form->getItemByPostVar('va')->setOpenAnswerIndexes($udf->getValueOptions());
271 
272  $this->tpl->setContent($this->form->getHTML());
273  }
274 
279  protected function updateField()
280  {
281  $GLOBALS['ilLog']->write(__METHOD__ . ': ' . print_r($_POST, true));
282 
283  $this->initFieldForm(self::MODE_UPDATE);
284 
285  if ($this->form->checkInput()) {
286  $udf = new ilCourseDefinedFieldDefinition($this->getObjId(), (int) $_REQUEST['field_id']);
287  $udf->setName($this->form->getInput('na'));
288  $udf->setType($this->form->getInput('ty'));
289  $prepared = $udf->prepareValues($this->form->getInput('va'));
290  $udf->setValues($prepared);
291  $udf->setValueOptions($this->form->getItemByPostVar('va')->getOpenAnswerIndexes());
292  $udf->enableRequired($this->form->getInput('re'));
293  $udf->update();
294 
295  // Finally reset member agreements
297  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
298  $this->listFields();
299  return true;
300  }
301 
302  ilUtil::sendFailure($this->lng->txt('err_check_input'));
303  $this->form->setValuesByPost();
304  $this->tpl->setContent($this->form->getHTML());
305  return false;
306  }
307 
312  protected function initFieldForm($a_mode)
313  {
314  if ($this->form instanceof ilPropertyFormGUI) {
315  return true;
316  }
317  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
318  $this->form = new ilPropertyFormGUI();
319 
320  switch ($a_mode) {
321  case self::MODE_CREATE:
322  $this->form->setFormAction($this->ctrl->getFormAction($this));
323  $this->form->setTitle($this->lng->txt('ps_cdf_add_field'));
324  $this->form->addCommandButton('saveField', $this->lng->txt('save'));
325  $this->form->addCommandButton('listFields', $this->lng->txt('cancel'));
326  break;
327 
328  case self::MODE_UPDATE:
329  $this->ctrl->setParameter($this, 'field_id', (int) $_REQUEST['field_id']);
330  $this->form->setFormAction($this->ctrl->getFormAction($this));
331  $this->form->setTitle($this->lng->txt('ps_cdf_edit_field'));
332  $this->form->addCommandButton('updateField', $this->lng->txt('save'));
333  $this->form->addCommandButton('listFields', $this->lng->txt('cancel'));
334  break;
335  }
336 
337  // Name
338  $na = new ilTextInputGUI($this->lng->txt('ps_cdf_name'), 'na');
339  $na->setSize(32);
340  $na->setMaxLength(255);
341  $na->setRequired(true);
342  $this->form->addItem($na);
343 
344  // Type
345  $ty = new ilRadioGroupInputGUI($this->lng->txt('ps_field_type'), 'ty');
346  $ty->setRequired(true);
347  $this->form->addItem($ty);
348 
349  if ($a_mode == self::MODE_UPDATE) {
350  $ty->setDisabled(true); // #14888
351  }
352 
353  // Text type
354  $ty_te = new ilRadioOption($this->lng->txt('ps_type_txt_long'), IL_CDF_TYPE_TEXT);
355  $ty->addOption($ty_te);
356 
357  // Select Type
358  $ty_se = new ilRadioOption($this->lng->txt('ps_type_select_long'), IL_CDF_TYPE_SELECT);
359  $ty->addOption($ty_se);
360 
361  // Select Type Values
362  include_once './Services/Form/classes/class.ilSelectBuilderInputGUI.php';
363  $ty_se_mu = new ilSelectBuilderInputGUI($this->lng->txt('ps_cdf_value'), 'va');
364  $ty_se_mu->setAllowMove(true);
365  $ty_se_mu->setRequired(true);
366  $ty_se_mu->setSize(32);
367  $ty_se_mu->setMaxLength(128);
368  $ty_se->addSubItem($ty_se_mu);
369 
370  // Required
371  $re = new ilCheckboxInputGUI($this->lng->txt('ps_cdf_required'), 're');
372  $re->setValue(1);
373  $this->form->addItem($re);
374  }
375 
380  protected function editMember(ilPropertyFormGUI $form = null)
381  {
382  $GLOBALS['ilCtrl']->saveParameter($this, 'member_id');
383 
384  $GLOBALS['ilTabs']->clearTargets();
385  $GLOBALS['ilTabs']->clearSubTabs();
386  $GLOBALS['ilTabs']->setBackTarget($this->lng->txt('back'), $this->ctrl->getLinkTarget($this, 'cancelEditMember'));
387 
388 
389  if ($form instanceof ilPropertyFormGUI) {
390  $GLOBALS['tpl']->setContent($form->getHTML());
391  } else {
392  $form = $this->initMemberForm();
393  ilMemberAgreementGUI::setCourseDefinedFieldValues($form, $this->getObjId(), (int) $_REQUEST['member_id']);
394  }
395 
396  $GLOBALS['tpl']->setContent($form->getHTML());
397  }
398 
399 
403  protected function cancelEditMember()
404  {
405  $GLOBALS['ilCtrl']->returnToParent($this);
406  }
407 
412  protected function initMemberForm()
413  {
414  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
415  $form = new ilPropertyFormGUI();
416  $form->setFormAction($GLOBALS['ilCtrl']->getFormAction($this));
417  $title = $this->lng->txt(ilObject::_lookupType($this->getObjId()) . '_cdf_edit_member');
418  $name = ilObjUser::_lookupName((int) $_REQUEST['member_id']);
419  $title .= (': ' . $name['lastname'] . ', ' . $name['firstname']);
420  $form->setTitle($title);
421 
422  include_once './Services/Membership/classes/class.ilMemberAgreementGUI.php';
424 
425  $form->addCommandButton('saveMember', $this->lng->txt('save'));
426  $form->addCommandButton('cancelEditMember', $this->lng->txt('cancel'));
427 
428  return $form;
429  }
430 
431  protected function saveMember()
432  {
433  global $ilUser;
434 
435  $GLOBALS['ilCtrl']->saveParameter($this, 'member_id');
436 
437  $form = $this->initMemberForm();
438  if ($form->checkInput()) {
439  // save history
440  include_once './Services/Membership/classes/class.ilObjectCustomUserFieldHistory.php';
441  $history = new ilObjectCustomUserFieldHistory($this->getObjId(), (int) $_REQUEST['member_id']);
442  $history->setEditingTime(new ilDateTime(time(), IL_CAL_UNIX));
443  $history->setUpdateUser($ilUser->getId());
444  $history->save();
445 
446  ilMemberAgreementGUI::saveCourseDefinedFields($form, $this->getObjId(), (int) $_REQUEST['member_id']);
447  ilUtil::sendSuccess($this->lng->txt('settings_saved'), true);
448  $GLOBALS['ilCtrl']->returnToParent($this);
449  return true;
450  }
451 
452  $form->setValuesByPost();
453  ilUtil::sendFailure($this->lng->txt('err_check_input'));
454  return $this->editMember($form);
455  }
456 }
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
setAllowMove($a_allow_move)
Set allow move.
static _lookupName($a_user_id)
lookup user name
global $ilErr
Definition: raiseError.php:16
This class represents an option in a radio group.
editMember(ilPropertyFormGUI $form=null)
Edit Member.
This class represents a property form user interface.
initFieldForm($a_mode)
Init/create property form for fields.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
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
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.
if($format !==null) $name
Definition: metadata.php:146
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
Create styles array
The data for the language used.
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.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
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.
Confirmation screen class.
static _getFields($a_container_id, $a_sort=IL_CDF_SORT_NAME)
Get all fields of a container.