Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 29... Línea 29...
29
 
29
 
30
global $CFG;
30
global $CFG;
Línea 31... Línea 31...
31
require_once($CFG->libdir . '/filelib.php');
31
require_once($CFG->libdir . '/filelib.php');
32
 
-
 
33
use context_system;
-
 
34
use core_badges\external\assertion_exporter;
-
 
35
use core_badges\external\collection_exporter;
-
 
36
use core_badges\external\issuer_exporter;
32
 
Línea 37... Línea 33...
37
use core_badges\external\badgeclass_exporter;
33
use context_system;
38
use curl;
34
use curl;
39
 
35
 
Línea 80... Línea 76...
80
    private static $authenticationerror = '';
76
    private static $authenticationerror = '';
Línea 81... Línea 77...
81
 
77
 
82
    /** @var mixed List of parameters for this method. */
78
    /** @var mixed List of parameters for this method. */
Línea 83... Línea 79...
83
    protected $postparams;
79
    protected $postparams;
84
 
80
 
Línea -... Línea 81...
-
 
81
    /** @var int OpenBadges version. */
-
 
82
    protected $backpackapiversion;
-
 
83
 
85
    /** @var int OpenBadges version 1 or 2. */
84
    /** @var array Errors encountered during the request. */
86
    protected $backpackapiversion;
85
    protected $errors = [];
87
 
86
 
88
    /**
87
    /**
89
     * Create a mapping.
88
     * Create a mapping.
Línea 96... Línea 95...
96
     * @param boolean $multiple This method returns an array of responses.
95
     * @param boolean $multiple This method returns an array of responses.
97
     * @param string $method get or post methods.
96
     * @param string $method get or post methods.
98
     * @param boolean $json json decode the response.
97
     * @param boolean $json json decode the response.
99
     * @param boolean $authrequired Authentication is required for this request.
98
     * @param boolean $authrequired Authentication is required for this request.
100
     * @param boolean $isuserbackpack user backpack or a site backpack.
99
     * @param boolean $isuserbackpack user backpack or a site backpack.
101
     * @param integer $backpackapiversion OpenBadges version 1 or 2.
100
     * @param integer $backpackapiversion OpenBadges version.
102
     */
101
     */
103
    public function __construct($action, $url, $postparams, $requestexporter, $responseexporter,
102
    public function __construct($action, $url, $postparams, $requestexporter, $responseexporter,
104
                                $multiple, $method, $json, $authrequired, $isuserbackpack, $backpackapiversion) {
103
                                $multiple, $method, $json, $authrequired, $isuserbackpack, $backpackapiversion) {
105
        $this->action = $action;
104
        $this->action = $action;
106
        $this->url = $url;
105
        $this->url = $url;
Línea 149... Línea 148...
149
    public static function get_authentication_error() {
148
    public static function get_authentication_error() {
150
        return self::$authenticationerror;
149
        return self::$authenticationerror;
151
    }
150
    }
Línea 152... Línea 151...
152
 
151
 
-
 
152
    /**
-
 
153
     * Get the errors encountered during the request.
-
 
154
     *
-
 
155
     * @return array The list of errors.
-
 
156
     */
-
 
157
    public function get_errors() {
-
 
158
        return $this->errors;
-
 
159
    }
-
 
160
 
-
 
161
    /**
-
 
162
     * Add an error to the list of errors.
-
 
163
     *
-
 
164
     * @param string $error The error message.
-
 
165
     * @return self This instance for method chaining.
-
 
166
     */
-
 
167
    public function add_error(string $error): self {
-
 
168
        $this->errors[] = $error;
-
 
169
        return $this;
-
 
170
    }
-
 
171
 
153
    /**
172
    /**
154
     * Does the action match this mapping?
173
     * Does the action match this mapping?
155
     *
174
     *
156
     * @param string $action The action.
175
     * @param string $action The action.
157
     * @return boolean
176
     * @return boolean
Línea 232... Línea 251...
232
        }
251
        }
233
        return $request;
252
        return $request;
234
    }
253
    }
Línea 235... Línea 254...
235
 
254
 
236
    /**
-
 
237
     * Read the response from a V1 user request and save the userID.
-
 
238
     *
-
 
239
     * @param string $response The request response.
-
 
240
     * @param integer $backpackid The backpack id.
-
 
241
     * @return mixed
-
 
242
     */
-
 
243
    private function convert_email_response($response, $backpackid) {
-
 
244
        global $SESSION;
-
 
245
 
-
 
246
        if (isset($response->status) && $response->status == 'okay') {
-
 
247
 
-
 
248
            // Remember the tokens.
-
 
249
            $useridkey = $this->get_token_key(BADGE_USER_ID_TOKEN);
-
 
250
            $backpackidkey = $this->get_token_key(BADGE_BACKPACK_ID_TOKEN);
-
 
251
 
-
 
252
            $SESSION->$useridkey = $response->userId;
-
 
253
            $SESSION->$backpackidkey = $backpackid;
-
 
254
            return $response->userId;
-
 
255
        }
-
 
256
        if (!empty($response->error)) {
-
 
257
            self::set_authentication_error($response->error);
-
 
258
        }
-
 
259
        return false;
-
 
260
    }
-
 
261
 
-
 
262
    /**
255
    /**
263
     * Get the user id from a previous user request.
256
     * Get the user id from a previous user request.
264
     *
257
     *
265
     * @return integer
258
     * @return integer
266
     */
259
     */
Línea 370... Línea 363...
370
            $response = $curl->post($url, $post, $options);
363
            $response = $curl->post($url, $post, $options);
371
        } else if ($this->method == 'put') {
364
        } else if ($this->method == 'put') {
372
            $response = $curl->put($url, $post, $options);
365
            $response = $curl->put($url, $post, $options);
373
        }
366
        }
374
        $response = json_decode($response);
367
        $response = json_decode($response);
-
 
368
        if ($response === null) {
-
 
369
            $this->add_error(get_string('invalidrequest', 'error'));
-
 
370
            return null;
-
 
371
        }
-
 
372
        if (isset($response->status) && isset($response->status->success) && $response->status->success != true) {
-
 
373
            // If the response wasn't successful, store the errors and return null.
-
 
374
            if (isset($response->validationErrors)) {
-
 
375
                $error = implode(', ', $response->validationErrors);
-
 
376
            } else if (isset($response->status->description)) {
-
 
377
                $error = $response->status->description;
-
 
378
            } else {
-
 
379
                $error = get_string('invalidrequest', 'error');
-
 
380
            }
-
 
381
            $this->add_error($error);
-
 
382
            return null;
-
 
383
        }
-
 
384
 
375
        if (isset($response->result)) {
385
        if (isset($response->result)) {
376
            $response = $response->result;
386
            $response = $response->result;
377
        }
387
        }
378
        $context = context_system::instance();
388
        $context = context_system::instance();
379
        $exporter = $this->responseexporter;
389
        $exporter = $this->responseexporter;