ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMemberAgreementGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
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 $profile = $DIC['user']->getProfile();
130
131 $type = ilObject::_lookupType($a_obj_id);
133
134 $fields = new ilCustomInputGUI($lng->txt($a_type . '_user_agreement'), '');
135 $tpl = new ilTemplate('tpl.agreement_form.html', true, true, 'components/ILIAS/Membership');
136 $tpl->setVariable('TXT_INFO_AGREEMENT', $lng->txt($a_type . '_info_agreement'));
137 foreach ($fields_info->getExportableFields() as $field) {
138 $tpl->setCurrentBlock('field_item');
139 $tpl->setVariable('FIELD_NAME', $lng->txt($field));
141 }
142
143 // #17609 - not part of ilExportFieldsInfo::getExportableFields()
144 // see ilExportFieldsInfo::getSelectableFieldsInfo()
145 foreach ($profile->getVisibleUserDefinedFields(Context::buildFromObjectType($type)) as $field) {
146 $tpl->setCurrentBlock('field_item');
147 $tpl->setVariable('FIELD_NAME', $field->getLabel($lng));
149 }
150 $fields->setHtml($tpl->get());
151 $form->addItem($fields);
152 return $form;
153 }
154
155 public static function addAgreement(ilPropertyFormGUI $form, int $a_obj_id, string $a_type): ilPropertyFormGUI
156 {
157 global $DIC;
158
159 $lng = $DIC['lng'];
160
161 $agreement = new ilCheckboxInputGUI($lng->txt($a_type . '_agree'), 'agreement');
162 $agreement->setRequired(true);
163 $agreement->setOptionTitle($lng->txt($a_type . '_info_agree'));
164 $agreement->setValue('1');
165 $form->addItem($agreement);
166
167 return $form;
168 }
169
170 public static function addCustomFields(
171 ilPropertyFormGUI $form,
172 int $a_obj_id,
173 string $a_type,
174 string $a_mode = 'user'
176 global $DIC;
177
178 $lng = $DIC['lng'];
179
180 if (!count($cdf_fields = ilCourseDefinedFieldDefinition::_getFields($a_obj_id))) {
181 return $form;
182 }
183
184 if ($a_mode === 'user') {
185 $cdf = new ilNonEditableValueGUI($lng->txt('ps_' . $a_type . '_user_fields'));
186 $cdf->setValue($lng->txt($a_type . '_ps_cdf_info'));
187 $cdf->setRequired(true);
188 }
189
190 foreach ($cdf_fields as $field_obj) {
191 switch ($field_obj->getType()) {
193
194 if ($field_obj->getValueOptions()) {
195 // Show as radio group
196 $option_radios = new ilRadioGroupInputGUI($field_obj->getName(), 'cdf_' . $field_obj->getId());
197 if ($field_obj->isRequired()) {
198 $option_radios->setRequired(true);
199 }
200
201 $open_answer_indexes = $field_obj->getValueOptions();
202 foreach ($field_obj->getValues() as $key => $val) {
203 $option_radio = new ilRadioOption($val, $field_obj->getId() . '_' . $key);
204
205 // open answers
206 if (in_array($key, $open_answer_indexes)) {
207 $open_answer = new ilTextInputGUI(
208 $lng->txt("form_open_answer"),
209 'cdf_oa_' . $field_obj->getId() . '_' . $key
210 );
211 $open_answer->setRequired(true);
212 $option_radio->addSubItem($open_answer);
213 }
214
215 $option_radios->addOption($option_radio);
216 }
217 if ($a_mode === 'user') {
218 $cdf->addSubItem($option_radios);
219 } else {
220 $form->addItem($option_radios);
221 }
222 } else {
223 $select = new ilSelectInputGUI($field_obj->getName(), 'cdf_' . $field_obj->getId());
224 $select->setOptions($field_obj->prepareSelectBox());
225 if ($field_obj->isRequired()) {
226 $select->setRequired(true);
227 }
228 if ($a_mode === 'user') {
229 $cdf->addSubItem($select);
230 } else {
231 $form->addItem($select);
232 }
233 }
234 break;
235
237 $text = new ilTextInputGUI($field_obj->getName(), 'cdf_' . $field_obj->getId());
238 $text->setSize(32);
239 $text->setMaxLength(255);
240 if ($field_obj->isRequired()) {
241 $text->setRequired(true);
242 }
243 if ($a_mode === 'user') {
244 $cdf->addSubItem($text);
245 } else {
246 $form->addItem($text);
247 }
248 break;
249 }
250 }
251 if ($a_mode === 'user') {
252 $form->addItem($cdf);
253 }
254 return $form;
255 }
256
257 private function save(): bool
258 {
259 $form = $this->initFormAgreement();
260
261 // #14715 - checkInput() does not work for checkboxes
262 if ($this->checkAgreement() && $form->checkInput()) {
263 self::saveCourseDefinedFields($form, $this->obj_id);
264
265 $this->getAgreement()->setAccepted(true);
266 $this->getAgreement()->setAcceptanceTime(time());
267 $this->getAgreement()->save();
268
269 $history = new ilObjectCustomUserFieldHistory($this->obj_id, $this->user->getId());
270 $history->setUpdateUser($this->user->getId());
271 $history->setEditingTime(new ilDateTime(time(), IL_CAL_UNIX));
272 $history->save();
273
274 $this->ctrl->returnToParent($this);
275 } elseif (!$this->checkAgreement()) {
276 $this->tpl->setOnScreenMessage('failure', $this->lng->txt($this->type . '_agreement_required'));
277 $form->setValuesByPost();
278 $this->showAgreement($form);
279 return false;
280 } else {
281 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('fill_out_all_required_fields'));
282 $form->setValuesByPost();
283 $this->showAgreement($form);
284 return false;
285 }
286 return true;
287 }
288
289 public static function setCourseDefinedFieldValues(
290 ilPropertyFormGUI $form,
291 int $a_obj_id,
292 int $a_usr_id = 0
293 ): void {
294 global $DIC;
295
296 $ilUser = $DIC['ilUser'];
297
298 if (!$a_usr_id) {
299 $a_usr_id = $ilUser->getId();
300 }
301
303
304 foreach (ilCourseDefinedFieldDefinition::_getFields($a_obj_id) as $field_obj) {
305 $current_value = (string) ($ud[$a_usr_id][$field_obj->getId()] ?? '');
306 if (!$current_value) {
307 continue;
308 }
309
310 switch ($field_obj->getType()) {
312
313 $id = $field_obj->getIdByValue($current_value);
314
315 if ($id >= 0) {
316 $item = $form->getItemByPostVar('cdf_' . $field_obj->getId());
317 $item->setValue($field_obj->getId() . '_' . $id);
318 } else {
319 // open answer
320 $open_answer_indexes = $field_obj->getValueOptions();
321 $open_answer_index = end($open_answer_indexes);
322 $item = $form->getItemByPostVar('cdf_' . $field_obj->getId());
323 $item->setValue($field_obj->getId() . '_' . $open_answer_index);
324 $item_txt = $form->getItemByPostVar('cdf_oa_' . $field_obj->getId() . '_' . $open_answer_index);
325 if ($item_txt) {
326 $item_txt->setValue($current_value);
327 }
328 }
329 break;
330
332 $item = $form->getItemByPostVar('cdf_' . $field_obj->getId());
333 $item->setValue($current_value);
334 break;
335 }
336 }
337 }
338
339 public static function saveCourseDefinedFields(ilPropertyFormGUI $form, int $a_obj_id, int $a_usr_id = 0): void
340 {
341 global $DIC;
342
343 $ilUser = $DIC['ilUser'];
344 if (!$a_usr_id) {
345 $a_usr_id = $ilUser->getId();
346 }
347 foreach (ilCourseDefinedFieldDefinition::_getFields($a_obj_id) as $field_obj) {
348 $value = '';
349 switch ($field_obj->getType()) {
351 // Split value id from post
352 $exp = explode('_', $form->getInput('cdf_' . $field_obj->getId()));
353 $field_id = $exp[0];
354 $option_id = $exp[1] ?? null;
355 $open_answer_indexes = $field_obj->getValueOptions();
356 if (in_array($option_id, $open_answer_indexes)) {
357 $value = $form->getInput('cdf_oa_' . $field_obj->getId() . '_' . $option_id);
358 } else {
359 $value = $field_obj->getValueById((int) $option_id);
360 }
361 break;
362
364 $value = $form->getInput('cdf_' . $field_obj->getId());
365 break;
366 }
367
368 $course_user_data = new ilCourseUserData($a_usr_id, $field_obj->getId());
369 $course_user_data->setValue($value);
370 $course_user_data->update();
371 }
372 }
373
374 private function checkAgreement(): bool
375 {
376 $agreement = false;
377 if ($this->http->wrapper()->post()->has('agreement')) {
378 $agreement = $this->http->wrapper()->post()->retrieve(
379 'agreement',
380 $this->refinery->kindlyTo()->bool()
381 );
382 }
383 if ($agreement) {
384 return true;
385 }
386 if ($this->privacy->confirmationRequired($this->type)) {
387 return false;
388 }
389 return true;
390 }
391
392 private function init(): void
393 {
394 $this->required_fullfilled = ilCourseUserData::_checkRequired($this->user->getId(), $this->obj_id);
395 $this->agreement_required = $this->getAgreement()->agreementRequired();
396 }
397
398 private function sendInfoMessage(): void
399 {
400 $message = '';
401 if ($this->agreement_required) {
402 $message = $this->lng->txt($this->type . '_ps_agreement_req_info');
403 }
404 if (!$this->required_fullfilled) {
405 if ($message !== '') {
406 $message .= '<br />';
407 }
408 $message .= $this->lng->txt($this->type . '_ps_required_info');
409 }
410 if ($message !== '') {
411 $this->tpl->setOnScreenMessage('failure', $message);
412 }
413 }
414}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds data types.
Definition: Factory.php:36
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.
static _getValuesByObjId(int $a_obj_id)
static _checkRequired(int $a_usr_id, int $a_obj_id)
This class represents a custom property in a property form.
@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 class represents a non editable value in a property form.
User class.
Editing history for object custom user fields.
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 class represents an option in a radio group.
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.
Interface GlobalHttpState.
setVariable(string $variable, $value='')
Sets the given variable to the given value.
parseCurrentBlock(string $block_name=self::DEFAULT_BLOCK)
Parses the given block.
setCurrentBlock(string $part=self::DEFAULT_BLOCK)
Sets the template to the given block.
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static http()
Fetches the global http state from ILIAS.
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26
$message
Definition: xapiexit.php:31