ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAssOrderingFormValuesObjectsConverter.php
Go to the documentation of this file.
1<?php
2
21
29{
30 public const INDENTATIONS_POSTVAR_SUFFIX = '_ordering';
31 public const INDENTATIONS_POSTVAR_SUFFIX_JS = '__default';
32
33 public const CONTEXT_MAINTAIN_ELEMENT_TEXT = 'maintainItemText';
34 public const CONTEXT_MAINTAIN_ELEMENT_IMAGE = 'maintainItemImage';
35 public const CONTEXT_MAINTAIN_HIERARCHY = 'maintainHierarchy';
36
40 protected $context = null;
41
45 protected $postVar = null;
46
50 protected $imageRemovalCommand = null;
51
55 protected $imageUrlPath;
56
60 protected $imageFsPath;
61
66
68
69 public function __construct()
70 {
71 $local_dic = QuestionPoolDIC::dic();
72 $this->request_data_collector = $local_dic['request_data_collector'];
73 }
74
78 public function getContext(): ?string
79 {
80 return $this->context;
81 }
82
86 public function setContext($context): void
87 {
88 $this->context = $context;
89 }
90
94 public function getPostVar(): ?string
95 {
96 return $this->postVar;
97 }
98
102 public function setPostVar($postVar): void
103 {
104 $this->postVar = $postVar;
105 }
106
107 public function getImageRemovalCommand(): ?string
108 {
110 }
111
113 {
114 $this->imageRemovalCommand = $imageRemovalCommand;
115 }
116
117 public function getImageUrlPath(): string
118 {
119 return $this->imageUrlPath;
120 }
121
125 public function setImageUrlPath($imageUrlPath): void
126 {
127 $this->imageUrlPath = $imageUrlPath;
128 }
129
133 public function getImageFsPath(): string
134 {
135 return $this->imageFsPath;
136 }
137
141 public function setImageFsPath($imageFsPath): void
142 {
143 $this->imageFsPath = $imageFsPath;
144 }
145
149 public function getThumbnailPrefix(): string
150 {
152 }
153
158 {
159 $this->thumbnailPrefix = $thumbnailPrefix;
160 }
161
162 public function getIndentationsPostVar(): string
163 {
164 $postVar = $this->getPostVar();
167
168 return $postVar;
169 }
170
171 protected function needsConvertToValues($elements_or_values): bool
172 {
173 if (!count($elements_or_values)) {
174 return false;
175 }
176
177 return (current($elements_or_values) instanceof ilAssOrderingElement);
178 }
179
180 public function manipulateFormInputValues(array $input_values): array
181 {
182 if ($this->needsConvertToValues($input_values)) {
183 $input_values = $this->collectValuesFromElements($input_values);
184 }
185
186 return $input_values;
187 }
188
189 protected function collectValuesFromElements(array $elements): array
190 {
191 $values = [];
192
193 foreach ($elements as $identifier => $ordering_element) {
194 switch ($this->getContext()) {
196
197 $values[$identifier] = $this->getTextContentValueFromObject($ordering_element);
198 break;
199
201
202 $values[$identifier] = $this->getImageContentValueFromObject($ordering_element);
203 break;
204
206
207 $values[$identifier] = $this->getStructValueFromObject($ordering_element);
208 break;
209
210 default:
211 throw new ilFormException('unsupported context: ' . $this->getContext());
212 }
213 }
214
215 return $values;
216 }
217
218 protected function getTextContentValueFromObject(ilAssOrderingElement $element): ?string
219 {
220 return $element->getContent();
221 }
222
223 protected function getImageContentValueFromObject(ilAssOrderingElement $element): array
224 {
225 $element->setImagePathWeb($this->getImageUrlPath());
226 $element->setImagePathFs($this->getImageFsPath());
228
229 return [
230 'title' => $element->getContent(),
231 'src' => $element->getPresentationImageUrl()
232 ];
233 }
234
235 protected function getStructValueFromObject(ilAssOrderingElement $element): array
236 {
237 return [
238 'answer_id' => $element->getId(),
239 'random_id' => $element->getRandomIdentifier(),
240 'content' => (string) $element->getContent(),
241 'ordering_position' => $element->getPosition(),
242 'ordering_indentation' => $element->getIndentation()
243 ];
244 }
245
246 protected function needsConvertToElements($values_or_elements): bool
247 {
248 if (!count($values_or_elements)) {
249 return false;
250 }
251
252 return !(current($values_or_elements) instanceof ilAssOrderingElement);
253 }
254
255 public function manipulateFormSubmitValues(array $submit_values): array
256 {
257 if ($this->needsConvertToElements($submit_values)) {
258 $submit_values = $this->constructElementsFromValues($submit_values);
259 }
260
261 return $submit_values;
262 }
263
264 public function constructElementsFromValues(array $values): array
265 {
266 $elements = [];
267
268 $content = $values;
269 if (array_key_exists('content', $values)) {
270 $content = $values['content'];
271 }
272
273 $position = [];
274 if (array_key_exists('position', $values)) {
275 $position = $values['position'];
276 }
277
278 $indentation = [];
279 if (array_key_exists('indentation', $values)) {
280 $indentation = $values['indentation'];
281 }
282
283 $counter = 0;
284 foreach ($content as $identifier => $value) {
285 $element = new ilAssOrderingElement();
286
287 $element->setRandomIdentifier((int) $identifier);
288 $element->setPosition((int) ($position[$identifier] ?? $counter));
289 $element->setContent($value);
290 $element->setIndentation((int) ($indentation[$identifier] ?? 0));
291
292 if ($this->getContext() === self::CONTEXT_MAINTAIN_ELEMENT_IMAGE) {
293 $element->setUploadImageName($this->fetchSubmittedImageFilename($identifier));
294 $element->setUploadImageFile($this->fetchSubmittedUploadFilename($identifier));
295
296 $element->setImageRemovalRequest($this->wasImageRemovalRequested($identifier));
297 }
298
299 $elements[$identifier] = $element;
300 $counter++;
301 }
302
303 return $elements;
304 }
305
306 protected function fetchSubmittedImageFilename($identifier)
307 {
308 $fileUpload = $this->fetchElementFileUpload($identifier);
309 return $this->fetchSubmittedFileUploadProperty($fileUpload, 'name');
310 }
311
312 protected function fetchSubmittedUploadFilename($identifier)
313 {
314 $fileUpload = $this->fetchElementFileUpload($identifier);
315 return $this->fetchSubmittedFileUploadProperty($fileUpload, 'tmp_name');
316 }
317
318 protected function fetchSubmittedFileUploadProperty(mixed $file_upload, string $property)
319 {
320 return $file_upload[$property] ?? null;
321 }
322
323 protected function fetchElementFileUpload($identifier)
324 {
325 return $this->fetchSubmittedUploadFiles()[$identifier] ?? [];
326 }
327
328 protected function fetchSubmittedUploadFiles(): array
329 {
330 $submitted_upload_files = $this->getFileSubmitDataRestructuredByIdentifiers();
331 //$submittedUploadFiles = $this->getFileSubmitsHavingActualUpload($submittedUploadFiles);
332 return $submitted_upload_files;
333 }
334
335 protected function getFileSubmitsHavingActualUpload(array $submitted_upload_files): array
336 {
337 foreach ($submitted_upload_files as $identifier => $upload_properties) {
338 if (!isset($upload_properties['tmp_name'])) {
339 unset($submitted_upload_files[$identifier]);
340 continue;
341 }
342
343 if ($upload_properties['tmp_name'] === '') {
344 unset($submitted_upload_files[$identifier]);
345 continue;
346 }
347
348 if (!is_uploaded_file($upload_properties['tmp_name'])) {
349 unset($submitted_upload_files[$identifier]);
350 }
351 }
352
353 return $submitted_upload_files;
354 }
355
360 {
361 $submitted_upload_files = [];
362
363 foreach ($this->getFileSubmitData() as $uploadProperty => $valueElement) {
364 foreach ($valueElement as $element_identifier => $uploadValue) {
365 if (!isset($submitted_upload_files[$element_identifier])) {
366 $submitted_upload_files[$element_identifier] = [];
367 }
368
369 $submitted_upload_files[$element_identifier][$uploadProperty] = $uploadValue;
370 }
371 }
372
373 return $submitted_upload_files;
374 }
375
376 protected function getFileSubmitData(): array
377 {
378 return $_FILES[$this->getPostVar()] ?? [];
379 }
380
386 protected function wasImageRemovalRequested($identifier): bool
387 {
388 if (!$this->getImageRemovalCommand()) {
389 return false;
390 }
391
392 $cmd = $this->request_data_collector->strArray('cmd', 3);
393
394 if (!isset($cmd[$this->getImageRemovalCommand()])) {
395 return false;
396 }
397
398 $field_arr = $cmd[$this->getImageRemovalCommand()];
399
400 if (!isset($field_arr[$this->getPostVar()])) {
401 return false;
402 }
403
404 return (string) str_replace(
406 '',
407 (string) key($field_arr[$this->getPostVar()])
408 ) === (string) $identifier;
409 }
410}
setImagePathFs(string $image_path_fs)
setImageThumbnailPrefix($imageThumbnailPrefix)
wasImageRemovalRequested($identifier)
TODO: Instead of accessing post, the complete ilFormValuesManipulator should be aware of a server req...
$counter