ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
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;
40 protected ilTree $tree;
44 protected $callback;
45 protected array $presets = [];
46 protected array $role_data = [];
47 protected array $roles = [];
48 protected bool $has_local_role = false;
49 protected array $blank_columns = [];
50 protected string $title = '';
51 protected string $description = '';
52 protected array $pre_blanks = [];
53 protected string $id = '';
54 protected bool $include_waiting_list = false;
55 protected bool $include_subscribers = false;
56 protected array $user_filters = [];
57
58 public function __construct(
59 object $a_parent_gui,
60 ilObject $a_parent_obj,
61 ?ilParticipants $a_participants_object = null,
62 ?ilWaitingList $a_waiting_list = null
63 ) {
64 global $DIC;
65
66 $this->logger = $DIC->logger()->mmbr();
67 $this->lng = $DIC->language();
68 $this->ctrl = $DIC->ctrl();
69 $this->tpl = $DIC->ui()->mainTemplate();
70 $this->profile = $DIC['user']->getProfile();
71 $this->tree = $DIC->repositoryTree();
72 $this->parent_gui = $a_parent_gui;
73 $this->parent_obj = $a_parent_obj;
74 $this->participants = $a_participants_object;
75 $this->waiting_list = $a_waiting_list;
76
77 // always available
78 $this->presets['name'] = array($DIC->language()->txt('name'), true);
79 $this->presets['login'] = array($DIC->language()->txt('login'), true);
80
81 // add exportable fields
83
84 $DIC->language()->loadLanguageModule('crs');
85
86 // roles
87 $roles = $this->participants->getRoles();
88
89 foreach ($roles as $role_id) {
91 switch (substr($title, 0, 8)) {
92 case 'il_crs_a':
93 case 'il_grp_a':
94 case 'il_lso_a':
95 $this->addRole($role_id, $DIC->language()->txt('event_tbl_admin'), 'admin');
96 break;
97
98 case 'il_crs_t':
99 $this->addRole($role_id, $DIC->language()->txt('event_tbl_tutor'), 'tutor');
100 break;
101
102 case 'il_crs_m':
103 case 'il_grp_m':
104 case 'il_sess_':
105 case 'il_lso_m':
106 $this->addRole($role_id, $DIC->language()->txt('event_tbl_member'), 'member');
107 break;
108
109 // local
110 default:
111 $this->has_local_role = true;
112 $this->addRole($role_id, $title, 'local');
113 break;
114 }
115 }
116 }
117
121 protected function readOrderedExportableFields(): bool
122 {
123 $field_info = ilExportFieldsInfo::_getInstanceByType($this->parent_obj->getType());
124 $field_info->sortExportFields();
125
126 foreach ($field_info->getExportableFields() as $field) {
127 switch ($field) {
128 case 'username':
129 case 'firstname':
130 case 'lastname':
131 continue 2;
132 }
133
134 // Check if default enabled
135 $this->presets[$field] = array(
136 $GLOBALS['DIC']['lng']->txt($field),
137 false
138 );
139 }
140
141 $parent_obj_type = $this->tree->checkForParentType($this->parent_obj->getRefId(), 'crs') ? 'crs' : '';
142 $parent_obj_type = $this->tree->checkForParentType($this->parent_obj->getRefId(), 'grp') ? 'grp' : $parent_obj_type;
143 $user_defined_fields = $parent_obj_type === ''
144 ? $this->profile->getAllUserDefinedFields()
145 : $this->profile->getVisibleUserDefinedFields(Context::buildFromObjectType($parent_obj_type));
146
147 // add udf fields
148 foreach ($user_defined_fields as $field) {
149 $this->presets['udf_' . $field->getIdentifier()] = array(
150 $field->getLabel($this->lng),
151 false
152 );
153 }
154
155 // add cdf fields
156 foreach (ilCourseDefinedFieldDefinition::_getFields($this->parent_obj->getId()) as $field_obj) {
157 $this->presets['cdf_' . $field_obj->getId()] = array(
158 $field_obj->getName(),
159 false
160 );
161 }
162 return true;
163 }
164
168 public function addPreset(string $a_id, string $a_caption, bool $a_selected = false): void
169 {
170 $this->presets[$a_id] = array($a_caption, $a_selected);
171 }
172
176 public function addBlank(string $a_caption): void
177 {
178 $this->pre_blanks[] = $a_caption;
179 }
180
184 public function setTitle(string $a_title, ?string $a_description = null): void
185 {
186 $this->title = $a_title;
187 $this->description = (string) $a_description;
188 }
189
193 protected function addRole(int $a_id, string $a_caption, string $a_type): void
194 {
195 $this->role_data[$a_id] = array($a_caption, $a_type);
196 }
197
198 protected function setRoleSelection(array $a_role_ids): void
199 {
200 $this->roles = $a_role_ids;
201 }
202
206 public function addUserFilter(string $a_id, string $a_caption, bool $a_checked = false): void
207 {
208 $this->user_filters[$a_id] = array($a_caption, $a_checked);
209 }
210
214 public function getNonMemberUserData(array &$a_res): void
215 {
216 $subscriber_ids = $this->participants->getSubscribers();
217 $user_ids = $subscriber_ids;
218 if ($this->waiting_list) {
219 $user_ids = array_merge($user_ids, $this->waiting_list->getUserIds());
220 }
221
222 // Finally read user profile data
223 $profile_data = ilObjUser::_readUsersProfileData($user_ids);
224 foreach ($profile_data as $user_id => $fields) {
225 foreach ((array) $fields as $field => $value) {
226 $a_res[$user_id][$field] = $value;
227 }
228 }
229
230 foreach ($this->profile->getVisibleUserDefinedFields(
231 Context::buildFromObjectType($this->parent_obj->getType())
232 ) as $field) {
233 $profile_data = $this->profile->getDataForMultiple($user_ids);
234 foreach ($profile_data as $user_id => $field) {
235 $a_res[$user_id]['udf_' . $field->getIdentifier()] = $profile_data->getAdditionalFieldByIdentifier(
236 $field->getIdentifier()
237 );
238 }
239 }
240
241 if (count($user_ids)) {
242 // object specific user data
243 $cdfs = ilCourseUserData::_getValuesByObjId($this->parent_obj->getId());
244 foreach (array_unique($user_ids) as $user_id) {
245 if ($tmp_obj = ilObjectFactory::getInstanceByObjId($user_id, false)) {
246 $a_res[$user_id]['login'] = $tmp_obj->getLogin();
247 $a_res[$user_id]['name'] = $tmp_obj->getLastname() . ', ' . $tmp_obj->getFirstname();
248
249 if (in_array($user_id, $subscriber_ids)) {
250 $a_res[$user_id]['status'] = $this->lng->txt('crs_subscriber');
251 } else {
252 $a_res[$user_id]['status'] = $this->lng->txt('crs_waiting_list');
253 }
254
255 foreach ((array) ($cdfs[$user_id] ?? []) as $field_id => $value) {
256 $a_res[$user_id]['cdf_' . $field_id] = (string) $value;
257 }
258 }
259 }
260 }
261 }
262
266 public function setBlankColumns(array $a_values): void
267 {
268 if (!implode("", $a_values)) {
269 $a_values = array();
270 } else {
271 foreach ($a_values as $idx => $value) {
272 $a_values[$idx] = trim($value);
273 if ($a_values[$idx] == "") {
274 unset($a_values[$idx]);
275 }
276 }
277 }
278 $this->blank_columns = $a_values;
279 }
280
284 public function setCallback(callable $a_callback): void
285 {
286 $this->callback = $a_callback;
287 }
288
289 public function setId(string $a_value): void
290 {
291 $this->id = $a_value;
292 }
293
297 public function initForm(string $a_cmd = ""): ilPropertyFormGUI
298 {
299 $this->lng->loadLanguageModule('crs');
300
301 $form = new ilPropertyFormGUI();
302 $form->setFormAction($this->ctrl->getFormAction($this->parent_gui, $a_cmd));
303 $form->setPreventDoubleSubmission(false);
304 $form->setTitle($this->lng->txt('sess_gen_attendance_list'));
305
306 $title = new ilTextInputGUI($this->lng->txt('title'), 'title');
307 $title->setValue($this->title);
308 $form->addItem($title);
309
310 $desc = new ilTextInputGUI($this->lng->txt('description'), 'desc');
311 $desc->setValue($this->description);
312 $form->addItem($desc);
313
314 if (count($this->presets)) {
315 $preset = new ilCheckboxGroupInputGUI($this->lng->txt('user_detail'), 'preset');
316 $preset_value = array();
317 foreach ($this->presets as $id => $item) {
318 $preset->addOption(new ilCheckboxOption($item[0], $id));
319 if ($item[1]) {
320 $preset_value[] = $id;
321 }
322 }
323 $preset->setValue($preset_value);
324 $form->addItem($preset);
325 }
326
327 $blank = new ilTextInputGUI($this->lng->txt('event_blank_columns'), 'blank');
328 $blank->setMulti(true);
329 $form->addItem($blank);
330
331 if ($this->pre_blanks) {
332 $blank->setValue($this->pre_blanks);
333 }
334
335 $checked = array();
336
337 $chk_grp = new ilCheckboxGroupInputGUI($this->lng->txt('event_user_selection'), 'selection_of_users');
338
339 // participants by roles
340 foreach ($this->role_data as $role_id => $role_data) {
341 $title = ilObject::_lookupTitle($role_id);
342
343 $role_name = $role_id;
344 if (strpos($title, 'il_' . $this->parent_obj->getType() . '_adm') === 0) {
345 $role_name = 'adm';
346 }
347 if (strpos($title, 'il_' . $this->parent_obj->getType() . '_mem') === 0) {
348 $role_name = 'mem';
349 }
350 if (strpos($title, 'il_' . $this->parent_obj->getType() . '_tut') === 0) {
351 $role_name = 'tut';
352 }
353
354 $chk = new ilCheckboxOption(
355 sprintf($this->lng->txt('event_user_selection_include_role'), $role_data[0]),
356 'role_' . $role_name
357 );
358 $checked[] = 'role_' . $role_name;
359 $chk_grp->addOption($chk);
360 }
361
362 if ($this->waiting_list) {
363 $chk = new ilCheckboxOption($this->lng->txt('event_user_selection_include_requests'), 'subscr');
364 $chk_grp->addOption($chk);
365
366 $chk = new ilCheckboxOption($this->lng->txt('event_user_selection_include_waiting_list'), 'wlist');
367 $chk_grp->addOption($chk);
368 }
369
370 if ($this->user_filters) {
371 foreach ($this->user_filters as $sub_id => $sub_item) {
372 $chk = new ilCheckboxOption(
373 sprintf($this->lng->txt('event_user_selection_include_filter'), $sub_item[0]),
374 'members_' . $sub_id
375 );
376 if ($sub_item[1]) {
377 $checked[] = 'members_' . $sub_id;
378 }
379 $chk_grp->addOption($chk);
380 }
381 }
382 $chk_grp->setValue($checked);
383 $form->addItem($chk_grp);
384
385 $form->addCommandButton($a_cmd, $this->lng->txt('sess_print_attendance_list'));
386
387 if ($this->id && $a_cmd) {
388 $settings = new ilUserFormSettings($this->id);
389 if (!$settings->hasStoredEntry()) {
390 $settings = new ilUserFormSettings($this->parent_obj->getType() . 's_pview', -1);
391 }
392
393 $settings->deleteValue('desc'); // #11340
394 $settings->exportToForm($form);
395 }
396 return $form;
397 }
398
402 public function initFromForm(): void
403 {
404 $form = $this->initForm();
405 if ($form->checkInput()) {
406 foreach (array_keys($this->presets) as $id) {
407 $this->presets[$id][1] = false;
408 }
409 foreach ((array) $form->getInput('preset') as $value) {
410 if (isset($this->presets[$value])) {
411 $this->presets[$value][1] = true;
412 } else {
413 $this->addPreset($value, $value, true);
414 }
415 }
416
417 $this->setTitle($form->getInput('title'), $form->getInput('desc'));
418 $this->setBlankColumns($form->getInput('blank'));
419
420 $selection_of_users = (array) $form->getInput('selection_of_users'); // #18238
421
422 $roles = array();
423 foreach (array_keys($this->role_data) as $role_id) {
424 $title = ilObject::_lookupTitle($role_id);
425 $role_name = $role_id;
426 if (strpos($title, 'il_' . $this->parent_obj->getType() . '_adm') === 0) {
427 $role_name = 'adm';
428 }
429 if (strpos($title, 'il_' . $this->parent_obj->getType() . '_mem') === 0) {
430 $role_name = 'mem';
431 }
432 if (strpos($title, 'il_' . $this->parent_obj->getType() . '_tut') === 0) {
433 $role_name = 'tut';
434 }
435
436 if (in_array('role_' . $role_name, $selection_of_users)) {
437 $roles[] = $role_id;
438 }
439 }
440 $this->setRoleSelection($roles);
441
442 // not in sessions
443 if ($this->waiting_list) {
444 $this->include_subscribers = in_array('subscr', $selection_of_users);
445 $this->include_waiting_list = in_array('wlist', $selection_of_users);
446 }
447
448 if ($this->user_filters) {
449 foreach (array_keys($this->user_filters) as $msub_id) {
450 $this->user_filters[$msub_id][2] = in_array("members_" . $msub_id, $selection_of_users);
451 }
452 }
453
454 if ($this->id) {
455 #$form->setValuesByPost();
456
457 #$settings = new ilUserFormSettings($this->id);
458 #$settings->deleteValue('desc'); // #11340
459 #$settings->importFromForm($form);
460 #$settings->store();
461 }
462 }
463 }
464
468 public function initFromSettings(): void
469 {
470 $settings = new ilUserFormSettings(
471 $this->parent_obj->getType() . 's_pview_' . $this->parent_obj->getId(),
472 -1
473 );
474 if (!$settings->hasStoredEntry()) {
475 // init from global defaults
476 $settings = new ilUserFormSettings($this->parent_obj->getType() . 's_pview', -1);
477 }
478 $settings->deleteValue('desc'); // #11340
479
480 // title and description
481 $this->setTitle(
482 (string) ($settings->getValue('title') ?? $this->title),
483 (string) ($settings->getValue('desc') ?? $this->description)
484 );
485
486 // preset
487 if ($preset_value = $settings->getValue('preset')) {
488 foreach (array_keys($this->presets) as $id) {
489 $this->presets[$id][1] = false;
490 }
491 foreach ((array) $preset_value as $value) {
492 if (isset($this->presets[$value])) {
493 $this->presets[$value][1] = true;
494 } else {
495 $this->addPreset($value, $value, true);
496 }
497 }
498 }
499
500 // blank
501 $this->setBlankColumns(
502 (array) ($settings->getValue('blank') ?? $this->pre_blanks)
503 );
504
505 // selection of users
506 $selection_of_users = $settings->getValue('selection_of_users');
507
508 // participants by roles
509 $roles = [];
510 foreach ($this->role_data as $role_id => $role_data) {
511 $title = ilObject::_lookupTitle($role_id);
512
513 $role_name = $role_id;
514 if (strpos($title, 'il_' . $this->parent_obj->getType() . '_adm') === 0) {
515 $role_name = 'adm';
516 }
517 if (strpos($title, 'il_' . $this->parent_obj->getType() . '_mem') === 0) {
518 $role_name = 'mem';
519 }
520 if (strpos($title, 'il_' . $this->parent_obj->getType() . '_tut') === 0) {
521 $role_name = 'tut';
522 }
523
524 if (
525 isset($selection_of_users) &&
526 !in_array('role_' . $role_name, $selection_of_users)
527 ) {
528 continue;
529 }
530 $roles[] = $role_id;
531 }
532 $this->setRoleSelection($roles);
533
534 // waiting list and subscribers (not in sessions)
535 if ($this->waiting_list && isset($selection_of_users)) {
536 $this->include_subscribers = in_array('subscr', $selection_of_users);
537 $this->include_waiting_list = in_array('wlist', $selection_of_users);
538 }
539
540 // user filters
541 foreach (array_keys($this->user_filters) as $msub_id) {
542 if (isset($selection_of_users)) {
543 $this->user_filters[$msub_id][2] = in_array("members_" . $msub_id, $selection_of_users);
544 continue;
545 }
546 if ($this->user_filters[$msub_id][1]) {
547 $this->user_filters[$msub_id][2] = true;
548 }
549 }
550 }
551
555 public function getFullscreenHTML(): void
556 {
557 $this->tpl->setContent($this->getHTML());
558 $this->tpl->addOnLoadCode("il.Util.print();");
559 }
560
564 public function getHTML(): string
565 {
566 $tpl = new ilTemplate('tpl.attendance_list_print.html', true, true, 'components/ILIAS/Membership');
569
570 $tpl->setVariable('TXT_TITLE', $this->title);
571 if ($this->description) {
572 $tpl->setVariable('TXT_DESCRIPTION', $this->description . " (" . $time . ")");
573 } else {
574 $tpl->setVariable('TXT_DESCRIPTION', $time);
575 }
576
577 $tpl->setCurrentBlock('head_item');
578 foreach ($this->presets as $item) {
579 if ($item[1]) {
580 $tpl->setVariable('TXT_HEAD', $item[0]);
582 }
583 }
584
585 if ($this->blank_columns) {
586 foreach ($this->blank_columns as $blank) {
587 $tpl->setVariable('TXT_HEAD', $blank);
589 }
590 }
591
592 // handle members
593
594 $valid_user_ids = $filters = array();
595
596 if ($this->roles) {
597 if ($this->has_local_role) {
598 $members = array();
599 foreach ($this->participants->getMembers() as $member_id) {
600 foreach ($this->participants->getAssignedRoles($member_id) as $role_id) {
601 $members[$role_id][] = $member_id;
602 }
603 }
604 } else {
605 $members = $this->participants->getMembers();
606 }
607
608 foreach ($this->roles as $role_id) {
609 switch ($this->role_data[$role_id][1]) {
610 case "admin":
611 $valid_user_ids = array_merge($valid_user_ids, $this->participants->getAdmins());
612 break;
613
614 case "tutor":
615 $valid_user_ids = array_merge($valid_user_ids, $this->participants->getTutors());
616 break;
617
618 // member/local
619 default:
620 if (!$this->has_local_role) {
621 $valid_user_ids = array_merge($valid_user_ids, $members);
622 } else {
623 $valid_user_ids = array_merge($valid_user_ids, (array) ($members[$role_id] ?? []));
624 }
625 break;
626 }
627 }
628 }
629
630 if ($this->include_subscribers) {
631 $valid_user_ids = array_merge($valid_user_ids, $this->participants->getSubscribers());
632 }
633
634 if ($this->include_waiting_list) {
635 $valid_user_ids = array_merge($valid_user_ids, $this->waiting_list->getUserIds());
636 }
637
638 if ($this->user_filters) {
639 foreach ($this->user_filters as $sub_id => $sub_item) {
640 $filters[$sub_id] = (bool) ($sub_item[2] ?? false);
641 }
642 }
643 $valid_user_ids = ilUtil::_sortIds(array_unique($valid_user_ids), 'usr_data', 'lastname', 'usr_id');
644 foreach ($valid_user_ids as $user_id) {
645 if ($this->callback) {
646 $user_data = call_user_func_array($this->callback, [(int) $user_id, $filters]);
647 if (!$user_data) {
648 continue;
649 }
650
651 $tpl->setCurrentBlock("row_preset");
652 foreach ($this->presets as $id => $item) {
653 if ($item[1]) {
654 switch ($id) {
655 case 'org_units':
656 $value = ilOrgUnitPathStorage::getTextRepresentationOfUsersOrgUnits((int) $user_id);
657 break;
658
659 case "name":
660 if (!($user_data[$id] ?? null)) {
661 $name = ilObjUser::_lookupName((int) $user_id);
662 $value = $name["lastname"] . ", " . $name["firstname"];
663 break;
664 }
665 // no break
666 case "login":
667 if (!($user_data[$id] ?? false)) {
668 $value = ilObjUser::_lookupLogin((int) $user_id);
669 break;
670 }
671
672 // no break
673 default:
674 $value = (string) ($user_data[$id] ?? '');
675 break;
676 }
677 $tpl->setVariable("TXT_PRESET", $value);
679 }
680 }
681 }
682
683 if ($this->blank_columns) {
684 for ($loop = 0, $loopMax = count($this->blank_columns); $loop < $loopMax; $loop++) {
685 $tpl->touchBlock('row_blank');
686 }
687 }
688
689 $tpl->touchBlock("member_row");
690 }
691 return $tpl->get();
692 }
693}
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.
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,...
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