ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
49  private $cdf;
50 
58  public function __construct($a_obj_id)
59  {
60  global $lng,$tpl,$ilCtrl,$ilTabs;
61 
62  $this->lng = $lng;
63  $this->lng->loadLanguageModule('ps');
64  $this->lng->loadLanguageModule(ilObject::_lookupType($a_obj_id));
65 
66  $this->tpl = $tpl;
67  $this->ctrl = $ilCtrl;
68  $this->tabs_gui = $ilTabs;
69 
70  $this->obj_id = $a_obj_id;
71  }
72 
79  public function executeCommand()
80  {
81  $cmd = $this->ctrl->getCmd();
82 
83  switch($next_class = $this->ctrl->getNextClass($this))
84  {
85  default:
86  if(!$cmd)
87  {
88  $cmd = 'show';
89  }
90  $this->$cmd();
91  break;
92  }
93  }
94 
99  public function getObjId()
100  {
101  return $this->obj_id;
102  }
103 
108  protected function show()
109  {
111  {
112  ilUtil::sendInfo($this->lng->txt('ps_cdf_warning_modify'));
113  }
114  $this->listFields();
115  }
116 
121  protected function listFields()
122  {
123  global $ilToolbar;
124 
125  $ilToolbar->addButton(
126  $this->lng->txt('ps_cdf_add_field'),
127  $this->ctrl->getLinkTarget($this,'addField')
128  );
129 
130  include_once './Services/Membership/classes/class.ilObjectCustomUserFieldsTableGUI.php';
131  $table = new ilObjectCustomUserFieldsTableGUI($this,'listFields');
132  $table->parse(ilCourseDefinedFieldDefinition::_getFields($this->getObjId()));
133  $this->tpl->setContent($table->getHTML());
134  }
135 
140  protected function saveFields()
141  {
143  foreach($fields as $field_obj)
144  {
145  $field_obj->enableRequired((bool) isset($_POST['required'][$field_obj->getId()]));
146  $field_obj->update();
147  }
148 
150  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
151  $this->listFields();
152  return true;
153  }
154 
159  protected function confirmDeleteFields()
160  {
161  if(!count($_POST['field_ids']))
162  {
163  ilUtil::sendFailure($this->lng->txt('ps_cdf_select_one'));
164  $this->listFields();
165  return false;
166  }
167  include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
168  $confirm = new ilConfirmationGUI();
169  $confirm->setFormAction($this->ctrl->getFormAction($this));
170  $confirm->setHeaderText($this->lng->txt('ps_cdf_delete_sure'));
171 
172  foreach($_POST['field_ids'] as $field_id)
173  {
174  $tmp_field = new ilCourseDefinedFieldDefinition($this->getObjId(),$field_id);
175 
176  $confirm->addItem('field_ids[]', $field_id, $tmp_field->getName());
177  }
178 
179  $confirm->setConfirm($this->lng->txt('delete'), 'deleteFields');
180  $confirm->setCancel($this->lng->txt('cancel'), 'listFields');
181  $this->tpl->setContent($confirm->getHTML());
182  }
183 
188  protected function deleteFields()
189  {
190  foreach((array) $_POST['field_ids'] as $field_id)
191  {
192  $tmp_field = new ilCourseDefinedFieldDefinition($this->obj_id,$field_id);
193  $tmp_field->delete();
194  }
195 
196  ilMemberAgreement::_deleteByObjId($this->obj_id);
197 
198  ilUtil::sendSuccess($this->lng->txt('ps_cdf_deleted'));
199  $this->listFields();
200  return true;
201  }
202 
207  protected function addField()
208  {
209  $this->initFieldForm(self::MODE_CREATE);
210 
211  $this->form->getItemByPostVar('va')->setValues(array(''));
212 
213  $this->tpl->setContent($this->form->getHTML());
214  }
215 
220  protected function saveField()
221  {
222  $this->initFieldForm(self::MODE_CREATE);
223  if($this->form->checkInput())
224  {
225  $udf = new ilCourseDefinedFieldDefinition($this->getObjId());
226  $udf->setName($this->form->getInput('na'));
227  $udf->setType($this->form->getInput('ty'));
228  $udf->setValues($udf->prepareValues($this->form->getInput('va')));
229  $udf->enableRequired($this->form->getInput('re'));
230  $udf->save();
231 
232  ilUtil::sendSuccess($this->lng->txt('ps_cdf_added_field'));
233  $this->listFields();
234  return true;
235  }
236  // not valid
237  ilUtil::sendFailure($this->lng->txt('err_check_input'));
238  $this->form->setValuesByPost();
239  $this->tpl->setContent($this->form->getHTML());
240  return false;
241  }
242 
248  protected function editField()
249  {
250  if(!$_REQUEST['field_id'])
251  {
252  $this->listFields();
253  return false;
254  }
255 
256  $this->initFieldForm(self::MODE_UPDATE);
257 
258  $udf = new ilCourseDefinedFieldDefinition($this->getObjId(),(int) $_REQUEST['field_id']);
259  $this->form->getItemByPostVar('na')->setValue($udf->getName());
260  $this->form->getItemByPostVar('ty')->setValue($udf->getType());
261  $this->form->getItemByPostVar('re')->setChecked($udf->isRequired());
262  $this->form->getItemByPostVar('va')->setValues($udf->getValues());
263 
264  $this->tpl->setContent($this->form->getHTML());
265  }
266 
271  protected function updateField()
272  {
273  $this->initFieldForm(self::MODE_UPDATE);
274 
275  if($this->form->checkInput())
276  {
277  $udf = new ilCourseDefinedFieldDefinition($this->getObjId(),(int) $_REQUEST['field_id']);
278  $udf->setName($this->form->getInput('na'));
279  $udf->setType($this->form->getInput('ty'));
280  $udf->setValues($udf->prepareValues($this->form->getInput('va')));
281  $udf->enableRequired($this->form->getInput('re'));
282  $udf->update();
283 
284  // Finally reset member agreements
286  ilUtil::sendSuccess($this->lng->txt('settings_saved'));
287  $this->listFields();
288  return true;
289  }
290 
291  ilUtil::sendFailure($this->lng->txt('err_check_input'));
292  $this->form->setValuesByPost();
293  $this->tpl->setContent($this->form->getHTML());
294  return false;
295  }
296 
301  protected function initFieldForm($a_mode)
302  {
303  if($this->form instanceof ilPropertyFormGUI)
304  {
305  return true;
306  }
307  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
308  $this->form = new ilPropertyFormGUI();
309 
310  switch($a_mode)
311  {
312  case self::MODE_CREATE:
313  $this->form->setFormAction($this->ctrl->getFormAction($this));
314  $this->form->setTitle($this->lng->txt('ps_cdf_add_field'));
315  $this->form->addCommandButton('saveField', $this->lng->txt('save'));
316  $this->form->addCommandButton('listFields', $this->lng->txt('cancel'));
317  break;
318 
319  case self::MODE_UPDATE:
320  $this->ctrl->setParameter($this,'field_id',(int) $_REQUEST['field_id']);
321  $this->form->setFormAction($this->ctrl->getFormAction($this));
322  $this->form->setTitle($this->lng->txt('ps_cdf_edit_field'));
323  $this->form->addCommandButton('updateField', $this->lng->txt('save'));
324  $this->form->addCommandButton('listFields', $this->lng->txt('cancel'));
325  break;
326  }
327 
328  // Name
329  $na = new ilTextInputGUI($this->lng->txt('ps_cdf_name'),'na');
330  $na->setSize(32);
331  $na->setMaxLength(255);
332  $na->setRequired(true);
333  $this->form->addItem($na);
334 
335  // Type
336  $ty = new ilRadioGroupInputGUI($this->lng->txt('ps_field_type'),'ty');
337  $ty->setRequired(true);
338  $this->form->addItem($ty);
339 
340  // Text type
341  $ty_te = new ilRadioOption($this->lng->txt('ps_type_txt_long'),IL_CDF_TYPE_TEXT);
342  $ty->addOption($ty_te);
343 
344  // Select Type
345  $ty_se = new ilRadioOption($this->lng->txt('ps_type_select_long'),IL_CDF_TYPE_SELECT);
346  $ty->addOption($ty_se);
347 
348  // Select Type Values
349  $ty_se_mu = new ilTextWizardInputGUI($this->lng->txt('ps_cdf_value'),'va');
350  $ty_se_mu->setRequired(true);
351  $ty_se_mu->setSize(32);
352  $ty_se_mu->setMaxLength(128);
353  $ty_se->addSubItem($ty_se_mu);
354 
355 
356  // Required
357  $re = new ilCheckboxInputGUI($this->lng->txt('ps_cdf_required'),'re');
358  $re->setValue(1);
359  $this->form->addItem($re);
360  }
361 }
362 ?>