ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilTestExportPlugin.php
Go to the documentation of this file.
1 <?php
2 
25 abstract class ilTestExportPlugin extends ilPlugin
26 {
30  protected $test;
31 
35  protected $timestmap = -1;
36 
40  protected static $reserved_formats = array(
41  'xml',
42  'csv'
43  );
44 
45  public function __construct(
48  string $id
49  ) {
50  parent::__construct($db, $component_repository, $id);
51  }
52 
56  final public function setTest($test)
57  {
58  $this->test = $test;
59  }
60 
64  final protected function getTest(): ilObjTest
65  {
66  return $this->test;
67  }
68 
72  public function setTimestmap($timestmap)
73  {
74  $this->timestmap = $timestmap;
75  }
76 
80  public function getTimestmap(): int
81  {
82  return $this->timestmap;
83  }
84 
89  final public function getFormat(): string
90  {
91  $format_id = $this->getFormatIdentifier();
92 
93  if (!is_string($format_id)) {
94  throw new ilException('The format must be of type string.');
95  }
96 
97  if (!strlen($format_id)) {
98  throw new ilException('The format is empty.');
99  }
100 
101  if (strtolower($format_id) != $format_id) {
102  throw new ilException('Please use a lowercase format.');
103  }
104 
105  if (in_array($format_id, self::$reserved_formats)) {
106  throw new ilException('The format must not be one of: ' . implode(', ', self::$reserved_formats));
107  }
108 
109  return $format_id;
110  }
111 
115  final public function export()
116  {
118  global $DIC;
119  $lng = $DIC['lng'];
120  $ilCtrl = $DIC['ilCtrl'];
121  $main_tpl = $DIC['tpl'];
122 
123  if (!$this->getTest() instanceof ilObjTest) {
124  throw new ilException('Incomplete object configuration. Please pass an instance of ilObjTest before calling the export!');
125  }
126 
127  try {
128  $this->buildExportFile(new ilTestExportFilename($this->getTest()));
129  } catch (ilException $e) {
130  if ($this->txt($e->getMessage()) == '-' . $e->getMessage() . '-') {
131  $main_tpl->setOnScreenMessage('failure', $e->getMessage(), true);
132  } else {
133  $main_tpl->setOnScreenMessage('failure', $this->txt($e->getMessage()), true);
134  }
135  $ilCtrl->redirectByClass('iltestexportgui');
136  }
137 
138  $main_tpl->setOnScreenMessage('success', $lng->txt('exp_file_created'), true);
139  $ilCtrl->redirectByClass('iltestexportgui');
140  }
141 
148  abstract protected function buildExportFile(ilTestExportFilename $export_path);
149 
157  abstract protected function getFormatIdentifier(): string;
158 
163  abstract public function getFormatLabel(): string;
164 }
string $id
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Writeable part of repository interface to ilComponentDataDB.
$lng
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
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
__construct(Container $dic, ilPlugin $plugin)