Skip to content

Commit ba8f7ef

Browse files
committed
Feature: add bulk libray actions
This includes: - Bulk update libraries to latest versions - Bulk upgrade content to latest library version - Bulk export libraries (for importing to another site) - Bulk import is already a feature using the existing "Upload Libraries"
1 parent 1e76b27 commit ba8f7ef

6 files changed

+924
-0
lines changed

export_libraries.php

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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+
/**
18+
* Export all libraries script.
19+
*
20+
* @package mod_hvp
21+
* @author Rossco Hellmans <[email protected]>
22+
* @copyright Catalyst IT, 2021
23+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24+
*/
25+
26+
require_once('../../config.php');
27+
require_once($CFG->libdir . '/adminlib.php');
28+
require_once('locallib.php');
29+
30+
admin_externalpage_setup('h5plibraries');
31+
32+
$COURSE = $SITE;
33+
34+
$interface = mod_hvp\framework::instance('interface');
35+
$core = mod_hvp\framework::instance('core');
36+
$exporter = new H5PExport($interface, $core);
37+
38+
$libraries = $core->h5pF->loadLibraries();
39+
$tmppath = make_request_directory();
40+
41+
$exportedlibraries = [];
42+
43+
// Add libraries to h5p.
44+
foreach ($libraries as $versions) {
45+
try {
46+
// We only want to export the latest version.
47+
$libraryinfo = array_pop($versions);
48+
$library = $interface->loadLibrary(
49+
$libraryinfo->machine_name,
50+
$libraryinfo->major_version,
51+
$libraryinfo->minor_version
52+
);
53+
54+
exportlibrary($library, $exporter, $tmppath, $exportedlibraries);
55+
} catch (Exception $e) {
56+
throw new moodle_exception('exportlibrarieserror', 'hvp', '', null, $e->getMessage());
57+
}
58+
}
59+
60+
$files = [];
61+
populatefilelist($tmppath, $files);
62+
63+
// Get path to temporary export target file.
64+
$tmpfile = tempnam(get_request_storage_directory(), 'hvplibs');
65+
66+
// Create new zip instance.
67+
$zip = new ZipArchive();
68+
$zip->open($tmpfile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
69+
70+
// Add all the files from the tmp dir.
71+
foreach ($files as $file) {
72+
// Please note that the zip format has no concept of folders, we must
73+
// use forward slashes to separate our directories.
74+
if (file_exists(realpath($file->absolutePath))) {
75+
$zip->addFile(realpath($file->absolutePath), $file->relativePath);
76+
}
77+
}
78+
79+
// Close zip.
80+
$zip->close();
81+
82+
$filename = $SITE->shortname . '_site_libraries.h5p';
83+
try {
84+
// Save export.
85+
$exporter->h5pC->fs->saveExport($tmpfile, $filename);
86+
} catch (Exception $e) {
87+
throw new moodle_exception('exportlibrarieserror', 'hvp', '', null, $e->getMessage());
88+
}
89+
90+
// Now send the stored export to user.
91+
$context = \context_course::instance($COURSE->id);
92+
$fs = get_file_storage();
93+
$file = $fs->get_file($context->id, 'mod_hvp', 'exports', 0, '/', $filename);
94+
send_stored_file($file);
95+
96+
/**
97+
* Exports a library.
98+
*
99+
* @param array $library
100+
* @param H5PExport $exporter
101+
* @param string $tmppath
102+
* @param array $exportedlibraries
103+
*/
104+
function exportlibrary($library, $exporter, $tmppath, &$exportedlibraries) {
105+
if (in_array($library['libraryId'], $exportedlibraries)) {
106+
// Already exported.
107+
return;
108+
}
109+
110+
$exportfolder = null;
111+
112+
// Determine path of export library.
113+
if (isset($exporter->h5pC) && isset($exporter->h5pC->h5pD)) {
114+
115+
// Tries to find library in development folder.
116+
$isdevlibrary = $exporter->h5pC->h5pD->getLibrary(
117+
$library['machineName'],
118+
$library['majorVersion'],
119+
$library['minorVersion']
120+
);
121+
122+
if ($isdevlibrary !== null && isset($library['path'])) {
123+
$exportfolder = "/" . $library['path'];
124+
}
125+
}
126+
127+
// Export library.
128+
$exporter->h5pC->fs->exportLibrary($library, $tmppath, $exportfolder);
129+
$exportedlibraries[] = $library['libraryId'];
130+
131+
// Now export the dependancies.
132+
$dependencies = [];
133+
$exporter->h5pC->findLibraryDependencies($dependencies, $library);
134+
135+
foreach ($dependencies as $dependency) {
136+
exportlibrary($dependency['library'], $exporter, $tmppath, $exportedlibraries);
137+
}
138+
}
139+
140+
/**
141+
* Populates an array with a list of files to zip up.
142+
*
143+
* @param string $dir
144+
* @param array $files
145+
* @param string $relative
146+
*/
147+
function populatefilelist($dir, &$files, $relative = '') {
148+
$strip = strlen($dir) + 1;
149+
$contents = glob($dir . '/' . '*');
150+
if (!empty($contents)) {
151+
foreach ($contents as $file) {
152+
$rel = $relative . substr($file, $strip);
153+
if (is_dir($file)) {
154+
populatefilelist($file, $files, $rel . '/');
155+
} else {
156+
$files[] = (object)[
157+
'absolutePath' => $file,
158+
'relativePath' => $rel,
159+
];
160+
}
161+
}
162+
}
163+
}

0 commit comments

Comments
 (0)