Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 402] [Versions 310 and 403]

   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   * Represent the url for each method and the encoding of the parameters and response.
  19   *
  20   * The code is based on badges/classes/backpack_api_mapping.php by Yuliya Bozhko <yuliya.bozhko@totaralms.com>.
  21   *
  22   * @copyright  2020 Tung Thai
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @author     Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
  25   */
  26  
  27  namespace core_badges;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  global $CFG;
  32  require_once($CFG->libdir . '/filelib.php');
  33  
  34  use context_system;
  35  use curl;
  36  
  37  /**
  38   * Represent a single method for the remote api and this class using for Open Badge API v2.1 methods.
  39   *
  40   * @package   core_badges
  41   * @copyright  2020 Tung Thai
  42   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  43   */
  44  class backpack_api2p1_mapping {
  45  
  46      /** @var string The action of this method. */
  47      public $action;
  48  
  49      /** @var string The base url of this backpack. */
  50      private $url;
  51  
  52      /** @var array List of parameters for this method. */
  53      public $params;
  54  
  55      /** @var boolean This method returns an array of responses. */
  56      public $multiple;
  57  
  58      /** @var string get or post methods. */
  59      public $method;
  60  
  61      /** @var boolean json decode the response. */
  62      public $json;
  63  
  64      /** @var boolean Authentication is required for this request. */
  65      public $authrequired;
  66  
  67      /** @var boolean Differentiate the function that can be called on a user backpack or a site backpack. */
  68      private $isuserbackpack;
  69  
  70      /**
  71       * Create a mapping.
  72       *
  73       * @param string $action The action of this method.
  74       * @param string $url The base url of this backpack.
  75       * @param mixed $postparams List of parameters for this method.
  76       * @param boolean $multiple This method returns an array of responses.
  77       * @param string $method get or post methods.
  78       * @param boolean $json json decode the response.
  79       * @param boolean $authrequired Authentication is required for this request.
  80       * @param boolean $isuserbackpack user backpack or a site backpack.
  81       * @param integer $backpackapiversion OpenBadges version 1 or 2.
  82       */
  83      public function __construct($action, $url, $postparams,
  84                                  $multiple, $method, $json, $authrequired, $isuserbackpack, $backpackapiversion) {
  85          $this->action = $action;
  86          $this->url = $url;
  87          $this->postparams = $postparams;
  88          $this->multiple = $multiple;
  89          $this->method = $method;
  90          $this->json = $json;
  91          $this->authrequired = $authrequired;
  92          $this->isuserbackpack = $isuserbackpack;
  93          $this->backpackapiversion = $backpackapiversion;
  94      }
  95  
  96      /**
  97       * Does the action match this mapping?
  98       *
  99       * @param string $action The action.
 100       * @return boolean
 101       */
 102      public function is_match($action) {
 103          return $this->action == $action;
 104      }
 105  
 106      /**
 107       * Parse the method url and insert parameters.
 108       *
 109       * @param string $apiurl The raw apiurl.
 110       * @return string
 111       */
 112      private function get_url($apiurl) {
 113          $urlscheme = parse_url($apiurl, PHP_URL_SCHEME);
 114          $urlhost = parse_url($apiurl, PHP_URL_HOST);
 115  
 116          $url = $this->url;
 117          $url = str_replace('[SCHEME]', $urlscheme, $url);
 118          $url = str_replace('[HOST]', $urlhost, $url);
 119          $url = str_replace('[URL]', $apiurl, $url);
 120  
 121          return $url;
 122      }
 123  
 124      /**
 125       * Standard options used for all curl requests.
 126       *
 127       * @return array
 128       */
 129      private function get_curl_options() {
 130          return array(
 131              'FRESH_CONNECT'     => true,
 132              'RETURNTRANSFER'    => true,
 133              'FORBID_REUSE'      => true,
 134              'HEADER'            => 0,
 135              'CONNECTTIMEOUT'    => 3,
 136              'CONNECTTIMEOUT'    => 3,
 137              // Follow redirects with the same type of request when sent 301, or 302 redirects.
 138              'CURLOPT_POSTREDIR' => 3,
 139          );
 140      }
 141  
 142      /**
 143       * Make an api request and parse the response.
 144       *
 145       * @param string $apiurl Raw request url.
 146       * @param string $tokenkey to verify authorization.
 147       * @param array $post request method.
 148       * @return bool|mixed
 149       */
 150      public function request($apiurl, $tokenkey, $post = []) {
 151          $curl = new curl();
 152          $url = $this->get_url($apiurl);
 153          if ($tokenkey) {
 154              $curl->setHeader('Authorization: Bearer ' . $tokenkey);
 155          }
 156  
 157          if ($this->json) {
 158              $curl->setHeader(array('Content-type: application/json'));
 159              if ($this->method == 'post') {
 160                  $post = json_encode($post);
 161              }
 162          }
 163  
 164          $curl->setHeader(array('Accept: application/json', 'Expect:'));
 165          $options = $this->get_curl_options();
 166          if ($this->method == 'get') {
 167              $response = $curl->get($url, $post, $options);
 168          } else if ($this->method == 'post') {
 169              $response = $curl->post($url, $post, $options);
 170          }
 171          $response = json_decode($response);
 172          if (isset($response->result)) {
 173              $response = $response->result;
 174          }
 175  
 176          return $response;
 177      }
 178  }