Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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 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  
  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 Link Memberships.
  37   * The link membership is no longer defined in the published
  38   * version of the LTI specification. It is replaced by the
  39   * rlid parameter in the context membership URL.
  40   *
  41   * @package    ltiservice_memberships
  42   * @since      Moodle 3.0
  43   * @copyright  2015 Vital Source Technologies http://vitalsource.com
  44   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class linkmemberships extends resource_base {
  47  
  48      /**
  49       * Class constructor.
  50       *
  51       * @param \ltiservice_memberships\local\service\memberships $service Service instance
  52       */
  53      public function __construct($service) {
  54  
  55          parent::__construct($service);
  56          $this->id = 'LtiLinkMemberships';
  57          $this->template = '/links/{link_id}/memberships';
  58          $this->variables[] = 'LtiLink.memberships.url';
  59          $this->formats[] = 'application/vnd.ims.lis.v2.membershipcontainer+json';
  60          $this->methods[] = 'GET';
  61  
  62      }
  63  
  64      /**
  65       * Execute the request for this resource.
  66       *
  67       * @param \mod_lti\local\ltiservice\response $response  Response object for this request.
  68       */
  69      public function execute($response) {
  70          global $DB;
  71  
  72          $params = $this->parse_template();
  73          $linkid = $params['link_id'];
  74          $role = optional_param('role', '', PARAM_TEXT);
  75          $limitnum = optional_param('limit', 0, PARAM_INT);
  76          $limitfrom = optional_param('from', 0, PARAM_INT);
  77  
  78          if ($limitnum <= 0) {
  79              $limitfrom = 0;
  80          }
  81  
  82          if (empty($linkid)) {
  83              $response->set_code(404);
  84              return;
  85          }
  86          if (!($lti = $DB->get_record('lti', array('id' => $linkid), 'id,course,typeid,servicesalt', IGNORE_MISSING))) {
  87              $response->set_code(404);
  88              return;
  89          }
  90          if (!$this->check_tool($lti->typeid, $response->get_request_data(), array(memberships::SCOPE_MEMBERSHIPS_READ))) {
  91              $response->set_code(403);
  92              return;
  93          }
  94          if (!($course = $DB->get_record('course', array('id' => $lti->course), 'id', IGNORE_MISSING))) {
  95              $response->set_code(404);
  96              return;
  97          }
  98          if (!($context = \context_course::instance($lti->course))) {
  99              $response->set_code(404);
 100              return;
 101          }
 102          $modinfo = get_fast_modinfo($course);
 103          $cm = get_coursemodule_from_instance('lti', $linkid, $lti->course, false, MUST_EXIST);
 104          $cm = $modinfo->get_cm($cm->id);
 105          $info = new info_module($cm);
 106          if ($info->is_available_for_all()) {
 107              $info = null;
 108          }
 109          $json = $this->get_service()->get_members_json($this, $context, $course, $role,
 110                                                         $limitfrom, $limitnum, $lti, $info, $response);
 111  
 112          $response->set_content_type($this->formats[0]);
 113          $response->set_body($json);
 114      }
 115  
 116      /**
 117       * get permissions from the config of the tool for that resource
 118       *
 119       * @param string $typeid
 120       *
 121       * @return array with the permissions related to this resource by the $lti_type or null if none.
 122       */
 123      public function get_permissions($typeid) {
 124          $tool = lti_get_type_type_config($typeid);
 125          if ($tool->memberships == '1') {
 126              return array('ToolProxyBinding.memberships.url:get');
 127          } else {
 128              return array();
 129          }
 130      }
 131  
 132      /**
 133       * Parse a value for custom parameter substitution variables.
 134       *
 135       * @param string $value String to be parsed
 136       *
 137       * @return string
 138       */
 139      public function parse_value($value) {
 140  
 141          if (strpos($value, '$LtiLink.memberships.url') !== false) {
 142              $id = optional_param('id', 0, PARAM_INT); // Course Module ID.
 143              if (!empty($id)) {
 144                  $cm = get_coursemodule_from_id('lti', $id, 0, false, MUST_EXIST);
 145                  $this->params['link_id'] = $cm->instance;
 146              }
 147              $value = str_replace('$LtiLink.memberships.url', parent::get_endpoint(), $value);
 148          }
 149          return $value;
 150  
 151      }
 152  
 153  }