ILIAS  release_8 Revision v8.24
class.ilTermsOfServiceCriterionFormGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28{
31 protected string $formAction;
32 protected ilObjUser $actor;
33 protected string $saveCommand;
34 protected string $cancelCommand;
35 protected string $translatedError = '';
37
38 public function __construct(
43 string $formAction = '',
44 string $saveCommand = 'saveDocument',
45 string $cancelCommand = 'showDocuments'
46 ) {
47 $this->document = $document;
48 $this->assignment = $assignment;
49 $this->criterionTypeFactory = $criterionTypeFactory;
50 $this->actor = $actor;
51 $this->formAction = $formAction;
52 $this->saveCommand = $saveCommand;
53 $this->cancelCommand = $cancelCommand;
54
56
57 $this->initForm();
58 }
59
60 public function setCheckInputCalled(bool $status): void
61 {
62 $this->check_input_called = $status;
63 }
64
65 protected function initForm(): void
66 {
67 if ($this->assignment->getId() > 0) {
68 $this->setTitle($this->lng->txt('tos_form_edit_criterion_head'));
69 } else {
70 $this->setTitle($this->lng->txt('tos_form_attach_criterion_head'));
71 }
72
73 $this->setFormAction($this->formAction);
74
75 $document = new ilNonEditableValueGUI($this->lng->txt('tos_document'));
76 $document->setValue($this->document->getTitle());
77 $this->addItem($document);
78
79 $criteriaSelection = new ilRadioGroupInputGUI($this->lng->txt('tos_form_criterion'), 'criterion');
80 $criteriaSelection->setRequired(true);
81 $criteriaSelection->setValue($this->assignment->getCriterionId());
82
83 $first = true;
84 foreach ($this->criterionTypeFactory->getTypesByIdentMap() as $criterion) {
86 if ($first && !$this->assignment->getId()) {
87 $criteriaSelection->setValue($criterion->getTypeIdent());
88 }
89 $first = false;
90
91 $criterionGui = $criterion->ui($this->lng);
92 if ($this->assignment->getCriterionId() === $criterion->getTypeIdent()) {
93 $criterionGui->appendOption(
94 $criteriaSelection,
95 $this->assignment->getCriterionValue()
96 );
97 } else {
98 $criterionGui->appendOption($criteriaSelection, new ilTermsOfServiceCriterionConfig());
99 }
100 }
101 $this->addItem($criteriaSelection);
102
103 $this->addCommandButton($this->saveCommand, $this->lng->txt('save'));
104 $this->addCommandButton($this->cancelCommand, $this->lng->txt('cancel'));
105 }
106
107 public function hasTranslatedError(): bool
108 {
109 return $this->translatedError !== '';
110 }
111
112 public function getTranslatedError(): string
113 {
115 }
116
117 public function saveObject(): bool
118 {
119 if (!$this->fillObject()) {
120 $this->setValuesByPost();
121 return false;
122 }
123
124 $uniqueAssignmentConstraint = new ilTermsOfServiceDocumentCriterionAssignmentConstraint(
125 $this->criterionTypeFactory,
126 $this->document,
127 new Factory(),
128 $this->lng
129 );
130
131 if (!$uniqueAssignmentConstraint->accepts($this->assignment)) {
132 $this->getItemByPostVar('criterion')->setAlert($this->lng->txt('tos_criterion_assignment_must_be_unique_insert'));
133 if ($this->assignment->getId() > 0) {
134 $this->getItemByPostVar('criterion')->setAlert($this->lng->txt('tos_criterion_assignment_must_be_unique_update'));
135 }
136
137 $this->translatedError = $this->lng->txt('form_input_not_valid');
138 $this->setValuesByPost();
139 return false;
140 }
141
142 if (!$this->assignment->getId()) {
143 $this->document->attachCriterion($this->assignment);
144 }
145 $this->document->save();
146
147 return true;
148 }
149
150 protected function fillObject(): bool
151 {
152 if (!$this->checkInput()) {
153 return false;
154 }
155
156 try {
157 $criterionType = $this->criterionTypeFactory->findByTypeIdent($this->getInput('criterion'));
158 $criterionGui = $criterionType->ui($this->lng);
159
160 $this->assignment->setCriterionId($criterionType->getTypeIdent());
161 $this->assignment->setCriterionValue($criterionGui->getConfigByForm($this));
162
163 if ($this->assignment->getId() > 0) {
164 $this->assignment->setLastModifiedUsrId($this->actor->getId());
165 } else {
166 $this->assignment->setOwnerUsrId($this->actor->getId());
167 }
168 } catch (Exception $e) {
169 $this->getItemByPostVar('criterion')->setAlert($e->getMessage());
170 $this->translatedError = $this->lng->txt('form_input_not_valid');
171 return false;
172 }
173
174 return true;
175 }
176}
Builds data types.
Definition: Factory.php:21
setFormAction(string $a_formaction)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
User class.
This class represents a property form user interface.
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
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.
Class ilTermsOfServiceCriterionConfig.
ilTermsOfServiceDocumentCriterionAssignment $assignment
__construct(ilTermsOfServiceDocument $document, ilTermsOfServiceDocumentCriterionAssignment $assignment, ilTermsOfServiceCriterionTypeFactoryInterface $criterionTypeFactory, ilObjUser $actor, string $formAction='', string $saveCommand='saveDocument', string $cancelCommand='showDocuments')
ilTermsOfServiceCriterionTypeFactoryInterface $criterionTypeFactory
Class ilTermsOfServiceDocument.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc