LTI Integration Library  3.1.0
PHP class library for building LTI integrations
ToolSettings.php
Go to the documentation of this file.
1 <?php
2 
3 namespace ceLTIc\LTI\Service;
4 
8 
17 class ToolSettings extends Service
18 {
19 
23  const MODE_CURRENT_LEVEL = 1;
24 
28  const MODE_ALL_LEVELS = 2;
29 
34 
40  private static $LEVEL_NAMES = array('ToolProxy' => 'system',
41  'ToolProxyBinding' => 'context',
42  'LtiLink' => 'link');
43 
49  private $source;
50 
56  private $simple;
57 
65  public function __construct($source, $endpoint, $simple = true)
66  {
67  if (is_a($source, 'ceLTIc\LTI\ToolConsumer')) {
68  $consumer = $source;
69  } else {
70  $consumer = $source->getConsumer();
71  }
72  if ($simple) {
73  $mediaType = 'application/vnd.ims.lti.v2.toolsettings.simple+json';
74  } else {
75  $mediaType = 'application/vnd.ims.lti.v2.toolsettings+json';
76  }
77  parent::__construct($consumer, $endpoint, $mediaType);
78  $this->source = $source;
79  $this->simple = $simple;
80  }
81 
89  public function get($mode = self::MODE_CURRENT_LEVEL)
90  {
91  $parameter = array();
92  if ($mode === self::MODE_ALL_LEVELS) {
93  $parameter['bubble'] = 'all';
94  } elseif ($mode === self::MODE_DISTINCT_NAMES) {
95  $parameter['bubble'] = 'distinct';
96  }
97  $http = $this->send('GET', $parameter);
98  if (!$http->ok) {
99  $response = false;
100  } elseif ($this->simple) {
101  $response = json_decode($http->response, true);
102  } elseif (isset($http->responseJson->{'@graph'})) {
103  $response = array();
104  foreach ($http->responseJson->{'@graph'} as $level) {
105  $settings = json_decode(json_encode($level->custom), true);
106  unset($settings['@id']);
107  $response[self::$LEVEL_NAMES[$level->{'@type'}]] = $settings;
108  }
109  }
110 
111  return $response;
112  }
113 
121  public function set($settings)
122  {
123  if (!$this->simple) {
124  if (is_a($this->source, 'ToolConsumer')) {
125  $type = 'ToolProxy';
126  } elseif (is_a($this->source, 'ToolConsumer')) {
127  $type = 'ToolProxyBinding';
128  } else {
129  $type = 'LtiLink';
130  }
131  $obj = new \stdClass();
132  $obj->{'@context'} = 'http://purl.imsglobal.org/ctx/lti/v2/ToolSettings';
133  $obj->{'@graph'} = array();
134  $level = new \stdClass();
135  $level->{'@type'} = $type;
136  $level->{'@id'} = $this->endpoint;
137  $level->{'custom'} = $settings;
138  $obj->{'@graph'}[] = $level;
139  $body = json_encode($obj);
140  } else {
141  $body = json_encode($settings);
142  }
143 
144  $response = parent::send('PUT', null, $body);
145 
146  return $response->ok;
147  }
148 
149 }
$endpoint
Service endpoint.
Definition: Service.php:32
Class to implement the Tool Settings service.
Class to implement a service.
Definition: Service.php:17
Class to represent a tool consumer.
Class to represent a tool consumer context.
Definition: Context.php:17
const MODE_CURRENT_LEVEL
Settings at current level mode.
__construct($source, $endpoint, $simple=true)
Class constructor.
send($method, $parameters=array(), $body=null)
Send a service request.
Definition: Service.php:78
const MODE_ALL_LEVELS
Settings at all levels mode.
const MODE_DISTINCT_NAMES
Settings with distinct names at all levels mode.