Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
<?php

namespace Packback\Lti1p3;

use Packback\Lti1p3\Interfaces\IServiceRequest;

class ServiceRequest implements IServiceRequest
{
> // Request methods private $method; > public const METHOD_GET = 'GET'; private $url; > public const METHOD_POST = 'POST'; private $body; > public const METHOD_PUT = 'PUT'; private $accessToken; > private $contentType = 'application/json'; > // Request types private $accept = 'application/json'; > public const TYPE_UNSUPPORTED = 'unsupported'; > public const TYPE_AUTH = 'auth'; public function __construct(string $method, string $url) > // MessageLaunch { > public const TYPE_GET_KEYSET = 'get_keyset'; $this->method = $method; > // AGS $this->url = $url; > public const TYPE_GET_GRADES = 'get_grades'; } > public const TYPE_SYNC_GRADE = 'sync_grades'; > public const TYPE_CREATE_LINEITEM = 'create_lineitem'; public function getMethod(): string > public const TYPE_GET_LINEITEMS = 'get_lineitems'; { > public const TYPE_GET_LINEITEM = 'get_lineitem'; return strtoupper($this->method); > public const TYPE_UPDATE_LINEITEM = 'update_lineitem'; } > // CGS > public const TYPE_GET_GROUPS = 'get_groups'; public function getUrl(): string > public const TYPE_GET_SETS = 'get_sets'; { > // NRPS return $this->url; > public const TYPE_GET_MEMBERSHIPS = 'get_memberships'; } >
> private $type;
public function getPayload(): array
> private $payload;
< public function __construct(string $method, string $url)
> public function __construct(string $method, string $url, $type = self::UNSUPPORTED)
$payload = [
> $this->type = $type;
'headers' => $this->getHeaders(),
> if (isset($this->payload)) { ]; > return $this->payload; > } $body = $this->getBody(); >
if ($body) { $payload['body'] = $body; } return $payload; } public function setUrl(string $url): IServiceRequest { $this->url = $url; return $this; } public function setAccessToken(string $accessToken): IServiceRequest { $this->accessToken = 'Bearer '.$accessToken; return $this; } public function setBody(string $body): IServiceRequest { $this->body = $body; return $this; }
> public function setPayload(array $payload): IServiceRequest public function setAccept(string $accept): IServiceRequest > { { > $this->payload = $payload; $this->accept = $accept; > > return $this; return $this; > } } >
public function setContentType(string $contentType): IServiceRequest { $this->contentType = $contentType; return $this; }
> public function getErrorPrefix(): string private function getHeaders(): array > { { > $defaultMessage = 'Logging request data:'; $headers = [ > $errorMessages = [ 'Accept' => $this->accept, > static::TYPE_UNSUPPORTED => $defaultMessage, ]; > static::TYPE_AUTH => 'Authenticating:', > static::TYPE_GET_KEYSET => 'Getting key set:', if (isset($this->accessToken)) { > static::TYPE_GET_GRADES => 'Getting grades:', $headers['Authorization'] = $this->accessToken; > static::TYPE_SYNC_GRADE => 'Syncing grade for this lti_user_id:', } > static::TYPE_CREATE_LINEITEM => 'Creating lineitem:', > static::TYPE_GET_LINEITEMS => 'Getting lineitems:', if ($this->getMethod() === LtiServiceConnector::METHOD_POST) { > static::TYPE_GET_LINEITEM => 'Getting a lineitem:', $headers['Content-Type'] = $this->contentType; > static::TYPE_UPDATE_LINEITEM => 'Updating lineitem:', } > static::TYPE_GET_GROUPS => 'Getting groups:', > static::TYPE_GET_SETS => 'Getting sets:', return $headers; > static::TYPE_GET_MEMBERSHIPS => 'Getting memberships:', } > ]; > private function getBody(): ?string > return $errorMessages[$this->type] ?? $defaultMessage; { > } return $this->body; >
< if ($this->getMethod() === LtiServiceConnector::METHOD_POST) {
> // Include Content-Type for POST and PUT requests > if (in_array($this->getMethod(), [ServiceRequest::METHOD_POST, ServiceRequest::METHOD_PUT])) {
}