ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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  {
85  parent::__construct($platform, $endpoint);
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  //UK changed from $http = $this->send('POST', null, self::toJson($lineItem));
161  $http = $this->send('POST', [], self::toJson($lineItem));
162  $ok = $http->ok && !empty($http->responseJson);
163  if ($ok) {
164  $newLineItem = self::toLineItem($this->getPlatform(), $http->responseJson);
165  foreach (get_object_vars($newLineItem) as $key => $value) {
166  $lineItem->$key = $value;
167  }
168  }
169 
170  return $ok;
171  }
172 
178  public function saveLineItem(ToolProvider\LineItem $lineItem): bool
179  {
180  $this->mediaType = self::MEDIA_TYPE_LINE_ITEM;
181  //UK changed from $http = $this->send('PUT', null, self::toJson($lineItem));
182  $http = $this->send('PUT', [], self::toJson($lineItem));
183  $ok = $http->ok;
184  if ($ok && !empty($http->responseJson)) {
185  $savedLineItem = self::toLineItem($this->getPlatform(), $http->responseJson);
186  foreach (get_object_vars($savedLineItem) as $key => $value) {
187  $lineItem->$key = $value;
188  }
189  }
190 
191  return $ok;
192  }
193 
199  public function deleteLineItem(ToolProvider\LineItem $lineItem): bool
200  {
201  $this->mediaType = self::MEDIA_TYPE_LINE_ITEM;
202  $http = $this->send('DELETE');
203 
204  return $http->ok;
205  }
206 
213  public static function getLineItem(Platform $platform, string $endpoint)
214  {
215  $service = new self($platform, $endpoint);
216  $service->scope = self::$SCOPE_READONLY;
217  $service->mediaType = self::MEDIA_TYPE_LINE_ITEM;
218  $http = $service->send('GET');
219  $service->scope = self::$SCOPE;
220  if ($http->ok && !empty($http->responseJson)) {
221  $lineItem = self::toLineItem($platform, $http->responseJson);
222  } else {
223  $lineItem = false;
224  }
225 
226  return $lineItem;
227  }
228 
229  ###
230  ### PRIVATE METHODS
231  ###
232 
239  private static function toLineItem(Platform $platform, object $json): ?ToolProvider\LineItem
240  {
241  if (!empty($json->id) && !empty($json->label) && !empty($json->scoreMaximum)) {
242  // $lineItem = new LTI\LineItem($platform, $json->label, $json->scoreMaximum);
243  $lineItem = new \ILIAS\LTI\ToolProvider\LineItem($platform, $json->label, $json->scoreMaximum);
244  if (!empty($json->id)) {
245  $lineItem->endpoint = $json->id;
246  }
247  if (!empty($json->resourceLinkId)) {
248  $lineItem->ltiResourceLinkId = $json->resourceLinkId;
249  }
250  if (!empty($json->resourceId)) {
251  $lineItem->resourceId = $json->resourceId;
252  }
253  if (!empty($json->tag)) {
254  $lineItem->tag = $json->tag;
255  }
256  if (!empty($json->startDateTime)) {
257  $lineItem->submitFrom = strtotime($json->startDateTime);
258  }
259  if (!empty($json->endDateTime)) {
260  $lineItem->submitUntil = strtotime($json->endDateTime);
261  }
262  } else {
263  $lineItem = null;
264  }
265 
266  return $lineItem;
267  }
268 
274  private static function toJson(ToolProvider\LineItem $lineItem): string
275  {
276  $json = new \stdClass();
277  if (!empty($lineItem->endpoint)) {
278  $json->id = $lineItem->endpoint;
279  }
280  if (!empty($lineItem->label)) {
281  $json->label = $lineItem->label;
282  }
283  if (!empty($lineItem->pointsPossible)) {
284  $json->scoreMaximum = $lineItem->pointsPossible;
285  }
286  if (!empty($lineItem->ltiResourceLinkId)) {
287  $json->resourceLinkId = $lineItem->ltiResourceLinkId;
288  }
289  if (!empty($lineItem->resourceId)) {
290  $json->resourceId = $lineItem->resourceId;
291  }
292  if (!empty($lineItem->tag)) {
293  $json->tag = $lineItem->tag;
294  }
295  if (!empty($lineItem->submitFrom)) {
296  $json->startDateTime = date('Y-m-d\TH:i:sP', $lineItem->submitFrom);
297  }
298  if (!empty($lineItem->submitUntil)) {
299  $json->endDateTime = date('Y-m-d\TH:i:sP', $lineItem->submitUntil);
300  }
301 
302  return json_encode($json);
303  }
304 }
Class to implement the Assignment and Grade services.
createLineItem(ToolProvider\LineItem $lineItem)
Create a new line item.
Definition: LineItem.php:156
getAll(string $ltiResourceLinkId=null, string $resourceId=null, string $tag=null, int $limit=null)
Retrieve all line items.
Definition: LineItem.php:102
Platform $platform
Platform for this line item.
Definition: LineItem.php:93
Class to represent a platform.
Definition: Platform.php:35
bool $pagingMode
Whether requests should be made one page at a time when a limit is set.
Definition: LineItem.php:74
string $resourceId
Tool resource ID associated with the line item.
Definition: LineItem.php:58
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:33
string $ltiResourceLinkId
LTI Resource Link ID with which the line item is associated.
Definition: LineItem.php:51
Class to represent a line item.
Definition: LineItem.php:30
deleteLineItem(ToolProvider\LineItem $lineItem)
Delete a line item.
Definition: LineItem.php:199
send(string $method, array $parameters=array(), string $body=null)
Send a service request.
Definition: Service.php:130
static toLineItem(Platform $platform, object $json)
Create a line item from a JSON object.
Definition: LineItem.php:239
HTTPMessage $http
HttpMessage object for last service request.
Definition: Service.php:75
static string $SCOPE
Access scope.
Definition: LineItem.php:46
const MEDIA_TYPE_LINE_ITEM
Line item media type.
Definition: LineItem.php:36
static toJson(ToolProvider\LineItem $lineItem)
Create a JSON string from a line item.
Definition: LineItem.php:274
string $tag
Tag value.
Definition: LineItem.php:65
__construct(VocabulariesInterface $vocabularies)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: AccessToken.php:19
static int $defaultLimit
Default limit on size of container to be returned from requests.
Definition: LineItem.php:56
string $key
Consumer key/client ID value.
Definition: System.php:193
$url
Definition: ltiregstart.php:35
static getLineItem(Platform $platform, string $endpoint)
Retrieve a line item.
Definition: LineItem.php:213
saveLineItem(ToolProvider\LineItem $lineItem)
Save a line item.
Definition: LineItem.php:178
int $limit
Limit on size of container to be returned from requests.
Definition: LineItem.php:65
string $endpoint
Line item endpoint.
Definition: LineItem.php:86
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Platform $platform, string $endpoint, int $limit=null, bool $pagingMode=false)
Class constructor.
Definition: LineItem.php:83
$service
Definition: ltiservices.php:43
const MEDIA_TYPE_LINE_ITEMS
Line item container media type.
Definition: LineItem.php:41
static string $SCOPE_READONLY
Read-only access scope.
Definition: LineItem.php:51