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