Merge branch 'MDL-61761-master' of git://github.com/mihailges/moodle
[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
29use \mod_lti\local\ltiservice\service_base;
30use ltiservice_memberships\local\service\memberships;
31
32defined('MOODLE_INTERNAL') || die();
33
34/**
35 * A resource implementing Context Memberships.
36 *
37 * @package ltiservice_memberships
38 * @since Moodle 3.0
39 * @copyright 2015 Vital Source Technologies http://vitalsource.com
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class contextmemberships extends \mod_lti\local\ltiservice\resource_base {
43
44 /**
45 * Class constructor.
46 *
47 * @param ltiservice_memberships\local\service\memberships $service Service instance
48 */
49 public function __construct($service) {
50
51 parent::__construct($service);
52 $this->id = 'ToolProxyBindingMemberships';
53 $this->template = '/{context_type}/{context_id}/bindings/{vendor_code}/{product_code}/{tool_code}/memberships';
54 $this->variables[] = 'ToolProxyBinding.memberships.url';
55 $this->formats[] = 'application/vnd.ims.lis.v2.membershipcontainer+json';
56 $this->methods[] = 'GET';
57
58 }
59
60 /**
61 * Execute the request for this resource.
62 *
63 * @param mod_lti\local\ltiservice\response $response Response object for this request.
64 */
65 public function execute($response) {
66 global $CFG, $DB;
67
68 $params = $this->parse_template();
69 $role = optional_param('role', '', PARAM_TEXT);
70 $limitnum = optional_param('limit', 0, PARAM_INT);
71 $limitfrom = optional_param('from', 0, PARAM_INT);
72 if ($limitnum <= 0) {
73 $limitfrom = 0;
74 }
75
76 try {
77 if (!$this->get_service()->check_tool_proxy($params['product_code'])) {
78 throw new \Exception(null, 401);
79 }
80 if (!($course = $DB->get_record('course', array('id' => $params['context_id']), 'id', IGNORE_MISSING))) {
81 throw new \Exception(null, 404);
82 }
83 if (!($context = \context_course::instance($course->id))) {
84 throw new \Exception(null, 404);
85 }
86 if (!($tool = $DB->get_record('lti_types', array('id' => $params['tool_code']),
87 'toolproxyid,enabledcapability,parameter', IGNORE_MISSING))) {
88 throw new \Exception(null, 404);
89 }
90 $toolproxy = $DB->get_record('lti_tool_proxies', array('id' => $tool->toolproxyid), 'guid', IGNORE_MISSING);
91 if (!$toolproxy || ($toolproxy->guid !== $this->get_service()->get_tool_proxy()->guid)) {
92 throw new \Exception(null, 400);
93 }
94 $json = memberships::get_users_json($this, $context, $course->id, $tool, $role, $limitfrom, $limitnum, null, null);
95
96 $response->set_content_type($this->formats[0]);
97 $response->set_body($json);
98
99 } catch (\Exception $e) {
100 $response->set_code($e->getCode());
101 }
102 }
103
104 /**
105 * Parse a value for custom parameter substitution variables.
106 *
107 * @param string $value String to be parsed
108 *
109 * @return string
110 */
111 public function parse_value($value) {
112 global $COURSE, $DB;
113
114 if ($COURSE->id === SITEID) {
115 $this->params['context_type'] = 'Group';
116 } else {
117 $this->params['context_type'] = 'CourseSection';
118 }
119 $this->params['context_id'] = $COURSE->id;
120 $this->params['vendor_code'] = $this->get_service()->get_tool_proxy()->vendorcode;
121 $this->params['product_code'] = $this->get_service()->get_tool_proxy()->guid;
122
123 $id = optional_param('id', 0, PARAM_INT); // Course Module ID.
124 if (!empty($id)) {
125 $cm = get_coursemodule_from_id('lti', $id, 0, false, IGNORE_MISSING);
126 $lti = $DB->get_record('lti', array('id' => $cm->instance), 'typeid', IGNORE_MISSING);
127 if ($lti && !empty($lti->typeid)) {
128 $this->params['tool_code'] = $lti->typeid;
129 }
130 }
131 $value = str_replace('$ToolProxyBinding.memberships.url', parent::get_endpoint(), $value);
132
133 return $value;
134
135 }
136
137}