ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMemberExport.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 include_once('Services/PrivacySecurity/classes/class.ilPrivacySettings.php');
24 include_once('Services/Membership/classes/class.ilMemberAgreement.php');
25 include_once('Modules/Course/classes/class.ilCourseParticipants.php');
26 include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
27 include_once('Services/User/classes/class.ilUserDefinedData.php');
28 include_once('Services/User/classes/class.ilUserFormSettings.php');
29 
30 define("IL_MEMBER_EXPORT_CSV_FIELD_SEPERATOR",',');
31 define("IL_MEMBER_EXPORT_CSV_STRING_DELIMITER",'"');
32 
33 
43 {
44  const EXPORT_CSV = 1;
45  const EXPORT_EXCEL = 2;
46 
47 
48  private $ref_id;
49  private $obj_id;
50  private $type;
51  private $members;
52 
53  private $lng;
54 
55  private $settings;
56 
57  private $export_type = null;
58  private $filename = null;
59 
60  private $user_ids = array();
61  private $user_course_data = array();
62  private $user_course_fields = array();
63  private $user_profile_data = array();
64  private $privacy;
65 
72  public function __construct($a_ref_id, $a_type = self::EXPORT_CSV)
73  {
74  global $ilObjDataCache,$lng;
75 
76  $this->lng = $lng;
77 
78  $this->export_type = $a_type;
79 
80  $this->ref_id = $a_ref_id;
81  $this->obj_id = $ilObjDataCache->lookupObjId($this->ref_id);
82  $this->type = ilObject::_lookupType($this->obj_id);
83 
84  $this->initMembers();
85 
86  $this->agreement = ilMemberAgreement::_readByObjId($this->obj_id);
87  $this->settings = new ilUserFormSettings('memexp');
88  $this->privacy = ilPrivacySettings::_getInstance();
89  }
90 
96  public function setFilename($a_file)
97  {
98  $this->filename = $a_file;
99  }
100 
105  public function getFilename()
106  {
107  return $this->filename;
108  }
109 
114  public function getRefId()
115  {
116  return $this->ref_id;
117  }
118 
123  public function getType()
124  {
125  return $this->type;
126  }
127 
132  public function getExportType()
133  {
134  return $this->export_type;
135  }
136 
141  public function getObjId()
142  {
143  return $this->obj_id;
144  }
145 
152  public function create()
153  {
154  $this->fetchUsers();
155 
156  // DONE: Switch different export types
157  switch($this->getExportType())
158  {
159  case self::EXPORT_CSV:
160  $this->createCSV();
161  break;
162 
163  case self::EXPORT_EXCEL:
164  $this->createExcel();
165  break;
166  }
167  }
168 
176  public function getCSVString()
177  {
178  return $this->csv->getCSVString();
179  }
180 
181 
186  public function createExcel()
187  {
188  include_once "./Services/Excel/classes/class.ilExcelUtils.php";
189  include_once "./Services/Excel/classes/class.ilExcelWriterAdapter.php";
190  $adapter = new ilExcelWriterAdapter($this->getFilename(), false);
191  $workbook = $adapter->getWorkbook();
192  $this->worksheet = $workbook->addWorksheet();
193  $this->write();
194  $workbook->close();
195  }
196 
203  public function createCSV()
204  {
205  include_once('Services/Utilities/classes/class.ilCSVWriter.php');
206  $this->csv = new ilCSVWriter();
207 
208  $this->write();
209  }
210 
211 
212 
220  protected function addCol($a_value,$a_row,$a_col)
221  {
222  switch($this->getExportType())
223  {
224  case self::EXPORT_CSV:
225  $this->csv->addColumn($a_value);
226  break;
227 
228  case self::EXPORT_EXCEL:
229  $this->worksheet->write($a_row,$a_col,$a_value);
230  break;
231  }
232  }
233 
238  protected function addRow()
239  {
240  switch($this->getExportType())
241  {
242  case self::EXPORT_CSV:
243  $this->csv->addRow();
244  break;
245 
246  case self::EXPORT_EXCEL:
247  break;
248  }
249  }
250 
258  protected function getOrderedExportableFields()
259  {
260  include_once('Services/PrivacySecurity/classes/class.ilPrivacySettings.php');
261  include_once('Services/PrivacySecurity/classes/class.ilExportFieldsInfo.php');
262  include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
263  include_once('Services/User/classes/class.ilUserDefinedFields.php');
264 
266 
267  $fields[] = 'role';
268  // Append agreement info
270  if($privacy->courseConfirmationRequired() or ilCourseDefinedFieldDefinition::_hasFields($this->obj_id))
271  {
272  $fields[] = 'agreement';
273  }
274 
275  foreach($field_info->getExportableFields() as $field)
276  {
277  if($this->settings->enabled($field))
278  {
279  $fields[] = $field;
280  }
281  }
282 
284  foreach($udf->getCourseExportableFields() as $field_id => $udf_data)
285  {
286  if($this->settings->enabled('udf_'.$field_id))
287  {
288  $fields[] = 'udf_'.$field_id;
289  }
290  }
291 
292  // Add course specific fields
293  foreach(ilCourseDefinedFieldDefinition::_getFields($this->obj_id) as $field_obj)
294  {
295  if($this->settings->enabled('cdf_'.$field_obj->getId()))
296  {
297  $fields[] = 'cdf_'.$field_obj->getId();
298  }
299  }
300  return $fields ? $fields : array();
301  }
302 
307  protected function write()
308  {
309  // Add header line
310  $row = 0;
311  $col = 0;
312  foreach($all_fields = $this->getOrderedExportableFields() as $field)
313  {
314  switch($field)
315  {
316  case 'role':
317  #$this->csv->addColumn($this->lng->txt($this->getType().'_role_status'));
318  $this->addCol($this->lng->txt($this->getType().'_role_status'), $row, $col++);
319  break;
320  case 'agreement':
321  #$this->csv->addColumn($this->lng->txt('ps_agreement_accepted'));
322  $this->addCol($this->lng->txt('ps_agreement_accepted'), $row, $col++);
323  break;
324  default:
325  if(substr($field,0,4) == 'udf_')
326  {
327  $field_id = explode('_',$field);
328  include_once('Services/User/classes/class.ilUserDefinedFields.php');
330  $def = $udf->getDefinition($field_id[1]);
331  #$this->csv->addColumn($def['field_name']);
332  $this->addCol($def['field_name'], $row, $col++);
333  }
334  elseif(substr($field,0,4) == 'cdf_')
335  {
336  $field_id = explode('_',$field);
337  #$this->csv->addColumn(ilCourseDefinedFieldDefinition::_lookupName($field_id[1]));
338  $this->addCol(ilCourseDefinedFieldDefinition::_lookupName($field_id[1]),$row,$col++);
339  }
340  else
341  {
342  #$this->csv->addColumn($this->lng->txt($field));
343  $this->addCol($this->lng->txt($field), $row, $col++);
344  }
345  break;
346  }
347  }
348  #$this->csv->addRow();
349  $this->addRow();
350  // Add user data
351  foreach($this->user_ids as $usr_id)
352  {
353  $row++;
354  $col = 0;
355 
356  $udf_data = new ilUserDefinedData($usr_id);
357  foreach($all_fields as $field)
358  {
359  // Handle course defined fields
360  if($this->addUserDefinedField($udf_data,$field,$row,$col))
361  {
362  $col++;
363  continue;
364  }
365 
366  if($this->addCourseField($usr_id,$field,$row,$col))
367  {
368  $col++;
369  continue;
370  }
371 
372  switch($field)
373  {
374  case 'role':
375  switch($this->user_course_data[$usr_id]['role'])
376  {
377  case IL_CRS_ADMIN:
378  #$this->csv->addColumn($this->lng->txt('crs_admin'));
379  $this->addCol($this->lng->txt('crs_admin'), $row, $col++);
380  break;
381 
382  case IL_CRS_TUTOR:
383  #$this->csv->addColumn($this->lng->txt('crs_tutor'));
384  $this->addCol($this->lng->txt('crs_tutor'), $row, $col++);
385  break;
386 
387  case IL_CRS_MEMBER:
388  #$this->csv->addColumn($this->lng->txt('crs_member'));
389  $this->addCol($this->lng->txt('crs_member'), $row, $col++);
390  break;
391 
392  case IL_GRP_ADMIN:
393  #$this->csv->addColumn($this->lng->txt('il_grp_admin'));
394  $this->addCol($this->lng->txt('il_grp_admin'), $row, $col++);
395  break;
396 
397  case IL_GRP_MEMBER:
398  #$this->csv->addColumn($this->lng->txt('il_grp_member'));
399  $this->addCol($this->lng->txt('il_grp_member'), $row, $col++);
400  break;
401 
402  case 'subscriber':
403  #$this->csv->addColumn($this->lng->txt($this->getType().'_subscriber'));
404  $this->addCol($this->lng->txt($this->getType().'_subscriber'), $row, $col++);
405  break;
406 
407  default:
408  #$this->csv->addColumn($this->lng->txt('crs_waiting_list'));
409  $this->addCol($this->lng->txt('crs_waiting_list'), $row, $col++);
410  break;
411 
412  }
413  break;
414 
415  case 'agreement':
416  if(isset($this->agreement[$usr_id]))
417  {
418  if($this->agreement[$usr_id]['accepted'])
419  {
420  #$this->csv->addColumn(ilFormat::formatUnixTime($this->agreement[$usr_id]['acceptance_time'],true));
421  $this->addCol(ilFormat::formatUnixTime($this->agreement[$usr_id]['acceptance_time'],true),$row,$col++);
422  }
423  else
424  {
425  #$this->csv->addColumn($this->lng->txt('ps_not_accepted'));
426  $this->addCol($this->lng->txt('ps_not_accepted'),$row,$col++);
427  }
428  }
429  else
430  {
431  #$this->csv->addColumn($this->lng->txt('ps_not_accepted'));
432  $this->addCol($this->lng->txt('ps_not_accepted'),$row,$col++);
433  }
434  break;
435 
436  // These fields are always enabled
437  case 'username':
438  #$this->csv->addColumn($this->user_profile_data[$usr_id]['login']);
439  $this->addCol($this->user_profile_data[$usr_id]['login'],$row,$col++);
440  break;
441 
442  case 'firstname':
443  case 'lastname':
444  #$this->csv->addColumn($this->user_profile_data[$usr_id][$field]);
445  $this->addCol($this->user_profile_data[$usr_id][$field],$row,$col++);
446  break;
447 
448  default:
449  // Check aggreement
450  if((!$this->privacy->courseConfirmationRequired() and !ilCourseDefinedFieldDefinition::_getFields($this->obj_id))
451  or $this->agreement[$usr_id]['accepted'])
452  {
453  #$this->csv->addColumn($this->user_profile_data[$usr_id][$field]);
454  $this->addCol($this->user_profile_data[$usr_id][$field],$row,$col++);
455  }
456  else
457  {
458  #$this->csv->addColumn('');
459  $this->addCol('', $row, $col++);
460  }
461  break;
462 
463  }
464  }
465  #$this->csv->addRow();
466  $this->addRow();
467  }
468 
469  }
470 
471 
472 
479  private function fetchUsers()
480  {
482 
483  if($this->settings->enabled('admin'))
484  {
485  $this->user_ids = $tmp_ids = $this->members->getAdmins();
486  $this->readCourseData($tmp_ids);
487  }
488  if($this->settings->enabled('tutor'))
489  {
490  $this->user_ids = array_merge($tmp_ids = $this->members->getTutors(),$this->user_ids);
491  $this->readCourseData($tmp_ids);
492  }
493  if($this->settings->enabled('member'))
494  {
495  $this->user_ids = array_merge($tmp_ids = $this->members->getMembers(),$this->user_ids);
496  $this->readCourseData($tmp_ids);
497  }
498  if($this->settings->enabled('subscribers'))
499  {
500  $this->user_ids = array_merge($tmp_ids = $this->members->getSubscribers(),$this->user_ids);
501  $this->readCourseData($tmp_ids,'subscriber');
502  }
503  if($this->settings->enabled('waiting_list'))
504  {
505  include_once('Modules/Course/classes/class.ilCourseWaitingList.php');
506  $waiting_list = new ilCourseWaitingList($this->obj_id);
507  $this->user_ids = array_merge($waiting_list->getUserIds(),$this->user_ids);
508 
509  }
510  // Sort by lastname
511  $this->user_ids = ilUtil::_sortIds($this->user_ids,'usr_data','lastname','usr_id');
512 
513  // Finally read user profile data
514  $this->user_profile_data = ilObjUser::_readUsersProfileData($this->user_ids);
515  }
516 
523  private function readCourseData($a_user_ids,$a_status = 'member')
524  {
525  foreach($a_user_ids as $user_id)
526  {
527  // Read course related data
528  if($this->members->isAdmin($user_id))
529  {
530  $this->user_course_data[$user_id]['role'] = $this->getType() == 'crs' ? IL_CRS_ADMIN : IL_GRP_ADMIN;
531  }
532  elseif($this->members->isTutor($user_id))
533  {
534  $this->user_course_data[$user_id]['role'] = IL_CRS_TUTOR;
535  }
536  elseif($this->members->isMember($user_id))
537  {
538  $this->user_course_data[$user_id]['role'] = $this->getType() == 'crs' ? IL_CRS_MEMBER : IL_GRP_MEMBER;
539  }
540  else
541  {
542  $this->user_course_data[$user_id]['role'] = 'subscriber';
543  }
544  }
545  }
546 
554  private function readCourseSpecificFieldsData()
555  {
556  include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
557  $this->user_course_fields = ilCourseUserData::_getValuesByObjId($this->obj_id);
558  }
559 
569  private function addCourseField($a_usr_id,$a_field,$row,$col)
570  {
571  if(substr($a_field,0,4) != 'cdf_')
572  {
573  return false;
574  }
575  if((!$this->privacy->courseConfirmationRequired() and ilCourseDefinedFieldDefinition::_getFields($this->obj_id))
576  or $this->agreement[$a_usr_id]['accepted'])
577  {
578  $field_info = explode('_',$a_field);
579  $field_id = $field_info[1];
580  $value = $this->user_course_fields[$a_usr_id][$field_id];
581  #$this->csv->addColumn($value);
582  $this->addCol($value, $row, $col);
583  return true;
584  }
585  #$this->csv->addColumn('');
586  $this->addCol('', $row, $col);
587  return true;
588 
589  }
590 
599  private function addUserDefinedField($udf_data,$a_field,$row,$col)
600  {
601  if(substr($a_field,0,4) != 'udf_')
602  {
603  return false;
604  }
605  if(!$this->privacy->courseConfirmationRequired() or $this->agreement[$udf_data->getUserId()]['accepted'])
606  {
607  $field_info = explode('_',$a_field);
608  $field_id = $field_info[1];
609  $value = $udf_data->get('f_'.$field_id);
610  #$this->csv->addColumn($value);
611  $this->addCol($value, $row, $col);
612  return true;
613  }
614  #$this->csv->addColumn('');
615  $this->addCol('', $row, $col);
616  }
617 
622  protected function initMembers()
623  {
624  if($this->getType() == 'crs')
625  {
626  $this->members = ilCourseParticipants::_getInstanceByObjId($this->getObjId());
627  }
628  if($this->getType() == 'grp')
629  {
630  $this->members = ilGroupParticipants::_getInstanceByObjId($this->getObjId());
631  }
632  return true;
633  }
634 }
635 
636 
637 ?>