ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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;
52 
60  public function __construct($a_ref_id)
61  {
62  global $ilCtrl,$tpl,$lng,$ilUser,$ilObjDataCache;
63 
64  $this->ctrl = $ilCtrl;
65  $this->tpl = $tpl;
66  $this->lng = $lng;
67  $this->lng->loadLanguageModule('ps');
68  $this->ref_id = $a_ref_id;
69  $this->obj_id = $ilObjDataCache->lookupObjId($this->ref_id);
70  $this->type = ilObject::_lookupType($this->obj_id);
71 
72  $this->fields_info = ilExportFieldsInfo::_getInstanceByType(ilObject::_lookupType($this->obj_id));
73  $this->initFileSystemStorage();
74  }
75 
83  public function executeCommand()
84  {
85  global $ilAccess,$rbacsystem;
86 
87  include_once('Services/PrivacySecurity/classes/class.ilPrivacySettings.php');
88  if(!ilPrivacySettings::_getInstance()->checkExportAccess($this->ref_id))
89  {
90  ilUtil::sendFailure($this->lng->txt('permission_denied'),true);
91  $this->ctrl->returnToParent($this);
92  }
93 
94  $next_class = $this->ctrl->getNextClass($this);
95  $cmd = $this->ctrl->getCmd();
96 
97  switch($next_class)
98  {
99  default:
100  if(!$cmd)
101  {
102  $cmd = 'show';
103  }
104  $this->$cmd();
105  break;
106  }
107  }
108 
115  public function show($a_deliver_file = false)
116  {
117  $this->showFileList();
118  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.member_export.html','Modules/Course');
119  $this->tpl->setVariable('FORM_ACTION',$this->ctrl->getFormAction($this));
120  $this->tpl->setVariable('TXT_EXPORT_SETTINGS',$this->lng->txt('ps_export_settings'));
121  $this->tpl->setVariable('TXT_USER_SELECTION',$this->lng->txt('ps_user_selection'));
122  $this->tpl->setVariable('TXT_EXPORT_ADMIN',$this->lng->txt('ps_export_admin'));
123  $this->tpl->setVariable('TXT_EXPORT_MEMBER',$this->lng->txt('ps_export_member'));
124  $this->tpl->setVariable('TXT_EXPORT_WAIT',$this->lng->txt('ps_export_wait'));
125  $this->tpl->setVariable('TXT_EXPORT_SUB',$this->lng->txt('ps_export_sub'));
126 
127  // Check user selection
128  $this->exportSettings = new ilUserFormSettings('memexp');
129 
130  $this->tpl->setVariable('CHECK_EXPORT_ADMIN',ilUtil::formCheckbox($this->exportSettings->enabled('admin'),'export_members[admin]',1));
131  if($this->type == 'crs')
132  {
133  $this->tpl->setVariable('CHECK_EXPORT_TUTOR',ilUtil::formCheckbox($this->exportSettings->enabled('tutor'),'export_members[tutor]',1));
134  $this->tpl->setVariable('TXT_EXPORT_TUTOR',$this->lng->txt('ps_export_tutor'));
135  }
136  $this->tpl->setVariable('CHECK_EXPORT_MEMBER',ilUtil::formCheckbox($this->exportSettings->enabled('member'),'export_members[member]',1));
137  $this->tpl->setVariable('CHECK_EXPORT_SUB',ilUtil::formCheckbox($this->exportSettings->enabled('subscribers'),'export_members[subscribers]',1));
138  $this->tpl->setVariable('CHECK_EXPORT_WAIT',ilUtil::formCheckbox($this->exportSettings->enabled('waiting_list'),'export_members[waiting_list]',1));
139 
140  $this->tpl->setVariable('TXT_EXPORT',$this->lng->txt('ps_perform_export'));
141  $this->tpl->setVariable('TXT_EXPORT_EXCEL',$this->lng->txt('ps_export_excel'));
142 
143  // User Data
144  $this->tpl->setVariable('TXT_USER_DATA_SELECTION',$this->lng->txt('ps_export_data'));
145  $this->tpl->setVariable('TXT_EXPORT_USER_DATA_HEADER',$this->lng->txt('ps_export_user_data'));
146 
147 
148  $fields = $this->fields_info->getFieldsInfo();
149  foreach($fields as $field => $exportable)
150  {
151  if(!$exportable)
152  {
153  continue;
154  }
155  $this->tpl->setCurrentBlock('user_data_row');
156  $this->tpl->setVariable('CHECK_EXPORT_USER_DATA',ilUtil::formCheckbox($this->exportSettings->enabled($field),'export_members['.$field.']',1));
157  $this->tpl->setVariable('TXT_EXPORT_USER_DATA',$this->lng->txt($field));
158  $this->tpl->parseCurrentBlock();
159  }
160 
162  foreach($exp = $udf->getExportableFields($this->obj_id) as $field_id => $udf_data)
163  {
164  $this->tpl->setCurrentBlock('user_data_row');
165  $this->tpl->setVariable('CHECK_EXPORT_USER_DATA',ilUtil::formCheckbox($this->exportSettings->enabled('udf_'.$field_id),
166  'export_members[udf_'.$field_id.']',1));
167  $this->tpl->setVariable('TXT_EXPORT_USER_DATA',$udf_data['field_name']);
168  $this->tpl->parseCurrentBlock();
169  }
170 
171 
172  $cdf_fields = ilCourseDefinedFieldDefinition::_getFields($this->obj_id);
173  foreach($cdf_fields as $field_obj)
174  {
175  $this->tpl->setCurrentBlock('cdf_row');
176  $this->tpl->setVariable('CHECK_CDF_DATA',ilUtil::formCheckbox($this->exportSettings->enabled('cdf_'.$field_obj->getId()),
177  'export_members[cdf_'.$field_obj->getId().']',
178  1));
179  $this->tpl->setVariable('TXT_CDF_NAME',$field_obj->getName());
180  $this->tpl->parseCurrentBlock();
181  }
182  if(count($cdf_fields))
183  {
184  $this->tpl->setCurrentBlock('cdf_fields');
185  $this->tpl->setVariable('TXT_CDF_SELECTION',$this->lng->txt('ps_'.$this->type.'_user_fields'));
186  $this->tpl->parseCurrentBlock();
187  }
188 
189  if($a_deliver_file and 0)
190  {
191  $this->tpl->setCurrentBlock('iframe');
192  $this->tpl->setVariable('SOURCE',$this->ctrl->getLinkTarget($this,'deliverData'));
193  }
194  }
195 
203  public function deliverData()
204  {
205  foreach($this->fss_export->getMemberExportFiles() as $file)
206  {
207  if($file['name'] == $_SESSION['member_export_filename'])
208  {
209  $content = $this->fss_export->getMemberExportFile($_SESSION['member_export_filename']);
210  ilUtil::deliverData($content,date('Y_m_d_H-i',$file['timest']).
211  '_member_export_'.
212  $this->obj_id.
213  '.csv','text/csv');
214  }
215  }
216  }
217 
224  public function showFileList()
225  {
226  global $ilUser;
227 
228  if(!count($files = $this->fss_export->getMemberExportFiles()))
229  {
230  return false;
231  }
232 
233  $a_tpl = new ilTemplate('tpl.table.html',true,true);
234  $a_tpl->addBlockfile("TBL_CONTENT", "tbl_content", "tpl.member_export_file_row.html", "Modules/Course");
235  $a_tpl->setVariable('FORMACTION',$this->ctrl->getFormaction($this));
236 
237  include_once("./Services/Table/classes/class.ilTableGUI.php");
238  $tbl = new ilTableGUI();
239 
240  // load template for table content data
241 
242  $tbl->setTitle($this->lng->txt("ps_export_files"));
243 
244  $tbl->setHeaderNames(array("", $this->lng->txt("type"),
245  $this->lng->txt("ps_size"),
246  $this->lng->txt("date") ));
247 
248  $cols = array("", "type","size", "date");
249 
250  $header_params = $this->ctrl->getParameterArray($this,'show');
251  $tbl->setHeaderVars($cols, $header_params);
252  $tbl->setColumnWidth(array("1%", "9%", "45%", "45%"));
253 
254  // control
255  $tbl->setOrderColumn($_GET["sort_by"]);
256  $tbl->setOrderDirection($_GET["sort_order"]);
257  $tbl->setLimit($ilUser->getPref('hits_per_page',9999));
258  $tbl->setOffset($_GET["offset"]);
259  $tbl->setMaxCount(count($files));
260  $tbl->disable("sort");
261  $a_tpl->setVariable("COLUMN_COUNTS",4);
262 
263  $files = array_reverse($files);
264  $files = array_slice($files, $_GET["offset"], $_GET["limit"]);
265  $num = 0;
266  $i=0;
267  foreach($files as $exp_file)
268  {
269  $a_tpl->setCurrentBlock("tbl_content");
270  $a_tpl->setVariable("TXT_FILENAME", $exp_file["file"]);
271 
272  $css_row = ilUtil::switchColor($i++, "tblrow1", "tblrow2");
273  $a_tpl->setVariable("CSS_ROW", $css_row);
274 
275  $a_tpl->setVariable("TXT_SIZE",$exp_file['size']);
276  $a_tpl->setVariable("TXT_TYPE", strtoupper($exp_file["type"]));
277  $a_tpl->setVariable("CHECKBOX_ID",$exp_file["timest"]);
278  $a_tpl->setVariable("TXT_DATE", date("Y-m-d H:i",$exp_file['timest']));
279  $a_tpl->parseCurrentBlock();
280  }
281 
282 
283  // delete button
284  $a_tpl->setVariable("IMG_ARROW", ilUtil::getImagePath("arrow_downright.png"));
285  $a_tpl->setCurrentBlock("tbl_action_btn");
286  $a_tpl->setVariable("BTN_NAME", "confirmDeleteExportFile");
287  $a_tpl->setVariable("BTN_VALUE", $this->lng->txt("delete"));
288  $a_tpl->parseCurrentBlock();
289 
290  $a_tpl->setCurrentBlock("tbl_action_btn");
291  $a_tpl->setVariable("BTN_NAME", "downloadExportFile");
292  $a_tpl->setVariable("BTN_VALUE", $this->lng->txt("download"));
293  $a_tpl->parseCurrentBlock();
294 
295  // footer
296  $tbl->setFooter("tblfooter",$this->lng->txt("previous"),$this->lng->txt("next"));
297  //$tbl->disable("footer");
298 
299  $tbl->setTemplate($a_tpl);
300  $tbl->render();
301 
302  #$this->tpl->setCurrentBlock('file_list');
303  $this->tpl->setVariable('FILE_LIST_TABLE',$a_tpl->get());
304  #$this->tpl->parseCurrentBlock();
305 
306  }
307 
315  public function downloadExportFile()
316  {
317  if(count($_POST['files']) != 1)
318  {
319  ilUtil::sendFailure($this->lng->txt('ps_select_one'));
320  $this->show();
321  return true;
322  }
323  foreach($this->fss_export->getMemberExportFiles() as $file)
324  {
325  if(!in_array($file['timest'],$_POST['files']))
326  {
327  continue;
328  }
329  $contents = $this->fss_export->getMemberExportFile($file['timest'].'_participant_export_'.
330  $file['type'].'_'.$this->obj_id.'.'.$file['type']);
331 
332 
333  switch($file['type'])
334  {
335  case 'xls':
337  $contents,
338  date('Y_m_d_H-i'.$file['timest']).'_member_export_'.$this->obj_id.'.xls',
339  'application/vnd.ms-excel'
340  );
341 
342  default:
343  case 'csv':
344  ilUtil::deliverData($contents,date('Y_m_d_H-i'.$file['timest']).
345  '_member_export_'.
346  $this->obj_id.
347  '.csv','text/csv');
348  break;
349  }
350  return true;
351 
352  }
353 
354 
355  }
356 
364  public function confirmDeleteExportFile()
365  {
366  if(!count($_POST['files']))
367  {
368  ilUtil::sendFailure($this->lng->txt('ps_select_one'));
369  $this->show();
370  return false;
371  }
372  $_SESSION['il_del_member_export'] = $_POST['files'];
373  ilUtil::sendQuestion($this->lng->txt("info_delete_sure"));
374 
375  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.member_export_confirm_delete.html','Modules/Course');
376  $this->tpl->setVariable('FORMACTION',$this->ctrl->getFormAction($this));
377  $this->tpl->setVariable('TEXT',$this->lng->txt('ps_delete_export_files'));
378 
379 
380  $counter = 0;
381  foreach($this->fss_export->getMemberExportFiles() as $file)
382  {
383  if(!in_array($file['timest'],$_POST['files']))
384  {
385  continue;
386  }
387  $this->tpl->setCurrentBlock('table_row');
388  $this->tpl->setVariable('CSS_ROW',ilUtil::switchColor($counter++,'tblrow1','tblrow2'));
389  $this->tpl->setVariable('TEXT_TYPE',strtoupper($file['type']));
390  $this->tpl->setVariable('DATE',ilDatePresentation::formatDate(new ilDateTime($file['timest'],IL_CAL_UNIX)));
391  $this->tpl->parseCurrentBlock();
392  }
393  $this->tpl->setCurrentBlock('operation_btn');
394  $this->tpl->setVariable('BTN_NAME','deleteExportFile');
395  $this->tpl->setVariable('BTN_VALUE',$this->lng->txt('delete'));
396  $this->tpl->parseCurrentBlock();
397 
398  $this->tpl->setCurrentBlock('operation_btn');
399  $this->tpl->setVariable('BTN_NAME','show');
400  $this->tpl->setVariable('BTN_VALUE',$this->lng->txt('cancel'));
401  $this->tpl->parseCurrentBlock();
402 
403  }
404 
412  public function deleteExportFile()
413  {
414  if(!is_array($_SESSION['il_del_member_export']))
415  {
416  $this->show();
417  return false;
418  }
419  $counter = 0;
420  foreach($this->fss_export->getMemberExportFiles() as $file)
421  {
422  if(!in_array($file['timest'],$_SESSION['il_del_member_export']))
423  {
424  continue;
425  }
426  $this->fss_export->deleteMemberExportFile($file['timest'].'_participant_export_'.$file['type'].'_'.$this->obj_id.'.'.$file['type']);
427  }
428  ilUtil::sendSuccess($this->lng->txt('ps_files_deleted'));
429  $this->show();
430  }
431 
432 
433 
434 
441  public function export()
442  {
443  // Save settings
444  $this->exportSettings = new ilUserFormSettings('memexp');
445  $this->exportSettings->set($_POST['export_members']);
446  $this->exportSettings->store();
447 
448  $this->export = new ilMemberExport($this->ref_id);
449  $this->export->create();
450 
451  $filename = time().'_participant_export_csv_'.$this->obj_id.'.csv';
452  $this->fss_export->addMemberExportFile($this->export->getCSVString(),$filename);
453 
454  $_SESSION['member_export_filename'] = $filename;
455 
456  $this->show(true);
457  }
458 
459  public function exportExcel()
460  {
461  $this->exportSettings = new ilUserFormSettings('memexp');
462  $this->exportSettings->set($_POST['export_members']);
463  $this->exportSettings->store();
464 
465  $filename = time().'_participant_export_xls_'.$this->obj_id.'.xls';
466  $this->fss_export->initMemberExportDirectory();
467  $filepath = $this->fss_export->getMemberExportDirectory().DIRECTORY_SEPARATOR.$filename;
468 
469  $this->export = new ilMemberExport($this->ref_id,ilMemberExport::EXPORT_EXCEL);
470  $this->export->setFilename($filepath);
471  $this->export->create();
472 
473  $_SESSION['member_export_filename'] = $filename;
474 
475  $this->show(true);
476 
477  }
478 
479 
484  protected function initFileSystemStorage()
485  {
486  if($this->type == 'crs')
487  {
488  $this->fss_export = new ilFSStorageCourse($this->obj_id);
489  }
490  if($this->type == 'grp')
491  {
492  $this->fss_export = new ilFSStorageGroup($this->obj_id);
493  }
494 
495  }
496 }
497 ?>