ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ToolSettings.php
Go to the documentation of this file.
1 <?php
2 
4 
14 class ToolSettings extends Service
15 {
16 
20  const MODE_CURRENT_LEVEL = 1;
24  const MODE_ALL_LEVELS = 2;
29 
35  private static $LEVEL_NAMES = array('ToolProxy' => 'system',
36  'ToolProxyBinding' => 'context',
37  'LtiLink' => 'link');
38 
44  private $source;
50  private $simple;
51 
59  public function __construct($source, $endpoint, $simple = true)
60  {
61 
62  if (is_a($source, 'IMSGlobal\LTI\ToolProvider\ToolConsumer')) {
64  } else {
65  $consumer = $source->getConsumer();
66  }
67  if ($simple) {
68  $mediaType = 'application/vnd.ims.lti.v2.toolsettings.simple+json';
69  } else {
70  $mediaType = 'application/vnd.ims.lti.v2.toolsettings+json';
71  }
72  parent::__construct($consumer, $endpoint, $mediaType);
73  $this->source = $source;
74  $this->simple = $simple;
75 
76  }
77 
85  public function get($mode = self::MODE_CURRENT_LEVEL) {
86 
87  $parameter = array();
88  if ($mode === self::MODE_ALL_LEVELS) {
89  $parameter['bubble'] = 'all';
90  } else if ($mode === self::MODE_DISTINCT_NAMES) {
91  $parameter['bubble'] = 'distinct';
92  }
93  $http = $this->send('GET', $parameter);
94  if (!$http->ok) {
95  $response = false;
96  } else if ($this->simple) {
97  $response = json_decode($http->response, true);
98  } else if (isset($http->responseJson->{'@graph'})) {
99  $response = array();
100  foreach ($http->responseJson->{'@graph'} as $level) {
101  $settings = json_decode(json_encode($level->custom), true);
102  unset($settings['@id']);
103  $response[self::$LEVEL_NAMES[$level->{'@type'}]] = $settings;
104  }
105  }
106 
107  return $response;
108 
109  }
110 
118  public function set($settings) {
119 
120  if (!$this->simple) {
121  if (is_a($this->source, 'ToolConsumer')) {
122  $type = 'ToolProxy';
123  } else if (is_a($this->source, 'ToolConsumer')) {
124  $type = 'ToolProxyBinding';
125  } else {
126  $type = 'LtiLink';
127  }
128  $obj = new \stdClass();
129  $obj->{'@context'} = 'http://purl.imsglobal.org/ctx/lti/v2/ToolSettings';
130  $obj->{'@graph'} = array();
131  $level = new \stdClass();
132  $level->{'@type'} = $type;
133  $level->{'@id'} = $this->endpoint;
134  $level->{'custom'} = $settings;
135  $obj->{'@graph'}[] = $level;
136  $body = json_encode($obj);
137  } else {
138  $body = json_encode($settings);
139  }
140 
141  $response = parent::send('PUT', null, $body);
142 
143  return $response->ok;
144 
145  }
146 
147 }
$source
The object to which the settings apply (ResourceLink, Context or ToolConsumer).
$type
$mediaType
Media type of message body.
Definition: Service.php:44
send($method, $parameters=array(), $body=null)
Send a service request.
Definition: Service.php:71
static $LEVEL_NAMES
Names of LTI parameters to be retained in the consumer settings property.
$simple
Whether to use the simple JSON format.
const MODE_ALL_LEVELS
Settings at all levels mode.
const MODE_DISTINCT_NAMES
Settings with distinct names at all levels mode.
const MODE_CURRENT_LEVEL
Settings at current level mode.
$http
Definition: raiseError.php:7
__construct($source, $endpoint, $simple=true)
Class constructor.
Class to implement a service.
Definition: Service.php:17
$consumer
Tool Consumer for this service request.
Definition: Service.php:38
$response
Class to implement the Tool Settings service.