ILIAS  release_8 Revision v8.23
class.ilMemberExport.php
Go to the documentation of this file.
1 <?php
18 declare(strict_types=1);
19 
27 {
28  public const EXPORT_CSV = 1;
29  public const EXPORT_EXCEL = 2;
30 
31  private int $ref_id;
32  private int $obj_id;
33  private string $type;
34  private int $export_type;
36  private array $groups = [];
37  private array $groups_participants = [];
38  private array $groups_rights = [];
39  private ?string $filename = null;
40 
41  protected ilLanguage $lng;
42  protected ilTree $tree;
44  protected array $agreement = [];
45 
48  protected ?ilCSVWriter $csv = null;
49  protected ?ilExcel $worksheet = null;
50 
51  private array $user_ids = array();
52  private array $user_course_data = array();
53  private array $user_course_fields = array();
54  private array $user_profile_data = array();
55 
56  public function __construct(int $a_ref_id, int $a_type = self::EXPORT_CSV)
57  {
58  global $DIC;
59 
60  $ilObjDataCache = $DIC['ilObjDataCache'];
61 
62  $this->lng = $DIC->language();
63  $this->tree = $DIC->repositoryTree();
64  $this->access = $DIC->access();
65 
66  $this->export_type = $a_type;
67  $this->ref_id = $a_ref_id;
68  $this->obj_id = $ilObjDataCache->lookupObjId($this->ref_id);
69  $this->type = ilObject::_lookupType($this->obj_id);
70 
71  $this->initMembers();
72  $this->initGroups();
73 
74  $this->agreement = ilMemberAgreement::_readByObjId($this->obj_id);
75  $this->settings = new ilUserFormSettings('memexp');
76  $this->privacy = ilPrivacySettings::getInstance();
77  }
78 
83  public function filterUsers(array $a_usr_ids): array
84  {
85  return $this->access->filterUserIdsByRbacOrPositionOfCurrentUser(
86  'manage_members',
87  'manage_members',
88  $this->ref_id,
89  $a_usr_ids
90  );
91  }
92 
93  public function setFilename(string $a_file): void
94  {
95  $this->filename = $a_file;
96  }
97 
98  public function getFilename(): ?string
99  {
100  return $this->filename;
101  }
102 
103  public function getRefId(): int
104  {
105  return $this->ref_id;
106  }
107 
108  public function getType(): string
109  {
110  return $this->type;
111  }
112 
113  public function getExportType(): int
114  {
115  return $this->export_type;
116  }
117 
118  public function getObjId(): int
119  {
120  return $this->obj_id;
121  }
122 
123  public function create(): void
124  {
125  $this->fetchUsers();
126  switch ($this->getExportType()) {
127  case self::EXPORT_CSV:
128  $this->createCSV();
129  break;
130 
131  case self::EXPORT_EXCEL:
132  $this->createExcel();
133  break;
134  }
135  }
136 
137  public function getCSVString(): ?string
138  {
139  if ($this->csv instanceof ilCSVWriter) {
140  return $this->csv->getCSVString();
141  }
142  return null;
143  }
144 
145  public function createExcel(): void
146  {
147  $this->worksheet = new ilExcel();
148  $this->worksheet->addSheet($this->lng->txt("members"));
149  $this->write();
150 
151  $this->worksheet->writeToFile($this->getFilename());
152  }
153 
154  public function createCSV(): void
155  {
156  $this->csv = new ilCSVWriter();
157  $this->write();
158  }
159 
163  protected function addCol(string $a_value, int $a_row, int $a_col): void
164  {
165  switch ($this->getExportType()) {
166  case self::EXPORT_CSV:
167  $this->csv->addColumn($a_value);
168  break;
169 
170  case self::EXPORT_EXCEL:
171  $this->worksheet->setCell($a_row + 1, $a_col, $a_value);
172  break;
173  }
174  }
175 
176  protected function addRow(): void
177  {
178  switch ($this->getExportType()) {
179  case self::EXPORT_CSV:
180  $this->csv->addRow();
181  break;
182 
183  case self::EXPORT_EXCEL:
184  break;
185  }
186  }
187 
188  protected function getOrderedExportableFields(): array
189  {
191  $field_info->sortExportFields();
192  $fields[] = 'role';
193  // Append agreement info
194  $privacy = ilPrivacySettings::getInstance();
195  if ($privacy->courseConfirmationRequired()) {
196  $fields[] = 'agreement';
197  }
198 
199  foreach ($field_info->getExportableFields() as $field) {
200  if ($this->settings->enabled($field)) {
201  $fields[] = $field;
202  }
203  }
204 
206  foreach ($udf->getCourseExportableFields() as $field_id => $udf_data) {
207  if ($this->settings->enabled('udf_' . $field_id)) {
208  $fields[] = 'udf_' . $field_id;
209  }
210  }
211 
212  // Add course specific fields
213  foreach (ilCourseDefinedFieldDefinition::_getFields($this->obj_id) as $field_obj) {
214  if ($this->settings->enabled('cdf_' . $field_obj->getId())) {
215  $fields[] = 'cdf_' . $field_obj->getId();
216  }
217  }
218  if ($this->settings->enabled('group_memberships')) {
219  $fields[] = 'crs_members_groups';
220  }
221  return $fields;
222  }
223 
224  protected function write(): void
225  {
226  // Add header line
227  $row = 0;
228  $col = 0;
229  foreach ($all_fields = $this->getOrderedExportableFields() as $field) {
230  switch ($field) {
231  case 'role':
232  $this->addCol($this->lng->txt($this->getType() . '_role_status'), $row, $col++);
233  break;
234  case 'agreement':
235  $this->addCol($this->lng->txt('ps_agreement_accepted'), $row, $col++);
236  break;
237  case 'consultation_hour':
238  $this->lng->loadLanguageModule('dateplaner');
239  $this->addCol($this->lng->txt('cal_ch_field_ch'), $row, $col++);
240  break;
241 
242  case 'org_units':
243  $this->addCol($this->lng->txt('org_units'), $row, $col++);
244  break;
245 
246  default:
247  if (strpos($field, 'udf_') === 0) {
248  $field_id = explode('_', $field);
250  $def = $udf->getDefinition((int) $field_id[1]);
251  #$this->csv->addColumn($def['field_name']);
252  $this->addCol($def['field_name'], $row, $col++);
253  } elseif (strpos($field, 'cdf_') === 0) {
254  $field_id = explode('_', $field);
255  #$this->csv->addColumn(ilCourseDefinedFieldDefinition::_lookupName($field_id[1]));
256  $this->addCol(ilCourseDefinedFieldDefinition::_lookupName((int) $field_id[1]), $row, $col++);
257  } elseif ($field === "username") {//User Name Presentation Guideline; username should be named login
258  $this->addCol($this->lng->txt("login"), $row, $col++);
259  } else {
260  #$this->csv->addColumn($this->lng->txt($field));
261  $this->addCol($this->lng->txt($field), $row, $col++);
262  }
263  break;
264  }
265  }
266  $this->addRow();
267  // Add user data
268  foreach ($this->user_ids as $usr_id) {
269  $row++;
270  $col = 0;
271  $usr_id = (int) $usr_id;
272 
273  $udf_data = new ilUserDefinedData($usr_id);
274  foreach ($all_fields as $field) {
275  // Handle course defined fields
276  if ($this->addUserDefinedField($udf_data, $field, $row, $col)) {
277  $col++;
278  continue;
279  }
280 
281  if ($this->addCourseField($usr_id, $field, $row, $col)) {
282  $col++;
283  continue;
284  }
285 
286  switch ($field) {
287  case 'role':
288  switch ($this->user_course_data[$usr_id]['role'] ?? '') {
290  $this->addCol($this->lng->txt('crs_admin'), $row, $col++);
291  break;
292 
294  $this->addCol($this->lng->txt('crs_tutor'), $row, $col++);
295  break;
296 
298  $this->addCol($this->lng->txt('crs_member'), $row, $col++);
299  break;
300 
302  $this->addCol($this->lng->txt('il_grp_admin'), $row, $col++);
303  break;
304 
306  $this->addCol($this->lng->txt('il_grp_member'), $row, $col++);
307  break;
308 
309  case 'subscriber':
310  $this->addCol($this->lng->txt($this->getType() . '_subscriber'), $row, $col++);
311  break;
312 
313  default:
314  $this->addCol($this->lng->txt('crs_waiting_list'), $row, $col++);
315  break;
316 
317  }
318  break;
319 
320  case 'agreement':
321  if (isset($this->agreement[$usr_id])) {
322  if ($this->agreement[$usr_id]['accepted']) {
323  $dt = new ilDateTime($this->agreement[$usr_id]['acceptance_time'], IL_CAL_UNIX);
324  $this->addCol($dt->get(IL_CAL_DATETIME), $row, $col++);
325  } else {
326  $this->addCol($this->lng->txt('ps_not_accepted'), $row, $col++);
327  }
328  } else {
329  $this->addCol($this->lng->txt('ps_not_accepted'), $row, $col++);
330  }
331  break;
332 
333  // These fields are always enabled
334  case 'username':
335  $this->addCol($this->user_profile_data[$usr_id]['login'], $row, $col++);
336  break;
337 
338  case 'firstname':
339  case 'lastname':
340  $this->addCol($this->user_profile_data[$usr_id][$field], $row, $col++);
341  break;
342 
343  case 'consultation_hour':
345  $this->obj_id,
346  $GLOBALS['DIC']['ilUser']->getId()
347  );
348 
349  $uts = array();
350  foreach ((array) $bookings[$usr_id] as $ut) {
353  new ilDateTime($ut['dt'], IL_CAL_UNIX),
354  new ilDateTime($ut['dtend'], IL_CAL_UNIX)
355  );
356  if (strlen($ut['explanation'])) {
357  $tmp .= ' ' . $ut['explanation'];
358  }
359  $uts[] = $tmp;
360  }
361  $uts_str = implode(',', $uts);
362  $this->addCol($uts_str, $row, $col++);
363  break;
364  case 'crs_members_groups':
365  $groups = array();
366 
367  foreach (array_keys($this->groups) as $grp_ref) {
368  if (in_array($usr_id, $this->groups_participants[$grp_ref])
369  && $this->groups_rights[$grp_ref]) {
370  $groups[] = $this->groups[$grp_ref];
371  }
372  }
373  $this->addCol(implode(", ", $groups), $row, $col++);
374  break;
375 
376  case 'org_units':
377  $this->addCol(ilObjUser::lookupOrgUnitsRepresentation($usr_id), $row, $col++);
378  break;
379 
380  default:
381  // Check aggreement
382  if (
383  !$this->privacy->courseConfirmationRequired() or
384  (isset($this->agreement[$usr_id]['accepted']) &&
385  $this->agreement[$usr_id]['accepted'])
386  ) {
387  #$this->csv->addColumn($this->user_profile_data[$usr_id][$field]);
388  $this->addCol($this->user_profile_data[$usr_id][$field] ?? '', $row, $col++);
389  } else {
390  #$this->csv->addColumn('');
391  $this->addCol('', $row, $col++);
392  }
393  break;
394 
395  }
396  }
397  $this->addRow();
398  }
399  }
400 
401  private function fetchUsers(): void
402  {
404 
405  if ($this->settings->enabled('admin')) {
406  $this->user_ids = $tmp_ids = $this->members->getAdmins();
407  $this->readCourseData($tmp_ids);
408  }
409  if ($this->settings->enabled('tutor')) {
410  $this->user_ids = array_merge($tmp_ids = $this->members->getTutors(), $this->user_ids);
411  $this->readCourseData($tmp_ids);
412  }
413  if ($this->settings->enabled('member')) {
414  $this->user_ids = array_merge($tmp_ids = $this->members->getMembers(), $this->user_ids);
415  $this->readCourseData($tmp_ids);
416  }
417  if ($this->settings->enabled('subscribers')) {
418  $this->user_ids = array_merge($tmp_ids = $this->members->getSubscribers(), $this->user_ids);
419  $this->readCourseData($tmp_ids);
420  }
421  if ($this->settings->enabled('waiting_list')) {
422  $waiting_list = new ilCourseWaitingList($this->obj_id);
423  $this->user_ids = array_merge($waiting_list->getUserIds(), $this->user_ids);
424  }
425  $this->user_ids = $this->filterUsers($this->user_ids);
426 
427  // Sort by lastname
428  $this->user_ids = ilUtil::_sortIds($this->user_ids, 'usr_data', 'lastname', 'usr_id');
429 
430  // Finally read user profile data
431  $this->user_profile_data = ilObjUser::_readUsersProfileData($this->user_ids);
432  }
433 
439  private function readCourseData(array $a_user_ids): void
440  {
441  foreach ($a_user_ids as $user_id) {
442  // Read course related data
443  if ($this->members->isAdmin($user_id)) {
444  $this->user_course_data[$user_id]['role'] = $this->getType() === 'crs' ? ilParticipants::IL_CRS_ADMIN : ilParticipants::IL_GRP_ADMIN;
445  } elseif ($this->members->isTutor($user_id)) {
446  $this->user_course_data[$user_id]['role'] = ilParticipants::IL_CRS_TUTOR;
447  } elseif ($this->members->isMember($user_id)) {
448  $this->user_course_data[$user_id]['role'] = $this->getType() === 'crs' ? ilParticipants::IL_CRS_MEMBER : ilParticipants::IL_GRP_MEMBER;
449  } else {
450  $this->user_course_data[$user_id]['role'] = 'subscriber';
451  }
452  }
453  }
454 
455  private function readCourseSpecificFieldsData(): void
456  {
457  $this->user_course_fields = ilCourseUserData::_getValuesByObjId($this->obj_id);
458  }
459 
463  private function addCourseField(int $a_usr_id, string $a_field, int $row, int $col): bool
464  {
465  if (strpos($a_field, 'cdf_') !== 0) {
466  return false;
467  }
468  if (
469  !$this->privacy->courseConfirmationRequired() ||
470  ($this->agreement[$a_usr_id]['accepted'] ?? false)
471  ) {
472  $field_info = explode('_', $a_field);
473  $field_id = $field_info[1] ?? 0;
474  $value = '';
475  if (isset($this->user_course_fields[$a_usr_id][$field_id])) {
476  $value = $this->user_course_fields[$a_usr_id][$field_id];
477  }
478  $this->addCol((string) $value, $row, $col);
479  return true;
480  }
481  #$this->csv->addColumn('');
482  $this->addCol('', $row, $col);
483  return true;
484  }
485 
489  private function addUserDefinedField(ilUserDefinedData $udf_data, string $a_field, int $row, int $col): bool
490  {
491  if (strpos($a_field, 'udf_') !== 0) {
492  return false;
493  }
494 
495  if (
496  !$this->privacy->courseConfirmationRequired() ||
497  (isset($this->agreement[$udf_data->getUserId()]['accepted']) &&
498  $this->agreement[$udf_data->getUserId()]['accepted'])
499  ) {
500  $field_info = explode('_', $a_field);
501  $field_id = $field_info[1];
502  $value = $udf_data->get('f_' . $field_id);
503  #$this->csv->addColumn($value);
504  $this->addCol($value, $row, $col);
505  return true;
506  }
507 
508  $this->addCol('', $row, $col);
509  return true;
510  }
511 
515  protected function initMembers(): void
516  {
517  if ($this->getType() === 'crs') {
518  $this->members = ilCourseParticipants::_getInstanceByObjId($this->getObjId());
519  }
520  if ($this->getType() === 'grp') {
521  $this->members = ilGroupParticipants::_getInstanceByObjId($this->getObjId());
522  }
523  }
524 
525  protected function initGroups(): void
526  {
527  $parent_node = $this->tree->getNodeData($this->ref_id);
528  $groups = $this->tree->getSubTree($parent_node, true, ['grp']);
529  if (is_array($groups) && count($groups)) {
530  $this->groups_rights = [];
531  foreach ($groups as $idx => $group_data) {
532  // check for group in group
533  if (
534  $group_data["parent"] != $this->ref_id &&
535  $this->tree->checkForParentType((int) $group_data["ref_id"], "grp", true)
536  ) {
537  unset($groups[$idx]);
538  } else {
539  $this->groups[$group_data["ref_id"]] = $group_data["title"];
540  //TODO: change permissions from write to manage_members plus "|| ilObjGroup->getShowMembers()"----- uncomment below; testing required
541  $this->groups_rights[$group_data["ref_id"]] =
542  $this->access->checkAccess("write", "", (int) $group_data["ref_id"]);
543  $gobj = ilGroupParticipants::_getInstanceByObjId((int) $group_data["obj_id"]);
544  $this->groups_participants[$group_data["ref_id"]] = $gobj->getParticipants();
545  }
546  }
547  }
548  }
549 }
__construct(int $a_ref_id, int $a_type=self::EXPORT_CSV)
Class for generation of member export files.
const IL_CAL_DATETIME
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getValuesByObjId(int $a_obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilUserFormSettings $settings
static _getFields(int $a_container_id, $a_sort=self::IL_CDF_SORT_NAME)
Get all fields of a container.
const IL_CAL_UNIX
static lookupOrgUnitsRepresentation(int $a_usr_id)
lookup org unit representation
global $DIC
Definition: feed.php:28
addUserDefinedField(ilUserDefinedData $udf_data, string $a_field, int $row, int $col)
Add user defined fields.
ilParticipants $members
static _getInstanceByObjId(int $a_obj_id)
setFilename(string $a_file)
ilPrivacySettings $privacy
readCourseData(array $a_user_ids)
Read All User related course data.
Singleton class that stores all privacy settings.
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
addCol(string $a_value, int $a_row, int $a_col)
Write one column.
static lookupManagedBookingsForObject(int $a_obj_id, int $a_usr_id)
Lookup bookings for own and managed consultation hours of an object.
static _sortIds(array $a_ids, string $a_table, string $a_field, string $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.
ilAccessHandler $access
Base class for course and group participants.
static _getInstanceByType(string $a_type)
Get Singleton Instance.
static formatPeriod(ilDateTime $start, ilDateTime $end, bool $a_skip_starting_day=false)
Format a period of two dates Shows: 14.
filterUsers(array $a_usr_ids)
static _getInstanceByObjId(int $a_obj_id)
Get singleton instance.
static _readUsersProfileData(array $a_user_ids)
static _readByObjId(int $a_obj_id)
Read user data by object id.
addCourseField(int $a_usr_id, string $a_field, int $row, int $col)
Fill course specific fields.
initMembers()
Init member object.
static _lookupType(int $id, bool $reference=false)
static setUseRelativeDates(bool $a_status)
set use relative dates