ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilLTIConsumeProviderList.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5
15{
19 protected $providers = array();
20
21 const SCOPE_GLOBAL = 'global';
22 const SCOPE_USER = 'user';
23 const SCOPE_BOTH = 'both';
24
28 protected $usagesUntrashed = array();
29
33 protected $usagesTrashed = array();
34
38 protected $idsFilter = array();
39
44
48 protected $availabilityFilter = '';
49
53 protected $creatorFilter = 0;
54
58 protected $titleFilter = '';
59
63 protected $categoryFilter = '';
64
68 protected $keywordFilter = '';
69
73 protected $hasOutcomeFilter = null;
74
78 protected $isExternalFilter = null;
79
84
88 public function getProviders()
89 {
90 return $this->providers;
91 }
92
96 public function setProviders(array $providers)
97 {
98 $this->providers = $providers;
99 }
100
104 public function getIdsFilter() : array
105 {
106 return $this->idsFilter;
107 }
108
112 public function setIdsFilter(array $idsFilter)
113 {
114 $this->idsFilter = $idsFilter;
115 }
116
120 public function getAvailabilityFilter() : string
121 {
123 }
124
129 {
130 $this->availabilityFilter = $availabilityFilter;
131 }
132
136 public function getScopeFilter()
137 {
138 return $this->scopeFilter;
139 }
140
145 {
146 $this->scopeFilter = $scopeFilter;
147 }
148
152 public function getCreatorFilter()
153 {
155 }
156
161 {
162 $this->creatorFilter = $creatorFilter;
163 }
164
168 public function getTitleFilter() : string
169 {
170 return $this->titleFilter;
171 }
172
176 public function setTitleFilter(string $titleFilter)
177 {
178 $this->titleFilter = $titleFilter;
179 }
180
184 public function getCategoryFilter() : string
185 {
187 }
188
192 public function setCategoryFilter(string $categoryFilter)
193 {
194 $this->categoryFilter = $categoryFilter;
195 }
196
200 public function getKeywordFilter() : string
201 {
203 }
204
208 public function setKeywordFilter(string $keywordFilter)
209 {
210 $this->keywordFilter = $keywordFilter;
211 }
212
216 public function getHasOutcomeFilter()
217 {
219 }
220
225 {
226 $this->hasOutcomeFilter = $hasOutcomeFilter;
227 }
228
232 public function getIsExternalFilter()
233 {
235 }
236
241 {
242 $this->isExternalFilter = $isExternalFilter;
243 }
244
249 {
251 }
252
257 {
258 $this->isProviderKeyCustomizableFilter = $isProviderKeyCustomizableFilter;
259 }
260
264 public function add(ilLTIConsumeProvider $provider)
265 {
266 $this->providers[] = $provider;
267 }
268
273 public function getById(int $providerId) : ilLTIConsumeProvider
274 {
275 foreach ($this as $provider) {
276 if ($provider->getId() != $providerId) {
277 continue;
278 }
279
280 return $provider;
281 }
282
283 throw new ilLtiConsumerException('provider does not exist in list! (id=' . $providerId . ')');
284 }
285
286 protected function getWhereExpression()
287 {
288 global $DIC; /* @var \ILIAS\DI\Container $DIC */
289
290 $conditions = [];
291
292 if ($this->getIdsFilter()) {
293 $conditions[] = $DIC->database()->in('id', $this->getIdsFilter(), false, 'integer');
294 }
295
296 if (strlen($this->getAvailabilityFilter())) {
297 switch ($this->getAvailabilityFilter()) {
301 $conditions[] = "availability = " . $DIC->database()->quote(
302 $this->getAvailabilityFilter(),
303 'integer'
304 );
305 }
306 }
307
308 switch ($this->getScopeFilter()) {
310 $conditions[] = "global = " . $DIC->database()->quote(1, 'integer');
311 break;
312 case self::SCOPE_USER:
313 $conditions[] = "global = " . $DIC->database()->quote(0, 'integer');
314 break;
315 case self::SCOPE_BOTH:
316 default:
317 }
318
319 if ($this->getCreatorFilter()) {
320 $conditions[] = "creator = " . $DIC->database()->quote($this->getCreatorFilter(), 'integer');
321 }
322
323 if ($this->getTitleFilter()) {
324 $conditions[] = $DIC->database()->like('title', 'text', "%{$this->getTitleFilter()}%");
325 }
326
327 if ($this->getCategoryFilter()) {
328 $conditions[] = "category = " . $DIC->database()->quote($this->getCategoryFilter(), 'text');
329 }
330
331 if ($this->getKeywordFilter()) {
332 $conditions[] = $DIC->database()->like('keywords', 'text', "%{$this->getKeywordFilter()}%");
333 }
334
335 if ($this->getHasOutcomeFilter() !== null) {
336 $conditions[] = "has_outcome = " . $DIC->database()->quote((int) $this->getHasOutcomeFilter(), 'integer');
337 }
338
339 if ($this->getIsExternalFilter() !== null) {
340 $conditions[] = "external_provider = " . $DIC->database()->quote((int) $this->getIsExternalFilter(), 'integer');
341 }
342
343 if ($this->getIsProviderKeyCustomizableFilter() !== null) {
344 $conditions[] = "provider_key_customizable = " . $DIC->database()->quote((int) $this->getIsProviderKeyCustomizableFilter(), 'integer');
345 }
346
347
348 if (!count($conditions)) {
349 return '1 = 1';
350 }
351
352 return implode("\n\t\t\tAND ", $conditions);
353 }
354
355 protected function buildQuery()
356 {
357 $query = "
358 SELECT *
359 FROM lti_ext_provider
360 WHERE {$this->getWhereExpression()}
361 ";
362
363 return $query;
364 }
365
366 public function load()
367 {
368 global $DIC; /* @var \ILIAS\DI\Container $DIC */
369
370 $res = $DIC->database()->query($this->buildQuery());
371
372 while ($row = $DIC->database()->fetchAssoc($res)) {
373 $provider = new ilLTIConsumeProvider();
374 $provider->assignFromDbRow($row);
375 $this->add($provider);
376 }
377 }
378
379 public function loadUsages()
380 {
381 global $DIC; /* @var \ILIAS\DI\Container $DIC */
382
383 $res = $DIC->database()->query("
384 SELECT 'untrashed' query, oset.provider_id, COUNT(oset.obj_id) cnt
385 FROM lti_consumer_settings oset
386 INNER JOIN object_reference oref
387 ON oref.obj_id = oset.obj_id
388 AND oref.deleted IS NULL
389 GROUP BY oset.provider_id
390
391 UNION
392
393 SELECT 'trashed' query, oset.provider_id, COUNT(oset.obj_id) cnt
394 FROM lti_consumer_settings oset
395 INNER JOIN object_reference oref
396 ON oref.obj_id = oset.obj_id
397 AND oref.deleted IS NOT NULL
398 GROUP BY oset.provider_id
399 ");
400
401 while ($row = $DIC->database()->fetchAssoc($res)) {
402 if ($row['query'] == 'untrashed') {
403 $this->usagesUntrashed[ $row['provider_id'] ] = (int) $row['cnt'];
404 } elseif ($row['query'] == 'trashed') {
405 $this->usagesTrashed[ $row['provider_id'] ] = (int) $row['cnt'];
406 }
407 }
408 }
409
414 public function hasUsages(int $providerId) : bool
415 {
416 return $this->hasUntrashedUsages($providerId) || $this->hasTrashedUsages($providerId);
417 }
418
423 public function hasUntrashedUsages(int $providerId) : bool
424 {
425 return isset($this->usagesUntrashed[$providerId]) && $this->usagesUntrashed[$providerId];
426 }
427
432 public function hasTrashedUsages(int $providerId) : bool
433 {
434 return isset($this->usagesTrashed[$providerId]) && $this->usagesTrashed[$providerId];
435 }
436
440 public function getTableData()
441 {
442 $this->loadUsages();
443
444 $tableData = array();
445
446 foreach ($this as $provider) {
447 $tblRow = array();
448
449 $tblRow['id'] = $provider->getId();
450 $tblRow['title'] = $provider->getTitle();
451 $tblRow['description'] = $provider->getDescription();
452 $tblRow['category'] = $provider->getCategory();
453 $tblRow['keywords'] = $provider->getKeywordsArray();
454 $tblRow['outcome'] = $provider->getHasOutcome();
455 $tblRow['external'] = $provider->isExternalProvider();
456 $tblRow['provider_key_customizable'] = $provider->isProviderKeyCustomizable();
457 $tblRow['availability'] = $provider->getAvailability();
458 $tblRow['creator'] = $provider->getCreator();
459 $tblRow['accepted_by'] = $provider->getAcceptedBy();
460
461 if ($provider->getProviderIcon()->exists()) {
462 $tblRow['icon'] = $provider->getProviderIcon()->getAbsoluteFilePath();
463 }
464
465 $tblRow['usages_untrashed'] = 0;
466 if (isset($this->usagesUntrashed[$provider->getId()])) {
467 $tblRow['usages_untrashed'] = $this->usagesUntrashed[$provider->getId()];
468 }
469
470 $tblRow['usages_trashed'] = 0;
471 if (isset($this->usagesTrashed[$provider->getId()])) {
472 $tblRow['usages_trashed'] = $this->usagesTrashed[$provider->getId()];
473 }
474
475 $tableData[] = $tblRow;
476 }
477
478 return $tableData;
479 }
480
481 public function getTableDataUsedBy()
482 {
483 $tableData = [];
484 $i = 0;
485 foreach ($this->getTableData() as $key => $tableRow) {
486 if (!(bool) $tableRow['usages_trashed'] && !(bool) $tableRow['usages_untrashed']) {
487 continue;
488 }
489 foreach ($this->loadUsedBy($tableRow['id']) as $usedByObjId => $usedByData) {
490 $tableData[$i] = $tableRow;
491 $tableData[$i]['usedByObjId'] = $usedByObjId;
492 $tableData[$i]['usedByRefId'] = $usedByData['ref_id'];
493 $tableData[$i]['usedByTitle'] = $usedByData['title'];
494 $tableData[$i]['usedByIsTrashed'] = $usedByData['trashed'];
495 $i++;
496 } // EOF foreach( $this->loadUsedBy($tableRow['id'])
497 } // EOF foreach($this->getTableData()
498 return $tableData;
499 }
500
501 private function loadUsedBy($providerId)
502 {
503 global $DIC; /* @var \ILIAS\DI\Container $DIC */
504
505 $retArr = [];
506 $pId = $DIC->database()->quote($providerId, 'integer');
507 $res = $DIC->database()->query(
508 "SELECT oset.obj_id AS obj_id, oref.ref_id AS ref_id, oref.deleted as trashed, odata.title AS title" .
509 " FROM lti_consumer_settings oset, object_reference oref, object_data odata" .
510 " WHERE oset.provider_id = " . $pId .
511 " AND oref.obj_id = oset.obj_id" .
512 " AND odata.obj_id = oset.obj_id"
513 );
514 while ($row = $DIC->database()->fetchAssoc($res)) {
515 $retArr[$row['obj_id']] = [
516 'ref_id' => $row['ref_id'],
517 'title' => $row['title'],
518 'trashed' => null !== $row['trashed'] ? true : false
519 ];
520 }
521 return $retArr;
522 }
523
524 public function current()
525 {
526 return current($this->providers);
527 }
528 public function next()
529 {
530 return next($this->providers);
531 }
532 public function key()
533 {
534 return key($this->providers);
535 }
536 public function valid()
537 {
538 return key($this->providers) !== null;
539 }
540 public function rewind()
541 {
542 return reset($this->providers);
543 }
544}
An exception for terminatinating execution or to throw for unit testing.
setAvailabilityFilter(string $availabilityFilter)
setIsProviderKeyCustomizableFilter($isProviderKeyCustomizableFilter)
add(ilLTIConsumeProvider $provider)
$i
Definition: metadata.php:24
$query
foreach($_POST as $key=> $value) $res
$DIC
Definition: xapitoken.php:46