Skip to content

Commit

Permalink
Export: test, eport options
Browse files Browse the repository at this point in the history
  • Loading branch information
chlulei committed Oct 17, 2024
1 parent 83298b3 commit dce015e
Show file tree
Hide file tree
Showing 6 changed files with 1,010 additions and 13 deletions.
78 changes: 65 additions & 13 deletions components/ILIAS/Test/classes/class.ilTestExportGUI.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
use ILIAS\TestQuestionPool\Questions\GeneralQuestionPropertiesRepository;
use ILIAS\Test\Logging\TestLogger;
use ILIAS\Test\Scoring\Manual\TestScoring;

use ILIAS\UI\Factory as UIFactory;
use ILIAS\UI\Renderer as UIRenderer;
use ILIAS\ResourceStorage\Services as IRSS;
use ILIAS\UI\Component\Input\Container\Form\Standard as StandardForm;
use ILIAS\DI\UIServices as UIServices;

/**
* Export User Interface Class
Expand All @@ -38,9 +39,13 @@
* @ingroup components\ILIASTest
*
* @ilCtrl_Calls ilTestExportGUI: ilParticipantsTestResultsGUI
* @ilCtrl_Calls ilTestExportGUI: ilExportGUI
*/
class ilTestExportGUI extends ilExportGUI
{
protected array $active_export_plugins;
protected UIServices $ui;

public function __construct(
ilObjTestGUI $parent_gui,
private readonly ilDBInterface $db,
Expand All @@ -58,21 +63,68 @@ public function __construct(
private readonly GeneralQuestionPropertiesRepository $questionrepository,
private readonly RequestDataCollector $testrequest
) {
$this->active_export_plugins = iterator_to_array($active_export_plugins);
parent::__construct($parent_gui, null);
}

$this->addFormat('xml', $this->lng->txt('ass_create_export_file'));
$this->addFormat('xmlres', $this->lng->txt('ass_create_export_file_with_results'), $this, 'createTestExportWithResults');
$this->addFormat('csv', $this->lng->txt('ass_create_export_test_results'), $this, 'createTestResultsExport');
$this->addFormat('arc', $this->lng->txt('ass_create_export_test_archive'), $this, 'createTestArchiveExport');
foreach ($active_export_plugins as $plugin) {
$plugin->setTest($this->obj);
$this->addFormat(
$plugin->getFormat(),
$plugin->getFormatLabel(),
$plugin,
'export'
);
public function executeCommand(): void
{
$cmd = $this->ctrl->getCmd();
if (
$cmd === "exportPlugin" ||
$cmd === "showExportPluginMenu"
) {
$this->$cmd();
return;
}
parent::executeCommand();
}

protected function buildExportPluginMenuForm(): StandardForm
{
$this->lng->loadLanguageModule('exp');
$items = [];
foreach ($this->active_export_plugins as $plugin) {
/** @var ilPlugin $plugin */
$items[$plugin->getId()] = $plugin->getFormatLabel();
}
$select = $this->ui_factory->input()->field()->select($this->lng->txt("export_type"), $items)
->withRequired(true);
$section = $this->ui_factory->input()->field()->section(
[$select],
$this->lng->txt("export_options")
);
return $this->ui_factory->input()->container()->form()->standard(
$this->ctrl->getLinkTarget($this, "exportPlugin"),
[$section]
)->withSubmitLabel($this->lng->txt("export"));
}

protected function exportPlugin(): void
{
$form = $this->buildExportPluginMenuForm()->withRequest($this->http->request());
$plugin = null;
if (!is_null($form->getData())) {
$plugin_id = $form->getData()[0][0];
foreach ($this->active_export_plugins as $current_plugin) {
if ($current_plugin->getId() === $plugin_id) {
$plugin = $current_plugin;
break;
}
}
}
if (is_null($form->getData())) {
$this->tpl->setContent($this->ui_renderer->render($form));
return;
}
$plugin->export();
$this->ctrl->redirectByClass(ilExportGUI::class, ilExportGUI::CMD_LIST_EXPORT_FILES);
}

public function showExportPluginMenu(): void
{
$form = $this->buildExportPluginMenuForm();
$this->tpl->setContent($this->ui_renderer->render($form));
}

/**
Expand Down
175 changes: 175 additions & 0 deletions components/ILIAS/Test/classes/class.ilTestExportOptionARC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

use ILIAS\Data\ObjectId;
use ILIAS\Data\ReferenceId;
use ILIAS\Export\ExportHandler\Consumer\ExportOption\BasicLegacyHandler as ilBasicLegacyExportOption;
use ILIAS\Export\ExportHandler\I\Consumer\Context\HandlerInterface as ilExportHandlerConsumerContextInterface;
use ILIAS\DI\Container;
use ILIAS\Export\ExportHandler\I\Info\File\CollectionInterface as ilExportHandlerFileInfoCollectionInterface;
use ILIAS\Export\ExportHandler\I\Consumer\File\Identifier\CollectionInterface as ilExportHandlerConsumerFileIdentifierCollectionInterface;
use ILIAS\Export\ExportHandler\I\Consumer\File\Identifier\HandlerInterface as ilExportHandlerConsumerFileIdentifierInterface;

class ilTestExportOptionARC extends ilBasicLegacyExportOption
{
protected ilLanguage $lng;
protected string $data_dir;

public function init(
Container $DIC
): void {
$this->lng = $DIC->language();
$this->data_dir = $DIC->iliasIni()->readVariable('clients', 'datadir');
parent::init($DIC);
}

public function getExportType(): string
{
return 'Archive';
}

public function getExportOptionId(): string
{
return 'test_exp_option_arc';
}

public function getSupportedRepositoryObjectTypes(): array
{
return ['tst'];
}

public function getLabel(): string
{
return $this->lng->txt('ass_create_export_test_archive');
}

public function onDeleteFiles(
ilExportHandlerConsumerContextInterface $context,
ilExportHandlerConsumerFileIdentifierCollectionInterface $file_identifiers
): void {
$object_id = new ObjectId($context->exportObject()->getId());
foreach ($file_identifiers as $file_identifier) {
$file = explode(":", $file_identifier->getIdentifier());
$file[1] = basename($file[1]);
$export_dir = $this->getDirectory($object_id, $context->exportObject()->getType());
$exp_file = $export_dir . "/" . str_replace("..", "", $file[1]);
$exp_dir = $export_dir . "/" . substr($file[1], 0, strlen($file[1]) - 4);
if (is_file($exp_file)) {
unlink($exp_file);
}
if (
is_dir($exp_dir) and
count(scandir($exp_dir)) === 2
) {
ilFileUtils::delDir($exp_dir);
}
}
}

public function onDownloadFiles(
ilExportHandlerConsumerContextInterface $context,
ilExportHandlerConsumerFileIdentifierCollectionInterface $file_identifiers
): void {
$object_id = new ObjectId($context->exportObject()->getId());
foreach ($file_identifiers as $file_identifier) {
$file = explode(":", trim($file_identifier->getIdentifier()));
$export_dir = $this->getDirectory($object_id, $context->exportObject()->getType());
$file[1] = basename($file[1]);
ilFileDelivery::deliverFileLegacy(
$export_dir . "/" . $file[1],
$file[1]
);
}
}

public function onDownloadWithLink(
ReferenceId $reference_id,
ilExportHandlerConsumerFileIdentifierInterface $file_identifier
): void {
$object_id = $reference_id->toObjectId();
$type = ilObject::_lookupType($object_id->toInt());
$file = explode(":", trim($file_identifier->getIdentifier()));
$export_dir = $this->getDirectory($object_id, $type);
$file[1] = basename($file[1]);
ilFileDelivery::deliverFileLegacy(
$export_dir . "/" . $file[1],
$file[1]
);
}

public function getFiles(
ilExportHandlerConsumerContextInterface $context
): ilExportHandlerFileInfoCollectionInterface {
$collection_builder = $context->fileCollectionBuilder();
$object_id = new ObjectId($context->exportObject()->getId());
$dir = $this->getDirectory($object_id, $context->exportObject()->getType());
$file_infos = $this->getExportFiles($dir);
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 {
$context->exportGUIObject()->createTestArchiveExport();
}

protected function getExportFiles(
string $directory
): array {
$file = [];
try {
$h_dir = dir($directory);
while ($entry = $h_dir->read()) {
if (
$entry !== "." &&
$entry !== ".." &&
substr($entry, -4) === ".zip"
) {
$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 getDirectory(
ObjectId $object_id,
string $export_object_type
): string {
return $this->data_dir
. ilTestArchiver::DIR_SEP . CLIENT_ID . ilTestArchiver::DIR_SEP . 'tst_data' . ilTestArchiver::DIR_SEP
. ilTestArchiver::EXPORT_DIRECTORY . DIRECTORY_SEPARATOR . $export_object_type . "_" . $object_id->toInt();
}
}
Loading

0 comments on commit dce015e

Please sign in to comment.