ILIAS  trunk Revision v11.0_alpha-2662-g519ff7d528f
AvailabilityPeriod.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 
33 class AvailabilityPeriod implements Property
34 {
35  public function __construct(
36  private ?int $object_reference_id = null,
37  private ?bool $availability_period_enabled = null,
38  private ?\DateTimeImmutable $time_limit_start = null,
39  private ?\DateTimeImmutable $time_limit_end = null,
40  private ?bool $visible_when_disabled = false
41  ) {
42  }
43 
44  public function getObjectReferenceId(): ?int
45  {
46  return $this->object_reference_id;
47  }
48 
49  public function getAvailabilityPeriodEnabled(): bool
50  {
51  return $this->availability_period_enabled === true;
52  }
53 
55  {
56  return $this->time_limit_start;
57  }
58 
60  {
61  return $this->time_limit_end;
62  }
63 
64  public function getVisibleWhenDisabled(): bool
65  {
66  return $this->visible_when_disabled;
67  }
68 
69  public function objectCurrentlyEnabled(): bool
70  {
71  if ($this->availability_period_enabled === false) {
72  return true;
73  }
74 
75  $timing_start_utc = $this->timing_start->setTimezone(new \DateTimeZone('UTC'));
76  $timing_end_utc = $this->timing_end->setTimezone(new \DateTimeZone('UTC'));
77  $now_utc = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));
78 
79  if ($timing_start_utc < $now_utc && $now_utc < $timing_end_utc) {
80  return true;
81  }
82 
83  return false;
84  }
85 
86  public function withObjectReferenceId(int $object_reference_id): self
87  {
88  $clone = clone $this;
89  $clone->object_reference_id = $object_reference_id;
90  return $clone;
91  }
92 
93  public function toForm(
94  \ilLanguage $language,
95  FieldFactory $field_factory,
97  ?array $environment = null
98  ): FormInput {
100  $refinery,
101  $language
102  );
103  $trafo = $this->getTransformationForActivationLimitedOptionalGroup($refinery);
105  new \DateTimeZone($environment['user_time_zone'])
106  );
107 
108  $inputs['time_limit_start'] = $field_factory->dateTime($language->txt('rep_activation_limited_start'))
109  ->withTimezone($environment['user_time_zone'])
110  ->withFormat($environment['user_date_format'])
111  ->withUseTime(true);
112  $inputs['time_limit_end'] = $field_factory->dateTime($language->txt('rep_activation_limited_end'))
113  ->withTimezone($environment['user_time_zone'])
114  ->withFormat($environment['user_date_format'])
115  ->withUseTime(true);
116  $inputs['visible_when_disabled'] = $field_factory->checkbox(
117  $language->txt('activation_visible_when_disabled'),
118  $language->txt('activation_visible_when_disabled_info')
119  );
120 
121  return $field_factory->optionalGroup(
122  $inputs,
123  $language->txt('rep_time_based_availability'),
124  $language->txt('rep_time_based_availability_info')
125  )->withAdditionalTransformation($constraint)
126  ->withAdditionalTransformation($trafo)
127  ->withValue($value);
128  }
129 
131  {
132  return $refinery->custom()->transformation(
133  function (?array $vs): self {
134  if ($vs === null
135  || $vs['time_limit_start'] === null
136  && $vs['time_limit_end'] === null) {
137  return new self($this->getObjectReferenceId());
138  }
139 
140  return new self(
141  $this->getObjectReferenceId(),
142  true,
143  $vs['time_limit_start']?->setTimezone(new \DateTimeZone('UTC')),
144  $vs['time_limit_end']?->setTimezone(new \DateTimeZone('UTC')),
145  $vs['visible_when_disabled']
146  );
147  }
148  );
149  }
150 
153  \ilLanguage $language
154  ): Constraint {
155  return $refinery->custom()->constraint(
156  function (?array $vs): bool {
157  if ($vs === null
158  || $vs['time_limit_start'] === null
159  || $vs['time_limit_end'] === null) {
160  return true;
161  }
162 
163  if ($vs['time_limit_start'] > $vs['time_limit_end']) {
164  return false;
165  }
166 
167  return true;
168  },
169  $language->txt('duration_end_must_not_be_earlier_than_start')
170  );
171  }
172 
173 
174  private function getValueForActivationLimitedOptionalGroup(\DateTimeZone $timezone): ?array
175  {
176  $value = null;
177  if ($this->getAvailabilityPeriodEnabled()) {
178  $value = [
179  'time_limit_start' => $this->getAvailabilityPeriodStart()?->setTimezone($timezone)->format('Y-m-d H:i') ?? '',
180  'time_limit_end' => $this->getAvailabilityPeriodEnd()?->setTimezone($timezone)->format('Y-m-d H:i') ?? '',
181  'visible_when_disabled' => $this->getVisibleWhenDisabled()
182  ];
183  }
184  return $value;
185  }
186 }
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...
toForm(\ilLanguage $language, FieldFactory $field_factory, Refinery $refinery, ?array $environment=null)
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
__construct(private ?int $object_reference_id=null, private ?bool $availability_period_enabled=null, private ?\DateTimeImmutable $time_limit_start=null, private ?\DateTimeImmutable $time_limit_end=null, private ?bool $visible_when_disabled=false)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
A transformation is a function from one datatype to another.
This describes inputs that can be used in forms.
Definition: FormInput.php:32