ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  default:
71  if (!$cmd or $cmd == 'view') {
72  $cmd = 'showAgreement';
73  }
74  $this->$cmd();
75  break;
76  }
77  }
78 
83  public function getPrivacy()
84  {
85  return $this->privacy;
86  }
87 
91  public function getAgreement()
92  {
93  return $this->agreement;
94  }
95 
101  protected function showAgreement(ilPropertyFormGUI $form = null)
102  {
103  global $ilUser;
104 
105  if (!$form instanceof ilPropertyFormGUI) {
106  $form = $this->initFormAgreement($form);
107  self::setCourseDefinedFieldValues($form, $this->obj_id, $ilUser->getId());
108  }
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  $form = self::addAgreement($form, $this->obj_id, $this->type);
129  }
130 
131  return $form;
132  }
133 
142  public static function addExportFieldInfo($form, $a_obj_id, $a_type)
143  {
144  global $lng;
145 
146  include_once('Services/PrivacySecurity/classes/class.ilExportFieldsInfo.php');
148 
149  $fields = new ilCustomInputGUI($lng->txt($a_type . '_user_agreement'), '');
150  $tpl = new ilTemplate('tpl.agreement_form.html', true, true, 'Services/Membership');
151  $tpl->setVariable('TXT_INFO_AGREEMENT', $lng->txt($a_type . '_info_agreement'));
152  foreach ($fields_info->getExportableFields() as $field) {
153  $tpl->setCurrentBlock('field_item');
154  $tpl->setVariable('FIELD_NAME', $lng->txt($field));
155  $tpl->parseCurrentBlock();
156  }
157 
158  // #17609 - not part of ilExportFieldsInfo::getExportableFields()
159  // see ilExportFieldsInfo::getSelectableFieldsInfo()
160  include_once('Services/User/classes/class.ilUserDefinedFields.php');
161  foreach (ilUserDefinedFields::_getInstance()->getExportableFields($a_obj_id) as $field) {
162  $tpl->setCurrentBlock('field_item');
163  $tpl->setVariable('FIELD_NAME', $field['field_name']);
164  $tpl->parseCurrentBlock();
165  }
166 
167  $fields->setHtml($tpl->get());
168  $form->addItem($fields);
169 
170  return $form;
171  }
172 
179  public static function addAgreement($form, $a_obj_id, $a_type)
180  {
181  global $lng;
182 
183  $agreement = new ilCheckboxInputGUI($lng->txt($a_type . '_agree'), 'agreement');
184  $agreement->setRequired(true);
185  $agreement->setOptionTitle($lng->txt($a_type . '_info_agree'));
186  $agreement->setValue(1);
187  $form->addItem($agreement);
188 
189  return $form;
190  }
191 
198  public static function addCustomFields($form, $a_obj_id, $a_type, $a_mode = 'user')
199  {
200  global $lng;
201 
202  include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
203  include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
204 
205  if (!count($cdf_fields = ilCourseDefinedFieldDefinition::_getFields($a_obj_id))) {
206  return $form;
207  }
208 
209  if ($a_mode == 'user') {
210  $cdf = new ilNonEditableValueGUI($lng->txt('ps_' . $a_type . '_user_fields'));
211  $cdf->setValue($lng->txt($a_type . '_ps_cdf_info'));
212  $cdf->setRequired(true);
213  }
214 
215  foreach ($cdf_fields as $field_obj) {
216  switch ($field_obj->getType()) {
217  case IL_CDF_TYPE_SELECT:
218 
219  if ($field_obj->getValueOptions()) {
220  // Show as radio group
221  $option_radios = new ilRadioGroupInputGUI($field_obj->getName(), 'cdf_' . $field_obj->getId());
222  if ($field_obj->isRequired()) {
223  $option_radios->setRequired(true);
224  }
225 
226  $open_answer_indexes = (array) $field_obj->getValueOptions();
227  foreach ($field_obj->getValues() as $key => $val) {
228  $option_radio = new ilRadioOption($val, $field_obj->getId() . '_' . $key);
229 
230  // open answers
231  if (in_array($key, $open_answer_indexes)) {
232  $open_answer = new ilTextInputGUI($lng->txt("form_open_answer"), 'cdf_oa_' . $field_obj->getId() . '_' . $key);
233  $open_answer->setRequired(true);
234  $option_radio->addSubItem($open_answer);
235  }
236 
237  $option_radios->addOption($option_radio);
238  }
239  if ($a_mode == 'user') {
240  $cdf->addSubItem($option_radios);
241  } else {
242  $form->addItem($option_radios);
243  }
244  } else {
245  $select = new ilSelectInputGUI($field_obj->getName(), 'cdf_' . $field_obj->getId());
246  #$select->setValue(ilUtil::stripSlashes($_POST['cdf'][$field_obj->getId()]));
247  $select->setOptions($field_obj->prepareSelectBox());
248  if ($field_obj->isRequired()) {
249  $select->setRequired(true);
250  }
251  if ($a_mode == 'user') {
252  $cdf->addSubItem($select);
253  } else {
254  $form->addItem($select);
255  }
256  }
257  break;
258 
259  case IL_CDF_TYPE_TEXT:
260  $text = new ilTextInputGUI($field_obj->getName(), 'cdf_' . $field_obj->getId());
261  #$text->setValue(ilUtil::stripSlashes($_POST['cdf'][$field_obj->getId()]));
262  $text->setSize(32);
263  $text->setMaxLength(255);
264  if ($field_obj->isRequired()) {
265  $text->setRequired(true);
266  }
267  if ($a_mode == 'user') {
268  $cdf->addSubItem($text);
269  } else {
270  $form->addItem($text);
271  }
272  break;
273  }
274  }
275  if ($a_mode == 'user') {
276  $form->addItem($cdf);
277  }
278  return $form;
279  }
280 
281 
282 
290  private function save()
291  {
292  global $ilUser;
293 
294  $form = $this->initFormAgreement();
295 
296  // #14715 - checkInput() does not work for checkboxes
297  if ($this->checkAgreement() && $form->checkInput()) {
298  self::saveCourseDefinedFields($form, $this->obj_id);
299 
300  $this->getAgreement()->setAccepted(true);
301  $this->getAgreement()->setAcceptanceTime(time());
302  $this->getAgreement()->save();
303 
304  include_once './Services/Membership/classes/class.ilObjectCustomUserFieldHistory.php';
305  $history = new ilObjectCustomUserFieldHistory($this->obj_id, $ilUser->getId());
306  $history->setUpdateUser($ilUser->getId());
307  $history->setEditingTime(new ilDateTime(time(), IL_CAL_UNIX));
308  $history->save();
309 
310  $this->ctrl->returnToParent($this);
311  } elseif (!$this->checkAgreement()) {
312  ilUtil::sendFailure($this->lng->txt($this->type . '_agreement_required'));
313  $form->setValuesByPost();
314  $this->showAgreement($form);
315  return false;
316  } else {
317  ilUtil::sendFailure($this->lng->txt('fill_out_all_required_fields'));
318  $form->setValuesByPost();
319  $this->showAgreement($form);
320  return false;
321  }
322  }
323 
324  public static function setCourseDefinedFieldValues(ilPropertyFormGUI $form, $a_obj_id, $a_usr_id = 0)
325  {
326  global $ilUser;
327 
328  if (!$a_usr_id) {
329  $a_usr_id = $ilUser->getId();
330  }
331 
332  $ud = ilCourseUserData::_getValuesByObjId($a_obj_id);
333 
334  foreach (ilCourseDefinedFieldDefinition::_getFields($a_obj_id) as $field_obj) {
335  $current_value = $ud[$a_usr_id][$field_obj->getId()];
336  if (!$current_value) {
337  continue;
338  }
339 
340  switch ($field_obj->getType()) {
341  case IL_CDF_TYPE_SELECT:
342 
343  $id = $field_obj->getIdByValue($current_value);
344 
345  if ($id >= 0) {
346  $item = $form->getItemByPostVar('cdf_' . $field_obj->getId());
347  $item->setValue($field_obj->getId() . '_' . $id);
348  } else {
349  // open answer
350  $open_answer_indexes = $field_obj->getValueOptions();
351  $open_answer_index = end($open_answer_indexes);
352  $item = $form->getItemByPostVar('cdf_' . $field_obj->getId());
353  $item->setValue($field_obj->getId() . '_' . $open_answer_index);
354  $item_txt = $form->getItemByPostVar('cdf_oa_' . $field_obj->getId() . '_' . $open_answer_index);
355  if ($item_txt) {
356  $item_txt->setValue($current_value);
357  }
358  }
359  break;
360 
361  case IL_CDF_TYPE_TEXT:
362  $item = $form->getItemByPostVar('cdf_' . $field_obj->getId());
363  $item->setValue($current_value);
364  break;
365  }
366  }
367  }
368 
369 
374  public static function saveCourseDefinedFields(ilPropertyFormGUI $form, $a_obj_id, $a_usr_id = 0)
375  {
376  global $ilUser;
377 
378  if (!$a_usr_id) {
379  $a_usr_id = $ilUser->getId();
380  }
381 
382  foreach (ilCourseDefinedFieldDefinition::_getFields($a_obj_id) as $field_obj) {
383  switch ($field_obj->getType()) {
384  case IL_CDF_TYPE_SELECT:
385 
386  // Split value id from post
387  list($field_id, $option_id) = explode('_', $form->getInput('cdf_' . $field_obj->getId()));
388  $open_answer_indexes = (array) $field_obj->getValueOptions();
389  if (in_array($option_id, $open_answer_indexes)) {
390  $value = $form->getInput('cdf_oa_' . $field_obj->getId() . '_' . $option_id);
391  } else {
392  $value = $field_obj->getValueById($option_id);
393  }
394  break;
395 
396  case IL_CDF_TYPE_TEXT:
397  $value = $form->getInput('cdf_' . $field_obj->getId());
398  break;
399  }
400 
401  $course_user_data = new ilCourseUserData($a_usr_id, $field_obj->getId());
402  $course_user_data->setValue($value);
403  $course_user_data->update();
404  }
405  }
406 
407 
414  private function checkAgreement()
415  {
416  global $ilUser;
417 
418  if ($_POST['agreement']) {
419  return true;
420  }
421  if ($this->privacy->confirmationRequired($this->type)) {
422  return false;
423  }
424  return true;
425  }
426 
427 
428 
435  private function init()
436  {
437  global $ilUser;
438 
439  $this->required_fullfilled = ilCourseUserData::_checkRequired($ilUser->getId(), $this->obj_id);
440  $this->agreement_required = $this->getAgreement()->agreementRequired();
441  }
442 
448  private function sendInfoMessage()
449  {
450  $message = '';
451  if ($this->agreement_required) {
452  $message = $this->lng->txt($this->type . '_ps_agreement_req_info');
453  }
454  if (!$this->required_fullfilled) {
455  if (strlen($message)) {
456  $message .= '<br />';
457  }
458  $message .= $this->lng->txt($this->type . '_ps_required_info');
459  }
460 
461  if (strlen($message)) {
463  }
464  }
465 }
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.
if(!array_key_exists('StateId', $_REQUEST)) $id
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:92
catch(Exception $e) $message
This class represents a property in a property form.
if(isset($_POST['submit'])) $form
static _getInstanceByType($a_type)
Get Singleton Instance.
special template class to simplify handling of ITX/PEAR
$text
Definition: errorreport.php:18
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
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)
$key
Definition: croninfo.php:18
$_POST["username"]
setRequired($a_required)
Set Required.
static _getFields($a_container_id, $a_sort=IL_CDF_SORT_NAME)
Get all fields of a container.