ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilTestExportPlugin.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
28 abstract 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  ) {
43  parent::__construct($db, $component_repository, $id);
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  $this->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  {
117  return $this->createExportFile();
118  }
119 
120  private function createExportFile(ilGlobalTemplateInterface $main_tpl): ?string
121  {
122  if ($this->getTest() === null) {
123  throw new ilException('Incomplete object configuration. Please pass an instance of ilObjTest before calling the export!');
124  }
125 
126  try {
127  $export_filename = new ExportFilename($this->getTest()->getId());
128  $this->buildExportFile($export_filename);
129  return $export_filename->getPathname();
130  } catch (ilException $e) {
131  if ($this->txt($e->getMessage()) == '-' . $e->getMessage() . '-') {
132  $main_tpl->setOnScreenMessage('failure', $e->getMessage(), true);
133  } else {
134  $main_tpl->setOnScreenMessage('failure', $this->txt($e->getMessage()), true);
135  }
136  return null;
137  }
138  }
139 
146  abstract protected function buildExportFile(ExportFilename $export_path): void;
147 
155  abstract protected function getFormatIdentifier(): string;
156 
161  abstract public function getFormatLabel(): string;
162 }
setOnScreenMessage(string $type, string $a_txt, bool $a_keep=false)
Set a message to be displayed to the user.
createExportFile(ilGlobalTemplateInterface $main_tpl)
string $id
Abstract parent class for all test export plugin classes.
Writeable part of repository interface to ilComponentDataDB.
getFormatLabel()
This method should return a human readable label for your export.
__construct(\ilDBInterface $db, \ilComponentRepositoryWrite $component_repository, string $id)
getFormatIdentifier()
A unique identifier which describes your export type, e.g.
ilComponentRepositoryWrite $component_repository
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:26
txt(string $a_var)
Get Language Variable (prefix will be prepended automatically)
ilDBInterface $db
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(Container $dic, ilPlugin $plugin)
$file_delivery
Definition: deliver.php:29