MDL-61574 mod_lti: removed usage of PHP7 reserved word in namespace
[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
29use \mod_lti\local\ltiservice\service_base;
30use ltiservice_memberships\local\service\memberships;
31use core_availability\info;
32use core_availability\info_module;
33
34defined('MOODLE_INTERNAL') || die();
35
36/**
37 * A resource implementing Link Memberships.
38 *
39 * @package ltiservice_memberships
40 * @since Moodle 3.0
41 * @copyright 2015 Vital Source Technologies http://vitalsource.com
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 */
44class linkmemberships extends \mod_lti\local\ltiservice\resource_base {
45
46 /**
47 * Class constructor.
48 *
49 * @param ltiservice_memberships\local\service\memberships $service Service instance
50 */
51 public function __construct($service) {
52
53 parent::__construct($service);
54 $this->id = 'LtiLinkMemberships';
55 $this->template = '/links/{link_id}/memberships';
56 $this->variables[] = 'LtiLink.memberships.url';
57 $this->formats[] = 'application/vnd.ims.lis.v2.membershipcontainer+json';
58 $this->methods[] = 'GET';
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 global $CFG, $DB;
69
70 $params = $this->parse_template();
71 $linkid = $params['link_id'];
72 $role = optional_param('role', '', PARAM_TEXT);
73 $limitnum = optional_param('limit', 0, PARAM_INT);
74 $limitfrom = optional_param('from', 0, PARAM_INT);
75 if ($limitnum <= 0) {
76 $limitfrom = 0;
77 }
78
79 try {
80 if (empty($linkid)) {
81 throw new \Exception(null, 404);
82 }
83 if (!($lti = $DB->get_record('lti', array('id' => $linkid), 'id,course,typeid,servicesalt', IGNORE_MISSING))) {
84 throw new \Exception(null, 404);
85 }
86 $tool = $DB->get_record('lti_types', array('id' => $lti->typeid));
87 $toolproxy = $DB->get_record('lti_tool_proxies', array('id' => $tool->toolproxyid));
88 if (!$this->check_tool_proxy($toolproxy->guid, $response->get_request_data())) {
89 throw new \Exception(null, 401);
90 }
91 if (!($course = $DB->get_record('course', array('id' => $lti->course), 'id', IGNORE_MISSING))) {
92 throw new \Exception(null, 404);
93 }
94 if (!($context = \context_course::instance($lti->course))) {
95 throw new \Exception(null, 404);
96 }
97 $modinfo = get_fast_modinfo($course);
98 $cm = get_coursemodule_from_instance('lti', $linkid, $lti->course, false, MUST_EXIST);
99 $cm = $modinfo->get_cm($cm->id);
100 $info = new info_module($cm);
101 if ($info->is_available_for_all()) {
102 $info = null;
103 }
104
105 $json = memberships::get_users_json($this, $context, $lti->course, $tool, $role, $limitfrom, $limitnum, $lti, $info);
106
107 $response->set_content_type($this->formats[0]);
108 $response->set_body($json);
109
110 } catch (\Exception $e) {
111 $response->set_code($e->getCode());
112 }
113
114 }
115
116 /**
117 * Parse a value for custom parameter substitution variables.
118 *
119 * @param string $value String to be parsed
120 *
121 * @return string
122 */
123 public function parse_value($value) {
124
125 $id = optional_param('id', 0, PARAM_INT); // Course Module ID.
126 if (!empty($id)) {
127 $cm = get_coursemodule_from_id('lti', $id, 0, false, MUST_EXIST);
128 $this->params['link_id'] = $cm->instance;
129 }
130 $value = str_replace('$LtiLink.memberships.url', parent::get_endpoint(), $value);
131
132 return $value;
133
134 }
135
136}