ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilTestExportPlugin.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
28abstract class ilTestExportPlugin extends ilPlugin implements Exporter
29{
30 protected ?ilObjTest $test = null;
31 protected int $timestmap = -1;
32
33 protected static $reserved_formats = [
34 'xml',
35 'csv'
36 ];
37
38 public function __construct(
41 string $id
42 ) {
44 }
45
46 final public function setTest(ilObjTest $test): void
47 {
48 $this->test = $test;
49 }
50
51 final protected function getTest(): ?ilObjTest
52 {
53 return $this->test;
54 }
55
56 public function setTimestmap(int $timestmap): void
57 {
58 $this->timestmap = $timestmap;
59 }
60
61 public function getTimestmap(): int
62 {
63 return $this->timestmap;
64 }
65
69 final public function getFormat(): string
70 {
71 $format_id = $this->getFormatIdentifier();
72
73 if (!is_string($format_id)) {
74 throw new ilException('The format must be of type string.');
75 }
76
77 if (!strlen($format_id)) {
78 throw new ilException('The format is empty.');
79 }
80
81 if (strtolower($format_id) != $format_id) {
82 throw new ilException('Please use a lowercase format.');
83 }
84
85 if (in_array($format_id, self::$reserved_formats)) {
86 throw new ilException('The format must not be one of: ' . implode(', ', self::$reserved_formats));
87 }
88
89 return $format_id;
90 }
91
95 final public function deliver(): void
96 {
98 global $DIC;
99 $main_tpl = $DIC['tpl'];
100 $file_delivery = $DIC['file_delivery'];
101
102 if (($file_name = $this->createExportFile($main_tpl)) === null) {
103 return;
104 }
105
106 $file_delivery->legacyDelivery()->attached(
107 $file_name,
108 null,
109 null,
110 true
111 );
112 $file_delivery->deliver();
113 }
114
115 final public function write(): ?string
116 {
118 global $DIC;
119 $main_tpl = $DIC['tpl'];
120
121 return $this->createExportFile($main_tpl);
122 }
123
124 private function createExportFile(ilGlobalTemplateInterface $main_tpl): ?string
125 {
126 if ($this->getTest() === null) {
127 throw new ilException('Incomplete object configuration. Please pass an instance of ilObjTest before calling the export!');
128 }
129
130 try {
131 $export_filename = new ExportFilename($this->getTest()->getId());
132 $this->buildExportFile($export_filename);
133 return $export_filename->getPathname();
134 } catch (ilException $e) {
135 if ($this->txt($e->getMessage()) == '-' . $e->getMessage() . '-') {
136 $main_tpl->setOnScreenMessage('failure', $e->getMessage(), true);
137 } else {
138 $main_tpl->setOnScreenMessage('failure', $this->txt($e->getMessage()), true);
139 }
140 return null;
141 }
142 }
143
150 abstract protected function buildExportFile(ExportFilename $export_path): void;
151
159 abstract protected function getFormatIdentifier(): string;
160
165 abstract public function getFormatLabel(): string;
166}
Base class for ILIAS Exception handling.
ilDBInterface $db
txt(string $a_var)
Get Language Variable (prefix will be prepended automatically)
string $id
ilComponentRepositoryWrite $component_repository
Abstract parent class for all test export plugin classes.
createExportFile(ilGlobalTemplateInterface $main_tpl)
buildExportFile(ExportFilename $export_path)
This method is called if the user wants to export a test of YOUR export type If you throw an exceptio...
__construct(\ilDBInterface $db, \ilComponentRepositoryWrite $component_repository, string $id)
getFormatLabel()
This method should return a human readable label for your export.
getFormatIdentifier()
A unique identifier which describes your export type, e.g.
$file_delivery
Definition: deliver.php:29
setOnScreenMessage(string $type, string $a_txt, bool $a_keep=false)
Set a message to be displayed to the user.
Writeable part of repository interface to ilComponentDataDB.
Interface ilDBInterface.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26