ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
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('Modules/Course/classes/Export/class.ilExportUserSettings.php');
24 include_once('Services/PrivacySecurity/classes/class.ilPrivacySettings.php');
25 include_once('Services/Membership/classes/class.ilMemberAgreement.php');
26 include_once('Modules/Course/classes/class.ilCourseParticipants.php');
27 include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
28 include_once('Services/User/classes/class.ilUserDefinedData.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 
53  private $lng;
54 
55  private $settings;
56 
57  private $export_type = null;
58  private $filename = null;
59 
60  private $user_ids = array();
61  private $user_course_data = array();
62  private $user_course_fields = array();
63  private $user_profile_data = array();
64  private $privacy;
65 
72  public function __construct($a_ref_id, $a_type = self::EXPORT_CSV)
73  {
74  global $ilUser,$ilObjDataCache,$lng;
75 
76  $this->lng = $lng;
77 
78  $this->export_type = $a_type;
79 
80  $this->ref_id = $a_ref_id;
81  $this->obj_id = $ilObjDataCache->lookupObjId($this->ref_id);
82  $this->type = ilObject::_lookupType($this->obj_id);
83 
84  $this->initMembers();
85 
86  $this->agreement = ilMemberAgreement::_readByObjId($this->obj_id);
87  $this->settings = new ilExportUserSettings($ilUser->getId(),$this->obj_id);
88  $this->privacy = ilPrivacySettings::_getInstance();
89  }
90 
96  public function setFilename($a_file)
97  {
98  $this->filename = $a_file;
99  }
100 
105  public function getFilename()
106  {
107  return $this->filename;
108  }
109 
114  public function getRefId()
115  {
116  return $this->ref_id;
117  }
118 
123  public function getType()
124  {
125  return $this->type;
126  }
127 
132  public function getExportType()
133  {
134  return $this->export_type;
135  }
136 
141  public function getObjId()
142  {
143  return $this->obj_id;
144  }
145 
152  public function create()
153  {
154  $this->fetchUsers();
155 
156  // DONE: Switch different export types
157  switch($this->getExportType())
158  {
159  case self::EXPORT_CSV:
160  $this->createCSV();
161  break;
162 
163  case self::EXPORT_EXCEL:
164  $this->createExcel();
165  break;
166  }
167  }
168 
176  public function getCSVString()
177  {
178  return $this->csv->getCSVString();
179  }
180 
181 
186  public function createExcel()
187  {
188  include_once "./Services/Excel/classes/class.ilExcelUtils.php";
189  include_once "./Services/Excel/classes/class.ilExcelWriterAdapter.php";
190  $adapter = new ilExcelWriterAdapter($this->getFilename(), false);
191  $workbook = $adapter->getWorkbook();
192  $this->worksheet = $workbook->addWorksheet();
193  $this->write();
194  $workbook->close();
195  }
196 
203  public function createCSV()
204  {
205  include_once('Services/Utilities/classes/class.ilCSVWriter.php');
206  $this->csv = new ilCSVWriter();
207 
208  $this->write();
209  }
210 
211 
212 
220  protected function addCol($a_value,$a_row,$a_col)
221  {
222  switch($this->getExportType())
223  {
224  case self::EXPORT_CSV:
225  $this->csv->addColumn($a_value);
226  break;
227 
228  case self::EXPORT_EXCEL:
229  $this->worksheet->write($a_row,$a_col,$a_value);
230  break;
231  }
232  }
233 
238  protected function addRow()
239  {
240  switch($this->getExportType())
241  {
242  case self::EXPORT_CSV:
243  $this->csv->addRow();
244  break;
245 
246  case self::EXPORT_EXCEL:
247  break;
248  }
249  }
250 
255  protected function write()
256  {
257  // Add header line
258  $row = 0;
259  $col = 0;
260  foreach($all_fields = $this->settings->getOrderedExportableFields() as $field)
261  {
262  switch($field)
263  {
264  case 'role':
265  #$this->csv->addColumn($this->lng->txt($this->getType().'_role_status'));
266  $this->addCol($this->lng->txt($this->getType().'_role_status'), $row, $col++);
267  break;
268  case 'agreement':
269  #$this->csv->addColumn($this->lng->txt('ps_agreement_accepted'));
270  $this->addCol($this->lng->txt('ps_agreement_accepted'), $row, $col++);
271  break;
272  default:
273  if(substr($field,0,4) == 'udf_')
274  {
275  $field_id = explode('_',$field);
276  include_once('Services/User/classes/class.ilUserDefinedFields.php');
278  $def = $udf->getDefinition($field_id[1]);
279  #$this->csv->addColumn($def['field_name']);
280  $this->addCol($def['field_name'], $row, $col++);
281  }
282  elseif(substr($field,0,4) == 'cdf_')
283  {
284  $field_id = explode('_',$field);
285  #$this->csv->addColumn(ilCourseDefinedFieldDefinition::_lookupName($field_id[1]));
286  $this->addCol(ilCourseDefinedFieldDefinition::_lookupName($field_id[1]),$row,$col++);
287  }
288  else
289  {
290  #$this->csv->addColumn($this->lng->txt($field));
291  $this->addCol($this->lng->txt($field), $row, $col++);
292  }
293  break;
294  }
295  }
296  #$this->csv->addRow();
297  $this->addRow();
298  // Add user data
299  foreach($this->user_ids as $usr_id)
300  {
301  $row++;
302  $col = 0;
303 
304  $udf_data = new ilUserDefinedData($usr_id);
305  foreach($all_fields as $field)
306  {
307  // Handle course defined fields
308  if($this->addUserDefinedField($udf_data,$field,$row,$col))
309  {
310  $col++;
311  continue;
312  }
313 
314  if($this->addCourseField($usr_id,$field,$row,$col))
315  {
316  $col++;
317  continue;
318  }
319 
320  switch($field)
321  {
322  case 'role':
323  switch($this->user_course_data[$usr_id]['role'])
324  {
325  case IL_CRS_ADMIN:
326  #$this->csv->addColumn($this->lng->txt('crs_admin'));
327  $this->addCol($this->lng->txt('crs_admin'), $row, $col++);
328  break;
329 
330  case IL_CRS_TUTOR:
331  #$this->csv->addColumn($this->lng->txt('crs_tutor'));
332  $this->addCol($this->lng->txt('crs_tutor'), $row, $col++);
333  break;
334 
335  case IL_CRS_MEMBER:
336  #$this->csv->addColumn($this->lng->txt('crs_member'));
337  $this->addCol($this->lng->txt('crs_member'), $row, $col++);
338  break;
339 
340  case IL_GRP_ADMIN:
341  #$this->csv->addColumn($this->lng->txt('il_grp_admin'));
342  $this->addCol($this->lng->txt('il_grp_admin'), $row, $col++);
343  break;
344 
345  case IL_GRP_MEMBER:
346  #$this->csv->addColumn($this->lng->txt('il_grp_member'));
347  $this->addCol($this->lng->txt('il_grp_member'), $row, $col++);
348  break;
349 
350  case 'subscriber':
351  #$this->csv->addColumn($this->lng->txt($this->getType().'_subscriber'));
352  $this->addCol($this->lng->txt($this->getType().'_subscriber'), $row, $col++);
353  break;
354 
355  default:
356  #$this->csv->addColumn($this->lng->txt('crs_waiting_list'));
357  $this->addCol($this->lng->txt('crs_waiting_list'), $row, $col++);
358  break;
359 
360  }
361  break;
362 
363  case 'agreement':
364  if(isset($this->agreement[$usr_id]))
365  {
366  if($this->agreement[$usr_id]['accepted'])
367  {
368  #$this->csv->addColumn(ilFormat::formatUnixTime($this->agreement[$usr_id]['acceptance_time'],true));
369  $this->addCol(ilFormat::formatUnixTime($this->agreement[$usr_id]['acceptance_time'],true),$row,$col++);
370  }
371  else
372  {
373  #$this->csv->addColumn($this->lng->txt('ps_not_accepted'));
374  $this->addCol($this->lng->txt('ps_not_accepted'),$row,$col++);
375  }
376  }
377  else
378  {
379  #$this->csv->addColumn($this->lng->txt('ps_not_accepted'));
380  $this->addCol($this->lng->txt('ps_not_accepted'),$row,$col++);
381  }
382  break;
383 
384  // These fields are always enabled
385  case 'username':
386  #$this->csv->addColumn($this->user_profile_data[$usr_id]['login']);
387  $this->addCol($this->user_profile_data[$usr_id]['login'],$row,$col++);
388  break;
389 
390  case 'firstname':
391  case 'lastname':
392  #$this->csv->addColumn($this->user_profile_data[$usr_id][$field]);
393  $this->addCol($this->user_profile_data[$usr_id][$field],$row,$col++);
394  break;
395 
396  default:
397  // Check aggreement
398  if((!$this->privacy->courseConfirmationRequired() and !ilCourseDefinedFieldDefinition::_getFields($this->obj_id))
399  or $this->agreement[$usr_id]['accepted'])
400  {
401  #$this->csv->addColumn($this->user_profile_data[$usr_id][$field]);
402  $this->addCol($this->user_profile_data[$usr_id][$field],$row,$col++);
403  }
404  else
405  {
406  #$this->csv->addColumn('');
407  $this->addCol('', $row, $col++);
408  }
409  break;
410 
411  }
412  }
413  #$this->csv->addRow();
414  $this->addRow();
415  }
416 
417  }
418 
419 
420 
427  private function fetchUsers()
428  {
430 
431  if($this->settings->enabled('admin'))
432  {
433  $this->user_ids = $tmp_ids = $this->members->getAdmins();
434  $this->readCourseData($tmp_ids);
435  }
436  if($this->settings->enabled('tutor'))
437  {
438  $this->user_ids = array_merge($tmp_ids = $this->members->getTutors(),$this->user_ids);
439  $this->readCourseData($tmp_ids);
440  }
441  if($this->settings->enabled('member'))
442  {
443  $this->user_ids = array_merge($tmp_ids = $this->members->getMembers(),$this->user_ids);
444  $this->readCourseData($tmp_ids);
445  }
446  if($this->settings->enabled('subscribers'))
447  {
448  $this->user_ids = array_merge($tmp_ids = $this->members->getSubscribers(),$this->user_ids);
449  $this->readCourseData($tmp_ids,'subscriber');
450  }
451  if($this->settings->enabled('waiting_list'))
452  {
453  include_once('Modules/Course/classes/class.ilCourseWaitingList.php');
454  $waiting_list = new ilCourseWaitingList($this->obj_id);
455  $this->user_ids = array_merge($waiting_list->getUserIds(),$this->user_ids);
456 
457  }
458  // Sort by lastname
459  $this->user_ids = ilUtil::_sortIds($this->user_ids,'usr_data','lastname','usr_id');
460 
461  // Finally read user profile data
462  $this->user_profile_data = ilObjUser::_readUsersProfileData($this->user_ids);
463  }
464 
471  private function readCourseData($a_user_ids,$a_status = 'member')
472  {
473  foreach($a_user_ids as $user_id)
474  {
475  // Read course related data
476  if($this->members->isAdmin($user_id))
477  {
478  $this->user_course_data[$user_id]['role'] = $this->getType() == 'crs' ? IL_CRS_ADMIN : IL_GRP_ADMIN;
479  }
480  elseif($this->members->isTutor($user_id))
481  {
482  $this->user_course_data[$user_id]['role'] = IL_CRS_TUTOR;
483  }
484  elseif($this->members->isMember($user_id))
485  {
486  $this->user_course_data[$user_id]['role'] = $this->getType() == 'crs' ? IL_CRS_MEMBER : IL_GRP_MEMBER;
487  }
488  else
489  {
490  $this->user_course_data[$user_id]['role'] = 'subscriber';
491  }
492  }
493  }
494 
502  private function readCourseSpecificFieldsData()
503  {
504  include_once('Modules/Course/classes/Export/class.ilCourseUserData.php');
505  $this->user_course_fields = ilCourseUserData::_getValuesByObjId($this->obj_id);
506  }
507 
517  private function addCourseField($a_usr_id,$a_field,$row,$col)
518  {
519  if(substr($a_field,0,4) != 'cdf_')
520  {
521  return false;
522  }
523  if((!$this->privacy->courseConfirmationRequired() and ilCourseDefinedFieldDefinition::_getFields($this->obj_id))
524  or $this->agreement[$a_usr_id]['accepted'])
525  {
526  $field_info = explode('_',$a_field);
527  $field_id = $field_info[1];
528  $value = $this->user_course_fields[$a_usr_id][$field_id];
529  #$this->csv->addColumn($value);
530  $this->addCol($value, $row, $col);
531  return true;
532  }
533  #$this->csv->addColumn('');
534  $this->addCol('', $row, $col);
535  return true;
536 
537  }
538 
547  private function addUserDefinedField($udf_data,$a_field,$row,$col)
548  {
549  if(substr($a_field,0,4) != 'udf_')
550  {
551  return false;
552  }
553  if(!$this->privacy->courseConfirmationRequired() or $this->agreement[$udf_data->getUserId()]['accepted'])
554  {
555  $field_info = explode('_',$a_field);
556  $field_id = $field_info[1];
557  $value = $udf_data->get('f_'.$field_id);
558  #$this->csv->addColumn($value);
559  $this->addCol($value, $row, $col);
560  return true;
561  }
562  #$this->csv->addColumn('');
563  $this->addCol('', $row, $col);
564  }
565 
570  protected function initMembers()
571  {
572  if($this->getType() == 'crs')
573  {
574  $this->members = ilCourseParticipants::_getInstanceByObjId($this->getObjId());
575  }
576  if($this->getType() == 'grp')
577  {
578  $this->members = ilGroupParticipants::_getInstanceByObjId($this->getObjId());
579  }
580  return true;
581  }
582 }
583 
584 
585 ?>