Skip to content

Commit 70af5d2

Browse files
committed
change: move bulk library updating into adhoc tasks
1 parent d4d2400 commit 70af5d2

File tree

3 files changed

+125
-38
lines changed

3 files changed

+125
-38
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace mod_hvp\task;
18+
19+
use core\task\adhoc_task;
20+
use mod_hvp\framework;
21+
22+
/**
23+
* Update H5P library task.
24+
*
25+
* @package mod_hvp
26+
* @author Rossco Hellmans <rosscohellmans@catalyst-au.net>
27+
* @copyright 2025 Catalyst IT Australia Pty Ltd
28+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29+
*/
30+
class update_library_task extends adhoc_task {
31+
/**
32+
* Get task name
33+
*
34+
* @return string
35+
*/
36+
public function get_name() {
37+
return get_string('updatelibrarytask', 'mod_hvp');
38+
}
39+
40+
/**
41+
* Execute task
42+
*/
43+
public function execute() {
44+
global $DB;
45+
46+
$data = $this->get_custom_data();
47+
if (empty($data->machinename)) {
48+
mtrace('No library set to update, ending task.');
49+
return;
50+
}
51+
52+
$machinename = $data->machinename;
53+
$librarytitle = $data->librarytitle;
54+
$editor = framework::instance('editor');
55+
$ajax = $editor->ajax;
56+
\H5PCore::createToken('editorajax');
57+
58+
// Look up content type to ensure it's valid(and to check permissions).
59+
$contenttype = $editor->ajaxInterface->getContentTypeCache($machinename);
60+
if (!$contenttype) {
61+
throw new \moodle_exception('updateinvalidcontenttype', 'mod_hvp', '', $librarytitle);
62+
}
63+
64+
// Override core permission check.
65+
$ajax->core->mayUpdateLibraries(true);
66+
67+
// Retrieve content type from hub endpoint.
68+
$endpoint = \H5PHubEndpoints::CONTENT_TYPES . $machinename;
69+
$url = \H5PHubEndpoints::createURL($endpoint);
70+
$response = $ajax->core->h5pF->fetchExternalData($url, null, true, true);
71+
if (!$response) {
72+
throw new \moodle_exception('updatedownloadfailed', 'mod_hvp', '', $librarytitle);
73+
};
74+
$path = $ajax->core->h5pF->getUploadedH5pPath();
75+
76+
// Validate package.
77+
$validator = new \H5PValidator($ajax->core->h5pF, $ajax->core);
78+
if (!$validator->isValidPackage(true, true)) {
79+
$ajax->storage->removeTemporarilySavedFiles($path);
80+
throw new \moodle_exception('updatevalidationfailed', 'mod_hvp', '', $librarytitle);
81+
}
82+
83+
// Save H5P.
84+
$storage = new \H5PStorage($ajax->core->h5pF, $ajax->core);
85+
$storage->savePackage(null, null, true);
86+
87+
// Clean up.
88+
$ajax->storage->removeTemporarilySavedFiles($path);
89+
90+
// Refresh content types.
91+
$librariescache = $ajax->editor->getLatestGlobalLibrariesData();
92+
93+
$library = $DB->get_record('hvp_libraries_hub_cache', ['machine_name' => $machinename]);
94+
mtrace("Successfully updated {$librarytitle} to version {$library->major_version}.{$library->minor_version}");
95+
}
96+
}

lang/en/hvp.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,10 @@
680680
// Update all libraries.
681681
$string['updatealllibraries'] = 'Update libraries';
682682
$string['updatealllibrariesconfirm'] = 'Do you wish to update all libraries (exluding restricted libraries)?';
683+
$string['updatelibrarytask'] = 'Update library';
684+
$string['updatedownloadfailed'] = 'Unable to update {$a}: DOWNLOAD_FAILED';
685+
$string['updateinvalidcontenttype'] = 'Unable to update {$a}: INVALID_CONTENT_TYPE';
686+
$string['updatevalidationfailed'] = 'Unable to update {$a}: VALIDATION_FAILED';
683687

684688
// Upgrade all content.
685689
$string['upgradebulkcontent'] = 'Upgrade all content';

update_libraries.php

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@
5151
$progressbar->create();
5252
$progressbar->update(0, 1, 'Finding libraries with updates');
5353

54+
// Update the hub cache first so we have the latest version info.
5455
$editor = mod_hvp\framework::instance('editor');
5556
$ajax = $editor->ajax;
5657
$token = \H5PCore::createToken('editorajax');
57-
58-
// Update the hub cache first so we have the latest version info.
5958
$ajax->core->updateContentTypeCache();
6059

6160
$sql = "SELECT DISTINCT lhc.machine_name, lhc.title, lhc.major_version, lhc.minor_version
@@ -77,50 +76,38 @@
7776
$total = count($libraries);
7877
$counter = 0;
7978

80-
foreach ($libraries as $library) {
81-
$progressbar->update($counter, $total, "Updating {$library->title}");
82-
$counter++;
79+
$queuedlibraries = [];
8380

81+
foreach ($libraries as $library) {
8482
$machinename = $library->machine_name;
83+
$librarytitle = $library->title;
8584

86-
// Look up content type to ensure it's valid(and to check permissions).
87-
$contenttype = $editor->ajaxInterface->getContentTypeCache($machinename);
88-
if (!$contenttype) {
89-
echo $OUTPUT->notification("Unable to update {$library->title}: INVALID_CONTENT_TYPE", 'error');
90-
break;
91-
}
85+
$progressbar->update($counter, $total, "Creating update task for {$librarytitle}");
86+
$counter++;
9287

93-
// Override core permission check.
94-
$ajax->core->mayUpdateLibraries(true);
95-
96-
// Retrieve content type from hub endpoint.
97-
$endpoint = H5PHubEndpoints::CONTENT_TYPES . $machinename;
98-
$url = H5PHubEndpoints::createURL($endpoint);
99-
$response = $ajax->core->h5pF->fetchExternalData($url, null, true, true);
100-
if (!$response) {
101-
echo $OUTPUT->notification("Unable to update {$library->title}: DOWNLOAD_FAILED", 'error');
102-
break;
103-
};
104-
$path = $ajax->core->h5pF->getUploadedH5pPath();
105-
106-
// Validate package.
107-
$validator = new H5PValidator($ajax->core->h5pF, $ajax->core);
108-
if (!$validator->isValidPackage(true, true)) {
109-
$ajax->storage->removeTemporarilySavedFiles($path);
110-
echo $OUTPUT->notification("Unable to update {$library->title}: VALIDATION_FAILED", 'error');
111-
break;
112-
}
88+
$updatelibrarytask = new mod_hvp\task\update_library_task();
89+
$updatelibrarytask->set_custom_data([
90+
'machinename' => $machinename,
91+
'librarytitle' => $librarytitle,
92+
]);
93+
\core\task\manager::queue_adhoc_task($updatelibrarytask, true);
11394

114-
// Save H5P.
115-
$storage = new H5PStorage($ajax->core->h5pF, $ajax->core);
116-
$storage->savePackage(null, null, true);
95+
$version = "{$library->major_version}.{$library->minor_version}";
96+
$queuedlibraries[$librarytitle] = $version;
97+
}
11798

118-
// Clean up.
119-
$ajax->storage->removeTemporarilySavedFiles($path);
99+
if (!empty($queuedlibraries)) {
100+
$message = 'The following libraries have been queued for updating:';
101+
$message .= html_writer::start_tag('ul');
102+
foreach ($queuedlibraries as $librarytitle => $version) {
103+
$message .= html_writer::tag('li', "{$librarytitle} ({$version})");
104+
}
105+
$message .= html_writer::end_tag('ul');
106+
} else {
107+
$message = 'No libraries have been queued for updating.';
120108
}
121109

122-
// Refresh content types.
123-
$librariescache = $ajax->editor->getLatestGlobalLibrariesData();
110+
\core\notification::add($message, \core\notification::SUCCESS);
124111

125112
$progressbar->update(1, 1, get_string('completed'));
126113
echo $OUTPUT->single_button($returnurl, get_string('upgradereturn', 'hvp'));

0 commit comments

Comments
 (0)