Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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