ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMStListUsersTableGUI.php
Go to the documentation of this file.
1 <?php
18 declare(strict_types=1);
19 
20 namespace ILIAS\MyStaff\ListUsers;
21 
22 use Closure;
24 use ilCSVWriter;
25 use ilExcel;
28 use ilObjOrgUnit;
32 use ilTable2GUI;
33 use ilTextInputGUI;
35 use ilMyStaffGUI;
36 
42 {
43  protected array $filter = [];
44  protected array $selectable_columns_cached = [];
45  protected array $usr_orgu_names = [];
47 
48  private \ILIAS\UI\Factory $uiFactory;
49  private \ILIAS\UI\Renderer $uiRenderer;
50  private \ilLanguage $language;
51 
57  {
58  global $DIC;
59 
61 
62  $this->setPrefix('myst_lu');
63  $this->setFormName('myst_lu');
64  $this->setId('myst_lu');
65 
66  parent::__construct($parent_obj, $parent_cmd, '');
67 
68  $this->uiFactory = $DIC->ui()->factory();
69  $this->uiRenderer = $DIC->ui()->renderer();
70  $this->language = $DIC->language();
71 
72  $this->setRowTemplate('tpl.list_users_row.html', "Services/MyStaff");
73  $this->setFormAction($DIC->ctrl()->getFormAction($parent_obj));
74  $this->setDefaultOrderDirection('desc');
75 
76  $this->setShowRowsSelector(true);
77 
78  $this->setEnableTitle(true);
79  $this->setDisableFilterHiding(true);
80  $this->setEnableNumInfo(true);
81 
82  $this->setExportFormats(array(self::EXPORT_EXCEL, self::EXPORT_CSV));
83 
84  $this->setFilterCols(4);
85  $this->initFilter();
86  $this->addColumns();
87 
88  $this->parseData();
89  }
90 
91  protected function parseData(): void
92  {
93  global $DIC;
94 
95  $this->setExternalSorting(true);
96  $this->setExternalSegmentation(true);
97  $this->setDefaultOrderField('lastname');
98 
99  $this->determineLimit();
100  $this->determineOffsetAndOrder();
101 
102  //Permission Filter
103  $arr_usr_id = $this->access->getUsersForUser($DIC->user()->getId());
104 
105  $options = array(
106  'filters' => $this->filter,
107  'limit' => array(
108  'start' => $this->getOffset(),
109  'end' => $this->getLimit(),
110  ),
111  'sort' => array(
112  'field' => $this->getOrderField(),
113  'direction' => $this->getOrderDirection(),
114  ),
115  );
116 
117  $list_users_fetcher = new ilMStListUsers($DIC);
118  $result = $list_users_fetcher->getData($arr_usr_id, $options);
119 
120  $this->setMaxCount($result->getTotalDatasetCount());
121  $data = $result->getDataset();
122 
123  // Workaround because the fillRow Method only accepts arrays
124  $data = array_map(function (ilMStListUser $it): array {
125  return [$it];
126  }, $data);
127  $this->setData($data);
128  }
129 
130  final public function initFilter(): void
131  {
132  global $DIC;
133 
134  // User name, login, email filter
135  $item = new ilTextInputGUI(
136  $DIC->language()->txt("login") . "/" . $DIC->language()->txt("email") . "/" . $DIC->language()
137  ->txt("name"),
138  "user"
139  );
140  $this->addFilterItem($item);
141  $item->readFromSession();
142  $this->filter['user'] = $item->getValue();
143 
144  if (ilUserSearchOptions::_isEnabled('org_units')) {
146  $options[0] = $DIC->language()->txt('mst_opt_all');
147  foreach ($paths as $org_ref_id => $path) {
148  $options[$org_ref_id] = $path;
149  }
150  $item = new ilSelectInputGUI($DIC->language()->txt('obj_orgu'), 'org_unit');
151  $item->setOptions($options);
152  $item->addCustomAttribute("style='width:100%'");
153  $this->addFilterItem($item);
154  $item->readFromSession();
155  $this->filter['org_unit'] = $item->getValue();
156  }
157  }
158 
159  final public function getSelectableColumns(): array
160  {
161  if ($this->selectable_columns_cached) {
163  }
164 
165  return $this->selectable_columns_cached = $this->initSelectableColumns();
166  }
167 
168  protected function initSelectableColumns(): array
169  {
170  $arr_fields_without_table_sort = array(
171  'org_units',
172  'interests_general',
173  'interests_help_offered',
174  'interests_help_looking',
175  );
176  $cols = array();
177  foreach (ilUserSearchOptions::getSelectableColumnInfo() as $key => $col) {
178  $cols[$key] = $col;
179  if (!in_array($key, $arr_fields_without_table_sort)) {
180  $cols[$key]['sort_field'] = $key;
181  }
182  }
183 
184  $user_defined_fields = \ilUserDefinedFields::_getInstance();
185  foreach ($user_defined_fields->getDefinitions() as $field => $definition) {
186  unset($cols["udf_" . $field]);
187  }
188 
189  return $cols;
190  }
191 
192  private function addColumns(): void
193  {
194  global $DIC;
195 
196  //User Profile Picture
197  if (!$this->getExportMode()) {
198  $this->addColumn('');
199  }
200 
201  foreach ($this->getSelectableColumns() as $k => $v) {
202  if ($this->isColumnSelected($k)) {
203  $sort = $v['sort_field'] ?? "";
204  $this->addColumn($v['txt'], $sort);
205  }
206  }
207  //Actions
208  if (!$this->getExportMode()) {
209  $this->addColumn($DIC->language()->txt('actions'));
210  }
211  }
212 
213  protected function getTextRepresentationOfUsersOrgUnits(int $user_id): string
214  {
215  if (isset($this->usr_orgu_names[$user_id])) {
216  return $this->usr_orgu_names[$user_id];
217  }
218 
219  return $this->usr_orgu_names[$user_id] = \ilOrgUnitPathStorage::getTextRepresentationOfUsersOrgUnits($user_id);
220  }
221 
228  final protected function fillRow(array $a_set): void
229  {
230  global $DIC;
231 
232  $set = array_pop($a_set);
233 
234  $propGetter = Closure::bind(function ($prop) {
235  return $this->$prop ?? null;
236  }, $set, $set);
237 
238  //Avatar
239  $this->tpl->setCurrentBlock('user_profile_picture');
240  $il_obj_user = $set->returnIlUserObj();
241  $avatar = $this->uiFactory->image()->standard($il_obj_user->getPersonalPicturePath('small'), $il_obj_user->getPublicName());
242  $this->tpl->setVariable('user_profile_picture', $this->uiRenderer->render($avatar));
243  $this->tpl->parseCurrentBlock();
244 
245  foreach ($this->getSelectedColumns() as $k => $v) {
246  switch ($k) {
247  case 'org_units':
248  $this->tpl->setCurrentBlock('td');
249  $this->tpl->setVariable(
250  'VALUE',
251  $this->getTextRepresentationOfUsersOrgUnits($set->getUsrId())
252  );
253  $this->tpl->parseCurrentBlock();
254  break;
255  case 'gender':
256  $this->tpl->setCurrentBlock('td');
257  $this->tpl->setVariable('VALUE', $DIC->language()->txt('gender_' . $set->getGender()));
258  $this->tpl->parseCurrentBlock();
259  break;
260  case 'interests_general':
261  $this->tpl->setCurrentBlock('td');
262  $this->tpl->setVariable('VALUE', ($set->returnIlUserObj()
263  ->getGeneralInterestsAsText() ? $set->returnIlUserObj()->getGeneralInterestsAsText() : '&nbsp;'));
264  $this->tpl->parseCurrentBlock();
265  break;
266  case 'interests_help_offered':
267  $this->tpl->setCurrentBlock('td');
268  $this->tpl->setVariable('VALUE', ($set->returnIlUserObj()
269  ->getOfferingHelpAsText() ? $set->returnIlUserObj()->getOfferingHelpAsText() : '&nbsp;'));
270  $this->tpl->parseCurrentBlock();
271  break;
272  case 'interests_help_looking':
273  $this->tpl->setCurrentBlock('td');
274  $this->tpl->setVariable('VALUE', ($set->returnIlUserObj()
275  ->getLookingForHelpAsText() ? $set->returnIlUserObj()->getLookingForHelpAsText() : '&nbsp;'));
276  $this->tpl->parseCurrentBlock();
277  break;
278  default:
279  if ($propGetter($k) !== null) {
280  $this->tpl->setCurrentBlock('td');
281  $this->tpl->setVariable(
282  'VALUE',
283  (is_array($propGetter($k)) ? implode(", ", $propGetter($k)) : $propGetter($k))
284  );
285  $this->tpl->parseCurrentBlock();
286  } else {
287  $this->tpl->setCurrentBlock('td');
288  $this->tpl->setVariable('VALUE', '&nbsp;');
289  $this->tpl->parseCurrentBlock();
290  }
291  break;
292  }
293  }
294 
295  $actions = new ilAdvancedSelectionListGUI();
296  $actions->setListTitle($this->language->txt("actions"));
297  $actions->setId(strval($set->getUsrId()));
298 
299  $mst_lus_usr_id = $set->getUsrId();
300 
301  if ($this->access->hasCurrentUserAccessToCourseMemberships()) {
302  $DIC->ctrl()->setParameterByClass(\ilMStShowUserCoursesGUI::class, 'usr_id', $mst_lus_usr_id);
303  $actions->addItem(
304  $DIC->language()->txt('mst_show_courses'),
305  '',
306  $DIC->ctrl()->getLinkTargetByClass(array(
307  \ilDashboardGUI::class,
308  ilMyStaffGUI::class,
309  \ilMStShowUserGUI::class,
310  \ilMStShowUserCoursesGUI::class,
311  ))
312  );
313  }
314 
315  if ($this->access->hasCurrentUserAccessToCertificates()) {
316  $DIC->ctrl()->setParameterByClass(\ilUserCertificateGUI::class, 'usr_id', $mst_lus_usr_id);
317  $actions->addItem(
318  $DIC->language()->txt('mst_list_certificates'),
319  '',
320  $DIC->ctrl()->getLinkTargetByClass(array(
321  \ilDashboardGUI::class,
322  ilMyStaffGUI::class,
323  \ilMStShowUserGUI::class,
324  \ilUserCertificateGUI::class,
325  ))
326  );
327  }
328 
329  if ($this->access->hasCurrentUserAccessToCompetences()) {
330  $DIC->ctrl()->setParameterByClass(\ilMStShowUserCompetencesGUI::class, 'usr_id', $mst_lus_usr_id);
331  $actions->addItem(
332  $DIC->language()->txt('mst_list_competences'),
333  '',
334  $DIC->ctrl()->getLinkTargetByClass(array(
335  \ilDashboardGUI::class,
336  ilMyStaffGUI::class,
337  \ilMStShowUserGUI::class,
338  \ilMStShowUserCompetencesGUI::class,
339  ))
340  );
341  }
342 
343  $this->ctrl->setParameterByClass(\ilMStListUsersGUI::class, 'mst_lus_usr_id', $mst_lus_usr_id);
344 
345  $actions = ilMyStaffGUI::extendActionMenuWithUserActions(
346  $actions,
347  $mst_lus_usr_id,
348  rawurlencode($this->ctrl->getLinkTargetByClass("ilMStListUsersGUI", \ilMStListUsersGUI::CMD_INDEX))
349  );
350 
351  $this->tpl->setVariable('ACTIONS', $actions->getHTML());
352  $this->tpl->parseCurrentBlock();
353  }
354 
355  private function getProfileBackUrl(): string
356  {
357  global $DIC;
358 
359  return rawurlencode($DIC->ctrl()->getLinkTargetByClass(
360  strtolower(ilMyStaffGUI::class),
362  ));
363  }
364 
365  protected function fillRowExcel(ilExcel $a_excel, int &$a_row, array $a_set): void
366  {
367  $set = array_pop($a_set);
368 
369  $col = 0;
370  foreach ($this->getFieldValuesForExport($set) as $k => $v) {
371  $a_excel->setCell($a_row, $col, $v);
372  $col++;
373  }
374  }
375 
376  protected function fillRowCSV(ilCSVWriter $a_csv, array $a_set): void
377  {
378  $set = array_pop($a_set);
379 
380  foreach ($this->getFieldValuesForExport($set) as $k => $v) {
381  $a_csv->addColumn($v);
382  }
383  $a_csv->addRow();
384  }
385 
386  protected function getFieldValuesForExport(ilMStListUser $my_staff_user): array
387  {
388  global $DIC;
389 
390  $propGetter = Closure::bind(function ($prop) {
391  return $this->$prop ?? null;
392  }, $my_staff_user, $my_staff_user);
393 
394  $field_values = array();
395 
396  foreach ($this->getSelectedColumns() as $k => $v) {
397  switch ($k) {
398  case 'org_units':
399  $field_values[$k] = $this->getTextRepresentationOfUsersOrgUnits($my_staff_user->getUsrId());
400  break;
401  case 'gender':
402  $field_values[$k] = $DIC->language()->txt('gender_' . $my_staff_user->getGender());
403  break;
404  case 'interests_general':
405  $field_values[$k] = $my_staff_user->returnIlUserObj()->getGeneralInterestsAsText();
406  break;
407  case 'interests_help_offered':
408  $field_values[$k] = $my_staff_user->returnIlUserObj()->getOfferingHelpAsText();
409  break;
410  case 'interests_help_looking':
411  $field_values[$k] = $my_staff_user->returnIlUserObj()->getLookingForHelpAsText();
412  break;
413  default:
414  $field_values[$k] = strip_tags($propGetter($k) ?? "");
415  break;
416  }
417  }
418 
419  return $field_values;
420  }
421 }
setData(array $a_data)
addColumn(string $a_col)
setExportFormats(array $formats)
Set available export formats.
setFormAction(string $a_form_action, bool $a_multipart=false)
addFilterItem(ilTableFilterItem $a_input_item, bool $a_optional=false)
setEnableTitle(bool $a_enabletitle)
__construct(ilMStListUsersGUI $parent_obj, $parent_cmd=ilMStListUsersGUI::CMD_INDEX)
fillRowExcel(ilExcel $a_excel, int &$a_row, array $a_set)
setDisableFilterHiding(bool $a_val=true)
setCell(int $a_row, int $a_col, $a_value, ?string $a_datatype=null)
Set cell value.
setFormName(string $a_name="")
setId(string $a_val)
$path
Definition: ltiservices.php:32
static getTextRepresentationOfOrgUnits(bool $sort_by_title=true)
Get ref id path array.
global $DIC
Definition: feed.php:28
setExternalSorting(bool $a_val)
isColumnSelected(string $col)
setShowRowsSelector(bool $a_value)
Toggle rows-per-page selector.
setDefaultOrderField(string $a_defaultorderfield)
setRowTemplate(string $a_template, string $a_template_dir="")
Set row template.
string $key
Consumer key/client ID value.
Definition: System.php:193
setDefaultOrderDirection(string $a_defaultorderdirection)
static getSelectableColumnInfo(bool $a_admin=false)
Get info of searchable fields for selectable columns in table gui.
setEnableNumInfo(bool $a_val)
__construct(Container $dic, ilPlugin $plugin)
addColumn(string $a_text, string $a_sort_field="", string $a_width="", bool $a_is_checkbox_action_column=false, string $a_class="", string $a_tooltip="", bool $a_tooltip_with_html=false)
$cols
Definition: xhr_table.php:11
determineOffsetAndOrder(bool $a_omit_offset=false)
setMaxCount(int $a_max_count)
set max.
setFilterCols(int $a_val)
setExternalSegmentation(bool $a_val)
setPrefix(string $a_prefix)