ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ilObjTestSettingsAccess.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 
29 {
30  private const MAX_PASSWORD_LENGTH = 20;
31 
32  public function __construct(
33  int $test_id,
34  protected bool $start_time_enabled = false,
35  protected ?DateTimeImmutable $start_time = null,
36  protected bool $end_time_enabled = false,
37  protected ?DateTimeImmutable $end_time = null,
38  protected bool $password_enabled = false,
39  protected ?string $password = null,
40  protected bool $fixed_participants = false,
41  ) {
42  parent::__construct($test_id);
43  }
44 
45  public function toForm(
49  array $environment = null
50  ): FormInput {
51  $inputs['access_window'] = $this->getInputAccessWindow($lng, $f, $refinery, $environment);
52  $inputs['test_password'] = $this->getInputPassword($lng, $f, $refinery);
53 
54  $inputs['fixed_participants_enabled'] = $f->checkbox(
55  $lng->txt('participants_invitation'),
56  $lng->txt('participants_invitation_description')
57  )->withValue($this->getFixedParticipants());
58  if ($environment['participant_data_exists']) {
59  $inputs['fixed_participants_enabled'] = $inputs['fixed_participants_enabled']
60  ->withDisabled(true);
61  }
62 
63  return $f->section($inputs, $lng->txt('tst_settings_header_execution'));
64  }
65 
66  private function getInputAccessWindow(
70  array $environment = null
71  ): Group {
72  $constraint = $refinery->custom()->constraint(
73  static fn (array $vs) =>
74  $vs['start_time'] === null || $vs['end_time'] === null || $vs['start_time'] < $vs['end_time'],
75  $lng->txt('duration_end_must_not_be_earlier_than_start')
76  );
77 
78  $trafo = $refinery->custom()->transformation(
79  static function (array $vs): array {
80  $vs['start_time_enabled'] = $vs['start_time'] !== null;
81  $vs['end_time_enabled'] = $vs['end_time'] !== null;
82  return $vs;
83  }
84  );
85 
86  return $f->group($this->getSubInputsAccessWindow($lng, $f, $environment))
87  ->withAdditionalTransformation($constraint)
88  ->withAdditionalTransformation($trafo);
89  }
90 
91  private function getSubInputsAccessWindow(
94  array $environment
95  ): array {
96  $sub_inputs_access_window['start_time'] = $f->dateTime(
97  $lng->txt('tst_starting_time'),
98  $lng->txt('tst_starting_time_desc')
99  )->withTimezone($environment['user_time_zone'])
100  ->withFormat($environment['user_date_format'])
101  ->withUseTime(true);
102  if ($this->getStartTime() !== null) {
103  $sub_inputs_access_window['start_time'] = $sub_inputs_access_window['start_time']
104  ->withValue($this->getStartTime()->setTimezone(new DateTimeZone($environment['user_time_zone'])));
105  }
106  if ($environment['participant_data_exists']) {
107  $sub_inputs_access_window['start_time'] = $sub_inputs_access_window['start_time']->withDisabled(true);
108  }
109 
110  $sub_inputs_access_window['end_time'] = $f->dateTime(
111  $lng->txt('tst_ending_time'),
112  $lng->txt('tst_ending_time_desc')
113  )->withTimezone($environment['user_time_zone'])
114  ->withFormat($environment['user_date_format'])
115  ->withUseTime(true);
116  if ($this->getEndTime() !== null) {
117  $sub_inputs_access_window['end_time'] = $sub_inputs_access_window['end_time']
118  ->withValue($this->getEndTime()->setTimezone(new DateTimeZone($environment['user_time_zone'])));
119  }
120 
121  return $sub_inputs_access_window;
122  }
123 
124  private function getInputPassword(
125  \ilLanguage $lng,
128  ): OptionalGroup {
129  $trafo = $refinery->custom()->transformation(
130  static function (?array $vs): array {
131  if ($vs === null) {
132  return [
133  'password_enabled' => false,
134  'password_value' => null
135  ];
136  }
137 
138  $vs['password_enabled'] = true;
139  return $vs;
140  }
141  );
142 
143  $sub_inputs_password['password_value'] = $f->text($lng->txt('tst_password_enter'))
144  ->withRequired(true)->withMaxLength(self::MAX_PASSWORD_LENGTH);
145 
146  $password_input = $f->optionalGroup(
147  $sub_inputs_password,
148  $lng->txt('tst_password'),
149  $lng->txt('tst_password_details')
150  )->withValue(null)
151  ->withAdditionalTransformation($trafo);
152 
153  if (!$this->getPasswordEnabled()) {
154  return $password_input;
155  }
156 
157  return $password_input->withValue(
158  ['password_value' => $this->getPassword()]
159  );
160  }
161 
162  public function toStorage(): array
163  {
164  return [
165  'starting_time_enabled' => ['integer', (int) $this->getStartTimeEnabled()],
166  'starting_time' => ['integer', $this->getStartTime() !== null ? $this->getStartTime()->getTimestamp() : 0],
167  'ending_time_enabled' => ['integer', (int) $this->getEndTimeEnabled()],
168  'ending_time' => ['integer', $this->getEndTime() !== null ? $this->getEndTime()->getTimestamp() : 0],
169  'password_enabled' => ['integer', (int) $this->getPasswordEnabled()],
170  'password' => ['text', $this->getPassword()],
171  'fixed_participants' => ['integer', (int) $this->getFixedParticipants()]
172  ];
173  }
174 
175  public function getStartTimeEnabled(): bool
176  {
177  return $this->start_time_enabled;
178  }
179  public function withStartTimeEnabled(bool $start_time_enabled): self
180  {
181  $clone = clone $this;
182  $clone->start_time_enabled = $start_time_enabled;
183  return $clone;
184  }
185 
186  public function getStartTime(): ?DateTimeImmutable
187  {
188  return $this->start_time;
189  }
190  public function withStartTime(?DateTimeImmutable $start_time): self
191  {
192  $clone = clone $this;
193  $clone->start_time = $start_time;
194  return $clone;
195  }
196 
197  public function getEndTimeEnabled(): bool
198  {
199  return $this->end_time_enabled;
200  }
201  public function withEndTimeEnabled(bool $end_time_enabled): self
202  {
203  $clone = clone $this;
204  $clone->end_time_enabled = $end_time_enabled;
205  return $clone;
206  }
207 
208  public function getEndTime(): ?DateTimeImmutable
209  {
210  return $this->end_time;
211  }
212  public function withEndTime(?DateTimeImmutable $end_time): self
213  {
214  $clone = clone $this;
215  $clone->end_time = $end_time;
216  return $clone;
217  }
218 
219  public function getPasswordEnabled(): bool
220  {
221  return $this->password_enabled;
222  }
223  public function withPasswordEnabled(bool $password_enabled): self
224  {
225  $clone = clone $this;
226  $clone->password_enabled = $password_enabled;
227  return $clone;
228  }
229 
230  public function getPassword(): ?string
231  {
232  return $this->password;
233  }
234  public function withPassword(?string $password): self
235  {
236  $clone = clone $this;
237  $clone->password = $password;
238  return $clone;
239  }
240 
241  public function getFixedParticipants(): bool
242  {
243  return $this->fixed_participants;
244  }
245  public function withFixedParticipants(bool $fixed_participants): self
246  {
247  $clone = clone $this;
248  $clone->fixed_participants = $fixed_participants;
249  return $clone;
250  }
251 }
This is what a factory for input fields looks like.
Definition: Factory.php:28
__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 bool $fixed_participants=false,)
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...
withPasswordEnabled(bool $password_enabled)
This describes optional group inputs.
toForm(\ilLanguage $lng, FieldFactory $f, Refinery $refinery, array $environment=null)
getInputPassword(\ilLanguage $lng, FieldFactory $f, Refinery $refinery)
getInputAccessWindow(\ilLanguage $lng, FieldFactory $f, Refinery $refinery, array $environment=null)
withEndTime(?DateTimeImmutable $end_time)
withEndTimeEnabled(bool $end_time_enabled)
__construct(VocabulariesInterface $vocabularies)
$lng
getSubInputsAccessWindow(\ilLanguage $lng, FieldFactory $f, array $environment)
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:58
withStartTime(?DateTimeImmutable $start_time)
This describes inputs that can be used in forms.
Definition: FormInput.php:31
withStartTimeEnabled(bool $start_time_enabled)
withFixedParticipants(bool $fixed_participants)
Refinery Factory $refinery