moodlefetcher.php 1.19 KB
Newer Older
Francois Gannaz's avatar
Francois Gannaz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php

/**
 * @license http://www.gnu.org/licenses/gpl-3.0.html  GNU GPL v3
 */

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();
28
        $env->orig = "inst"; // default value
Francois Gannaz's avatar
Francois Gannaz committed
29
30
31
32
33
        $env->iss = (int) $CFG->labnbook_institution_id;
        $env->sub = (int) $USER->id;
        return $env;
    }

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

    abstract protected function getSigningKey() : string;
}