2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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();
32 require_once($CFG->libdir
. '/filelib.php');
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. */
49 /** @var string The base url of this backpack. */
52 /** @var array List of parameters for this method. */
55 /** @var boolean This method returns an array of responses. */
58 /** @var string get or post methods. */
61 /** @var boolean json decode the response. */
64 /** @var boolean Authentication is required for this request. */
67 /** @var boolean Differentiate the function that can be called on a user backpack or a site backpack. */
68 private $isuserbackpack;
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;
87 $this->postparams
= $postparams;
88 $this->multiple
= $multiple;
89 $this->method
= $method;
91 $this->authrequired
= $authrequired;
92 $this->isuserbackpack
= $isuserbackpack;
93 $this->backpackapiversion
= $backpackapiversion;
97 * Does the action match this mapping?
99 * @param string $action The action.
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.
112 private function get_url($apiurl) {
113 $urlscheme = parse_url($apiurl, PHP_URL_SCHEME
);
114 $urlhost = parse_url($apiurl, PHP_URL_HOST
);
117 $url = str_replace('[SCHEME]', $urlscheme, $url);
118 $url = str_replace('[HOST]', $urlhost, $url);
119 $url = str_replace('[URL]', $apiurl, $url);
125 * Standard options used for all curl requests.
129 private function get_curl_options() {
131 'FRESH_CONNECT' => true,
132 'RETURNTRANSFER' => true,
133 'FORBID_REUSE' => true,
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.
150 public function request($apiurl, $tokenkey, $post = []) {
152 $url = $this->get_url($apiurl);
154 $curl->setHeader('Authorization: Bearer ' . $tokenkey);
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
;