ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
class.ilAssOrderingElement.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26
35{
37
38 public static $objectInstanceCounter = 0;
40
45 public int $id;
46
54 protected ?int $random_identifier = null;
55
66 protected ?int $solution_identifier = null;
67
71 protected int $indentation = 0;
72
76 protected ?int $position = null;
77 protected ?string $content = null;
78 protected ?string $uploadImageName = null;
79 protected ?string $uploadImageFile = null;
80 protected bool $imageRemovalRequest = false;
81 protected ?string $imagePathWeb = null;
82 protected ?string $image_path_fs = null;
83 protected $imageThumbnailPrefix = null;
84
88 public function __construct(int $id = -1)
89 {
90 $this->id = $id;
91 $this->objectInstanceId = ++self::$objectInstanceCounter;
92 }
93
97 public function __clone()
98 {
99 $this->objectInstanceId = ++self::$objectInstanceCounter;
100 }
101
103 {
104 return clone $this;
105 }
106
107 public function getId(): int
108 {
109 return $this->id;
110 }
111
112 public function setId(int $id): void
113 {
114 $this->id = $id;
115 }
116
117 public function getRandomIdentifier(): ?int
118 {
120 }
121
122 public function setRandomIdentifier(int $random_identifier): void
123 {
124 $this->random_identifier = $random_identifier;
125 }
126
130 public function getSolutionIdentifier(): ?int
131 {
133 }
134
136 {
137 $this->solution_identifier = $solution_identifier;
138 }
139
140 public function setIndentation(int $indentation): void
141 {
142 $this->indentation = $indentation;
143 }
144
145 public function getIndentation(): int
146 {
147 return $this->indentation;
148 }
149
150 public function getPosition(): ?int
151 {
152 return $this->position;
153 }
154
155 public function setPosition(int $position): void
156 {
157 $this->position = $position;
158 }
159
163 public function getContent(): ?string
164 {
165 return $this->content;
166 }
167
168
169 public function setContent($content): void
170 {
171 if (is_array($content)) {
172 $content = array_shift($content);
173 }
174 $this->content = $content;
175 }
176
177 public function getUploadImageFile(): ?string
178 {
180 }
181
186 {
187 if (is_array($uploadImageFile)) {
188 $uploadImageFile = array_shift($uploadImageFile);
189 }
190 $this->uploadImageFile = $uploadImageFile;
191 }
192
196 public function getUploadImageName(): ?string
197 {
199 }
200
205 {
206 if (is_array($uploadImageName)) {
207 $uploadImageName = array_shift($uploadImageName);
208 }
209 $this->uploadImageName = $uploadImageName;
210 }
211
215 public function isImageUploadAvailable(): bool
216 {
217 return $this->getUploadImageFile() !== null
218 && $this->getUploadImageFile() !== '';
219 }
220
224 public function isImageRemovalRequest(): ?bool
225 {
227 }
228
233 {
234 $this->imageRemovalRequest = $imageRemovalRequest;
235 }
236
240 public function getImagePathWeb(): ?string
241 {
242 return $this->imagePathWeb;
243 }
244
248 public function setImagePathWeb($imagePathWeb): void
249 {
250 $this->imagePathWeb = $imagePathWeb;
251 }
252
256 public function getImagePathFs(): ?string
257 {
259 }
260
261 public function setImagePathFs(string $image_path_fs): void
262 {
263 $this->image_path_fs = $image_path_fs;
264 }
265
266 public function getImageThumbnailPrefix()
267 {
269 }
270
272 {
273 $this->imageThumbnailPrefix = $imageThumbnailPrefix;
274 }
275
276 public function isSameElement(ilAssOrderingElement $element): bool
277 {
278 return [
279 $this->getRandomIdentifier(),
280 $this->getSolutionIdentifier(),
281 $this->getPosition(),
282 $this->getIndentation(),
283 ] == [
284 $element->getRandomIdentifier(),
285 $element->getSolutionIdentifier(),
286 $element->getPosition(),
287 $element->getIndentation()
288 ];
289 }
290
291 public function getStorageValue1(int $ordering_type): int|string
292 {
293 switch ($ordering_type) {
296
297 return $this->getPosition();
298
301 default:
302 return $this->getSolutionIdentifier();
303 }
304 }
305
306 public function getStorageValue2(int $ordering_type): int|string
307 {
308 switch ($ordering_type) {
311
312 return $this->getRandomIdentifier() . ':' . $this->getIndentation();
313
316 default:
317 return $this->getPosition() + 1;
318 }
319 }
320
321 public function __toString(): string
322 {
323 return $this->getContent() ?? '';
324 }
325
326 protected function thumbnailFileExists(): bool
327 {
328 if (!$this->getContent()) {
329 return false;
330 }
331
332 return file_exists($this->getThumbnailFilePath());
333 }
334
335 protected function getThumbnailFilePath(): string
336 {
337 return $this->getImagePathFs() . $this->getImageThumbnailPrefix() . $this->getContent();
338 }
339
340 protected function getThumbnailFileUrl(): string
341 {
342 return $this->getImagePathWeb() . $this->getImageThumbnailPrefix() . $this->getContent();
343 }
344
345 protected function imageFileExists(): bool
346 {
347 if (!$this->getContent()) {
348 return false;
349 }
350
351 return file_exists($this->getImageFilePath());
352 }
353
354 protected function getImageFilePath(): string
355 {
356 return $this->getImagePathFs() . $this->getContent();
357 }
358
359 protected function getImageFileUrl(): string
360 {
361 return $this->getImagePathWeb() . $this->getContent();
362 }
363
364 public function getPresentationImageUrl(): string
365 {
366 if ($this->thumbnailFileExists()) {
367 return $this->getThumbnailFileUrl();
368 }
369
370 if ($this->imageFileExists()) {
371 return $this->getImageFileUrl();
372 }
373
374 return '';
375 }
376
377 public function getExportIdent(): string
378 {
379 $ident = [
380 $this->getRandomIdentifier(),
381 $this->getSolutionIdentifier(),
382 $this->getPosition(),
383 $this->getIndentation()
384 ];
385
386 return implode(self::EXPORT_IDENT_PROPERTY_SEPARATOR, $ident);
387 }
388
389 public function isExportIdent(string $ident): bool
390 {
391 if (!strlen($ident)) {
392 return false;
393 }
394
395 $parts = explode(self::EXPORT_IDENT_PROPERTY_SEPARATOR, $ident);
396 return
397 count($parts) == 4
402 }
403
404 public function setExportIdent($ident): void
405 {
406 if ($this->isExportIdent($ident)) {
407 list($randomId, $solutionId, $pos, $indent) = explode(
408 self::EXPORT_IDENT_PROPERTY_SEPARATOR,
409 $ident
410 );
411 $this->setRandomIdentifier((int) $randomId);
412 $this->setSolutionIdentifier((int) $solutionId);
413 $this->setPosition((int) $pos);
414 $this->setIndentation((int) $indent);
415 }
416 }
417
418
419 public function withRandomIdentifier(int $id): self
420 {
421 $clone = clone $this;
422 $clone->random_identifier = $id;
423 return $clone;
424 }
425 public function withSolutionIdentifier(int $id): self
426 {
427 $clone = clone $this;
428 $clone->solution_identifier = $id;
429 return $clone;
430 }
431 public function withPosition(int $position): self
432 {
433 $clone = clone $this;
434 $clone->position = $position;
435 return $clone;
436 }
437 public function withIndentation(int $indentation): self
438 {
439 $clone = clone $this;
440 $clone->indentation = $indentation;
441 return $clone;
442 }
443 public function withContent(string $content): self
444 {
445 $clone = clone $this;
446 $clone->content = $content;
447 return $clone;
448 }
449
450 #[\Override]
451 public function toNormalized(Transformations $transformations, array $context = []): array|float|bool|int|string|null
452 {
453 return [
454 'id' => $transformations->normalize(new Id($this->id, 'ordering')),
455 'random_identifier' => $this->random_identifier,
456 'solution_identifier' => $this->solution_identifier,
457 'position' => $this->position,
458 'indentation' => $this->indentation,
459 'content' => $this->content ? $transformations->normalize(
460 new QuestionImage($this->content, $context['question_id'] ?? null)
461 ) : null,
462 ];
463 }
464
465 #[\Override]
466 public function fromNormalized(array $normalized, Transformations $transformations): self
467 {
468 $clone = $this->withRandomIdentifier($transformations->int($normalized['random_identifier']))
469 ->withSolutionIdentifier($transformations->int($normalized['solution_identifier']))
470 ->withPosition($transformations->int($normalized['position']))
471 ->withIndentation($transformations->int($normalized['indentation']));
472
473 if (is_array($normalized['content'])) {
474 $clone->setContent($transformations->denormalize($normalized['content'], QuestionImage::class)?->getFilename());
475 } else {
476 $clone->setContent((string) $normalized['content']);
477 }
478
479 $clone->setId($transformations->denormalize($normalized['id'], Id::class)->getId());
480
481 return $clone;
482 }
483}
Wrapper object for an object identifier and an optional object type the identifier belongs to.
Definition: Id.php:33
Provides a set of transformations for normalizing and denormalizing values.
denormalize(array|float|bool|int|string|null $normalized, string|object $expected)
Wrapper object for an image file within a question (e.g.
int $position
the correct position in the ordering sequence
fromNormalized(array $normalized, Transformations $transformations)
Restore the object from its normalized representation.
setImagePathFs(string $image_path_fs)
int $indentation
the correct width of indentation for the element
__construct(int $id=-1)
ilAssOrderingElement constructor.
getStorageValue1(int $ordering_type)
setImageRemovalRequest($imageRemovalRequest)
setImageThumbnailPrefix($imageThumbnailPrefix)
setRandomIdentifier(int $random_identifier)
isSameElement(ilAssOrderingElement $element)
int $random_identifier
this identifier is generated randomly it is recycled for known elements
toNormalized(Transformations $transformations, array $context=[])
Convert the internal state of the implementing object into a language-neutral array structure.
setSolutionIdentifier(int $solution_identifier)
getStorageValue2(int $ordering_type)
int $solution_identifier
this identifier is used to identify elements and is stored together with the set position and indenta...