LTI Integration Library 4.10.3
PHP class library for building LTI integrations
 
Loading...
Searching...
No Matches
Content/Item.php
1<?php
2
3namespace ceLTIc\LTI\Content;
4
6
14class Item
15{
16
20 const TYPE_LINK = 'link';
21
25 const TYPE_LTI_LINK = 'ltiResourceLink';
26
30 const TYPE_LTI_ASSIGNMENT = 'ltiAssignment';
31
35 const TYPE_FILE = 'file';
36
40 const TYPE_HTML = 'html';
41
45 const TYPE_IMAGE = 'image';
46
50 const LTI_LINK_MEDIA_TYPE = 'application/vnd.ims.lti.v1.ltilink';
51
55 const LTI_ASSIGNMENT_MEDIA_TYPE = 'application/vnd.ims.lti.v1.ltiassignment';
56
62 private $type = null;
63
69 private $id = null;
70
76 private $placements = array();
77
83 private $url = null;
84
90 private $mediaType = null;
91
97 private $title = null;
98
104 private $text = null;
105
111 private $html = null;
112
118 private $icon = null;
119
125 private $thumbnail = null;
126
132 private $hideOnCreate = null;
133
141 function __construct($type, $placementAdvices = null, $id = null)
142 {
143 $this->type = $type;
144 if (!empty($placementAdvices)) {
145 if (!is_array($placementAdvices)) {
146 $placementAdvices = array($placementAdvices);
147 }
148 foreach ($placementAdvices as $placementAdvice) {
149 $this->placements[$placementAdvice->documentTarget] = $placementAdvice;
150 }
151 }
152 $this->id = $id;
153 }
154
160 public function setUrl($url)
161 {
162 $this->url = $url;
163 }
164
170 public function setMediaType($mediaType)
171 {
172 $this->mediaType = $mediaType;
173 }
174
180 public function setTitle($title)
181 {
182 $this->title = $title;
183 }
184
190 public function setText($text)
191 {
192 $this->text = $text;
193 }
194
200 public function setHtml($html)
201 {
202 $this->html = $html;
203 }
204
210 public function addPlacementAdvice($placementAdvice)
211 {
212 if (!empty($placementAdvice)) {
213 $this->placements[$placementAdvice->documentTarget] = $placementAdvice;
214 }
215 }
216
222 public function setIcon($icon)
223 {
224 $this->icon = $icon;
225 }
226
232 public function setThumbnail($thumbnail)
233 {
234 $this->thumbnail = $thumbnail;
235 }
236
242 public function setHideOnCreate($hideOnCreate)
243 {
244 $this->hideOnCreate = $hideOnCreate;
245 }
246
255 public static function toJson($items, $ltiVersion = Util::LTI_VERSION1)
256 {
257 if (!is_array($items)) {
258 $items = array($items);
259 }
260 if ($ltiVersion !== Util::LTI_VERSION1P3) {
261 $obj = new \stdClass();
262 $obj->{'@context'} = 'http://purl.imsglobal.org/ctx/lti/v1/ContentItem';
263 $obj->{'@graph'} = array();
264 foreach ($items as $item) {
265 $obj->{'@graph'}[] = $item->toJsonldObject();
266 }
267 } else {
268 $obj = array();
269 foreach ($items as $item) {
270 $obj[] = $item->toJsonObject();
271 }
272 }
273
274 return json_encode($obj);
275 }
276
284 public static function fromJson($items)
285 {
286 $isJsonLd = isset($items->{'@graph'});
287 if ($isJsonLd) {
288 $items = $items->{'@graph'};
289 }
290 if (!is_array($items)) {
291 $items = array($items);
292 }
293 $objs = array();
294 foreach ($items as $item) {
295 $obj = self::fromJsonItem($item);
296 if (!empty($obj)) {
297 $objs[] = $obj;
298 }
299 }
300
301 return $objs;
302 }
303
309 protected function toJsonldObject()
310 {
311 $item = new \stdClass();
312 if (!empty($this->id)) {
313 $item->{'@id'} = $this->id;
314 }
315 if (!empty($this->type)) {
316 if (($this->type === self::TYPE_LTI_LINK) || ($this->type === self::TYPE_LTI_ASSIGNMENT)) {
317 $item->{'@type'} = 'LtiLinkItem';
318 } elseif ($this->type === self::TYPE_FILE) {
319 $item->{'@type'} = 'FileItem';
320 } else {
321 $item->{'@type'} = 'ContentItem';
322 }
323 } else {
324 $item->{'@type'} = 'ContentItem';
325 }
326 if (!empty($this->title)) {
327 $item->title = $this->title;
328 }
329 if (!empty($this->text)) {
330 $item->text = $this->text;
331 } elseif (!empty($this->html)) {
332 $item->text = $this->html;
333 }
334 if (!empty($this->url)) {
335 $item->url = $this->url;
336 }
337 if (!empty($this->mediaType)) {
338 $item->mediaType = $this->mediaType;
339 }
340 if (!empty($this->placements)) {
341 $placementAdvice = new \stdClass();
342 $placementAdvices = array();
343 foreach ($this->placements as $placement) {
344 $obj = $placement->toJsonldObject();
345 if (!empty($obj)) {
346 if (!empty($placement->documentTarget)) {
347 $placementAdvices[] = $placement->documentTarget;
348 }
349 $placementAdvice = (object) array_merge((array) $placementAdvice, (array) $obj);
350 }
351 }
352 if (!empty($placementAdvice)) {
353 $item->placementAdvice = $placementAdvice;
354 if (!empty($placementAdvices)) {
355 $item->placementAdvice->presentationDocumentTarget = implode(',', $placementAdvices);
356 }
357 }
358 }
359 if (!empty($this->icon)) {
360 $item->icon = $this->icon->toJsonldObject();
361 }
362 if (!empty($this->thumbnail)) {
363 $item->thumbnail = $this->thumbnail->toJsonldObject();
364 }
365 if (!is_null($this->hideOnCreate)) {
366 $item->hideOnCreate = $this->hideOnCreate;
367 }
368
369 return $item;
370 }
371
377 protected function toJsonObject()
378 {
379 $item = new \stdClass();
380 switch ($this->type) {
381 case 'LtiLinkItem':
382 $item->type = self::TYPE_LTI_LINK;
383 break;
384 case 'FileItem':
385 $item->type = self::TYPE_FILE;
386 break;
387 case 'ContentItem':
388 if (empty($this->url)) {
389 $item->type = self::TYPE_HTML;
390 } elseif (!empty($this->mediaType) && (strpos($this->mediaType, 'image') === 0)) {
391 $item->type = self::TYPE_IMAGE;
392 } else {
393 $item->type = self::TYPE_LINK;
394 }
395 break;
396 default:
397 $item->type = $this->type;
398 break;
399 }
400 if (!empty($this->title)) {
401 $item->title = $this->title;
402 }
403 if (!empty($this->text)) {
404 $item->text = Util::stripHtml($this->text);
405 }
406 if (!empty($this->html)) {
407 $item->html = $this->html;
408 }
409 if (!empty($this->url)) {
410 $item->url = $this->url;
411 }
412 foreach ($this->placements as $type => $placement) {
413 switch ($type) {
414 case Placement::TYPE_EMBED:
415 case Placement::TYPE_IFRAME:
416 case Placement::TYPE_WINDOW:
417 case Placement::TYPE_FRAME:
418 $obj = $placement->toJsonObject();
419 break;
420 default:
421 $obj = null;
422 break;
423 }
424 if (!empty($obj)) {
425 $item->{$type} = $obj;
426 }
427 }
428 if (!empty($this->icon)) {
429 $item->icon = $this->icon->toJsonObject();
430 }
431 if (!empty($this->thumbnail)) {
432 $item->thumbnail = $this->thumbnail->toJsonObject();
433 }
434 if (!is_null($this->hideOnCreate)) {
435 $item->hideOnCreate = $this->hideOnCreate;
436 }
437
438 return $item;
439 }
440
448 public static function fromJsonItem($item)
449 {
450 $obj = null;
451 $placement = null;
452 if (isset($item->{'@type'})) {
453 if (isset($item->presentationDocumentTarget)) {
454 $placement = Placement::fromJsonObject($item, $item->presentationDocumentTarget);
455 }
456 switch ($item->{'@type'}) {
457 case 'ContentItem':
458 $obj = new Item('ContentItem', $placement);
459 break;
460 case 'LtiLinkItem':
461 $obj = new LtiLinkItem($placement);
462 break;
463 case 'FileItem':
464 $obj = new FileItem($placement);
465 break;
466 }
467 } elseif (isset($item->type)) {
468 $placements = array();
469 $placement = Placement::fromJsonObject($item, 'embed');
470 if (!empty($placement)) {
471 $placements[] = $placement;
472 }
473 $placement = Placement::fromJsonObject($item, 'iframe');
474 if (!empty($placement)) {
475 $placements[] = $placement;
476 }
477 $placement = Placement::fromJsonObject($item, 'window');
478 if (!empty($placement)) {
479 $placements[] = $placement;
480 }
481 switch ($item->type) {
482 case self::TYPE_LINK:
483 case self::TYPE_HTML:
484 case self::TYPE_IMAGE:
485 $obj = new Item($item->type, $placements);
486 break;
487 case self::TYPE_LTI_LINK:
488 $obj = new LtiLinkItem($placements);
489 break;
490 case self::TYPE_LTI_ASSIGNMENT:
491 $obj = new LtiAssignmentItem($placements);
492 break;
493 case self::TYPE_FILE:
494 $obj = new FileItem($placements);
495 break;
496 }
497 }
498 if (!empty($obj)) {
499 $obj->fromJsonObject($item);
500 }
501
502 return $obj;
503 }
504
510 protected function fromJsonObject($item)
511 {
512 if (isset($item->{'@id'})) {
513 $this->id = $item->{'@id'};
514 }
515 foreach (get_object_vars($item) as $name => $value) {
516 switch ($name) {
517 case 'title':
518 case 'text':
519 case 'html':
520 case 'url':
521 case 'mediaType':
522 case 'hideOnCreate':
523 $this->{$name} = $item->{$name};
524 break;
525 case 'placementAdvice':
526 $this->addPlacementAdvice(Placement::fromJsonObject($item));
527 break;
528 case 'embed':
529 case 'window':
530 case 'iframe':
531 $this->addPlacementAdvice(Placement::fromJsonObject($item, $name));
532 break;
533 case 'icon':
534 case 'thumbnail':
535 $this->{$name} = Image::fromJsonObject($item->{$name});
536 break;
537 }
538 }
539 }
540
541}
Class to represent a content-item object.
static fromJson($items)
Generate an array of Item objects from their JSON representation.
setHtml($html)
Set an HTML embed value for the content-item.
addPlacementAdvice($placementAdvice)
Add a placement for the content-item.
setUrl($url)
Set a URL value for the content-item.
setIcon($icon)
Set an icon image for the content-item.
const TYPE_LINK
Type for link content-item.
static toJson($items, $ltiVersion=Util::LTI_VERSION1)
Wrap the content items to form a complete application/vnd.ims.lti.v1.contentitems+json media type ins...
static fromJsonItem($item)
Generate an Item object from its JSON or JSON-LD representation.
const TYPE_LTI_LINK
Type for LTI link content-item.
const TYPE_FILE
Type for file content-item.
const TYPE_IMAGE
Type for image content-item.
const LTI_LINK_MEDIA_TYPE
Media type for LTI launch links.
__construct($type, $placementAdvices=null, $id=null)
Class constructor.
toJsonObject()
Wrap the content items to form a complete value for the https://purl.imsglobal.org/spec/lti-dl/claim/...
setTitle($title)
Set a title value for the content-item.
fromJsonObject($item)
Extract content-item details from its JSON representation.
setThumbnail($thumbnail)
Set a thumbnail image for the content-item.
const TYPE_LTI_ASSIGNMENT
Type for LTI assignment content-item.
setHideOnCreate($hideOnCreate)
Set whether the content-item should be hidden from learners by default.
const TYPE_HTML
Type for HTML content-item.
toJsonldObject()
Wrap the content item to form an item complying with the application/vnd.ims.lti.v1....
const LTI_ASSIGNMENT_MEDIA_TYPE
Media type for LTI assignment links.
setMediaType($mediaType)
Set a media type value for the content-item.
setText($text)
Set a link text value for the content-item.
Class to implement utility methods.
Definition Util.php:15
const LTI_VERSION1P3
LTI version 1.3 for messages.
Definition Util.php:25
const LTI_VERSION1
LTI version 1 for messages.
Definition Util.php:20