MDL-61574 mod_lti: removed usage of PHP7 reserved word in namespace
[moodle.git] / mod / lti / service / profile / classes / local / resources / profile.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 Tool Consumer Profile resource
19 *
20 * @package ltiservice_profile
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_profile\local\resources;
e3f69b58 28
29use \mod_lti\local\ltiservice\service_base;
30
31defined('MOODLE_INTERNAL') || die();
32
33/**
34 * A resource implementing the Tool Consumer Profile.
35 *
36 * @package ltiservice_profile
37 * @since Moodle 2.8
38 * @copyright 2014 Vital Source Technologies http://vitalsource.com
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 */
41class profile extends \mod_lti\local\ltiservice\resource_base {
42
43 /**
44 * Class constructor.
45 *
6fb64a4e 46 * @param ltiservice_profile\local\resources\profile $service Service instance
e3f69b58 47 */
48 public function __construct($service) {
49
50 parent::__construct($service);
51 $this->id = 'ToolConsumerProfile';
52 $this->template = '/profile/{tool_proxy_id}';
53 $this->variables[] = 'ToolConsumerProfile.url';
54 $this->formats[] = 'application/vnd.ims.lti.v2.toolconsumerprofile+json';
55 $this->methods[] = 'GET';
56
57 }
58
59 /**
60 * Get the path for this resource.
61 *
62 * @return string
63 */
64 public function get_path() {
65
66 $path = $this->template;
67 $toolproxy = $this->get_service()->get_tool_proxy();
68 if (!empty($toolproxy)) {
69 $path = str_replace('{tool_proxy_id}', $toolproxy->guid, $path);
70 }
71
72 return $path;
73
74 }
75
76 /**
77 * Execute the request for this resource.
78 *
79 * @param mod_lti\local\ltiservice\response $response Response object for this request.
80 */
81 public function execute($response) {
82
83 global $CFG;
84
85 $version = service_base::LTI_VERSION2P0;
86 $params = $this->parse_template();
87 $ok = $this->get_service()->check_tool_proxy($params['tool_proxy_id']);
88 if (!$ok) {
89 $response->set_code(404);
90 } else if (optional_param('lti_version', '', PARAM_ALPHANUMEXT) != $version) {
91 $response->set_code(400);
92 } else {
93 $toolproxy = $this->get_service()->get_tool_proxy();
94 $response->set_content_type($this->formats[0]);
95
96 $servicepath = $this->get_service()->get_service_path();
97 $id = $servicepath . $this->get_path();
98 $now = date('Y-m-d\TH:iO');
99 $capabilityofferedarr = explode("\n", $toolproxy->capabilityoffered);
100 $serviceofferedarr = explode("\n", $toolproxy->serviceoffered);
101 $serviceoffered = '';
102 $sep = '';
103 $services = \core_component::get_plugin_list('ltiservice');
104 foreach ($services as $name => $location) {
105 if (in_array($name, $serviceofferedarr)) {
106 $classname = "\\ltiservice_{$name}\\local\\service\\{$name}";
107 $service = new $classname();
108 $service->set_tool_proxy($toolproxy);
109 $resources = $service->get_resources();
110 foreach ($resources as $resource) {
111 $formats = implode("\", \"", $resource->get_formats());
112 $methods = implode("\", \"", $resource->get_methods());
113 $capabilityofferedarr = array_merge($capabilityofferedarr, $resource->get_variables());
114 $path = $servicepath . preg_replace('/\{?.*\}$/', '', $resource->get_path());
115 $serviceoffered .= <<< EOD
116{$sep}
117 {
118 "@type":"{$resource->get_type()}",
119 "@id":"tcp:{$resource->get_id()}",
120 "endpoint":"{$path}",
121 "format":["{$formats}"],
122 "action":["{$methods}"]
123 }
124EOD;
125 $sep = ',';
126 }
127 }
128 }
129 $capabilityoffered = implode("\",\n \"", $capabilityofferedarr);
130 if (strlen($capabilityoffered) > 0) {
131 $capabilityoffered = "\n \"{$capabilityoffered}\"";
132 }
133 $urlparts = parse_url($CFG->wwwroot);
134 $orgid = $urlparts['host'];
135 $name = 'Moodle';
136 $code = 'moodle';
137 $vendorname = 'Moodle.org';
138 $vendorcode = 'mdl';
139 $prodversion = strval($CFG->version);
140 if (!empty($CFG->mod_lti_institution_name)) {
141 $consumername = $CFG->mod_lti_institution_name;
142 $consumerdesc = '';
143 } else {
144 $consumername = get_site()->fullname;
145 $consumerdesc = strip_tags(get_site()->summary);
146 }
147 $profile = <<< EOD
148{
149 "@context":[
150 "http://purl.imsglobal.org/ctx/lti/v2/ToolConsumerProfile",
151 {
152 "tcp":"{$id}#"
153 }
154 ],
155 "@type":"ToolConsumerProfile",
156 "@id":"{$id}",
157 "lti_version":"{$version}",
158 "guid":"{$toolproxy->guid}",
159 "product_instance":{
160 "guid":"{$orgid}",
161 "product_info":{
162 "product_name":{
163 "default_value":"{$name}",
164 "key":"product.name"
165 },
166 "product_version":"{$prodversion}",
167 "product_family":{
168 "code":"{$code}",
169 "vendor":{
170 "code":"{$vendorcode}",
171 "vendor_name":{
172 "default_value":"{$vendorname}",
173 "key":"product.vendor.name"
174 },
175 "timestamp":"{$now}"
176 }
177 }
178 },
179 "service_owner":{
180 "@id":"ServiceOwner",
181 "service_owner_name":{
182 "default_value":"{$consumername}",
183 "key":"service_owner.name"
184 },
185 "description":{
186 "default_value":"{$consumerdesc}",
187 "key":"service_owner.description"
188 }
189 }
190 },
191 "capability_offered":[{$capabilityoffered}
192 ],
193 "service_offered":[{$serviceoffered}
194 ]
195}
196EOD;
197 $response->set_body($profile);
198
199 }
200 }
201
202 /**
203 * Get the resource fully qualified endpoint.
204 *
205 * @return string
206 */
207 public function get_endpoint() {
208
209 return parent::get_endpoint() . '?lti_version=' . service_base::LTI_VERSION2P0;
210
211 }
212
213 /**
214 * Parse a value for custom parameter substitution variables.
215 *
216 * @param string $value String to be parsed
217 *
218 * @return string
219 */
220 public function parse_value($value) {
221
222 $value = str_replace('$ToolConsumerProfile.url', $this->get_endpoint(), $value);
223
224 return $value;
225
226 }
227
228}