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