1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
<?php
require_once "edu-sharing-helper-abstract.php";
class DisplayMode {
const Inline = 'inline';
const Embed = 'embed';
const Dynamic = 'dynamic';
}
class Usage {
public $nodeId;
public $nodeVersion;
public $containerId;
public $resourceId;
public $usageId;
public function __construct($nodeId, $nodeVersion, $containerId, $resourceId, $usageId)
{
$this->nodeId = $nodeId;
$this->nodeVersion = $nodeVersion;
$this->containerId = $containerId;
$this->resourceId = $resourceId;
$this->usageId = $usageId;
}
}
class EduSharingNodeHelper extends EduSharingHelperAbstract {
/**
* creates a usage for a given node
* The given usage can later be used to fetch this node REGARDLESS of the actual user
* The usage gives permanent access to this node and acts similar to a license
* In order to be able to create an usage for a node, the current user (provided via the ticket)
* MUST have CC_PUBLISH permissions on the given node id
* @param string $ticket
* A ticket with the user session who is creating this usage
* @param string $containerId
* A unique page / course id this usage refers to inside your system (e.g. a database id of the page you include the usage)
* @param string $resourceId
* The individual resource id on the current page or course this object refers to
* (you may enumerate or use unique UUID's)
* @param string $nodeId
* The edu-sharing node id the usage shall be created for
* @param string|null $nodeVersion
* Optional: The fixed version this usage should refer to
* If you leave it empty, the usage will always refer to the latest version of the node
* @return Usage
* An usage element you can use with @getNodeByUsage
* Keep all data of this object stored inside your system!
*/
public function createUsage(
string $ticket,
string $containerId,
string $resourceId,
string $nodeId,
string $nodeVersion = null
) {
$curl = curl_init($this->base->baseUrl . '/rest/usage/v1/usages/repository/-home-');
$headers = $this->getSignatureHeaders($ticket);
$headers[] = $this->getRESTAuthenticationHeader($ticket);
curl_setopt_array($curl, [
CURLOPT_FAILONERROR => false,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => json_encode([
'appId' => $this->base->appId,
'courseId' => $containerId,
'resourceId' => $resourceId,
'nodeId' => $nodeId,
'nodeVersion' => $nodeVersion,
]),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers
]);
if ($this->base->http_proxy) {
curl_setopt($curl, CURLOPT_PROXY, $this->base->http_proxy);
}
$data = json_decode(curl_exec($curl), true);
$err = curl_errno( $curl );
$info = curl_getinfo($curl);
curl_close($curl);
if ($err === 0 && $info["http_code"] === 200) {
return new Usage(
$data['parentNodeId'],
$nodeVersion,
$containerId,
$resourceId,
$data['nodeId']
);
} else {
throw new Exception('creating usage failed ' .
$info["http_code"] . ': ' . $data['error'] . ' ' . $data['message']);
}
}
/**
* Loads the edu-sharing node refered by a given usage
* @param Usage $usage
* The usage, as previously returned by @createUsage
* @param string $displayMode
* The displayMode
* This will ONLY change the content representation inside the "detailsSnippet" return value
* @param array $renderingParams
* @return mixed
* Returns an object containing a "detailsSnippet" repesentation
* as well as the full node as provided by the REST API
* Please refer to the edu-sharing REST documentation for more details
* @throws Exception
*/
public function getNodeByUsage(
Usage $usage,
$displayMode = DisplayMode::Inline,
array $renderingParams = null
)
{
$url = $this->base->baseUrl . '/rest/rendering/v1/details/-home-/' . rawurlencode($usage->nodeId);
$url .= '?displayMode=' . rawurlencode($displayMode);
if($usage->nodeVersion) {
$url .= '&version=' . rawurlencode($usage->nodeVersion);
}
$curl = curl_init($url);
$headers = $this->getSignatureHeaders($usage->usageId);
$headers[] = 'X-Edu-Usage-Node-Id: ' . $usage->nodeId;
$headers[] = 'X-Edu-Usage-Course-Id: ' . $usage->containerId;
$headers[] = 'X-Edu-Usage-Resource-Id: ' . $usage->resourceId;
curl_setopt_array($curl, [
CURLOPT_FAILONERROR => false,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => json_encode($renderingParams),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers
]);
if ($this->base->http_proxy) {
curl_setopt($curl, CURLOPT_PROXY, $this->base->http_proxy);
}
$data = json_decode(curl_exec($curl), true);
$err = curl_errno( $curl );
$info = curl_getinfo($curl);
if ($err === 0 && $info["http_code"] === 200) {
return $data;
} else {
throw new Exception('fetching node by usage failed ' .
$info["http_code"] . ': ' . $data['error'] . ' ' . $data['message']);
}
}
/**
* Deletes the given usage
* We trust that you've validated if the current user in your context is allowed to do so
* There is no restriction in deleting usages even from foreign users, as long as they were generated by your app
* Thus, this endpoint does not require any user ticket
* @param string $nodeId
* The edu-sharing node id this usage belongs to
* @param string $usageId
* The usage id
*/
public function deleteUsage(
string $nodeId,
string $usageId
) {
$curl = curl_init($this->base->baseUrl . '/rest/usage/v1/usages/node/' . rawurlencode($nodeId) . '/' . rawurlencode($usageId));
$headers = $this->getSignatureHeaders($nodeId.$usageId);
curl_setopt_array($curl, [
CURLOPT_FAILONERROR => false,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers
]);
if ($this->base->http_proxy) {
curl_setopt($curl, CURLOPT_PROXY, $this->base->http_proxy);
}
$data = json_decode(curl_exec($curl), true);
$err = curl_errno( $curl );
$info = curl_getinfo($curl);
curl_close($curl);
if ($err === 0 && $info["http_code"] === 200) {
} else {
throw new Exception('deleting usage failed ' .
$info["http_code"] . ': ' . $data['error'] . ' ' . $data['message']);
}
}
}
|