weekly release 3.5dev
[moodle.git] / mod / lti / service / memberships / classes / local / resources / contextmemberships.php
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/>.
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  */
27 namespace ltiservice_memberships\local\resources;
29 use \mod_lti\local\ltiservice\service_base;
30 use ltiservice_memberships\local\service\memberships;
32 defined('MOODLE_INTERNAL') || die();
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  */
42 class contextmemberships extends \mod_lti\local\ltiservice\resource_base {
44     /**
45      * Class constructor.
46      *
47      * @param ltiservice_memberships\local\service\memberships $service Service instance
48      */
49     public function __construct($service) {
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';
58     }
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;
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         }
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);
96             $response->set_content_type($this->formats[0]);
97             $response->set_body($json);
99         } catch (\Exception $e) {
100             $response->set_code($e->getCode());
101         }
102     }
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;
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;
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);
133         return $value;
135     }