on-demand release 4.0dev+
[moodle.git] / mod / lti / service / memberships / classes / local / resources / linkmemberships.php
CommitLineData
591b3128 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 Link Memberships resource
19 *
20 * @package ltiservice_memberships
21 * @copyright 2015 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_memberships\local\resources;
591b3128 28
c02324d9 29use mod_lti\local\ltiservice\resource_base;
591b3128 30use ltiservice_memberships\local\service\memberships;
591b3128 31use core_availability\info_module;
32
33defined('MOODLE_INTERNAL') || die();
34
35/**
36 * A resource implementing Link Memberships.
c02324d9
CV
37 * The link membership is no longer defined in the published
38 * version of the LTI specification. It is replaced by the
39 * rlid parameter in the context membership URL.
591b3128 40 *
41 * @package ltiservice_memberships
42 * @since Moodle 3.0
43 * @copyright 2015 Vital Source Technologies http://vitalsource.com
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 */
c02324d9 46class linkmemberships extends resource_base {
591b3128 47
48 /**
49 * Class constructor.
50 *
c02324d9 51 * @param \ltiservice_memberships\local\service\memberships $service Service instance
591b3128 52 */
53 public function __construct($service) {
54
55 parent::__construct($service);
56 $this->id = 'LtiLinkMemberships';
57 $this->template = '/links/{link_id}/memberships';
58 $this->variables[] = 'LtiLink.memberships.url';
59 $this->formats[] = 'application/vnd.ims.lis.v2.membershipcontainer+json';
60 $this->methods[] = 'GET';
61
62 }
63
64 /**
65 * Execute the request for this resource.
66 *
c02324d9 67 * @param \mod_lti\local\ltiservice\response $response Response object for this request.
591b3128 68 */
69 public function execute($response) {
c02324d9 70 global $DB;
591b3128 71
72 $params = $this->parse_template();
73 $linkid = $params['link_id'];
74 $role = optional_param('role', '', PARAM_TEXT);
75 $limitnum = optional_param('limit', 0, PARAM_INT);
76 $limitfrom = optional_param('from', 0, PARAM_INT);
c02324d9 77
591b3128 78 if ($limitnum <= 0) {
79 $limitfrom = 0;
80 }
81
c02324d9
CV
82 if (empty($linkid)) {
83 $response->set_code(404);
84 return;
85 }
86 if (!($lti = $DB->get_record('lti', array('id' => $linkid), 'id,course,typeid,servicesalt', IGNORE_MISSING))) {
87 $response->set_code(404);
88 return;
89 }
5140a7bb
SV
90 if (!$this->check_tool($lti->typeid, $response->get_request_data(), array(memberships::SCOPE_MEMBERSHIPS_READ))) {
91 $response->set_code(403);
92 return;
93 }
c02324d9
CV
94 if (!($course = $DB->get_record('course', array('id' => $lti->course), 'id', IGNORE_MISSING))) {
95 $response->set_code(404);
96 return;
97 }
98 if (!($context = \context_course::instance($lti->course))) {
99 $response->set_code(404);
100 return;
101 }
102 $modinfo = get_fast_modinfo($course);
103 $cm = get_coursemodule_from_instance('lti', $linkid, $lti->course, false, MUST_EXIST);
104 $cm = $modinfo->get_cm($cm->id);
105 $info = new info_module($cm);
106 if ($info->is_available_for_all()) {
107 $info = null;
108 }
5140a7bb 109 $json = $this->get_service()->get_members_json($this, $context, $course, $role,
27cbb596 110 $limitfrom, $limitnum, $lti, $info, $response);
591b3128 111
c02324d9
CV
112 $response->set_content_type($this->formats[0]);
113 $response->set_body($json);
114 }
591b3128 115
c02324d9
CV
116 /**
117 * get permissions from the config of the tool for that resource
118 *
119 * @param string $typeid
120 *
121 * @return array with the permissions related to this resource by the $lti_type or null if none.
122 */
123 public function get_permissions($typeid) {
124 $tool = lti_get_type_type_config($typeid);
125 if ($tool->memberships == '1') {
126 return array('ToolProxyBinding.memberships.url:get');
127 } else {
128 return array();
591b3128 129 }
591b3128 130 }
131
132 /**
133 * Parse a value for custom parameter substitution variables.
134 *
135 * @param string $value String to be parsed
136 *
137 * @return string
138 */
139 public function parse_value($value) {
140
520c5e08 141 if (strpos($value, '$LtiLink.memberships.url') !== false) {
c02324d9
CV
142 $id = optional_param('id', 0, PARAM_INT); // Course Module ID.
143 if (!empty($id)) {
144 $cm = get_coursemodule_from_id('lti', $id, 0, false, MUST_EXIST);
145 $this->params['link_id'] = $cm->instance;
146 }
147 $value = str_replace('$LtiLink.memberships.url', parent::get_endpoint(), $value);
591b3128 148 }
591b3128 149 return $value;
150
151 }
152
153}