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