ILIAS  release_8 Revision v8.24
class.ilMemberAgreementGUI.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5
6
25
32{
33 private int $ref_id;
34 private int $obj_id;
35 private string $type;
37 protected Factory $refinery;
44 private bool $required_fullfilled = false;
45 private bool $agreement_required = false;
46
47 public function __construct(int $a_ref_id)
48 {
49 global $DIC;
50
51 $this->http = $DIC->http();
52 $this->refinery = $DIC->refinery();
53
54 $this->ref_id = $a_ref_id;
55 $this->obj_id = ilObject::_lookupObjId($this->ref_id);
56 $this->type = ilObject::_lookupType($this->obj_id);
57 $this->ctrl = $DIC->ctrl();
58 $this->tpl = $DIC->ui()->mainTemplate();
59 $this->lng = $DIC->language();
60 $this->lng->loadLanguageModule('ps');
61 $this->user = $DIC->user();
62
63 $this->privacy = ilPrivacySettings::getInstance();
64 $this->agreement = new ilMemberAgreement($DIC->user()->getId(), $this->obj_id);
65 $this->init();
66 }
67
68 public function executeCommand(): void
69 {
70 $next_class = $this->ctrl->getNextClass($this);
71 $cmd = $this->ctrl->getCmd();
72
73 switch ($next_class) {
74 default:
75 if (!$cmd || $cmd === 'view') {
76 $cmd = 'showAgreement';
77 }
78 $this->$cmd();
79 break;
80 }
81 }
82
83 public function getPrivacy(): ilPrivacySettings
84 {
85 return $this->privacy;
86 }
87
89 {
90 return $this->agreement;
91 }
92
96 protected function showAgreement(?ilPropertyFormGUI $form = null): void
97 {
98 if ($form === null) {
99 $form = $this->initFormAgreement();
100 self::setCourseDefinedFieldValues($form, $this->obj_id, $this->user->getId());
101 }
102 $this->tpl->setContent($form->getHTML());
103 }
104
106 {
107 $form = new ilPropertyFormGUI();
108 $form->setTitle($this->lng->txt($this->type . '_agreement_header'));
109 $form->setFormAction($this->ctrl->getFormAction($this));
110 $form->addCommandButton('save', $this->lng->txt('save'));
111
112 $form = self::addExportFieldInfo($form, $this->obj_id, $this->type);
113 $form = self::addCustomFields($form, $this->obj_id, $this->type);
114
115 if ($this->getPrivacy()->confirmationRequired($this->type)) {
116 $form = self::addAgreement($form, $this->obj_id, $this->type);
117 }
118 return $form;
119 }
120
121 public static function addExportFieldInfo(
122 ilPropertyFormGUI $form,
123 int $a_obj_id,
124 string $a_type
126 global $DIC;
127
128 $lng = $DIC->language();
129
131
132 $fields = new ilCustomInputGUI($lng->txt($a_type . '_user_agreement'), '');
133 $tpl = new ilTemplate('tpl.agreement_form.html', true, true, 'Services/Membership');
134 $tpl->setVariable('TXT_INFO_AGREEMENT', $lng->txt($a_type . '_info_agreement'));
135 foreach ($fields_info->getExportableFields() as $field) {
136 $tpl->setCurrentBlock('field_item');
137 $tpl->setVariable('FIELD_NAME', $lng->txt($field));
139 }
140
141 // #17609 - not part of ilExportFieldsInfo::getExportableFields()
142 // see ilExportFieldsInfo::getSelectableFieldsInfo()
143 foreach (ilUserDefinedFields::_getInstance()->getExportableFields($a_obj_id) as $field) {
144 $tpl->setCurrentBlock('field_item');
145 $tpl->setVariable('FIELD_NAME', $field['field_name']);
147 }
148 $fields->setHtml($tpl->get());
149 $form->addItem($fields);
150 return $form;
151 }
152
153 public static function addAgreement(ilPropertyFormGUI $form, int $a_obj_id, string $a_type): ilPropertyFormGUI
154 {
155 global $DIC;
156
157 $lng = $DIC['lng'];
158
159 $agreement = new ilCheckboxInputGUI($lng->txt($a_type . '_agree'), 'agreement');
160 $agreement->setRequired(true);
161 $agreement->setOptionTitle($lng->txt($a_type . '_info_agree'));
162 $agreement->setValue('1');
163 $form->addItem($agreement);
164
165 return $form;
166 }
167
168 public static function addCustomFields(
169 ilPropertyFormGUI $form,
170 int $a_obj_id,
171 string $a_type,
172 string $a_mode = 'user'
174 global $DIC;
175
176 $lng = $DIC['lng'];
177
178 if (!count($cdf_fields = ilCourseDefinedFieldDefinition::_getFields($a_obj_id))) {
179 return $form;
180 }
181
182 if ($a_mode === 'user') {
183 $cdf = new ilNonEditableValueGUI($lng->txt('ps_' . $a_type . '_user_fields'));
184 $cdf->setValue($lng->txt($a_type . '_ps_cdf_info'));
185 $cdf->setRequired(true);
186 }
187
188 foreach ($cdf_fields as $field_obj) {
189 switch ($field_obj->getType()) {
191
192 if ($field_obj->getValueOptions()) {
193 // Show as radio group
194 $option_radios = new ilRadioGroupInputGUI($field_obj->getName(), 'cdf_' . $field_obj->getId());
195 if ($field_obj->isRequired()) {
196 $option_radios->setRequired(true);
197 }
198
199 $open_answer_indexes = $field_obj->getValueOptions();
200 foreach ($field_obj->getValues() as $key => $val) {
201 $option_radio = new ilRadioOption($val, $field_obj->getId() . '_' . $key);
202
203 // open answers
204 if (in_array($key, $open_answer_indexes)) {
205 $open_answer = new ilTextInputGUI(
206 $lng->txt("form_open_answer"),
207 'cdf_oa_' . $field_obj->getId() . '_' . $key
208 );
209 $open_answer->setRequired(true);
210 $option_radio->addSubItem($open_answer);
211 }
212
213 $option_radios->addOption($option_radio);
214 }
215 if ($a_mode === 'user') {
216 $cdf->addSubItem($option_radios);
217 } else {
218 $form->addItem($option_radios);
219 }
220 } else {
221 $select = new ilSelectInputGUI($field_obj->getName(), 'cdf_' . $field_obj->getId());
222 $select->setOptions($field_obj->prepareSelectBox());
223 if ($field_obj->isRequired()) {
224 $select->setRequired(true);
225 }
226 if ($a_mode === 'user') {
227 $cdf->addSubItem($select);
228 } else {
229 $form->addItem($select);
230 }
231 }
232 break;
233
235 $text = new ilTextInputGUI($field_obj->getName(), 'cdf_' . $field_obj->getId());
236 $text->setSize(32);
237 $text->setMaxLength(255);
238 if ($field_obj->isRequired()) {
239 $text->setRequired(true);
240 }
241 if ($a_mode === 'user') {
242 $cdf->addSubItem($text);
243 } else {
244 $form->addItem($text);
245 }
246 break;
247 }
248 }
249 if ($a_mode === 'user') {
250 $form->addItem($cdf);
251 }
252 return $form;
253 }
254
255 private function save(): bool
256 {
257 $form = $this->initFormAgreement();
258
259 // #14715 - checkInput() does not work for checkboxes
260 if ($this->checkAgreement() && $form->checkInput()) {
261 self::saveCourseDefinedFields($form, $this->obj_id);
262
263 $this->getAgreement()->setAccepted(true);
264 $this->getAgreement()->setAcceptanceTime(time());
265 $this->getAgreement()->save();
266
267 $history = new ilObjectCustomUserFieldHistory($this->obj_id, $this->user->getId());
268 $history->setUpdateUser($this->user->getId());
269 $history->setEditingTime(new ilDateTime(time(), IL_CAL_UNIX));
270 $history->save();
271
272 $this->ctrl->returnToParent($this);
273 } elseif (!$this->checkAgreement()) {
274 $this->tpl->setOnScreenMessage('failure', $this->lng->txt($this->type . '_agreement_required'));
275 $form->setValuesByPost();
276 $this->showAgreement($form);
277 return false;
278 } else {
279 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('fill_out_all_required_fields'));
280 $form->setValuesByPost();
281 $this->showAgreement($form);
282 return false;
283 }
284 return true;
285 }
286
287 public static function setCourseDefinedFieldValues(
288 ilPropertyFormGUI $form,
289 int $a_obj_id,
290 int $a_usr_id = 0
291 ): void {
292 global $DIC;
293
294 $ilUser = $DIC['ilUser'];
295
296 if (!$a_usr_id) {
297 $a_usr_id = $ilUser->getId();
298 }
299
301
302 foreach (ilCourseDefinedFieldDefinition::_getFields($a_obj_id) as $field_obj) {
303 $current_value = (string) ($ud[$a_usr_id][$field_obj->getId()] ?? '');
304 if (!$current_value) {
305 continue;
306 }
307
308 switch ($field_obj->getType()) {
310
311 $id = $field_obj->getIdByValue($current_value);
312
313 if ($id >= 0) {
314 $item = $form->getItemByPostVar('cdf_' . $field_obj->getId());
315 $item->setValue($field_obj->getId() . '_' . $id);
316 } else {
317 // open answer
318 $open_answer_indexes = $field_obj->getValueOptions();
319 $open_answer_index = end($open_answer_indexes);
320 $item = $form->getItemByPostVar('cdf_' . $field_obj->getId());
321 $item->setValue($field_obj->getId() . '_' . $open_answer_index);
322 $item_txt = $form->getItemByPostVar('cdf_oa_' . $field_obj->getId() . '_' . $open_answer_index);
323 if ($item_txt) {
324 $item_txt->setValue($current_value);
325 }
326 }
327 break;
328
330 $item = $form->getItemByPostVar('cdf_' . $field_obj->getId());
331 $item->setValue($current_value);
332 break;
333 }
334 }
335 }
336
337 public static function saveCourseDefinedFields(ilPropertyFormGUI $form, int $a_obj_id, int $a_usr_id = 0): void
338 {
339 global $DIC;
340
341 $ilUser = $DIC['ilUser'];
342 if (!$a_usr_id) {
343 $a_usr_id = $ilUser->getId();
344 }
345 foreach (ilCourseDefinedFieldDefinition::_getFields($a_obj_id) as $field_obj) {
346 $value = '';
347 switch ($field_obj->getType()) {
349 // Split value id from post
350 $exp = explode('_', $form->getInput('cdf_' . $field_obj->getId()));
351 $field_id = $exp[0];
352 $option_id = $exp[1] ?? null;
353 $open_answer_indexes = $field_obj->getValueOptions();
354 if (in_array($option_id, $open_answer_indexes)) {
355 $value = $form->getInput('cdf_oa_' . $field_obj->getId() . '_' . $option_id);
356 } else {
357 $value = $field_obj->getValueById((int) $option_id);
358 }
359 break;
360
362 $value = $form->getInput('cdf_' . $field_obj->getId());
363 break;
364 }
365
366 $course_user_data = new ilCourseUserData($a_usr_id, $field_obj->getId());
367 $course_user_data->setValue($value);
368 $course_user_data->update();
369 }
370 }
371
372 private function checkAgreement(): bool
373 {
374 $agreement = false;
375 if ($this->http->wrapper()->post()->has('agreement')) {
376 $agreement = $this->http->wrapper()->post()->retrieve(
377 'agreement',
378 $this->refinery->kindlyTo()->bool()
379 );
380 }
381 if ($agreement) {
382 return true;
383 }
384 if ($this->privacy->confirmationRequired($this->type)) {
385 return false;
386 }
387 return true;
388 }
389
390 private function init(): void
391 {
392 $this->required_fullfilled = ilCourseUserData::_checkRequired($this->user->getId(), $this->obj_id);
393 $this->agreement_required = $this->getAgreement()->agreementRequired();
394 }
395
396 private function sendInfoMessage(): void
397 {
398 $message = '';
399 if ($this->agreement_required) {
400 $message = $this->lng->txt($this->type . '_ps_agreement_req_info');
401 }
402 if (!$this->required_fullfilled) {
403 if ($message !== '') {
404 $message .= '<br />';
405 }
406 $message .= $this->lng->txt($this->type . '_ps_required_info');
407 }
408 if ($message !== '') {
409 $this->tpl->setOnScreenMessage('failure', $message);
410 }
411 }
412}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds data types.
Definition: Factory.php:21
const IL_CAL_UNIX
This class represents a checkbox property in a property form.
static _getFields(int $a_container_id, $a_sort=self::IL_CDF_SORT_NAME)
Get all fields of a container.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getValuesByObjId(int $a_obj_id)
static _checkRequired(int $a_usr_id, int $a_obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
@classDescription Date and time handling
static _getInstanceByType(string $a_type)
Get Singleton Instance.
language handling
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
static addCustomFields(ilPropertyFormGUI $form, int $a_obj_id, string $a_type, string $a_mode='user')
static setCourseDefinedFieldValues(ilPropertyFormGUI $form, int $a_obj_id, int $a_usr_id=0)
static addAgreement(ilPropertyFormGUI $form, int $a_obj_id, string $a_type)
ilGlobalTemplateInterface $tpl
static addExportFieldInfo(ilPropertyFormGUI $form, int $a_obj_id, string $a_type)
showAgreement(?ilPropertyFormGUI $form=null)
Show agreement form.
static saveCourseDefinedFields(ilPropertyFormGUI $form, int $a_obj_id, int $a_usr_id=0)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
User class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupType(int $id, bool $reference=false)
static _lookupObjId(int $ref_id)
Singleton class that stores all privacy settings.
This class represents a property form user interface.
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
getItemByPostVar(string $a_post_var)
This class represents a property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a selection list property in a property form.
special template class to simplify handling of ITX/PEAR
This class represents a text property in a property form.
global $DIC
Definition: feed.php:28
$ilUser
Definition: imgupload.php:34
Interface GlobalHttpState.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setCurrentBlock(string $part=self::DEFAULT_BLOCK)
Sets the template to the given block.
parseCurrentBlock(string $block_name=self::DEFAULT_BLOCK)
Parses the given block.
setVariable(string $variable, $value='')
Sets the given variable to the given value.
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
static http()
Fetches the global http state from ILIAS.
string $key
Consumer key/client ID value.
Definition: System.php:193
$lng
$message
Definition: xapiexit.php:32