ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilMemberExportGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once('Services/PrivacySecurity/classes/class.ilExportFieldsInfo.php');
25 include_once('./Services/Membership/classes/Export/class.ilMemberExport.php');
26 include_once('Modules/Course/classes/class.ilFSStorageCourse.php');
27 include_once('Modules/Group/classes/class.ilFSStorageGroup.php');
28 include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
29 include_once('Services/User/classes/class.ilUserDefinedFields.php');
30 include_once('Services/User/classes/class.ilUserFormSettings.php');
31 
42 {
43  private $ref_id;
44  private $obj_id;
45  private $type;
46  private $ctrl;
47  private $tpl;
48  private $lng;
49 
50  private $fields_info;
51  private $fss_export = null;
55  private $exportSettings;
56 
64  public function __construct($a_ref_id)
65  {
66  global $ilCtrl,$tpl,$lng,$ilUser,$ilObjDataCache;
67 
68  $this->ctrl = $ilCtrl;
69  $this->tpl = $tpl;
70  $this->lng = $lng;
71  $this->lng->loadLanguageModule('ps');
72  $this->ref_id = $a_ref_id;
73  $this->obj_id = $ilObjDataCache->lookupObjId($this->ref_id);
74  $this->type = ilObject::_lookupType($this->obj_id);
75 
76  $this->fields_info = ilExportFieldsInfo::_getInstanceByType(ilObject::_lookupType($this->obj_id));
77  $this->initFileSystemStorage();
78  }
79 
87  public function executeCommand()
88  {
89  global $ilAccess,$rbacsystem;
90 
91  include_once('Services/PrivacySecurity/classes/class.ilPrivacySettings.php');
92  if(!ilPrivacySettings::_getInstance()->checkExportAccess($this->ref_id))
93  {
94  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
95  $this->ctrl->returnToParent($this);
96  }
97 
98  $next_class = $this->ctrl->getNextClass($this);
99  $cmd = $this->ctrl->getCmd();
100 
101  switch($next_class)
102  {
103  default:
104  if(!$cmd)
105  {
106  $cmd = 'show';
107  }
108  $this->$cmd();
109  break;
110  }
111  }
112 
113 
114  //
115  // export incl. settings
116  //
117 
118  protected function initSettingsForm($a_is_excel = false)
119  {
120  // Check user selection
121  $this->exportSettings = new ilUserFormSettings('memexp');
122 
123  include_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
124  $form = new ilPropertyFormGUI();
125  $form->setFormAction($this->ctrl->getFormAction($this));
126  $form->setTitle($this->lng->txt('ps_export_settings'));
127 
128  if((bool)$a_is_excel)
129  {
130  $form->addCommandButton('exportExcel', $this->lng->txt('ps_export_excel'));
131  }
132  else
133  {
134  $form->addCommandButton('export', $this->lng->txt('ps_perform_export'));
135  }
136  $form->addCommandButton('show', $this->lng->txt('cancel'));
137 
138  // roles
139  $roles = new ilCheckboxGroupInputGUI($this->lng->txt('ps_user_selection'), 'export_members');
140  $roles->addOption(new ilCheckboxOption($this->lng->txt('ps_export_admin'), 'admin'));
141  if($this->type == 'crs')
142  {
143  $roles->addOption(new ilCheckboxOption($this->lng->txt('ps_export_tutor'), 'tutor'));
144  }
145  $roles->addOption(new ilCheckboxOption($this->lng->txt('ps_export_member'), 'member'));
146  $roles->addOption(new ilCheckboxOption($this->lng->txt('ps_export_sub'), 'subscribers'));
147  $roles->addOption(new ilCheckboxOption($this->lng->txt('ps_export_wait'), 'waiting_list'));
148  $form->addItem($roles);
149 
150  $current_roles = array();
151  foreach(array('admin', 'tutor', 'member', 'subscribers', 'waiting_list') as $role)
152  {
153  if($this->exportSettings->enabled($role))
154  {
155  $current_roles[] = $role;
156  }
157  }
158  $roles->setValue($current_roles);
159 
160  // user data
161  $current_udata = array();
162  $udata = new ilCheckboxGroupInputGUI($this->lng->txt('ps_export_user_data'), 'export_members');
163  $form->addItem($udata);
164 
165  // standard fields
166  $this->fields_info->sortExportFields();
167  foreach($this->fields_info->getFieldsInfo() as $field => $exportable)
168  {
169  if(!$exportable)
170  {
171  continue;
172  }
173  $udata->addOption(new ilCheckboxOption($this->lng->txt($field), $field));
174  if($this->exportSettings->enabled($field))
175  {
176  $current_udata[] = $field;
177  }
178  }
179 
180  // udf
181  foreach(ilUserDefinedFields::_getInstance()->getExportableFields($this->obj_id) as $field_id => $udf_data)
182  {
183  $field = 'udf_'.$field_id;
184  $udata->addOption(new ilCheckboxOption($udf_data['field_name'], $field));
185  if($this->exportSettings->enabled($field))
186  {
187  $current_udata[] = $field;
188  }
189  }
190 
191  $udata->setValue($current_udata);
192 
193  // course custom data
194  $cdf_fields = ilCourseDefinedFieldDefinition::_getFields($this->obj_id);
195  if(count($cdf_fields))
196  {
197  $cdf = new ilCheckboxGroupInputGUI($this->lng->txt('ps_'.$this->type.'_user_fields'), 'export_members');
198  $form->addItem($cdf);
199 
200  $current_cdf = array();
201  foreach($cdf_fields as $field_obj)
202  {
203  $field = 'cdf_'.$field_obj->getId();
204  $cdf->addOption(new ilCheckboxOption($field_obj->getName(), $field));
205  if($this->exportSettings->enabled($field))
206  {
207  $current_cdf[] = $field;
208  }
209  }
210 
211  $cdf->setValue($current_cdf);
212  }
213 
214  // consultation hours
215  include_once './Services/Booking/classes/class.ilBookingEntry.php';
216  if(ilBookingEntry::hasObjectBookingEntries($this->obj_id, $GLOBALS['ilUser']->getId()))
217  {
218  $this->lng->loadLanguageModule('dateplaner');
219  $chours = new ilCheckboxInputGUI($this->lng->txt('cal_ch_field_ch'), 'export_members[]');
220  $chours->setValue('consultation_hour');
221  $chours->setChecked($this->exportSettings->enabled('consultation_hour'));
222  $form->addItem($chours);
223  }
224 
225  $grp_membr = new ilCheckboxInputGUI($this->lng->txt('crs_members_groups'), 'export_members[]');
226  $grp_membr->setValue('group_memberships');
227  $grp_membr->setChecked($this->exportSettings->enabled('group_memberships'));
228  $form->addItem($grp_membr);
229  return $form;
230  }
231 
232  public function initCSV(ilPropertyFormGUI $a_form = null)
233  {
234  if(!$a_form)
235  {
236  $a_form = $this->initSettingsForm();
237  }
238  $this->tpl->setContent($a_form->getHTML());
239  }
240 
241  public function initExcel(ilPropertyFormGUI $a_form = null)
242  {
243  if(!$a_form)
244  {
245  $a_form = $this->initSettingsForm(true);
246  }
247  $this->tpl->setContent($a_form->getHTML());
248  }
249 
256  public function show()
257  {
258  global $ilToolbar;
259 
260  $ilToolbar->addButton($this->lng->txt('ps_perform_export'),
261  $this->ctrl->getLinkTarget($this, "initCSV"));
262  $ilToolbar->addButton($this->lng->txt('ps_export_excel'),
263  $this->ctrl->getLinkTarget($this, "initExcel"));
264 
265  $this->showFileList();
266  }
267 
268  protected function handleIncoming()
269  {
270  $settings = array();
271  $incoming = $_POST['export_members'];
272  if(is_array($incoming))
273  {
274  foreach($incoming as $id)
275  {
276  $settings[$id] = true;
277  }
278  }
279 
280  // Save (form) settings
281  $this->exportSettings = new ilUserFormSettings('memexp');
282  $this->exportSettings->set($settings);
283  $this->exportSettings->store();
284  }
285 
292  public function export()
293  {
294  $this->handleIncoming();
295 
296  $this->export = new ilMemberExport($this->ref_id);
297  $this->export->create();
298 
299  $filename = time().'_participant_export_csv_'.$this->obj_id.'.csv';
300  $this->fss_export->addMemberExportFile($this->export->getCSVString(),$filename);
301 
302  $this->ctrl->redirect($this, 'show');
303  }
304 
305  public function exportExcel()
306  {
307  $this->handleIncoming();
308 
309  $filename = time().'_participant_export_xls_'.$this->obj_id;
310  $this->fss_export->initMemberExportDirectory();
311  $filepath = $this->fss_export->getMemberExportDirectory().DIRECTORY_SEPARATOR.$filename;
312 
313  $this->export = new ilMemberExport($this->ref_id,ilMemberExport::EXPORT_EXCEL);
314  $this->export->setFilename($filepath);
315  $this->export->create();
316 
317  $this->ctrl->redirect($this, 'show');
318  }
319 
327  public function deliverData()
328  {
329  foreach($this->fss_export->getMemberExportFiles() as $file)
330  {
331  if($file['name'] == $_SESSION['member_export_filename'])
332  {
333  $content = $this->fss_export->getMemberExportFile($_SESSION['member_export_filename']);
334  ilUtil::deliverData($content,date('Y_m_d_H-i',$file['timest']).
335  '_member_export_'.
336  $this->obj_id.
337  '.csv','text/csv');
338  }
339  }
340  }
341 
348  public function showFileList()
349  {
350  include_once 'Services/Membership/classes/Export/class.ilMemberExportFileTableGUI.php';
351  $tbl = new ilMemberExportFileTableGUI($this, 'show', $this->fss_export);
352  $this->tpl->setContent($tbl->getHTML());
353  }
354 
362  public function downloadExportFile()
363  {
364  $hash = trim($_GET['fl']);
365  if(!$hash)
366  {
367  $this->ctrl->redirect($this, 'show');
368  }
369 
370  foreach($this->fss_export->getMemberExportFiles() as $file)
371  {
372  if(md5($file['name']) == $hash)
373  {
374  $contents = $this->fss_export->getMemberExportFile($file['timest'].'_participant_export_'.
375  $file['type'].'_'.$this->obj_id.'.'.$file['type']);
376 
377  // newer export files could be .xlsx
378  if($file['type'] == 'xls' && !$contents)
379  {
380  $contents = $this->fss_export->getMemberExportFile($file['timest'].'_participant_export_'.
381  $file['type'].'_'.$this->obj_id.'.xlsx');
382  $file['type'] = 'xlsx';
383  }
384 
385  switch($file['type'])
386  {
387  case 'xlsx':
389  $contents,
390  date('Y_m_d_H-i'.$file['timest']).'_member_export_'.$this->obj_id.'.xlsx',
391  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
392  );
393 
394  case 'xls':
396  $contents,
397  date('Y_m_d_H-i'.$file['timest']).'_member_export_'.$this->obj_id.'.xls',
398  'application/vnd.ms-excel'
399  );
400 
401  default:
402  case 'csv':
403  ilUtil::deliverData($contents,date('Y_m_d_H-i'.$file['timest']).
404  '_member_export_'.
405  $this->obj_id.
406  '.csv','text/csv');
407  break;
408  }
409  return true;
410  }
411  }
412  }
413 
421  public function confirmDeleteExportFile()
422  {
423  if(!count($_POST['id']))
424  {
425  ilUtil::sendFailure($this->lng->txt('ps_select_one'), true);
426  $this->ctrl->redirect($this, 'show');
427  }
428 
429  include_once('./Services/Utilities/classes/class.ilConfirmationGUI.php');
430  $confirmation_gui = new ilConfirmationGUI();
431  $confirmation_gui->setFormAction($this->ctrl->getFormAction($this));
432  $confirmation_gui->setHeaderText($this->lng->txt('info_delete_sure') /* .' '.$this->lng->txt('ps_delete_export_files') */);
433  $confirmation_gui->setCancel($this->lng->txt('cancel'), 'show');
434  $confirmation_gui->setConfirm($this->lng->txt('delete'), 'deleteExportFile');
435 
436  $counter = 0;
437  foreach($this->fss_export->getMemberExportFiles() as $file)
438  {
439  if(!in_array(md5($file['name']), $_POST['id']))
440  {
441  continue;
442  }
443 
444  $confirmation_gui->addItem("id[]", md5($file['name']),
445  strtoupper($file['type']).' - '.
447  }
448 
449  $this->tpl->setContent($confirmation_gui->getHTML());
450  }
451 
459  public function deleteExportFile()
460  {
461  if(!count($_POST['id']))
462  {
463  $this->ctrl->redirect($this, 'show');
464  }
465 
466  $counter = 0;
467  foreach($this->fss_export->getMemberExportFiles() as $file)
468  {
469  if(!in_array(md5($file['name']), $_POST['id']))
470  {
471  continue;
472  }
473 
474  $ret = $this->fss_export->deleteMemberExportFile($file['timest'].'_participant_export_'.
475  $file['type'].'_'.$this->obj_id.'.'.$file['type']);
476 
477  //try xlsx if return is false and type is xls
478  if($file['type'] == "xls" && !$ret)
479  {
480  $this->fss_export->deleteMemberExportFile($file['timest'].'_participant_export_'.
481  $file['type'].'_'.$this->obj_id.'.'."xlsx");
482  }
483  }
484 
485  ilUtil::sendSuccess($this->lng->txt('ps_files_deleted'), true);
486  $this->ctrl->redirect($this, 'show');
487  }
488 
489 
494  protected function initFileSystemStorage()
495  {
496  if($this->type == 'crs')
497  {
498  $this->fss_export = new ilFSStorageCourse($this->obj_id);
499  }
500  if($this->type == 'grp')
501  {
502  $this->fss_export = new ilFSStorageGroup($this->obj_id);
503  }
504  }
505 }
506 ?>
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
static deliverData($a_data, $a_filename, $mime="application/octet-stream", $charset="")
deliver data for download via browser.
Class for generation of member export files.
downloadExportFile()
Download export file.
deleteExportFile()
Delete member export files.
showFileList()
Show file list of available export files.
This class represents an option in a checkbox group.
static hasObjectBookingEntries($a_obj_id, $a_usr_id)
Check if object has assigned consultation hour appointments.
static _getInstance()
Get instance.
$_SESSION["AccountId"]
This class represents a property form user interface.
$_GET["client_id"]
$tbl
Definition: example_048.php:81
export()
Export Create member export file and store it in data directory.
initExcel(ilPropertyFormGUI $a_form=null)
__construct($a_ref_id)
Constructor.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
$cmd
Definition: sahs_server.php:35
This class represents a checkbox property in a property form.
initSettingsForm($a_is_excel=false)
const IL_CAL_UNIX
global $ilCtrl
Definition: ilias.php:18
$counter
executeCommand()
Execute Command.
static _getInstanceByType($a_type)
Get Singleton Instance.
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
setValue($a_value)
Set Value.
static formatDate(ilDateTime $date)
Format a date public.
Date and time handling
$ilUser
Definition: imgupload.php:18
This class represents a property in a property form.
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
show()
Show list of export files.
confirmDeleteExportFile()
Confirm deletion of export files.
Table presentation of membership export files.
initCSV(ilPropertyFormGUI $a_form=null)
initFileSystemStorage()
Init file object.
$ret
Definition: parser.php:6
static _getInstance()
Get instance of ilPrivacySettings.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
$_POST["username"]
Confirmation screen class.
static _getFields($a_container_id, $a_sort=IL_CDF_SORT_NAME)
Get all fields of a container.