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