ILIAS  release_8 Revision v8.24
ToolSettings.php
Go to the documentation of this file.
1<?php
2
20
24
32class ToolSettings extends Service
33{
37 public const MODE_CURRENT_LEVEL = 1;
38
42 public const MODE_ALL_LEVELS = 2;
43
47 public const MODE_DISTINCT_NAMES = 3;
48
52 public static string $SCOPE = 'https://purl.imsglobal.org/spec/lti-ts/scope/toolsetting';
53
59 private static array $LEVEL_NAMES = array('ToolProxy' => 'system',
60 'ToolProxyBinding' => 'context',
61 'LtiLink' => 'link');
62
68 private $source;
69
75 private bool $simple;
76
83 public function __construct($source, string $endpoint, bool $simple = true)
84 {
85 if (is_a($source, 'ceLTIc\LTI\Platform')) {
87 } else {
88 $platform = $source->getPlatform();
89 }
91 $this->scope = self::$SCOPE;
92 if ($simple) {
93 $this->mediaType = 'application/vnd.ims.lti.v2.toolsettings.simple+json';
94 } else {
95 $this->mediaType = 'application/vnd.ims.lti.v2.toolsettings+json';
96 }
97 $this->source = $source;
98 $this->simple = $simple;
99 }
100
106 public function get(int $mode = self::MODE_CURRENT_LEVEL)
107 {
108 $parameter = array();
109 if ($mode === self::MODE_ALL_LEVELS) {
110 $parameter['bubble'] = 'all';
111 } elseif ($mode === self::MODE_DISTINCT_NAMES) {
112 $parameter['bubble'] = 'distinct';
113 }
114 $http = $this->send('GET', $parameter);
115 if (!$http->ok) {
116 $response = false;
117 } elseif ($this->simple) {
118 $response = json_decode($http->response, true);
119 } elseif (isset($http->responseJson->{'@graph'})) {
120 $response = array();
121 foreach ($http->responseJson->{'@graph'} as $level) {
122 $settings = json_decode(json_encode($level->custom), true);
123 unset($settings['@id']);
124 $response[self::$LEVEL_NAMES[$level->{'@type'}]] = $settings;
125 }
126 }
127
128 return $response;
129 }
130
136 public function set(array $settings): bool
137 {
138 if (!$this->simple) {
139 if (is_a($this->source, 'Platform')) {
140 $type = 'ToolProxy';
141 } elseif (is_a($this->source, 'Context')) {
142 $type = 'ToolProxyBinding';
143 } else {
144 $type = 'LtiLink';
145 }
146 $obj = new \stdClass();
147 $obj->{'@context'} = 'http://purl.imsglobal.org/ctx/lti/v2/ToolSettings';
148 $obj->{'@graph'} = array();
149 $level = new \stdClass();
150 $level->{'@type'} = $type;
151 $level->{'@id'} = $this->endpoint;
152 $level->{'custom'} = $settings;
153 $obj->{'@graph'}[] = $level;
154 $body = json_encode($obj);
155 } else {
156 $body = json_encode($settings);
157 }
158
159 $response = parent::send('PUT', null, $body);
160
161 return $response->ok;
162 }
163}
Class to represent a platform context.
Definition: Context.php:34
Class to represent a platform.
Definition: Platform.php:36
Class to implement a service.
Definition: Service.php:34
string $endpoint
Service endpoint.
Definition: Service.php:47
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
Platform $platform
Platform for this service request.
Definition: Service.php:68
Class to implement the Tool Settings service.
static array $LEVEL_NAMES
Names of LTI parameters to be retained in the consumer settings property.
__construct($source, string $endpoint, bool $simple=true)
Class constructor.
$source
The object to which the settings apply (ResourceLink, Context or Platform).
bool $simple
Whether to use the simple JSON format.
const MODE_ALL_LEVELS
Settings at all levels mode.
const MODE_CURRENT_LEVEL
Settings at current level mode.
const MODE_DISTINCT_NAMES
Settings with distinct names at all levels mode.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
$type
$response
catch(ilCmiXapiException $e) send($response)
Definition: xapitoken.php:100