|
51 | 51 | $progressbar->create(); |
52 | 52 | $progressbar->update(0, 1, 'Finding libraries with updates'); |
53 | 53 |
|
| 54 | + // Update the hub cache first so we have the latest version info. |
54 | 55 | $editor = mod_hvp\framework::instance('editor'); |
55 | 56 | $ajax = $editor->ajax; |
56 | 57 | $token = \H5PCore::createToken('editorajax'); |
57 | | - |
58 | | - // Update the hub cache first so we have the latest version info. |
59 | 58 | $ajax->core->updateContentTypeCache(); |
60 | 59 |
|
61 | 60 | $sql = "SELECT DISTINCT lhc.machine_name, lhc.title, lhc.major_version, lhc.minor_version |
|
77 | 76 | $total = count($libraries); |
78 | 77 | $counter = 0; |
79 | 78 |
|
80 | | - foreach ($libraries as $library) { |
81 | | - $progressbar->update($counter, $total, "Updating {$library->title}"); |
82 | | - $counter++; |
| 79 | + $queuedlibraries = []; |
83 | 80 |
|
| 81 | + foreach ($libraries as $library) { |
84 | 82 | $machinename = $library->machine_name; |
| 83 | + $librarytitle = $library->title; |
85 | 84 |
|
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++; |
92 | 87 |
|
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); |
113 | 94 |
|
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 | + } |
117 | 98 |
|
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.'; |
120 | 108 | } |
121 | 109 |
|
122 | | - // Refresh content types. |
123 | | - $librariescache = $ajax->editor->getLatestGlobalLibrariesData(); |
| 110 | + \core\notification::add($message, \core\notification::SUCCESS); |
124 | 111 |
|
125 | 112 | $progressbar->update(1, 1, get_string('completed')); |
126 | 113 | echo $OUTPUT->single_button($returnurl, get_string('upgradereturn', 'hvp')); |
|
0 commit comments