LTI Integration Library 4.10.3
PHP class library for building LTI integrations
 
Loading...
Searching...
No Matches
Service/LineItem.php
1<?php
2
3namespace ceLTIc\LTI\Service;
4
5use ceLTIc\LTI;
7
16{
17
21 const MEDIA_TYPE_LINE_ITEM = 'application/vnd.ims.lis.v2.lineitem+json';
22
26 const MEDIA_TYPE_LINE_ITEMS = 'application/vnd.ims.lis.v2.lineitemcontainer+json';
27
31 public static $SCOPE = 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem';
32
36 public static $SCOPE_READONLY = 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem.readonly';
37
41 public static $defaultLimit = null;
42
50 private $limit;
51
59 private $pagingMode;
60
69 public function __construct($platform, $endpoint, $limit = null, $pagingMode = false)
70 {
71 parent::__construct($platform, $endpoint);
72 $this->limit = $limit;
73 $this->pagingMode = $pagingMode;
74 $this->scope = self::$SCOPE;
75 }
76
91 public function getAll($ltiResourceLinkId = null, $resourceId = null, $tag = null, $limit = null)
92 {
93 $params = array();
94 if (!empty($ltiResourceLinkId)) {
95 $params['resource_link_id'] = $ltiResourceLinkId;
96 }
97 if (!empty($resourceId)) {
98 $params['resource_id'] = $resourceId;
99 }
100 if (!empty($tag)) {
101 $params['tag'] = $tag;
102 }
103 if (is_null($limit)) {
104 $limit = $this->limit;
105 }
106 if (is_null($limit)) {
107 $limit = self::$defaultLimit;
108 }
109 if (!empty($limit)) {
110 $params['limit'] = $limit;
111 }
112 $lineItems = array();
114 do {
115 $this->scope = self::$SCOPE_READONLY;
116 $this->mediaType = self::MEDIA_TYPE_LINE_ITEMS;
117 $http = $this->send('GET', $params);
118 $this->scope = self::$SCOPE;
119 $url = '';
120 if ($http->ok) {
121 if (!empty($http->responseJson)) {
122 foreach ($http->responseJson as $lineItem) {
123 $lineItems[] = self::toLineItem($this->getPlatform(), $lineItem);
124 }
125 }
126 if (!$this->pagingMode && $http->hasRelativeLink('next')) {
127 $url = $http->getRelativeLink('next');
128 $this->endpoint = $url;
129 $params = array();
130 }
131 } else {
132 $lineItems = false;
133 }
134 } while ($url);
135 $this->endpoint = $endpoint;
136
137 return $lineItems;
138 }
139
147 public function createLineItem($lineItem)
148 {
149 $lineItem->endpoint = null;
150 $this->mediaType = self::MEDIA_TYPE_LINE_ITEM;
151 $http = $this->send('POST', null, self::toJson($lineItem));
152 $ok = $http->ok && !empty($http->responseJson);
153 if ($ok) {
154 $newLineItem = self::toLineItem($this->getPlatform(), $http->responseJson);
155 foreach (get_object_vars($newLineItem) as $key => $value) {
156 $lineItem->$key = $value;
157 }
158 }
159
160 return $ok;
161 }
162
170 public function saveLineItem($lineItem)
171 {
172 $this->mediaType = self::MEDIA_TYPE_LINE_ITEM;
173 $http = $this->send('PUT', null, self::toJson($lineItem));
174 $ok = $http->ok;
175 if ($ok && !empty($http->responseJson)) {
176 $savedLineItem = self::toLineItem($this->getPlatform(), $http->responseJson);
177 foreach (get_object_vars($savedLineItem) as $key => $value) {
178 $lineItem->$key = $value;
179 }
180 }
181
182 return $ok;
183 }
184
192 public function deleteLineItem($lineItem)
193 {
194 $this->mediaType = self::MEDIA_TYPE_LINE_ITEM;
195 $http = $this->send('DELETE');
196
197 return $http->ok;
198 }
199
208 public static function getLineItem($platform, $endpoint)
209 {
210 $service = new self($platform, $endpoint);
211 $service->scope = self::$SCOPE_READONLY;
212 $service->mediaType = self::MEDIA_TYPE_LINE_ITEM;
213 $http = $service->send('GET');
214 $service->scope = self::$SCOPE;
215 if ($http->ok && !empty($http->responseJson)) {
216 $lineItem = self::toLineItem($platform, $http->responseJson);
217 } else {
218 $lineItem = false;
219 }
220
221 return $lineItem;
222 }
223
224###
225### PRIVATE METHODS
226###
227
236 private static function toLineItem($platform, $json)
237 {
238 if (!empty($json->id) && !empty($json->label) && !empty($json->scoreMaximum)) {
239 $lineItem = new LTI\LineItem($platform, $json->label, $json->scoreMaximum);
240 if (!empty($json->id)) {
241 $lineItem->endpoint = $json->id;
242 }
243 if (!empty($json->resourceLinkId)) {
244 $lineItem->ltiResourceLinkId = $json->resourceLinkId;
245 }
246 if (!empty($json->resourceId)) {
247 $lineItem->resourceId = $json->resourceId;
248 }
249 if (!empty($json->tag)) {
250 $lineItem->tag = $json->tag;
251 }
252 if (!empty($json->startDateTime)) {
253 $lineItem->submitFrom = strtotime($json->startDateTime);
254 }
255 if (!empty($json->endDateTime)) {
256 $lineItem->submitUntil = strtotime($json->endDateTime);
257 }
258 if (!empty($json->submissionReview)) {
259 $lineItem->submissionReview = LTI\SubmissionReview::fromJsonObject($json->submissionReview);
260 }
261 } else {
262 $lineItem = null;
263 }
264
265 return $lineItem;
266 }
267
275 private static function toJson($lineItem)
276 {
277 $json = new \stdClass();
278 if (!empty($lineItem->endpoint)) {
279 $json->id = $lineItem->endpoint;
280 }
281 if (!empty($lineItem->label)) {
282 $json->label = $lineItem->label;
283 }
284 if (!empty($lineItem->pointsPossible)) {
285 $json->scoreMaximum = $lineItem->pointsPossible;
286 }
287 if (!empty($lineItem->ltiResourceLinkId)) {
288 $json->resourceLinkId = $lineItem->ltiResourceLinkId;
289 }
290 if (!empty($lineItem->resourceId)) {
291 $json->resourceId = $lineItem->resourceId;
292 }
293 if (!empty($lineItem->tag)) {
294 $json->tag = $lineItem->tag;
295 }
296 if (!empty($lineItem->submitFrom)) {
297 $json->startDateTime = date('Y-m-d\TH:i:sP', $lineItem->submitFrom);
298 }
299 if (!empty($lineItem->submitUntil)) {
300 $json->endDateTime = date('Y-m-d\TH:i:sP', $lineItem->submitUntil);
301 }
302 if (!empty($lineItem->submissionReview)) {
303 $json->submissionReview = $lineItem->submissionReview->toJsonObject();
304 }
305
306 return json_encode($json);
307 }
308
309}
Class to represent a line-item.
Definition LineItem.php:15
$endpoint
Line-item endpoint.
Definition LineItem.php:71
$ltiResourceLinkId
LTI Resource Link ID with which the line item is associated.
Definition LineItem.php:36
$resourceId
Tool resource ID associated with the line-item.
Definition LineItem.php:43
getPlatform()
Get platform.
Definition LineItem.php:106
Class to represent a platform.
Definition Platform.php:18
Class to implement the Assignment and Grade services.
const MEDIA_TYPE_LINE_ITEMS
Line-item container media type.
static $defaultLimit
Default limit on size of container to be returned from requests.
getAll($ltiResourceLinkId=null, $resourceId=null, $tag=null, $limit=null)
Retrieve all line-items.
__construct($platform, $endpoint, $limit=null, $pagingMode=false)
Class constructor.
static $SCOPE_READONLY
Read-only access scope.
static getLineItem($platform, $endpoint)
Retrieve a line item.
const MEDIA_TYPE_LINE_ITEM
Line-item media type.
deleteLineItem($lineItem)
Delete a line-item.
saveLineItem($lineItem)
Save a line-item.
createLineItem($lineItem)
Create a new line-item.
Class to implement a service.
Definition Service.php:19
send($method, $parameters=array(), $body=null)
Send a service request.
Definition Service.php:119