1 |
efrain |
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 |
/** @var mixed List of parameters for this method. */
|
|
|
71 |
protected $postparams;
|
|
|
72 |
|
|
|
73 |
/** @var int OpenBadges version 1 or 2. */
|
|
|
74 |
protected $backpackapiversion;
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Create a mapping.
|
|
|
78 |
*
|
|
|
79 |
* @param string $action The action of this method.
|
|
|
80 |
* @param string $url The base url of this backpack.
|
|
|
81 |
* @param mixed $postparams List of parameters for this method.
|
|
|
82 |
* @param boolean $multiple This method returns an array of responses.
|
|
|
83 |
* @param string $method get or post methods.
|
|
|
84 |
* @param boolean $json json decode the response.
|
|
|
85 |
* @param boolean $authrequired Authentication is required for this request.
|
|
|
86 |
* @param boolean $isuserbackpack user backpack or a site backpack.
|
|
|
87 |
* @param integer $backpackapiversion OpenBadges version 1 or 2.
|
|
|
88 |
*/
|
|
|
89 |
public function __construct($action, $url, $postparams,
|
|
|
90 |
$multiple, $method, $json, $authrequired, $isuserbackpack, $backpackapiversion) {
|
|
|
91 |
$this->action = $action;
|
|
|
92 |
$this->url = $url;
|
|
|
93 |
$this->postparams = $postparams;
|
|
|
94 |
$this->multiple = $multiple;
|
|
|
95 |
$this->method = $method;
|
|
|
96 |
$this->json = $json;
|
|
|
97 |
$this->authrequired = $authrequired;
|
|
|
98 |
$this->isuserbackpack = $isuserbackpack;
|
|
|
99 |
$this->backpackapiversion = $backpackapiversion;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Does the action match this mapping?
|
|
|
104 |
*
|
|
|
105 |
* @param string $action The action.
|
|
|
106 |
* @return boolean
|
|
|
107 |
*/
|
|
|
108 |
public function is_match($action) {
|
|
|
109 |
return $this->action == $action;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Parse the method url and insert parameters.
|
|
|
114 |
*
|
|
|
115 |
* @param string $apiurl The raw apiurl.
|
|
|
116 |
* @return string
|
|
|
117 |
*/
|
|
|
118 |
private function get_url($apiurl) {
|
|
|
119 |
$urlscheme = parse_url($apiurl, PHP_URL_SCHEME);
|
|
|
120 |
$urlhost = parse_url($apiurl, PHP_URL_HOST);
|
|
|
121 |
|
|
|
122 |
$url = $this->url;
|
|
|
123 |
$url = str_replace('[SCHEME]', $urlscheme, $url);
|
|
|
124 |
$url = str_replace('[HOST]', $urlhost, $url);
|
|
|
125 |
$url = str_replace('[URL]', $apiurl, $url);
|
|
|
126 |
|
|
|
127 |
return $url;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Standard options used for all curl requests.
|
|
|
132 |
*
|
|
|
133 |
* @return array
|
|
|
134 |
*/
|
|
|
135 |
private function get_curl_options() {
|
|
|
136 |
return array(
|
|
|
137 |
'FRESH_CONNECT' => true,
|
|
|
138 |
'RETURNTRANSFER' => true,
|
|
|
139 |
'FORBID_REUSE' => true,
|
|
|
140 |
'HEADER' => 0,
|
|
|
141 |
'CONNECTTIMEOUT' => 3,
|
|
|
142 |
'CONNECTTIMEOUT' => 3,
|
|
|
143 |
// Follow redirects with the same type of request when sent 301, or 302 redirects.
|
|
|
144 |
'CURLOPT_POSTREDIR' => 3,
|
|
|
145 |
);
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/**
|
|
|
149 |
* Make an api request and parse the response.
|
|
|
150 |
*
|
|
|
151 |
* @param string $apiurl Raw request url.
|
|
|
152 |
* @param string $tokenkey to verify authorization.
|
|
|
153 |
* @param array $post request method.
|
|
|
154 |
* @return bool|mixed
|
|
|
155 |
*/
|
|
|
156 |
public function request($apiurl, $tokenkey, $post = []) {
|
|
|
157 |
$curl = new curl();
|
|
|
158 |
$url = $this->get_url($apiurl);
|
|
|
159 |
if ($tokenkey) {
|
|
|
160 |
$curl->setHeader('Authorization: Bearer ' . $tokenkey);
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
if ($this->json) {
|
|
|
164 |
$curl->setHeader(array('Content-type: application/json'));
|
|
|
165 |
if ($this->method == 'post') {
|
|
|
166 |
$post = json_encode($post);
|
|
|
167 |
}
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
$curl->setHeader(array('Accept: application/json', 'Expect:'));
|
|
|
171 |
$options = $this->get_curl_options();
|
|
|
172 |
if ($this->method == 'get') {
|
|
|
173 |
$response = $curl->get($url, $post, $options);
|
|
|
174 |
} else if ($this->method == 'post') {
|
|
|
175 |
$response = $curl->post($url, $post, $options);
|
|
|
176 |
}
|
|
|
177 |
$response = json_decode($response);
|
|
|
178 |
if (isset($response->result)) {
|
|
|
179 |
$response = $response->result;
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
return $response;
|
|
|
183 |
}
|
|
|
184 |
}
|