ILIAS  release_8 Revision v8.24
Item.php
Go to the documentation of this file.
1<?php
2
20
22
31class Item
32{
36 public const TYPE_LINK = 'link';
37
41 public const TYPE_LTI_LINK = 'ltiResourceLink';
42
46 public const TYPE_LTI_ASSIGNMENT = 'ltiAssignment';
47
51 public const TYPE_FILE = 'file';
52
56 public const TYPE_HTML = 'html';
57
61 public const TYPE_IMAGE = 'image';
62
66 public const LTI_LINK_MEDIA_TYPE = 'application/vnd.ims.lti.v1.ltilink';
67
71 public const LTI_ASSIGNMENT_MEDIA_TYPE = 'application/vnd.ims.lti.v1.ltiassignment';
72
78 private ?string $type = null;
79
85// private ?string $id = null; //UK: changed to public
86 public ?string $id = null;
87
93 private array $placements = array();
94
100// private ?string $url = null; //UK: changed to public
101 public ?string $url = null;
102
108 private ?string $mediaType = null;
109
115 private ?string $title = null;
116
122 private ?string $text = null;
123
129 private ?string $html = null;
130
136 private ?Image $icon = null;
137
143 private ?Image $thumbnail = null;
144
150 private ?bool $hideOnCreate = null;
151
158 public function __construct(string $type, $placementAdvices = null, string $id = null)
159 {
160 $this->type = $type;
161 if (!empty($placementAdvices)) {
162 if (!is_array($placementAdvices)) {
163 $placementAdvices = array($placementAdvices);
164 }
165 foreach ($placementAdvices as $placementAdvice) {
166 $this->placements[$placementAdvice->documentTarget] = $placementAdvice;
167 }
168 }
169 $this->id = $id;
170 }
171
176 public function setUrl(string $url)
177 {
178 $this->url = $url;
179 }
180
185 public function setMediaType(string $mediaType)
186 {
187 $this->mediaType = $mediaType;
188 }
189
194 public function setTitle(string $title)
195 {
196 $this->title = $title;
197 }
198
203 public function setText(string $text)
204 {
205 $this->text = $text;
206 }
207
212 public function setHtml(string $html)
213 {
214 $this->html = $html;
215 }
216
221 public function addPlacementAdvice(Placement $placementAdvice)
222 {
223 if (!empty($placementAdvice)) {
224 $this->placements[$placementAdvice->documentTarget] = $placementAdvice;
225 }
226 }
227
232 public function setIcon(Image $icon)
233 {
234 $this->icon = $icon;
235 }
236
242 {
243 $this->thumbnail = $thumbnail;
244 }
245
250 public function setHideOnCreate(?bool $hideOnCreate)
251 {
252 $this->hideOnCreate = $hideOnCreate;
253 }
254
261 public static function toJson($items, string $ltiVersion = Util::LTI_VERSION1): string
262 {
263 if (!is_array($items)) {
264 $items = array($items);
265 }
266 if ($ltiVersion !== Util::LTI_VERSION1P3) {
267 $obj = new \stdClass();
268 $obj->{'@context'} = 'http://purl.imsglobal.org/ctx/lti/v1/ContentItem';
269 $obj->{'@graph'} = array();
270 foreach ($items as $item) {
271 $obj->{'@graph'}[] = $item->toJsonldObject();
272 }
273 } else {
274 $obj = array();
275 foreach ($items as $item) {
276 $obj[] = $item->toJsonObject();
277 }
278 }
279
280 return json_encode($obj);
281 }
282
288 public static function fromJson(object $items): array
289 {
290 $isJsonLd = isset($items->{'@graph'});
291 if ($isJsonLd) {
292 $items = $items->{'@graph'};
293 }
294 if (!is_array($items)) {
295 $items = array($items);
296 }
297 $objs = array();
298 foreach ($items as $item) {
299 $obj = self::fromJsonItem($item);
300 if (!empty($obj)) {
301 $objs[] = $obj;
302 }
303 }
304
305 return $objs;
306 }
307
313 protected function toJsonldObject(): object
314 {
315 $item = new \stdClass();
316 if (!empty($this->id)) {
317 $item->{'@id'} = $this->id;
318 }
319 if (!empty($this->type)) {
320 if (($this->type === self::TYPE_LTI_LINK) || ($this->type === self::TYPE_LTI_ASSIGNMENT)) {
321 $item->{'@type'} = 'LtiLinkItem';
322 } elseif ($this->type === self::TYPE_FILE) {
323 $item->{'@type'} = 'FileItem';
324 } else {
325 $item->{'@type'} = 'ContentItem';
326 }
327 } else {
328 $item->{'@type'} = 'ContentItem';
329 }
330 if (!empty($this->title)) {
331 $item->title = $this->title;
332 }
333 if (!empty($this->text)) {
334 $item->text = $this->text;
335 } elseif (!empty($this->html)) {
336 $item->text = $this->html;
337 }
338 if (!empty($this->url)) {
339 $item->url = $this->url;
340 }
341 if (!empty($this->mediaType)) {
342 $item->mediaType = $this->mediaType;
343 }
344 if (!empty($this->placements)) {
345 $placementAdvice = new \stdClass();
346 $placementAdvices = array();
347 foreach ($this->placements as $placement) {
348 $obj = $placement->toJsonldObject();
349 if (!empty($obj)) {
350 if (!empty($placement->documentTarget)) {
351 $placementAdvices[] = $placement->documentTarget;
352 }
353 $placementAdvice = (object) array_merge((array) $placementAdvice, (array) $obj);
354 }
355 }
356 if (!empty($placementAdvice)) {
357 $item->placementAdvice = $placementAdvice;
358 if (!empty($placementAdvices)) {
359 $item->placementAdvice->presentationDocumentTarget = implode(',', $placementAdvices);
360 }
361 }
362 }
363 if (!empty($this->icon)) {
364 $item->icon = $this->icon->toJsonldObject();
365 }
366 if (!empty($this->thumbnail)) {
367 $item->thumbnail = $this->thumbnail->toJsonldObject();
368 }
369 if (!is_null($this->hideOnCreate)) {
370 $item->hideOnCreate = $this->hideOnCreate;
371 }
372
373 return $item;
374 }
375
381 protected function toJsonObject(): object
382 {
383 $item = new \stdClass();
384 switch ($this->type) {
385 case 'LtiLinkItem':
386 $item->type = self::TYPE_LTI_LINK;
387 break;
388 case 'FileItem':
389 $item->type = self::TYPE_FILE;
390 break;
391 case 'ContentItem':
392 if (empty($this->url)) {
393 $item->type = self::TYPE_HTML;
394 } elseif (!empty($this->mediaType) && (strpos($this->mediaType, 'image') === 0)) {
395 $item->type = self::TYPE_IMAGE;
396 } else {
397 $item->type = self::TYPE_LINK;
398 }
399 break;
400 default:
401 $item->type = $this->type;
402 break;
403 }
404 if (!empty($this->title)) {
405 $item->title = $this->title;
406 }
407 if (!empty($this->text)) {
408 $item->text = Util::stripHtml($this->text);
409 }
410 if (!empty($this->html)) {
411 $item->html = $this->html;
412 }
413 if (!empty($this->url)) {
414 $item->url = $this->url;
415 }
416 foreach ($this->placements as $type => $placement) {
417 switch ($type) {
418 case Placement::TYPE_EMBED:
419 case Placement::TYPE_IFRAME:
420 case Placement::TYPE_WINDOW:
421 case Placement::TYPE_FRAME:
422 $obj = $placement->toJsonObject();
423 break;
424 default:
425 $obj = null;
426 break;
427 }
428 if (!empty($obj)) {
429 $item->{$type} = $obj;
430 }
431 }
432 if (!empty($this->icon)) {
433 $item->icon = $this->icon->toJsonObject();
434 }
435 if (!empty($this->thumbnail)) {
436 $item->thumbnail = $this->thumbnail->toJsonObject();
437 }
438 if (!is_null($this->hideOnCreate)) {
439 $item->hideOnCreate = $this->hideOnCreate;
440 }
441
442 return $item;
443 }
444
450 public static function fromJsonItem(object $item)
451 {
452 $obj = null;
453 $placement = null;
454 if (isset($item->{'@type'})) {
455 if (isset($item->presentationDocumentTarget)) {
456 $placement = Placement::fromJsonObject($item, $item->presentationDocumentTarget);
457 }
458 switch ($item->{'@type'}) {
459 case 'ContentItem':
460 $obj = new Item('ContentItem', $placement);
461 break;
462 case 'LtiLinkItem':
463 $obj = new LtiLinkItem($placement);
464 break;
465 case 'FileItem':
466 $obj = new FileItem($placement);
467 break;
468 }
469 } elseif (isset($item->type)) {
470 $placements = array();
471 $placement = Placement::fromJsonObject($item, 'embed');
472 if (!empty($placement)) {
473 $placements[] = $placement;
474 }
475 $placement = Placement::fromJsonObject($item, 'iframe');
476 if (!empty($placement)) {
477 $placements[] = $placement;
478 }
479 $placement = Placement::fromJsonObject($item, 'window');
480 if (!empty($placement)) {
481 $placements[] = $placement;
482 }
483 switch ($item->type) {
484 case self::TYPE_LINK:
485 case self::TYPE_HTML:
486 case self::TYPE_IMAGE:
487 $obj = new Item($item->type, $placements);
488 break;
489 case self::TYPE_LTI_LINK:
490 $obj = new LtiLinkItem($placements);
491 break;
492 case self::TYPE_LTI_ASSIGNMENT:
493 $obj = new LtiAssignmentItem($placements);
494 break;
495 case self::TYPE_FILE:
496 $obj = new FileItem($placements);
497 break;
498 }
499 }
500 if (!empty($obj)) {
501 $obj->fromJsonObject($item);
502 }
503
504 return $obj;
505 }
506
511 protected function fromJsonObject(object $item)
512 {
513 if (isset($item->{'@id'})) {
514 $this->id = $item->{'@id'};
515 }
516 foreach (get_object_vars($item) as $name => $value) {
517 switch ($name) {
518 case 'title':
519 case 'text':
520 case 'html':
521 case 'url':
522 case 'mediaType':
523 case 'hideOnCreate':
524 $this->{$name} = $item->{$name};
525 break;
526 case 'placementAdvice':
527 $this->addPlacementAdvice(Placement::fromJsonObject($item));
528 break;
529 case 'embed':
530 case 'window':
531 case 'iframe':
532 $this->addPlacementAdvice(Placement::fromJsonObject($item, $name));
533 break;
534 case 'icon':
535 case 'thumbnail':
536 $this->{$name} = Image::fromJsonObject($item->{$name});
537 break;
538 }
539 }
540 }
541}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Class to represent a content-item image object.
Definition: Image.php:29
Class to represent a content-item object.
Definition: Item.php:32
Image $icon
Icon image object for content-item.
Definition: Item.php:136
setHtml(string $html)
Set an HTML embed value for the content-item.
Definition: Item.php:212
setIcon(Image $icon)
Set an icon image for the content-item.
Definition: Item.php:232
const TYPE_IMAGE
Type for image content-item.
Definition: Item.php:61
string $type
Type of content-item.
Definition: Item.php:78
string $mediaType
Media type of content-item.
Definition: Item.php:108
setTitle(string $title)
Set a title value for the content-item.
Definition: Item.php:194
setThumbnail(Image $thumbnail)
Set a thumbnail image for the content-item.
Definition: Item.php:241
array $placements
Array of placement objects for content-item.
Definition: Item.php:93
const TYPE_FILE
Type for file content-item.
Definition: Item.php:51
string $title
Title of content-item.
Definition: Item.php:115
setMediaType(string $mediaType)
Set a media type value for the content-item.
Definition: Item.php:185
const TYPE_LINK
Type for link content-item.
Definition: Item.php:36
__construct(string $type, $placementAdvices=null, string $id=null)
Class constructor.
Definition: Item.php:158
setText(string $text)
Set a link text value for the content-item.
Definition: Item.php:203
static toJson($items, string $ltiVersion=Util::LTI_VERSION1)
Wrap the content items to form a complete application/vnd.ims.lti.v1.contentitems+json media type ins...
Definition: Item.php:261
toJsonldObject()
Wrap the content item to form an item complying with the application/vnd.ims.lti.v1....
Definition: Item.php:313
fromJsonObject(object $item)
Extract content-item details from its JSON representation.
Definition: Item.php:511
const TYPE_LTI_LINK
Type for LTI link content-item.
Definition: Item.php:41
setHideOnCreate(?bool $hideOnCreate)
Set whether the content-item should be hidden from learners by default.
Definition: Item.php:250
static fromJsonItem(object $item)
Generate an Item object from its JSON or JSON-LD representation.
Definition: Item.php:450
string $html
HTML to be embedded.
Definition: Item.php:129
string $id
ID of content-item.
Definition: Item.php:86
bool $hideOnCreate
Hide the item from learners by default?
Definition: Item.php:150
const TYPE_LTI_ASSIGNMENT
Type for LTI assignment content-item.
Definition: Item.php:46
const LTI_LINK_MEDIA_TYPE
Media type for LTI launch links.
Definition: Item.php:66
string $url
URL of content-item.
Definition: Item.php:101
string $text
Description of content-item.
Definition: Item.php:122
Image $thumbnail
Thumbnail image object for content-item.
Definition: Item.php:143
static fromJson(object $items)
Generate an array of Item objects from their JSON representation.
Definition: Item.php:288
const TYPE_HTML
Type for HTML content-item.
Definition: Item.php:56
const LTI_ASSIGNMENT_MEDIA_TYPE
Media type for LTI assignment links.
Definition: Item.php:71
toJsonObject()
Wrap the content items to form a complete value for the https://purl.imsglobal.org/spec/lti-dl/claim/...
Definition: Item.php:381
addPlacementAdvice(Placement $placementAdvice)
Add a placement for the content-item.
Definition: Item.php:221
setUrl(string $url)
Set a URL value for the content-item.
Definition: Item.php:176
Class to represent a content-item placement object.
Definition: Placement.php:29
const LTI_VERSION1
LTI version 1 for messages.
Definition: Util.php:28
const LTI_VERSION1P3
LTI version 1.3 for messages.
Definition: Util.php:33
if($format !==null) $name
Definition: metadata.php:247
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: FileItem.php:19
$type
$url