ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMailMemberSearchDataProvider.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 {
28  protected int $ref_id;
29  protected string $type = 'crs';
30  protected array $data = [];
36  protected array $roleSortWeightMap = [
37  'il_crs_a' => 10,
38  'il_grp_a' => 10,
39  'il_crs_t' => 9,
40  'il_crs_m' => 8,
41  'il_grp_m' => 8,
42  ];
43  protected ilLanguage $lng;
44 
45 
46  public function __construct(ilParticipants $objParticipants, int $a_ref_id)
47  {
48  global $DIC;
49 
50  $this->dataCache = $DIC['ilObjDataCache'];
51  $this->access = $DIC->access();
52  $this->objParticipants = $objParticipants;
53  $this->type = $this->objParticipants->getType();
54  $this->lng = $DIC['lng'];
55 
56  $this->ref_id = $a_ref_id;
57 
58  $this->collectTableData();
59  }
60 
61  private function collectTableData(): void
62  {
63  $participants = $this->objParticipants->getParticipants();
64  if ($this->type === 'crs' || $this->type === 'grp') {
65  $participants = $this->access->filterUserIdsByRbacOrPositionOfCurrentUser(
66  'read',
67  'manage_members',
68  $this->ref_id,
69  $participants
70  );
71  }
72 
73  $preloadedRoleIds = [];
74  foreach ($participants as $user_id) {
75  $user = ilObjectFactory::getInstanceByObjId($user_id, false);
76  if (!($user instanceof ilObjUser)) {
77  continue;
78  }
79 
80  if (!$user->getActive()) {
81  continue;
82  }
83 
84  $login = $user->getLogin();
85 
86  $publicName = '';
87  if (in_array($user->getPref('public_profile'), ['g', 'y'])) {
88  $publicName = $user->getLastname() . ', ' . $user->getFirstname();
89  }
90 
91  $this->data[$user_id]['user_id'] = $user_id;
92  $this->data[$user_id]['login'] = $login;
93  $this->data[$user_id]['name'] = $publicName;
94 
95  $assignedRoles = $this->objParticipants->getAssignedRoles($user_id);
96  $rolesToPreload = array_diff($assignedRoles, $preloadedRoleIds);
97  $this->dataCache->preloadObjectCache($rolesToPreload);
98 
99  $roleTitles = [];
100  foreach ($assignedRoles as $roleId) {
101  $preloadedRoleIds[$roleId] = $roleId;
102  $title = $this->dataCache->lookupTitle((int) $roleId);
103  $roleTitles[] = $title;
104  }
105 
106  $roleTitles = $this->sortRoles($roleTitles);
107 
108  $roleTitles = array_map(function (string $roleTitle): string {
109  return $this->buildRoleTitle($roleTitle);
110  }, $roleTitles);
111 
112  $this->data[$user_id]['role'] = implode(', ', $roleTitles);
113  }
114  }
115 
120  private function sortRoles(array $roleTitles): array
121  {
122  usort($roleTitles, function (string $a, string $b): int {
123  $leftPrefixTitle = substr($a, 0, 8);
124  $rightPrefixTitle = substr($b, 0, 8);
125 
126  $leftRating = $this->roleSortWeightMap[$leftPrefixTitle] ?? 0;
127  $rightRating = $this->roleSortWeightMap[$rightPrefixTitle] ?? 0;
128 
129  if ($leftRating > 0 || $rightRating > 0) {
130  if ($leftRating !== $rightRating) {
131  return $rightRating - $leftRating > 0 ? 1 : -1;
132  }
133 
134  return 0;
135  }
136 
137  return strcmp($a, $b);
138  });
139 
140  return $roleTitles;
141  }
142 
143  private function buildRoleTitle(string $role): string
144  {
145  return ilObjRole::_getTranslation($role);
146  }
147 
148  public function getData(): array
149  {
150  return $this->data;
151  }
152 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
static _getTranslation(string $a_role_title)
__construct(ilParticipants $objParticipants, int $a_ref_id)
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.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
Class ilMailMemberSearchDataProvider.