ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilOrgUnitUserRepository.php
Go to the documentation of this file.
1 <?php
2 
20 namespace OrgUnit\User;
21 
24 
30 {
31  protected \ILIAS\DI\Container $dic;
32  protected \ilOrgUnitPositionDBRepository $positionRepo;
33 
37  protected static array $instance;
41  protected array $orgu_users;
42  protected bool $with_superiors = false;
43  protected bool $with_positions = false;
44  protected \ilOrgUnitUserAssignmentDBRepository $assignmentRepo;
45 
49  public function __construct()
50  {
51  global $DIC;
52  $this->dic = $DIC;
53  }
54 
56  {
57  if (!isset($this->positionRepo)) {
58  $dic = \ilOrgUnitLocalDIC::dic();
59  $this->positionRepo = $dic["repo.Positions"];
60  }
61 
62  return $this->positionRepo;
63  }
64 
66  {
67  if (!isset($this->assignmentRepo)) {
68  $dic = \ilOrgUnitLocalDIC::dic();
69  $this->assignmentRepo = $dic["repo.UserAssignments"];
70  }
71 
72  return $this->assignmentRepo;
73  }
74 
79  {
80  $this->with_superiors = true;
81 
82  return $this;
83  }
84 
89  {
90  $this->with_positions = true;
91 
92  return $this;
93  }
94 
99  public function getOrgUnitUsers(array $arr_user_id): array
100  {
101  $this->orgu_users = $this->loadUsersByUserIds($arr_user_id);
102 
103  if ($this->with_superiors === true) {
104  $this->loadSuperiors($arr_user_id);
105  }
106 
107  if ($this->with_positions === true) {
108  $this->loadPositions($arr_user_id);
109  }
110 
111  return $this->orgu_users;
112  }
113 
118  public function getOrgUnitUser(int $user_id): ?ilOrgUnitUser
119  {
120  $this->orgu_users = $this->loadUsersByUserIds([$user_id]);
121 
122  if (count($this->orgu_users) == 0) {
123  return null;
124  }
125 
126  if ($this->with_superiors === true) {
127  $this->loadSuperiors([$user_id]);
128  }
129 
130  return $this->orgu_users[0];
131  }
132 
136  public function loadSuperiors(array $user_ids): void
137  {
138  global $DIC;
139 
140  $st = $DIC->database()->query($this->getSuperiorsSql($user_ids));
141 
142  $empl_id_sup_ids = [];
143  while ($data = $DIC->database()->fetchAssoc($st)) {
144  $org_unit_user = ilOrgUnitUser::getInstanceById($data['empl_usr_id']);
145  $superior = ilOrgUnitUser::getInstance(
146  $data['sup_usr_id'],
147  (string) $data['sup_login'],
148  (string) $data['sup_email'],
149  (string) $data['sup_second_email']
150  );
151  $org_unit_user->addSuperior($superior);
152  }
153  }
154 
159  public function getEmailAdressesOfSuperiors(array $user_ids): array
160  {
161  global $DIC;
162 
163  $st = $DIC->database()->query($this->getSuperiorsSql($user_ids));
164 
165  $arr_email_sup = [];
166  while ($data = $DIC->database()->fetchAssoc($st)) {
167  $arr_email_sup[] = $data['sup_email'];
168  }
169 
170  return $arr_email_sup;
171  }
172 
177  protected function getSuperiorsSql(array $user_ids): string
178  {
179  global $DIC;
180 
181  $sql = "SELECT
182  orgu_ua.orgu_id AS orgu_id,
183  orgu_ua.user_id AS empl_usr_id,
184  orgu_ua2.user_id as sup_usr_id,
185  superior.email as sup_email,
186  superior.second_email as sup_second_email,
187  superior.login as sup_login
188  FROM
189  il_orgu_ua as orgu_ua,
190  il_orgu_ua as orgu_ua2
191  inner join usr_data as superior on superior.usr_id = orgu_ua2.user_id
192  WHERE
193  orgu_ua.orgu_id = orgu_ua2.orgu_id
194  and orgu_ua.user_id <> orgu_ua2.user_id
195  and orgu_ua.position_id = " . \ilOrgUnitPosition::CORE_POSITION_EMPLOYEE . "
196  and orgu_ua2.position_id = " . \ilOrgUnitPosition::CORE_POSITION_SUPERIOR . "
197  AND " . $DIC->database()->in('orgu_ua.user_id', $user_ids, false, 'integer');
198 
199  return $sql;
200  }
201 
202  public function loadPositions(array $user_ids): void
203  {
204  $assignments = $this->getAssignmentRepo()
205  ->getByUsers($user_ids);
206  foreach ($assignments as $assignment) {
207  $org_unit_user = ilOrgUnitUser::getInstanceById($assignment->getUserId());
208  $org_unit_user->addPositions($this->getPositionRepo()->getSingle($assignment->getPositionId(), 'id'));
209  }
210  }
211 
216  private function loadUsersByUserIds(array $user_ids): array
217  {
218  $users = array();
219 
220  $q = "SELECT * FROM usr_data WHERE " . $this->dic->database()->in('usr_id', $user_ids, false, 'int');
221 
222  $set = $this->dic->database()->query($q);
223 
224  while ($row = $this->dic->database()->fetchAssoc($set)) {
225  $users[] = ilOrgUnitUser::getInstance(
226  $row['usr_id'],
227  (string) $row['login'],
228  (string) $row['email'],
229  (string) $row['second_email']
230  );
231  }
232 
233  return $users;
234  }
235 }
static getInstanceById(int $user_id)
__construct()
ilOrgUnitUserRepository constructor.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilOrgUnitUserAssignmentDBRepository $assignmentRepo
static getInstance(int $user_id, string $login, string $email, string $second_email)
global $DIC
Definition: shib_login.php:25
$q
Definition: shib_logout.php:18