Skip to content

Commit

Permalink
Export: testquestionpool, export options
Browse files Browse the repository at this point in the history
  • Loading branch information
chlulei committed Oct 16, 2024
1 parent 83298b3 commit f4d816e
Show file tree
Hide file tree
Showing 3 changed files with 372 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use ILIAS\TestQuestionPool\RequestDataCollector;
use ILIAS\TestQuestionPool\Presentation\QuestionTable;
use ILIAS\TestQuestionPool\Questions\GeneralQuestionPropertiesRepository;

use ILIAS\DI\RBACServices;
use ILIAS\Taxonomy\Service;
use ILIAS\UI\Component\Input\Container\Form\Form;
use ILIAS\UI\Component\Input\Field\Select;
Expand Down Expand Up @@ -56,7 +58,6 @@
* @ilCtrl_Calls ilObjQuestionPoolGUI: ilAssQuestionPreviewGUI
* @ilCtrl_Calls ilObjQuestionPoolGUI: assKprimChoiceGUI, assLongMenuGUI
* @ilCtrl_Calls ilObjQuestionPoolGUI: ilQuestionPoolSkillAdministrationGUI
* @ilCtrl_Calls ilObjQuestionPoolGUI: ilBulkEditQuestionsGUI
*
* @ingroup components\ILIASTestQuestionPool
*
Expand All @@ -67,6 +68,7 @@ class ilObjQuestionPoolGUI extends ilObjectGUI implements ilCtrlBaseClassInterfa

public const SUPPORTED_IMPORT_MIME_TYPES = [MimeType::APPLICATION__ZIP, MimeType::TEXT__XML];
public const DEFAULT_CMD = 'questions';
public const CREATE_XLSX_EXPORT = 'createExportExcel';

private HTTPServices $http;
protected Service $taxonomy;
Expand Down Expand Up @@ -340,8 +342,6 @@ public function executeCommand(): void

case 'ilquestionpoolexportgui':
$exp_gui = new ilQuestionPoolExportGUI($this);
$exp_gui->addFormat('xml', $this->lng->txt('qpl_export_xml'));
$exp_gui->addFormat('xlsx', $this->lng->txt('qpl_export_excel'), $this, 'createExportExcel');
$ret = $this->ctrl->forwardCommand($exp_gui);
break;

Expand Down Expand Up @@ -654,6 +654,10 @@ public function executeCommand(): void
if ($cmd == self::DEFAULT_CMD) {
$this->ctrl->setParameter($this, 'q_id', '');
}
if ($cmd == self::CREATE_XLSX_EXPORT) {
$this->$cmd();
break;
}
$cmd .= 'Object';
$ret = $this->$cmd();
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?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 ilTestQuestionPoolExportOptionXLSX extends ilBasicLegacyExportOption
{
protected ilLanguage $lng;
protected ilCtrl $ctrl;

public function init(Container $DIC): void
{
$this->lng = $DIC->language();
$this->ctrl = $DIC->ctrl();
}

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

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

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

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

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 = $this->getDirectory(
$context->exportObject()->getId(),
$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);
}
}
}

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->toInt(), $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->toInt(), $type);
$file[1] = basename($file[1]);
ilFileDelivery::deliverFileLegacy(
$export_dir . "/" . $file[1],
$file[1]
);
}

public function getFiles(
ilExportHandlerConsumerContextInterface $context
): ilExportHandlerFileInfoCollectionInterface {
$collection_builder = $context->fileCollectionBuilder();
$dir = $this->getDirectory(
$context->exportObject()->getId(),
$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 {
$this->ctrl->redirectByClass(ilObjQuestionPoolGUI::class, ilObjQuestionPoolGUI::CREATE_XLSX_EXPORT);
}

protected function getExportFiles(
string $directory
): array {
$file = [];
try {
$h_dir = dir($directory);
while ($entry = $h_dir->read()) {
if (
$entry !== "." &&
$entry !== ".." &&
substr($entry, -5) === ".xlsx"
) {
$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(
int $object_id,
string $object_type
): string {
$dir = ilExport::_getExportDirectory(
$object_id,
"",
$object_type
);
$dir .= "xlsx";
return $dir;
}
}
Loading

0 comments on commit f4d816e

Please sign in to comment.