Merge branch 'MDL-70941-310' of git://github.com/dravek/moodle into MOODLE_310_STABLE
[moodle.git] / badges / classes / backpack_api2p1_mapping.php
blob7419f3b65e23ce38e0022beb32b5425b954b237e
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.
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 * Represent the url for each method and the encoding of the parameters and response.
20 * The code is based on badges/classes/backpack_api_mapping.php by Yuliya Bozhko <yuliya.bozhko@totaralms.com>.
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>
27 namespace core_badges;
29 defined('MOODLE_INTERNAL') || die();
31 global $CFG;
32 require_once($CFG->libdir . '/filelib.php');
34 use context_system;
35 use curl;
37 /**
38 * Represent a single method for the remote api and this class using for Open Badge API v2.1 methods.
40 * @package core_badges
41 * @copyright 2020 Tung Thai
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class backpack_api2p1_mapping {
46 /** @var string The action of this method. */
47 public $action;
49 /** @var string The base url of this backpack. */
50 private $url;
52 /** @var array List of parameters for this method. */
53 public $params;
55 /** @var boolean This method returns an array of responses. */
56 public $multiple;
58 /** @var string get or post methods. */
59 public $method;
61 /** @var boolean json decode the response. */
62 public $json;
64 /** @var boolean Authentication is required for this request. */
65 public $authrequired;
67 /** @var boolean Differentiate the function that can be called on a user backpack or a site backpack. */
68 private $isuserbackpack;
70 /**
71 * Create a mapping.
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.
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;
96 /**
97 * Does the action match this mapping?
99 * @param string $action The action.
100 * @return boolean
102 public function is_match($action) {
103 return $this->action == $action;
107 * Parse the method url and insert parameters.
109 * @param string $apiurl The raw apiurl.
110 * @return string
112 private function get_url($apiurl) {
113 $urlscheme = parse_url($apiurl, PHP_URL_SCHEME);
114 $urlhost = parse_url($apiurl, PHP_URL_HOST);
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);
121 return $url;
125 * Standard options used for all curl requests.
127 * @return array
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,
143 * Make an api request and parse the response.
145 * @param string $apiurl Raw request url.
146 * @param string $tokenkey to verify authorization.
147 * @param array $post request method.
148 * @return bool|mixed
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);
157 if ($this->json) {
158 $curl->setHeader(array('Content-type: application/json'));
159 if ($this->method == 'post') {
160 $post = json_encode($post);
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);
171 $response = json_decode($response);
172 if (isset($response->result)) {
173 $response = $response->result;
176 return $response;