See Release Notes
Long Term Support Release
Differences Between: [Versions 401 and 402] [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 class ToolSettings extends Service 15 { 16 17 /** 18 * Settings at current level mode. 19 */ 20 const MODE_CURRENT_LEVEL = 1; 21 /** 22 * Settings at all levels mode. 23 */ 24 const MODE_ALL_LEVELS = 2; 25 /** 26 * Settings with distinct names at all levels mode. 27 */ 28 const MODE_DISTINCT_NAMES = 3; 29 30 /** 31 * Names of LTI parameters to be retained in the consumer settings property. 32 * 33 * @var array $LEVEL_NAMES 34 */ 35 private static $LEVEL_NAMES = array('ToolProxy' => 'system', 36 'ToolProxyBinding' => 'context', 37 'LtiLink' => 'link'); 38 39 /** 40 * The object to which the settings apply (ResourceLink, Context or ToolConsumer). 41 * 42 * @var object $source 43 */ 44 private $source; 45 /** 46 * Whether to use the simple JSON format. 47 * 48 * @var boolean $simple 49 */ 50 private $simple; 51 52 /** 53 * Class constructor. 54 * 55 * @param object $source The object to which the settings apply (ResourceLink, Context or ToolConsumer) 56 * @param string $endpoint Service endpoint 57 * @param boolean $simple True if the simple media type is to be used (optional, default is true) 58 */ 59 public function __construct($source, $endpoint, $simple = true) 60 { 61 62 if (is_a($source, 'IMSGlobal\LTI\ToolProvider\ToolConsumer')) { 63 $consumer = $source; 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 78 /** 79 * Get the tool settings. 80 * 81 * @param int $mode Mode for request (optional, default is current level only) 82 * 83 * @return mixed The array of settings if successful, otherwise false 84 */ 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 111 /** 112 * Set the tool settings. 113 * 114 * @param array $settings An associative array of settings (optional, default is null) 115 * 116 * @return HTTPMessage HTTP object containing request and response details 117 */ 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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body