Skip to content

Commit

Permalink
Add support for Line Item service
Browse files Browse the repository at this point in the history
  • Loading branch information
spvickers committed Jun 25, 2020
1 parent f2f4beb commit 8805e25
Show file tree
Hide file tree
Showing 6 changed files with 659 additions and 13 deletions.
79 changes: 79 additions & 0 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,68 @@ public function getMemberships($withGroups = false)
return $userResults;
}

/**
* Check if the Line Item service is available.
*
* @return bool True if this resource link supports the Line Item service
*/
public function hasLineItemService()
{
$url = $this->getSetting('custom_lineitems_url');

return !empty($url);
}

/**
* Get line items.
*
* @param string|null $resourceId Tool resource ID
* @param string|null $tag Tag
* @param int|null $limit Limit of line items to be returned, null for service default
*
* @return LineItem[]|bool Array of LineItem objects or false on error
*/
public function getLineItems($resourceId = null, $tag = null, $limit = null)
{
$lineItems = false;
$this->extRequest = '';
$this->extRequestHeaders = '';
$this->extResponse = '';
$this->extResponseHeaders = '';
$this->lastServiceRequest = null;
$url = $this->getSetting('custom_lineitems_url');
if (!empty($url)) {
$lineItemService = new Service\LineItem($this->getConsumer(), $url, $limit);
$lineItems = $lineItemService->getAll(null, $resourceId, $tag);
$http = $lineItemService->getHttpMessage();
$this->extResponse = $http->response;
$this->extResponseHeaders = $http->responseHeaders;
$this->extRequest = $http->request;
$this->extRequestHeaders = $http->requestHeaders;
$this->lastServiceRequest = $http;
}

return $lineItems;
}

/**
* Create a new line item.
*
* @param LineItem $lineItem Line item object
*
* @return bool True if successful
*/
public function createLineItem($lineItem)
{
$ok = false;
$lineItemService = $this->getLineItemService();
if (!empty($lineItemService)) {
$ok = $lineItemService->createLineItem($lineItem);
}

return $ok;
}

/**
* Load the context from the database.
*
Expand Down Expand Up @@ -518,4 +580,21 @@ private function load($id = null)
return $this->getDataConnector()->loadContext($this);
}

/**
* Get the Line Item service object.
*
* @return Service\\LineItem Line Item service, or false if not available
*/
private function getLineItemService()
{
$url = $this->getSetting('custom_lineitems_url');
if (!empty($url)) {
$lineItemService = new Service\LineItem($this->getConsumer(), $url);
} else {
$lineItemService = false;
}

return $lineItemService;
}

}
204 changes: 204 additions & 0 deletions src/LineItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php

namespace ceLTIc\LTI;

use ceLTIc\LTI\Service;

/**
* Class to represent a line item
*
* @author Stephen P Vickers <[email protected]>
* @copyright SPV Software Products
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3
*/
class LineItem
{

/**
* Label value.
*
* @var string $label
*/
public $label = null;

/**
* Points possible value.
*
* @var int $pointsPossible
*/
public $pointsPossible = 1;

/**
* LTI Resource Link ID with which the line item is associated.
*
* @var string|null $ltiResourceLinkId
*/
public $ltiResourceLinkId = null;

/**
* Tool resource ID associated with the line item.
*
* @var string|null $resourceId
*/
public $resourceId = null;

/**
* Tag value.
*
* @var string|null $tag
*/
public $tag = null;

/**
* Outcome start submit timestamp.
*
* @var int|null $submitFrom
*/
public $submitFrom = null;

/**
* Outcome end submit timestamp.
*
* @var int|null $submitUntil
*/
public $submitUntil = null;

/**
* Line item endpoint.
*
* @var string $endpoint
*/
public $endpoint = null;

/**
* Tool Consumer for this line item.
*
* @var ToolConsumer|null $consumer
*/
private $consumer = null;

/**
* Class constructor.
*
* @param ToolConsumer $consumer ToolConsumer object
* @param string $label Label
* @param int $pointsPossible Points possible value
*/
public function __construct($consumer, $label, $pointsPossible)
{
$this->consumer = $consumer;
$this->label = $label;
$this->pointsPossible = $pointsPossible;
}

/**
* Get Tool Consumer.
*
* @return ToolConsumer ToolConsumer object for this line item.
*/
public function getConsumer()
{
return $this->consumer;
}

/**
* Save the line item to the tool consumer.
*
* @return bool True if successful
*/
public function save()
{
$service = new Service\LineItem($this->consumer, $this->endpoint);
$http = $this->send('PUT', null, Service\LineItem::toJson($lineItem));
$ok = $http->ok;
if ($ok && !empty($http->responseJson)) {
$lineItem = Service\LineItem::toLineItem($this->consumer, $http->responseJson);
foreach (get_object_vars($lineItem) as $key => $value) {
$this->$key = $value;
}
}

return $ok;
}

/**
* Delete the line item on the tool consumer.
*
* @return bool True if successful
*/
public function delete()
{
$service = new Service\LineItem($this->consumer, $this->endpoint);
$http = $service->send('DELETE');

return $http->ok;
}

/**
* Retrieve all outcomes.
*
* @param int|null $limit Limit of outcomes to be returned in each request, null for service default
*
* @return Outcome[]|bool Array of outcome objects, or false on error
*/
public function getOutcomes($limit = null)
{
$resultService = new Service\Result($this->consumer, $this->endpoint);
return $resultService->getAll();
}

/**
* Retrieve the outcome for a user.
*
* @param User $user User object
*
* @return Outcome|null|bool Outcome object, or null if none, or false on error
*/
public function readOutcome($user)
{
$resultService = new Service\Result($this->consumer, $this->endpoint);
return $resultService->get($user);
}

/**
* Submit the outcome for a user.
*
* @param Outcome $ltiOutcome Outcome object
* @param User $user User object
*
* @return bool True if successful
*/
public function submitOutcome($ltiOutcome, $user)
{
$scoreService = new Service\Score($this->consumer, $this->endpoint);
return $scoreService->submit($ltiOutcome, $user);
}

/**
* Delete the outcome for a user.
*
* @param User $user User object
*
* @return bool True if successful, otherwise false
*/
public function deleteOutcome($user)
{
$ltiOutcome = new Outcome();
$scoreService = new Service\Score($this->consumer, $this->endpoint);
return $scoreService->submit($ltiOutcome, $user);
}

/**
* Retrieve a line item definition.
*
* @param ToolConsumer $consumer ToolConsumer object
* @param string $endpoint ID value
*
* @return LineItem|bool LineItem object or false on error
*/
public static function fromEndpoint($consumer, $endpoint)
{
return Service\LineItem::getLineItem($consumer, $endpoint);
}

}
Loading

0 comments on commit 8805e25

Please sign in to comment.