-
Notifications
You must be signed in to change notification settings - Fork 1
/
ElggApiClient.php
109 lines (97 loc) · 2.93 KB
/
ElggApiClient.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
// Require class responsible for creating the actual API calls
require_once('ElggApiCall.php');
/**
* Class with customized API methods
*/
class ElggApiClient {
private $error = '';
private $keys = array();
private $elgg_url = '';
private $client;
private $token;
private $groupGuid;
/**
* Set API credentials.
*/
public function __construct(array $params) {
if (is_array($params)) {
if (isset($params['keys'])) {
$this->keys = $params['keys'];
}
$this->elgg_url = $params['elgg_url'];
}
$this->client = new ElggApiCall();
}
/**
* Initialize connection to Elgg.
*
* Get user specific authentication token if user is found from Elgg.
* If user doesn't exist a new Elgg user account will be created.
*
* @param array $params Array of user's name, username nad email
*/
public function init ($params) {
$this->token = $this->post('elgg.get_auth_token', $params);
return !empty($this->token);
}
/**
* Get group GUID.
*
* @param string $shortname The shortname of the course
* @return int
*/
public function getGroupGUID($shortname) {
if (isset($this->groupGuid)) {
return $this->groupGuid;
} else {
$this->groupGuid = $this->post('elgg.get_groupGUID', array('shortname' => $shortname));
return $this->groupGuid;
}
}
/**
* Send request and handle the result.
*
* @param string $method
* @param string $post_data
* @return mixed The result or false on error
*/
public function post($method, $params) {
$url = "{$this->elgg_url}services/api/rest/json/";
$call = array('method' => $method);
if (isset($this->token)) {
$params['auth_token'] = $this->token;
}
$post_data = null;
if (isset($params) && is_array($params)) {
$post_data = http_build_query($params);
}
try {
$results = $this->client->sendApiCall($this->keys, $url, $call, 'POST', $post_data);
$response = json_decode($results);
if (is_object($response)) {
if ($response->status === 0) {
// Request was succesfull. Return the requested data.
return $response->result;
} else {
$this->error = $response->message;
return false;
}
} else {
$this->error = get_string('misconfigured', 'block_elgg_community');
return false;
}
} catch (Exception $e) {
$this->error = $e->getMessage();
return false;
}
}
/**
* Return the possible error message.
*
* @return string
*/
public function getError() {
return $this->error;
}
}