ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilLTIConsumeProviderList.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
29class ilLTIConsumeProviderList implements Iterator
30{
34 protected array $providers = array();
35
36 public const SCOPE_GLOBAL = 'global';
37 public const SCOPE_USER = 'user';
38 public const SCOPE_BOTH = 'both';
39
43 protected array $usagesUntrashed = array();
44
48 protected array $usagesTrashed = array();
49
53 protected array $idsFilter = array();
54
58 protected string $scopeFilter = self::SCOPE_BOTH;
59
63 protected string $availabilityFilter = '';
64
68 protected int $creatorFilter = 0;
69
73 protected string $titleFilter = '';
74
78 protected string $categoryFilter = '';
79
83 protected string $keywordFilter = '';
84
88 protected ?bool $hasOutcomeFilter = null;
89
93 protected ?bool $isExternalFilter = null;
94
98 protected ?bool $isProviderKeyCustomizableFilter = null;
99
103 public function getProviders(): array
104 {
105 return $this->providers;
106 }
107
111 public function setProviders(array $providers): void
112 {
113 $this->providers = $providers;
114 }
115
119 public function getIdsFilter(): array
120 {
121 return $this->idsFilter;
122 }
123
127 public function setIdsFilter(array $idsFilter): void
128 {
129 $this->idsFilter = $idsFilter;
130 }
131
132 public function getAvailabilityFilter(): string
133 {
135 }
136
137 public function setAvailabilityFilter(string $availabilityFilter): void
138 {
139 $this->availabilityFilter = $availabilityFilter;
140 }
141
142 public function getScopeFilter(): string
143 {
144 return $this->scopeFilter;
145 }
146
147 public function setScopeFilter(string $scopeFilter): void
148 {
149 $this->scopeFilter = $scopeFilter;
150 }
151
152 public function getCreatorFilter(): int
153 {
155 }
156
157 public function setCreatorFilter(int $creatorFilter): void
158 {
159 $this->creatorFilter = $creatorFilter;
160 }
161
162 public function getTitleFilter(): string
163 {
164 return $this->titleFilter;
165 }
166
167 public function setTitleFilter(string $titleFilter): void
168 {
169 $this->titleFilter = $titleFilter;
170 }
171
172 public function getCategoryFilter(): string
173 {
175 }
176
177 public function setCategoryFilter(string $categoryFilter): void
178 {
179 $this->categoryFilter = $categoryFilter;
180 }
181
182 public function getKeywordFilter(): string
183 {
185 }
186
187 public function setKeywordFilter(string $keywordFilter): void
188 {
189 $this->keywordFilter = $keywordFilter;
190 }
191
192 public function getHasOutcomeFilter(): ?bool
193 {
195 }
196
197 public function setHasOutcomeFilter(?bool $hasOutcomeFilter): void
198 {
199 $this->hasOutcomeFilter = $hasOutcomeFilter;
200 }
201
202 public function getIsExternalFilter(): ?bool
203 {
205 }
206
207 public function setIsExternalFilter(?bool $isExternalFilter): void
208 {
209 $this->isExternalFilter = $isExternalFilter;
210 }
211
212 public function getIsProviderKeyCustomizableFilter(): ?bool
213 {
215 }
216
218 {
219 $this->isProviderKeyCustomizableFilter = $isProviderKeyCustomizableFilter;
220 }
221
222 public function add(ilLTIConsumeProvider $provider): void
223 {
224 $this->providers[] = $provider;
225 }
226
231 {
232 foreach ($this as $provider) {
233 if ($provider->getId() != $providerId) {
234 continue;
235 }
236
237 return $provider;
238 }
239
240 throw new ilException('provider does not exist in list! (id=' . $providerId . ')');
241 }
242
243 protected function getWhereExpression(): string
244 {
245 global $DIC; /* @var \ILIAS\DI\Container $DIC */
246
247 $conditions = [];
248
249 if ($this->getIdsFilter()) {
250 $conditions[] = $DIC->database()->in('id', $this->getIdsFilter(), false, 'integer');
251 }
252
253 if (strlen($this->getAvailabilityFilter())) {
254 switch ($this->getAvailabilityFilter()) {
258 $conditions[] = "availability = " . $DIC->database()->quote(
259 $this->getAvailabilityFilter(),
260 'integer'
261 );
262 }
263 }
264
265 switch ($this->getScopeFilter()) {
267 $conditions[] = "global = " . $DIC->database()->quote(1, 'integer');
268 break;
269 case self::SCOPE_USER:
270 $conditions[] = "global = " . $DIC->database()->quote(0, 'integer');
271 break;
272 case self::SCOPE_BOTH:
273 default:
274 }
275
276 if ($this->getCreatorFilter()) {
277 $conditions[] = "creator = " . $DIC->database()->quote($this->getCreatorFilter(), 'integer');
278 }
279
280 if ($this->getTitleFilter()) {
281 $conditions[] = $DIC->database()->like('title', 'text', "%{$this->getTitleFilter()}%");
282 }
283
284 if ($this->getCategoryFilter()) {
285 $conditions[] = "category = " . $DIC->database()->quote($this->getCategoryFilter(), 'text');
286 }
287
288 if ($this->getKeywordFilter()) {
289 $conditions[] = $DIC->database()->like('keywords', 'text', "%{$this->getKeywordFilter()}%");
290 }
291
292 if ($this->getHasOutcomeFilter() !== null) {
293 $conditions[] = "has_outcome = " . $DIC->database()->quote((int) $this->getHasOutcomeFilter(), 'integer');
294 }
295
296 if ($this->getIsExternalFilter() !== null) {
297 $conditions[] = "external_provider = " . $DIC->database()->quote((int) $this->getIsExternalFilter(), 'integer');
298 }
299
300 if ($this->getIsProviderKeyCustomizableFilter() !== null) {
301 $conditions[] = "provider_key_customizable = " . $DIC->database()->quote((int) $this->getIsProviderKeyCustomizableFilter(), 'integer');
302 }
303
304
305 if (count($conditions) === 0) {
306 return '1 = 1';
307 }
308
309 return implode("\n\t\t\tAND ", $conditions);
310 }
311
312 protected function buildQuery(): string
313 {
314 return "
315 SELECT *
316 FROM lti_ext_provider
317 WHERE {$this->getWhereExpression()}
318 ";
319 }
320
321 public function load(): void
322 {
323 global $DIC; /* @var \ILIAS\DI\Container $DIC */
324
325 $res = $DIC->database()->query($this->buildQuery());
326
327 while ($row = $DIC->database()->fetchAssoc($res)) {
329 $provider->assignFromDbRow($row);
330 $this->add($provider);
331 }
332 }
333
334 public function loadUsages(): void
335 {
336 global $DIC; /* @var \ILIAS\DI\Container $DIC */
337
338 $res = $DIC->database()->query("
339 SELECT 'untrashed' query, oset.provider_id, COUNT(oset.obj_id) cnt
340 FROM lti_consumer_settings oset
341 INNER JOIN object_reference oref
342 ON oref.obj_id = oset.obj_id
343 AND oref.deleted IS NULL
344 GROUP BY oset.provider_id
345
346 UNION
347
348 SELECT 'trashed' query, oset.provider_id, COUNT(oset.obj_id) cnt
349 FROM lti_consumer_settings oset
350 INNER JOIN object_reference oref
351 ON oref.obj_id = oset.obj_id
352 AND oref.deleted IS NOT NULL
353 GROUP BY oset.provider_id
354 ");
355
356 while ($row = $DIC->database()->fetchAssoc($res)) {
357 if ($row['query'] == 'untrashed') {
358 $this->usagesUntrashed[ $row['provider_id'] ] = (int) $row['cnt'];
359 } elseif ($row['query'] == 'trashed') {
360 $this->usagesTrashed[ $row['provider_id'] ] = (int) $row['cnt'];
361 }
362 }
363 }
364
365 public function hasUsages(int $providerId): bool
366 {
367 return $this->hasUntrashedUsages($providerId) || $this->hasTrashedUsages($providerId);
368 }
369
370 public function hasUntrashedUsages(int $providerId): bool
371 {
372 return isset($this->usagesUntrashed[$providerId]) && $this->usagesUntrashed[$providerId];
373 }
374
375 public function hasTrashedUsages(int $providerId): bool
376 {
377 return isset($this->usagesTrashed[$providerId]) && $this->usagesTrashed[$providerId];
378 }
379
383 public function getTableData(): array
384 {
385 $this->loadUsages();
386
387 $tableData = array();
388
389 foreach ($this as $provider) {
390 $tblRow = array();
391
392 $tblRow['id'] = $provider->getId();
393 $tblRow['title'] = htmlspecialchars($provider->getTitle());
394 $tblRow['description'] = htmlspecialchars($provider->getDescription());
395 $tblRow['category'] = $provider->getCategory();
396 $tblRow['keywords'] = $this->getKeywordsFormatted($provider->getKeywordsArray());
397 $tblRow['outcome'] = $provider->getHasOutcome();
398 $tblRow['external'] = $provider->isExternalProvider();
399 $tblRow['provider_key_customizable'] = $provider->isProviderKeyCustomizable();
400 $tblRow['availability'] = $provider->getAvailability();
401 $tblRow['creator'] = $provider->getCreator();
402 $tblRow['accepted_by'] = $provider->getAcceptedBy();
403
404 if ($provider->getProviderIcon()->exists()) {
405 $tblRow['icon'] = $provider->getProviderIcon()->getAbsoluteFilePath();
406 }
407
408 $tblRow['usages_untrashed'] = 0;
409 if (isset($this->usagesUntrashed[$provider->getId()])) {
410 $tblRow['usages_untrashed'] = $this->usagesUntrashed[$provider->getId()];
411 }
412
413 $tblRow['usages_trashed'] = 0;
414 if (isset($this->usagesTrashed[$provider->getId()])) {
415 $tblRow['usages_trashed'] = $this->usagesTrashed[$provider->getId()];
416 }
417
418 $tableData[] = $tblRow;
419 }
420
421 return $tableData;
422 }
423
427 public function getTableDataUsedBy(): array
428 {
429 $tableData = [];
430 $i = 0;
431 foreach ($this->getTableData() as $tableRow) {
432 if (!(bool) $tableRow['usages_trashed'] && !(bool) $tableRow['usages_untrashed']) {
433 continue;
434 }
435 foreach ($this->loadUsedBy($tableRow['id']) as $usedByObjId => $usedByData) {
436 $tableData[$i] = $tableRow;
437 $tableData[$i]['usedByObjId'] = $usedByObjId;
438 $tableData[$i]['usedByRefId'] = $usedByData['ref_id'];
439 $tableData[$i]['usedByTitle'] = $usedByData['title'];
440 $tableData[$i]['usedByIsTrashed'] = $usedByData['trashed'];
441 $i++;
442 } // EOF foreach( $this->loadUsedBy($tableRow['id'])
443 } // EOF foreach($this->getTableData()
444 return $tableData;
445 }
446
450 private function loadUsedBy(int $providerId): array
451 {
452 global $DIC; /* @var \ILIAS\DI\Container $DIC */
453
454 $retArr = [];
455 $pId = $DIC->database()->quote($providerId, 'integer');
456 $res = $DIC->database()->query(
457 "SELECT oset.obj_id AS obj_id, oref.ref_id AS ref_id, oref.deleted as trashed, odata.title AS title" .
458 " FROM lti_consumer_settings oset, object_reference oref, object_data odata" .
459 " WHERE oset.provider_id = " . $pId .
460 " AND oref.obj_id = oset.obj_id" .
461 " AND odata.obj_id = oset.obj_id"
462 );
463 while ($row = $DIC->database()->fetchAssoc($res)) {
464 $retArr[$row['obj_id']] = [
465 'ref_id' => $row['ref_id'],
466 'title' => $row['title'],
467 'trashed' => null !== $row['trashed'] ? true : false
468 ];
469 }
470 return $retArr;
471 }
472
476 public function current(): mixed
477 {
478 return current($this->providers);
479 }
480
481 public function next(): void
482 {
483 next($this->providers);
484 }
485
486 public function key(): mixed
487 {
488 return key($this->providers);
489 }
490
491 public function valid(): bool
492 {
493 return key($this->providers) !== null;
494 }
495
496 public function rewind(): void
497 {
498 reset($this->providers);
499 }
500
501 protected function getKeywordsFormatted(array $keywords): string
502 {
503 return implode('<br />', $keywords);
504 }
505}
return true
Base class for ILIAS Exception handling.
setAvailabilityFilter(string $availabilityFilter)
add(ilLTIConsumeProvider $provider)
setIsExternalFilter(?bool $isExternalFilter)
setHasOutcomeFilter(?bool $hasOutcomeFilter)
setIsProviderKeyCustomizableFilter(?bool $isProviderKeyCustomizableFilter)
$res
Definition: ltiservices.php:69
$provider
Definition: ltitoken.php:80
if(empty($clientId)) $providerId
Definition: ltitoken.php:79
global $DIC
Definition: shib_login.php:26