Register site with new endpoint if no site is already registered

HFP-489
pull/38/head
thomasmars 2017-03-16 09:51:46 +01:00
parent b3e0539261
commit 93e9d6e0ec
1 changed files with 31 additions and 1 deletions

View File

@ -2426,6 +2426,8 @@ class H5PCore {
* implementation is responsible for invoking this, eg using cron
*
* @param bool $fetchingDisabled
*
* @return bool|object Returns endpoint data if found, otherwise FALSE
*/
public function fetchLibrariesMetadata($fetchingDisabled = FALSE) {
// Gather data
@ -2453,6 +2455,26 @@ class H5PCore {
)))
);
// Register site if it is not registered
if (empty($uuid)) {
$protocol = (extension_loaded('openssl') ? 'https' : 'http');
$endpoint = 'api.h5p.org/v1/sites';
$registration = $this->h5pF->fetchExternalData("{$protocol}://{$endpoint}", $data);
// Failed retrieving uuid
if (!$registration) {
return H5PCore::ajaxError(
t('Site could not be registered with the hub. Please contact your site administrator.'),
'SITE_REGISTRATION_FAILED'
);
}
// Successfully retrieved new uuid
$json = json_decode($registration);
$data['uuid'] = $json->uuid;
$this->h5pF->setOption('site_uuid', $json->uuid);
}
$result = $this->updateContentTypeCache($data);
// No data received
@ -2783,8 +2805,16 @@ class H5PCore {
// Set uuid
if (!$postData) {
$site_uuid = $this->h5pF->getOption('site_uuid', '');
// Register site with site uuid if we don't already have it
// and try to update content type cache again when this is done
if (empty($site_uuid)) {
return $this->fetchLibrariesMetadata();
}
$postData = array(
'uuid' => $this->h5pF->getOption('site_uuid', '')
'uuid' => $site_uuid
);
}