3namespace ceLTIc\LTI\Content;
76 private $placements = array();
90 private $mediaType =
null;
97 private $title =
null;
104 private $text =
null;
111 private $html =
null;
118 private $icon =
null;
125 private $thumbnail =
null;
132 private $hideOnCreate =
null;
144 if (!empty($placementAdvices)) {
145 if (!is_array($placementAdvices)) {
146 $placementAdvices = array($placementAdvices);
148 foreach ($placementAdvices as $placementAdvice) {
149 $this->placements[$placementAdvice->documentTarget] = $placementAdvice;
172 $this->mediaType = $mediaType;
182 $this->title = $title;
212 if (!empty($placementAdvice)) {
213 $this->placements[$placementAdvice->documentTarget] = $placementAdvice;
234 $this->thumbnail = $thumbnail;
244 $this->hideOnCreate = $hideOnCreate;
257 if (!is_array($items)) {
258 $items = array($items);
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();
269 foreach ($items as $item) {
270 $obj[] = $item->toJsonObject();
274 return json_encode($obj);
286 $isJsonLd = isset($items->{
'@graph'});
288 $items = $items->{
'@graph'};
290 if (!is_array($items)) {
291 $items = array($items);
294 foreach ($items as $item) {
295 $obj = self::fromJsonItem($item);
311 $item = new \stdClass();
312 if (!empty($this->
id)) {
313 $item->{
'@id'} = $this->id;
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';
321 $item->{
'@type'} =
'ContentItem';
324 $item->{
'@type'} =
'ContentItem';
326 if (!empty($this->title)) {
327 $item->title = $this->title;
329 if (!empty($this->text)) {
330 $item->text = $this->text;
331 } elseif (!empty($this->html)) {
332 $item->text = $this->html;
334 if (!empty($this->url)) {
335 $item->url = $this->url;
337 if (!empty($this->mediaType)) {
338 $item->mediaType = $this->mediaType;
340 if (!empty($this->placements)) {
341 $placementAdvice = new \stdClass();
342 $placementAdvices = array();
343 foreach ($this->placements as $placement) {
344 $obj = $placement->toJsonldObject();
346 if (!empty($placement->documentTarget)) {
347 $placementAdvices[] = $placement->documentTarget;
349 $placementAdvice = (object) array_merge((array) $placementAdvice, (array) $obj);
352 if (!empty($placementAdvice)) {
353 $item->placementAdvice = $placementAdvice;
354 if (!empty($placementAdvices)) {
355 $item->placementAdvice->presentationDocumentTarget = implode(
',', $placementAdvices);
359 if (!empty($this->icon)) {
360 $item->icon = $this->icon->toJsonldObject();
362 if (!empty($this->thumbnail)) {
363 $item->thumbnail = $this->thumbnail->toJsonldObject();
365 if (!is_null($this->hideOnCreate)) {
366 $item->hideOnCreate = $this->hideOnCreate;
379 $item = new \stdClass();
380 switch ($this->type) {
382 $item->type = self::TYPE_LTI_LINK;
385 $item->type = self::TYPE_FILE;
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;
393 $item->type = self::TYPE_LINK;
397 $item->type = $this->type;
400 if (!empty($this->title)) {
401 $item->title = $this->title;
403 if (!empty($this->text)) {
404 $item->text = Util::stripHtml($this->text);
406 if (!empty($this->html)) {
407 $item->html = $this->html;
409 if (!empty($this->url)) {
410 $item->url = $this->url;
412 foreach ($this->placements as $type => $placement) {
414 case Placement::TYPE_EMBED:
415 case Placement::TYPE_IFRAME:
416 case Placement::TYPE_WINDOW:
417 case Placement::TYPE_FRAME:
418 $obj = $placement->toJsonObject();
425 $item->{$type} = $obj;
428 if (!empty($this->icon)) {
429 $item->icon = $this->icon->toJsonObject();
431 if (!empty($this->thumbnail)) {
432 $item->thumbnail = $this->thumbnail->toJsonObject();
434 if (!is_null($this->hideOnCreate)) {
435 $item->hideOnCreate = $this->hideOnCreate;
452 if (isset($item->{
'@type'})) {
453 if (isset($item->presentationDocumentTarget)) {
454 $placement = Placement::fromJsonObject($item, $item->presentationDocumentTarget);
456 switch ($item->{
'@type'}) {
458 $obj =
new Item(
'ContentItem', $placement);
461 $obj =
new LtiLinkItem($placement);
464 $obj =
new FileItem($placement);
467 } elseif (isset($item->type)) {
468 $placements = array();
469 $placement = Placement::fromJsonObject($item,
'embed');
470 if (!empty($placement)) {
471 $placements[] = $placement;
473 $placement = Placement::fromJsonObject($item,
'iframe');
474 if (!empty($placement)) {
475 $placements[] = $placement;
477 $placement = Placement::fromJsonObject($item,
'window');
478 if (!empty($placement)) {
479 $placements[] = $placement;
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);
487 case self::TYPE_LTI_LINK:
488 $obj =
new LtiLinkItem($placements);
490 case self::TYPE_LTI_ASSIGNMENT:
491 $obj =
new LtiAssignmentItem($placements);
493 case self::TYPE_FILE:
494 $obj =
new FileItem($placements);
499 $obj->fromJsonObject($item);
512 if (isset($item->{
'@id'})) {
513 $this->
id = $item->{
'@id'};
515 foreach (get_object_vars($item) as $name => $value) {
523 $this->{$name} = $item->{$name};
525 case 'placementAdvice':
526 $this->addPlacementAdvice(Placement::fromJsonObject($item));
531 $this->addPlacementAdvice(Placement::fromJsonObject($item, $name));
535 $this->{$name} = Image::fromJsonObject($item->{$name});
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.
const LTI_VERSION1P3
LTI version 1.3 for messages.
const LTI_VERSION1
LTI version 1 for messages.