on-demand release 4.0dev+
[moodle.git] / mod / lti / service / memberships / classes / local / resources / contextmemberships.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 Context 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;
c02324d9 31use core_availability\info_module;
591b3128 32
33defined('MOODLE_INTERNAL') || die();
34
35/**
36 * A resource implementing Context Memberships.
37 *
38 * @package ltiservice_memberships
39 * @since Moodle 3.0
40 * @copyright 2015 Vital Source Technologies http://vitalsource.com
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
c02324d9 43class contextmemberships extends resource_base {
591b3128 44
45 /**
46 * Class constructor.
47 *
c02324d9 48 * @param \ltiservice_memberships\local\service\memberships $service Service instance
591b3128 49 */
50 public function __construct($service) {
51
52 parent::__construct($service);
53 $this->id = 'ToolProxyBindingMemberships';
c02324d9 54 $this->template = '/{context_type}/{context_id}/bindings/{tool_code}/memberships';
591b3128 55 $this->variables[] = 'ToolProxyBinding.memberships.url';
56 $this->formats[] = 'application/vnd.ims.lis.v2.membershipcontainer+json';
27cbb596
SV
57 $this->formats[] = 'application/vnd.ims.lti-nrps.v2.membershipcontainer+json';
58 $this->methods[] = self::HTTP_GET;
591b3128 59
60 }
61
62 /**
63 * Execute the request for this resource.
64 *
c02324d9 65 * @param \mod_lti\local\ltiservice\response $response Response object for this request.
591b3128 66 */
67 public function execute($response) {
c02324d9 68 global $DB;
591b3128 69
70 $params = $this->parse_template();
71 $role = optional_param('role', '', PARAM_TEXT);
72 $limitnum = optional_param('limit', 0, PARAM_INT);
73 $limitfrom = optional_param('from', 0, PARAM_INT);
c02324d9
CV
74 $linkid = optional_param('rlid', '', PARAM_TEXT);
75 $lti = null;
76 $modinfo = null;
77
591b3128 78 if ($limitnum <= 0) {
79 $limitfrom = 0;
80 }
81
82 try {
27cbb596
SV
83 if (!$this->check_tool($params['tool_code'], $response->get_request_data(),
84 array(memberships::SCOPE_MEMBERSHIPS_READ))) {
85 throw new \Exception(null, 401);
591b3128 86 }
27cbb596
SV
87 if (!($course = $DB->get_record('course', array('id' => $params['context_id']), 'id,shortname,fullname',
88 IGNORE_MISSING))) {
89 throw new \Exception("Not Found: Course {$params['context_id']} doesn't exist", 404);
591b3128 90 }
27cbb596 91 if (!$this->get_service()->is_allowed_in_context($params['tool_code'], $course->id)) {
591b3128 92 throw new \Exception(null, 404);
93 }
27cbb596
SV
94 if (!($context = \context_course::instance($course->id))) {
95 throw new \Exception("Not Found: Course instance {$course->id} doesn't exist", 404);
96 }
c02324d9
CV
97 if (!empty($linkid)) {
98 if (!($lti = $DB->get_record('lti', array('id' => $linkid), 'id,course,typeid,servicesalt', IGNORE_MISSING))) {
27cbb596 99 throw new \Exception("Not Found: LTI link {$linkid} doesn't exist", 404);
c02324d9
CV
100 }
101 $modinfo = get_fast_modinfo($course);
102 $cm = get_coursemodule_from_instance('lti', $linkid, $lti->course, false, MUST_EXIST);
103 $cm = $modinfo->get_cm($cm->id);
104 $modinfo = new info_module($cm);
105 if ($modinfo->is_available_for_all()) {
106 $modinfo = null;
107 }
591b3128 108 }
591b3128 109
27cbb596
SV
110 $json = $this->get_service()->get_members_json($this, $context, $course, $role, $limitfrom, $limitnum, $lti,
111 $modinfo, $response);
112
591b3128 113 $response->set_body($json);
114
115 } catch (\Exception $e) {
116 $response->set_code($e->getCode());
27cbb596 117 $response->set_reason($e->getMessage());
c02324d9
CV
118 }
119 }
120
591b3128 121 /**
122 * Parse a value for custom parameter substitution variables.
123 *
124 * @param string $value String to be parsed
125 *
126 * @return string
127 */
128 public function parse_value($value) {
129 global $COURSE, $DB;
130
c02324d9
CV
131 if (strpos($value, '$ToolProxyBinding.memberships.url') !== false) {
132 if ($COURSE->id === SITEID) {
133 $this->params['context_type'] = 'Group';
134 } else {
135 $this->params['context_type'] = 'CourseSection';
136 }
137 $this->params['context_id'] = $COURSE->id;
27cbb596
SV
138 if ($tool = $this->get_service()->get_type()) {
139 $this->params['tool_code'] = $tool->id;
591b3128 140 }
c02324d9 141 $value = str_replace('$ToolProxyBinding.memberships.url', parent::get_endpoint(), $value);
591b3128 142 }
591b3128 143 return $value;
144
145 }
146
147}