ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  private $groups = array();
54  private $groups_rights = array();
55 
56  private $lng;
57 
58  private $settings;
59 
60  private $export_type = null;
61  private $filename = null;
62 
63  private $user_ids = array();
64  private $user_course_data = array();
67  private $privacy;
68 
75  public function __construct($a_ref_id, $a_type = self::EXPORT_CSV)
76  {
77  global $ilObjDataCache,$lng;
78 
79  $this->lng = $lng;
80 
81  $this->export_type = $a_type;
82 
83  $this->ref_id = $a_ref_id;
84  $this->obj_id = $ilObjDataCache->lookupObjId($this->ref_id);
85  $this->type = ilObject::_lookupType($this->obj_id);
86 
87  $this->initMembers();
88  $this->initGroups();
89 
90  $this->agreement = ilMemberAgreement::_readByObjId($this->obj_id);
91  $this->settings = new ilUserFormSettings('memexp');
92  $this->privacy = ilPrivacySettings::_getInstance();
93  }
94 
100  public function setFilename($a_file)
101  {
102  $this->filename = $a_file;
103  }
104 
109  public function getFilename()
110  {
111  return $this->filename;
112  }
113 
118  public function getRefId()
119  {
120  return $this->ref_id;
121  }
122 
127  public function getType()
128  {
129  return $this->type;
130  }
131 
136  public function getExportType()
137  {
138  return $this->export_type;
139  }
140 
145  public function getObjId()
146  {
147  return $this->obj_id;
148  }
149 
156  public function create()
157  {
158  $this->fetchUsers();
159 
160  // DONE: Switch different export types
161  switch($this->getExportType())
162  {
163  case self::EXPORT_CSV:
164  $this->createCSV();
165  break;
166 
167  case self::EXPORT_EXCEL:
168  $this->createExcel();
169  break;
170  }
171  }
172 
180  public function getCSVString()
181  {
182  return $this->csv->getCSVString();
183  }
184 
185 
190  public function createExcel()
191  {
192  include_once "./Services/Excel/classes/class.ilExcel.php";
193  $this->worksheet = new ilExcel();
194  $this->worksheet->addSheet($this->lng->txt("members"));
195 
196  $this->write();
197 
198  $this->worksheet->writeToFile($this->getFilename());
199  }
200 
207  public function createCSV()
208  {
209  include_once('Services/Utilities/classes/class.ilCSVWriter.php');
210  $this->csv = new ilCSVWriter();
211 
212  $this->write();
213  }
214 
215 
216 
224  protected function addCol($a_value,$a_row,$a_col)
225  {
226  switch($this->getExportType())
227  {
228  case self::EXPORT_CSV:
229  $this->csv->addColumn($a_value);
230  break;
231 
232  case self::EXPORT_EXCEL:
233  $this->worksheet->setCell($a_row+1,$a_col,$a_value);
234  break;
235  }
236  }
237 
242  protected function addRow()
243  {
244  switch($this->getExportType())
245  {
246  case self::EXPORT_CSV:
247  $this->csv->addRow();
248  break;
249 
250  case self::EXPORT_EXCEL:
251  break;
252  }
253  }
254 
262  protected function getOrderedExportableFields()
263  {
264  include_once('Services/PrivacySecurity/classes/class.ilPrivacySettings.php');
265  include_once('Services/PrivacySecurity/classes/class.ilExportFieldsInfo.php');
266  include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
267  include_once('Services/User/classes/class.ilUserDefinedFields.php');
268 
270  $field_info->sortExportFields();
271  $fields[] = 'role';
272  // Append agreement info
274  if($privacy->courseConfirmationRequired())
275  {
276  $fields[] = 'agreement';
277  }
278 
279  foreach($field_info->getExportableFields() as $field)
280  {
281  if($this->settings->enabled($field))
282  {
283  $fields[] = $field;
284  }
285  }
286 
288  foreach($udf->getCourseExportableFields() as $field_id => $udf_data)
289  {
290  if($this->settings->enabled('udf_'.$field_id))
291  {
292  $fields[] = 'udf_'.$field_id;
293  }
294  }
295 
296  // Add course specific fields
297  foreach(ilCourseDefinedFieldDefinition::_getFields($this->obj_id) as $field_obj)
298  {
299  if($this->settings->enabled('cdf_'.$field_obj->getId()))
300  {
301  $fields[] = 'cdf_'.$field_obj->getId();
302  }
303  }
304  if($this->settings->enabled('group_memberships'))
305  {
306  $fields[] = 'crs_members_groups';
307  }
308 
309  return $fields ? $fields : array();
310  }
311 
316  protected function write()
317  {
318  // Add header line
319  $row = 0;
320  $col = 0;
321  foreach($all_fields = $this->getOrderedExportableFields() as $field)
322  {
323  switch($field)
324  {
325  case 'role':
326  #$this->csv->addColumn($this->lng->txt($this->getType().'_role_status'));
327  $this->addCol($this->lng->txt($this->getType().'_role_status'), $row, $col++);
328  break;
329  case 'agreement':
330  #$this->csv->addColumn($this->lng->txt('ps_agreement_accepted'));
331  $this->addCol($this->lng->txt('ps_agreement_accepted'), $row, $col++);
332  break;
333  case 'consultation_hour':
334  $this->lng->loadLanguageModule('dateplaner');
335  $this->addCol($this->lng->txt('cal_ch_field_ch'), $row, $col++);
336  break;
337 
338  case 'org_units':
339  $this->addCol($this->lng->txt('org_units'), $row, $col++);
340  break;
341 
342  default:
343  if(substr($field,0,4) == 'udf_')
344  {
345  $field_id = explode('_',$field);
346  include_once('Services/User/classes/class.ilUserDefinedFields.php');
348  $def = $udf->getDefinition($field_id[1]);
349  #$this->csv->addColumn($def['field_name']);
350  $this->addCol($def['field_name'], $row, $col++);
351  }
352  elseif(substr($field,0,4) == 'cdf_')
353  {
354  $field_id = explode('_',$field);
355  #$this->csv->addColumn(ilCourseDefinedFieldDefinition::_lookupName($field_id[1]));
356  $this->addCol(ilCourseDefinedFieldDefinition::_lookupName($field_id[1]),$row,$col++);
357  }elseif($field == "username")//User Name Presentation Guideline; username should be named login
358  {
359  $this->addCol($this->lng->txt("login"), $row, $col++);
360  }
361  else
362  {
363  #$this->csv->addColumn($this->lng->txt($field));
364  $this->addCol($this->lng->txt($field), $row, $col++);
365  }
366  break;
367  }
368  }
369  #$this->csv->addRow();
370  $this->addRow();
371  // Add user data
372  foreach($this->user_ids as $usr_id)
373  {
374  $row++;
375  $col = 0;
376 
377  $udf_data = new ilUserDefinedData($usr_id);
378  foreach($all_fields as $field)
379  {
380  // Handle course defined fields
381  if($this->addUserDefinedField($udf_data,$field,$row,$col))
382  {
383  $col++;
384  continue;
385  }
386 
387  if($this->addCourseField($usr_id,$field,$row,$col))
388  {
389  $col++;
390  continue;
391  }
392 
393  switch($field)
394  {
395  case 'role':
396  switch($this->user_course_data[$usr_id]['role'])
397  {
398  case IL_CRS_ADMIN:
399  #$this->csv->addColumn($this->lng->txt('crs_admin'));
400  $this->addCol($this->lng->txt('crs_admin'), $row, $col++);
401  break;
402 
403  case IL_CRS_TUTOR:
404  #$this->csv->addColumn($this->lng->txt('crs_tutor'));
405  $this->addCol($this->lng->txt('crs_tutor'), $row, $col++);
406  break;
407 
408  case IL_CRS_MEMBER:
409  #$this->csv->addColumn($this->lng->txt('crs_member'));
410  $this->addCol($this->lng->txt('crs_member'), $row, $col++);
411  break;
412 
413  case IL_GRP_ADMIN:
414  #$this->csv->addColumn($this->lng->txt('il_grp_admin'));
415  $this->addCol($this->lng->txt('il_grp_admin'), $row, $col++);
416  break;
417 
418  case IL_GRP_MEMBER:
419  #$this->csv->addColumn($this->lng->txt('il_grp_member'));
420  $this->addCol($this->lng->txt('il_grp_member'), $row, $col++);
421  break;
422 
423  case 'subscriber':
424  #$this->csv->addColumn($this->lng->txt($this->getType().'_subscriber'));
425  $this->addCol($this->lng->txt($this->getType().'_subscriber'), $row, $col++);
426  break;
427 
428  default:
429  #$this->csv->addColumn($this->lng->txt('crs_waiting_list'));
430  $this->addCol($this->lng->txt('crs_waiting_list'), $row, $col++);
431  break;
432 
433  }
434  break;
435 
436  case 'agreement':
437  if(isset($this->agreement[$usr_id]))
438  {
439  if($this->agreement[$usr_id]['accepted'])
440  {
441  #$this->csv->addColumn(il-Format::format-Unix-Time($this->agreement[$usr_id]['acceptance_time'],true));
442  $dt = new ilDateTime($this->agreement[$usr_id]['acceptance_time'], IL_CAL_UNIX);
443  $this->addCol($dt->get(IL_CAL_DATETIME),$row,$col++);
444  }
445  else
446  {
447  #$this->csv->addColumn($this->lng->txt('ps_not_accepted'));
448  $this->addCol($this->lng->txt('ps_not_accepted'),$row,$col++);
449  }
450  }
451  else
452  {
453  #$this->csv->addColumn($this->lng->txt('ps_not_accepted'));
454  $this->addCol($this->lng->txt('ps_not_accepted'),$row,$col++);
455  }
456  break;
457 
458  // These fields are always enabled
459  case 'username':
460  #$this->csv->addColumn($this->user_profile_data[$usr_id]['login']);
461  $this->addCol($this->user_profile_data[$usr_id]['login'],$row,$col++);
462  break;
463 
464  case 'firstname':
465  case 'lastname':
466  #$this->csv->addColumn($this->user_profile_data[$usr_id][$field]);
467  $this->addCol($this->user_profile_data[$usr_id][$field],$row,$col++);
468  break;
469 
470  case 'consultation_hour':
471  include_once './Services/Booking/classes/class.ilBookingEntry.php';
472  $bookings = ilBookingEntry::lookupManagedBookingsForObject($this->obj_id, $GLOBALS['ilUser']->getId());
473 
474  $uts = array();
475  foreach((array) $bookings[$usr_id] as $ut)
476  {
479  new ilDateTime($ut['dt'],IL_CAL_UNIX),
480  new ilDateTime($ut['dtend'],IL_CAL_UNIX)
481  );
482  if(strlen($ut['explanation']))
483  {
484  $tmp .= ' '.$ut['explanation'];
485  }
486  $uts[] = $tmp;
487  }
488  $uts_str = implode(',',$uts);
489  $this->addCol($uts_str, $row, $col++);
490  break;
491  case 'crs_members_groups':
492  $groups = array();
493 
494  foreach(array_keys($this->groups) as $grp_ref)
495  {
496  if(in_array($usr_id, $this->groups_participants[$grp_ref])
497  && $this->groups_rights[$grp_ref])
498  {
499  $groups[] = $this->groups[$grp_ref];
500  }
501  }
502  $this->addCol(implode(", ", $groups), $row, $col++);
503  break;
504 
505  case 'org_units':
506  $this->addCol(ilObjUser::lookupOrgUnitsRepresentation($usr_id), $row, $col++);
507  break;
508 
509  default:
510  // Check aggreement
511  if(!$this->privacy->courseConfirmationRequired() or $this->agreement[$usr_id]['accepted'])
512  {
513  #$this->csv->addColumn($this->user_profile_data[$usr_id][$field]);
514  $this->addCol($this->user_profile_data[$usr_id][$field],$row,$col++);
515  }
516  else
517  {
518  #$this->csv->addColumn('');
519  $this->addCol('', $row, $col++);
520  }
521  break;
522 
523  }
524  }
525  #$this->csv->addRow();
526  $this->addRow();
527  }
528 
529  }
530 
531 
532 
539  private function fetchUsers()
540  {
542 
543  if($this->settings->enabled('admin'))
544  {
545  $this->user_ids = $tmp_ids = $this->members->getAdmins();
546  $this->readCourseData($tmp_ids);
547  }
548  if($this->settings->enabled('tutor'))
549  {
550  $this->user_ids = array_merge($tmp_ids = $this->members->getTutors(),$this->user_ids);
551  $this->readCourseData($tmp_ids);
552  }
553  if($this->settings->enabled('member'))
554  {
555  $this->user_ids = array_merge($tmp_ids = $this->members->getMembers(),$this->user_ids);
556  $this->readCourseData($tmp_ids);
557  }
558  if($this->settings->enabled('subscribers'))
559  {
560  $this->user_ids = array_merge($tmp_ids = $this->members->getSubscribers(),$this->user_ids);
561  $this->readCourseData($tmp_ids,'subscriber');
562  }
563  if($this->settings->enabled('waiting_list'))
564  {
565  include_once('Modules/Course/classes/class.ilCourseWaitingList.php');
566  $waiting_list = new ilCourseWaitingList($this->obj_id);
567  $this->user_ids = array_merge($waiting_list->getUserIds(),$this->user_ids);
568 
569  }
570  // Sort by lastname
571  $this->user_ids = ilUtil::_sortIds($this->user_ids,'usr_data','lastname','usr_id');
572 
573  // Finally read user profile data
574  $this->user_profile_data = ilObjUser::_readUsersProfileData($this->user_ids);
575  }
576 
583  private function readCourseData($a_user_ids,$a_status = 'member')
584  {
585  foreach($a_user_ids as $user_id)
586  {
587  // Read course related data
588  if($this->members->isAdmin($user_id))
589  {
590  $this->user_course_data[$user_id]['role'] = $this->getType() == 'crs' ? IL_CRS_ADMIN : IL_GRP_ADMIN;
591  }
592  elseif($this->members->isTutor($user_id))
593  {
594  $this->user_course_data[$user_id]['role'] = IL_CRS_TUTOR;
595  }
596  elseif($this->members->isMember($user_id))
597  {
598  $this->user_course_data[$user_id]['role'] = $this->getType() == 'crs' ? IL_CRS_MEMBER : IL_GRP_MEMBER;
599  }
600  else
601  {
602  $this->user_course_data[$user_id]['role'] = 'subscriber';
603  }
604  }
605  }
606 
614  private function readCourseSpecificFieldsData()
615  {
616  include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
617  $this->user_course_fields = ilCourseUserData::_getValuesByObjId($this->obj_id);
618  }
619 
629  private function addCourseField($a_usr_id,$a_field,$row,$col)
630  {
631  if(substr($a_field,0,4) != 'cdf_')
632  {
633  return false;
634  }
635  if(!$this->privacy->courseConfirmationRequired() or $this->agreement[$a_usr_id]['accepted'])
636  {
637  $field_info = explode('_',$a_field);
638  $field_id = $field_info[1];
639  $value = $this->user_course_fields[$a_usr_id][$field_id];
640  #$this->csv->addColumn($value);
641  $this->addCol($value, $row, $col);
642  return true;
643  }
644  #$this->csv->addColumn('');
645  $this->addCol('', $row, $col);
646  return true;
647 
648  }
649 
658  private function addUserDefinedField($udf_data,$a_field,$row,$col)
659  {
660  if(substr($a_field,0,4) != 'udf_')
661  {
662  return false;
663  }
664  if(!$this->privacy->courseConfirmationRequired() or $this->agreement[$udf_data->getUserId()]['accepted'])
665  {
666  $field_info = explode('_',$a_field);
667  $field_id = $field_info[1];
668  $value = $udf_data->get('f_'.$field_id);
669  #$this->csv->addColumn($value);
670  $this->addCol($value, $row, $col);
671  return true;
672  }
673  #$this->csv->addColumn('');
674  $this->addCol('', $row, $col);
675  }
676 
681  protected function initMembers()
682  {
683  if($this->getType() == 'crs')
684  {
685  $this->members = ilCourseParticipants::_getInstanceByObjId($this->getObjId());
686  }
687  if($this->getType() == 'grp')
688  {
689  $this->members = ilGroupParticipants::_getInstanceByObjId($this->getObjId());
690  }
691  return true;
692  }
693 
694  protected function initGroups()
695  {
696  global $tree, $ilAccess;
697  $parent_node = $tree->getNodeData($this->ref_id);
698  $groups = $tree->getSubTree($parent_node, true, "grp");
699  if(is_array($groups) && sizeof($groups))
700  {
701  include_once('./Modules/Group/classes/class.ilGroupParticipants.php');
702  $this->groups_rights = array();
703  foreach($groups as $idx => $group_data)
704  {
705  // check for group in group
706  if($group_data["parent"] != $this->ref_id && $tree->checkForParentType($group_data["ref_id"], "grp",true))
707  {
708  unset($groups[$idx]);
709  }
710  else
711  {
712  $this->groups[$group_data["ref_id"]] = $group_data["title"];
713  //TODO: change permissions from write to manage_members plus "|| ilObjGroup->getShowMembers()"----- uncomment below; testing required
714  //$obj = new ilObjGroup($group_data["ref_id"], true);
715  $this->groups_rights[$group_data["ref_id"]] = (bool)$ilAccess->checkAccess("write", "", $group_data["ref_id"])/* || $obj->getShowMembers()*/;
716  $gobj = ilGroupParticipants::_getInstanceByObjId($group_data["obj_id"]);
717  $this->groups_participants[$group_data["ref_id"]] = $gobj->getParticipants();
718  }
719  }
720  }
721  }
722 }
723 
724 
725 ?>
__construct($a_ref_id, $a_type=self::EXPORT_CSV)
Constructor.
Class for generation of member export files.
getCSVString()
toString method
const IL_CAL_DATETIME
static _getInstance()
Get instance.
Helper class to generate CSV files.
Class ilUserDefinedData.
createCSV()
Create CSV File.
Rename worksheet
readCourseData($a_user_ids, $a_status='member')
Read All User related course data.
getOrderedExportableFields()
Get ordered enabled fields.
const IL_GRP_ADMIN
static lookupManagedBookingsForObject($a_obj_id, $a_usr_id)
Lookup bookings for own and managed consultation hours of an object.
addUserDefinedField($udf_data, $a_field, $row, $col)
Add user defined fields.
static _getValuesByObjId($a_obj_id)
Get values by obj_id (for all users)
static formatPeriod(ilDateTime $start, ilDateTime $end)
Format a period of two date Shows: 14.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
getObjId()
Get obj id.
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
readCourseSpecificFieldsData()
Read course specific fields data.
static setUseRelativeDates($a_status)
set use relative dates
const IL_CRS_TUTOR
create()
Create Export File.
const IL_CAL_UNIX
const IL_GRP_MEMBER
static _readByObjId($a_obj_id)
Read user data by object id.
static _readUsersProfileData($a_user_ids)
STATIC METHOD get user data of selected users.
setFilename($a_file)
set filename
const IL_CRS_MEMBER
$a_type
Definition: workflow.php:93
getFilename()
get filename
static lookupOrgUnitsRepresentation($a_usr_id)
lokup org unit representation
static _sortIds($a_ids, $a_table, $a_field, $a_id_name)
Function that sorts ids by a given table field using WHERE IN E.g: __sort(array(6,7),&#39;usr_data&#39;,&#39;lastname&#39;,&#39;usr_id&#39;) => sorts by lastname.
static _getInstanceByType($a_type)
Get Singleton Instance.
addCol($a_value, $a_row, $a_col)
Write on column.
Date and time handling
const IL_CRS_ADMIN
Base class for course and group participants.
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
addCourseField($a_usr_id, $a_field, $row, $col)
fill course specific fields
getExportType()
get current export type
getType()
get obj type
settings()
Definition: settings.php:2
fetchUsers()
Fetch all users that will be exported.
initMembers()
Init member object.
static _getInstance()
Get instance of ilPrivacySettings.
static _lookupName($a_field_id)
Lookup field name.
static _getFields($a_container_id, $a_sort=IL_CDF_SORT_NAME)
Get all fields of a container.