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