ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilTestExportGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
36 {
37  public function __construct(
39  private ilDBInterface $db,
40  private ilLogger $logger,
41  private ilObjectDataCache $obj_cache,
42  private ilComponentRepository $component_repository,
43  Generator $active_export_plugins,
44  private ilTestHTMLGenerator $html_generator,
45  private array $selected_files,
46  private QuestionInfoService $questioninfo,
47  ) {
48  parent::__construct($parent_gui, null);
49 
50  $this->addFormat('xml', $this->lng->txt('ass_create_export_file'));
51  $this->addFormat('xmlres', $this->lng->txt('ass_create_export_file_with_results'), $this, 'createTestExportWithResults');
52  $this->addFormat('csv', $this->lng->txt('ass_create_export_test_results'), $this, 'createTestResultsExport');
53  $this->addFormat('arc', $this->lng->txt('ass_create_export_test_archive'), $this, 'createTestArchiveExport');
54  foreach ($active_export_plugins as $plugin) {
55  $plugin->setTest($this->obj);
56  $this->addFormat(
57  $plugin->getFormat(),
58  $plugin->getFormatLabel(),
59  $plugin,
60  'export'
61  );
62  }
63  }
64 
69  {
70  $table = new ilTestExportTableGUI($this, 'listExportFiles', $this->obj);
71  return $table;
72  }
73 
77  public function createTestExportWithResults()
78  {
79  if (!$this->access->checkAccess('write', '', $this->obj->getRefId())) {
80  $this->tpl->setOnScreenMessage('info', 'cannot_export_archive', true);
81  $this->ctrl->redirectByClass(self::class);
82  }
83 
84  $this->ctrl->setParameterByClass(self::class, 'export_results', 1);
85  (new ilExport())->exportObject($this->obj->getType(), $this->obj->getId());
86  $this->tpl->setOnScreenMessage('success', $this->lng->txt('exp_file_created'), true);
87  $this->ctrl->redirectByClass('iltestexportgui');
88  }
89 
90  public function createTestResultsExport()
91  {
92  if (!$this->access->checkAccess('write', '', $this->obj->getRefId())) {
93  $this->tpl->setOnScreenMessage('info', 'cannot_export_archive', true);
94  $this->ctrl->redirectByClass(self::class);
95  }
96 
97  $export_factory = new ilTestExportFactory(
98  $this->obj,
99  $this->lng,
100  $this->logger,
101  $this->tree,
102  $this->component_repository,
103  $this->questioninfo
104  );
105  $test_exp = $export_factory->getExporter('results');
106  $test_exp->buildExportFile();
107  $this->tpl->setOnScreenMessage('success', $this->lng->txt('exp_file_created'), true);
108  $this->ctrl->redirectByClass('iltestexportgui');
109  }
110 
111  public function createTestArchiveExport()
112  {
113  if ($this->access->checkAccess('write', '', $this->obj->getRefId())) {
114  // prepare generation before contents are processed (for mathjax)
115 
116  $evaluation = new ilTestEvaluation($this->db, $this->obj->getTestId());
117  $allActivesPasses = $evaluation->getAllActivesPasses();
118  $participantData = new ilTestParticipantData($this->db, $this->lng);
119  $participantData->setActiveIdsFilter(array_keys($allActivesPasses));
120  $participantData->load($this->obj->getTestId());
121 
122  $archiveService = new ilTestArchiveService(
123  $this->obj,
124  $this->lng,
125  $this->obj_cache,
126  $this->html_generator
127  );
128  $archiveService->setParticipantData($participantData);
129  $archiveService->archivePassesByActives($allActivesPasses);
130 
131  $test_id = $this->obj->getId();
132  $test_ref = $this->obj->getRefId();
133  $archive_exp = new ilTestArchiver($test_id, $test_ref);
134 
135  $scoring = new ilTestScoring($this->obj, $this->db);
136  $best_solution = $scoring->calculateBestSolutionForTest();
137 
138  $tmpFileName = ilFileUtils::ilTempnam();
139  if (!is_dir($tmpFileName)) {
140  ilFileUtils::makeDirParents($tmpFileName);
141  }
142 
143  $archive_exp->handInTestBestSolution($best_solution);
144 
145  $archive_exp->updateTestArchive();
146  $archive_exp->compressTestArchive();
147  } else {
148  $this->tpl->setOnScreenMessage('info', 'cannot_export_archive', true);
149  }
150  $this->ctrl->redirectByClass('iltestexportgui');
151  }
152 
153  public function listExportFiles(): void
154  {
155  $this->toolbar->setFormAction($this->ctrl->getFormAction($this));
156 
157  if (count($this->getFormats()) > 1) {
158  foreach ($this->getFormats() as $f) {
159  $options[$f['key']] = $f['txt'];
160  }
161  $si = new ilSelectInputGUI($this->lng->txt('type'), 'format');
162  $si->setOptions($options);
163  $this->toolbar->addInputItem($si, true);
164  $this->toolbar->addFormButton($this->lng->txt('exp_create_file'), 'createExportFile');
165  } else {
166  $format = $this->getFormats()[0];
167  $this->toolbar->addFormButton(
168  $this->lng->txt('exp_create_file')
169  . ' (' . $format['txt'] . ')',
170  'create_' . $format['key']
171  );
172  }
173 
174  $archiver = new ilTestArchiver($this->getParentGUI()->getTestObject()->getId());
175  $archive_dir = $archiver->getZipExportDirectory();
176  $archive_files = [];
177 
178  if (file_exists($archive_dir) && is_dir($archive_dir)) {
179  $archive_files = scandir($archive_dir);
180  }
181 
182  $export_dir = $this->obj->getExportDirectory();
183  $export_files = $this->obj->getExportFiles($export_dir);
184  $data = [];
185  if (count($export_files) > 0) {
186  foreach ($export_files as $exp_file) {
187  $file_arr = explode('__', $exp_file);
188  if ($file_arr[0] == $exp_file) {
189  continue;
190  }
191 
192  array_push(
193  $data,
194  [
195  'file' => $exp_file,
196  'size' => filesize($export_dir . '/' . $exp_file),
197  'timestamp' => $file_arr[0],
198  'type' => $this->getExportTypeFromFileName($exp_file)
199  ]
200  );
201  }
202  }
203 
204  if (count($archive_files) > 0) {
205  foreach ($archive_files as $exp_file) {
206  if ($exp_file == '.' || $exp_file == '..') {
207  continue;
208  }
209  $file_arr = explode('_', $exp_file);
210 
211  $data[] = [
212  'file' => $exp_file,
213  'size' => filesize($archive_dir . '/' . $exp_file),
214  'timestamp' => $file_arr[4],
215  'type' => $this->getExportTypeFromFileName($exp_file)
216  ];
217  }
218  }
219 
220  $table = $this->buildExportTableGUI();
221  $table->setSelectAllCheckbox('file');
222  foreach ($this->getCustomColumns() as $c) {
223  $table->addCustomColumn($c['txt'], $c['obj'], $c['func']);
224  }
225 
226  foreach ($this->getCustomMultiCommands() as $c) {
227  $table->addCustomMultiCommand($c['txt'], 'multi_' . $c['func']);
228  }
229 
230  $table->resetFormats();
231  foreach ($this->formats as $format) {
232  $table->addFormat($format['key']);
233  }
234 
235  $table->setData($data);
236  $this->tpl->setOnScreenMessage('info', $this->lng->txt('no_manual_feedback_export_info'), true);
237  $this->tpl->setContent($table->getHTML());
238  }
239 
240  private function getExportTypeFromFileName(string $export_file)
241  {
242  $extension = strtoupper(pathinfo($export_file, PATHINFO_EXTENSION));
243  if (in_array($extension, ['XLSX', 'CSV', 'XLS'])) {
244  return $this->lng->txt('results');
245  }
246  return $extension;
247  }
248 
249  public function download(): void
250  {
251  if ($this->selected_files === []) {
252  $this->tpl->setOnScreenMessage('info', $this->lng->txt('no_checkbox'), true);
253  $this->ctrl->redirect($this, 'listExportFiles');
254  }
255 
256  if (count($this->selected_files) > 1) {
257  $this->tpl->setOnScreenMessage('info', $this->lng->txt('select_max_one_item'), true);
258  $this->ctrl->redirect($this, 'listExportFiles');
259  }
260 
261  $archiver = new ilTestArchiver($this->getParentGUI()->getTestObject()->getId());
262 
263  $filename = basename($this->selected_files[0]);
264  $exportFile = $this->obj->getExportDirectory() . '/' . $filename;
265  $archiveFile = $archiver->getZipExportDirectory() . '/' . $filename;
266 
267  if (file_exists($exportFile)) {
269  }
270 
271  if (file_exists($archiveFile)) {
273  }
274 
275  $this->ctrl->redirect($this, 'listExportFiles');
276  }
277 
278  public function delete(): void
279  {
280  $archiver = new ilTestArchiver($this->getParentGUI()->getTestObject()->getId());
281  $archiveDir = $archiver->getZipExportDirectory();
282 
283  $export_dir = $this->obj->getExportDirectory();
284  foreach ($this->selected_files as $file) {
285  $file = basename($file);
286  $dir = substr($file, 0, strlen($file) - 4);
287 
288  if (!strlen($file) || !strlen($dir)) {
289  continue;
290  }
291 
292  $exp_file = $export_dir . '/' . $file;
293  $arc_file = $archiveDir . '/' . $file;
294  $exp_dir = $export_dir . '/' . $dir;
295  if (@is_file($exp_file)) {
296  unlink($exp_file);
297  }
298  if (@is_file($arc_file)) {
299  unlink($arc_file);
300  }
301  if (@is_dir($exp_dir)) {
302  ilFileUtils::delDir($exp_dir);
303  }
304  }
305  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_deleted_export_files'), true);
306  $this->ctrl->redirect($this, 'listExportFiles');
307  }
308 }
Readable part of repository interface to ilComponentDataDB.
Export User Interface Class.
Class ilObjTestGUI.
This class represents a selection list property in a property form.
setOptions(array $a_options)
Export.
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static deliverFileLegacy(string $a_file, ?string $a_filename=null, ?string $a_mime=null, ?bool $isInline=false, ?bool $removeAfterDelivery=false, ?bool $a_exit_after=true)
getExportTypeFromFileName(string $export_file)
Export User Interface Class.
__construct(VocabulariesInterface $vocabularies)
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
addFormat(string $a_key, string $a_txt="", object $a_call_obj=null, string $a_call_func="")
Class ilTestScoring.
__construct(ilObjTestGUI $parent_gui, private ilDBInterface $db, private ilLogger $logger, private ilObjectDataCache $obj_cache, private ilComponentRepository $component_repository, Generator $active_export_plugins, private ilTestHTMLGenerator $html_generator, private array $selected_files, private QuestionInfoService $questioninfo,)
createTestExportWithResults()
Create test export file.
$filename
Definition: buildRTE.php:78
Class that handles PDF generation for test and assessment.
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
Class ilTestArchiver.