Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]
1 <?php 2 3 namespace IMSGlobal\LTI\ToolProvider\Service; 4 5 /** 6 * Class to implement the Tool Settings service 7 * 8 * @author Stephen P Vickers <svickers@imsglobal.org> 9 * @copyright IMS Global Learning Consortium Inc 10 * @date 2016 11 * @version 3.0.0 12 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 13 */ 14 #[\AllowDynamicProperties] 15 class ToolSettings extends Service 16 { 17 18 /** 19 * Settings at current level mode. 20 */ 21 const MODE_CURRENT_LEVEL = 1; 22 /** 23 * Settings at all levels mode. 24 */ 25 const MODE_ALL_LEVELS = 2; 26 /** 27 * Settings with distinct names at all levels mode. 28 */ 29 const MODE_DISTINCT_NAMES = 3; 30 31 /** 32 * Names of LTI parameters to be retained in the consumer settings property. 33 * 34 * @var array $LEVEL_NAMES 35 */ 36 private static $LEVEL_NAMES = array('ToolProxy' => 'system', 37 'ToolProxyBinding' => 'context', 38 'LtiLink' => 'link'); 39 40 /** 41 * The object to which the settings apply (ResourceLink, Context or ToolConsumer). 42 * 43 * @var object $source 44 */ 45 private $source; 46 /** 47 * Whether to use the simple JSON format. 48 * 49 * @var boolean $simple 50 */ 51 private $simple; 52 53 /** 54 * Class constructor. 55 * 56 * @param object $source The object to which the settings apply (ResourceLink, Context or ToolConsumer) 57 * @param string $endpoint Service endpoint 58 * @param boolean $simple True if the simple media type is to be used (optional, default is true) 59 */ 60 public function __construct($source, $endpoint, $simple = true) 61 { 62 63 if (is_a($source, 'IMSGlobal\LTI\ToolProvider\ToolConsumer')) { 64 $consumer = $source; 65 } else { 66 $consumer = $source->getConsumer(); 67 } 68 if ($simple) { 69 $mediaType = 'application/vnd.ims.lti.v2.toolsettings.simple+json'; 70 } else { 71 $mediaType = 'application/vnd.ims.lti.v2.toolsettings+json'; 72 } 73 parent::__construct($consumer, $endpoint, $mediaType); 74 $this->source = $source; 75 $this->simple = $simple; 76 77 } 78 79 /** 80 * Get the tool settings. 81 * 82 * @param int $mode Mode for request (optional, default is current level only) 83 * 84 * @return mixed The array of settings if successful, otherwise false 85 */ 86 public function get($mode = self::MODE_CURRENT_LEVEL) { 87 88 $parameter = array(); 89 if ($mode === self::MODE_ALL_LEVELS) { 90 $parameter['bubble'] = 'all'; 91 } else if ($mode === self::MODE_DISTINCT_NAMES) { 92 $parameter['bubble'] = 'distinct'; 93 } 94 $http = $this->send('GET', $parameter); 95 if (!$http->ok) { 96 $response = false; 97 } else if ($this->simple) { 98 $response = json_decode($http->response, true); 99 } else if (isset($http->responseJson->{'@graph'})) { 100 $response = array(); 101 foreach ($http->responseJson->{'@graph'} as $level) { 102 $settings = json_decode(json_encode($level->custom), true); 103 unset($settings['@id']); 104 $response[self::$LEVEL_NAMES[$level->{'@type'}]] = $settings; 105 } 106 } 107 108 return $response; 109 110 } 111 112 /** 113 * Set the tool settings. 114 * 115 * @param array $settings An associative array of settings (optional, default is null) 116 * 117 * @return HTTPMessage HTTP object containing request and response details 118 */ 119 public function set($settings) { 120 121 if (!$this->simple) { 122 if (is_a($this->source, 'ToolConsumer')) { 123 $type = 'ToolProxy'; 124 } else if (is_a($this->source, 'ToolConsumer')) { 125 $type = 'ToolProxyBinding'; 126 } else { 127 $type = 'LtiLink'; 128 } 129 $obj = new \stdClass(); 130 $obj->{'@context'} = 'http://purl.imsglobal.org/ctx/lti/v2/ToolSettings'; 131 $obj->{'@graph'} = array(); 132 $level = new \stdClass(); 133 $level->{'@type'} = $type; 134 $level->{'@id'} = $this->endpoint; 135 $level->{'custom'} = $settings; 136 $obj->{'@graph'}[] = $level; 137 $body = json_encode($obj); 138 } else { 139 $body = json_encode($settings); 140 } 141 142 $response = parent::send('PUT', null, $body); 143 144 return $response->ok; 145 146 } 147 148 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body