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