ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilTestExportOptionARC.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24use ILIAS\Export\ExportHandler\I\Consumer\Context\HandlerInterface as ilExportHandlerConsumerContextInterface;
26use ILIAS\Export\ExportHandler\I\Info\File\CollectionInterface as ilExportHandlerFileInfoCollectionInterface;
27use ILIAS\Export\ExportHandler\I\Consumer\File\Identifier\CollectionInterface as ilExportHandlerConsumerFileIdentifierCollectionInterface;
28use ILIAS\Export\ExportHandler\I\Consumer\File\Identifier\HandlerInterface as ilExportHandlerConsumerFileIdentifierInterface;
29
30class ilTestExportOptionARC extends ilBasicLegacyExportOption
31{
32 protected ilLanguage $lng;
33 protected string $data_dir;
34
35 public function init(
37 ): void {
38 $this->lng = $DIC->language();
39 $this->data_dir = $DIC->iliasIni()->readVariable('clients', 'datadir');
41 }
42
43 public function getExportType(): string
44 {
45 return 'Archive';
46 }
47
48 public function getExportOptionId(): string
49 {
50 return 'test_exp_option_arc';
51 }
52
53 public function getSupportedRepositoryObjectTypes(): array
54 {
55 return ['tst'];
56 }
57
58 public function getLabel(): string
59 {
60 $this->lng->loadLanguageModule('assessment');
61 return $this->lng->txt('ass_create_export_test_archive');
62 }
63
64 public function onDeleteFiles(
65 ilExportHandlerConsumerContextInterface $context,
66 ilExportHandlerConsumerFileIdentifierCollectionInterface $file_identifiers
67 ): void {
68 $object_id = new ObjectId($context->exportObject()->getId());
69 foreach ($file_identifiers as $file_identifier) {
70 $file = explode(":", $file_identifier->getIdentifier());
71 $file[1] = basename($file[1]);
72 $export_dir = $this->getDirectory($object_id, $context->exportObject()->getType());
73 $exp_file = $export_dir . "/" . str_replace("..", "", $file[1]);
74 $exp_dir = $export_dir . "/" . substr($file[1], 0, strlen($file[1]) - 4);
75 if (is_file($exp_file)) {
76 unlink($exp_file);
77 }
78 if (
79 is_dir($exp_dir) and
80 count(scandir($exp_dir)) === 2
81 ) {
82 ilFileUtils::delDir($exp_dir);
83 }
84 }
85 }
86
87 public function onDownloadFiles(
88 ilExportHandlerConsumerContextInterface $context,
89 ilExportHandlerConsumerFileIdentifierCollectionInterface $file_identifiers
90 ): void {
91 $object_id = new ObjectId($context->exportObject()->getId());
92 foreach ($file_identifiers as $file_identifier) {
93 $file = explode(":", trim($file_identifier->getIdentifier()));
94 $export_dir = $this->getDirectory($object_id, $context->exportObject()->getType());
95 $file[1] = basename($file[1]);
97 $export_dir . "/" . $file[1],
98 $file[1]
99 );
100 }
101 }
102
103 public function onDownloadWithLink(
104 ReferenceId $reference_id,
105 ilExportHandlerConsumerFileIdentifierInterface $file_identifier
106 ): void {
107 $object_id = $reference_id->toObjectId();
108 $type = ilObject::_lookupType($object_id->toInt());
109 $file = explode(":", trim($file_identifier->getIdentifier()));
110 $export_dir = $this->getDirectory($object_id, $type);
111 $file[1] = basename($file[1]);
113 $export_dir . "/" . $file[1],
114 $file[1]
115 );
116 }
117
118 public function getFiles(
119 ilExportHandlerConsumerContextInterface $context
120 ): ilExportHandlerFileInfoCollectionInterface {
121 $collection_builder = $context->fileCollectionBuilder();
122 $object_id = new ObjectId($context->exportObject()->getId());
123 $dir = $this->getDirectory($object_id, $context->exportObject()->getType());
124 $file_infos = $this->getExportFiles($dir);
125 foreach ($file_infos as $file_name => $file_info) {
126 $collection_builder = $collection_builder->withSPLFileInfo(
127 new SplFileInfo($dir . DIRECTORY_SEPARATOR . $file_info["file"]),
128 $object_id,
129 $this
130 );
131 }
132 return $collection_builder->collection();
133 }
134
135 public function onExportOptionSelected(
136 ilExportHandlerConsumerContextInterface $context
137 ): void {
138 $context->exportGUIObject()->createTestArchiveExport();
139 }
140
141 protected function getExportFiles(
142 string $directory
143 ): array {
144 $files = [];
145 try {
146 foreach (scandir($directory) as $file) {
147 if (
148 in_array($file, ['.', '..']) ||
149 !str_ends_with($file, ".zip")
150 ) {
151 continue;
152 }
153 $files[$file] = ["file" => $file];
154 }
155 } catch (Exception $e) {
156
157 }
158 return $files;
159 }
160
161 protected function getDirectory(
162 ObjectId $object_id,
163 string $export_object_type
164 ): string {
165 return $this->data_dir
166 . DIRECTORY_SEPARATOR . CLIENT_ID . DIRECTORY_SEPARATOR . 'tst_data' . DIRECTORY_SEPARATOR
167 . ilTestArchiver::EXPORT_DIRECTORY . DIRECTORY_SEPARATOR . $export_object_type . "_" . $object_id->toInt();
168 }
169}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
static deliverFileLegacy(string $a_file, ?string $a_filename=null, ?string $a_mime=null, ?bool $isInline=false, ?bool $removeAfterDelivery=false, ?bool $a_exit_after=true)
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
language handling
static _lookupType(int $id, bool $reference=false)
onDownloadFiles(ilExportHandlerConsumerContextInterface $context, ilExportHandlerConsumerFileIdentifierCollectionInterface $file_identifiers)
onExportOptionSelected(ilExportHandlerConsumerContextInterface $context)
onDeleteFiles(ilExportHandlerConsumerContextInterface $context, ilExportHandlerConsumerFileIdentifierCollectionInterface $file_identifiers)
getDirectory(ObjectId $object_id, string $export_object_type)
onDownloadWithLink(ReferenceId $reference_id, ilExportHandlerConsumerFileIdentifierInterface $file_identifier)
getFiles(ilExportHandlerConsumerContextInterface $context)
const CLIENT_ID
Definition: constants.php:41
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26
$context
Definition: webdav.php:31