ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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();
53  private $groups_participants = 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();
65  private $user_course_fields = array();
66  private $user_profile_data = array();
67  private $privacy;
68 
75  public function __construct($a_ref_id, $a_type = self::EXPORT_CSV)
76  {
77  global $DIC;
78 
79  $ilObjDataCache = $DIC['ilObjDataCache'];
80  $lng = $DIC['lng'];
81 
82  $this->lng = $lng;
83 
84  $this->export_type = $a_type;
85 
86  $this->ref_id = $a_ref_id;
87  $this->obj_id = $ilObjDataCache->lookupObjId($this->ref_id);
88  $this->type = ilObject::_lookupType($this->obj_id);
89 
90  $this->initMembers();
91  $this->initGroups();
92 
93  $this->agreement = ilMemberAgreement::_readByObjId($this->obj_id);
94  $this->settings = new ilUserFormSettings('memexp');
95  $this->privacy = ilPrivacySettings::_getInstance();
96  }
97 
98 
99  public function filterUsers($a_usr_ids)
100  {
101  return $GLOBALS['DIC']->access()->filterUserIdsByRbacOrPositionOfCurrentUser(
102  'manage_members',
103  'manage_members',
104  $this->ref_id,
105  $a_usr_ids
106  );
107  }
108 
114  public function setFilename($a_file)
115  {
116  $this->filename = $a_file;
117  }
118 
123  public function getFilename()
124  {
125  return $this->filename;
126  }
127 
132  public function getRefId()
133  {
134  return $this->ref_id;
135  }
136 
141  public function getType()
142  {
143  return $this->type;
144  }
145 
150  public function getExportType()
151  {
152  return $this->export_type;
153  }
154 
159  public function getObjId()
160  {
161  return $this->obj_id;
162  }
163 
170  public function create()
171  {
172  $this->fetchUsers();
173 
174  // DONE: Switch different export types
175  switch ($this->getExportType()) {
176  case self::EXPORT_CSV:
177  $this->createCSV();
178  break;
179 
180  case self::EXPORT_EXCEL:
181  $this->createExcel();
182  break;
183  }
184  }
185 
193  public function getCSVString()
194  {
195  return $this->csv->getCSVString();
196  }
197 
198 
203  public function createExcel()
204  {
205  include_once "./Services/Excel/classes/class.ilExcel.php";
206  $this->worksheet = new ilExcel();
207  $this->worksheet->addSheet($this->lng->txt("members"));
208 
209  $this->write();
210 
211  $this->worksheet->writeToFile($this->getFilename());
212  }
213 
220  public function createCSV()
221  {
222  include_once('Services/Utilities/classes/class.ilCSVWriter.php');
223  $this->csv = new ilCSVWriter();
224 
225  $this->write();
226  }
227 
228 
229 
237  protected function addCol($a_value, $a_row, $a_col)
238  {
239  switch ($this->getExportType()) {
240  case self::EXPORT_CSV:
241  $this->csv->addColumn($a_value);
242  break;
243 
244  case self::EXPORT_EXCEL:
245  $this->worksheet->setCell($a_row + 1, $a_col, $a_value);
246  break;
247  }
248  }
249 
254  protected function addRow()
255  {
256  switch ($this->getExportType()) {
257  case self::EXPORT_CSV:
258  $this->csv->addRow();
259  break;
260 
261  case self::EXPORT_EXCEL:
262  break;
263  }
264  }
265 
273  protected function getOrderedExportableFields()
274  {
275  include_once('Services/PrivacySecurity/classes/class.ilPrivacySettings.php');
276  include_once('Services/PrivacySecurity/classes/class.ilExportFieldsInfo.php');
277  include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
278  include_once('Services/User/classes/class.ilUserDefinedFields.php');
279 
281  $field_info->sortExportFields();
282  $fields[] = 'role';
283  // Append agreement info
285  if ($privacy->courseConfirmationRequired()) {
286  $fields[] = 'agreement';
287  }
288 
289  foreach ($field_info->getExportableFields() as $field) {
290  if ($this->settings->enabled($field)) {
291  $fields[] = $field;
292  }
293  }
294 
296  foreach ($udf->getCourseExportableFields() as $field_id => $udf_data) {
297  if ($this->settings->enabled('udf_' . $field_id)) {
298  $fields[] = 'udf_' . $field_id;
299  }
300  }
301 
302  // Add course specific fields
303  foreach (ilCourseDefinedFieldDefinition::_getFields($this->obj_id) as $field_obj) {
304  if ($this->settings->enabled('cdf_' . $field_obj->getId())) {
305  $fields[] = 'cdf_' . $field_obj->getId();
306  }
307  }
308  if ($this->settings->enabled('group_memberships')) {
309  $fields[] = 'crs_members_groups';
310  }
311 
312  return $fields ? $fields : array();
313  }
314 
319  protected function write()
320  {
321  // Add header line
322  $row = 0;
323  $col = 0;
324  foreach ($all_fields = $this->getOrderedExportableFields() as $field) {
325  switch ($field) {
326  case 'role':
327  #$this->csv->addColumn($this->lng->txt($this->getType().'_role_status'));
328  $this->addCol($this->lng->txt($this->getType() . '_role_status'), $row, $col++);
329  break;
330  case 'agreement':
331  #$this->csv->addColumn($this->lng->txt('ps_agreement_accepted'));
332  $this->addCol($this->lng->txt('ps_agreement_accepted'), $row, $col++);
333  break;
334  case 'consultation_hour':
335  $this->lng->loadLanguageModule('dateplaner');
336  $this->addCol($this->lng->txt('cal_ch_field_ch'), $row, $col++);
337  break;
338 
339  case 'org_units':
340  $this->addCol($this->lng->txt('org_units'), $row, $col++);
341  break;
342 
343  default:
344  if (substr($field, 0, 4) == 'udf_') {
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  } elseif (substr($field, 0, 4) == 'cdf_') {
352  $field_id = explode('_', $field);
353  #$this->csv->addColumn(ilCourseDefinedFieldDefinition::_lookupName($field_id[1]));
354  $this->addCol(ilCourseDefinedFieldDefinition::_lookupName($field_id[1]), $row, $col++);
355  } elseif ($field == "username") {//User Name Presentation Guideline; username should be named login
356  $this->addCol($this->lng->txt("login"), $row, $col++);
357  } else {
358  #$this->csv->addColumn($this->lng->txt($field));
359  $this->addCol($this->lng->txt($field), $row, $col++);
360  }
361  break;
362  }
363  }
364  #$this->csv->addRow();
365  $this->addRow();
366  // Add user data
367  foreach ($this->user_ids as $usr_id) {
368  $row++;
369  $col = 0;
370 
371  $udf_data = new ilUserDefinedData($usr_id);
372  foreach ($all_fields as $field) {
373  // Handle course defined fields
374  if ($this->addUserDefinedField($udf_data, $field, $row, $col)) {
375  $col++;
376  continue;
377  }
378 
379  if ($this->addCourseField($usr_id, $field, $row, $col)) {
380  $col++;
381  continue;
382  }
383 
384  switch ($field) {
385  case 'role':
386  switch ($this->user_course_data[$usr_id]['role']) {
387  case IL_CRS_ADMIN:
388  #$this->csv->addColumn($this->lng->txt('crs_admin'));
389  $this->addCol($this->lng->txt('crs_admin'), $row, $col++);
390  break;
391 
392  case IL_CRS_TUTOR:
393  #$this->csv->addColumn($this->lng->txt('crs_tutor'));
394  $this->addCol($this->lng->txt('crs_tutor'), $row, $col++);
395  break;
396 
397  case IL_CRS_MEMBER:
398  #$this->csv->addColumn($this->lng->txt('crs_member'));
399  $this->addCol($this->lng->txt('crs_member'), $row, $col++);
400  break;
401 
402  case IL_GRP_ADMIN:
403  #$this->csv->addColumn($this->lng->txt('il_grp_admin'));
404  $this->addCol($this->lng->txt('il_grp_admin'), $row, $col++);
405  break;
406 
407  case IL_GRP_MEMBER:
408  #$this->csv->addColumn($this->lng->txt('il_grp_member'));
409  $this->addCol($this->lng->txt('il_grp_member'), $row, $col++);
410  break;
411 
412  case 'subscriber':
413  #$this->csv->addColumn($this->lng->txt($this->getType().'_subscriber'));
414  $this->addCol($this->lng->txt($this->getType() . '_subscriber'), $row, $col++);
415  break;
416 
417  default:
418  #$this->csv->addColumn($this->lng->txt('crs_waiting_list'));
419  $this->addCol($this->lng->txt('crs_waiting_list'), $row, $col++);
420  break;
421 
422  }
423  break;
424 
425  case 'agreement':
426  if (isset($this->agreement[$usr_id])) {
427  if ($this->agreement[$usr_id]['accepted']) {
428  #$this->csv->addColumn(il-Format::format-Unix-Time($this->agreement[$usr_id]['acceptance_time'],true));
429  $dt = new ilDateTime($this->agreement[$usr_id]['acceptance_time'], IL_CAL_UNIX);
430  $this->addCol($dt->get(IL_CAL_DATETIME), $row, $col++);
431  } else {
432  #$this->csv->addColumn($this->lng->txt('ps_not_accepted'));
433  $this->addCol($this->lng->txt('ps_not_accepted'), $row, $col++);
434  }
435  } else {
436  #$this->csv->addColumn($this->lng->txt('ps_not_accepted'));
437  $this->addCol($this->lng->txt('ps_not_accepted'), $row, $col++);
438  }
439  break;
440 
441  // These fields are always enabled
442  case 'username':
443  #$this->csv->addColumn($this->user_profile_data[$usr_id]['login']);
444  $this->addCol($this->user_profile_data[$usr_id]['login'], $row, $col++);
445  break;
446 
447  case 'firstname':
448  case 'lastname':
449  #$this->csv->addColumn($this->user_profile_data[$usr_id][$field]);
450  $this->addCol($this->user_profile_data[$usr_id][$field], $row, $col++);
451  break;
452 
453  case 'consultation_hour':
454  include_once './Services/Booking/classes/class.ilBookingEntry.php';
455  $bookings = ilBookingEntry::lookupManagedBookingsForObject($this->obj_id, $GLOBALS['DIC']['ilUser']->getId());
456 
457  $uts = array();
458  foreach ((array) $bookings[$usr_id] as $ut) {
461  new ilDateTime($ut['dt'], IL_CAL_UNIX),
462  new ilDateTime($ut['dtend'], IL_CAL_UNIX)
463  );
464  if (strlen($ut['explanation'])) {
465  $tmp .= ' ' . $ut['explanation'];
466  }
467  $uts[] = $tmp;
468  }
469  $uts_str = implode(',', $uts);
470  $this->addCol($uts_str, $row, $col++);
471  break;
472  case 'crs_members_groups':
473  $groups = array();
474 
475  foreach (array_keys($this->groups) as $grp_ref) {
476  if (in_array($usr_id, $this->groups_participants[$grp_ref])
477  && $this->groups_rights[$grp_ref]) {
478  $groups[] = $this->groups[$grp_ref];
479  }
480  }
481  $this->addCol(implode(", ", $groups), $row, $col++);
482  break;
483 
484  case 'org_units':
485  $this->addCol(ilObjUser::lookupOrgUnitsRepresentation($usr_id), $row, $col++);
486  break;
487 
488  default:
489  // Check aggreement
490  if (!$this->privacy->courseConfirmationRequired() or $this->agreement[$usr_id]['accepted']) {
491  #$this->csv->addColumn($this->user_profile_data[$usr_id][$field]);
492  $this->addCol($this->user_profile_data[$usr_id][$field], $row, $col++);
493  } else {
494  #$this->csv->addColumn('');
495  $this->addCol('', $row, $col++);
496  }
497  break;
498 
499  }
500  }
501  #$this->csv->addRow();
502  $this->addRow();
503  }
504  }
505 
506 
507 
514  private function fetchUsers()
515  {
517 
518  if ($this->settings->enabled('admin')) {
519  $this->user_ids = $tmp_ids = $this->members->getAdmins();
520  $this->readCourseData($tmp_ids);
521  }
522  if ($this->settings->enabled('tutor')) {
523  $this->user_ids = array_merge($tmp_ids = $this->members->getTutors(), $this->user_ids);
524  $this->readCourseData($tmp_ids);
525  }
526  if ($this->settings->enabled('member')) {
527  $this->user_ids = array_merge($tmp_ids = $this->members->getMembers(), $this->user_ids);
528  $this->readCourseData($tmp_ids);
529  }
530  if ($this->settings->enabled('subscribers')) {
531  $this->user_ids = array_merge($tmp_ids = $this->members->getSubscribers(), $this->user_ids);
532  $this->readCourseData($tmp_ids, 'subscriber');
533  }
534  if ($this->settings->enabled('waiting_list')) {
535  include_once('Modules/Course/classes/class.ilCourseWaitingList.php');
536  $waiting_list = new ilCourseWaitingList($this->obj_id);
537  $this->user_ids = array_merge($waiting_list->getUserIds(), $this->user_ids);
538  }
539  $this->user_ids = $this->filterUsers($this->user_ids);
540 
541  // Sort by lastname
542  $this->user_ids = ilUtil::_sortIds($this->user_ids, 'usr_data', 'lastname', 'usr_id');
543 
544  // Finally read user profile data
545  $this->user_profile_data = ilObjUser::_readUsersProfileData($this->user_ids);
546  }
547 
554  private function readCourseData($a_user_ids, $a_status = 'member')
555  {
556  foreach ($a_user_ids as $user_id) {
557  // Read course related data
558  if ($this->members->isAdmin($user_id)) {
559  $this->user_course_data[$user_id]['role'] = $this->getType() == 'crs' ? IL_CRS_ADMIN : IL_GRP_ADMIN;
560  } elseif ($this->members->isTutor($user_id)) {
561  $this->user_course_data[$user_id]['role'] = IL_CRS_TUTOR;
562  } elseif ($this->members->isMember($user_id)) {
563  $this->user_course_data[$user_id]['role'] = $this->getType() == 'crs' ? IL_CRS_MEMBER : IL_GRP_MEMBER;
564  } else {
565  $this->user_course_data[$user_id]['role'] = 'subscriber';
566  }
567  }
568  }
569 
577  private function readCourseSpecificFieldsData()
578  {
579  include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
580  $this->user_course_fields = ilCourseUserData::_getValuesByObjId($this->obj_id);
581  }
582 
592  private function addCourseField($a_usr_id, $a_field, $row, $col)
593  {
594  if (substr($a_field, 0, 4) != 'cdf_') {
595  return false;
596  }
597  if (!$this->privacy->courseConfirmationRequired() or $this->agreement[$a_usr_id]['accepted']) {
598  $field_info = explode('_', $a_field);
599  $field_id = $field_info[1];
600  $value = $this->user_course_fields[$a_usr_id][$field_id];
601  #$this->csv->addColumn($value);
602  $this->addCol($value, $row, $col);
603  return true;
604  }
605  #$this->csv->addColumn('');
606  $this->addCol('', $row, $col);
607  return true;
608  }
609 
618  private function addUserDefinedField($udf_data, $a_field, $row, $col)
619  {
620  if (substr($a_field, 0, 4) != 'udf_') {
621  return false;
622  }
623 
624  if (!$this->privacy->courseConfirmationRequired() or $this->agreement[$udf_data->getUserId()]['accepted']) {
625  $field_info = explode('_', $a_field);
626  $field_id = $field_info[1];
627  $value = $udf_data->get('f_' . $field_id);
628  #$this->csv->addColumn($value);
629  $this->addCol($value, $row, $col);
630  return true;
631  }
632 
633  $this->addCol('', $row, $col);
634  return true;
635  }
636 
641  protected function initMembers()
642  {
643  if ($this->getType() == 'crs') {
644  $this->members = ilCourseParticipants::_getInstanceByObjId($this->getObjId());
645  }
646  if ($this->getType() == 'grp') {
647  $this->members = ilGroupParticipants::_getInstanceByObjId($this->getObjId());
648  }
649  return true;
650  }
651 
652  protected function initGroups()
653  {
654  global $DIC;
655 
656  $tree = $DIC['tree'];
657  $ilAccess = $DIC['ilAccess'];
658  $parent_node = $tree->getNodeData($this->ref_id);
659  $groups = $tree->getSubTree($parent_node, true, "grp");
660  if (is_array($groups) && sizeof($groups)) {
661  include_once('./Modules/Group/classes/class.ilGroupParticipants.php');
662  $this->groups_rights = array();
663  foreach ($groups as $idx => $group_data) {
664  // check for group in group
665  if ($group_data["parent"] != $this->ref_id && $tree->checkForParentType($group_data["ref_id"], "grp", true)) {
666  unset($groups[$idx]);
667  } else {
668  $this->groups[$group_data["ref_id"]] = $group_data["title"];
669  //TODO: change permissions from write to manage_members plus "|| ilObjGroup->getShowMembers()"----- uncomment below; testing required
670  //$obj = new ilObjGroup($group_data["ref_id"], true);
671  $this->groups_rights[$group_data["ref_id"]] = (bool) $ilAccess->checkAccess("write", "", $group_data["ref_id"])/* || $obj->getShowMembers()*/;
672  $gobj = ilGroupParticipants::_getInstanceByObjId($group_data["obj_id"]);
673  $this->groups_participants[$group_data["ref_id"]] = $gobj->getParticipants();
674  }
675  }
676  }
677  }
678 }
__construct($a_ref_id, $a_type=self::EXPORT_CSV)
Constructor.
Class for generation of member export files.
getCSVString()
toString method
settings()
Definition: settings.php:2
const IL_CAL_DATETIME
static _getInstance()
Get instance.
Helper class to generate CSV files.
Class ilUserDefinedData.
createCSV()
Create CSV File.
readCourseData($a_user_ids, $a_status='member')
Read All User related course data.
getOrderedExportableFields()
Get ordered enabled fields.
global $DIC
Definition: saml.php:7
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)
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:92
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.
static _lookupType($a_id, $a_reference=false)
lookup object type
addCourseField($a_usr_id, $a_field, $row, $col)
fill course specific fields
$row
getExportType()
get current export type
getType()
get obj type
static formatPeriod(ilDateTime $start, ilDateTime $end, $a_skip_starting_day=false)
Format a period of two date Shows: 14.
$def
Definition: croninfo.php:21
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.
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.
static _getFields($a_container_id, $a_sort=IL_CDF_SORT_NAME)
Get all fields of a container.