MDL-61574 mod_lti: removed usage of PHP7 reserved word in namespace
[moodle.git] / mod / lti / service / toolsettings / classes / local / resources / contextsettings.php
CommitLineData
e3f69b58 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/>.
16
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 */
25
26
6fb64a4e 27namespace ltiservice_toolsettings\local\resources;
e3f69b58 28
6fb64a4e 29use ltiservice_toolsettings\local\resources\systemsettings;
e3f69b58 30use ltiservice_toolsettings\local\service\toolsettings;
31
32defined('MOODLE_INTERNAL') || die();
33
34/**
35 * A resource implementing the Context-level (ToolProxyBinding) Settings.
36 *
37 * @package ltiservice_toolsettings
38 * @since Moodle 2.8
39 * @copyright 2014 Vital Source Technologies http://vitalsource.com
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class contextsettings extends \mod_lti\local\ltiservice\resource_base {
43
44 /**
45 * Class constructor.
46 *
6fb64a4e 47 * @param ltiservice_toolsettings\local\resources\contextsettings $service Service instance
e3f69b58 48 */
49 public function __construct($service) {
50
51 parent::__construct($service);
52 $this->id = 'ToolProxyBindingSettings';
82f52265 53 $this->template = '/{context_type}/{context_id}/bindings/{vendor_code}/{product_code}';
e3f69b58 54 $this->variables[] = 'ToolProxyBinding.custom.url';
55 $this->formats[] = 'application/vnd.ims.lti.v2.toolsettings+json';
56 $this->formats[] = 'application/vnd.ims.lti.v2.toolsettings.simple+json';
57 $this->methods[] = 'GET';
58 $this->methods[] = 'PUT';
59
60 }
61
62 /**
63 * Execute the request for this resource.
64 *
65 * @param mod_lti\local\ltiservice\response $response Response object for this request.
66 */
67 public function execute($response) {
68
69 $params = $this->parse_template();
70 $contexttype = $params['context_type'];
71 $contextid = $params['context_id'];
72 $vendorcode = $params['vendor_code'];
73 $productcode = $params['product_code'];
74 $bubble = optional_param('bubble', '', PARAM_ALPHA);
75 $ok = !empty($contexttype) && !empty($contextid) &&
76 !empty($vendorcode) && !empty($productcode) &&
77 $this->check_tool_proxy($productcode, $response->get_request_data());
78 if (!$ok) {
79 $response->set_code(401);
80 }
81 $contenttype = $response->get_accept();
82 $simpleformat = !empty($contenttype) && ($contenttype == $this->formats[1]);
83 if ($ok) {
84 $ok = (empty($bubble) || ((($bubble == 'distinct') || ($bubble == 'all')))) &&
85 (!$simpleformat || empty($bubble) || ($bubble != 'all')) &&
86 (empty($bubble) || ($response->get_request_method() == 'GET'));
87 }
88
89 if (!$ok) {
90 $response->set_code(404);
91 } else {
92 $systemsetting = null;
93 $contextsettings = lti_get_tool_settings($this->get_service()->get_tool_proxy()->id, $contextid);
94 if (!empty($bubble)) {
95 $systemsetting = new systemsettings($this->get_service());
96 $systemsetting->params['tool_proxy_id'] = $productcode;
97 $systemsettings = lti_get_tool_settings($this->get_service()->get_tool_proxy()->id);
98 if ($bubble == 'distinct') {
99 toolsettings::distinct_settings($systemsettings, $contextsettings, null);
100 }
101 } else {
102 $systemsettings = null;
103 }
104 if ($response->get_request_method() == 'GET') {
105 $json = '';
106 if ($simpleformat) {
107 $response->set_content_type($this->formats[1]);
108 $json .= "{";
109 } else {
110 $response->set_content_type($this->formats[0]);
111 $json .= "{\n \"@context\":\"http://purl.imsglobal.org/ctx/lti/v2/ToolSettings\",\n \"@graph\":[\n";
112 }
113 $settings = toolsettings::settings_to_json($systemsettings, $simpleformat, 'ToolProxy', $systemsetting);
114 $json .= $settings;
115 $isfirst = strlen($settings) <= 0;
116 $settings = toolsettings::settings_to_json($contextsettings, $simpleformat, 'ToolProxyBinding', $this);
117 if ((strlen($settings) > 0) && !$isfirst) {
118 $json .= ",";
119 }
120 $json .= $settings;
121 if ($simpleformat) {
122 $json .= "\n}";
123 } else {
124 $json .= "\n ]\n}";
125 }
126 $response->set_body($json);
127 } else { // PUT.
128 $settings = null;
129 if ($response->get_content_type() == $this->formats[0]) {
130 $json = json_decode($response->get_request_data());
131 $ok = !empty($json);
132 if ($ok) {
133 $ok = isset($json->{"@graph"}) && is_array($json->{"@graph"}) && (count($json->{"@graph"}) == 1) &&
134 ($json->{"@graph"}[0]->{"@type"} == 'ToolProxyBinding');
135 }
136 if ($ok) {
137 $settings = $json->{"@graph"}[0]->custom;
82f52265 138 unset($settings->{'@id'});
e3f69b58 139 }
140 } else { // Simple JSON.
141 $json = json_decode($response->get_request_data(), true);
142 $ok = !empty($json);
143 if ($ok) {
144 $ok = is_array($json);
145 }
146 if ($ok) {
147 $settings = $json;
148 }
149 }
150 if ($ok) {
151 lti_set_tool_settings($settings, $this->get_service()->get_tool_proxy()->id, $contextid);
152 } else {
153 $response->set_code(406);
154 }
155 }
156 }
157 }
158
159 /**
160 * Parse a value for custom parameter substitution variables.
161 *
162 * @param string $value String to be parsed
163 *
164 * @return string
165 */
166 public function parse_value($value) {
167 global $COURSE;
168
169 if ($COURSE->format == 'site') {
170 $this->params['context_type'] = 'Group';
171 } else {
172 $this->params['context_type'] = 'CourseSection';
173 }
174 $this->params['context_id'] = $COURSE->id;
175 $this->params['vendor_code'] = $this->get_service()->get_tool_proxy()->vendorcode;
176 $this->params['product_code'] = $this->get_service()->get_tool_proxy()->guid;
177 $value = str_replace('$ToolProxyBinding.custom.url', parent::get_endpoint(), $value);
178
179 return $value;
180
181 }
182
183}