ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilRepositoryUserResultTableGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
33 {
38 
39  public const TYPE_STANDARD = 1;
40  public const TYPE_GLOBAL_SEARCH = 2;
41 
43 
44  protected static array $all_selectable_cols = [];
45  protected bool $admin_mode;
46  protected int $type;
47  protected bool $user_limitations = true;
48 
49  protected ilObjUser $user;
50  protected ilRbacReview $review;
51  protected UIServices $ui;
52  protected HTTP $http;
53  protected Refinery $refinery;
54 
55 
56  public function __construct($a_parent_obj, $a_parent_cmd, $a_admin_mode = false, $a_type = self::TYPE_STANDARD)
57  {
58  global $DIC;
59 
60  $this->user = $DIC->user();
61  $this->review = $DIC->rbac()->review();
62  $this->ui = $DIC->ui();
63  $this->http = $DIC->http();
64  $this->refinery = $DIC->refinery();
65 
66  $this->admin_mode = (bool) $a_admin_mode;
67  $this->type = $a_type;
68 
69  $this->setId("rep_search_" . $this->user->getId());
70  parent::__construct($a_parent_obj, $a_parent_cmd);
71 
72 
73  $this->setFormAction($this->ctrl->getFormAction($this->parent_obj));
74  $this->setTitle($this->lng->txt('search_results'));
75  $this->setEnableTitle(true);
76  $this->setShowRowsSelector(true);
77 
78 
79  if ($this->getType() == self::TYPE_STANDARD) {
80  $this->setRowTemplate("tpl.rep_search_usr_result_row.html", "components/ILIAS/Search");
81  $this->addColumn("", "", "1", true);
82  $this->enable('select_all');
83  $this->setSelectAllCheckbox("user[]");
84  $this->setDefaultOrderField("login");
85  $this->setDefaultOrderDirection("asc");
86  } else {
87  $this->setRowTemplate("tpl.global_search_usr_result_row.html", "components/ILIAS/Search");
88  $this->addColumn('', '', "110px");
89  $this->setExternalSorting(true);
90  }
91 
92  $all_cols = $this->getSelectableColumns();
93  foreach ($this->getSelectedColumns() as $col) {
94  $sort_field = '';
95  if ($this->getType() == self::TYPE_STANDARD) {
96  $sort_field = $col;
97  }
98  $this->addColumn($all_cols[$col]['txt'], $sort_field);
99  }
100 
101  if (
102  $this->getType() != self::TYPE_STANDARD &&
103  ilBuddySystem::getInstance()->isEnabled()
104  ) {
105  $this->addColumn('', '');
106  }
107  }
108 
109  public function getType(): int
110  {
111  return $this->type;
112  }
113 
115  {
116  $this->lucene_result = $res;
117  }
118 
120  {
121  return $this->lucene_result;
122  }
123 
124  public function setUserLimitations(bool $a_limitations): void
125  {
126  $this->user_limitations = $a_limitations;
127  }
128 
129  public function getUserLimitations(): bool
130  {
132  }
133 
137  public function getSelectableColumns(): array
138  {
139  if (!self::$all_selectable_cols) {
140  $columns = ilUserSearchOptions::getSelectableColumnInfo($this->review->isAssigned($this->user->getId(), SYSTEM_ROLE_ID));
141 
142  if ($this->admin_mode) {
143  // #11293
144  $columns['access_until'] = array('txt' => $this->lng->txt('access_until'));
145  $columns['last_login'] = array('txt' => $this->lng->txt('last_login'));
146  }
147 
148  self::$all_selectable_cols = $columns;
149  }
150  return self::$all_selectable_cols;
151  }
152 
153  public function initMultiCommands(array $a_commands): bool
154  {
155  if (!count($a_commands)) {
156  $this->addMultiCommand('addUser', $this->lng->txt('btn_add'));
157  return true;
158  }
159  $this->addMultiItemSelectionButton('member_type', $a_commands, 'addUser', $this->lng->txt('btn_add'));
160  return true;
161  }
162 
163  protected function fillRow(array $a_set): void
164  {
165  $this->tpl->setVariable("VAL_ID", $a_set["usr_id"]);
166 
167  $link = '';
168  if ($this->getType() == self::TYPE_GLOBAL_SEARCH) {
169  $link = ilUserUtil::getProfileLink((int) $a_set['usr_id']);
170  if ($link) {
171  $this->tpl->setVariable('IMG_LINKED_TO_PROFILE', $link);
172  $this->tpl->setVariable(
173  'USR_IMG_SRC_LINKED',
174  ilObjUser::_getPersonalPicturePath((int) $a_set['usr_id'], 'xsmall')
175  );
176  } else {
177  $this->tpl->setVariable(
178  'USR_IMG_SRC',
179  ilObjUser::_getPersonalPicturePath((int) $a_set['usr_id'], 'xsmall')
180  );
181  }
182  }
183 
184 
185  foreach ($this->getSelectedColumns() as $field) {
186  switch ($field) {
187  case 'gender':
188  $a_set['gender'] = $a_set['gender'] ? $this->lng->txt('gender_' . $a_set['gender']) : '';
189  $this->tpl->setCurrentBlock('custom_fields');
190  $this->tpl->setVariable('VAL_CUST', $a_set[$field]);
191  $this->tpl->parseCurrentBlock();
192  break;
193 
194  case 'birthday':
195  $a_set['birthday'] = $a_set['birthday'] ? ilDatePresentation::formatDate(new ilDate($a_set['birthday'], IL_CAL_DATE)) : $this->lng->txt('no_date');
196  $this->tpl->setCurrentBlock('custom_fields');
197  $this->tpl->setVariable('VAL_CUST', $a_set[$field]);
198  $this->tpl->parseCurrentBlock();
199  break;
200 
201  case 'access_until':
202  $this->tpl->setCurrentBlock('custom_fields');
203  $this->tpl->setVariable('CUST_CLASS', ' ' . $a_set['access_class']);
204  $this->tpl->setVariable('VAL_CUST', $a_set[$field]);
205  $this->tpl->parseCurrentBlock();
206  break;
207 
208  case 'last_login':
209  $a_set['last_login'] = $a_set['last_login'] ? ilDatePresentation::formatDate(new ilDateTime($a_set['last_login'], IL_CAL_DATETIME)) : $this->lng->txt('no_date');
210  $this->tpl->setCurrentBlock('custom_fields');
211  $this->tpl->setVariable('VAL_CUST', $a_set[$field]);
212  $this->tpl->parseCurrentBlock();
213  break;
214 
215  case 'interests_general':
216  case 'interests_help_offered':
217  case 'interests_help_looking':
218  $this->tpl->setCurrentBlock('custom_fields');
219  $this->tpl->setVariable('VAL_CUST', implode(', ', (array) $a_set[$field]));
220  $this->tpl->parseCurrentBlock();
221  break;
222 
223  case 'org_units':
224  $this->tpl->setCurrentBlock('custom_fields');
225  $this->tpl->setVariable(
226  'VAL_CUST',
227  ilOrgUnitPathStorage::getTextRepresentationOfUsersOrgUnits((int) $a_set['usr_id'])
228  );
229  $this->tpl->parseCurrentBlock();
230  break;
231 
232 
233  case 'login':
234  if ($this->admin_mode) {
235  $this->ctrl->setParameterByClass("ilobjusergui", "ref_id", "7");
236  $this->ctrl->setParameterByClass("ilobjusergui", "obj_id", $a_set["usr_id"]);
237  $this->ctrl->setParameterByClass("ilobjusergui", "search", "1");
238  $link = $this->ctrl->getLinkTargetByClass(array("iladministrationgui", "ilobjusergui"), "view");
239  $a_set[$field] = "<a href=\"" . $link . "\">" . $a_set[$field] . "</a>";
240  } elseif ($this->getType() == self::TYPE_GLOBAL_SEARCH) {
241  $a_set[$field] = "<a href=\"" . $link . "\">" . $a_set[$field] . "</a>";
242  }
243  // fallthrough
244 
245  // no break
246  default:
247  $this->tpl->setCurrentBlock('custom_fields');
248  $this->tpl->setVariable('VAL_CUST', (string) ($a_set[$field] ?: ''));
249  $this->tpl->parseCurrentBlock();
250  break;
251  }
252  }
253 
254  if ($this->getType() == self::TYPE_GLOBAL_SEARCH) {
255  if (ilBuddySystem::getInstance()->isEnabled() && $a_set['usr_id'] != $this->user->getId()) {
256  $this->tpl->setVariable('CONTACT_ACTIONS', ilBuddySystemLinkButton::getInstanceByUserId((int) $a_set['usr_id'])->getHtml());
257  } else {
258  $this->tpl->setVariable('CONTACT_ACTIONS', '');
259  }
260  }
261  }
262 
263  public function parseUserIds(array $a_user_ids): bool
264  {
265  if (!$a_user_ids) {
266  $this->setData(array());
267  return true;
268  }
269 
270  $additional_fields = $this->getSelectedColumns();
271 
272  $parse_access = false;
273  if (isset($additional_fields['access_until'])) {
274  $parse_access = true;
275  unset($additional_fields['access_until']);
276  }
277 
278  $udf_ids = $usr_data_fields = $odf_ids = array();
279  foreach ($additional_fields as $field) {
280  if ($field == 'org_units') {
281  continue;
282  }
283  if (substr($field, 0, 3) == 'udf') {
284  $udf_ids[] = substr($field, 4);
285  continue;
286  }
287  $usr_data_fields[] = $field;
288  }
289 
290  $u_query = new ilUserQuery();
291  $u_query->setOrderField('login');
292  $u_query->setOrderDirection('ASC');
293  $u_query->setLimit(999999);
294 
295  if (!ilSearchSettings::getInstance()->isInactiveUserVisible() && $this->getUserLimitations()) {
296  $u_query->setActionFilter("active");
297  }
298 
299  if (!ilSearchSettings::getInstance()->isLimitedUserVisible() && $this->getUserLimitations()) {
300  $u_query->setAccessFilter(true);
301  }
302 
303  $u_query->setAdditionalFields($usr_data_fields);
304  $u_query->setUserFilter($a_user_ids);
305 
306  $usr_data = $u_query->query();
307 
308 
309  if ($this->admin_mode && $parse_access) {
310  // see ilUserTableGUI
311  $current_time = time();
312  foreach ($usr_data['set'] as $k => $user) {
313  if ($user['active']) {
314  if ($user["time_limit_unlimited"]) {
315  $txt_access = $this->lng->txt("access_unlimited");
316  $usr_data["set"][$k]["access_class"] = "smallgreen";
317  } elseif ($user["time_limit_until"] < $current_time) {
318  $txt_access = $this->lng->txt("access_expired");
319  $usr_data["set"][$k]["access_class"] = "smallred";
320  } else {
321  $txt_access = ilDatePresentation::formatDate(new ilDateTime($user["time_limit_until"], IL_CAL_UNIX));
322  $usr_data["set"][$k]["access_class"] = "small";
323  }
324  } else {
325  $txt_access = $this->lng->txt("inactive");
326  $usr_data["set"][$k]["access_class"] = "smallred";
327  }
328  $usr_data["set"][$k]["access_until"] = $txt_access;
329  }
330  }
331 
332  // Custom user data fields
333  if ($udf_ids) {
334  $data = ilUserDefinedData::lookupData($a_user_ids, $udf_ids);
335 
336  $users = array();
337  $counter = 0;
338  foreach ($usr_data['set'] as $set) {
339  $users[$counter] = $set;
340  foreach ($udf_ids as $udf_field) {
341  $users[$counter]['udf_' . $udf_field] = ($data[(int) ($set['usr_id'] ?? 0)][$udf_field] ?? '');
342  }
343  ++$counter;
344  }
345  } else {
346  $users = $usr_data['set'];
347  }
348 
349  if ($this->getType() == self::TYPE_GLOBAL_SEARCH) {
350  if ($this->getLuceneResult() instanceof ilLuceneSearchResult) {
351  foreach ($users as $counter => $ud) {
352  $users[$counter]['relevance'] = $this->getLuceneResult()->getRelevance((int) ($ud['usr_id']) ?? 0);
353  }
354  }
355  }
356  if ($this->getType() === self::TYPE_GLOBAL_SEARCH) {
357  $users = $this->applySortation($users);
358  }
359  $this->setData($users);
360  return true;
361  }
362 
363  public function getHTML(): string
364  {
365  $html = parent::getHTML();
366  if ($this->getType() == self::TYPE_GLOBAL_SEARCH) {
367  $html = $this->ui->renderer()->render($this->buildSortationViewControl()) . $html;
368  }
369  return $html;
370  }
371 
372  protected function applySortation(array $set): array
373  {
374  $sort = $this->getCurrentSortation();
375 
376  if ($sort === 'relevance') {
377  usort($set, function ($a, $b) {
378  return $b['relevance'] <=> $a['relevance'];
379  });
380  return $set;
381  }
382 
383  if (str_ends_with($sort, '_asc')) {
384  $col = substr($sort, 0, -4);
385  usort($set, function ($a, $b) use ($col) {
386  return [$a[$col], $b['relevance'] ?? ''] <=> [$b[$col], $a['relevance'] ?? ''];
387  });
388  } elseif (str_ends_with($sort, '_desc')) {
389  $col = substr($sort, 0, -5);
390  usort($set, function ($a, $b) use ($col) {
391  return [$b[$col], $b['relevance'] ?? ''] <=> [$a[$col], $a['relevance'] ?? ''];
392  });
393  }
394 
395  return $set;
396  }
397 
398  protected function getPossibleSortations(): array
399  {
400  $sorts = ['relevance' => $this->lng->txt('search_sort_relevance')];
401  $all_cols = $this->getSelectableColumns();
402  foreach ($this->getSelectedColumns() as $col) {
403  $sorts[$col . '_asc'] = sprintf(
404  $this->lng->txt('search_sort_generic_asc'),
405  $all_cols[$col]['txt']
406  );
407  $sorts[$col . '_desc'] = sprintf(
408  $this->lng->txt('search_sort_generic_desc'),
409  $all_cols[$col]['txt']
410  );
411  }
412  return $sorts;
413  }
414 
415  protected function getDefaultSortation(): string
416  {
417  $lucene_enabled = ($this->getType() == self::TYPE_GLOBAL_SEARCH) &&
418  ($this->getLuceneResult() instanceof ilLuceneSearchResult);
419  return $lucene_enabled ? 'relevance' : 'login_asc';
420  }
421 }
static lookupData(array $a_user_ids, array $a_field_ids)
Lookup data.
setData(array $a_data)
$res
Definition: ltiservices.php:66
enable(string $a_module_name)
const IL_CAL_DATETIME
setFormAction(string $a_form_action, bool $a_multipart=false)
setEnableTitle(bool $a_enabletitle)
const SYSTEM_ROLE_ID
Definition: constants.php:29
trait ilSearchResultTableHelper
setSelectAllCheckbox(string $a_select_all_checkbox, bool $a_select_all_on_top=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getProfileLink(int $a_usr_id)
Get link to personal profile Return empty string in case of not public profile.
const IL_CAL_UNIX
setId(string $a_val)
__construct($a_parent_obj, $a_parent_cmd, $a_admin_mode=false, $a_type=self::TYPE_STANDARD)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
addMultiItemSelectionButton(string $a_sel_var, array $a_options, string $a_cmd, string $a_text, string $a_default_selection='')
Provides fluid interface to RBAC services.
Definition: UIServices.php:24
Search result implementing iterator interface.
setExternalSorting(bool $a_val)
setShowRowsSelector(bool $a_value)
Toggle rows-per-page selector.
static http()
Fetches the global http state from ILIAS.
setDefaultOrderField(string $a_defaultorderfield)
setRowTemplate(string $a_template, string $a_template_dir="")
Set row template.
global $DIC
Definition: shib_login.php:26
setDefaultOrderDirection(string $a_defaultorderdirection)
setTitle(string $a_title, string $a_icon="", string $a_icon_alt="")
static _getPersonalPicturePath(int $a_usr_id, string $a_size='small', bool $a_force_pic=false, bool $a_prevent_no_photo_image=false, bool $html_export=false)
const IL_CAL_DATE
static getSelectableColumnInfo(bool $a_admin=false)
Get info of searchable fields for selectable columns in table gui.
__construct(Container $dic, ilPlugin $plugin)
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
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)
addMultiCommand(string $a_cmd, string $a_text)