ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMemberAgreementGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once('Services/PrivacySecurity/classes/class.ilPrivacySettings.php');
5 include_once('Services/Membership/classes/class.ilMemberAgreement.php');
6 include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
7 include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
8 
19 {
20  private $ref_id;
21  private $obj_id;
22  private $type;
23 
24  private $db;
25  private $ctrl;
26  private $lng;
27  private $tpl;
28 
29  private $privacy;
30  private $agreement;
31 
32  private $required_fullfilled = false;
33  private $agrement_required = false;
34 
41  public function __construct($a_ref_id)
42  {
43  global $ilDB,$ilCtrl,$lng,$tpl,$ilUser,$ilObjDataCache;
44 
45  $this->ref_id = $a_ref_id;
46  $this->obj_id = $ilObjDataCache->lookupObjId($this->ref_id);
47  $this->type = ilObject::_lookupType($this->obj_id);
48  $this->ctrl = $ilCtrl;
49  $this->tpl = $tpl;
50  $this->lng = $lng;
51  $this->lng->loadLanguageModule('ps');
52 
53  $this->privacy = ilPrivacySettings::_getInstance();
54  $this->agreement = new ilMemberAgreement($ilUser->getId(),$this->obj_id);
55  $this->init();
56  }
57 
64  public function executeCommand()
65  {
66  $next_class = $this->ctrl->getNextClass($this);
67  $cmd = $this->ctrl->getCmd();
68 
69  switch($next_class)
70  {
71  default:
72  if(!$cmd or $cmd == 'view')
73  {
74  $cmd = 'showAgreement';
75  }
76  $this->$cmd();
77  break;
78  }
79  }
80 
85  public function getPrivacy()
86  {
87  return $this->privacy;
88  }
89 
93  public function getAgreement()
94  {
95  return $this->agreement;
96  }
97 
103  protected function showAgreement(ilPropertyFormGUI $form = null)
104  {
105  $form = $this->initFormAgreement($form);
106 
107  $this->tpl->setContent($form->getHTML());
108  return true;
109  }
110 
111 
112 
113  protected function initFormAgreement()
114  {
115  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
116  $form = new ilPropertyFormGUI();
117  $form->setTitle($this->lng->txt($this->type.'_agreement_header'));
118  $form->setFormAction($GLOBALS['ilCtrl']->getFormAction($this));
119  $form->addCommandButton('save', $this->lng->txt('save'));
120 
121  $form = self::addExportFieldInfo($form, $this->obj_id, $this->type);
122  $form = self::addCustomFields($form, $this->obj_id, $this->type);
123 
124  if($this->getPrivacy()->confirmationRequired($this->type))
125  {
126  $form = self::addAgreement($form, $this->obj_id, $this->type);
127  }
128 
129  return $form;
130  }
131 
140  public static function addExportFieldInfo($form,$a_obj_id,$a_type)
141  {
142  global $lng;
143 
144  include_once('Services/PrivacySecurity/classes/class.ilExportFieldsInfo.php');
146 
147  $fields = new ilCustomInputGUI($lng->txt($a_type.'_user_agreement'),'');
148  $tpl = new ilTemplate('tpl.agreement_form.html',true,true,'Services/Membership');
149  $tpl->setVariable('TXT_INFO_AGREEMENT',$lng->txt($a_type.'_info_agreement'));
150  foreach($fields_info->getExportableFields() as $field)
151  {
152  $tpl->setCurrentBlock('field_item');
153  $tpl->setVariable('FIELD_NAME',$lng->txt($field));
154  $tpl->parseCurrentBlock();
155  }
156  $fields->setHtml($tpl->get());
157  $form->addItem($fields);
158 
159  return $form;
160  }
161 
168  public static function addAgreement($form, $a_obj_id, $a_type)
169  {
170  global $lng;
171 
172  $agreement = new ilCheckboxInputGUI($lng->txt($a_type.'_agree'),'agreement');
173  $agreement->setRequired(true);
174  $agreement->setOptionTitle($lng->txt($a_type.'_info_agree'));
175  $agreement->setValue(1);
176  $form->addItem($agreement);
177 
178  return $form;
179  }
180 
187  public static function addCustomFields($form, $a_obj_id, $a_type)
188  {
189  global $lng;
190 
191  include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
192  include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
193 
194  if(!count($cdf_fields = ilCourseDefinedFieldDefinition::_getFields($a_obj_id)))
195  {
196  return $form;
197  }
198 
199  $cdf = new ilNonEditableValueGUI($lng->txt('ps_crs_user_fields'));
200  $cdf->setValue($lng->txt($a_type.'_ps_cdf_info'));
201  $cdf->setRequired(true);
202 
203  foreach($cdf_fields as $field_obj)
204  {
205  switch($field_obj->getType())
206  {
207  case IL_CDF_TYPE_SELECT:
208 
209  if($field_obj->getValueOptions())
210  {
211  // Show as radio group
212  $option_radios = new ilRadioGroupInputGUI($field_obj->getName(), 'cdf_'.$field_obj->getId());
213  if($field_obj->isRequired())
214  {
215  $option_radios->setRequired(true);
216  }
217 
218  $open_answer_indexes = (array) $field_obj->getValueOptions();
219  foreach($field_obj->getValues() as $key => $val)
220  {
221  $option_radio = new ilRadioOption($val,$field_obj->getId().'_'.$key);
222 
223  // open answers
224  if(in_array($key, $open_answer_indexes))
225  {
226  $open_answer = new ilTextInputGUI('Sonstiges', 'cdf_oa_'.$field_obj->getId());
227  $open_answer->setRequired(true);
228  $option_radio->addSubItem($open_answer);
229  }
230 
231  $option_radios->addOption($option_radio);
232  }
233  $cdf->addSubItem($option_radios);
234  }
235  else
236  {
237  $select = new ilSelectInputGUI($field_obj->getName(),'cdf_'.$field_obj->getId());
238  #$select->setValue(ilUtil::stripSlashes($_POST['cdf'][$field_obj->getId()]));
239  $select->setOptions($field_obj->prepareSelectBox());
240  if($field_obj->isRequired())
241  {
242  $select->setRequired(true);
243  }
244  $cdf->addSubItem($select);
245  }
246  break;
247 
248  case IL_CDF_TYPE_TEXT:
249  $text = new ilTextInputGUI($field_obj->getName(),'cdf_'.$field_obj->getId());
250  #$text->setValue(ilUtil::stripSlashes($_POST['cdf'][$field_obj->getId()]));
251  $text->setSize(32);
252  $text->setMaxLength(255);
253  if($field_obj->isRequired())
254  {
255  $text->setRequired(true);
256  }
257  $cdf->addSubItem($text);
258  break;
259  }
260  }
261  $form->addItem($cdf);
262  return $form;
263 
264  }
265 
266 
267 
275  private function save()
276  {
277  $form = $this->initFormAgreement();
278 
279  // #14715 - checkInput() does not work for checkboxes
280  if($this->checkAgreement() &&
281  $form->checkInput())
282  {
283  self::saveCourseDefinedFields($form, $this->obj_id);
284 
285  $this->getAgreement()->setAccepted(true);
286  $this->getAgreement()->setAcceptanceTime(time());
287  $this->getAgreement()->save();
288  $this->ctrl->returnToParent($this);
289  }
290  elseif(!$this->checkAgreement())
291  {
292  ilUtil::sendFailure($this->lng->txt($this->type.'_agreement_required'));
293  $form->setValuesByPost();
294  $this->showAgreement($form);
295  return false;
296  }
297  else
298  {
299  ilUtil::sendFailure($this->lng->txt('fill_out_all_required_fields'));
300  $form->setValuesByPost();
301  $this->showAgreement($form);
302  return false;
303  }
304  }
305 
306 
311  public static function saveCourseDefinedFields(ilPropertyFormGUI $form, $a_obj_id)
312  {
313  global $ilUser;
314 
315  foreach(ilCourseDefinedFieldDefinition::_getFields($a_obj_id) as $field_obj)
316  {
317  switch($field_obj->getType())
318  {
319  case IL_CDF_TYPE_SELECT:
320 
321  // Split value id from post
322  list($field_id,$option_id) = explode('_', $form->getInput('cdf_'.$field_obj->getId()));
323  $open_answer_indexes = (array) $field_obj->getValueOptions();
324  if(in_array($option_id, $open_answer_indexes))
325  {
326  $value = $form->getInput('cdf_oa_'.$field_obj->getId());
327  }
328  else
329  {
330  $value = $field_obj->getValueById($option_id);
331  }
332  break;
333 
334  case IL_CDF_TYPE_TEXT:
335  $value = $form->getInput('cdf_'.$field_obj->getId());
336  break;
337  }
338 
339  $course_user_data = new ilCourseUserData($ilUser->getId(),$field_obj->getId());
340  $course_user_data->setValue($value);
341  $course_user_data->update();
342  }
343  }
344 
345 
352  private function checkAgreement()
353  {
354  global $ilUser;
355 
356  if($_POST['agreement'])
357  {
358  return true;
359  }
360  if($this->privacy->confirmationRequired($this->type))
361  {
362  return false;
363  }
364  return true;
365  }
366 
367 
368 
375  private function init()
376  {
377  global $ilUser;
378 
379  $this->required_fullfilled = ilCourseUserData::_checkRequired($ilUser->getId(),$this->obj_id);
380  $this->agreement_required = $this->getAgreement()->agreementRequired();
381  }
382 
388  private function sendInfoMessage()
389  {
390  $message = '';
391  if($this->agreement_required)
392  {
393  $message = $this->lng->txt($this->type.'_ps_agreement_req_info');
394  }
395  if(!$this->required_fullfilled)
396  {
397  if(strlen($message))
398  {
399  $message .= '<br />';
400  }
401  $message .= $this->lng->txt($this->type.'_ps_required_info');
402  }
403 
404  if(strlen($message))
405  {
406  ilUtil::sendFailure($message);
407  }
408  }
409 }
410 
411 
412 ?>