-
Notifications
You must be signed in to change notification settings - Fork 1
/
dcconnectorpoll.php
76 lines (67 loc) · 2.75 KB
/
dcconnectorpoll.php
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
// This file is part of Moodle - http:https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http:https://www.gnu.org/licenses/>.
/**
* Script that is polled to check for relationship requests
*
* @package mod_ilddigitalcert
* @copyright 2020 ILD TH Lübeck <[email protected]>
* @license http:https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__.'/../../config.php');
require_once('dcconnectorlib.php');
require_login();
$result = new stdClass();
if (isguestuser()) {
$result->status = get_string('not_logged_in', 'mod_ilddigitalcert');
echo json_encode($result);
exit;
}
$host = get_config('mod_ilddigitalcert', 'dchost');
$xapikey = get_config('mod_ilddigitalcert', 'dcxapikey');
callAPI('POST', $host.'/api/v2/Account/Sync', false, $xapikey);
$apiresult = callAPI('GET', $host.'/api/v2/Relationships', false, $xapikey);
$apiresult = json_decode($apiresult);
$templateid = get_user_preferences('mod_ilddigitalcert_template_id', 'error', $USER->id);
foreach ($apiresult->result as $ar) {
if ($templateid == $ar->template->id) {
if (count($ar->changes) == 1) {
if (checkrequest($ar)) {
// Accept request.
$data = '{"content": {}}';
$acceptresult = callAPI(
'PUT',
$host.'/api/v2/Relationships/'.$ar->id.'/Changes/'.$ar->changes[0]->id.'/Accept',
$data,
$xapikey
);
$accept = json_decode($acceptresult);
if (isset($accept->result->id)) {
set_user_preference('mod_ilddigitalcert_relationship_id', $accept->result->id, $USER->id);
unset_user_preference('mod_ilddigitalcert_template_id', $USER->id);
set_user_preference('mod_ilddigitalcert_wallet_id', $accept->result->peer, $USER->id);
$result->status = 'request_accepted';
}
} else {
$result->status = 'bad_request';
}
} else {
$result->status = 'bad_request';
}
exit(json_encode($result));
}
}
$result->status = 'polling';
echo json_encode($result);