integration_moodle/lib/Service/MoodleAPIService.php
Julien Veyssier a5e3fc7298
almost there
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
2020-09-02 19:57:18 +02:00

149 lines
4.8 KiB
PHP

<?php
/**
* Nextcloud - moodle
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Julien Veyssier
* @copyright Julien Veyssier 2020
*/
namespace OCA\Moodle\Service;
use OCP\IL10N;
use OCP\ILogger;
use OCP\Http\Client\IClientService;
use OCA\Moodle\AppInfo\Application;
class MoodleAPIService {
private $l10n;
private $logger;
/**
* Service to make requests to Moodle v1 API
*/
public function __construct (
string $appName,
ILogger $logger,
IL10N $l10n,
IClientService $clientService
) {
$this->appName = $appName;
$this->l10n = $l10n;
$this->logger = $logger;
$this->clientService = $clientService;
$this->client = $clientService->newClient();
}
public function getNotifications(string $url, string $accessToken, ?int $since): array {
$params = [
'wstoken' => $accessToken,
'wsfunction' => 'block_recentlyaccesseditems_get_recent_items',
'moodlewsrestformat' => 'json',
];
$recentItems = $this->request($url, 'webservice/rest/server.php', $params);
if (isset($recentItems['error'])) {
return $recentItems;
}
$courseIds = [];
foreach ($recentItems as $recentItem) {
if (isset($recentItem['courseid']) && !in_array($recentItem['courseid'], $courseIds)) {
array_push($courseIds, $recentItem['courseid']);
}
}
//$upcomingEvents = [];
//foreach ($courseIds as $courseId) {
// $params['wsfunction'] = 'core_calendar_get_calendar_upcoming_view';
// $params['courseid'] = $courseId;
// $upcomingEvents = array_merge($upcomingEvents, $this->request($url, 'webservice/rest/server.php', $params));
//}
$results = $recentItems;
// filter by date
if (!is_null($since)) {
$results = array_filter($results, function($elem) use ($since) {
$ts = intval($elem['timeaccess']);
return $ts > $since;
});
}
// sort results by date
$a = usort($results, function($a, $b) {
$ta = $a['timeaccess'];
$tb = $b['timeaccess'];
return ($ta > $tb) ? -1 : 1;
});
return $results;
}
public function getMoodleAvatar($url) {
return $this->client->get($url)->getBody();
}
public function request(string $url, string $endPoint, array $params = [], string $method = 'GET'): array {
try {
$url = $url . '/' . $endPoint;
$options = [
'headers' => [
'User-Agent' => 'Nextcloud Moodle integration',
]
];
if (count($params) > 0) {
if ($method === 'GET') {
// manage array parameters
$paramsContent = '';
foreach ($params as $key => $value) {
if (is_array($value)) {
foreach ($value as $oneArrayValue) {
$paramsContent .= $key . '[]=' . urlencode($oneArrayValue) . '&';
}
unset($params[$key]);
}
}
$paramsContent .= http_build_query($params);
$url .= '?' . $paramsContent;
} else {
$options['body'] = $params;
}
}
if ($method === 'GET') {
$response = $this->client->get($url, $options);
} else if ($method === 'POST') {
$response = $this->client->post($url, $options);
} else if ($method === 'PUT') {
$response = $this->client->put($url, $options);
} else if ($method === 'DELETE') {
$response = $this->client->delete($url, $options);
}
$body = $response->getBody();
$respCode = $response->getStatusCode();
if ($respCode >= 400) {
return ['error' => $this->l10n->t('Bad credentials')];
} else {
return json_decode($body, true);
}
} catch (\Exception $e) {
$this->logger->warning('Moodle API error : '.$e, array('app' => $this->appName));
return ['error' => $e->getMessage()];
}
}
public function getToken(string $moodleUrl, string $login, string $password): array {
$params = [
'username' => $login,
'password' => $password,
'service' => 'moodle_mobile_app',
];
return $this->request($moodleUrl, 'login/token.php', $params, 'POST');
}
}