31b30dcab657831e01001119523e94be53457c95
[moodle.git] / mod / lti / service / toolsettings / classes / local / resource / linksettings.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * This file contains a class definition for the Context Settings resource
19  *
20  * @package    ltiservice_toolsettings
21  * @copyright  2014 Vital Source Technologies http://vitalsource.com
22  * @author     Stephen Vickers
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
27 namespace ltiservice_toolsettings\local\resource;
29 use ltiservice_toolsettings\local\resource\systemsettings;
30 use ltiservice_toolsettings\local\resource\contextsettings;
31 use ltiservice_toolsettings\local\service\toolsettings;
33 defined('MOODLE_INTERNAL') || die();
35 /**
36  * A resource implementing the Context-level (ToolProxyBinding) Settings.
37  *
38  * @package    ltiservice_toolsettings
39  * @since      Moodle 2.8
40  * @copyright  2014 Vital Source Technologies http://vitalsource.com
41  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42  */
43 class linksettings extends \mod_lti\local\ltiservice\resource_base {
45     /**
46      * Class constructor.
47      *
48      * @param ltiservice_toolsettings\local\resource\linksettings $service Service instance
49      */
50     public function __construct($service) {
52         parent::__construct($service);
53         $this->id = 'LtiLinkSettings';
54         $this->template = '/links/{link_id}';
55         $this->variables[] = 'LtiLink.custom.url';
56         $this->formats[] = 'application/vnd.ims.lti.v2.toolsettings+json';
57         $this->formats[] = 'application/vnd.ims.lti.v2.toolsettings.simple+json';
58         $this->methods[] = 'GET';
59         $this->methods[] = 'PUT';
61     }
63     /**
64      * Execute the request for this resource.
65      *
66      * @param mod_lti\local\ltiservice\response $response  Response object for this request.
67      */
68     public function execute($response) {
69         global $DB, $COURSE;
71         $params = $this->parse_template();
72         $linkid = $params['link_id'];
73         $bubble = optional_param('bubble', '', PARAM_ALPHA);
74         $contenttype = $response->get_accept();
75         $simpleformat = !empty($contenttype) && ($contenttype == $this->formats[1]);
76         $ok = (empty($bubble) || ((($bubble == 'distinct') || ($bubble == 'all')))) &&
77              (!$simpleformat || empty($bubble) || ($bubble != 'all')) &&
78              (empty($bubble) || ($response->get_request_method() == 'GET'));
79         if (!$ok) {
80             $response->set_code(406);
81         }
83         $systemsetting = null;
84         $contextsetting = null;
85         if ($ok) {
86             $ok = !empty($linkid);
87             if ($ok) {
88                 $lti = $DB->get_record('lti', array('id' => $linkid), 'course,typeid', MUST_EXIST);
89                 $ltitype = $DB->get_record('lti_types', array('id' => $lti->typeid));
90                 $toolproxy = $DB->get_record('lti_tool_proxies', array('id' => $ltitype->toolproxyid));
91                 $ok = $this->check_tool_proxy($toolproxy->guid, $response->get_request_data());
92             }
93             if (!$ok) {
94                 $response->set_code(401);
95             }
96         }
97         if ($ok) {
98             $linksettings = lti_get_tool_settings($this->get_service()->get_tool_proxy()->id, $lti->course, $linkid);
99             if (!empty($bubble)) {
100                 $contextsetting = new contextsettings($this->get_service());
101                 if ($COURSE == 'site') {
102                     $contextsetting->params['context_type'] = 'Group';
103                 } else {
104                     $contextsetting->params['context_type'] = 'CourseSection';
105                 }
106                 $contextsetting->params['context_id'] = $lti->course;
107                 $contextsetting->params['vendor_code'] = $this->get_service()->get_tool_proxy()->vendorcode;
108                 $contextsetting->params['product_code'] = $this->get_service()->get_tool_proxy()->id;
109                 $contextsettings = lti_get_tool_settings($this->get_service()->get_tool_proxy()->id, $lti->course);
110                 $systemsetting = new systemsettings($this->get_service());
111                 $systemsetting->params['tool_proxy_id'] = $this->get_service()->get_tool_proxy()->id;
112                 $systemsettings = lti_get_tool_settings($this->get_service()->get_tool_proxy()->id);
113                 if ($bubble == 'distinct') {
114                     toolsettings::distinct_settings($systemsettings, $contextsettings, $linksettings);
115                 }
116             } else {
117                 $contextsettings = null;
118                 $systemsettings = null;
119             }
120             if ($response->get_request_method() == 'GET') {
121                 $json = '';
122                 if ($simpleformat) {
123                     $response->set_content_type($this->formats[1]);
124                     $json .= "{";
125                 } else {
126                     $response->set_content_type($this->formats[0]);
127                     $json .= "{\n  \"@context\":\"http://purl.imsglobal.org/ctx/lti/v2/ToolSettings\",\n  \"@graph\":[\n";
128                 }
129                 $settings = toolsettings::settings_to_json($systemsettings, $simpleformat, 'ToolProxy', $systemsetting);
130                 $json .= $settings;
131                 $isfirst = strlen($settings) <= 0;
132                 $settings = toolsettings::settings_to_json($contextsettings, $simpleformat, 'ToolProxyBinding', $contextsetting);
133                 if (strlen($settings) > 0) {
134                     if (!$isfirst) {
135                         $json .= ",";
136                         if (!$simpleformat) {
137                             $json .= "\n";
138                         }
139                     }
140                     $isfirst = false;
141                 }
142                 $json .= $settings;
143                 $settings = toolsettings::settings_to_json($linksettings, $simpleformat, 'LtiLink', $this);
144                 if ((strlen($settings) > 0) && !$isfirst) {
145                     $json .= ",";
146                     if (!$simpleformat) {
147                         $json .= "\n";
148                     }
149                 }
150                 $json .= $settings;
151                 if ($simpleformat) {
152                     $json .= "\n}";
153                 } else {
154                     $json .= "\n  ]\n}";
155                 }
156                 $response->set_body($json);
157             } else { // PUT.
158                 $settings = null;
159                 if ($response->get_content_type() == $this->formats[0]) {
160                     $json = json_decode($response->get_request_data());
161                     $ok = !empty($json);
162                     if ($ok) {
163                         $ok = isset($json->{"@graph"}) && is_array($json->{"@graph"}) && (count($json->{"@graph"}) == 1) &&
164                               ($json->{"@graph"}[0]->{"@type"} == 'LtiLink');
165                     }
166                     if ($ok) {
167                         $settings = $json->{"@graph"}[0]->custom;
168                         unset($settings->{'@id'});
169                     }
170                 } else {  // Simple JSON.
171                     $json = json_decode($response->get_request_data(), true);
172                     $ok = !empty($json);
173                     if ($ok) {
174                         $ok = is_array($json);
175                     }
176                     if ($ok) {
177                         $settings = $json;
178                     }
179                 }
180                 if ($ok) {
181                     lti_set_tool_settings($settings, $this->get_service()->get_tool_proxy()->id, $lti->course, $linkid);
182                 } else {
183                     $response->set_code(406);
184                 }
185             }
186         }
187     }
189     /**
190      * Parse a value for custom parameter substitution variables.
191      *
192      * @param string $value String to be parsed
193      *
194      * @return string
195      */
196     public function parse_value($value) {
198         $id = optional_param('id', 0, PARAM_INT); // Course Module ID.
199         if (!empty($id)) {
200             $cm = get_coursemodule_from_id('lti', $id, 0, false, MUST_EXIST);
201             $this->params['link_id'] = $cm->instance;
202         }
203         $value = str_replace('$LtiLink.custom.url', parent::get_endpoint(), $value);
205         return $value;
207     }