ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilOrgUnitUserAssignmentDBRepository.php
Go to the documentation of this file.
1 <?php
19 declare(strict_types=1);
20 
22 {
23  public const TABLE_NAME = 'il_orgu_ua';
24  protected ilDBInterface $db;
27 
28  public function __construct(ilDBInterface $db, ilAppEventHandler $handler = null)
29  {
30  $this->db = $db;
31 
32  if ($handler) {
33  $this->ilAppEventHandler = $handler;
34  } else {
35  global $DIC;
36  $this->ilAppEventHandler = $DIC->event();
37  }
38  }
39 
41  {
42  if (!isset($this->positionRepo)) {
44  $this->positionRepo = $dic["repo.Positions"];
45  }
46 
47  return $this->positionRepo;
48  }
49 
50  public function get(int $user_id, int $position_id, int $orgu_id): ilOrgUnitUserAssignment
51  {
52  $query = 'SELECT id, user_id, position_id, orgu_id FROM' . PHP_EOL
53  . self::TABLE_NAME
54  . ' WHERE ' . self::TABLE_NAME . '.user_id = ' . $this->db->quote($user_id, 'integer') . PHP_EOL
55  . ' AND ' . self::TABLE_NAME . '.position_id = ' . $this->db->quote($position_id, 'integer') . PHP_EOL
56  . ' AND ' . self::TABLE_NAME . '.orgu_id = ' . $this->db->quote($orgu_id, 'integer');
57 
58  $res = $this->db->query($query);
59  if ($res->numRows() > 0) {
60  $rec = $this->db->fetchAssoc($res);
61  return (new ilOrgUnitUserAssignment((int) $rec['id']))
62  ->withUserId((int) $rec['user_id'])
63  ->withPositionId((int) $rec['position_id'])
64  ->withOrguId((int) $rec['orgu_id']);
65  }
66 
67  $assignment = (new ilOrgUnitUserAssignment())
68  ->withUserId($user_id)
69  ->withPositionId($position_id)
70  ->withOrguId($orgu_id);
71  $assignment = $this->store($assignment);
72  return $assignment;
73  }
74 
75  public function find(int $user_id, int $position_id, int $orgu_id): ?ilOrgUnitUserAssignment
76  {
77  $query = 'SELECT id, user_id, position_id, orgu_id FROM' . PHP_EOL
78  . self::TABLE_NAME
79  . ' WHERE ' . self::TABLE_NAME . '.user_id = ' . $this->db->quote($user_id, 'integer') . PHP_EOL
80  . ' AND ' . self::TABLE_NAME . '.position_id = ' . $this->db->quote($position_id, 'integer') . PHP_EOL
81  . ' AND ' . self::TABLE_NAME . '.orgu_id = ' . $this->db->quote($orgu_id, 'integer');
82 
83  $res = $this->db->query($query);
84  if ($res->numRows() === 0) {
85  return null;
86  }
87 
88  $rec = $this->db->fetchAssoc($res);
89  return (new ilOrgUnitUserAssignment((int) $rec['id']))
90  ->withUserId((int) $rec['user_id'])
91  ->withPositionId((int) $rec['position_id'])
92  ->withOrguId((int) $rec['orgu_id']);
93  }
94 
96  {
97  if ($assignment->getId() === 0) {
98  $assignment = $this->insert($assignment);
99  } else {
100  $this->update($assignment);
101  }
102 
103  $this->raiseEvent('assignUserToPosition', $assignment);
104 
105  return $assignment;
106  }
107 
108  protected function insert(ilOrgUnitUserAssignment $assignment): ilOrgUnitUserAssignment
109  {
110  $id = $this->db->nextId(self::TABLE_NAME);
111 
112  $values = [
113  'id' => [ 'integer', $id ],
114  'user_id' => [ 'integer', $assignment->getUserId() ],
115  'position_id' => [ 'integer', $assignment->getPositionId() ],
116  'orgu_id' => [ 'integer', $assignment->getOrguId() ]
117  ];
118 
119  $this->db->insert(self::TABLE_NAME, $values);
120 
121  $ret = (new ilOrgUnitUserAssignment($id))
122  ->withUserId($assignment->getUserId())
123  ->withPositionId($assignment->getPositionId())
124  ->withOrguId($assignment->getOrguId());
125 
126  return $ret;
127  }
128 
129  protected function update(ilOrgUnitUserAssignment $assignment): void
130  {
131  $where = [ 'id' => [ 'integer', $assignment->getId() ] ];
132 
133  $values = [
134  'user_id' => [ 'integer', $assignment->getUserId() ],
135  'position_id' => [ 'integer', $assignment->getPositionId(),
136  'orgu_id' => [ 'integer', $assignment->getOrguId() ]]
137  ];
138 
139  $this->db->update(self::TABLE_NAME, $values, $where);
140  }
141 
142  public function delete(ilOrgUnitUserAssignment $assignment): bool
143  {
144  if ($assignment->getId() === 0) {
145  return false;
146  }
147 
148  $query = 'DELETE FROM ' . self::TABLE_NAME . PHP_EOL
149  . ' WHERE id = ' . $this->db->quote($assignment->getId(), 'integer');
150  $rows = $this->db->manipulate($query);
151 
152  if ($rows > 0) {
153  $this->raiseEvent('deassignUserFromPosition', $assignment);
154  return true;
155  }
156 
157  return false;
158  }
159 
160  public function deleteByUser(int $user_id): bool
161  {
162  if ($user_id <= 0) {
163  return false;
164  }
165 
166  $query = 'DELETE FROM ' . self::TABLE_NAME . PHP_EOL
167  . ' WHERE user_id = ' . $this->db->quote($user_id, 'integer');
168  $rows = $this->db->manipulate($query);
169 
170  if ($rows > 0) {
171  return true;
172  }
173 
174  return false;
175  }
176 
177  public function getByUsers(array $user_ids): array
178  {
179  $query = 'SELECT id, user_id, position_id, orgu_id FROM' . PHP_EOL
180  . self::TABLE_NAME
181  . ' WHERE ' . $this->db->in('user_id', $user_ids, false, 'integer');
182  $res = $this->db->query($query);
183  $ret = [];
184  while ($rec = $this->db->fetchAssoc($res)) {
185  $ret[] = (new ilOrgUnitUserAssignment((int) $rec['id']))
186  ->withUserId((int) $rec['user_id'])
187  ->withPositionId((int) $rec['position_id'])
188  ->withOrguId((int) $rec['orgu_id']);
189  }
190  return $ret;
191  }
192 
193  public function getByPosition(int $position_id): array
194  {
195  $query = 'SELECT id, user_id, position_id, orgu_id FROM' . PHP_EOL
196  . self::TABLE_NAME
197  . ' WHERE ' . self::TABLE_NAME . '.position_id = ' . $this->db->quote($position_id, 'integer');
198  $res = $this->db->query($query);
199  $ret = [];
200  while ($rec = $this->db->fetchAssoc($res)) {
201  $ret[] = (new ilOrgUnitUserAssignment((int) $rec['id']))
202  ->withUserId((int) $rec['user_id'])
203  ->withPositionId((int) $rec['position_id'])
204  ->withOrguId((int) $rec['orgu_id']);
205  }
206  return $ret;
207  }
208 
209  public function getByOrgUnit(int $orgu_id): array
210  {
211  $query = 'SELECT id, user_id, position_id, orgu_id FROM' . PHP_EOL
212  . self::TABLE_NAME
213  . ' WHERE ' . self::TABLE_NAME . '.orgu_id = ' . $this->db->quote($orgu_id, 'integer');
214  $res = $this->db->query($query);
215  $ret = [];
216  while ($rec = $this->db->fetchAssoc($res)) {
217  $ret[] = (new ilOrgUnitUserAssignment((int) $rec['id']))
218  ->withUserId((int) $rec['user_id'])
219  ->withPositionId((int) $rec['position_id'])
220  ->withOrguId((int) $rec['orgu_id']);
221  }
222  return $ret;
223  }
224 
225 
226  public function getByUserAndPosition(int $user_id, int $position_id): array
227  {
228  $query = 'SELECT id, user_id, position_id, orgu_id FROM' . PHP_EOL
229  . self::TABLE_NAME
230  . ' WHERE ' . self::TABLE_NAME . '.user_id = ' . $this->db->quote($user_id, 'integer') . PHP_EOL
231  . ' AND ' . self::TABLE_NAME . '.position_id = ' . $this->db->quote($position_id, 'integer');
232  $res = $this->db->query($query);
233  $ret = [];
234  while ($rec = $this->db->fetchAssoc($res)) {
235  $ret[] = (new ilOrgUnitUserAssignment((int) $rec['id']))
236  ->withUserId((int) $rec['user_id'])
237  ->withPositionId((int) $rec['position_id'])
238  ->withOrguId((int) $rec['orgu_id']);
239  }
240  return $ret;
241  }
242 
243  public function getUsersByOrgUnits(array $orgu_ids): array
244  {
245  $query = 'SELECT user_id FROM' . PHP_EOL
246  . self::TABLE_NAME
247  . ' WHERE ' . $this->db->in(self::TABLE_NAME . '.orgu_id', $orgu_ids, false, 'integer');
248  $res = $this->db->query($query);
249  $users = [];
250  while ($rec = $this->db->fetchAssoc($res)) {
251  $users[] = (int) $rec['user_id'];
252  }
253  return $users;
254  }
255 
256  public function getUsersByPosition(int $position_id): array
257  {
258  $query = 'SELECT user_id FROM' . PHP_EOL
259  . self::TABLE_NAME
260  . ' WHERE ' . self::TABLE_NAME . '.position_id = ' . $this->db->quote($position_id, 'integer');
261  $res = $this->db->query($query);
262  $users = [];
263  while ($rec = $this->db->fetchAssoc($res)) {
264  $users[] = (int) $rec['user_id'];
265  }
266  return $users;
267  }
268 
269  public function getUsersByOrgUnitsAndPosition(array $orgu_ids, int $position_id): array
270  {
271  $query = 'SELECT user_id FROM' . PHP_EOL
272  . self::TABLE_NAME
273  . ' WHERE ' . $this->db->in(self::TABLE_NAME . '.orgu_id', $orgu_ids, false, 'integer') . PHP_EOL
274  . ' AND ' . self::TABLE_NAME . '.position_id = ' . $this->db->quote($position_id, 'integer');
275  $res = $this->db->query($query);
276  $users = [];
277  while ($rec = $this->db->fetchAssoc($res)) {
278  $users[] = (int) $rec['user_id'];
279  }
280  return $users;
281  }
282 
283  public function getUsersByUserAndPosition(int $user_id, int $position_id, bool $recursive = false): array
284  {
285  $orgu_ids = $this->getOrgUnitsByUserAndPosition($user_id, $position_id, $recursive);
286 
287  $query = 'SELECT user_id FROM' . PHP_EOL
288  . self::TABLE_NAME
289  . ' WHERE ' . $this->db->in(self::TABLE_NAME . '.orgu_id', $orgu_ids, false, 'integer');
290  $res = $this->db->query($query);
291  $users = [];
292  while ($rec = $this->db->fetchAssoc($res)) {
293  $users[] = (int) $rec['user_id'];
294  }
295  return $users;
296  }
297 
298  public function getFilteredUsersByUserAndPosition(int $user_id, int $position_id, int $position_filter_id, bool $recursive = false): array
299  {
300  $orgu_ids = $this->getOrgUnitsByUserAndPosition($user_id, $position_id, $recursive);
301 
302  $query = 'SELECT user_id FROM' . PHP_EOL
303  . self::TABLE_NAME
304  . ' WHERE ' . $this->db->in(self::TABLE_NAME . '.orgu_id', $orgu_ids, false, 'integer') . PHP_EOL
305  . ' AND ' . self::TABLE_NAME . '.position_id = ' . $this->db->quote($position_filter_id, 'integer');
306  $res = $this->db->query($query);
307  $users = [];
308  while ($rec = $this->db->fetchAssoc($res)) {
309  $users[] = (int) $rec['user_id'];
310  }
311  return $users;
312  }
313 
314  public function getOrgUnitsByUser(int $user_id): array
315  {
316  $query = 'SELECT orgu_id FROM' . PHP_EOL
317  . self::TABLE_NAME
318  . ' WHERE ' . self::TABLE_NAME . '.user_id = ' . $this->db->quote($user_id, 'integer');
319  $res = $this->db->query($query);
320  $orgu_ids = [];
321  while ($rec = $this->db->fetchAssoc($res)) {
322  $orgu_ids[] = (int) $rec['orgu_id'];
323  }
324  return $orgu_ids;
325  }
326 
327  public function getOrgUnitsByUserAndPosition(int $user_id, int $position_id, bool $recursive = false): array
328  {
329  $query = 'SELECT orgu_id FROM' . PHP_EOL
330  . self::TABLE_NAME
331  . ' WHERE ' . self::TABLE_NAME . '.user_id = ' . $this->db->quote($user_id, 'integer') . PHP_EOL
332  . ' AND ' . self::TABLE_NAME . '.position_id = ' . $this->db->quote($position_id, 'integer');
333  $res = $this->db->query($query);
334  $orgu_ids = [];
335  while ($rec = $this->db->fetchAssoc($res)) {
336  $orgu_ids[] = (int) $rec['orgu_id'];
337  }
338 
339  if (!$recursive) {
340  return $orgu_ids;
341  }
342 
343  $recursive_orgu_ids = [];
345  foreach ($orgu_ids as $orgu_id) {
346  $recursive_orgu_ids = array_merge($recursive_orgu_ids, $tree->getAllChildren($orgu_id));
347  }
348  return $recursive_orgu_ids;
349  }
350 
351  public function getPositionsByUser(int $user_id): array
352  {
353  $query = 'SELECT DISTINCT position_id FROM' . PHP_EOL
354  . self::TABLE_NAME
355  . ' WHERE ' . self::TABLE_NAME . '.user_id = ' . $this->db->quote($user_id, 'integer');
356  $res = $this->db->query($query);
357 
358  $positions = [];
359  while ($rec = $this->db->fetchAssoc($res)) {
360  $positions[] = $this->getPositionRepo()->getSingle((int) $rec['position_id'], 'id');
361  }
362  return $positions;
363  }
364 
365  public function getSuperiorsByUsers(array $user_ids): array
366  {
367  $query = 'SELECT ' . PHP_EOL
368  . ' ua.orgu_id AS orgu_id,' . PHP_EOL
369  . ' ua.user_id AS empl,' . PHP_EOL
370  . ' ua2.user_id as sup' . PHP_EOL
371  . ' FROM' . PHP_EOL
372  . self::TABLE_NAME . ' as ua,' . PHP_EOL
373  . self::TABLE_NAME . ' as ua2' . PHP_EOL
374  . ' WHERE ua.orgu_id = ua2.orgu_id' . PHP_EOL
375  . ' AND ua.user_id <> ua2.user_id' . PHP_EOL
376  . ' AND ua.position_id = ' . $this->db->quote(ilOrgUnitPosition::CORE_POSITION_EMPLOYEE, 'integer') . PHP_EOL
377  . ' AND ua2.position_id = ' . $this->db->quote(ilOrgUnitPosition::CORE_POSITION_SUPERIOR, 'integer') . PHP_EOL
378  . ' AND ' . $this->db->in('ua.user_id', $user_ids, false, 'integer');
379  $res = $this->db->query($query);
380  if ($res->numRows() === 0) {
381  return [];
382  }
383 
384  $ret = [];
385  while ($rec = $this->db->fetchAssoc($res)) {
386  $ret[$rec['empl']][] = $rec['sup'];
387  }
388  return $ret;
389  }
390 
391  protected function raiseEvent(string $event, ilOrgUnitUserAssignment $assignment): void
392  {
393  $this->ilAppEventHandler->raise('Modules/OrgUnit', $event, array(
394  'obj_id' => $assignment->getOrguId(),
395  'usr_id' => $assignment->getUserId(),
396  'position_id' => $assignment->getPositionId()
397  ));
398  }
399 }
getUsersByPosition(int $position_id)
Get all users with a certain position.
$res
Definition: ltiservices.php:69
Global event handler.
getPositionsByUser(int $user_id)
Get all positions a user is assigned to.
getFilteredUsersByUserAndPosition(int $user_id, int $position_id, int $position_filter_id, bool $recursive=false)
Get all users with position $position_filter_id from those org-units, where the user has position $po...
getUsersByOrgUnits(array $orgu_ids)
Get all users for a given set of org-units.
getByUserAndPosition(int $user_id, int $position_id)
Get assignments for a user in a dedicated position.
getOrgUnitsByUser(int $user_id)
Get all org-units a user is assigned to.
global $DIC
Definition: feed.php:28
deleteByUser(int $user_id)
Delete all assignments for a user_id Returns false if no assignments were found.
getUsersByUserAndPosition(int $user_id, int $position_id, bool $recursive=false)
Get all users from org-units where the user has a certain position i.e.
getByOrgUnit(int $orgu_id)
Get all assignments for an org-unit.
__construct(ilDBInterface $db, ilAppEventHandler $handler=null)
find(int $user_id, int $position_id, int $orgu_id)
Find assignment for user, position and org-unit Does not create new assigment, returns null if no ass...
getSuperiorsByUsers(array $user_ids)
Get all superiors of one or more users $user_id => [ $superior_ids ].
store(ilOrgUnitUserAssignment $assignment)
Store assignment to db.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$dic
Definition: result.php:32
getByPosition(int $position_id)
Get all assignments for a position.
getOrgUnitsByUserAndPosition(int $user_id, int $position_id, bool $recursive=false)
Get all org-units where a user has a dedicated position.
raiseEvent(string $event, ilOrgUnitUserAssignment $assignment)
getUsersByOrgUnitsAndPosition(array $orgu_ids, int $position_id)
Get all users in a specific position for a given set of org-units.
raise(string $a_component, string $a_event, array $a_parameter=[])
Raise an event.
getByUsers(array $user_ids)
Get assignments for one or more users.
$handler
Definition: index.php:18