ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
RequestDataCollector.php
Go to the documentation of this file.
1 <?php
2 
19 namespace ILIAS\TestQuestionPool;
20 
21 use Closure;
29 
31 {
32  use BaseGUIRequest;
33 
34  public function __construct(
37  protected readonly FileUpload $upload
38  ) {
39  $this->initRequest($http, $refinery);
40  }
41 
45  public function getProcessedUploads(): array
46  {
47  $uploads = [];
48 
49  if ($this->upload->hasUploads()) {
50  if (!$this->upload->hasBeenProcessed()) {
51  $this->upload->process();
52  }
53  $uploads = $this->upload->getResults();
54  }
55 
56  return $uploads;
57  }
58 
64  public function getUploadFilename(array $http_names, int $index): ?string
65  {
66  $uploaded_files = $this->http->request()->getUploadedFiles();
67 
68  while (($current_key = array_shift($http_names)) !== null) {
69  if (!isset($uploaded_files[$current_key])) {
70  return null;
71  }
72 
73  $uploaded_files = $uploaded_files[$current_key];
74 
75  if (isset($uploaded_files[$index]) && $http_names === []) {
77  $file = $uploaded_files[$index];
78  $c = Closure::bind(static function (UploadedFile $file): ?string {
79  return $file->file ?? null;
80  }, null, $file);
81 
82  return $c($file);
83  }
84  }
85 
86  return null;
87  }
88 
89  public function upload(): FileUpload
90  {
91  return $this->upload;
92  }
93 
94  public function isset(string $key): bool
95  {
96  return $this->raw($key) !== null;
97  }
98 
99  public function hasRefId(): int
100  {
101  return $this->raw('ref_id') !== null;
102  }
103 
104  public function getRefId(): int
105  {
106  return $this->int('ref_id');
107  }
108 
109  public function hasQuestionId(): bool
110  {
111  return $this->raw('q_id') !== null;
112  }
113 
114  public function getQuestionId(): int
115  {
116  return $this->int('q_id');
117  }
118 
122  public function getIds(): array
123  {
124  return $this->strArray('id');
125  }
126 
130  public function raw(string $key): mixed
131  {
132  return $this->get($key, $this->refinery->identity());
133  }
134 
135  public function float(string $key): float
136  {
137  try {
138  return $this->get($key, $this->refinery->kindlyTo()->float()) ?? 0.0;
139  } catch (ConstraintViolationException $e) {
140  return 0.0;
141  }
142  }
143 
144  public function string(string $key): string
145  {
146  return $this->get($key, $this->refinery->kindlyTo()->string()) ?? '';
147  }
148 
149  public function bool(string $key): ?bool
150  {
151  return $this->get($key, $this->refinery->kindlyTo()->bool());
152  }
153 
154  public function getParsedBody(): object|array|null
155  {
156  return $this->http->request()->getParsedBody();
157  }
158 
162  public function getUnitIds(): array
163  {
164  return $this->intArray('unit_ids');
165  }
166 
170  public function getUnitCategoryIds(): array
171  {
172  return $this->intArray('category_ids');
173  }
174 
175  public function getMatchingPairs(): array
176  {
177  if (!$this->http->wrapper()->post()->has('matching')) {
178  return [];
179  }
180 
181  return $this->http->wrapper()->post()->retrieve(
182  'matching',
183  $this->refinery->byTrying([
184  $this->refinery->container()->mapValues(
185  $this->refinery->custom()->transformation(
186  fn(string $v): array => $this->refinery->container()->mapValues(
187  $this->refinery->kindlyTo()->int()
188  )->transform(json_decode($v))
189  )
190  ),
191  $this->refinery->always([])
192  ])
193  );
194  }
195 
196  public function getPostKeys(): array
197  {
198  return $this->http->wrapper()->post()->keys();
199  }
200 
201  public function getCmdIndex(string $key): int|string|null
202  {
203  $cmd = $this->rawArray('cmd');
204  if (!isset($cmd[$key]) || !is_array($cmd[$key])) {
205  return null;
206  }
207  return key($cmd[$key]);
208  }
209 
213  public function strArray(string $key, int $depth = 1): array
214  {
215  return $this->retrieveArray($key, $depth, $this->refinery->kindlyTo()->string());
216  }
217 
221  public function floatArray(string $key): array
222  {
223  return $this->retrieveArray($key, 1, $this->refinery->kindlyTo()->float());
224  }
225 
229  public function intArray(string $key): array
230  {
231  return $this->retrieveArray($key, 1, $this->refinery->kindlyTo()->int());
232  }
233 
237  public function rawArray(string $key): array
238  {
239  return $this->retrieveArray($key, 1, $this->refinery->identity());
240  }
241 
242  private function retrieveArray(string $key, int $depth, Transformation $transformation): array
243  {
244  $chain = $this->refinery->kindlyTo()->dictOf($transformation);
245  for ($i = 1; $i < $depth; $i++) {
246  $chain = $this->refinery->kindlyTo()->dictOf($chain);
247  }
248 
249  return $this->http->wrapper()->post()->retrieve(
250  $key,
251  $this->refinery->byTrying([
252  $chain,
253  $this->refinery->always([])
254  ])
255  );
256  }
257 }
$http
Definition: deliver.php:30
$c
Definition: deliver.php:25
trait BaseGUIRequest
Base gui request wrapper.
retrieveArray(string $key, int $depth, Transformation $transformation)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
initRequest(HTTP\Services $http, Refinery\Factory $refinery, ?array $passed_query_params=null, ?array $passed_post_data=null)
Query params and post data parameters are used for testing.
static http()
Fetches the global http state from ILIAS.
Class FileUpload.
Definition: FileUpload.php:37
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A transformation is a function from one datatype to another.
__construct(Services $http, Factory $refinery, protected readonly FileUpload $upload)