LTI Integration Library 4.10.3
PHP class library for building LTI integrations
 
Loading...
Searching...
No Matches
ToolSettings.php
1<?php
2
3namespace ceLTIc\LTI\Service;
4
9
17class ToolSettings extends Service
18{
19
24
28 const MODE_ALL_LEVELS = 2;
29
34
38 public static $SCOPE = 'https://purl.imsglobal.org/spec/lti-ts/scope/toolsetting';
39
45 private static $LEVEL_NAMES = array('ToolProxy' => 'system',
46 'ToolProxyBinding' => 'context',
47 'LtiLink' => 'link');
48
54 private $source;
55
61 private $simple;
62
70 public function __construct($source, $endpoint, $simple = true)
71 {
72 if (is_a($source, 'ceLTIc\LTI\Platform')) {
73 $platform = $source;
74 } else {
75 $platform = $source->getPlatform();
76 }
77 parent::__construct($platform, $endpoint);
78 $this->scope = self::$SCOPE;
79 if ($simple) {
80 $this->mediaType = 'application/vnd.ims.lti.v2.toolsettings.simple+json';
81 } else {
82 $this->mediaType = 'application/vnd.ims.lti.v2.toolsettings+json';
83 }
84 $this->source = $source;
85 $this->simple = $simple;
86 }
87
95 public function get($mode = self::MODE_CURRENT_LEVEL)
96 {
97 $parameter = array();
98 if ($mode === self::MODE_ALL_LEVELS) {
99 $parameter['bubble'] = 'all';
100 } elseif ($mode === self::MODE_DISTINCT_NAMES) {
101 $parameter['bubble'] = 'distinct';
102 }
103 $http = $this->send('GET', $parameter);
104 if (!$http->ok) {
105 $response = false;
106 } elseif ($this->simple) {
107 $response = Util::jsonDecode($http->response, true);
108 } elseif (isset($http->responseJson->{'@graph'})) {
109 $response = array();
110 foreach ($http->responseJson->{'@graph'} as $level) {
111 $settings = Util::jsonDecode(json_encode($level->custom), true);
112 unset($settings['@id']);
113 $response[self::$LEVEL_NAMES[$level->{'@type'}]] = $settings;
114 }
115 }
116
117 return $response;
118 }
119
127 public function set($settings)
128 {
129 if (!$this->simple) {
130 if (is_a($this->source, 'Platform')) {
131 $type = 'ToolProxy';
132 } elseif (is_a($this->source, 'Context')) {
133 $type = 'ToolProxyBinding';
134 } else {
135 $type = 'LtiLink';
136 }
137 $obj = new \stdClass();
138 $obj->{'@context'} = 'http://purl.imsglobal.org/ctx/lti/v2/ToolSettings';
139 $obj->{'@graph'} = array();
140 $level = new \stdClass();
141 $level->{'@type'} = $type;
142 $level->{'@id'} = $this->endpoint;
143 $level->{'custom'} = $settings;
144 $obj->{'@graph'}[] = $level;
145 $body = json_encode($obj);
146 } else {
147 $body = json_encode($settings);
148 }
149
150 $response = parent::send('PUT', null, $body);
151
152 return $response->ok;
153 }
154
155}
Class to represent a platform context.
Definition Context.php:18
Class to represent a platform.
Definition Platform.php:18
Class to implement a service.
Definition Service.php:19
send($method, $parameters=array(), $body=null)
Send a service request.
Definition Service.php:119
$endpoint
Service endpoint.
Definition Service.php:33
Class to implement the Tool Settings service.
const MODE_CURRENT_LEVEL
Settings at current level mode.
__construct($source, $endpoint, $simple=true)
Class constructor.
const MODE_DISTINCT_NAMES
Settings with distinct names at all levels mode.
const MODE_ALL_LEVELS
Settings at all levels mode.
Class to implement utility methods.
Definition Util.php:15
static jsonDecode($str, $associative=false)
Decode a JSON string.
Definition Util.php:560