Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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  
  27  namespace ltiservice_memberships\local\resources;
  28  
  29  use mod_lti\local\ltiservice\resource_base;
  30  use ltiservice_memberships\local\service\memberships;
  31  use core_availability\info_module;
  32  
  33  defined('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   */
  43  class contextmemberships extends resource_base {
  44  
  45      /**
  46       * Class constructor.
  47       *
  48       * @param \ltiservice_memberships\local\service\memberships $service Service instance
  49       */
  50      public function __construct($service) {
  51  
  52          parent::__construct($service);
  53          $this->id = 'ToolProxyBindingMemberships';
  54          $this->template = '/{context_type}/{context_id}/bindings/{tool_code}/memberships';
  55          $this->variables[] = 'ToolProxyBinding.memberships.url';
  56          $this->formats[] = 'application/vnd.ims.lis.v2.membershipcontainer+json';
  57          $this->formats[] = 'application/vnd.ims.lti-nrps.v2.membershipcontainer+json';
  58          $this->methods[] = self::HTTP_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 $DB;
  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);
  74          $linkid = optional_param('rlid', '', PARAM_TEXT);
  75          $lti = null;
  76          $modinfo = null;
  77  
  78          if ($limitnum <= 0) {
  79              $limitfrom = 0;
  80          }
  81  
  82          try {
  83              if (!$this->check_tool($params['tool_code'], $response->get_request_data(),
  84                  array(memberships::SCOPE_MEMBERSHIPS_READ))) {
  85                  throw new \Exception(null, 401);
  86              }
  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);
  90              }
  91              if (!$this->get_service()->is_allowed_in_context($params['tool_code'], $course->id)) {
  92                  throw new \Exception(null, 404);
  93              }
  94              if (!($context = \context_course::instance($course->id))) {
  95                  throw new \Exception("Not Found: Course instance {$course->id} doesn't exist", 404);
  96              }
  97              if (!empty($linkid)) {
  98                  if (!($lti = $DB->get_record('lti', array('id' => $linkid), 'id,course,typeid,servicesalt', IGNORE_MISSING))) {
  99                      throw new \Exception("Not Found: LTI link {$linkid} doesn't exist", 404);
 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                  }
 108              }
 109  
 110              $json = $this->get_service()->get_members_json($this, $context, $course, $role, $limitfrom, $limitnum, $lti,
 111                  $modinfo, $response);
 112  
 113              $response->set_body($json);
 114  
 115          } catch (\Exception $e) {
 116              $response->set_code($e->getCode());
 117              $response->set_reason($e->getMessage());
 118          }
 119      }
 120  
 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  
 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;
 138              if ($tool = $this->get_service()->get_type()) {
 139                  $this->params['tool_code'] = $tool->id;
 140              }
 141              $value = str_replace('$ToolProxyBinding.memberships.url', parent::get_endpoint(), $value);
 142          }
 143          return $value;
 144  
 145      }
 146  
 147  }