ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilTestExportPlugin.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 abstract class ilTestExportPlugin extends ilPlugin
28 {
29  protected ilObjTest $test;
30  protected int $timestmap = -1;
31 
35  protected static $reserved_formats = array(
36  'xml',
37  'csv'
38  );
39  private \ilGlobalTemplateInterface $main_tpl;
40 
41  public function __construct(
44  string $id
45  ) {
46  parent::__construct($db, $component_repository, $id);
47  }
48 
49  final public function setTest(ilObjTest $test): void
50  {
51  $this->test = $test;
52  }
53 
54  final protected function getTest(): ilObjTest
55  {
56  return $this->test;
57  }
58 
59  public function setTimestmap(int $timestmap): void
60  {
61  $this->timestmap = $timestmap;
62  }
63 
64  public function getTimestmap(): int
65  {
66  return $this->timestmap;
67  }
68 
72  final public function getFormat(): string
73  {
74  $format_id = $this->getFormatIdentifier();
75 
76  if (!is_string($format_id)) {
77  throw new ilException('The format must be of type string.');
78  }
79 
80  if (!strlen($format_id)) {
81  throw new ilException('The format is empty.');
82  }
83 
84  if (strtolower($format_id) != $format_id) {
85  throw new ilException('Please use a lowercase format.');
86  }
87 
88  if (in_array($format_id, self::$reserved_formats)) {
89  throw new ilException('The format must not be one of: ' . implode(', ', self::$reserved_formats));
90  }
91 
92  return $format_id;
93  }
94 
98  final public function export(): void
99  {
101  global $DIC;
102  // these dependencies need to be fetched here because they are not available in constructor during ILIAS setup
103  $tpl = $DIC->ui()->mainTemplate();
104  $ctrl = $DIC->ctrl();
105  $lng = $DIC->language();
106 
107  if (!$this->getTest() instanceof ilObjTest) {
108  throw new ilException('Incomplete object configuration. Please pass an instance of ilObjTest before calling the export!');
109  }
110 
111  try {
112  $this->buildExportFile(new ilTestExportFilename($this->getTest()));
113  } catch (ilException $e) {
114  if ($this->txt($e->getMessage()) == '-' . $e->getMessage() . '-') {
115  $tpl->setOnScreenMessage('failure', $e->getMessage(), true);
116  } else {
117  $tpl->setOnScreenMessage('failure', $this->txt($e->getMessage()), true);
118  }
119  $ctrl->redirectByClass('iltestexportgui');
120  }
121 
122  $tpl->setOnScreenMessage('success', $lng->txt('exp_file_created'), true);
123  $ctrl->redirectByClass('iltestexportgui');
124  }
125 
132  abstract protected function buildExportFile(ilTestExportFilename $export_path): void;
133 
141  abstract protected function getFormatIdentifier(): string;
142 
147  abstract public function getFormatLabel(): string;
148 }
string $id
Abstract parent class for all event hook 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
global $DIC
Definition: feed.php:28
__construct(VocabulariesInterface $vocabularies)
$lng
ilGlobalTemplateInterface $main_tpl
txt(string $a_var)
Get Language Variable (prefix will be prepended automatically)
buildExportFile(ilTestExportFilename $export_path)
This method is called if the user wants to export a test of YOUR export type If you throw an exceptio...
ilDBInterface $db