ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilTermsOfServiceCriterionFormGUI.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
9{
11 protected $document;
12
14 protected $assignment;
15
17 protected $formAction;
18
20 protected $actor;
21
23 protected $saveCommand;
24
26 protected $cancelCommand;
27
29 protected $translatedError = '';
30
33
44 public function __construct(
49 string $formAction = '',
50 string $saveCommand = 'saveDocument',
51 string $cancelCommand = 'showDocuments'
52 ) {
53 $this->document = $document;
54 $this->assignment = $assignment;
55 $this->criterionTypeFactory = $criterionTypeFactory;
56 $this->actor = $actor;
57 $this->formAction = $formAction;
58 $this->saveCommand = $saveCommand;
59 $this->cancelCommand = $cancelCommand;
60
61 parent::__construct();
62
63 $this->initForm();
64 }
65
69 public function setCheckInputCalled(bool $status)
70 {
71 $this->check_input_called = $status;
72 }
73
77 protected function initForm()
78 {
79 if ($this->assignment->getId() > 0) {
80 $this->setTitle($this->lng->txt('tos_form_edit_criterion_head'));
81 } else {
82 $this->setTitle($this->lng->txt('tos_form_attach_criterion_head'));
83 }
84
85 $this->setFormAction($this->formAction);
86
87 $document = new \ilNonEditableValueGUI($this->lng->txt('tos_document'));
88 $document->setValue($this->document->getTitle());
89 $this->addItem($document);
90
91 $criteriaSelection = new \ilRadioGroupInputGUI($this->lng->txt('tos_form_criterion'), 'criterion');
92 $criteriaSelection->setRequired(true);
93 $criteriaSelection->setValue($this->assignment->getCriterionId());
94
95 $first = true;
96 foreach ($this->criterionTypeFactory->getTypesByIdentMap() as $criterion) {
98 if (!$this->assignment->getId() && $first) {
99 $criteriaSelection->setValue($criterion->getTypeIdent());
100 }
101 $first = false;
102
103 $criterionGui = $criterion->ui($this->lng);
104 if ($this->assignment->getCriterionId() == $criterion->getTypeIdent()) {
105 $criterionGui->appendOption(
106 $criteriaSelection,
107 $this->assignment->getCriterionValue()
108 );
109 } else {
110 $criterionGui->appendOption($criteriaSelection, new \ilTermsOfServiceCriterionConfig());
111 }
112 }
113 $this->addItem($criteriaSelection);
114
115 $this->addCommandButton($this->saveCommand, $this->lng->txt('save'));
116 $this->addCommandButton($this->cancelCommand, $this->lng->txt('cancel'));
117 }
118
122 public function hasTranslatedError() : bool
123 {
124 return strlen($this->translatedError);
125 }
126
130 public function getTranslatedError() : string
131 {
133 }
134
138 public function saveObject() : bool
139 {
140 if (!$this->fillObject()) {
141 $this->setValuesByPost();
142 return false;
143 }
144
145 $uniqueAssignmentConstraint = new ilTermsOfServiceDocumentCriterionAssignmentConstraint(
146 $this->criterionTypeFactory,
147 $this->document,
148 new \ILIAS\Data\Factory(),
149 $this->lng
150 );
151
152 if (!$uniqueAssignmentConstraint->accepts($this->assignment)) {
153 $this->getItemByPostVar('criterion')->setAlert($this->lng->txt('tos_criterion_assignment_must_be_unique_insert'));
154 if ($this->assignment->getId() > 0) {
155 $this->getItemByPostVar('criterion')->setAlert($this->lng->txt('tos_criterion_assignment_must_be_unique_update'));
156 }
157
158 $this->translatedError = $this->lng->txt('form_input_not_valid');
159 $this->setValuesByPost();
160 return false;
161 }
162
163 if (!$this->assignment->getId()) {
164 $this->document->attachCriterion($this->assignment);
165 }
166 $this->document->save();
167
168 return true;
169 }
170
174 protected function fillObject() : bool
175 {
176 if (!$this->checkInput()) {
177 return false;
178 }
179
180 try {
181 $criterionType = $this->criterionTypeFactory->findByTypeIdent($this->getInput('criterion'));
182 $criterionGui = $criterionType->ui($this->lng);
183
184 $this->assignment->setCriterionId($criterionType->getTypeIdent());
185 $this->assignment->setCriterionValue($criterionGui->getConfigByForm($this));
186
187 if ($this->assignment->getId() > 0) {
188 $this->assignment->setLastModifiedUsrId($this->actor->getId());
189 } else {
190 $this->assignment->setOwnerUsrId($this->actor->getId());
191 }
192 } catch (\Exception $e) {
193 $this->getItemByPostVar('criterion')->setAlert($e->getMessage());
194 $this->translatedError = $this->lng->txt('form_input_not_valid');
195 return false;
196 }
197
198 return true;
199 }
200}
An exception for terminatinating execution or to throw for unit testing.
setFormAction($a_formaction)
Set FormAction.
This class represents a property form user interface.
addItem($a_item)
Add Item (Property, SectionHeader).
addCommandButton($a_cmd, $a_text, $a_id="")
Add Command button.
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
checkInput()
Check Post Input.
setValuesByPost()
Set form values from POST values.
getItemByPostVar($a_post_var)
Get Item by POST variable.
setTitle($a_title)
Set Title.
Class ilTermsOfServiceCriterionConfig.
Class ilTermsOfServiceCriterionFormGUI.
__construct(\ilTermsOfServiceDocument $document, \ilTermsOfServiceDocumentCriterionAssignment $assignment, \ilTermsOfServiceCriterionTypeFactoryInterface $criterionTypeFactory, \ilObjUser $actor, string $formAction='', string $saveCommand='saveDocument', string $cancelCommand='showDocuments')
ilTermsOfServiceCriterionFormGUI constructor.
Class ilTermsOfServiceDocument.
Class BaseForm.