Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
27
namespace ltiservice_toolsettings\local\resources;
28
 
29
use ltiservice_toolsettings\local\service\toolsettings;
30
 
31
defined('MOODLE_INTERNAL') || die();
32
 
33
/**
34
 * A resource implementing the Context-level (ToolProxyBinding) Settings.
35
 *
36
 * @package    ltiservice_toolsettings
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
 */
41
class contextsettings extends \mod_lti\local\ltiservice\resource_base {
42
 
43
    /**
44
     * Class constructor.
45
     *
46
     * @param \mod_lti\local\ltiservice\service_base $service Service instance
47
     */
48
    public function __construct($service) {
49
 
50
        parent::__construct($service);
51
        $this->id = 'ToolProxyBindingSettings';
52
        $this->template = '/{context_type}/{context_id}/bindings/{vendor_code}/{product_code}(/custom)';
53
        $this->variables[] = 'ToolProxyBinding.custom.url';
54
        $this->formats[] = 'application/vnd.ims.lti.v2.toolsettings+json';
55
        $this->formats[] = 'application/vnd.ims.lti.v2.toolsettings.simple+json';
56
        $this->methods[] = 'GET';
57
        $this->methods[] = 'PUT';
58
 
59
    }
60
 
61
    /**
62
     * Execute the request for this resource.
63
     *
64
     * @param \mod_lti\local\ltiservice\response $response  Response object for this request.
65
     */
66
    public function execute($response) {
67
 
68
        $params = $this->parse_template();
69
        $contexttype = $params['context_type'];
70
        $contextid = $params['context_id'];
71
        $vendorcode = $params['vendor_code'];
72
        $productcode = $params['product_code'];
73
        $bubble = optional_param('bubble', '', PARAM_ALPHA);
74
        $typeid = null;
75
        if (($vendorcode === 'tool') && is_numeric($productcode)) {
76
            $typeid = $productcode;
77
        }
78
        $ok = !empty($contexttype) && !empty($contextid) &&
79
              !empty($vendorcode) && !empty($productcode) &&
80
              $this->check_tool($typeid, $response->get_request_data(),
81
                  array(toolsettings::SCOPE_TOOL_SETTINGS));
82
        if (!$ok) {
83
            $response->set_code(401);
84
        } else {
85
            $toolproxy = $this->get_service()->get_tool_proxy();
86
            if (!empty($toolproxy)) {
87
                $ok = $toolproxy->guid === $productcode;
88
                $typeid = null;
89
                $id = $toolproxy->id;
90
            } else {
91
                $ok = $vendorcode === 'tool';
92
                $typeid = intval($productcode);
93
                $id = -$typeid;
94
            }
95
            $contenttype = $response->get_accept();
96
            $simpleformat = !empty($contenttype) && ($contenttype == $this->formats[1]);
97
            if ($ok) {
98
                $ok = (empty($bubble) || ((($bubble == 'distinct') || ($bubble == 'all')))) &&
99
                     (!$simpleformat || empty($bubble) || ($bubble != 'all')) &&
100
                     (empty($bubble) || ($response->get_request_method() == 'GET'));
101
            }
102
 
103
            if (!$ok) {
104
                $response->set_code(404);
105
            } else {
106
                $systemsetting = null;
107
                $contextsettings = lti_get_tool_settings($id, $contextid);
108
                if (!empty($bubble)) {
109
                    $systemsetting = new systemsettings($this->get_service());
110
                    $systemsetting->params['tool_proxy_id'] = $productcode;
111
                    if ($id >= 0) {
112
                        $systemsetting->params['config_type'] = 'toolproxy';
113
                    } else {
114
                        $systemsetting->params['config_type'] = 'tool';
115
                    }
116
                    $systemsettings = lti_get_tool_settings($id);
117
                    if ($bubble == 'distinct') {
118
                        toolsettings::distinct_settings($systemsettings, $contextsettings, null);
119
                    }
120
                } else {
121
                    $systemsettings = null;
122
                }
123
                if ($response->get_request_method() == 'GET') {
124
                    $json = '';
125
                    if ($simpleformat) {
126
                        $response->set_content_type($this->formats[1]);
127
                        $json .= "{";
128
                    } else {
129
                        $response->set_content_type($this->formats[0]);
130
                        $json .= "{\n  \"@context\":\"http://purl.imsglobal.org/ctx/lti/v2/ToolSettings\",\n  \"@graph\":[\n";
131
                    }
132
                    $settings = toolsettings::settings_to_json($systemsettings, $simpleformat, 'ToolProxy', $systemsetting);
133
                    $json .= $settings;
134
                    $isfirst = strlen($settings) <= 0;
135
                    $settings = toolsettings::settings_to_json($contextsettings, $simpleformat, 'ToolProxyBinding', $this);
136
                    if ((strlen($settings) > 0) && !$isfirst) {
137
                        $json .= ",";
138
                    }
139
                    $json .= $settings;
140
                    if ($simpleformat) {
141
                        $json .= "\n}";
142
                    } else {
143
                        $json .= "\n  ]\n}";
144
                    }
145
                    $response->set_body($json);
146
                } else { // PUT.
147
                    $settings = null;
148
                    if ($response->get_content_type() == $this->formats[0]) {
149
                        $json = json_decode($response->get_request_data());
150
                        $ok = !empty($json);
151
                        if ($ok) {
152
                            $ok = isset($json->{"@graph"}) && is_array($json->{"@graph"}) && (count($json->{"@graph"}) == 1) &&
153
                                  ($json->{"@graph"}[0]->{"@type"} == 'ToolProxyBinding');
154
                        }
155
                        if ($ok) {
156
                            $settings = $json->{"@graph"}[0]->custom;
157
                            unset($settings->{'@id'});
158
                        }
159
                    } else {  // Simple JSON.
160
                        $json = json_decode($response->get_request_data(), true);
161
                        $ok = !empty($json);
162
                        if ($ok) {
163
                            $ok = is_array($json);
164
                        }
165
                        if ($ok) {
166
                            $settings = $json;
167
                        }
168
                    }
169
                    if ($ok) {
170
                        lti_set_tool_settings($settings, $id, $contextid);
171
                    } else {
172
                        $response->set_code(406);
173
                    }
174
                }
175
            }
176
        }
177
    }
178
 
179
    /**
180
     * Parse a value for custom parameter substitution variables.
181
     *
182
     * @param string $value String to be parsed
183
     *
184
     * @return string
185
     */
186
    public function parse_value($value) {
187
        global $COURSE;
188
 
189
        if (strpos($value, '$ToolProxyBinding.custom.url') !== false) {
190
            if ($COURSE->format == 'site') {
191
                $this->params['context_type'] = 'Group';
192
            } else {
193
                $this->params['context_type'] = 'CourseSection';
194
            }
195
            $this->params['context_id'] = $COURSE->id;
196
            if (!empty($this->get_service()->get_tool_proxy())) {
197
                $this->params['vendor_code'] = $this->get_service()->get_tool_proxy()->vendorcode;
198
                $this->params['product_code'] = $this->get_service()->get_tool_proxy()->guid;
199
            } else {
200
                $this->params['vendor_code'] = 'tool';
201
                $this->params['product_code'] = $this->get_service()->get_type()->id;
202
            }
203
            $value = str_replace('$ToolProxyBinding.custom.url', parent::get_endpoint(), $value);
204
        }
205
        return $value;
206
 
207
    }
208
 
209
}