ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
SettingsAccess.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 private const MAX_PASSWORD_LENGTH = 20;
34
35 public function __construct(
36 int $test_id,
37 protected bool $start_time_enabled = false,
38 protected ?\DateTimeImmutable $start_time = null,
39 protected bool $end_time_enabled = false,
40 protected ?\DateTimeImmutable $end_time = null,
41 protected bool $password_enabled = false,
42 protected ?string $password = null,
43 protected ?string $ip_range_from = null,
44 protected ?string $ip_range_to = null,
45 protected bool $fixed_participants = false
46 ) {
48 }
49
50 public function toForm(
52 FieldFactory $f,
54 ?array $environment = null
55 ): FormInput {
56 $inputs['access_window'] = $this->getInputAccessWindow($lng, $f, $refinery, $environment);
57 $inputs['test_password'] = $this->getInputPassword($lng, $f, $refinery);
58 $inputs['ip_range'] = $this->getInputIpRange($lng, $f, $refinery);
59
60 $inputs['fixed_participants_enabled'] = $f->checkbox(
61 $lng->txt('participants_invitation'),
62 $lng->txt('participants_invitation_description')
63 )->withValue($this->getFixedParticipants());
64 if ($environment['participant_data_exists']) {
65 $inputs['fixed_participants_enabled'] = $inputs['fixed_participants_enabled']
66 ->withDisabled(true);
67 }
68
69 return $f->section($inputs, $lng->txt('tst_settings_header_execution'));
70 }
71
72 private function getInputAccessWindow(
74 FieldFactory $f,
76 ?array $environment = null
77 ): Group {
78 $constraint = $refinery->custom()->constraint(
79 static fn(array $vs) =>
80 $vs['start_time'] === null || $vs['end_time'] === null || $vs['start_time'] < $vs['end_time'],
81 $lng->txt('duration_end_must_not_be_earlier_than_start')
82 );
83
84 $trafo = $refinery->custom()->transformation(
85 static function (array $vs): array {
86 $vs['start_time_enabled'] = $vs['start_time'] !== null;
87 $vs['end_time_enabled'] = $vs['end_time'] !== null;
88 return $vs;
89 }
90 );
91
92 return $f->group($this->getSubInputsAccessWindow($lng, $f, $environment))
93 ->withAdditionalTransformation($constraint)
94 ->withAdditionalTransformation($trafo);
95 }
96
97 private function getSubInputsAccessWindow(
99 FieldFactory $f,
100 array $environment
101 ): array {
102 $sub_inputs_access_window['start_time'] = $f->dateTime(
103 $lng->txt('tst_starting_time'),
104 $lng->txt('tst_starting_time_desc')
105 )->withTimezone($environment['user_time_zone'])
106 ->withFormat($environment['user_date_format'])
107 ->withUseTime(true);
108 if ($this->getStartTime() !== null) {
109 $sub_inputs_access_window['start_time'] = $sub_inputs_access_window['start_time']
110 ->withValue($this->getStartTime()->setTimezone(new \DateTimeZone($environment['user_time_zone'])));
111 }
112 if ($environment['participant_data_exists']) {
113 $sub_inputs_access_window['start_time'] = $sub_inputs_access_window['start_time']->withDisabled(true);
114 }
115
116 $sub_inputs_access_window['end_time'] = $f->dateTime(
117 $lng->txt('tst_ending_time'),
118 $lng->txt('tst_ending_time_desc')
119 )->withTimezone($environment['user_time_zone'])
120 ->withFormat($environment['user_date_format'])
121 ->withUseTime(true);
122 if ($this->getEndTime() !== null) {
123 $sub_inputs_access_window['end_time'] = $sub_inputs_access_window['end_time']
124 ->withValue($this->getEndTime()->setTimezone(new \DateTimeZone($environment['user_time_zone'])));
125 }
126
127 return $sub_inputs_access_window;
128 }
129
130 private function getInputPassword(
132 FieldFactory $f,
134 ): OptionalGroup {
135 $trafo = $refinery->custom()->transformation(
136 static function (?array $vs): array {
137 if ($vs === null) {
138 return [
139 'password_enabled' => false,
140 'password_value' => null
141 ];
142 }
143
144 $vs['password_enabled'] = true;
145 return $vs;
146 }
147 );
148
149 $sub_inputs_password['password_value'] = $f->text($lng->txt('tst_password_enter'))
150 ->withRequired(true)->withMaxLength(self::MAX_PASSWORD_LENGTH);
151
152 $password_input = $f->optionalGroup(
153 $sub_inputs_password,
154 $lng->txt('tst_password'),
155 $lng->txt('tst_password_details')
156 )->withValue(null)
157 ->withAdditionalTransformation($trafo);
158
159 if (!$this->getPasswordEnabled()) {
160 return $password_input;
161 }
162
163 return $password_input->withValue(
164 ['password_value' => $this->getPassword()]
165 );
166 }
167
168 private function getInputIpRange(
170 FieldFactory $f,
172 ): FormInput {
173 $validate_ip = $refinery->custom()->constraint(
174 static function (?string $v): bool {
175 if ($v === null) {
176 return true;
177 }
178 return filter_var($v, FILTER_VALIDATE_IP) !== false;
179 },
180 $lng->txt('invalid_ip')
181 );
182
183 $validate_order = $refinery->custom()->constraint(
184 function (?array $vs): bool {
185 if ($vs === null) {
186 return true;
187 }
188 return $this->checkIpRangeValidity(
189 $vs['ip_range_from'],
190 $vs['ip_range_to']
191 );
192 },
193 sprintf($lng->txt('not_greater_than'), $lng->txt('max_ip_label'), $lng->txt('min_ip_label'))
194 );
195 $trafo = $refinery->custom()->transformation(
196 static function (?array $vs): array {
197 if ($vs === null) {
198 $vs = [
199 'ip_range_from' => null,
200 'ip_range_to' => null
201 ];
202 }
203 return $vs;
204 }
205 );
206
207 $get_ip_range = $f->optionalGroup(
208 [
209 'ip_range_from' => $f->text($lng->txt('min_ip_label'))
210 ->withAdditionalTransformation($validate_ip),
211 'ip_range_to' => $f->text($lng->txt('max_ip_label'))
212 ->withAdditionalTransformation($validate_ip)
213 ],
214 $lng->txt('ip_range_label'),
215 $lng->txt('ip_range_info')
216 )->withValue(null);
217
218 if ($this->isIpRangeEnabled()) {
219 $get_ip_range = $get_ip_range->withValue(
220 [
221 'ip_range_from' => $this->getIpRangeFrom(),
222 'ip_range_to' => $this->getIpRangeTo()
223 ]
224 );
225 }
226
227 return $get_ip_range->withAdditionalTransformation($validate_order)
228 ->withAdditionalTransformation($trafo);
229 }
230
231 private function checkIpRangeValidity(string $start, string $end): bool
232 {
233 if (filter_var($start, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false
234 && filter_var($end, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
235 return ip2long($start) <= ip2long($end);
236 }
237
238 if (filter_var($start, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false
239 && filter_var($end, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
240 return bin2hex(inet_pton($start)) <= bin2hex(inet_pton($end));
241 }
242 return false;
243 }
244
245 public function toStorage(): array
246 {
247 return [
248 'starting_time_enabled' => ['integer', (int) $this->getStartTimeEnabled()],
249 'starting_time' => ['integer', $this->getStartTime() !== null ? $this->getStartTime()->getTimestamp() : 0],
250 'ending_time_enabled' => ['integer', (int) $this->getEndTimeEnabled()],
251 'ending_time' => ['integer', $this->getEndTime() !== null ? $this->getEndTime()->getTimestamp() : 0],
252 'password_enabled' => ['integer', (int) $this->getPasswordEnabled()],
253 'password' => ['text', $this->getPassword()],
254 'ip_range_from' => ['text', $this->getIpRangeFrom()],
255 'ip_range_to' => ['text', $this->getIpRangeTo()],
256 'fixed_participants' => ['integer', (int) $this->getFixedParticipants()]
257 ];
258 }
259
260 public function toLog(AdditionalInformationGenerator $additional_info): array
261 {
262 $starting_time = $additional_info->getNoneTag();
263 if (($st_immutable = $this->getStartTime()) instanceof \DateTimeImmutable) {
264 $starting_time = $st_immutable->format(AdditionalInformationGenerator::DATE_STORAGE_FORMAT);
265 }
266
267 $end_time = $additional_info->getNoneTag();
268 if (($et_immutable = $this->getEndTime()) instanceof \DateTimeImmutable) {
269 $end_time = $et_immutable->format(AdditionalInformationGenerator::DATE_STORAGE_FORMAT);
270 }
271
272 return [
273 AdditionalInformationGenerator::KEY_TEST_START_TIME => $starting_time,
274 AdditionalInformationGenerator::KEY_TEST_END_TIME => $end_time,
275 AdditionalInformationGenerator::KEY_TEST_PASSWORD => $this->getPassword() ?? $additional_info->getNoneTag(),
276 AdditionalInformationGenerator::KEY_TEST_IP_RANGE => $this->isIpRangeEnabled()
277 ? $this->getIpRangeFrom() . ' - ' . $this->getIpRangeTo()
278 : $additional_info->getEnabledDisabledTagForBool(false),
279 AdditionalInformationGenerator::KEY_TEST_FIXED_PARTICIPANTS => $additional_info
280 ->getEnabledDisabledTagForBool($this->getFixedParticipants())
281 ];
282 }
283
284 public function getStartTimeEnabled(): bool
285 {
286 return $this->start_time_enabled;
287 }
288 public function withStartTimeEnabled(bool $start_time_enabled): self
289 {
290 $clone = clone $this;
291 $clone->start_time_enabled = $start_time_enabled;
292 return $clone;
293 }
294
295 public function getStartTime(): ?\DateTimeImmutable
296 {
297 return $this->start_time;
298 }
299 public function withStartTime(?\DateTimeImmutable $start_time): self
300 {
301 $clone = clone $this;
302 $clone->start_time = $start_time;
303 return $clone;
304 }
305
306 public function getEndTimeEnabled(): bool
307 {
308 return $this->end_time_enabled;
309 }
310 public function withEndTimeEnabled(bool $end_time_enabled): self
311 {
312 $clone = clone $this;
313 $clone->end_time_enabled = $end_time_enabled;
314 return $clone;
315 }
316
317 public function getEndTime(): ?\DateTimeImmutable
318 {
319 return $this->end_time;
320 }
321 public function withEndTime(?\DateTimeImmutable $end_time): self
322 {
323 $clone = clone $this;
324 $clone->end_time = $end_time;
325 return $clone;
326 }
327
328 public function getPasswordEnabled(): bool
329 {
330 return $this->password_enabled;
331 }
332 public function withPasswordEnabled(bool $password_enabled): self
333 {
334 $clone = clone $this;
335 $clone->password_enabled = $password_enabled;
336 return $clone;
337 }
338
339 public function getPassword(): ?string
340 {
341 return $this->password;
342 }
343 public function withPassword(?string $password): self
344 {
345 $clone = clone $this;
346 $clone->password = $password;
347 return $clone;
348 }
349
350 public function getIpRangeFrom(): ?string
351 {
352 return $this->ip_range_from;
353 }
354 public function withIpRangeFrom(?string $ip_range_from): self
355 {
356 $clone = clone $this;
357 $clone->ip_range_from = $ip_range_from;
358 return $clone;
359 }
360
361 public function getIpRangeTo(): ?string
362 {
363 return $this->ip_range_to;
364 }
365 public function withIpRangeTo(?string $ip_range_to): self
366 {
367 $clone = clone $this;
368 $clone->ip_range_to = $ip_range_to;
369 return $clone;
370 }
371
372 public function isIpRangeEnabled(): ?bool
373 {
374 return $this->ip_range_from !== null && $this->ip_range_to !== null;
375 }
376
377 public function getFixedParticipants(): bool
378 {
379 return $this->fixed_participants;
380 }
381 public function withFixedParticipants(bool $fixed_participants): self
382 {
383 $clone = clone $this;
384 $clone->fixed_participants = $fixed_participants;
385 return $clone;
386 }
387}
Builds data types.
Definition: Factory.php:36
getSubInputsAccessWindow(\ilLanguage $lng, FieldFactory $f, array $environment)
getInputIpRange(\ilLanguage $lng, FieldFactory $f, Refinery $refinery)
__construct(int $test_id, protected bool $start_time_enabled=false, protected ?\DateTimeImmutable $start_time=null, protected bool $end_time_enabled=false, protected ?\DateTimeImmutable $end_time=null, protected bool $password_enabled=false, protected ?string $password=null, protected ?string $ip_range_from=null, protected ?string $ip_range_to=null, protected bool $fixed_participants=false)
getInputAccessWindow(\ilLanguage $lng, FieldFactory $f, Refinery $refinery, ?array $environment=null)
toLog(AdditionalInformationGenerator $additional_info)
withStartTime(?\DateTimeImmutable $start_time)
toForm(\ilLanguage $lng, FieldFactory $f, Refinery $refinery, ?array $environment=null)
checkIpRangeValidity(string $start, string $end)
getInputPassword(\ilLanguage $lng, FieldFactory $f, Refinery $refinery)
withEndTime(?\DateTimeImmutable $end_time)
return true
language handling
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
Describes the monoid operation of grouping form inputs.
Definition: Group.php:32
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...
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'))