-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoodleRest.php
477 lines (423 loc) · 13.1 KB
/
MoodleRest.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/**
* MoodleRest
*
* MoodleRest is a class to query Moodle REST webservices
*
* @package MoodleRest
* @version 2.4.0
* @author Lawrence Lagerlof <llagerlof@gmail.com>
* @copyright 2021 Lawrence Lagerlof
* @link http://github.com/llagerlof/MoodleRest
* @license https://opensource.org/licenses/MIT MIT
*/
class MoodleRest
{
/**
* The constant that defines the JSON return format
* @access public
*/
const RETURN_JSON = 'json';
/**
* The constant that defines the XML return format
* @access public
*/
const RETURN_XML = 'xml';
/**
* The constant that defines the ARRAY return format
* @access public
*/
const RETURN_ARRAY = 'array';
/**
* The constant that defines the request method using GET
* @access public
*/
const METHOD_GET = 'get';
/**
* The constant that defines the request method using POST
* @access public
*/
const METHOD_POST = 'post';
/**
* The full server address to Moodle REST webservices.
* @access private
*/
private $server_address;
/**
* The Moodle webservice token
* @access private
*/
private $token;
/**
* The return format (json, xml, array)
* @access private
*/
private $return_format = 'json'; // or xml
/**
* The RAW return data (as returned by the request. could be json or xml)
* @access private
*/
private $request_return;
/**
* The PARSED return data (could be json, xml or array)
* @access private
*/
private $parsed_return;
/**
* The full encoded URL used to access the webservice
* @access private
*/
private $url;
/**
* The full URL decoded
* @access private
*/
private $url_decoded;
/**
* The header string to be used in header() output
* @access private
*/
private $output_header;
/**
* The header string to be used in header() output
* @access private
*/
private $print_on_request = false;
/**
* The method to be used on request
* @access private
*/
private $method = 'get'; // or post
/**
* Print debug information to standard output?
* @access private
*/
private $debug = false;
/**
* Constructor
*
* @param string $server_address The full URL of Moodle rest server script. Eg: http://127.0.0.1/moodle/webservice/rest/server.php
*/
public function __construct($server_address = null, $token = null, $return_format = self::RETURN_ARRAY)
{
$this->server_address = $server_address;
$this->token = $token;
if (!is_null($return_format) && $return_format <> 'json' && $return_format <> 'xml' && $return_format <> 'array') {
throw new Exception("MoodleRest: Invalid return format: '$return_format'.");
}
$this->return_format = $return_format;
}
/**
* Set the full server address to Moodle REST webservices
*
* @param string $server_address The server address. eg:
*
* @return MoodleRest
*/
public function setServerAddress($server_address)
{
$this->server_address = $server_address;
return $this;
}
/**
* Get the server address to Moodle REST webservices
*
* @return string The server address
*/
public function getServerAddress()
{
return $this->server_address;
}
/**
* Set the Moodle token to access Moodle REST webservices
*
* @param string $token The oken generated by Moodle admin
*
* @return MoodleRest
*/
public function setToken($token)
{
$this->token = $token;
return $this;
}
/**
* Get the Moodle token
*
* @return string The Moodle token
*/
public function getToken()
{
return $this->token;
}
/**
* Set the return format (json, xml or array)
*
* @param string $return_format The return format (json, xml or array)
*
* @return MoodleRest
*/
public function setReturnFormat($return_format) // json, xml, array
{
if ($return_format <> 'json' && $return_format <> 'xml' && $return_format <> 'array') {
throw new Exception("MoodleRest: Invalid return format: '$return_format'.");
}
$this->return_format = $return_format;
return $this;
}
/**
* Get the return format
*
* @return string The return format (array, json or xml)
*/
public function getReturnFormat()
{
return $this->return_format;
}
/**
* Store the return data
*
* @param string $request_return The returned data made by request() method
*/
private function setRawData($request_return)
{
$this->request_return = $request_return;
}
/**
* Get the returned data previously made by request() method
*
* @return mixed
*/
public function getRawData()
{
return $this->request_return;
}
/**
* Store the parsed return data
*
* @param string $parsed_return The parsed returned data made by request() method
*/
private function setData($parsed_return)
{
$this->parsed_return = $parsed_return;
}
/**
* Get the parsed returned data previously made by request() method
*
* @return mixed The returned data in his final form
*/
public function getData()
{
return $this->parsed_return;
}
/**
* Store the full URL when querying the server
*
* @param string $url The parsed returned data made by request() method
*/
private function setUrl($url)
{
$this->url = $url;
$this->url_decoded = urldecode($url);
}
/**
* Get the full URL stored when the query was made
*
* @return string The requested URL
*/
public function getUrl($decoded = true)
{
return $decoded ? $this->url_decoded : $this->url;
}
/**
* Store the output header
*
* @param string $output_header The header
*/
private function setHeader($output_header)
{
$this->output_header = $output_header;
}
/**
* Get the output header string
*
* @return string Get the output header
*/
public function getHeader()
{
return $this->output_header;
}
/**
* Set the request method
*
* @param string $method The method to be used on request: MoodleRest::METHOD_GET or METHOD_POST
*/
public function setMethod($method)
{
$this->method = $method;
}
/**
* Get the method
*
* @return string Get the request method
*/
public function getMethod()
{
return $this->method;
}
/**
* Enable debugging information
*
* @param bool $enabled Enable or disable debugging information
*/
public function setDebug($enabled = true)
{
$this->debug = $enabled;
}
/**
* Print debug information
*
* @param string $url The full url used to request the Moodle REST server
* @param string $webservice_function The name of the Moodle function
* @param string $method The method used to request data (get or post)
* @param string $returned_data The data returned by Moodle webservice
*/
private function debug($url, $webservice_function, $method, $returned_data)
{
if ($this->debug) {
$line_break = php_sapi_name() == 'cli' ? "\n" : '<br />';
$open_html_pre = php_sapi_name() == 'cli' ? '' : '<pre>';
$close_html_pre = php_sapi_name() == 'cli' ? '' : '</pre>';
echo $open_html_pre;
echo $line_break;
echo '[debug][' . strtoupper($method) . '] ' . get_class($this) . "::request( $webservice_function )$line_break";
echo "$url $line_break";
if (is_array($returned_data) || is_object($returned_data)) {
print_r($returned_data);
} else {
if ((strlen(trim($returned_data)) > 0) && in_array($returned_data[0], array('[', '{'))) {
print_r(json_decode(trim($returned_data), true));
} else {
echo gettype($returned_data) . " '$returned_data'$line_break";
}
}
echo $line_break;
echo $close_html_pre;
}
}
/**
* Set the option to print the requested data to standard output
*
* @param bool $print_on_request Set to TRUE if you want to output the result
*/
public function setPrintOnRequest($print_on_request = true)
{
$this->print_on_request = $print_on_request;
}
/**
* Check if the object is configured to print the result to standard output
*
* @return bool Print the returned data to the standard output?
*/
public function getPrintOnRequest()
{
return $this->print_on_request;
}
/**
* Output the result if the requested data format is json or xml, or print_r if is an array
*/
public function printRequest()
{
if (($this->getReturnFormat() == 'json') || ($this->getReturnFormat() == 'xml')) {
if (empty($this->output_header)) {
if ($this->getReturnFormat() == 'json') {
header('Content-Type: application/json');
} elseif ($this->getReturnFormat() == 'xml') {
header('Content-Type: application/xml');
}
}
echo $this->getData();
} else {
print_r($this->getData());
}
}
/**
* Make the request
*
* @param string $function A Moodle function
* @param array $parameters The parameters to be passed to the Moodle function. eg: array('groupids' => array(1,2)) | This translates as "groupids[0]=1&groupids[1]=2" in URL
*
* @return mixed The final requested data
*/
public function request($function, $parameters = null, $method = self::METHOD_GET)
{
if (empty($this->server_address)) {
throw new Exception('MoodleRest: Empty server address. Use setServerAddress() or put the address on constructor.');
}
if (empty($this->token)) {
throw new Exception('MoodleRest: Empty token. Use setToken() or put the token on constructor.');
}
if (empty($this->return_format)) {
throw new Exception('MoodleRest: Empty return format. Use setReturnFormat().');
}
if (empty($function)) {
throw new Exception('MoodleRest: Empty function. Fill the first parameter of request().');
}
if (!is_null($parameters)) {
if (!is_array($parameters)) {
throw new Exception('MoodleRest: The second parameter of request() should be an array.');
}
}
if ($this->getReturnFormat() == 'array' || $this->getReturnFormat() == 'json') {
$return_format = 'json';
} else {
$return_format = 'xml';
}
$this->setMethod($method);
$query_string = is_array($parameters) ? http_build_query($parameters) : '';
$this->setUrl(
$this->getServerAddress() .
'?wstoken=' . $this->getToken() .
'&moodlewsrestformat=' . $return_format .
'&wsfunction=' . $function .
'&' . $query_string
);
$post_url =
$this->getServerAddress() .
'?wstoken=' . $this->getToken() .
'&moodlewsrestformat=' . $return_format .
'&wsfunction=' . $function;
if ($this->getMethod() != self::METHOD_POST) {
// GET
$moodle_request = file_get_contents($this->getUrl(false));
if ($moodle_request === false) {
throw new Exception('MoodleRest: Error trying to connect to Moodle server on GET request. Check PHP warning messages.');
}
$this->debug($this->getUrl(), $function, self::METHOD_GET, $moodle_request);
} else {
// POST
$options = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $query_string
)
);
$context = stream_context_create($options);
$moodle_request = file_get_contents($post_url, false, $context);
if ($moodle_request === false) {
throw new Exception('MoodleRest: Error trying to connect to Moodle server on POST request. Check PHP warning messages.');
}
$this->debug($this->getUrl(), $function, self::METHOD_POST, $moodle_request);
}
$this->setRawData($moodle_request);
if ($this->getReturnFormat() == 'array') {
$this->setData(json_decode($moodle_request, true));
} else {
$this->setData($moodle_request);
}
if ($this->getPrintOnRequest()) {
$this->printRequest();
}
return $this->getData();
}
}