ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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  global $ilUser;
106 
107  $form = $this->initFormAgreement($form);
108  self::setCourseDefinedFieldValues($form, $this->obj_id, $ilUser->getId());
109 
110  $this->tpl->setContent($form->getHTML());
111  return true;
112  }
113 
114 
115 
116  protected function initFormAgreement()
117  {
118  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
119  $form = new ilPropertyFormGUI();
120  $form->setTitle($this->lng->txt($this->type.'_agreement_header'));
121  $form->setFormAction($GLOBALS['ilCtrl']->getFormAction($this));
122  $form->addCommandButton('save', $this->lng->txt('save'));
123 
124  $form = self::addExportFieldInfo($form, $this->obj_id, $this->type);
125  $form = self::addCustomFields($form, $this->obj_id, $this->type);
126 
127  if($this->getPrivacy()->confirmationRequired($this->type))
128  {
129  $form = self::addAgreement($form, $this->obj_id, $this->type);
130  }
131 
132  return $form;
133  }
134 
143  public static function addExportFieldInfo($form,$a_obj_id,$a_type)
144  {
145  global $lng;
146 
147  include_once('Services/PrivacySecurity/classes/class.ilExportFieldsInfo.php');
149 
150  $fields = new ilCustomInputGUI($lng->txt($a_type.'_user_agreement'),'');
151  $tpl = new ilTemplate('tpl.agreement_form.html',true,true,'Services/Membership');
152  $tpl->setVariable('TXT_INFO_AGREEMENT',$lng->txt($a_type.'_info_agreement'));
153  foreach($fields_info->getExportableFields() as $field)
154  {
155  $tpl->setCurrentBlock('field_item');
156  $tpl->setVariable('FIELD_NAME',$lng->txt($field));
157  $tpl->parseCurrentBlock();
158  }
159 
160  // #17609 - not part of ilExportFieldsInfo::getExportableFields()
161  // see ilExportFieldsInfo::getSelectableFieldsInfo()
162  include_once('Services/User/classes/class.ilUserDefinedFields.php');
163  foreach(ilUserDefinedFields::_getInstance()->getExportableFields($a_obj_id) as $field)
164  {
165  $tpl->setCurrentBlock('field_item');
166  $tpl->setVariable('FIELD_NAME',$field['field_name']);
167  $tpl->parseCurrentBlock();
168  }
169 
170  $fields->setHtml($tpl->get());
171  $form->addItem($fields);
172 
173  return $form;
174  }
175 
182  public static function addAgreement($form, $a_obj_id, $a_type)
183  {
184  global $lng;
185 
186  $agreement = new ilCheckboxInputGUI($lng->txt($a_type.'_agree'),'agreement');
187  $agreement->setRequired(true);
188  $agreement->setOptionTitle($lng->txt($a_type.'_info_agree'));
189  $agreement->setValue(1);
190  $form->addItem($agreement);
191 
192  return $form;
193  }
194 
201  public static function addCustomFields($form, $a_obj_id, $a_type, $a_mode = 'user')
202  {
203  global $lng;
204 
205  include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
206  include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
207 
208  if(!count($cdf_fields = ilCourseDefinedFieldDefinition::_getFields($a_obj_id)))
209  {
210  return $form;
211  }
212 
213  if($a_mode == 'user')
214  {
215  $cdf = new ilNonEditableValueGUI($lng->txt('ps_'.$a_type.'_user_fields'));
216  $cdf->setValue($lng->txt($a_type.'_ps_cdf_info'));
217  $cdf->setRequired(true);
218  }
219 
220  foreach($cdf_fields as $field_obj)
221  {
222  switch($field_obj->getType())
223  {
224  case IL_CDF_TYPE_SELECT:
225 
226  if($field_obj->getValueOptions())
227  {
228  // Show as radio group
229  $option_radios = new ilRadioGroupInputGUI($field_obj->getName(), 'cdf_'.$field_obj->getId());
230  if($field_obj->isRequired())
231  {
232  $option_radios->setRequired(true);
233  }
234 
235  $open_answer_indexes = (array) $field_obj->getValueOptions();
236  foreach($field_obj->getValues() as $key => $val)
237  {
238  $option_radio = new ilRadioOption($val,$field_obj->getId().'_'.$key);
239 
240  // open answers
241  if(in_array($key, $open_answer_indexes))
242  {
243  $open_answer = new ilTextInputGUI($lng->txt("form_open_answer"), 'cdf_oa_'.$field_obj->getId().'_'.$key);
244  $open_answer->setRequired(true);
245  $option_radio->addSubItem($open_answer);
246  }
247 
248  $option_radios->addOption($option_radio);
249  }
250  if($a_mode == 'user')
251  {
252  $cdf->addSubItem($option_radios);
253  }
254  else
255  {
256  $form->addItem($option_radios);
257  }
258  }
259  else
260  {
261  $select = new ilSelectInputGUI($field_obj->getName(),'cdf_'.$field_obj->getId());
262  #$select->setValue(ilUtil::stripSlashes($_POST['cdf'][$field_obj->getId()]));
263  $select->setOptions($field_obj->prepareSelectBox());
264  if($field_obj->isRequired())
265  {
266  $select->setRequired(true);
267  }
268  if($a_mode == 'user')
269  {
270  $cdf->addSubItem($select);
271  }
272  else
273  {
274  $form->addItem($select);
275  }
276  }
277  break;
278 
279  case IL_CDF_TYPE_TEXT:
280  $text = new ilTextInputGUI($field_obj->getName(),'cdf_'.$field_obj->getId());
281  #$text->setValue(ilUtil::stripSlashes($_POST['cdf'][$field_obj->getId()]));
282  $text->setSize(32);
283  $text->setMaxLength(255);
284  if($field_obj->isRequired())
285  {
286  $text->setRequired(true);
287  }
288  if($a_mode == 'user')
289  {
290  $cdf->addSubItem($text);
291  }
292  else
293  {
294  $form->addItem($text);
295  }
296  break;
297  }
298  }
299  if($a_mode == 'user')
300  {
301  $form->addItem($cdf);
302  }
303  return $form;
304 
305  }
306 
307 
308 
316  private function save()
317  {
318  global $ilUser;
319 
320  $form = $this->initFormAgreement();
321 
322  // #14715 - checkInput() does not work for checkboxes
323  if($this->checkAgreement() &&
324  $form->checkInput())
325  {
326  self::saveCourseDefinedFields($form, $this->obj_id);
327 
328  $this->getAgreement()->setAccepted(true);
329  $this->getAgreement()->setAcceptanceTime(time());
330  $this->getAgreement()->save();
331 
332  include_once './Services/Membership/classes/class.ilObjectCustomUserFieldHistory.php';
333  $history = new ilObjectCustomUserFieldHistory($this->obj_id,$ilUser->getId());
334  $history->setUpdateUser($ilUser->getId());
335  $history->setEditingTime(new ilDateTime(time(),IL_CAL_UNIX));
336  $history->save();
337 
338  $this->ctrl->returnToParent($this);
339  }
340  elseif(!$this->checkAgreement())
341  {
342  ilUtil::sendFailure($this->lng->txt($this->type.'_agreement_required'));
343  $form->setValuesByPost();
344  $this->showAgreement($form);
345  return false;
346  }
347  else
348  {
349  ilUtil::sendFailure($this->lng->txt('fill_out_all_required_fields'));
350  $form->setValuesByPost();
351  $this->showAgreement($form);
352  return false;
353  }
354  }
355 
356  public static function setCourseDefinedFieldValues(ilPropertyFormGUI $form, $a_obj_id, $a_usr_id = 0)
357  {
358  global $ilUser;
359 
360  if(!$a_usr_id)
361  {
362  $a_usr_id = $ilUser->getId();
363  }
364 
365  $ud = ilCourseUserData::_getValuesByObjId($a_obj_id);
366 
367  foreach(ilCourseDefinedFieldDefinition::_getFields($a_obj_id) as $field_obj)
368  {
369  $current_value = $ud[$a_usr_id][$field_obj->getId()];
370  if(!$current_value)
371  {
372  continue;
373  }
374 
375  switch($field_obj->getType())
376  {
377  case IL_CDF_TYPE_SELECT:
378 
379  $id = $field_obj->getIdByValue($current_value);
380 
381  if($id >= 0)
382  {
383  $item = $form->getItemByPostVar('cdf_'.$field_obj->getId());
384  $item->setValue($field_obj->getId().'_'.$id);
385  }
386  else
387  {
388  // open answer
389  $open_answer_indexes = $field_obj->getValueOptions();
390  $open_answer_index = end($open_answer_indexes);
391  $item = $form->getItemByPostVar('cdf_'.$field_obj->getId());
392  $item->setValue($field_obj->getId().'_'.$open_answer_index);
393  $item_txt = $form->getItemByPostVar('cdf_oa_'.$field_obj->getId().'_'.$open_answer_index);
394  if($item_txt)
395  {
396  $item_txt->setValue($current_value);
397  }
398  }
399  break;
400 
401  case IL_CDF_TYPE_TEXT:
402  $item = $form->getItemByPostVar('cdf_'.$field_obj->getId());
403  $item->setValue($current_value);
404  break;
405  }
406  }
407  }
408 
409 
414  public static function saveCourseDefinedFields(ilPropertyFormGUI $form, $a_obj_id, $a_usr_id = 0)
415  {
416  global $ilUser;
417 
418  if(!$a_usr_id)
419  {
420  $a_usr_id = $ilUser->getId();
421  }
422 
423  foreach(ilCourseDefinedFieldDefinition::_getFields($a_obj_id) as $field_obj)
424  {
425  switch($field_obj->getType())
426  {
427  case IL_CDF_TYPE_SELECT:
428 
429  // Split value id from post
430  list($field_id,$option_id) = explode('_', $form->getInput('cdf_'.$field_obj->getId()));
431  $open_answer_indexes = (array) $field_obj->getValueOptions();
432  if(in_array($option_id, $open_answer_indexes))
433  {
434  $value = $form->getInput('cdf_oa_'.$field_obj->getId().'_'.$option_id);
435  }
436  else
437  {
438  $value = $field_obj->getValueById($option_id);
439  }
440  break;
441 
442  case IL_CDF_TYPE_TEXT:
443  $value = $form->getInput('cdf_'.$field_obj->getId());
444  break;
445  }
446 
447  $course_user_data = new ilCourseUserData($a_usr_id,$field_obj->getId());
448  $course_user_data->setValue($value);
449  $course_user_data->update();
450  }
451  }
452 
453 
460  private function checkAgreement()
461  {
462  global $ilUser;
463 
464  if($_POST['agreement'])
465  {
466  return true;
467  }
468  if($this->privacy->confirmationRequired($this->type))
469  {
470  return false;
471  }
472  return true;
473  }
474 
475 
476 
483  private function init()
484  {
485  global $ilUser;
486 
487  $this->required_fullfilled = ilCourseUserData::_checkRequired($ilUser->getId(),$this->obj_id);
488  $this->agreement_required = $this->getAgreement()->agreementRequired();
489  }
490 
496  private function sendInfoMessage()
497  {
498  $message = '';
499  if($this->agreement_required)
500  {
501  $message = $this->lng->txt($this->type.'_ps_agreement_req_info');
502  }
503  if(!$this->required_fullfilled)
504  {
505  if(strlen($message))
506  {
507  $message .= '<br />';
508  }
509  $message .= $this->lng->txt($this->type.'_ps_required_info');
510  }
511 
512  if(strlen($message))
513  {
514  ilUtil::sendFailure($message);
515  }
516  }
517 }
518 
519 
520 ?>
This class represents an option in a radio group.
setHtml($a_html)
Set Html.
$_POST['username']
Definition: cron.php:12
getItemByPostVar($a_post_var)
Get Item by POST variable.
static _getInstance()
Get instance.
This class represents a selection list property in a property form.
This class represents a property form user interface.
static addExportFieldInfo($form, $a_obj_id, $a_type)
Add export field info to form type $lng.
sendInfoMessage()
Send info message.
static _getValuesByObjId($a_obj_id)
Get values by obj_id (for all users)
$cmd
Definition: sahs_server.php:35
This class represents a checkbox property in a property form.
__construct($a_ref_id)
Constructor.
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
global $ilCtrl
Definition: ilias.php:18
This class represents a property in a property form.
static _getInstanceByType($a_type)
Get Singleton Instance.
special template class to simplify handling of ITX/PEAR
static addAgreement($form, $a_obj_id, $a_type)
Add agreement to form.
This class represents a text property in a property form.
Date and time handling
getPrivacy()
Get privycy settings.
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
setOptions($a_options)
Set Options.
static _checkRequired($a_usr_id, $a_obj_id)
Check required fields.
static _lookupType($a_id, $a_reference=false)
lookup object type
showAgreement(ilPropertyFormGUI $form=null)
Show agreement form.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static saveCourseDefinedFields(ilPropertyFormGUI $form, $a_obj_id, $a_usr_id=0)
Save course defined fields.
This class represents a custom property in a property form.
This class represents a non editable value in a property form.
global $ilUser
Definition: imgupload.php:15
global $ilDB
$text
static _getInstance()
Get instance of ilPrivacySettings.
static setCourseDefinedFieldValues(ilPropertyFormGUI $form, $a_obj_id, $a_usr_id=0)
$GLOBALS['PHPCAS_CLIENT']
This global variable is used by the interface class phpCAS.
Definition: CAS.php:276
setRequired($a_required)
Set Required.
static _getFields($a_container_id, $a_sort=IL_CDF_SORT_NAME)
Get all fields of a container.