ILIAS  release_8 Revision v8.24
LineItem.php
Go to the documentation of this file.
1<?php
2
20
23
32{
36 public const MEDIA_TYPE_LINE_ITEM = 'application/vnd.ims.lis.v2.lineitem+json';
37
41 public const MEDIA_TYPE_LINE_ITEMS = 'application/vnd.ims.lis.v2.lineitemcontainer+json';
42
46 public static string $SCOPE = 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem';
47
51 public static string $SCOPE_READONLY = 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem.readonly';
52
56 public static ?int $defaultLimit = null; //UK added ?int
57
65 private ?int $limit;
66
74 private bool $pagingMode;
75
83 public function __construct(Platform $platform, string $endpoint, int $limit = null, bool $pagingMode = false)
84 {
86 $this->limit = $limit;
87 $this->pagingMode = $pagingMode;
88 $this->scope = self::$SCOPE;
89 }
90
102 public function getAll(string $ltiResourceLinkId = null, string $resourceId = null, string $tag = null, int $limit = null)
103 {
104 $params = array();
105 if (!empty($ltiResourceLinkId)) {
106 $params['resource_link_id'] = $ltiResourceLinkId;
107 }
108 if (!empty($resourceId)) {
109 $params['resource_id'] = $resourceId;
110 }
111 if (!empty($tag)) {
112 $params['tag'] = $tag;
113 }
114 if (is_null($limit)) {
115 $limit = $this->limit;
116 }
117 if (is_null($limit)) {
118 $limit = self::$defaultLimit;
119 }
120 if (!empty($limit)) {
121 $params['limit'] = $limit;
122 }
123 $lineItems = array();
125 do {
126 $this->scope = self::$SCOPE_READONLY;
127 $this->mediaType = self::MEDIA_TYPE_LINE_ITEMS;
128 $http = $this->send('GET', $params);
129 $this->scope = self::$SCOPE;
130 $url = '';
131 if ($http->ok) {
132 if (!empty($http->responseJson)) {
133 foreach ($http->responseJson as $lineItem) {
134 $lineItems[] = self::toLineItem($this->getPlatform(), $lineItem);
135 }
136 }
137 if (!$this->pagingMode && $http->hasRelativeLink('next')) {
138 $url = $http->getRelativeLink('next');
139 $this->endpoint = $url;
140 $params = array();
141 }
142 } else {
143 $lineItems = false;
144 }
145 } while ($url);
146 $this->endpoint = $endpoint;
147
148 return $lineItems;
149 }
150
156 public function createLineItem(ToolProvider\LineItem $lineItem): bool
157 {
158 $lineItem->endpoint = null;
159 $this->mediaType = self::MEDIA_TYPE_LINE_ITEM;
160 $http = $this->send('POST', null, self::toJson($lineItem));
161 $ok = $http->ok && !empty($http->responseJson);
162 if ($ok) {
163 $newLineItem = self::toLineItem($this->getPlatform(), $http->responseJson);
164 foreach (get_object_vars($newLineItem) as $key => $value) {
165 $lineItem->$key = $value;
166 }
167 }
168
169 return $ok;
170 }
171
177 public function saveLineItem(ToolProvider\LineItem $lineItem): bool
178 {
179 $this->mediaType = self::MEDIA_TYPE_LINE_ITEM;
180 $http = $this->send('PUT', null, self::toJson($lineItem));
181 $ok = $http->ok;
182 if ($ok && !empty($http->responseJson)) {
183 $savedLineItem = self::toLineItem($this->getPlatform(), $http->responseJson);
184 foreach (get_object_vars($savedLineItem) as $key => $value) {
185 $lineItem->$key = $value;
186 }
187 }
188
189 return $ok;
190 }
191
197 public function deleteLineItem(ToolProvider\LineItem $lineItem): bool
198 {
199 $this->mediaType = self::MEDIA_TYPE_LINE_ITEM;
200 $http = $this->send('DELETE');
201
202 return $http->ok;
203 }
204
211 public static function getLineItem(Platform $platform, string $endpoint)
212 {
213 $service = new self($platform, $endpoint);
214 $service->scope = self::$SCOPE_READONLY;
215 $service->mediaType = self::MEDIA_TYPE_LINE_ITEM;
216 $http = $service->send('GET');
217 $service->scope = self::$SCOPE;
218 if ($http->ok && !empty($http->responseJson)) {
219 $lineItem = self::toLineItem($platform, $http->responseJson);
220 } else {
221 $lineItem = false;
222 }
223
224 return $lineItem;
225 }
226
227 ###
228 ### PRIVATE METHODS
229 ###
230
237 private static function toLineItem(Platform $platform, object $json): ?ToolProvider\LineItem
238 {
239 if (!empty($json->id) && !empty($json->label) && !empty($json->scoreMaximum)) {
240// $lineItem = new LTI\LineItem($platform, $json->label, $json->scoreMaximum);
241 $lineItem = new \ILIAS\LTI\ToolProvider\LineItem($platform, $json->label, $json->scoreMaximum);
242 if (!empty($json->id)) {
243 $lineItem->endpoint = $json->id;
244 }
245 if (!empty($json->resourceLinkId)) {
246 $lineItem->ltiResourceLinkId = $json->resourceLinkId;
247 }
248 if (!empty($json->resourceId)) {
249 $lineItem->resourceId = $json->resourceId;
250 }
251 if (!empty($json->tag)) {
252 $lineItem->tag = $json->tag;
253 }
254 if (!empty($json->startDateTime)) {
255 $lineItem->submitFrom = strtotime($json->startDateTime);
256 }
257 if (!empty($json->endDateTime)) {
258 $lineItem->submitUntil = strtotime($json->endDateTime);
259 }
260 } else {
261 $lineItem = null;
262 }
263
264 return $lineItem;
265 }
266
272 private static function toJson(ToolProvider\LineItem $lineItem): string
273 {
274 $json = new \stdClass();
275 if (!empty($lineItem->endpoint)) {
276 $json->id = $lineItem->endpoint;
277 }
278 if (!empty($lineItem->label)) {
279 $json->label = $lineItem->label;
280 }
281 if (!empty($lineItem->pointsPossible)) {
282 $json->scoreMaximum = $lineItem->pointsPossible;
283 }
284 if (!empty($lineItem->ltiResourceLinkId)) {
285 $json->resourceLinkId = $lineItem->ltiResourceLinkId;
286 }
287 if (!empty($lineItem->resourceId)) {
288 $json->resourceId = $lineItem->resourceId;
289 }
290 if (!empty($lineItem->tag)) {
291 $json->tag = $lineItem->tag;
292 }
293 if (!empty($lineItem->submitFrom)) {
294 $json->startDateTime = date('Y-m-d\TH:i:sP', $lineItem->submitFrom);
295 }
296 if (!empty($lineItem->submitUntil)) {
297 $json->endDateTime = date('Y-m-d\TH:i:sP', $lineItem->submitUntil);
298 }
299
300 return json_encode($json);
301 }
302}
Class to represent a line item.
Definition: LineItem.php:31
Platform $platform
Platform for this line item.
Definition: LineItem.php:93
string $ltiResourceLinkId
LTI Resource Link ID with which the line item is associated.
Definition: LineItem.php:51
string $endpoint
Line item endpoint.
Definition: LineItem.php:86
string $resourceId
Tool resource ID associated with the line item.
Definition: LineItem.php:58
Class to represent a platform.
Definition: Platform.php:36
Class to implement the Assignment and Grade services.
static int $defaultLimit
Default limit on size of container to be returned from requests.
Definition: LineItem.php:56
const MEDIA_TYPE_LINE_ITEM
Line item media type.
Definition: LineItem.php:36
saveLineItem(ToolProvider\LineItem $lineItem)
Save a line item.
Definition: LineItem.php:177
getAll(string $ltiResourceLinkId=null, string $resourceId=null, string $tag=null, int $limit=null)
Retrieve all line items.
Definition: LineItem.php:102
__construct(Platform $platform, string $endpoint, int $limit=null, bool $pagingMode=false)
Class constructor.
Definition: LineItem.php:83
static string $SCOPE_READONLY
Read-only access scope.
Definition: LineItem.php:51
deleteLineItem(ToolProvider\LineItem $lineItem)
Delete a line item.
Definition: LineItem.php:197
static toLineItem(Platform $platform, object $json)
Create a line item from a JSON object.
Definition: LineItem.php:237
const MEDIA_TYPE_LINE_ITEMS
Line item container media type.
Definition: LineItem.php:41
int $limit
Limit on size of container to be returned from requests.
Definition: LineItem.php:65
static getLineItem(Platform $platform, string $endpoint)
Retrieve a line item.
Definition: LineItem.php:211
bool $pagingMode
Whether requests should be made one page at a time when a limit is set.
Definition: LineItem.php:74
static toJson(ToolProvider\LineItem $lineItem)
Create a JSON string from a line item.
Definition: LineItem.php:272
createLineItem(ToolProvider\LineItem $lineItem)
Create a new line item.
Definition: LineItem.php:156
static string $SCOPE
Access scope.
Definition: LineItem.php:46
HTTPMessage $http
HttpMessage object for last service request.
Definition: Service.php:75
send(string $method, array $parameters=array(), string $body=null)
Send a service request.
Definition: Service.php:130
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:33
$service
Definition: ltiservices.php:43
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: AccessToken.php:19
string $key
Consumer key/client ID value.
Definition: System.php:193
$url