ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCourseUserFieldsGUI.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('Modules/Course/classes/class.ilCourseAgreement.php');
25 
36 {
37  private $lng;
38  private $tpl;
39  private $ctrl;
40  private $tabs_gui;
41 
42  private $obj_id;
43 
44  private $cdf;
45 
53  public function __construct($a_obj_id)
54  {
55  global $lng,$tpl,$ilCtrl,$ilTabs;
56 
57  $this->lng = $lng;
58  $this->lng->loadLanguageModule('ps');
59 
60  $this->tpl = $tpl;
61  $this->ctrl = $ilCtrl;
62  $this->tabs_gui = $ilTabs;
63 
64  $this->obj_id = $a_obj_id;
65  }
66 
73  public function executeCommand()
74  {
75  $cmd = $this->ctrl->getCmd();
76 
77  switch($next_class = $this->ctrl->getNextClass($this))
78  {
79  default:
80  if(!$cmd)
81  {
82  $cmd = 'show';
83  }
84  $this->$cmd();
85  break;
86  }
87  }
88 
94  public function show()
95  {
96  unset($_SESSION['il_cdf_delete']);
97  unset($_SESSION['il_cdf_select_num_values']);
98 
99 
100  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.user_fields_list.html','Modules/Course');
101 
102  $this->tpl->setVariable('FORMACTION',$this->ctrl->getFormAction($this));
103  $this->tpl->setVariable('TABLE_TITLE',$this->lng->txt('ps_crs_user_fields'));
104  $this->tpl->setVariable('HEAD_NAME',$this->lng->txt('ps_cdf_name'));
105  $this->tpl->setVariable('HEAD_TYPE',$this->lng->txt('ps_cdf_type'));
106  $this->tpl->setVariable('HEAD_REQUIRED',$this->lng->txt('ps_cdf_required'));
107 
108  $this->tpl->setVariable('ADD',$this->lng->txt('ps_cdf_add_field'));
109  $this->tpl->setVariable('LINK_ADD',$this->ctrl->getLinkTarget($this,'fieldSelection'));
110 
111 
112  $fields = ilCourseDefinedFieldDefinition::_getFields($this->obj_id);
113 
114  if(!count($fields))
115  {
116  $this->tpl->setCurrentBlock('table_empty');
117  $this->tpl->setVariable('EMPTY_TXT',$this->lng->txt('ps_cdf_no_fields'));
118  $this->tpl->parseCurrentBlock();
119  }
121  {
122  $this->tpl->setCurrentBlock('warning_modify');
123  $this->tpl->setVariable('TXT_WARNING',$this->lng->txt('ps_cdf_warning_modify'));
124  $this->tpl->parseCurrentBlock();
125  }
126  $counter = 0;
127  foreach($fields as $field_obj)
128  {
129  if($field_obj->getType() == IL_CDF_TYPE_SELECT or 1)
130  {
131  $this->tpl->setCurrentBlock('show_edit');
132 
133  $this->ctrl->setParameter($this,'field_id',$field_obj->getId());
134  $this->tpl->setVariable('EDIT_LINK',$this->ctrl->getLinkTarget($this,'editField'));
135  $this->ctrl->clearParameters($this);
136 
137  $this->tpl->setVariable('EDIT',$this->lng->txt('edit'));
138  $this->tpl->parseCurrentBlock();
139  }
140 
141  $this->tpl->setCurrentBlock('table_content');
142  $this->tpl->setVariable('ROWCOL',ilUtil::switchColor($counter++,'tblrow1','tblrow2'));
143  $this->tpl->setVariable('CHECKBOX',ilUtil::formCheckbox(0,'field_id[]',$field_obj->getId()));
144  $this->tpl->setVariable('NAME',$field_obj->getName());
145  $this->tpl->setVariable('TYPE',$field_obj->getType() == IL_CDF_TYPE_SELECT ?
146  $this->lng->txt('ps_type_select') :
147  $this->lng->txt('ps_type_text'));
148  $this->tpl->setVariable('REQUIRED',ilUtil::formCheckbox((int) $field_obj->isRequired(),'required['.$field_obj->getId().']',1));
149  $this->tpl->parseCurrentBlock();
150  }
151  $this->tpl->setVariable("DOWNRIGHT",ilUtil::getImagePath('arrow_downright.gif'));
152  $this->tpl->setVariable('BTN_DELETE',$this->lng->txt('delete'));
153 
154  if(count($fields))
155  {
156  $this->tpl->setCurrentBlock('show_save');
157  $this->tpl->setVariable('BTN_SAVE',$this->lng->txt('save'));
158  $this->tpl->parseCurrentBlock();
159  }
160  }
161 
167  public function editField()
168  {
169  $_SESSION['il_cdf_select_num_values'] = $_SESSION['il_cdf_select_num_values'] ? $_SESSION['il_cdf_select_num_values'] : 1;
170 
171  if(!$_GET['field_id'])
172  {
173  ilUtil::sendInfo('No field given');
174  $this->show();
175  return false;
176  }
177  $cdf = new ilCourseDefinedFieldDefinition($this->obj_id,(int) $_GET['field_id']);
178 
179  $name = isset($_POST['cmd']) ? ilUtil::prepareFormOutput($_POST['field_name'],true) : ilUtil::prepareFormOutput($cdf->getName());
180  $required = isset($_POST['cmd']) ? $_POST['required'] : $cdf->isRequired();
181 
182  $this->ctrl->setParameter($this,'field_id',(int) $_GET['field_id']);
183  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.user_fields_edit_select_field.html','Modules/Course');
184  $this->tpl->setVariable('FORMACTION',$this->ctrl->getFormAction($this));
185 
186 
187  $this->tpl->setVariable('TXT_FIELD_NAME',$this->lng->txt('ps_name_field'));
188  $this->tpl->setVariable('FIELD_NAME_VALUE',$name);
189 
190  $this->tpl->setVariable('TXT_REQUIRED',$this->lng->txt('required_field'));
191  $this->tpl->setVariable('BTN_ADD',$this->lng->txt('save'));
192  $this->tpl->setVariable('BTN_PREVIOUS',$this->lng->txt('cancel'));
193 
194  $this->tpl->setVariable('REQUIRED',ilUtil::formCheckbox($required,'required',1));
195 
196  // Old values
197  $i = 1;
198  foreach($cdf->getValues() as $key => $value)
199  {
200  $this->ctrl->setParameter($this,'del_field',$i - 1);
201 
202  $this->tpl->setCurrentBlock('values');
203 
204  $this->ctrl->setParameter($this,'del_field',$i - 1);
205  $this->tpl->setVariable('DELETE',$this->lng->txt('delete'));
206  $this->tpl->setVariable('LINK_DELETE',$this->ctrl->getLinkTarget($this,'deleteField'));
207  $this->tpl->setVariable('FIELD_NAME',ilUtil::prepareFormOutput($value));
208  $this->tpl->setVariable("TXT_VALUES",$this->lng->txt('ps_cdf_value').' '.($i++));
209  $this->tpl->parseCurrentBlock();
210  }
211  switch($cdf->getType())
212  {
213  case IL_CDF_TYPE_SELECT:
214  $this->tpl->setVariable('TXT_SELECT_TYPE',$this->lng->txt('ps_edit_select_field'));
215  $this->tpl->setVariable('BTN_NEW_VALUE',$this->lng->txt('ps_btn_add_value'));
216  // New values
217  for($j = 0; $j < $_SESSION['il_cdf_select_num_values'];$j++)
218  {
219  $this->tpl->setCurrentBlock('new_values');
220  $this->tpl->setVariable('COUNTER',$j);
221  $this->tpl->setVariable("TXT_NEW_VALUES",$this->lng->txt('ps_cdf_value').' '.($i + $j));
222  $this->tpl->setVariable("NEW_FIELD_NAME",$_POST['new_field_values'][$j]);
223  $this->tpl->parseCurrentBlock();
224  }
225  break;
226 
227  case IL_CDF_TYPE_TEXT:
228  $this->tpl->setVariable('TXT_SELECT_TYPE',$this->lng->txt('ps_edit_text_field'));
229  break;
230  }
231  }
232 
238  public function deleteField()
239  {
240  if(!$_GET['field_id'])
241  {
242  ilUtil::sendInfo('No field given');
243  $this->show();
244  return false;
245  }
246  $cdf = new ilCourseDefinedFieldDefinition($this->obj_id,(int) $_GET['field_id']);
247  $cdf->deleteValue((int) $_GET['del_field']);
248 
249  ilUtil::sendInfo($this->lng->txt('ps_cdf_deleted_field'));
250  $this->editField();
251  }
252 
253 
259  public function addNewValue()
260  {
261  $_SESSION['il_cdf_select_num_values'] += 1;
262  $this->editField();
263  return true;
264  }
265 
271  public function updateField()
272  {
273  if(!$_GET['field_id'])
274  {
275  ilUtil::sendInfo('No field given');
276  $this->show();
277  return false;
278  }
279 
280  $cdf = new ilCourseDefinedFieldDefinition($this->obj_id,$_GET['field_id']);
281 
282  if(!strlen($_POST['field_name']))
283  {
284  ilUtil::sendInfo($this->lng->txt('ps_cdf_no_name_given'));
285  $this->editField();
286  return false;
287  }
288 
289  switch($cdf->getType())
290  {
291  case IL_CDF_TYPE_SELECT:
292  $values = $cdf->prepareValues($_POST['new_field_values']);
293  $cdf->appendValues($values);
294  break;
295  default:
296  break;
297 
298  }
299 
300  $cdf->setName(ilUtil::stripSlashes($_POST['field_name']));
301  $cdf->enableRequired((int) $_POST['required']);
302  $cdf->update();
303 
304  // Finally reset member agreements
305  ilCourseAgreement::_deleteByObjId($this->obj_id);
306 
307  ilUtil::sendInfo($this->lng->txt('settings_saved'));
308  $this->show();
309  return true;
310  }
311 
312 
313 
314 
320  public function confirmDelete()
321  {
322  if(!count($_POST['field_id']))
323  {
324  ilUtil::sendInfo($this->lng->txt('ps_cdf_select_one'));
325  $this->show();
326  return false;
327  }
328 
329  ilUtil::sendInfo($this->lng->txt('ps_cdf_delete_sure'));
330  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.user_fields_confirm_delete.html','Modules/Course');
331  $this->tpl->setVariable('FORMACTION',$this->ctrl->getFormAction($this));
332  $this->tpl->setVariable('TXT_NAME',$this->lng->txt('ps_cdf_name'));
333  $this->tpl->setVariable('TXT_TYPE',$this->lng->txt('ps_cdf_type'));
334  $this->tpl->setVariable('DELETE',$this->lng->txt('delete'));
335  $this->tpl->setVariable('CANCEL',$this->lng->txt('cancel'));
336 
337  $counter = 0;
338  foreach($_POST['field_id'] as $field_id)
339  {
340  $tmp_field = new ilCourseDefinedFieldDefinition($this->obj_id,$field_id);
341 
342  $this->tpl->setCurrentBlock('del_row');
343  $this->tpl->setVariable('CSS_ROW',ilUtil::switchColor(++$counter,'tblrow1','tblrow2'));
344  $this->tpl->setVariable('DEL_NAME',$tmp_field->getName());
345  $this->tpl->setVariable('DEL_TYPE',$tmp_field->getType() == IL_CDF_TYPE_SELECT ?
346  $this->lng->txt('ps_type_select') :
347  $this->lng->txt('ps_type_text'));
348  $this->tpl->parseCurrentBlock();
349  }
350 
351  $_SESSION['il_cdf_delete'] = $_POST['field_id'];
352  }
353 
359  public function delete()
360  {
361  if(!count($_SESSION['il_cdf_delete']))
362  {
363  ilUtil::sendInfo($this->lng->txt('ps_cdf_select_one'));
364  $this->show();
365  return false;
366  }
367  foreach($_SESSION['il_cdf_delete'] as $field_id)
368  {
369  $tmp_field = new ilCourseDefinedFieldDefinition($this->obj_id,$field_id);
370  $tmp_field->delete();
371  }
372 
373  ilCourseAgreement::_deleteByObjId($this->obj_id);
374 
375  ilUtil::sendInfo($this->lng->txt('ps_cdf_deleted'));
376  unset($_SESSION['il_cdf_delete']);
377 
378  $this->show();
379  return true;
380  }
381 
382 
383 
391  public function save()
392  {
393  $fields = ilCourseDefinedFieldDefinition::_getFields($this->obj_id);
394  foreach($fields as $field_obj)
395  {
396  $field_obj->enableRequired((bool) isset($_POST['required'][$field_obj->getId()]));
397  $field_obj->update();
398  }
399 
400  ilCourseAgreement::_deleteByObjId($this->obj_id);
401  ilUtil::sendInfo($this->lng->txt('settings_saved'));
402  $this->show();
403  return true;
404  }
405 
412  public function fieldSelection()
413  {
414  // number of values defaults to 3
415  $_SESSION['il_cdf_select_num_values'] = 3;
416 
417 
418  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.user_fields_selection.html','Modules/Course');
419  $this->tpl->setVariable('FORMACTION',$this->ctrl->getFormAction($this));
420  $this->tpl->setVariable('TXT_SELECT_TYPE',$this->lng->txt('ps_cdf_add_field'));
421  $this->tpl->setVariable('FIELD_TYPE',$this->lng->txt('ps_field_type'));
422  $this->tpl->setVariable('TXT_TEXT',$this->lng->txt('ps_type_txt_long'));
423  $this->tpl->setVariable('TXT_SELECT',$this->lng->txt('ps_type_select_long'));
424  $this->tpl->setVariable('BTN_MORE',$this->lng->txt('btn_next'));
425  $this->tpl->setVariable('BTN_CANCEL',$this->lng->txt('cancel'));
426 
427  $this->tpl->setVariable("TYPE_TEXT",ilUtil::formRadioButton(1,'field_type',IL_CDF_TYPE_TEXT));
428  $this->tpl->setVariable("TYPE_SELECT",ilUtil::formRadioButton(0,'field_type',IL_CDF_TYPE_SELECT));
429 
430  }
431 
438  public function chooseDefinitions()
439  {
440  switch($field_type = (int) $_REQUEST['field_type'])
441  {
442  case IL_CDF_TYPE_TEXT:
443  $this->addTextField();
444  break;
445 
446  case IL_CDF_TYPE_SELECT:
447  $this->addSelectField();
448  break;
449  }
450  }
451 
452  /*
453  * @access public
454  */
455  public function addSelectField()
456  {
457  $_SESSION['il_cdf_select_num_values'] = $_SESSION['il_cdf_select_num_values'] ? $_SESSION['il_cdf_select_num_values'] : 3;
458 
459  $this->ctrl->setParameter($this,'field_type',(int) $_REQUEST['field_type']);
460 
461  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.user_fields_add_select.html','Modules/Course');
462  $this->tpl->setVariable('FORMACTION',$this->ctrl->getFormAction($this));
463  $this->tpl->setVariable('TXT_SELECT_TYPE',$this->lng->txt('ps_new_select_field'));
464  $this->tpl->setVariable('TXT_FIELD_NAME',$this->lng->txt('ps_name_field'));
465  $this->tpl->setVariable('TXT_REQUIRED',$this->lng->txt('required_field'));
466  $this->tpl->setVariable('BTN_ADD',$this->lng->txt('save'));
467  $this->tpl->setVariable('BTN_PREVIOUS',$this->lng->txt('btn_previous'));
468  $this->tpl->setVariable('BTN_NEW_VALUE',$this->lng->txt('ps_btn_add_value'));
469 
470  $this->tpl->setVariable('REQUIRED',ilUtil::formCheckbox($_POST['required'] ? 1 : 0,'required',1));
471  $this->tpl->setVariable('FIELD_NAME_VALUE',$_POST['field_name']);
472 
473  for($i = 0; $i < $_SESSION['il_cdf_select_num_values'];$i++)
474  {
475  $this->tpl->setCurrentBlock("values");
476  $this->tpl->setVariable('COUNTER',$i);
477  $this->tpl->setVariable("TXT_VALUES",$this->lng->txt('ps_cdf_value').' '.($i + 1));
478  $this->tpl->setVariable("FIELD_NAME",$_POST['field_values'][$i]);
479  $this->tpl->parseCurrentBlock();
480  }
481 
482  }
483 
489  public function addValue()
490  {
491  $_SESSION['il_cdf_select_num_values'] += 1;
492  $this->addSelectField();
493  return true;
494  }
495 
496 
497 
505  public function addTextField()
506  {
507  $this->ctrl->setParameter($this,'field_type',(int) $_REQUEST['field_type']);
508 
509  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.user_fields_add_text.html','Modules/Course');
510  $this->tpl->setVariable('FORMACTION',$this->ctrl->getFormAction($this));
511  $this->tpl->setVariable('TXT_SELECT_TYPE',$this->lng->txt('ps_new_text_field'));
512  $this->tpl->setVariable('TXT_FIELD_NAME',$this->lng->txt('ps_name_field'));
513  $this->tpl->setVariable('TXT_REQUIRED',$this->lng->txt('required_field'));
514  $this->tpl->setVariable('BTN_ADD',$this->lng->txt('btn_add'));
515  $this->tpl->setVariable('BTN_PREVIOUS',$this->lng->txt('btn_previous'));
516 
517  $this->tpl->setVariable('REQUIRED',ilUtil::formCheckbox(0,'required',1));
518  }
519 
525  public function saveField()
526  {
527  $cdf = new ilCourseDefinedFieldDefinition($this->obj_id);
528 
529  if(!strlen($_POST['field_name']))
530  {
531  ilUtil::sendInfo($this->lng->txt('ps_cdf_no_name_given'));
532  $this->chooseDefinitions();
533  return false;
534  }
535  if($_REQUEST['field_type'] == IL_CDF_TYPE_TEXT)
536  {
537  $cdf->setType(IL_CDF_TYPE_TEXT);
538  }
539  else
540  {
541  $cdf->setType(IL_CDF_TYPE_SELECT);
542  $values = $cdf->prepareValues($_POST['field_values']);
543  $cdf->setValues($values);
544  }
545 
546  $cdf->setName(ilUtil::stripSlashes($_POST['field_name']));
547  $cdf->enableRequired((int) $_POST['required']);
548  $cdf->save();
549 
550  ilUtil::sendInfo($this->lng->txt('ps_cdf_added_field'));
551  $this->show();
552  return true;
553  }
554 }
555 
556 
557 ?>