ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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*/
23include_once('Services/PrivacySecurity/classes/class.ilPrivacySettings.php');
24include_once('Services/Membership/classes/class.ilMemberAgreement.php');
25include_once('Modules/Course/classes/class.ilCourseParticipants.php');
26include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
27include_once('Services/User/classes/class.ilUserDefinedData.php');
28include_once('Services/User/classes/class.ilUserFormSettings.php');
29
30define("IL_MEMBER_EXPORT_CSV_FIELD_SEPERATOR", ',');
31define("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 $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
95
96 public function filterUsers($a_usr_ids)
97 {
98 return $GLOBALS['DIC']->access()->filterUserIdsByRbacOrPositionOfCurrentUser(
99 'manage_members',
100 'manage_members',
101 $this->ref_id,
102 $a_usr_ids
103 );
104 }
105
111 public function setFilename($a_file)
112 {
113 $this->filename = $a_file;
114 }
115
120 public function getFilename()
121 {
122 return $this->filename;
123 }
124
129 public function getRefId()
130 {
131 return $this->ref_id;
132 }
133
138 public function getType()
139 {
140 return $this->type;
141 }
142
147 public function getExportType()
148 {
149 return $this->export_type;
150 }
151
156 public function getObjId()
157 {
158 return $this->obj_id;
159 }
160
167 public function create()
168 {
169 $this->fetchUsers();
170
171 // DONE: Switch different export types
172 switch ($this->getExportType()) {
173 case self::EXPORT_CSV:
174 $this->createCSV();
175 break;
176
178 $this->createExcel();
179 break;
180 }
181 }
182
190 public function getCSVString()
191 {
192 return $this->csv->getCSVString();
193 }
194
195
200 public function createExcel()
201 {
202 include_once "./Services/Excel/classes/class.ilExcel.php";
203 $this->worksheet = new ilExcel();
204 $this->worksheet->addSheet($this->lng->txt("members"));
205
206 $this->write();
207
208 $this->worksheet->writeToFile($this->getFilename());
209 }
210
217 public function createCSV()
218 {
219 include_once('Services/Utilities/classes/class.ilCSVWriter.php');
220 $this->csv = new ilCSVWriter();
221
222 $this->write();
223 }
224
225
226
234 protected function addCol($a_value, $a_row, $a_col)
235 {
236 switch ($this->getExportType()) {
237 case self::EXPORT_CSV:
238 $this->csv->addColumn($a_value);
239 break;
240
242 $this->worksheet->setCell($a_row+1, $a_col, $a_value);
243 break;
244 }
245 }
246
251 protected function addRow()
252 {
253 switch ($this->getExportType()) {
254 case self::EXPORT_CSV:
255 $this->csv->addRow();
256 break;
257
259 break;
260 }
261 }
262
270 protected function getOrderedExportableFields()
271 {
272 include_once('Services/PrivacySecurity/classes/class.ilPrivacySettings.php');
273 include_once('Services/PrivacySecurity/classes/class.ilExportFieldsInfo.php');
274 include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
275 include_once('Services/User/classes/class.ilUserDefinedFields.php');
276
278 $field_info->sortExportFields();
279 $fields[] = 'role';
280 // Append agreement info
282 if ($privacy->courseConfirmationRequired()) {
283 $fields[] = 'agreement';
284 }
285
286 foreach ($field_info->getExportableFields() as $field) {
287 if ($this->settings->enabled($field)) {
288 $fields[] = $field;
289 }
290 }
291
293 foreach ($udf->getCourseExportableFields() as $field_id => $udf_data) {
294 if ($this->settings->enabled('udf_' . $field_id)) {
295 $fields[] = 'udf_' . $field_id;
296 }
297 }
298
299 // Add course specific fields
300 foreach (ilCourseDefinedFieldDefinition::_getFields($this->obj_id) as $field_obj) {
301 if ($this->settings->enabled('cdf_' . $field_obj->getId())) {
302 $fields[] = 'cdf_' . $field_obj->getId();
303 }
304 }
305 if ($this->settings->enabled('group_memberships')) {
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 switch ($field) {
323 case 'role':
324 #$this->csv->addColumn($this->lng->txt($this->getType().'_role_status'));
325 $this->addCol($this->lng->txt($this->getType() . '_role_status'), $row, $col++);
326 break;
327 case 'agreement':
328 #$this->csv->addColumn($this->lng->txt('ps_agreement_accepted'));
329 $this->addCol($this->lng->txt('ps_agreement_accepted'), $row, $col++);
330 break;
331 case 'consultation_hour':
332 $this->lng->loadLanguageModule('dateplaner');
333 $this->addCol($this->lng->txt('cal_ch_field_ch'), $row, $col++);
334 break;
335
336 case 'org_units':
337 $this->addCol($this->lng->txt('org_units'), $row, $col++);
338 break;
339
340 default:
341 if (substr($field, 0, 4) == 'udf_') {
342 $field_id = explode('_', $field);
343 include_once('Services/User/classes/class.ilUserDefinedFields.php');
345 $def = $udf->getDefinition($field_id[1]);
346 #$this->csv->addColumn($def['field_name']);
347 $this->addCol($def['field_name'], $row, $col++);
348 } elseif (substr($field, 0, 4) == 'cdf_') {
349 $field_id = explode('_', $field);
350 #$this->csv->addColumn(ilCourseDefinedFieldDefinition::_lookupName($field_id[1]));
351 $this->addCol(ilCourseDefinedFieldDefinition::_lookupName($field_id[1]), $row, $col++);
352 } elseif ($field == "username") {//User Name Presentation Guideline; username should be named login
353 $this->addCol($this->lng->txt("login"), $row, $col++);
354 } else {
355 #$this->csv->addColumn($this->lng->txt($field));
356 $this->addCol($this->lng->txt($field), $row, $col++);
357 }
358 break;
359 }
360 }
361 #$this->csv->addRow();
362 $this->addRow();
363 // Add user data
364 foreach ($this->user_ids as $usr_id) {
365 $row++;
366 $col = 0;
367
368 $udf_data = new ilUserDefinedData($usr_id);
369 foreach ($all_fields as $field) {
370 // Handle course defined fields
371 if ($this->addUserDefinedField($udf_data, $field, $row, $col)) {
372 $col++;
373 continue;
374 }
375
376 if ($this->addCourseField($usr_id, $field, $row, $col)) {
377 $col++;
378 continue;
379 }
380
381 switch ($field) {
382 case 'role':
383 switch ($this->user_course_data[$usr_id]['role']) {
384 case IL_CRS_ADMIN:
385 #$this->csv->addColumn($this->lng->txt('crs_admin'));
386 $this->addCol($this->lng->txt('crs_admin'), $row, $col++);
387 break;
388
389 case IL_CRS_TUTOR:
390 #$this->csv->addColumn($this->lng->txt('crs_tutor'));
391 $this->addCol($this->lng->txt('crs_tutor'), $row, $col++);
392 break;
393
394 case IL_CRS_MEMBER:
395 #$this->csv->addColumn($this->lng->txt('crs_member'));
396 $this->addCol($this->lng->txt('crs_member'), $row, $col++);
397 break;
398
399 case IL_GRP_ADMIN:
400 #$this->csv->addColumn($this->lng->txt('il_grp_admin'));
401 $this->addCol($this->lng->txt('il_grp_admin'), $row, $col++);
402 break;
403
404 case IL_GRP_MEMBER:
405 #$this->csv->addColumn($this->lng->txt('il_grp_member'));
406 $this->addCol($this->lng->txt('il_grp_member'), $row, $col++);
407 break;
408
409 case 'subscriber':
410 #$this->csv->addColumn($this->lng->txt($this->getType().'_subscriber'));
411 $this->addCol($this->lng->txt($this->getType() . '_subscriber'), $row, $col++);
412 break;
413
414 default:
415 #$this->csv->addColumn($this->lng->txt('crs_waiting_list'));
416 $this->addCol($this->lng->txt('crs_waiting_list'), $row, $col++);
417 break;
418
419 }
420 break;
421
422 case 'agreement':
423 if (isset($this->agreement[$usr_id])) {
424 if ($this->agreement[$usr_id]['accepted']) {
425 #$this->csv->addColumn(il-Format::format-Unix-Time($this->agreement[$usr_id]['acceptance_time'],true));
426 $dt = new ilDateTime($this->agreement[$usr_id]['acceptance_time'], IL_CAL_UNIX);
427 $this->addCol($dt->get(IL_CAL_DATETIME), $row, $col++);
428 } else {
429 #$this->csv->addColumn($this->lng->txt('ps_not_accepted'));
430 $this->addCol($this->lng->txt('ps_not_accepted'), $row, $col++);
431 }
432 } else {
433 #$this->csv->addColumn($this->lng->txt('ps_not_accepted'));
434 $this->addCol($this->lng->txt('ps_not_accepted'), $row, $col++);
435 }
436 break;
437
438 // These fields are always enabled
439 case 'username':
440 #$this->csv->addColumn($this->user_profile_data[$usr_id]['login']);
441 $this->addCol($this->user_profile_data[$usr_id]['login'], $row, $col++);
442 break;
443
444 case 'firstname':
445 case 'lastname':
446 #$this->csv->addColumn($this->user_profile_data[$usr_id][$field]);
447 $this->addCol($this->user_profile_data[$usr_id][$field], $row, $col++);
448 break;
449
450 case 'consultation_hour':
451 include_once './Services/Booking/classes/class.ilBookingEntry.php';
452 $bookings = ilBookingEntry::lookupManagedBookingsForObject($this->obj_id, $GLOBALS['ilUser']->getId());
453
454 $uts = array();
455 foreach ((array) $bookings[$usr_id] as $ut) {
458 new ilDateTime($ut['dt'], IL_CAL_UNIX),
459 new ilDateTime($ut['dtend'], IL_CAL_UNIX)
460 );
461 if (strlen($ut['explanation'])) {
462 $tmp .= ' ' . $ut['explanation'];
463 }
464 $uts[] = $tmp;
465 }
466 $uts_str = implode(',', $uts);
467 $this->addCol($uts_str, $row, $col++);
468 break;
469 case 'crs_members_groups':
470 $groups = array();
471
472 foreach (array_keys($this->groups) as $grp_ref) {
473 if (in_array($usr_id, $this->groups_participants[$grp_ref])
474 && $this->groups_rights[$grp_ref]) {
475 $groups[] = $this->groups[$grp_ref];
476 }
477 }
478 $this->addCol(implode(", ", $groups), $row, $col++);
479 break;
480
481 case 'org_units':
482 $this->addCol(ilObjUser::lookupOrgUnitsRepresentation($usr_id), $row, $col++);
483 break;
484
485 default:
486 // Check aggreement
487 if (!$this->privacy->courseConfirmationRequired() or $this->agreement[$usr_id]['accepted']) {
488 #$this->csv->addColumn($this->user_profile_data[$usr_id][$field]);
489 $this->addCol($this->user_profile_data[$usr_id][$field], $row, $col++);
490 } else {
491 #$this->csv->addColumn('');
492 $this->addCol('', $row, $col++);
493 }
494 break;
495
496 }
497 }
498 #$this->csv->addRow();
499 $this->addRow();
500 }
501 }
502
503
504
511 private function fetchUsers()
512 {
514
515 if ($this->settings->enabled('admin')) {
516 $this->user_ids = $tmp_ids = $this->members->getAdmins();
517 $this->readCourseData($tmp_ids);
518 }
519 if ($this->settings->enabled('tutor')) {
520 $this->user_ids = array_merge($tmp_ids = $this->members->getTutors(), $this->user_ids);
521 $this->readCourseData($tmp_ids);
522 }
523 if ($this->settings->enabled('member')) {
524 $this->user_ids = array_merge($tmp_ids = $this->members->getMembers(), $this->user_ids);
525 $this->readCourseData($tmp_ids);
526 }
527 if ($this->settings->enabled('subscribers')) {
528 $this->user_ids = array_merge($tmp_ids = $this->members->getSubscribers(), $this->user_ids);
529 $this->readCourseData($tmp_ids, 'subscriber');
530 }
531 if ($this->settings->enabled('waiting_list')) {
532 include_once('Modules/Course/classes/class.ilCourseWaitingList.php');
533 $waiting_list = new ilCourseWaitingList($this->obj_id);
534 $this->user_ids = array_merge($waiting_list->getUserIds(), $this->user_ids);
535 }
536 $this->user_ids = $this->filterUsers($this->user_ids);
537
538 // Sort by lastname
539 $this->user_ids = ilUtil::_sortIds($this->user_ids, 'usr_data', 'lastname', 'usr_id');
540
541 // Finally read user profile data
542 $this->user_profile_data = ilObjUser::_readUsersProfileData($this->user_ids);
543 }
544
551 private function readCourseData($a_user_ids, $a_status = 'member')
552 {
553 foreach ($a_user_ids as $user_id) {
554 // Read course related data
555 if ($this->members->isAdmin($user_id)) {
556 $this->user_course_data[$user_id]['role'] = $this->getType() == 'crs' ? IL_CRS_ADMIN : IL_GRP_ADMIN;
557 } elseif ($this->members->isTutor($user_id)) {
558 $this->user_course_data[$user_id]['role'] = IL_CRS_TUTOR;
559 } elseif ($this->members->isMember($user_id)) {
560 $this->user_course_data[$user_id]['role'] = $this->getType() == 'crs' ? IL_CRS_MEMBER : IL_GRP_MEMBER;
561 } else {
562 $this->user_course_data[$user_id]['role'] = 'subscriber';
563 }
564 }
565 }
566
575 {
576 include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
577 $this->user_course_fields = ilCourseUserData::_getValuesByObjId($this->obj_id);
578 }
579
589 private function addCourseField($a_usr_id, $a_field, $row, $col)
590 {
591 if (substr($a_field, 0, 4) != 'cdf_') {
592 return false;
593 }
594 if (!$this->privacy->courseConfirmationRequired() or $this->agreement[$a_usr_id]['accepted']) {
595 $field_info = explode('_', $a_field);
596 $field_id = $field_info[1];
597 $value = $this->user_course_fields[$a_usr_id][$field_id];
598 #$this->csv->addColumn($value);
599 $this->addCol($value, $row, $col);
600 return true;
601 }
602 #$this->csv->addColumn('');
603 $this->addCol('', $row, $col);
604 return true;
605 }
606
615 private function addUserDefinedField($udf_data, $a_field, $row, $col)
616 {
617 if (substr($a_field, 0, 4) != 'udf_') {
618 return false;
619 }
620 if (!$this->privacy->courseConfirmationRequired() or $this->agreement[$udf_data->getUserId()]['accepted']) {
621 $field_info = explode('_', $a_field);
622 $field_id = $field_info[1];
623 $value = $udf_data->get('f_' . $field_id);
624 #$this->csv->addColumn($value);
625 $this->addCol($value, $row, $col);
626 return true;
627 }
628 #$this->csv->addColumn('');
629 $this->addCol('', $row, $col);
630 }
631
636 protected function initMembers()
637 {
638 if ($this->getType() == 'crs') {
639 $this->members = ilCourseParticipants::_getInstanceByObjId($this->getObjId());
640 }
641 if ($this->getType() == 'grp') {
642 $this->members = ilGroupParticipants::_getInstanceByObjId($this->getObjId());
643 }
644 return true;
645 }
646
647 protected function initGroups()
648 {
649 global $tree, $ilAccess;
650 $parent_node = $tree->getNodeData($this->ref_id);
651 $groups = $tree->getSubTree($parent_node, true, "grp");
652 if (is_array($groups) && sizeof($groups)) {
653 include_once('./Modules/Group/classes/class.ilGroupParticipants.php');
654 $this->groups_rights = array();
655 foreach ($groups as $idx => $group_data) {
656 // check for group in group
657 if ($group_data["parent"] != $this->ref_id && $tree->checkForParentType($group_data["ref_id"], "grp", true)) {
658 unset($groups[$idx]);
659 } else {
660 $this->groups[$group_data["ref_id"]] = $group_data["title"];
661 //TODO: change permissions from write to manage_members plus "|| ilObjGroup->getShowMembers()"----- uncomment below; testing required
662 //$obj = new ilObjGroup($group_data["ref_id"], true);
663 $this->groups_rights[$group_data["ref_id"]] = (bool) $ilAccess->checkAccess("write", "", $group_data["ref_id"])/* || $obj->getShowMembers()*/;
664 $gobj = ilGroupParticipants::_getInstanceByObjId($group_data["obj_id"]);
665 $this->groups_participants[$group_data["ref_id"]] = $gobj->getParticipants();
666 }
667 }
668 }
669 }
670}
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
const IL_CAL_DATETIME
const IL_CRS_ADMIN
Base class for course and group participants.
const IL_CRS_MEMBER
const IL_GRP_MEMBER
const IL_CRS_TUTOR
const IL_GRP_ADMIN
static lookupManagedBookingsForObject($a_obj_id, $a_usr_id)
Lookup bookings for own and managed consultation hours of an object.
Helper class to generate CSV files.
static _getFields($a_container_id, $a_sort=IL_CDF_SORT_NAME)
Get all fields of a container.
static _lookupName($a_field_id)
Lookup field name.
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
static _getValuesByObjId($a_obj_id)
Get values by obj_id (for all users)
static setUseRelativeDates($a_status)
set use relative dates
static formatPeriod(ilDateTime $start, ilDateTime $end, $a_skip_starting_day=false)
Format a period of two date Shows: 14.
@classDescription Date and time handling
static _getInstanceByType($a_type)
Get Singleton Instance.
static _getInstanceByObjId($a_obj_id)
Get singleton instance.
static _readByObjId($a_obj_id)
Read user data by object id.
Class for generation of member export files.
__construct($a_ref_id, $a_type=self::EXPORT_CSV)
Constructor.
addCol($a_value, $a_row, $a_col)
Write on column.
addUserDefinedField($udf_data, $a_field, $row, $col)
Add user defined fields.
readCourseData($a_user_ids, $a_status='member')
Read All User related course data.
getCSVString()
toString method
fetchUsers()
Fetch all users that will be exported.
initMembers()
Init member object.
setFilename($a_file)
set filename
create()
Create Export File.
getFilename()
get filename
createCSV()
Create CSV File.
getExportType()
get current export type
readCourseSpecificFieldsData()
Read course specific fields data.
addCourseField($a_usr_id, $a_field, $row, $col)
fill course specific fields
getOrderedExportableFields()
Get ordered enabled fields.
static lookupOrgUnitsRepresentation($a_usr_id)
lokup org unit representation
static _readUsersProfileData($a_user_ids)
STATIC METHOD get user data of selected users.
static _lookupType($a_id, $a_reference=false)
lookup object type
static _getInstance()
Get instance of ilPrivacySettings.
Class ilUserDefinedData.
static _getInstance()
Get instance.
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,...
$def
Definition: croninfo.php:21
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
settings()
Definition: settings.php:2
$a_type
Definition: workflow.php:92