mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-67057 core_h5p: New method to get latest library version
This commit is contained in:
parent
285976f1bd
commit
7a0d9bd5e4
2 changed files with 111 additions and 2 deletions
|
@ -757,7 +757,19 @@ class framework implements \H5PFrameworkInterface {
|
||||||
$content['contenthash'] = '';
|
$content['contenthash'] = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = array(
|
// If the libraryid declared in the package is empty, get the latest version.
|
||||||
|
if (empty($content['library']['libraryId'])) {
|
||||||
|
$mainlibrary = $this->get_latest_library_version($content['library']['machineName']);
|
||||||
|
if (empty($mainlibrary)) {
|
||||||
|
// Raise an error if the main library is not defined and the latest version doesn't exist.
|
||||||
|
$message = $this->t('Missing required library @library', ['@library' => $content['library']['machineName']]);
|
||||||
|
$this->setErrorMessage($message, 'missing-required-library');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$content['library']['libraryId'] = $mainlibrary->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = [
|
||||||
'jsoncontent' => $content['params'],
|
'jsoncontent' => $content['params'],
|
||||||
'displayoptions' => $content['disable'],
|
'displayoptions' => $content['disable'],
|
||||||
'mainlibraryid' => $content['library']['libraryId'],
|
'mainlibraryid' => $content['library']['libraryId'],
|
||||||
|
@ -765,7 +777,7 @@ class framework implements \H5PFrameworkInterface {
|
||||||
'filtered' => null,
|
'filtered' => null,
|
||||||
'pathnamehash' => $content['pathnamehash'],
|
'pathnamehash' => $content['pathnamehash'],
|
||||||
'contenthash' => $content['contenthash']
|
'contenthash' => $content['contenthash']
|
||||||
);
|
];
|
||||||
|
|
||||||
if (!isset($content['id'])) {
|
if (!isset($content['id'])) {
|
||||||
$data['timecreated'] = $data['timemodified'];
|
$data['timecreated'] = $data['timemodified'];
|
||||||
|
@ -1585,4 +1597,22 @@ class framework implements \H5PFrameworkInterface {
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the latest library version.
|
||||||
|
*
|
||||||
|
* @param string $machinename The library's machine name
|
||||||
|
* @return stdClass|null An object with the latest library version
|
||||||
|
*/
|
||||||
|
public function get_latest_library_version(string $machinename): ?\stdClass {
|
||||||
|
global $DB;
|
||||||
|
|
||||||
|
$libraries = $DB->get_records('h5p_libraries', ['machinename' => $machinename],
|
||||||
|
'majorversion DESC, minorversion DESC, patchversion DESC', '*', 0, 1);
|
||||||
|
if ($libraries) {
|
||||||
|
return reset($libraries);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -845,6 +845,41 @@ class framework_testcase extends \advanced_testcase {
|
||||||
$this->assertEquals($content['disable'], $dbcontent->displayoptions);
|
$this->assertEquals($content['disable'], $dbcontent->displayoptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the behaviour of insertContent().
|
||||||
|
*/
|
||||||
|
public function test_insertContent_latestlibrary() {
|
||||||
|
global $DB;
|
||||||
|
|
||||||
|
$this->resetAfterTest();
|
||||||
|
|
||||||
|
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
|
||||||
|
// Create a library record.
|
||||||
|
$lib = $generator->create_library_record('TestLibrary', 'Test', 1, 1, 2);
|
||||||
|
|
||||||
|
$content = array(
|
||||||
|
'params' => json_encode(['param1' => 'Test']),
|
||||||
|
'library' => array(
|
||||||
|
'libraryId' => 0,
|
||||||
|
'machineName' => 'TestLibrary',
|
||||||
|
),
|
||||||
|
'disable' => 8
|
||||||
|
);
|
||||||
|
|
||||||
|
// Insert h5p content.
|
||||||
|
$contentid = $this->framework->insertContent($content);
|
||||||
|
|
||||||
|
// Get the entered content from the db.
|
||||||
|
$dbcontent = $DB->get_record('h5p', ['id' => $contentid]);
|
||||||
|
|
||||||
|
// Make sure the h5p content was properly inserted.
|
||||||
|
$this->assertNotEmpty($dbcontent);
|
||||||
|
$this->assertEquals($content['params'], $dbcontent->jsoncontent);
|
||||||
|
$this->assertEquals($content['disable'], $dbcontent->displayoptions);
|
||||||
|
// As the libraryId was empty, the latest library has been used.
|
||||||
|
$this->assertEquals($lib->id, $dbcontent->mainlibraryid);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the behaviour of updateContent().
|
* Test the behaviour of updateContent().
|
||||||
*/
|
*/
|
||||||
|
@ -2014,4 +2049,48 @@ class framework_testcase extends \advanced_testcase {
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the behaviour of get_latest_library_version().
|
||||||
|
*/
|
||||||
|
public function test_get_latest_library_version() {
|
||||||
|
global $DB;
|
||||||
|
|
||||||
|
$this->resetAfterTest();
|
||||||
|
|
||||||
|
$generator = $this->getDataGenerator()->get_plugin_generator('core_h5p');
|
||||||
|
// Create a library record.
|
||||||
|
$machinename = 'TestLibrary';
|
||||||
|
$lib1 = $generator->create_library_record($machinename, 'Test', 1, 1, 2);
|
||||||
|
$lib2 = $generator->create_library_record($machinename, 'Test', 1, 2, 1);
|
||||||
|
|
||||||
|
$content = array(
|
||||||
|
'params' => json_encode(['param1' => 'Test']),
|
||||||
|
'library' => array(
|
||||||
|
'libraryId' => 0,
|
||||||
|
'machineName' => 'TestLibrary',
|
||||||
|
),
|
||||||
|
'disable' => 8
|
||||||
|
);
|
||||||
|
|
||||||
|
// Get the latest id (at this point, should be lib2).
|
||||||
|
$latestlib = $this->framework->get_latest_library_version($machinename);
|
||||||
|
$this->assertEquals($lib2->id, $latestlib->id);
|
||||||
|
|
||||||
|
// Get the latest id (at this point, should be lib3).
|
||||||
|
$lib3 = $generator->create_library_record($machinename, 'Test', 2, 1, 0);
|
||||||
|
$latestlib = $this->framework->get_latest_library_version($machinename);
|
||||||
|
$this->assertEquals($lib3->id, $latestlib->id);
|
||||||
|
|
||||||
|
// Get the latest id (at this point, should be still lib3).
|
||||||
|
$lib4 = $generator->create_library_record($machinename, 'Test', 1, 1, 3);
|
||||||
|
$latestlib = $this->framework->get_latest_library_version($machinename);
|
||||||
|
$this->assertEquals($lib3->id, $latestlib->id);
|
||||||
|
|
||||||
|
// Get the latest id (at this point, should be lib5).
|
||||||
|
$lib5 = $generator->create_library_record($machinename, 'Test', 2, 1, 6);
|
||||||
|
$latestlib = $this->framework->get_latest_library_version($machinename);
|
||||||
|
$this->assertEquals($lib5->id, $latestlib->id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue