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