ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMemberExportGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
23 
29 {
30  private int $ref_id;
31  private int $obj_id;
32  private string $type;
33 
35  protected Factory $refinery;
36 
37  protected ilCtrl $ctrl;
39  protected ilLanguage $lng;
41 
42  protected ?ilMemberExport $export = null;
43  protected ?ilExportFieldsInfo $fields_info = null;
46 
52  public function __construct(int $a_ref_id)
53  {
54  global $DIC;
55 
56  $this->http = $DIC->http();
57  $this->refinery = $DIC->refinery();
58 
59  $this->ctrl = $DIC->ctrl();
60  $this->tpl = $DIC->ui()->mainTemplate();
61  $this->toolbar = $DIC->toolbar();
62  $this->lng = $DIC->language();
63  $this->lng->loadLanguageModule('ps');
64  $this->ref_id = $a_ref_id;
65  $this->obj_id = ilObject::_lookupObjId($this->ref_id);
66  $this->type = ilObject::_lookupType($this->obj_id);
67 
68  $this->fields_info = ilExportFieldsInfo::_getInstanceByType(ilObject::_lookupType($this->obj_id));
69  $this->initFileSystemStorage();
70  }
71 
72  public function executeCommand(): void
73  {
74  if (!ilPrivacySettings::getInstance()->checkExportAccess($this->ref_id)) {
75  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'), true);
76  $this->ctrl->returnToParent($this);
77  }
78 
79  $next_class = $this->ctrl->getNextClass($this);
80  $cmd = $this->ctrl->getCmd();
81 
82  switch ($next_class) {
83  default:
84  if (!$cmd) {
85  $cmd = 'show';
86  }
87  $this->$cmd();
88  break;
89  }
90  }
91 
92  protected function initSettingsForm(bool $a_is_excel = false): ilPropertyFormGUI
93  {
94  // Check user selection
95  $this->exportSettings = new ilUserFormSettings('memexp');
96 
97  $form = new ilPropertyFormGUI();
98  $form->setFormAction($this->ctrl->getFormAction($this));
99  $form->setTitle($this->lng->txt('ps_export_settings'));
100 
101  if ($a_is_excel) {
102  $form->addCommandButton('exportExcel', $this->lng->txt('ps_export_excel'));
103  } else {
104  $form->addCommandButton('export', $this->lng->txt('ps_perform_export'));
105  }
106  $form->addCommandButton('show', $this->lng->txt('cancel'));
107 
108  // roles
109  $roles = new ilCheckboxGroupInputGUI($this->lng->txt('ps_user_selection'), 'export_members');
110  $roles->addOption(new ilCheckboxOption($this->lng->txt('ps_export_admin'), 'admin'));
111  if ($this->type === 'crs') {
112  $roles->addOption(new ilCheckboxOption($this->lng->txt('ps_export_tutor'), 'tutor'));
113  }
114  $roles->addOption(new ilCheckboxOption($this->lng->txt('ps_export_member'), 'member'));
115  $roles->addOption(new ilCheckboxOption($this->lng->txt('ps_export_sub'), 'subscribers'));
116  $roles->addOption(new ilCheckboxOption($this->lng->txt('ps_export_wait'), 'waiting_list'));
117  $form->addItem($roles);
118  $current_roles = array();
119  foreach (array('admin', 'tutor', 'member', 'subscribers', 'waiting_list') as $role) {
120  if ($this->exportSettings->enabled($role)) {
121  $current_roles[] = $role;
122  }
123  }
124  $roles->setValue($current_roles);
125 
126  // user data
127  $current_udata = array();
128  $udata = new ilCheckboxGroupInputGUI($this->lng->txt('ps_export_user_data'), 'export_members');
129  $form->addItem($udata);
130 
131  // standard fields
132  $this->fields_info->sortExportFields();
133  foreach ($this->fields_info->getFieldsInfo() as $field => $exportable) {
134  if (!$exportable) {
135  continue;
136  }
137  $udata->addOption(new ilCheckboxOption($this->lng->txt($field), $field));
138  if ($this->exportSettings->enabled($field)) {
139  $current_udata[] = $field;
140  }
141  }
142 
143  // udf
144  foreach (ilUserDefinedFields::_getInstance()->getExportableFields($this->obj_id) as $field_id => $udf_data) {
145  $field = 'udf_' . $field_id;
146  $udata->addOption(new ilCheckboxOption($udf_data['field_name'], $field));
147  if ($this->exportSettings->enabled($field)) {
148  $current_udata[] = $field;
149  }
150  }
151 
152  $udata->setValue($current_udata);
153 
154  // course custom data
155  $cdf_fields = ilCourseDefinedFieldDefinition::_getFields($this->obj_id);
156  if (count($cdf_fields)) {
157  $cdf = new ilCheckboxGroupInputGUI($this->lng->txt('ps_' . $this->type . '_user_fields'), 'export_members');
158  $form->addItem($cdf);
159 
160  $current_cdf = array();
161  foreach ($cdf_fields as $field_obj) {
162  $field = 'cdf_' . $field_obj->getId();
163  $cdf->addOption(new ilCheckboxOption($field_obj->getName(), $field));
164  if ($this->exportSettings->enabled($field)) {
165  $current_cdf[] = $field;
166  }
167  }
168 
169  $cdf->setValue($current_cdf);
170  }
171 
172  // consultation hours
173  if (ilBookingEntry::hasObjectBookingEntries($this->obj_id, $GLOBALS['DIC']['ilUser']->getId())) {
174  $this->lng->loadLanguageModule('dateplaner');
175  $chours = new ilCheckboxInputGUI($this->lng->txt('cal_ch_field_ch'), 'export_members[]');
176  $chours->setValue('consultation_hour');
177  $chours->setChecked($this->exportSettings->enabled('consultation_hour'));
178  $form->addItem($chours);
179  }
180 
181  $grp_membr = new ilCheckboxInputGUI($this->lng->txt('crs_members_groups'), 'export_members[]');
182  $grp_membr->setValue('group_memberships');
183  $grp_membr->setChecked($this->exportSettings->enabled('group_memberships'));
184  $form->addItem($grp_membr);
185  return $form;
186  }
187 
188  public function initCSV(ilPropertyFormGUI $a_form = null): void
189  {
190  if (!$a_form) {
191  $a_form = $this->initSettingsForm();
192  }
193  $this->tpl->setContent($a_form->getHTML());
194  }
195 
196  public function initExcel(ilPropertyFormGUI $a_form = null): void
197  {
198  if (!$a_form) {
199  $a_form = $this->initSettingsForm(true);
200  }
201  $this->tpl->setContent($a_form->getHTML());
202  }
203 
204  public function show(): void
205  {
206  $this->toolbar->addButton(
207  $this->lng->txt('ps_perform_export'),
208  $this->ctrl->getLinkTarget($this, "initCSV")
209  );
210  $this->toolbar->addButton(
211  $this->lng->txt('ps_export_excel'),
212  $this->ctrl->getLinkTarget($this, "initExcel")
213  );
214 
215  $this->showFileList();
216  }
217 
218  protected function handleIncoming(): void
219  {
220  $settings = [];
221  $incoming = [];
222  if ($this->http->wrapper()->post()->has('export_members')) {
223  $incoming = $this->http->wrapper()->post()->retrieve(
224  'export_members',
225  $this->refinery->kindlyTo()->dictOf(
226  $this->refinery->kindlyTo()->string()
227  )
228  );
229  }
230  if (count($incoming)) {
231  foreach ($incoming as $id) {
232  $settings[$id] = true;
233  }
234  }
235 
236  // Save (form) settings
237  $this->exportSettings = new ilUserFormSettings('memexp');
238  $this->exportSettings->set($settings);
239  $this->exportSettings->store();
240  }
241 
245  public function export(): void
246  {
247  $this->handleIncoming();
248 
249  $this->export = new ilMemberExport($this->ref_id);
250  $this->export->create();
251 
252  $filename = time() . '_participant_export_csv_' . $this->obj_id . '.csv';
253  $this->fss_export->addMemberExportFile($this->export->getCSVString(), $filename);
254  $this->ctrl->redirect($this, 'show');
255  }
256 
257  public function exportExcel(): void
258  {
259  $this->handleIncoming();
260 
261  $filename = time() . '_participant_export_xls_' . $this->obj_id;
262  $this->fss_export->initMemberExportDirectory();
263  $filepath = $this->fss_export->getMemberExportDirectory() . DIRECTORY_SEPARATOR . $filename;
264 
265  $this->export = new ilMemberExport($this->ref_id, ilMemberExport::EXPORT_EXCEL);
266  $this->export->setFilename($filepath);
267  $this->export->create();
268 
269  $this->ctrl->redirect($this, 'show');
270  }
271 
272  public function deliverData(): void
273  {
274  foreach ($this->fss_export->getMemberExportFiles() as $file) {
275  $member_export_filename = (string) ilSession::get('member_export_filename');
276  if ($file['name'] === $member_export_filename) {
277  $content = $this->fss_export->getMemberExportFile($member_export_filename);
279  $content,
280  date('Y_m_d_H-i', $file['timest']) .
281  '_member_export_' .
282  $this->obj_id .
283  '.csv',
284  'text/csv'
285  );
286  }
287  }
288  }
289 
293  public function showFileList(): void
294  {
295  $tbl = new ilMemberExportFileTableGUI($this, 'show', $this->fss_export);
296  $this->tpl->setContent($tbl->getHTML());
297  }
298 
302  public function downloadExportFile(): void
303  {
304  $fl = '';
305  if ($this->http->wrapper()->query()->has('fl')) {
306  $fl = $this->http->wrapper()->query()->retrieve(
307  'fl',
308  $this->refinery->kindlyTo()->string()
309  );
310  }
311 
312  $hash = trim($fl);
313  if (!$hash) {
314  $this->ctrl->redirect($this, 'show');
315  }
316 
317  foreach ($this->fss_export->getMemberExportFiles() as $file) {
318  if (md5($file['name']) === $hash) {
319  $contents = $this->fss_export->getMemberExportFile($file['timest'] . '_participant_export_' .
320  $file['type'] . '_' . $this->obj_id . '.' . $file['type']);
321 
322  // newer export files could be .xlsx
323  if ($file['type'] === 'xls' && !$contents) {
324  $contents = $this->fss_export->getMemberExportFile($file['timest'] . '_participant_export_' .
325  $file['type'] . '_' . $this->obj_id . '.xlsx');
326  $file['type'] = 'xlsx';
327  }
328 
329  switch ($file['type']) {
330  case 'xlsx':
332  $contents,
333  date('Y_m_d_H-i' . $file['timest']) . '_member_export_' . $this->obj_id . '.xlsx',
334  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
335  );
336 
337  // no break
338  case 'xls':
340  $contents,
341  date('Y_m_d_H-i' . $file['timest']) . '_member_export_' . $this->obj_id . '.xls',
342  'application/vnd.ms-excel'
343  );
344 
345  // no break
346  default:
347  case 'csv':
349  $contents,
350  date('Y_m_d_H-i' . $file['timest']) .
351  '_member_export_' .
352  $this->obj_id .
353  '.csv',
354  'text/csv'
355  );
356  break;
357  }
358  }
359  }
360  }
361 
365  protected function initFileIdsFromPost(): array
366  {
367  $ids = [];
368  if ($this->http->wrapper()->post()->has('id')) {
369  $ids = $this->http->wrapper()->post()->retrieve(
370  'id',
371  $this->refinery->kindlyTo()->listOf(
372  $this->refinery->kindlyTo()->string()
373  )
374  );
375  }
376  return $ids;
377  }
378 
382  public function confirmDeleteExportFile(): void
383  {
384  $file_ids = $this->initFileIdsFromPost();
385  if (!count($file_ids)) {
386  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('ps_select_one'), true);
387  $this->ctrl->redirect($this, 'show');
388  }
389  $confirmation_gui = new ilConfirmationGUI();
390  $confirmation_gui->setFormAction($this->ctrl->getFormAction($this));
391  $confirmation_gui->setHeaderText($this->lng->txt('info_delete_sure') /* .' '.$this->lng->txt('ps_delete_export_files') */);
392  $confirmation_gui->setCancel($this->lng->txt('cancel'), 'show');
393  $confirmation_gui->setConfirm($this->lng->txt('delete'), 'deleteExportFile');
394  foreach ($this->fss_export->getMemberExportFiles() as $file) {
395  if (!in_array(md5($file['name']), $file_ids)) {
396  continue;
397  }
398  $confirmation_gui->addItem(
399  "id[]",
400  md5($file['name']),
401  strtoupper($file['type']) . ' - ' .
403  );
404  }
405  $this->tpl->setContent($confirmation_gui->getHTML());
406  }
407 
411  public function deleteExportFile(): void
412  {
413  $file_ids = $this->initFileIdsFromPost();
414  if (!count($file_ids)) {
415  $this->ctrl->redirect($this, 'show');
416  }
417  foreach ($this->fss_export->getMemberExportFiles() as $file) {
418  if (!in_array(md5($file['name']), $file_ids)) {
419  continue;
420  }
421 
422  $path = $file['timest'] . '_participant_export_' .
423  $file['type'] . '_' . $this->obj_id . '.' . $file['type'];
424  if ($this->fss_export->hasMemberExportFile($path)) {
425  $this->fss_export->deleteMemberExportFile($path);
426  continue;
427  }
428 
429  if ($file['type'] !== "xls") {
430  continue;
431  }
432  //try xlsx if type is xls and file can't be found
433  $path = $file['timest'] . '_participant_export_xls_' . $this->obj_id . '.xlsx';
434  if ($this->fss_export->hasMemberExportFile($path)) {
435  $this->fss_export->deleteMemberExportFile($path);
436  }
437  }
438 
439  $this->tpl->setOnScreenMessage('success', $this->lng->txt('ps_files_deleted'), true);
440  $this->ctrl->redirect($this, 'show');
441  }
442 
443  protected function initFileSystemStorage(): void
444  {
445  if ($this->type === 'crs') {
446  $this->fss_export = new ilFSStorageCourse($this->obj_id);
447  }
448  if ($this->type === 'grp') {
449  $this->fss_export = new ilFSStorageGroup($this->obj_id);
450  }
451  }
452 }
Interface GlobalHttpState.
static get(string $a_var)
Class for generation of member export files.
ilFileSystemAbstractionStorage $fss_export
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
downloadExportFile()
Download export file.
__construct(int $a_ref_id)
Constructor public.
deleteExportFile()
Delete member export files.
showFileList()
Show file list of available export files.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static hasObjectBookingEntries(int $a_obj_id, int $a_usr_id)
Check if object has assigned consultation hour appointments.
export()
Export, create member export file and store it in data directory.
initExcel(ilPropertyFormGUI $a_form=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilUserFormSettings $exportSettings
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
This class represents a checkbox property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static deliverData(string $a_data, string $a_filename, string $mime="application/octet-stream")
static _getFields(int $a_container_id, $a_sort=self::IL_CDF_SORT_NAME)
Get all fields of a container.
const IL_CAL_UNIX
$path
Definition: ltiservices.php:32
static _lookupObjId(int $ref_id)
global $DIC
Definition: feed.php:28
ilExportFieldsInfo $fields_info
static http()
Fetches the global http state from ILIAS.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
initSettingsForm(bool $a_is_excel=false)
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$filename
Definition: buildRTE.php:78
confirmDeleteExportFile()
Confirm deletion of export files.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getInstanceByType(string $a_type)
Get Singleton Instance.
initCSV(ilPropertyFormGUI $a_form=null)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static _lookupType(int $id, bool $reference=false)
ilGlobalTemplateInterface $tpl
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...