ILIAS  trunk Revision v11.0_alpha-1843-g9e1fad99175
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilAttendanceList.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
28  protected ilLogger $logger;
29  protected ilLanguage $lng;
32  protected object $parent_gui;
33  protected ilObject $parent_obj;
39  protected $callback;
40  protected array $presets = [];
41  protected array $role_data = [];
42  protected array $roles = [];
43  protected bool $has_local_role = false;
44  protected array $blank_columns = [];
45  protected string $title = '';
46  protected string $description = '';
47  protected array $pre_blanks = [];
48  protected string $id = '';
49  protected bool $include_waiting_list = false;
50  protected bool $include_subscribers = false;
51  protected array $user_filters = [];
52 
53  public function __construct(
54  object $a_parent_gui,
55  ilObject $a_parent_obj,
56  ?ilParticipants $a_participants_object = null,
57  ?ilWaitingList $a_waiting_list = null
58  ) {
59  global $DIC;
60 
61  $this->logger = $DIC->logger()->mmbr();
62  $this->lng = $DIC->language();
63  $this->ctrl = $DIC->ctrl();
64  $this->tpl = $DIC->ui()->mainTemplate();
65 
66  $this->parent_gui = $a_parent_gui;
67  $this->parent_obj = $a_parent_obj;
68  $this->participants = $a_participants_object;
69  $this->waiting_list = $a_waiting_list;
70 
71  // always available
72  $this->presets['name'] = array($DIC->language()->txt('name'), true);
73  $this->presets['login'] = array($DIC->language()->txt('login'), true);
74 
75  // add exportable fields
77 
78  $DIC->language()->loadLanguageModule('crs');
79 
80  // roles
81  $roles = $this->participants->getRoles();
82 
83  foreach ($roles as $role_id) {
84  $title = ilObject::_lookupTitle($role_id);
85  switch (substr($title, 0, 8)) {
86  case 'il_crs_a':
87  case 'il_grp_a':
88  case 'il_lso_a':
89  $this->addRole($role_id, $DIC->language()->txt('event_tbl_admin'), 'admin');
90  break;
91 
92  case 'il_crs_t':
93  $this->addRole($role_id, $DIC->language()->txt('event_tbl_tutor'), 'tutor');
94  break;
95 
96  case 'il_crs_m':
97  case 'il_grp_m':
98  case 'il_sess_':
99  case 'il_lso_m':
100  $this->addRole($role_id, $DIC->language()->txt('event_tbl_member'), 'member');
101  break;
102 
103  // local
104  default:
105  $this->has_local_role = true;
106  $this->addRole($role_id, $title, 'local');
107  break;
108  }
109  }
110  }
111 
115  protected function readOrderedExportableFields(): bool
116  {
117  $field_info = ilExportFieldsInfo::_getInstanceByType($this->parent_obj->getType());
118  $field_info->sortExportFields();
119 
120  foreach ($field_info->getExportableFields() as $field) {
121  switch ($field) {
122  case 'username':
123  case 'firstname':
124  case 'lastname':
125  continue 2;
126  }
127 
128  // Check if default enabled
129  $this->presets[$field] = array(
130  $GLOBALS['DIC']['lng']->txt($field),
131  false
132  );
133  }
134 
135  // add udf fields
137  foreach ($udf->getExportableFields($this->parent_obj->getId()) as $field_id => $udf_data) {
138  $this->presets['udf_' . $field_id] = array(
139  $udf_data['field_name'],
140  false
141  );
142  }
143 
144  // add cdf fields
145  foreach (ilCourseDefinedFieldDefinition::_getFields($this->parent_obj->getId()) as $field_obj) {
146  $this->presets['cdf_' . $field_obj->getId()] = array(
147  $field_obj->getName(),
148  false
149  );
150  }
151  return true;
152  }
153 
157  public function addPreset(string $a_id, string $a_caption, bool $a_selected = false): void
158  {
159  $this->presets[$a_id] = array($a_caption, $a_selected);
160  }
161 
165  public function addBlank(string $a_caption): void
166  {
167  $this->pre_blanks[] = $a_caption;
168  }
169 
173  public function setTitle(string $a_title, ?string $a_description = null): void
174  {
175  $this->title = $a_title;
176  $this->description = (string) $a_description;
177  }
178 
182  protected function addRole(int $a_id, string $a_caption, string $a_type): void
183  {
184  $this->role_data[$a_id] = array($a_caption, $a_type);
185  }
186 
187  protected function setRoleSelection(array $a_role_ids): void
188  {
189  $this->roles = $a_role_ids;
190  }
191 
195  public function addUserFilter(string $a_id, string $a_caption, bool $a_checked = false): void
196  {
197  $this->user_filters[$a_id] = array($a_caption, $a_checked);
198  }
199 
203  public function getNonMemberUserData(array &$a_res): void
204  {
205  $subscriber_ids = $this->participants->getSubscribers();
206  $user_ids = $subscriber_ids;
207  if ($this->waiting_list) {
208  $user_ids = array_merge($user_ids, $this->waiting_list->getUserIds());
209  }
210 
211  // Finally read user profile data
212  $profile_data = ilObjUser::_readUsersProfileData($user_ids);
213  foreach ($profile_data as $user_id => $fields) {
214  foreach ((array) $fields as $field => $value) {
215  $a_res[$user_id][$field] = $value;
216  }
217  }
218 
220 
221  foreach ($udf->getExportableFields($this->parent_obj->getId()) as $field_id => $udf_data) {
222  foreach ($profile_data as $user_id => $field) {
223  $udf_data = new ilUserDefinedData($user_id);
224  $a_res[$user_id]['udf_' . $field_id] = $udf_data->get('f_' . $field_id);
225  }
226  }
227 
228  if (count($user_ids)) {
229  // object specific user data
230  $cdfs = ilCourseUserData::_getValuesByObjId($this->parent_obj->getId());
231  foreach (array_unique($user_ids) as $user_id) {
232  if ($tmp_obj = ilObjectFactory::getInstanceByObjId($user_id, false)) {
233  $a_res[$user_id]['login'] = $tmp_obj->getLogin();
234  $a_res[$user_id]['name'] = $tmp_obj->getLastname() . ', ' . $tmp_obj->getFirstname();
235 
236  if (in_array($user_id, $subscriber_ids)) {
237  $a_res[$user_id]['status'] = $this->lng->txt('crs_subscriber');
238  } else {
239  $a_res[$user_id]['status'] = $this->lng->txt('crs_waiting_list');
240  }
241 
242  foreach ((array) ($cdfs[$user_id] ?? []) as $field_id => $value) {
243  $a_res[$user_id]['cdf_' . $field_id] = (string) $value;
244  }
245  }
246  }
247  }
248  }
249 
253  public function setBlankColumns(array $a_values): void
254  {
255  if (!implode("", $a_values)) {
256  $a_values = array();
257  } else {
258  foreach ($a_values as $idx => $value) {
259  $a_values[$idx] = trim($value);
260  if ($a_values[$idx] == "") {
261  unset($a_values[$idx]);
262  }
263  }
264  }
265  $this->blank_columns = $a_values;
266  }
267 
271  public function setCallback(callable $a_callback): void
272  {
273  $this->callback = $a_callback;
274  }
275 
276  public function setId(string $a_value): void
277  {
278  $this->id = $a_value;
279  }
280 
284  public function initForm(string $a_cmd = ""): ilPropertyFormGUI
285  {
286  $this->lng->loadLanguageModule('crs');
287 
288  $form = new ilPropertyFormGUI();
289  $form->setFormAction($this->ctrl->getFormAction($this->parent_gui, $a_cmd));
290  $form->setPreventDoubleSubmission(false);
291  $form->setTitle($this->lng->txt('sess_gen_attendance_list'));
292 
293  $title = new ilTextInputGUI($this->lng->txt('title'), 'title');
294  $title->setValue($this->title);
295  $form->addItem($title);
296 
297  $desc = new ilTextInputGUI($this->lng->txt('description'), 'desc');
298  $desc->setValue($this->description);
299  $form->addItem($desc);
300 
301  if (count($this->presets)) {
302  $preset = new ilCheckboxGroupInputGUI($this->lng->txt('user_detail'), 'preset');
303  $preset_value = array();
304  foreach ($this->presets as $id => $item) {
305  $preset->addOption(new ilCheckboxOption($item[0], $id));
306  if ($item[1]) {
307  $preset_value[] = $id;
308  }
309  }
310  $preset->setValue($preset_value);
311  $form->addItem($preset);
312  }
313 
314  $blank = new ilTextInputGUI($this->lng->txt('event_blank_columns'), 'blank');
315  $blank->setMulti(true);
316  $form->addItem($blank);
317 
318  if ($this->pre_blanks) {
319  $blank->setValue($this->pre_blanks);
320  }
321 
322  $checked = array();
323 
324  $chk_grp = new ilCheckboxGroupInputGUI($this->lng->txt('event_user_selection'), 'selection_of_users');
325 
326  // participants by roles
327  foreach ($this->role_data as $role_id => $role_data) {
328  $title = ilObject::_lookupTitle($role_id);
329 
330  $role_name = $role_id;
331  if (strpos($title, 'il_' . $this->parent_obj->getType() . '_adm') === 0) {
332  $role_name = 'adm';
333  }
334  if (strpos($title, 'il_' . $this->parent_obj->getType() . '_mem') === 0) {
335  $role_name = 'mem';
336  }
337  if (strpos($title, 'il_' . $this->parent_obj->getType() . '_tut') === 0) {
338  $role_name = 'tut';
339  }
340 
341  $chk = new ilCheckboxOption(
342  sprintf($this->lng->txt('event_user_selection_include_role'), $role_data[0]),
343  'role_' . $role_name
344  );
345  $checked[] = 'role_' . $role_name;
346  $chk_grp->addOption($chk);
347  }
348 
349  if ($this->waiting_list) {
350  $chk = new ilCheckboxOption($this->lng->txt('event_user_selection_include_requests'), 'subscr');
351  $chk_grp->addOption($chk);
352 
353  $chk = new ilCheckboxOption($this->lng->txt('event_user_selection_include_waiting_list'), 'wlist');
354  $chk_grp->addOption($chk);
355  }
356 
357  if ($this->user_filters) {
358  foreach ($this->user_filters as $sub_id => $sub_item) {
359  $chk = new ilCheckboxOption(
360  sprintf($this->lng->txt('event_user_selection_include_filter'), $sub_item[0]),
361  'members_' . $sub_id
362  );
363  if ($sub_item[1]) {
364  $checked[] = 'members_' . $sub_id;
365  }
366  $chk_grp->addOption($chk);
367  }
368  }
369  $chk_grp->setValue($checked);
370  $form->addItem($chk_grp);
371 
372  $form->addCommandButton($a_cmd, $this->lng->txt('sess_print_attendance_list'));
373 
374  if ($this->id && $a_cmd) {
375  $settings = new ilUserFormSettings($this->id);
376  if (!$settings->hasStoredEntry()) {
377  $settings = new ilUserFormSettings($this->parent_obj->getType() . 's_pview', -1);
378  }
379 
380  $settings->deleteValue('desc'); // #11340
381  $settings->exportToForm($form);
382  }
383  return $form;
384  }
385 
389  public function initFromForm(): void
390  {
391  $form = $this->initForm();
392  if ($form->checkInput()) {
393  foreach (array_keys($this->presets) as $id) {
394  $this->presets[$id][1] = false;
395  }
396  foreach ((array) $form->getInput('preset') as $value) {
397  if (isset($this->presets[$value])) {
398  $this->presets[$value][1] = true;
399  } else {
400  $this->addPreset($value, $value, true);
401  }
402  }
403 
404  $this->setTitle($form->getInput('title'), $form->getInput('desc'));
405  $this->setBlankColumns($form->getInput('blank'));
406 
407  $selection_of_users = (array) $form->getInput('selection_of_users'); // #18238
408 
409  $roles = array();
410  foreach (array_keys($this->role_data) as $role_id) {
411  $title = ilObject::_lookupTitle($role_id);
412  $role_name = $role_id;
413  if (strpos($title, 'il_' . $this->parent_obj->getType() . '_adm') === 0) {
414  $role_name = 'adm';
415  }
416  if (strpos($title, 'il_' . $this->parent_obj->getType() . '_mem') === 0) {
417  $role_name = 'mem';
418  }
419  if (strpos($title, 'il_' . $this->parent_obj->getType() . '_tut') === 0) {
420  $role_name = 'tut';
421  }
422 
423  if (in_array('role_' . $role_name, $selection_of_users)) {
424  $roles[] = $role_id;
425  }
426  }
427  $this->setRoleSelection($roles);
428 
429  // not in sessions
430  if ($this->waiting_list) {
431  $this->include_subscribers = in_array('subscr', $selection_of_users);
432  $this->include_waiting_list = in_array('wlist', $selection_of_users);
433  }
434 
435  if ($this->user_filters) {
436  foreach (array_keys($this->user_filters) as $msub_id) {
437  $this->user_filters[$msub_id][2] = in_array("members_" . $msub_id, $selection_of_users);
438  }
439  }
440 
441  if ($this->id) {
442  #$form->setValuesByPost();
443 
444  #$settings = new ilUserFormSettings($this->id);
445  #$settings->deleteValue('desc'); // #11340
446  #$settings->importFromForm($form);
447  #$settings->store();
448  }
449  }
450  }
451 
455  public function initFromSettings(): void
456  {
457  $settings = new ilUserFormSettings(
458  $this->parent_obj->getType() . 's_pview_' . $this->parent_obj->getId(),
459  -1
460  );
461  if (!$settings->hasStoredEntry()) {
462  // init from global defaults
463  $settings = new ilUserFormSettings($this->parent_obj->getType() . 's_pview', -1);
464  }
465  $settings->deleteValue('desc'); // #11340
466 
467  // title and description
468  $this->setTitle(
469  (string) ($settings->getValue('title') ?? $this->title),
470  (string) ($settings->getValue('desc') ?? $this->description)
471  );
472 
473  // preset
474  if ($preset_value = $settings->getValue('preset')) {
475  foreach (array_keys($this->presets) as $id) {
476  $this->presets[$id][1] = false;
477  }
478  foreach ((array) $preset_value as $value) {
479  if (isset($this->presets[$value])) {
480  $this->presets[$value][1] = true;
481  } else {
482  $this->addPreset($value, $value, true);
483  }
484  }
485  }
486 
487  // blank
488  $this->setBlankColumns(
489  (array) ($settings->getValue('blank') ?? $this->pre_blanks)
490  );
491 
492  // selection of users
493  $selection_of_users = $settings->getValue('selection_of_users');
494 
495  // participants by roles
496  $roles = [];
497  foreach ($this->role_data as $role_id => $role_data) {
498  $title = ilObject::_lookupTitle($role_id);
499 
500  $role_name = $role_id;
501  if (strpos($title, 'il_' . $this->parent_obj->getType() . '_adm') === 0) {
502  $role_name = 'adm';
503  }
504  if (strpos($title, 'il_' . $this->parent_obj->getType() . '_mem') === 0) {
505  $role_name = 'mem';
506  }
507  if (strpos($title, 'il_' . $this->parent_obj->getType() . '_tut') === 0) {
508  $role_name = 'tut';
509  }
510 
511  if (
512  isset($selection_of_users) &&
513  !in_array('role_' . $role_name, $selection_of_users)
514  ) {
515  continue;
516  }
517  $roles[] = $role_id;
518  }
519  $this->setRoleSelection($roles);
520 
521  // waiting list and subscribers (not in sessions)
522  if ($this->waiting_list && isset($selection_of_users)) {
523  $this->include_subscribers = in_array('subscr', $selection_of_users);
524  $this->include_waiting_list = in_array('wlist', $selection_of_users);
525  }
526 
527  // user filters
528  foreach (array_keys($this->user_filters) as $msub_id) {
529  if (isset($selection_of_users)) {
530  $this->user_filters[$msub_id][2] = in_array("members_" . $msub_id, $selection_of_users);
531  continue;
532  }
533  if ($this->user_filters[$msub_id][1]) {
534  $this->user_filters[$msub_id][2] = true;
535  }
536  }
537  }
538 
542  public function getFullscreenHTML(): void
543  {
544  $this->tpl->setContent($this->getHTML());
545  $this->tpl->addOnLoadCode("il.Util.print();");
546  }
547 
551  public function getHTML(): string
552  {
553  $tpl = new ilTemplate('tpl.attendance_list_print.html', true, true, 'components/ILIAS/Membership');
556 
557  $tpl->setVariable('TXT_TITLE', $this->title);
558  if ($this->description) {
559  $tpl->setVariable('TXT_DESCRIPTION', $this->description . " (" . $time . ")");
560  } else {
561  $tpl->setVariable('TXT_DESCRIPTION', $time);
562  }
563 
564  $tpl->setCurrentBlock('head_item');
565  foreach ($this->presets as $item) {
566  if ($item[1]) {
567  $tpl->setVariable('TXT_HEAD', $item[0]);
568  $tpl->parseCurrentBlock();
569  }
570  }
571 
572  if ($this->blank_columns) {
573  foreach ($this->blank_columns as $blank) {
574  $tpl->setVariable('TXT_HEAD', $blank);
575  $tpl->parseCurrentBlock();
576  }
577  }
578 
579  // handle members
580 
581  $valid_user_ids = $filters = array();
582 
583  if ($this->roles) {
584  if ($this->has_local_role) {
585  $members = array();
586  foreach ($this->participants->getMembers() as $member_id) {
587  foreach ($this->participants->getAssignedRoles($member_id) as $role_id) {
588  $members[$role_id][] = $member_id;
589  }
590  }
591  } else {
592  $members = $this->participants->getMembers();
593  }
594 
595  foreach ($this->roles as $role_id) {
596  switch ($this->role_data[$role_id][1]) {
597  case "admin":
598  $valid_user_ids = array_merge($valid_user_ids, $this->participants->getAdmins());
599  break;
600 
601  case "tutor":
602  $valid_user_ids = array_merge($valid_user_ids, $this->participants->getTutors());
603  break;
604 
605  // member/local
606  default:
607  if (!$this->has_local_role) {
608  $valid_user_ids = array_merge($valid_user_ids, $members);
609  } else {
610  $valid_user_ids = array_merge($valid_user_ids, (array) ($members[$role_id] ?? []));
611  }
612  break;
613  }
614  }
615  }
616 
617  if ($this->include_subscribers) {
618  $valid_user_ids = array_merge($valid_user_ids, $this->participants->getSubscribers());
619  }
620 
621  if ($this->include_waiting_list) {
622  $valid_user_ids = array_merge($valid_user_ids, $this->waiting_list->getUserIds());
623  }
624 
625  if ($this->user_filters) {
626  foreach ($this->user_filters as $sub_id => $sub_item) {
627  $filters[$sub_id] = (bool) ($sub_item[2] ?? false);
628  }
629  }
630  $valid_user_ids = ilUtil::_sortIds(array_unique($valid_user_ids), 'usr_data', 'lastname', 'usr_id');
631  foreach ($valid_user_ids as $user_id) {
632  if ($this->callback) {
633  $user_data = call_user_func_array($this->callback, [(int) $user_id, $filters]);
634  if (!$user_data) {
635  continue;
636  }
637 
638  $tpl->setCurrentBlock("row_preset");
639  foreach ($this->presets as $id => $item) {
640  if ($item[1]) {
641  switch ($id) {
642  case 'org_units':
643  $value = ilOrgUnitPathStorage::getTextRepresentationOfUsersOrgUnits((int) $user_id);
644  break;
645 
646  case "name":
647  if (!($user_data[$id] ?? null)) {
648  $name = ilObjUser::_lookupName((int) $user_id);
649  $value = $name["lastname"] . ", " . $name["firstname"];
650  break;
651  }
652  // no break
653  case "login":
654  if (!($user_data[$id] ?? false)) {
655  $value = ilObjUser::_lookupLogin((int) $user_id);
656  break;
657  }
658 
659  // no break
660  default:
661  $value = (string) ($user_data[$id] ?? '');
662  break;
663  }
664  $tpl->setVariable("TXT_PRESET", $value);
665  $tpl->parseCurrentBlock();
666  }
667  }
668  }
669 
670  if ($this->blank_columns) {
671  for ($loop = 0, $loopMax = count($this->blank_columns); $loop < $loopMax; $loop++) {
672  $tpl->touchBlock('row_blank');
673  }
674  }
675 
676  $tpl->touchBlock("member_row");
677  }
678  return $tpl->get();
679  }
680 }
static array static setUseRelativeDates(bool $a_status)
set use relative dates
parseCurrentBlock(string $block_name=self::DEFAULT_BLOCK)
Parses the given block.
initForm(string $a_cmd="")
Init form.
This class represents an option in a checkbox group.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Base class for course and group waiting lists.
addUserFilter(string $a_id, string $a_caption, bool $a_checked=false)
Add user filter.
setCurrentBlock(string $part=self::DEFAULT_BLOCK)
Sets the template to the given block.
touchBlock(string $block)
overwrites ITX::touchBlock.
addPreset(string $a_id, string $a_caption, bool $a_selected=false)
Add user field.
getFullscreenHTML()
render list in fullscreen mode
static _getValuesByObjId(int $a_obj_id)
static _lookupName(int $a_user_id)
lookup user name
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getNonMemberUserData(array &$a_res)
Get user data for subscribers and waiting list.
initFromSettings()
Directly set list attributes from default settings for print view.
static _getFields(int $a_container_id, $a_sort=self::IL_CDF_SORT_NAME)
Get all fields of a container.
const IL_CAL_UNIX
setVariable(string $variable, $value='')
Sets the given variable to the given value.
Base class for attendance lists.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
setRoleSelection(array $a_role_ids)
ilGlobalTemplateInterface $tpl
addBlank(string $a_caption)
Add blank column preset.
setCallback(callable $a_callback)
Set participant detail callback.
static _lookupTitle(int $obj_id)
$GLOBALS["DIC"]
Definition: wac.php:53
setBlankColumns(array $a_values)
Add blank columns.
__construct(object $a_parent_gui, ilObject $a_parent_obj, ?ilParticipants $a_participants_object=null, ?ilWaitingList $a_waiting_list=null)
global $DIC
Definition: shib_login.php:22
readOrderedExportableFields()
read object export fields
This class represents a property in a property form.
ilParticipants $participants
get(string $part=self::DEFAULT_BLOCK)
Renders the given block and returns the html string.
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.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
Base class for course and group participants.
static _getInstanceByType(string $a_type)
Get Singleton Instance.
deleteValue(string $a_option)
Delete value.
static _readUsersProfileData(array $a_user_ids)
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
getHTML()
render attendance list
setTitle(string $a_title, ?string $a_description=null)
Set titles.
addRole(int $a_id, string $a_caption, string $a_type)
Add role.
initFromForm()
Set list attributes from post values.
static _lookupLogin(int $a_user_id)