Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  
   3  namespace IMSGlobal\LTI\ToolProvider;
   4  
   5  /**
   6   * Class to represent a content-item object
   7   *
   8   * @author  Stephen P Vickers <svickers@imsglobal.org>
   9   * @copyright  IMS Global Learning Consortium Inc
  10   * @date  2016
  11   * @version 3.0.2
  12   * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  13   */
  14  class ContentItem
  15  {
  16  
  17  /**
  18   * Media type for LTI launch links.
  19   */
  20      const LTI_LINK_MEDIA_TYPE = 'application/vnd.ims.lti.v1.ltilink';
  21  
  22  /**
  23   * Class constructor.
  24   *
  25   * @param string $type Class type of content-item
  26   * @param ContentItemPlacement $placementAdvice  Placement object for item (optional)
  27   * @param string $id   URL of content-item (optional)
  28   */
  29      function __construct($type, $placementAdvice = null, $id = null)
  30      {
  31  
  32          $this->{'@type'} = $type;
  33          if (is_object($placementAdvice) && (count(get_object_vars($placementAdvice)) > 0)) {
  34              $this->placementAdvice = $placementAdvice;
  35          }
  36          if (!empty($id)) {
  37              $this->{'@id'} = $id;
  38          }
  39  
  40      }
  41  
  42  /**
  43   * Set a URL value for the content-item.
  44   *
  45   * @param string $url  URL value
  46   */
  47      public function setUrl($url)
  48      {
  49  
  50          if (!empty($url)) {
  51              $this->url = $url;
  52          } else {
  53              unset($this->url);
  54          }
  55  
  56      }
  57  
  58  /**
  59   * Set a media type value for the content-item.
  60   *
  61   * @param string $mediaType  Media type value
  62   */
  63      public function setMediaType($mediaType)
  64      {
  65  
  66          if (!empty($mediaType)) {
  67              $this->mediaType = $mediaType;
  68          } else {
  69              unset($this->mediaType);
  70          }
  71  
  72      }
  73  
  74  /**
  75   * Set a title value for the content-item.
  76   *
  77   * @param string $title  Title value
  78   */
  79      public function setTitle($title)
  80      {
  81  
  82          if (!empty($title)) {
  83              $this->title = $title;
  84          } else if (isset($this->title)) {
  85              unset($this->title);
  86          }
  87  
  88      }
  89  
  90  /**
  91   * Set a link text value for the content-item.
  92   *
  93   * @param string $text  Link text value
  94   */
  95      public function setText($text)
  96      {
  97  
  98          if (!empty($text)) {
  99              $this->text = $text;
 100          } else if (isset($this->text)) {
 101              unset($this->text);
 102          }
 103  
 104      }
 105  
 106  /**
 107   * Wrap the content items to form a complete application/vnd.ims.lti.v1.contentitems+json media type instance.
 108   *
 109   * @param mixed $items An array of content items or a single item
 110   * @return string
 111   */
 112      public static function toJson($items)
 113      {
 114  /*
 115          $data = array();
 116          if (!is_array($items)) {
 117              $data[] = json_encode($items);
 118          } else {
 119              foreach ($items as $item) {
 120                  $data[] = json_encode($item);
 121              }
 122          }
 123          $json = '{ "@context" : "http://purl.imsglobal.org/ctx/lti/v1/ContentItem", "@graph" : [' . implode(", ", $data) . '] }';
 124  */
 125          $obj = new \stdClass();
 126          $obj->{'@context'} = 'http://purl.imsglobal.org/ctx/lti/v1/ContentItem';
 127          if (!is_array($items)) {
 128            $obj->{'@graph'} = array();
 129            $obj->{'@graph'}[] = $items;
 130          } else {
 131            $obj->{'@graph'} = $items;
 132          }
 133  
 134          return json_encode($obj);
 135  
 136      }
 137  
 138  }