moodlefetcher.php 1.26 KB
Newer Older
Francois Gannaz's avatar
Francois Gannaz committed
1
2
3
4
<?php

/**
 * @license http://www.gnu.org/licenses/gpl-3.0.html  GNU GPL v3
Francois Gannaz's avatar
Francois Gannaz committed
5
6
 * @package    mod_labnbook
 * @copyright  2019 Université Grenoble Alpes
Francois Gannaz's avatar
Francois Gannaz committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 */

namespace mod_labnbook\fetch;

abstract class moodlefetcher
{
    /**
     * @var fetcher
     */
    protected $fetcher;

    public function __construct() {
        global $CFG;
        $this->fetcher = new fetcher(
            $CFG->labnbook_api_url,
            $this->getSigningKey(),
            $this->getEnvironment()
        );
    }

    protected function getEnvironment() : environment {
        global $CFG, $USER;
        $env = new environment();
30
        $env->orig = "inst"; // default value
Francois Gannaz's avatar
Francois Gannaz committed
31
32
33
34
35
        $env->iss = (int) $CFG->labnbook_institution_id;
        $env->sub = (int) $USER->id;
        return $env;
    }

36
    protected function fetch($verb, $path, $payload = []) {
Francois Gannaz's avatar
Francois Gannaz committed
37
38
39
40
        global $SESSION;
        $response = $this->fetcher->fetch($verb, $path, $payload);
        if (!empty($response->auth)) {
            if (empty($SESSION->labnbook)) {
41
                $SESSION->labnbook = new \Stdclass;
Francois Gannaz's avatar
Francois Gannaz committed
42
            }
43
            $SESSION->labnbook->auth = $response->auth;
Francois Gannaz's avatar
Francois Gannaz committed
44
45
46
47
48
49
        }
        return isset($response->data) ? $response->data : $response;
    }

    abstract protected function getSigningKey() : string;
}