-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.MoodleElgg.php
134 lines (111 loc) · 3.75 KB
/
class.MoodleElgg.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
require_once('elgg_api.php');
class MoodleElgg extends ElggApi {
private $token = '';
private $group_guid = '';
private $error = '';
private $elgg_url;
public function __construct(array $params) {
$this->elgg_url = $params['elgg_url'] . 'services/api/rest/json/';
$this->public_key = $params['public_key'];
$this->private_key = $params['private_key'];
$this->getToken();
if ($this->token != '') {
$this->group_guid = $this->getGroupGUID();
}
}
/**
* Get an authentication token from Elgg.
*
* @param string $username
* @param strgin $time
*/
private function getToken() {
global $USER;
$time = microtime(true);
$username = $USER->username;
$params = 'method=moodle.get_auth_token&username=' . $username . '&time=' . $time . '&code=' . $this->calculateCode($username, $time);
$this->token = $this->sendRequest($params);
}
/**
* Send request and handle the result.
*
* @param string $params
* @param string $method
*/
public function sendRequest($params) {
// Use token for authentication if already provided
if ( !empty($this->token) && strstr($this->elgg_url, 'auth_token') === false ) {
$params .= '&auth_token=' . $this->token;
}
$url = $this->elgg_url . '?' . $params;
if ( $response = $this->sendApiCall($url, $params) ) {
if ( $response->status == -1 ) {
$this->error = $response->message;
return false;
// The token has expired. Get a new one and the request with new token.
// @todo Should this be used? May cause infinite loops if an error occurs.
//$this->getToken();
//$this->sendRequest($params, $method);
} else {
if ( $response->status == 0 ) {
// Request was succesfull. Return the requested data.
return $response->result;
} else {
$this->error = $response->message;
return false;
}
}
} else {
$this->error = "API call to address $url failed!.";
return false;
}
}
/**
* Get the group guid for this group. If the group doesn't exist, it is created.
* @return mixed Int on success, false on failure.
*/
public function getGroupGUID() {
global $COURSE;
if ( $this->group_guid !== '' ) {
return $this->group_guid;
}
// Get the groupGUID from Elgg
$params = 'method=moodle.get_groupGUID&shortname=' . $COURSE->shortname;
return $this->sendRequest($params);
}
public function getGroupDiscussions() {
$params = 'method=moodle.get_group_discussions&group_guid=' . $this->getGroupGUID();
return $this->sendRequest($params);
}
public function getObjects($object_type, $tag) {
$params = 'method=moodle.get_objects&object_type=' . $object_type . '&tag=' . $tag;
return $this->sendRequest($params);
}
/**
* Create code used for initial authentication.
*
* @param float $time
* @return string $code
*/
private function calculateCode($username, $time) {
$code = md5($username . $time . $this->private_key);
return $code;
}
/**
* Check if an error has occurred.
*/
public function hasError() {
if (empty($this->error)) {
return false;
} else {
return true;
}
}
/**
* Return possible error message.
*/
public function getError() {
return $this->error;
}
}