From c528229d10665de714d87b86e02bcfc7c5cfd3f5 Mon Sep 17 00:00:00 2001 From: iszmais <45942348+iszmais@users.noreply.github.com> Date: Tue, 28 Jan 2025 10:48:21 +0100 Subject: [PATCH] Export: data collection, export options (#8206) (#8911) Co-authored-by: Christoph Ludolf <127395945+chlulei@users.noreply.github.com> --- ...lass.ilDataCollectionExportOptionsXLSX.php | 161 ++++++++++++++++++ .../classes/class.ilObjDataCollectionGUI.php | 12 +- 2 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 components/ILIAS/DataCollection/classes/class.ilDataCollectionExportOptionsXLSX.php diff --git a/components/ILIAS/DataCollection/classes/class.ilDataCollectionExportOptionsXLSX.php b/components/ILIAS/DataCollection/classes/class.ilDataCollectionExportOptionsXLSX.php new file mode 100644 index 000000000000..e51b66739d52 --- /dev/null +++ b/components/ILIAS/DataCollection/classes/class.ilDataCollectionExportOptionsXLSX.php @@ -0,0 +1,161 @@ +lng = $DIC->language(); + $this->tpl = $DIC->ui()->mainTemplate(); + parent::init($DIC); + } + + public function getExportType(): string + { + return 'xlsx'; + } + + public function getExportOptionId(): string + { + return 'dcl_exp_option_xlsx'; + } + + public function getSupportedRepositoryObjectTypes(): array + { + return ['dcl']; + } + + public function getLabel(): string + { + return $this->lng->txt('dcl_xls_async_export'); + } + + public function getFiles( + ilExportHandlerConsumerContextInterface $context + ): ilExportHandlerFileInfoCollectionInterface { + $collection_builder = $context->fileCollectionBuilder(); + $dir = ilExport::_getExportDirectory( + $context->exportObject()->getId(), + $this->getExportType(), + $context->exportObject()->getType() + ); + $file_infos = $this->getExportFiles($dir); + $object_id = new ObjectId($context->exportObject()->getId()); + foreach ($file_infos as $file_name => $file_info) { + $collection_builder = $collection_builder->withSPLFileInfo( + new SplFileInfo($dir . DIRECTORY_SEPARATOR . $file_info["file"]), + $object_id, + $this + ); + } + return $collection_builder->collection(); + } + + public function onExportOptionSelected( + ilExportHandlerConsumerContextInterface $context + ): void { + if (!$this->checkForExportableFields($context)) { + return; + } + $this->ctrl->redirectByClass(ilObjDataCollectionGUI::class, "handleExportAsync"); + } + + public function onDeleteFiles( + ilExportHandlerConsumerContextInterface $context, + ilExportHandlerConsumerFileIdentifierCollectionInterface $file_identifiers + ): void { + foreach ($file_identifiers as $file_identifier) { + $file = explode(":", $file_identifier->getIdentifier()); + $file[1] = basename($file[1]); + $export_dir = ilExport::_getExportDirectory( + $context->exportObject()->getId(), + str_replace("..", "", $file[0]), + $context->exportObject()->getType() + ); + $exp_file = $export_dir . "/" . str_replace("..", "", $file[1]); + $exp_dir = $export_dir . "/" . substr($file[1], 0, strlen($file[1]) - 5); + if (is_file($exp_file)) { + unlink($exp_file); + } + if ( + is_dir($exp_dir) and + count(scandir($exp_dir)) === 2 + ) { + ilFileUtils::delDir($exp_dir); + } + } + } + + protected function getExportFiles( + string $directory + ): array { + $file = []; + try { + $h_dir = dir($directory); + while ($entry = $h_dir->read()) { + if ( + $entry !== "." && + $entry !== ".." && + substr($entry, -5) === "." . $this->getExportType() + ) { + $ts = substr($entry, 0, strpos($entry, "__")); + $file[$entry . $this->getExportType()] = [ + "type" => $this->getExportType(), + "file" => $entry, + "size" => (int) filesize($directory . "/" . $entry), + "timestamp" => (int) $ts + ]; + } + } + } catch (Exception $e) { + } + return $file; + } + + protected function checkForExportableFields( + ilExportHandlerConsumerContextInterface $context + ): bool { + $obj = $context->exportObject(); + + foreach ($obj->getTables() as $tbl) { + /** @var $tbl ilDclTable */ + foreach ($tbl->getFields() as $field) { + if ($field->getExportable()) { + return true; + } + } + } + + $this->tpl->setOnScreenMessage('failure', $this->lng->txt('dcl_no_export_data_available'), true); + $this->ctrl->redirect($context->exportGUIObject(), "listExportFiles"); + + return false; + } +} diff --git a/components/ILIAS/DataCollection/classes/class.ilObjDataCollectionGUI.php b/components/ILIAS/DataCollection/classes/class.ilObjDataCollectionGUI.php index 225e2f8ac972..367bd2b7fe30 100755 --- a/components/ILIAS/DataCollection/classes/class.ilObjDataCollectionGUI.php +++ b/components/ILIAS/DataCollection/classes/class.ilObjDataCollectionGUI.php @@ -271,9 +271,6 @@ protected function handleExport(bool $do_default = false): void { $this->tabs->setTabActive(self::TAB_EXPORT); $exp_gui = new ilDclExportGUI($this); - $exporter = new ilDclContentExporter($this->object->getRefId(), $this->table_id); - $exp_gui->addFormat("xlsx", $this->lng->txt('dcl_xls_async_export'), $exporter, 'exportAsync'); - $exp_gui->addFormat("xml"); if ($do_default) { $exp_gui->listExportFiles(); } else { @@ -281,6 +278,15 @@ protected function handleExport(bool $do_default = false): void } } + protected function handleExportAsync(): void + { + $this->tabs->setTabActive(self::TAB_EXPORT); + $exp_gui = new ilDclExportGUI($this); + $exporter = new ilDclContentExporter($this->object->getRefId(), $this->table_id); + $exporter->exportAsync(); + $this->ctrl->redirect($exp_gui); + } + protected function getTableViewId(): int { $tableview_id = null;