mirror of
https://github.com/moodle/moodle.git
synced 2025-04-24 09:55:33 +02:00
MDL-67192 core_h5p: get site UUID from H5P using the API
This commit is contained in:
parent
4e90332195
commit
a23969a5fa
h5p
lib/tests
@ -164,7 +164,7 @@ class core extends \H5PCore {
|
||||
*/
|
||||
public function fetch_latest_content_types(): ?\stdClass {
|
||||
|
||||
$contenttypes = self::get_latest_content_types();
|
||||
$contenttypes = $this->get_latest_content_types();
|
||||
if (!empty($contenttypes->error)) {
|
||||
return $contenttypes;
|
||||
}
|
||||
@ -184,7 +184,7 @@ class core extends \H5PCore {
|
||||
'machineName' => $type->id,
|
||||
'majorVersion' => $type->version->major,
|
||||
'minorVersion' => $type->version->minor,
|
||||
'patchVersion' => $type->version->patch
|
||||
'patchVersion' => $type->version->patch,
|
||||
];
|
||||
|
||||
$shoulddownload = true;
|
||||
@ -256,14 +256,21 @@ class core extends \H5PCore {
|
||||
/**
|
||||
* Get H5P endpoints.
|
||||
*
|
||||
* If $library is null, moodle_url is the endpoint of the latest version of the H5P content types. If library is the
|
||||
* machine name of a content type, moodle_url is the endpoint to download the content type.
|
||||
* If $endpoint = 'content' and $library is null, moodle_url is the endpoint of the latest version of the H5P content
|
||||
* types; however, if $library is the machine name of a content type, moodle_url is the endpoint to download the content type.
|
||||
* The SITES endpoint ($endpoint = 'site') may be use to get a site UUID or send site data.
|
||||
*
|
||||
* @param string|null $library The machineName of the library whose endpoint is requested.
|
||||
* @param string $endpoint The endpoint required. Valid values: "site", "content".
|
||||
* @return moodle_url The endpoint moodle_url object.
|
||||
*/
|
||||
public function get_api_endpoint(?string $library): moodle_url {
|
||||
$h5purl = \H5PHubEndpoints::createURL(\H5PHubEndpoints::CONTENT_TYPES ) . $library;
|
||||
public function get_api_endpoint(?string $library = null, string $endpoint = 'content'): moodle_url {
|
||||
if ($endpoint == 'site') {
|
||||
$h5purl = \H5PHubEndpoints::createURL(\H5PHubEndpoints::SITES );
|
||||
} else if ($endpoint == 'content') {
|
||||
$h5purl = \H5PHubEndpoints::createURL(\H5PHubEndpoints::CONTENT_TYPES ) . $library;
|
||||
}
|
||||
|
||||
return new moodle_url($h5purl);
|
||||
}
|
||||
|
||||
@ -275,9 +282,11 @@ class core extends \H5PCore {
|
||||
* - array contentTypes: an object for each H5P content type with its information
|
||||
*/
|
||||
public function get_latest_content_types(): \stdClass {
|
||||
$siteuuid = $this->get_site_uuid() ?? md5($CFG->wwwroot);
|
||||
$postdata = ['uuid' => $siteuuid];
|
||||
|
||||
// Get the latest content-types json.
|
||||
$postdata = ['uuid' => 'foo'];
|
||||
$endpoint = $this->get_api_endpoint(null);
|
||||
$endpoint = $this->get_api_endpoint();
|
||||
$request = download_file_content($endpoint, null, $postdata, true);
|
||||
|
||||
if (!empty($request->error) || $request->status != '200' || empty($request->results)) {
|
||||
@ -293,6 +302,43 @@ class core extends \H5PCore {
|
||||
return $contenttypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the site UUID. If site UUID is not defined, try to register the site.
|
||||
*
|
||||
* return $string The site UUID, null if it is not set.
|
||||
*/
|
||||
public function get_site_uuid(): ?string {
|
||||
// Check if the site_uuid is already set.
|
||||
$siteuuid = get_config('core_h5p', 'site_uuid');
|
||||
|
||||
if (!$siteuuid) {
|
||||
$siteuuid = $this->register_site();
|
||||
}
|
||||
|
||||
return $siteuuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get H5P generated site UUID.
|
||||
*
|
||||
* return ?string Returns H5P generated site UUID, null if can't get it.
|
||||
*/
|
||||
private function register_site(): ?string {
|
||||
$endpoint = $this->get_api_endpoint(null, 'site');
|
||||
$siteuuid = download_file_content($endpoint, null, '');
|
||||
|
||||
// Successful UUID retrieval from H5P.
|
||||
if ($siteuuid) {
|
||||
$json = json_decode($siteuuid);
|
||||
if (isset($json->uuid)) {
|
||||
set_config('site_uuid', $json->uuid, 'core_h5p');
|
||||
return $json->uuid;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the required H5P core API version or higher is installed.
|
||||
*
|
||||
@ -308,5 +354,4 @@ class core extends \H5PCore {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class h5p_core_test extends \advanced_testcase {
|
||||
class h5p_core_testcase extends \advanced_testcase {
|
||||
|
||||
protected function setup() {
|
||||
global $CFG;
|
||||
@ -147,4 +147,27 @@ class h5p_core_test extends \advanced_testcase {
|
||||
$this->assertEquals($numcontenttypes, count($contentfiles));
|
||||
$this->assertCount(0, $result->typesinstalled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that if site_uuid is not set, the site is registered and site_uuid is set.
|
||||
*
|
||||
*/
|
||||
public function test_get_site_uuid(): void {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
if (!PHPUNIT_LONGTEST) {
|
||||
$this->markTestSkipped('PHPUNIT_LONGTEST is not defined');
|
||||
}
|
||||
|
||||
// Check that site_uuid does not have a value.
|
||||
$this->assertFalse(get_config('core_h5p', 'site_uuid'));
|
||||
|
||||
$siteuuid = $this->core->get_site_uuid();
|
||||
|
||||
$this->assertSame($siteuuid, get_config('core_h5p', 'site_uuid'));
|
||||
|
||||
// Check that after a new request the site_uuid remains the same.
|
||||
$siteuuid2 = $this->core->get_site_uuid();
|
||||
$this->assertEquals( $siteuuid, $siteuuid2);
|
||||
}
|
||||
}
|
||||
|
16
lib/tests/fixtures/testable_core_h5p.php
vendored
16
lib/tests/fixtures/testable_core_h5p.php
vendored
@ -83,18 +83,22 @@ class h5p_test_core extends core {
|
||||
/**
|
||||
* Get the URL of the test endpoints instead of the H5P ones.
|
||||
*
|
||||
* If $library is null, moodle_url is the endpoint of the json test file with the H5P content types definition. If library is
|
||||
* the machine name of a content type, moodle_url is the test URL for downloading the content type file.
|
||||
* If $endpoint = 'content' and $library is null, moodle_url is the endpoint of the latest version of the H5P content
|
||||
* types; however, if $library is the machine name of a content type, moodle_url is the endpoint to download the content type.
|
||||
* The SITES endpoint ($endpoint = 'site') may be use to get a site UUID or send site data.
|
||||
*
|
||||
* @param string|null $library The filename of the H5P content type file in external.
|
||||
* @return \moodle_url The moodle_url of the file in external.
|
||||
* @param string|null $library The machineName of the library whose endpoint is requested.
|
||||
* @param string $endpoint The endpoint required. Valid values: "site", "content".
|
||||
* @return \moodle_url The endpoint moodle_url object.
|
||||
*/
|
||||
public function get_api_endpoint(?string $library): \moodle_url {
|
||||
public function get_api_endpoint(?string $library = null, string $endpoint = 'content'): \moodle_url {
|
||||
|
||||
if ($library) {
|
||||
$h5purl = $this->endpoint . '/' . $library . '.h5p';
|
||||
} else if ($endpoint == 'content') {
|
||||
$h5purl = $this->endpoint . '/h5pcontenttypes.json';
|
||||
} else {
|
||||
$h5purl = $h5purl = $this->endpoint . '/h5pcontenttypes.json';
|
||||
$h5purl = $this->endpoint . '/h5puuid.json';
|
||||
}
|
||||
|
||||
return new \moodle_url($h5purl);
|
||||
|
@ -36,7 +36,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class h5p_get_content_types_task_test extends advanced_testcase {
|
||||
class h5p_get_content_types_task_testcase extends advanced_testcase {
|
||||
|
||||
protected function setup() {
|
||||
global $CFG;
|
||||
|
Loading…
x
Reference in New Issue
Block a user