ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
SettingsFinishing.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
29use ILIAS\Refinery\Factory as Refinery;
30
32{
33 public function __construct(
34 protected bool $show_answer_overview = false,
35 protected bool $concluding_remarks_enabled = false,
36 protected ?int $concluding_remarks_page_id = null,
37 protected RedirectionModes $redirection_mode = RedirectionModes::NONE,
38 protected ?string $redirection_url = null,
39 ) {
41 }
42
43 public function toForm(
45 FieldFactory $f,
47 ?array $environment = null
48 ): FormInput {
49 $inputs['show_answer_overview'] = $f->checkbox(
50 $lng->txt('enable_examview'),
51 $lng->txt('enable_examview_desc')
53
54 $inputs['show_concluding_remarks'] = $f->checkbox(
55 $lng->txt('final_statement'),
56 $lng->txt('final_statement_show_desc')
57 )->withValue((bool) $this->getConcludingRemarksEnabled());
58
59 $inputs['redirect_after_finish'] = $this->getRedirectionInputs($lng, $f, $refinery);
60
61 return $f->section($inputs, $lng->txt('tst_final_information'));
62 }
63
64 private function getRedirectionInputs(
66 FieldFactory $f,
68 ): OptionalGroup {
69 $redirection_trafo = $refinery->custom()->transformation(
70 static function (?array $v): array {
71 if ($v === null) {
72 return [
73 'redirect_mode' => RedirectionModes::NONE,
74 'redirect_url' => ''
75 ];
76 }
77
78 return [
79 'redirect_mode' => RedirectionModes::tryFrom($v['redirect_mode'])
80 ?? RedirectionModes::NONE,
81 'redirect_url' => $v['redirect_url']
82 ];
83 }
84 );
85
86 $sub_inputs_redirect = [
87 'redirect_mode' => $f
88 ->radio($lng->txt('redirect_after_finishing_rule'))
89 ->withOption(
90 (string) RedirectionModes::ALWAYS->value,
91 $lng->txt('redirect_always')
92 )->withOption(
94 $lng->txt('redirect_always_to_logout')
95 )->withOption(
96 (string) RedirectionModes::IF_KIOSK_ACTIVATED->value,
97 $lng->txt('redirect_in_kiosk_mode')
98 )->withRequired(true)
99 ->withAdditionalTransformation($refinery->kindlyTo()->int()),
100 'redirect_url' => $f
101 ->text($lng->txt('redirection_url'))
102 ->withAdditionalTransformation($refinery->string()->hasMaxLength(4000))
104 $refinery->custom()->constraint(
105 static function ($v) use ($refinery): bool {
106 try {
107 return $v === '' || $refinery->to()->data('uri')->transform($v);
108 } catch (Throwable) {
109 return false;
110 }
111 },
112 $lng->txt('redirect_url_invalid')
113 )
114 )
115 ];
116
117 $redirection_input = $f
118 ->optionalGroup(
119 $sub_inputs_redirect,
120 $lng->txt('redirect_after_finishing_tst'),
121 $lng->txt('redirect_after_finishing_tst_desc')
122 )
123 ->withValue(null)
124 ->withAdditionalTransformation($redirection_trafo)
125 ->withAdditionalTransformation(
126 $refinery->custom()->constraint(
127 static function (array $v): bool {
128 return in_array(
129 $v['redirect_mode'],
130 [RedirectionModes::NONE, RedirectionModes::ALWAYS_TO_LOGOUT],
131 true
132 ) || $v['redirect_url'] !== '';
133 },
134 static function (\Closure $txt, array $value): string {
135 return sprintf(
136 $txt('redirect_url_required_for_rule'),
137 $value['redirect_mode'] === RedirectionModes::ALWAYS
138 ? $txt('redirect_always')
139 : $txt('redirect_in_kiosk_mode')
140 );
141 }
142 )
143 );
144
145 if ($this->getRedirectionMode() === RedirectionModes::NONE) {
146 return $redirection_input;
147 }
148
149 return $redirection_input->withValue(
150 [
151 'redirect_mode' => $this->getRedirectionMode()->value,
152 'redirect_url' => $this->getRedirectionUrl()
153 ]
154 );
155 }
156
157 public function toStorage(): array
158 {
159 return [
160 'enable_examview' => ['integer', (int) $this->getShowAnswerOverview()],
161 'showfinalstatement' => ['integer', (int) $this->getConcludingRemarksEnabled()],
162 'concluding_remarks_page_id' => ['integer', $this->getConcludingRemarksPageId()],
163 'redirection_mode' => ['integer', $this->getRedirectionMode()->value],
164 'redirection_url' => ['text', $this->getRedirectionUrl()],
165 ];
166 }
167
168 public function toLog(AdditionalInformationGenerator $additional_info): array
169 {
170 $log_array = [
171 AdditionalInformationGenerator::KEY_TEST_ANSWER_OVERVIEW_ENABLED => $additional_info
172 ->getEnabledDisabledTagForBool($this->getShowAnswerOverview()),
173 AdditionalInformationGenerator::KEY_TEST_CONCLUDING_REMARKS_ENABLED => $additional_info
174 ->getEnabledDisabledTagForBool($this->getConcludingRemarksEnabled()),
175 AdditionalInformationGenerator::KEY_TEST_REDIRECT_URL => $this->getRedirectionUrl(),
176
177 ];
178
179 switch ($this->getRedirectionMode()) {
180 case RedirectionModes::NONE:
181 $log_array[AdditionalInformationGenerator::KEY_TEST_REDIRECT_MODE] = $additional_info
182 ->getNoneTag();
183 break;
185 $log_array[AdditionalInformationGenerator::KEY_TEST_REDIRECT_MODE] = $additional_info
186 ->getTagForLangVar('redirect_always');
187 break;
188 case RedirectionModes::IF_KIOSK_ACTIVATED:
189 $log_array[AdditionalInformationGenerator::KEY_TEST_REDIRECT_MODE] = $additional_info
190 ->getTagForLangVar('redirect_in_kiosk_mode');
191 break;
192 }
193
194 return $log_array;
195 }
196
197 public function getShowAnswerOverview(): bool
198 {
199 return $this->show_answer_overview;
200 }
201
202 public function withShowAnswerOverview(bool $show_answer_overview): self
203 {
204 $clone = clone $this;
205 $clone->show_answer_overview = $show_answer_overview;
206 return $clone;
207 }
208
209 public function getConcludingRemarksEnabled(): bool
210 {
211 return $this->concluding_remarks_enabled;
212 }
213
214 public function withConcludingRemarksEnabled(bool $concluding_remarks_enabled): self
215 {
216 $clone = clone $this;
217 $clone->concluding_remarks_enabled = $concluding_remarks_enabled;
218 return $clone;
219 }
220
221 public function getConcludingRemarksPageId(): ?int
222 {
223 return $this->concluding_remarks_page_id;
224 }
225
226 public function withConcludingRemarksPageId(?int $concluding_remarks_page_id): self
227 {
228 $clone = clone $this;
229 $clone->concluding_remarks_page_id = $concluding_remarks_page_id;
230 return $clone;
231 }
232
234 {
235 return $this->redirection_mode;
236 }
237
238 public function withRedirectionMode(RedirectionModes $redirection_mode): self
239 {
240 $clone = clone $this;
241 $clone->redirection_mode = $redirection_mode;
242 return $clone;
243 }
244
245 public function getRedirectionUrl(): ?string
246 {
247 return $this->redirection_url;
248 }
249
250 public function withRedirectionUrl(?string $redirection_url): self
251 {
252 $clone = clone $this;
253 $clone->redirection_url = $redirection_url;
254 return $clone;
255 }
256
257 public function toExport(): array
258 {
259 return [
260 'enable_examview' => $this->getShowAnswerOverview(),
261 'showfinalstatement' => $this->getConcludingRemarksEnabled(),
262 'concluding_remarks_page_id' => $this->getConcludingRemarksPageId(),
263 'redirection_mode' => $this->getRedirectionMode()->value,
264 'redirection_url' => $this->getRedirectionUrl()
265 ];
266 }
267
268 public static function fromExport(array $data): static
269 {
270 return new self(
271 (bool) $data['enable_examview'],
272 (bool) $data['showfinalstatement'],
273 $data['concluding_remarks_page_id'],
274 RedirectionModes::from($data['redirection_mode']),
275 $data['redirection_url']
276 );
277 }
278}
const ALWAYS
Builds data types.
Definition: Factory.php:36
withConcludingRemarksPageId(?int $concluding_remarks_page_id)
static fromExport(array $data)
Creates an instance of the object from an array.
__construct(protected bool $show_answer_overview=false, protected bool $concluding_remarks_enabled=false, protected ?int $concluding_remarks_page_id=null, protected RedirectionModes $redirection_mode=RedirectionModes::NONE, protected ?string $redirection_url=null,)
getRedirectionInputs(\ilLanguage $lng, FieldFactory $f, Refinery $refinery)
toExport()
Transform the object into a simple, associative array.
withRedirectionMode(RedirectionModes $redirection_mode)
toLog(AdditionalInformationGenerator $additional_info)
toForm(\ilLanguage $lng, FieldFactory $f, Refinery $refinery, ?array $environment=null)
withConcludingRemarksEnabled(bool $concluding_remarks_enabled)
language handling
This interface allows an object to define its own transformation into a language-neutral,...
Definition: Exportable.php:40
This describes inputs that can be used in forms.
Definition: FormInput.php:33
This is what a factory for input fields looks like.
Definition: Factory.php:31
This describes optional group inputs.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
withAdditionalTransformation(Transformation $trafo)
@inheritDoc
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
global $lng
Definition: privfeed.php:31
if(!file_exists('../ilias.ini.php'))