phpstormify

Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
This commit is contained in:
Julien Veyssier 2021-06-24 13:48:09 +02:00
parent 9fd4a6ab34
commit 60c91bdc67
No known key found for this signature in database
GPG key ID: 4141FEE162030638
12 changed files with 219 additions and 224 deletions

View file

@ -9,16 +9,13 @@
namespace OCA\Moodle\AppInfo; namespace OCA\Moodle\AppInfo;
use OCP\IContainer;
use OCP\IConfig; use OCP\IConfig;
use OCP\AppFramework\App; use OCP\AppFramework\App;
use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IBootstrap;
use OCA\Moodle\Controller\PageController;
use OCA\Moodle\Dashboard\MoodleWidget; use OCA\Moodle\Dashboard\MoodleWidget;
use OCA\Moodle\Search\MoodleSearchCoursesProvider; use OCA\Moodle\Search\MoodleSearchCoursesProvider;
use OCA\Moodle\Search\MoodleSearchModulesProvider; use OCA\Moodle\Search\MoodleSearchModulesProvider;
@ -32,6 +29,10 @@ use OCA\Moodle\Search\MoodleSearchUpcomingProvider;
class Application extends App implements IBootstrap { class Application extends App implements IBootstrap {
public const APP_ID = 'integration_moodle'; public const APP_ID = 'integration_moodle';
/**
* @var mixed
*/
private $config;
/** /**
* Constructor * Constructor
@ -42,7 +43,7 @@ class Application extends App implements IBootstrap {
parent::__construct(self::APP_ID, $urlParams); parent::__construct(self::APP_ID, $urlParams);
$container = $this->getContainer(); $container = $this->getContainer();
$this->config = $container->query(IConfig::class); $this->config = $container->get(IConfig::class);
} }
public function register(IRegistrationContext $context): void { public function register(IRegistrationContext $context): void {

View file

@ -11,58 +11,31 @@
namespace OCA\Moodle\Controller; namespace OCA\Moodle\Controller;
use OCP\App\IAppManager;
use OCP\Files\IAppData;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\IURLGenerator;
use OCP\IConfig; use OCP\IConfig;
use OCP\IServerContainer;
use OCP\IL10N;
use Psr\Log\LoggerInterface;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use OCP\IRequest; use OCP\IRequest;
use OCP\IDBConnection;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCA\Moodle\Service\MoodleAPIService;
use OCA\Moodle\AppInfo\Application; use OCA\Moodle\AppInfo\Application;
class ConfigController extends Controller { class ConfigController extends Controller {
private $userId; /**
* @var IConfig
*/
private $config; private $config;
private $dbconnection; /**
private $dbtype; * @var string|null
*/
private $userId;
public function __construct($AppName, public function __construct(string $appName,
IRequest $request, IRequest $request,
IServerContainer $serverContainer,
IConfig $config, IConfig $config,
IAppManager $appManager, ?string $userId) {
IAppData $appData, parent::__construct($appName, $request);
IDBConnection $dbconnection,
IURLGenerator $urlGenerator,
IL10N $l,
LoggerInterface $logger,
MoodleAPIService $moodleAPIService,
$userId) {
parent::__construct($AppName, $request);
$this->l = $l;
$this->userId = $userId;
$this->appData = $appData;
$this->serverContainer = $serverContainer;
$this->config = $config; $this->config = $config;
$this->dbconnection = $dbconnection; $this->userId = $userId;
$this->urlGenerator = $urlGenerator;
$this->logger = $logger;
$this->moodleAPIService = $moodleAPIService;
} }
/** /**
@ -81,8 +54,7 @@ class ConfigController extends Controller {
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'user_name'); $this->config->deleteUserValue($this->userId, Application::APP_ID, 'user_name');
$this->config->deleteUserValue($this->userId, Application::APP_ID, 'privatetoken'); $this->config->deleteUserValue($this->userId, Application::APP_ID, 'privatetoken');
} }
$response = new DataResponse(1); return new DataResponse(1);
return $response;
} }
/** /**
@ -95,7 +67,6 @@ class ConfigController extends Controller {
foreach ($values as $key => $value) { foreach ($values as $key => $value) {
$this->config->setAppValue(Application::APP_ID, $key, $value); $this->config->setAppValue(Application::APP_ID, $key, $value);
} }
$response = new DataResponse(1); return new DataResponse(1);
return $response;
} }
} }

View file

@ -11,21 +11,8 @@
namespace OCA\Moodle\Controller; namespace OCA\Moodle\Controller;
use OCP\App\IAppManager;
use OCP\Files\IAppData;
use OCP\AppFramework\Http\DataDisplayResponse; use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\IURLGenerator;
use OCP\IConfig; use OCP\IConfig;
use OCP\IServerContainer;
use OCP\IL10N;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Http\ContentSecurityPolicy;
use Psr\Log\LoggerInterface;
use OCP\IRequest; use OCP\IRequest;
use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
@ -35,32 +22,42 @@ use OCA\Moodle\AppInfo\Application;
class MoodleAPIController extends Controller { class MoodleAPIController extends Controller {
/**
private $userId; * @var IConfig
*/
private $config; private $config;
private $dbconnection; /**
private $dbtype; * @var MoodleAPIService
*/
private $moodleAPIService;
/**
* @var string|null
*/
private $userId;
/**
* @var string
*/
private $accessToken;
/**
* @var string
*/
private $moodleUrl;
/**
* @var bool
*/
private $checkSsl;
public function __construct($AppName, public function __construct(string $appName,
IRequest $request, IRequest $request,
IServerContainer $serverContainer,
IConfig $config, IConfig $config,
IL10N $l10n,
IAppManager $appManager,
IAppData $appData,
LoggerInterface $logger,
MoodleAPIService $moodleAPIService, MoodleAPIService $moodleAPIService,
$userId) { ?string $userId) {
parent::__construct($AppName, $request); parent::__construct($appName, $request);
$this->userId = $userId;
$this->l10n = $l10n;
$this->appData = $appData;
$this->serverContainer = $serverContainer;
$this->config = $config; $this->config = $config;
$this->logger = $logger;
$this->moodleAPIService = $moodleAPIService; $this->moodleAPIService = $moodleAPIService;
$this->accessToken = $this->config->getUserValue($this->userId, Application::APP_ID, 'token', ''); $this->userId = $userId;
$this->moodleUrl = $this->config->getUserValue($this->userId, Application::APP_ID, 'url', ''); $this->accessToken = $this->config->getUserValue($this->userId, Application::APP_ID, 'token');
$this->moodleUrl = $this->config->getUserValue($this->userId, Application::APP_ID, 'url');
$this->checkSsl = $this->config->getUserValue($this->userId, Application::APP_ID, 'check_ssl', '1') === '1'; $this->checkSsl = $this->config->getUserValue($this->userId, Application::APP_ID, 'check_ssl', '1') === '1';
} }

View file

@ -27,16 +27,22 @@ use OCP\Dashboard\IWidget;
use OCP\IL10N; use OCP\IL10N;
use OCA\Moodle\AppInfo\Application; use OCA\Moodle\AppInfo\Application;
use OCP\IURLGenerator;
use OCP\Util;
class MoodleWidget implements IWidget { class MoodleWidget implements IWidget {
/** @var IL10N */ /** @var IL10N */
private $l10n; private $l10n;
/**
* @var IURLGenerator
*/
private $url;
public function __construct( public function __construct(IL10N $l10n,
IL10N $l10n IURLGenerator $url) {
) {
$this->l10n = $l10n; $this->l10n = $l10n;
$this->url = $url;
} }
/** /**
@ -71,14 +77,14 @@ class MoodleWidget implements IWidget {
* @inheritDoc * @inheritDoc
*/ */
public function getUrl(): ?string { public function getUrl(): ?string {
return \OC::$server->getURLGenerator()->linkToRoute('settings.PersonalSettings.index', ['section' => 'connected-accounts']); return $this->url->linkToRoute('settings.PersonalSettings.index', ['section' => 'connected-accounts']);
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function load(): void { public function load(): void {
\OC_Util::addScript(Application::APP_ID, Application::APP_ID . '-dashboard'); Util::addScript(Application::APP_ID, Application::APP_ID . '-dashboard');
\OC_Util::addStyle(Application::APP_ID, 'dashboard'); Util::addStyle(Application::APP_ID, 'dashboard');
} }
} }

View file

@ -45,6 +45,14 @@ class MoodleSearchCoursesProvider implements IProvider {
/** @var IURLGenerator */ /** @var IURLGenerator */
private $urlGenerator; private $urlGenerator;
/**
* @var IConfig
*/
private $config;
/**
* @var MoodleAPIService
*/
private $service;
/** /**
* CospendSearchProvider constructor. * CospendSearchProvider constructor.
@ -105,13 +113,13 @@ class MoodleSearchCoursesProvider implements IProvider {
$offset = $query->getCursor(); $offset = $query->getCursor();
$offset = $offset ? intval($offset) : 0; $offset = $offset ? intval($offset) : 0;
$theme = $this->config->getUserValue($user->getUID(), 'accessibility', 'theme', ''); $theme = $this->config->getUserValue($user->getUID(), 'accessibility', 'theme');
$thumbnailUrl = ($theme === 'dark') ? $thumbnailUrl = ($theme === 'dark') ?
$this->urlGenerator->imagePath(Application::APP_ID, 'app.svg') : $this->urlGenerator->imagePath(Application::APP_ID, 'app.svg') :
$this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg'); $this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg');
$moodleUrl = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'url', ''); $moodleUrl = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'url');
$accessToken = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'token', ''); $accessToken = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'token');
$checkSsl = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'check_ssl', '1') === '1'; $checkSsl = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'check_ssl', '1') === '1';
$searchCoursesEnabled = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'search_courses_enabled', '0') === '1'; $searchCoursesEnabled = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'search_courses_enabled', '0') === '1';
if ($accessToken === '' || !$searchCoursesEnabled) { if ($accessToken === '' || !$searchCoursesEnabled) {
@ -123,7 +131,7 @@ class MoodleSearchCoursesProvider implements IProvider {
return SearchResult::paginated($this->getName(), [], 0); return SearchResult::paginated($this->getName(), [], 0);
} }
$formattedResults = \array_map(function (array $entry) use ($thumbnailUrl, $moodleUrl): MoodleSearchResultEntry { $formattedResults = array_map(function (array $entry) use ($thumbnailUrl, $moodleUrl): MoodleSearchResultEntry {
return new MoodleSearchResultEntry( return new MoodleSearchResultEntry(
$thumbnailUrl, $thumbnailUrl,
$this->getMainText($entry), $this->getMainText($entry),
@ -166,4 +174,4 @@ class MoodleSearchCoursesProvider implements IProvider {
return $url . '/course/view.php?id=' . $entry['id']; return $url . '/course/view.php?id=' . $entry['id'];
} }
} }

View file

@ -45,12 +45,21 @@ class MoodleSearchModulesProvider implements IProvider {
/** @var IURLGenerator */ /** @var IURLGenerator */
private $urlGenerator; private $urlGenerator;
/**
* @var IConfig
*/
private $config;
/**
* @var MoodleAPIService
*/
private $service;
/** /**
* CospendSearchProvider constructor. * CospendSearchProvider constructor.
* *
* @param IAppManager $appManager * @param IAppManager $appManager
* @param IL10N $l10n * @param IL10N $l10n
* @param IConfig $config
* @param IURLGenerator $urlGenerator * @param IURLGenerator $urlGenerator
* @param MoodleAPIService $service * @param MoodleAPIService $service
*/ */
@ -105,13 +114,13 @@ class MoodleSearchModulesProvider implements IProvider {
$offset = $query->getCursor(); $offset = $query->getCursor();
$offset = $offset ? intval($offset) : 0; $offset = $offset ? intval($offset) : 0;
$theme = $this->config->getUserValue($user->getUID(), 'accessibility', 'theme', ''); $theme = $this->config->getUserValue($user->getUID(), 'accessibility', 'theme');
$thumbnailUrl = ($theme === 'dark') ? $thumbnailUrl = ($theme === 'dark') ?
$this->urlGenerator->imagePath(Application::APP_ID, 'app.svg') : $this->urlGenerator->imagePath(Application::APP_ID, 'app.svg') :
$this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg'); $this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg');
$moodleUrl = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'url', ''); $moodleUrl = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'url');
$accessToken = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'token', ''); $accessToken = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'token');
$checkSsl = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'check_ssl', '1') === '1'; $checkSsl = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'check_ssl', '1') === '1';
$searchModulesEnabled = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'search_modules_enabled', '0') === '1'; $searchModulesEnabled = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'search_modules_enabled', '0') === '1';
if ($accessToken === '' || !$searchModulesEnabled) { if ($accessToken === '' || !$searchModulesEnabled) {
@ -123,7 +132,7 @@ class MoodleSearchModulesProvider implements IProvider {
return SearchResult::paginated($this->getName(), [], 0); return SearchResult::paginated($this->getName(), [], 0);
} }
$formattedResults = \array_map(function (array $entry) use ($thumbnailUrl, $moodleUrl): MoodleSearchResultEntry { $formattedResults = array_map(function (array $entry) use ($thumbnailUrl, $moodleUrl): MoodleSearchResultEntry {
return new MoodleSearchResultEntry( return new MoodleSearchResultEntry(
$this->getThumbnailUrl($entry, $thumbnailUrl), $this->getThumbnailUrl($entry, $thumbnailUrl),
$this->getMainText($entry), $this->getMainText($entry),
@ -183,8 +192,8 @@ class MoodleSearchModulesProvider implements IProvider {
*/ */
protected function getThumbnailurl(array $entry, string $thumbnailUrl): string { protected function getThumbnailurl(array $entry, string $thumbnailUrl): string {
return $thumbnailUrl; return $thumbnailUrl;
return $entry['modicon'] // return $entry['modicon']
? $this->urlGenerator->linkToRoute('integration_moodle.moodleAPI.getMoodleAvatar', []) . '?url=' . urlencode($entry['modicon']) // ? $this->urlGenerator->linkToRoute('integration_moodle.moodleAPI.getMoodleAvatar', []) . '?url=' . urlencode($entry['modicon'])
: $thumbnailUrl; // : $thumbnailUrl;
} }
} }

View file

@ -45,12 +45,21 @@ class MoodleSearchUpcomingProvider implements IProvider {
/** @var IURLGenerator */ /** @var IURLGenerator */
private $urlGenerator; private $urlGenerator;
/**
* @var IConfig
*/
private $config;
/**
* @var MoodleAPIService
*/
private $service;
/** /**
* CospendSearchProvider constructor. * CospendSearchProvider constructor.
* *
* @param IAppManager $appManager * @param IAppManager $appManager
* @param IL10N $l10n * @param IL10N $l10n
* @param IConfig $config
* @param IURLGenerator $urlGenerator * @param IURLGenerator $urlGenerator
* @param MoodleAPIService $service * @param MoodleAPIService $service
*/ */
@ -105,13 +114,13 @@ class MoodleSearchUpcomingProvider implements IProvider {
$offset = $query->getCursor(); $offset = $query->getCursor();
$offset = $offset ? intval($offset) : 0; $offset = $offset ? intval($offset) : 0;
$theme = $this->config->getUserValue($user->getUID(), 'accessibility', 'theme', ''); $theme = $this->config->getUserValue($user->getUID(), 'accessibility', 'theme');
$thumbnailUrl = ($theme === 'dark') ? $thumbnailUrl = ($theme === 'dark') ?
$this->urlGenerator->imagePath(Application::APP_ID, 'app.svg') : $this->urlGenerator->imagePath(Application::APP_ID, 'app.svg') :
$this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg'); $this->urlGenerator->imagePath(Application::APP_ID, 'app-dark.svg');
$moodleUrl = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'url', ''); $moodleUrl = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'url');
$accessToken = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'token', ''); $accessToken = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'token');
$checkSsl = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'check_ssl', '1') === '1'; $checkSsl = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'check_ssl', '1') === '1';
$searchUpcomingEnabled = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'search_upcoming_enabled', '0') === '1'; $searchUpcomingEnabled = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'search_upcoming_enabled', '0') === '1';
if ($accessToken === '' || !$searchUpcomingEnabled) { if ($accessToken === '' || !$searchUpcomingEnabled) {
@ -123,7 +132,7 @@ class MoodleSearchUpcomingProvider implements IProvider {
return SearchResult::paginated($this->getName(), [], 0); return SearchResult::paginated($this->getName(), [], 0);
} }
$formattedResults = \array_map(function (array $entry) use ($thumbnailUrl, $moodleUrl): MoodleSearchResultEntry { $formattedResults = array_map(function (array $entry) use ($thumbnailUrl, $moodleUrl): MoodleSearchResultEntry {
return new MoodleSearchResultEntry( return new MoodleSearchResultEntry(
$this->getThumbnailUrl($entry, $thumbnailUrl), $this->getThumbnailUrl($entry, $thumbnailUrl),
$this->getMainText($entry), $this->getMainText($entry),
@ -175,4 +184,4 @@ class MoodleSearchUpcomingProvider implements IProvider {
? $entry['course']['courseimage'] ? $entry['course']['courseimage']
: $thumbnailUrl; : $thumbnailUrl;
} }
} }

View file

@ -11,36 +11,46 @@
namespace OCA\Moodle\Service; namespace OCA\Moodle\Service;
use Exception;
use OCP\IL10N; use OCP\IL10N;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
use OCA\Moodle\AppInfo\Application;
class MoodleAPIService { class MoodleAPIService {
/**
private $l10n; * @var string
*/
private $appName;
/**
* @var LoggerInterface
*/
private $logger; private $logger;
/**
* @var IL10N
*/
private $l10n;
/**
* @var \OCP\Http\Client\IClient
*/
private $client;
/** /**
* Service to make requests to Moodle v1 API * Service to make requests to Moodle v1 API
*/ */
public function __construct ( public function __construct (string $appName,
string $appName, LoggerInterface $logger,
LoggerInterface $logger, IL10N $l10n,
IL10N $l10n, IClientService $clientService) {
IClientService $clientService
) {
$this->appName = $appName; $this->appName = $appName;
$this->l10n = $l10n;
$this->logger = $logger; $this->logger = $logger;
$this->clientService = $clientService; $this->l10n = $l10n;
$this->client = $clientService->newClient(); $this->client = $clientService->newClient();
} }
/** /**
* @param string $url * @param string $url
* @param string $accessToken * @param string $accessToken
* @param bool $checkSsl
* @param ?int $recentSince * @param ?int $recentSince
* @return array * @return array
*/ */
@ -57,7 +67,7 @@ class MoodleAPIService {
} }
// sort recent items by date DESC // sort recent items by date DESC
$a = usort($recentItems, function($a, $b) { usort($recentItems, function($a, $b) {
$ta = $a['timeaccess'] ?? 0; $ta = $a['timeaccess'] ?? 0;
$tb = $b['timeaccess'] ?? 0; $tb = $b['timeaccess'] ?? 0;
return ($ta > $tb) ? -1 : 1; return ($ta > $tb) ? -1 : 1;
@ -87,7 +97,7 @@ class MoodleAPIService {
} }
} }
// sort upcoming events by date ASC // sort upcoming events by date ASC
$a = usort($upcomingEvents, function($a, $b) { usort($upcomingEvents, function($a, $b) {
$ta = $a['timestart'] ?? 0; $ta = $a['timestart'] ?? 0;
$tb = $b['timestart'] ?? 0; $tb = $b['timestart'] ?? 0;
return ($ta < $tb) ? -1 : 1; return ($ta < $tb) ? -1 : 1;
@ -115,6 +125,7 @@ class MoodleAPIService {
/** /**
* @param string $url * @param string $url
* @param string $accessToken * @param string $accessToken
* @param bool $checkSsl
* @param string $query * @param string $query
* @param int $offset * @param int $offset
* @param int $limit * @param int $limit
@ -160,8 +171,8 @@ class MoodleAPIService {
if ($sections['exception'] || $sections['error']) { if ($sections['exception'] || $sections['error']) {
return $sections; return $sections;
} }
foreach ($sections as $k => $section) { foreach ($sections as $ks => $section) {
foreach ($section['modules'] as $k => $module) { foreach ($section['modules'] as $km => $module) {
$moduleName = strtolower($module['name']); $moduleName = strtolower($module['name']);
if (strpos($moduleName, $query) !== false) { if (strpos($moduleName, $query) !== false) {
$module['section_name'] = $section['name']; $module['section_name'] = $section['name'];
@ -181,6 +192,7 @@ class MoodleAPIService {
/** /**
* @param string $url * @param string $url
* @param string $accessToken * @param string $accessToken
* @param bool $checkSsl
* @param string $query * @param string $query
* @param int $offset * @param int $offset
* @param int $limit * @param int $limit
@ -228,7 +240,9 @@ class MoodleAPIService {
/** /**
* @param string $url * @param string $url
* @param bool $checkSsl
* @return string * @return string
* @throws Exception
*/ */
public function getMoodleAvatar(string $url, bool $checkSsl): string { public function getMoodleAvatar(string $url, bool $checkSsl): string {
if ($checkSsl) { if ($checkSsl) {
@ -255,6 +269,7 @@ class MoodleAPIService {
/** /**
* @param string $url * @param string $url
* @param string $endPoint * @param string $endPoint
* @param bool $checkSsl
* @param array $params * @param array $params
* @param string $method * @param string $method
* @return array * @return array
@ -299,6 +314,8 @@ class MoodleAPIService {
$response = $this->client->put($url, $options); $response = $this->client->put($url, $options);
} else if ($method === 'DELETE') { } else if ($method === 'DELETE') {
$response = $this->client->delete($url, $options); $response = $this->client->delete($url, $options);
} else {
return ['error' => $this->l10n->t('Bad HTTP method')];
} }
$body = $response->getBody(); $body = $response->getBody();
$respCode = $response->getStatusCode(); $respCode = $response->getStatusCode();
@ -308,7 +325,7 @@ class MoodleAPIService {
} else { } else {
return json_decode($body, true); return json_decode($body, true);
} }
} catch (\Exception $e) { } catch (Exception $e) {
$this->logger->warning('Moodle API error : '.$e, array('app' => $this->appName)); $this->logger->warning('Moodle API error : '.$e, array('app' => $this->appName));
return ['error' => $e->getMessage()]; return ['error' => $e->getMessage()];
} }
@ -318,6 +335,7 @@ class MoodleAPIService {
* @param string $moodleUrl * @param string $moodleUrl
* @param string $login * @param string $login
* @param string $password * @param string $password
* @param bool $checkSsl
* @return array * @return array
*/ */
public function getToken(string $moodleUrl, string $login, string $password, bool $checkSsl): array { public function getToken(string $moodleUrl, string $login, string $password, bool $checkSsl): array {

View file

@ -2,38 +2,27 @@
namespace OCA\Moodle\Settings; namespace OCA\Moodle\Settings;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\IRequest; use OCP\AppFramework\Services\IInitialState;
use OCP\IL10N;
use OCP\IConfig; use OCP\IConfig;
use OCP\Settings\ISettings; use OCP\Settings\ISettings;
use OCP\Util;
use OCP\IURLGenerator;
use OCP\IInitialStateService;
use OCA\Moodle\AppInfo\Application; use OCA\Moodle\AppInfo\Application;
class Admin implements ISettings { class Admin implements ISettings {
private $request; /**
* @var IConfig
*/
private $config; private $config;
private $dataDirPath; /**
private $urlGenerator; * @var IInitialState
private $l; */
private $initialStateService;
public function __construct(string $appName, public function __construct(IConfig $config,
IL10N $l, IInitialState $initialStateService) {
IRequest $request,
IConfig $config,
IURLGenerator $urlGenerator,
IInitialStateService $initialStateService,
$userId) {
$this->appName = $appName;
$this->urlGenerator = $urlGenerator;
$this->request = $request;
$this->l = $l;
$this->config = $config; $this->config = $config;
$this->initialStateService = $initialStateService; $this->initialStateService = $initialStateService;
$this->userId = $userId;
} }
/** /**
@ -45,7 +34,7 @@ class Admin implements ISettings {
$adminConfig = [ $adminConfig = [
'search_disabled' => $searchDisabled, 'search_disabled' => $searchDisabled,
]; ];
$this->initialStateService->provideInitialState($this->appName, 'admin-config', $adminConfig); $this->initialStateService->provideInitialState('admin-config', $adminConfig);
return new TemplateResponse(Application::APP_ID, 'adminSettings'); return new TemplateResponse(Application::APP_ID, 'adminSettings');
} }

View file

@ -13,13 +13,10 @@ class AdminSection implements IIconSection {
/** @var IURLGenerator */ /** @var IURLGenerator */
private $urlGenerator; private $urlGenerator;
public function __construct(string $appName, public function __construct(IURLGenerator $urlGenerator,
IURLGenerator $urlGenerator, IL10N $l) {
IL10N $l
) {
$this->appName = $appName;
$this->l = $l;
$this->urlGenerator = $urlGenerator; $this->urlGenerator = $urlGenerator;
$this->l = $l;
} }
/** /**
@ -57,4 +54,4 @@ class AdminSection implements IIconSection {
return $this->urlGenerator->imagePath('core', 'categories/integration.svg'); return $this->urlGenerator->imagePath('core', 'categories/integration.svg');
} }
} }

View file

@ -2,51 +2,45 @@
namespace OCA\Moodle\Settings; namespace OCA\Moodle\Settings;
use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\TemplateResponse;
use OCP\IRequest; use OCP\AppFramework\Services\IInitialState;
use OCP\IL10N;
use OCP\IConfig; use OCP\IConfig;
use OCP\Settings\ISettings; use OCP\Settings\ISettings;
use OCP\Util;
use OCP\IURLGenerator;
use OCP\IInitialStateService;
use OCA\Moodle\AppInfo\Application; use OCA\Moodle\AppInfo\Application;
class Personal implements ISettings { class Personal implements ISettings {
private $request; /**
private $config; * @var IConfig
private $dataDirPath; */
private $urlGenerator; private $config;
private $l; /**
* @var IInitialState
*/
private $initialStateService;
/**
* @var string|null
*/
private $userId;
public function __construct( public function __construct(IConfig $config,
string $appName, IInitialState $initialStateService,
IL10N $l, ?string $userId) {
IRequest $request, $this->config = $config;
IConfig $config, $this->initialStateService = $initialStateService;
IURLGenerator $urlGenerator, $this->userId = $userId;
IInitialStateService $initialStateService, }
$userId) {
$this->appName = $appName;
$this->urlGenerator = $urlGenerator;
$this->request = $request;
$this->l = $l;
$this->config = $config;
$this->initialStateService = $initialStateService;
$this->userId = $userId;
}
/** /**
* @return TemplateResponse * @return TemplateResponse
*/ */
public function getForm(): TemplateResponse { public function getForm(): TemplateResponse {
$token = $this->config->getUserValue($this->userId, Application::APP_ID, 'token', ''); $token = $this->config->getUserValue($this->userId, Application::APP_ID, 'token');
$url = $this->config->getUserValue($this->userId, Application::APP_ID, 'url', ''); $url = $this->config->getUserValue($this->userId, Application::APP_ID, 'url');
$searchCoursesEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'search_courses_enabled', '0'); $searchCoursesEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'search_courses_enabled', '0');
$searchModulesEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'search_modules_enabled', '0'); $searchModulesEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'search_modules_enabled', '0');
$searchUpcomingEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'search_upcoming_enabled', '0'); $searchUpcomingEnabled = $this->config->getUserValue($this->userId, Application::APP_ID, 'search_upcoming_enabled', '0');
$userName = $this->config->getUserValue($this->userId, Application::APP_ID, 'user_name', ''); $userName = $this->config->getUserValue($this->userId, Application::APP_ID, 'user_name');
$checkSsl = $this->config->getUserValue($this->userId, Application::APP_ID, 'check_ssl', '1') === '1'; $checkSsl = $this->config->getUserValue($this->userId, Application::APP_ID, 'check_ssl', '1') === '1';
$searchDisabled = $this->config->getAppValue(Application::APP_ID, 'search_disabled', '0') === '1'; $searchDisabled = $this->config->getAppValue(Application::APP_ID, 'search_disabled', '0') === '1';
@ -61,7 +55,7 @@ class Personal implements ISettings {
'check_ssl' => $checkSsl, 'check_ssl' => $checkSsl,
'search_disabled' => $searchDisabled, 'search_disabled' => $searchDisabled,
]; ];
$this->initialStateService->provideInitialState($this->appName, 'user-config', $userConfig); $this->initialStateService->provideInitialState('user-config', $userConfig);
return new TemplateResponse(Application::APP_ID, 'personalSettings'); return new TemplateResponse(Application::APP_ID, 'personalSettings');
} }

View file

@ -7,54 +7,50 @@ use OCP\Settings\IIconSection;
class PersonalSection implements IIconSection { class PersonalSection implements IIconSection {
/** @var IL10N */ /** @var IL10N */
private $l; private $l;
/** @var IURLGenerator */ /** @var IURLGenerator */
private $urlGenerator; private $urlGenerator;
public function __construct(string $appName, public function __construct(IURLGenerator $urlGenerator,
IURLGenerator $urlGenerator, IL10N $l) {
IL10N $l $this->l = $l;
) { $this->urlGenerator = $urlGenerator;
$this->appName = $appName; }
$this->l = $l;
$this->urlGenerator = $urlGenerator;
}
/** /**
* returns the ID of the section. It is supposed to be a lower case string * returns the ID of the section. It is supposed to be a lower case string
* *
* @returns string * @returns string
*/ */
public function getID(): string { public function getID(): string {
return 'connected-accounts'; //or a generic id if feasible return 'connected-accounts'; //or a generic id if feasible
} }
/** /**
* returns the translated name as it should be displayed, e.g. 'LDAP / AD * returns the translated name as it should be displayed, e.g. 'LDAP / AD
* integration'. Use the L10N service to translate it. * integration'. Use the L10N service to translate it.
* *
* @return string * @return string
*/ */
public function getName(): string { public function getName(): string {
return $this->l->t('Connected accounts'); return $this->l->t('Connected accounts');
} }
/** /**
* @return int whether the form should be rather on the top or bottom of * @return int whether the form should be rather on the top or bottom of
* the settings navigation. The sections are arranged in ascending order of * the settings navigation. The sections are arranged in ascending order of
* the priority values. It is required to return a value between 0 and 99. * the priority values. It is required to return a value between 0 and 99.
*/ */
public function getPriority(): int { public function getPriority(): int {
return 80; return 80;
} }
/** /**
* @return ?string The relative path to a an icon describing the section * @return ?string The relative path to a an icon describing the section
*/ */
public function getIcon(): ?string { public function getIcon(): ?string {
return $this->urlGenerator->imagePath('core', 'categories/integration.svg'); return $this->urlGenerator->imagePath('core', 'categories/integration.svg');
} }
}
}