ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilOrgUnitPositionDBRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
25 class ilOrgUnitPositionDBRepository implements OrgUnitPositionRepository, Table\DataRetrieval
26 {
27  public const TABLE_NAME = 'il_orgu_positions';
28  private const TABLE_NAME_UA = 'il_orgu_ua';
29 
30  public function __construct(
31  protected ilDBInterface $db,
32  protected ilOrgUnitAuthorityDBRepository $authorityRepo,
33  protected ilOrgUnitUserAssignmentDBRepository $assignmentRepo,
34  protected ilLanguage $lng
35  ) {
36  $this->lng->loadLanguageModule('orgu');
37  }
38 
45  public function get(int|string $value, string $field): array
46  {
47  $fields = [
48  'id' => 'integer',
49  'core_identifier' => 'integer',
50  'title' => 'string'
51  ];
52  if (!in_array($field, array_keys($fields))) {
53  throw new Exception("Invalid field: " . $field);
54  }
55 
56  $query = 'SELECT id, title, description, core_position, core_identifier FROM' . PHP_EOL
57  . self::TABLE_NAME
58  . ' WHERE ' . self::TABLE_NAME . '.' . $field . ' = ' . $this->db->quote($value, $fields[$field]);
59  $res = $this->db->query($query);
60  $ret = [];
61  while ($rec = $this->db->fetchAssoc($res)) {
62  $ret[] = (new ilOrgUnitPosition((int) $rec['id']))
63  ->withTitle((string) $rec['title'])
64  ->withDescription((string) $rec['description'])
65  ->withCorePosition((bool) $rec['core_position'])
66  ->withCoreIdentifier((int) $rec['core_identifier'])
67  ->withAuthorities($this->authorityRepo->get((int) $rec['id'], ilOrgUnitAuthority::POSITION_ID));
68  }
69 
70  return $ret;
71  }
72 
76  public function getSingle(int|string $value, string $field): ?ilOrgUnitPosition
77  {
78  $pos = $this->get($value, $field);
79  if (count($pos) === 0) {
80  return null;
81  }
82 
83  return (array_shift($pos));
84  }
85 
91  public function getAllPositions(
92  ?Range $range = null,
93  ?Order $order = null
94  ): array {
95  $sql_order_part = $order ? $order->join('ORDER BY', fn(...$o) => implode(' ', $o)) : '';
96  $sql_range_part = $range ? sprintf('LIMIT %2$s OFFSET %1$s', ...$range->unpack()) : '';
97 
98  $query = 'SELECT id, title, description, core_position, core_identifier FROM' . PHP_EOL
99  . self::TABLE_NAME . PHP_EOL
100  . $sql_order_part . PHP_EOL
101  . $sql_range_part;
102 
103  $res = $this->db->query($query);
104  $ret = [];
105  while ($rec = $this->db->fetchAssoc($res)) {
106  $ret[] = (new ilOrgUnitPosition((int) $rec['id']))
107  ->withTitle((string) $rec['title'])
108  ->withDescription((string) $rec['description'])
109  ->withCorePosition((bool) $rec['core_position'])
110  ->withCoreIdentifier((int) $rec['core_identifier'])
111  ->withAuthorities($this->authorityRepo->get((int) $rec['id'], ilOrgUnitAuthority::POSITION_ID));
112  }
113 
114  return $ret;
115  }
116 
117  protected function getAllPositionsCount(): int
118  {
119  $query = 'SELECT id FROM ' . self::TABLE_NAME;
120  $res = $this->db->query($query);
121  return $this->db->numRows($res);
122  }
123 
129  public function getArray(?string $key = null, ?string $field = null): array
130  {
131  if (!in_array($key, ['id', null])) {
132  throw new Exception("Invalid key: " . $field);
133  }
134 
135  $fields = [
136  'id' => 'int',
137  'title' => 'string',
138  'description' => 'string',
139  'core_identifier' => 'int',
140  'core_position' => 'int'
141  ];
142  if (!in_array($field, array_keys($fields)) && $field !== null) {
143  throw new Exception("Invalid field: " . $field);
144  }
145 
146  if ($field !== null && $this->db->tableColumnExists(self::TABLE_NAME, $field)) {
147  $query = 'SELECT id, ' . $field . ' FROM' . PHP_EOL
148  . self::TABLE_NAME
149  . ' WHERE 1';
150 
151  $res = $this->db->query($query);
152  $ret = [];
153  while ($rec = $this->db->fetchAssoc($res)) {
154  $value = $rec[$field];
155  if ($fields[$field] == 'int') {
156  $value = (int) $value;
157  } elseif ($fields[$field] == 'string') {
158  $value = (string) $value;
159  }
160  if ($key !== null) {
161  $ret[$rec[$key]] = $value;
162  } else {
163  $ret[] = $value;
164  }
165  }
166  } else {
167  $query = 'SELECT id, title, description, core_identifier, core_position FROM' . PHP_EOL
168  . self::TABLE_NAME
169  . ' WHERE 1';
170  $res = $this->db->query($query);
171  $ret = [];
172  while ($rec = $this->db->fetchAssoc($res)) {
173  if ($key !== null) {
174  $ret[$key] = $rec;
175  } else {
176  $ret[] = $rec;
177  }
178  }
179  }
180 
181  return $ret;
182  }
183 
190  public function getPositionsForOrgUnit(int $orgu_id): array
191  {
192  $query = 'SELECT DISTINCT ' . self::TABLE_NAME . '.id, ' . self::TABLE_NAME . '.*' . PHP_EOL
193  . 'FROM ' . self::TABLE_NAME . PHP_EOL
194  . 'LEFT JOIN ' . self::TABLE_NAME_UA . PHP_EOL
195  . 'ON ' . self::TABLE_NAME . '.id = ' . self::TABLE_NAME_UA . '.position_id' . PHP_EOL
196  . 'AND ' . self::TABLE_NAME_UA . '.orgu_id = ' . $this->db->quote($orgu_id, 'integer') . PHP_EOL
197  . 'WHERE ' . self::TABLE_NAME_UA . '.user_id IS NOT NULL' . PHP_EOL
198  . 'OR ' . self::TABLE_NAME . '.core_position = 1';
199  $res = $this->db->query($query);
200  $ret = [];
201  while ($rec = $this->db->fetchAssoc($res)) {
202  $ret[] = (new ilOrgUnitPosition((int) $rec['id']))
203  ->withTitle((string) $rec['title'])
204  ->withDescription((string) $rec['description'])
205  ->withCorePosition((bool) $rec['core_position'])
206  ->withCoreIdentifier((int) $rec['core_identifier'])
207  ->withAuthorities($this->authorityRepo->get((int) $rec['id'], ilOrgUnitAuthority::POSITION_ID));
208  }
209 
210  return $ret;
211  }
212 
213  public function create(): ilOrgUnitPosition
214  {
215  return new ilOrgUnitPosition();
216  }
217 
221  public function store(ilOrgUnitPosition $position): ilOrgUnitPosition
222  {
223  if ($position->getId() === 0) {
224  $position = $this->insert($position);
225  } else {
226  $this->update($position);
227  }
228 
229  $authorities = $position->getAuthorities();
230  $ids = [];
231  $new_authorities = [];
232  foreach ($authorities as $authority) {
233  $auth = $this->authorityRepo->store($authority->withPositionId($position->getId()));
234  $ids[] = $auth->getId();
235  $new_authorities[] = $auth;
236  }
237  $position = $position->withAuthorities($new_authorities);
238 
239  if (count($ids) > 0) {
240  $this->authorityRepo->deleteLeftoverAuthorities($ids, $position->getId());
241  }
242  if (count($ids) === 0) {
243  $authorities = $this->authorityRepo->get($position->getId(), 'position_id');
244  foreach ($authorities as $authority) {
245  $this->authorityRepo->delete($authority->getId());
246  }
247  }
248 
249  return $position;
250  }
251 
252  private function insert(ilOrgUnitPosition $position): ilOrgUnitPosition
253  {
254  $id = $this->db->nextId(self::TABLE_NAME);
255 
256  $values = [
257  'id' => [ 'integer', $id],
258  'title' => [ 'text', $position->getTitle() ],
259  'description' => [ 'text', $position->getDescription() ],
260  'core_position' => [ 'integer', ($position->isCorePosition()) ? 1 : 0 ],
261  'core_identifier' => [ 'integer', $position->getCoreIdentifier() ]
262  ];
263 
264  $this->db->insert(self::TABLE_NAME, $values);
265 
266  $ret = (new ilOrgUnitPosition($id))
267  ->withTitle($position->getTitle())
268  ->withDescription($position->getDescription())
269  ->withCorePosition($position->isCorePosition())
270  ->withCoreIdentifier($position->getCoreIdentifier())
271  ->withAuthorities($position->getAuthorities());
272 
273  return $ret;
274  }
275 
276  private function update(ilOrgUnitPosition $position): void
277  {
278  $where = [ 'id' => [ 'integer', $position->getId() ] ];
279 
280  $values = [
281  'title' => [ 'text', $position->getTitle() ],
282  'description' => [ 'text', $position->getDescription() ],
283  'core_position' => [ 'integer', (($position->isCorePosition()) ? 1 : 0) ],
284  'core_identifier' => [ 'integer', $position->getCoreIdentifier() ]
285  ];
286 
287  $this->db->update(self::TABLE_NAME, $values, $where);
288  }
289 
293  public function delete(int $position_id): void
294  {
295  $query = 'DELETE FROM ' . self::TABLE_NAME . PHP_EOL
296  . ' WHERE id = ' . $this->db->quote($position_id, 'integer');
297 
298  $this->db->manipulate($query);
299 
300  $authorities = $this->authorityRepo->get($position_id, 'position_id');
301  foreach ($authorities as $authority) {
302  $this->authorityRepo->delete($authority->getId());
303  }
304 
305  $assignments = $this->assignmentRepo->getByPosition($position_id);
306  foreach ($assignments as $assignment) {
307  $this->assignmentRepo->delete($assignment);
308  }
309  }
310 
314  public function getAuthority(?int $id): ilOrgUnitAuthority
315  {
316  if ($id === null) {
317  return $this->authorityRepo->create();
318  } else {
319  $authority = $this->authorityRepo->get($id, 'id');
320  return array_shift($authority);
321  }
322  }
323 
324  public function getTotalRowCount(
325  ?array $filter_data,
326  ?array $additional_parameters
327  ): ?int {
328  return $this->getAllPositionsCount();
329  }
330 
331  public function getRows(
332  Table\DataRowBuilder $row_builder,
333  array $visible_column_ids,
334  Range $range,
335  Order $order,
336  ?array $filter_data,
337  ?array $additional_parameters
338  ): \Generator {
339  foreach ($this->getAllPositions($range, $order) as $pos) {
340  $row_id = (string) $pos->getId();
341  $record = [
342  'title' => $pos->getTitle(),
343  'description' => $pos->getDescription(),
344  'authorities' => implode("<br>", $this->getAuthorityDescription($pos->getAuthorities())),
345  'is_core_position' => $pos->isCorePosition(),
346  ];
347 
348  yield $row_builder->buildDataRow($row_id, $record)
349  ->withDisabledAction('delete', $record['is_core_position']);
350  }
351  }
352 
353  private function getAuthorityDescription(array $authorities): array
354  {
355  $lang_keys = [
356  'in',
357  'over',
361  ];
362  $t = [];
363  foreach ($lang_keys as $key) {
364  $t[$key] = $this->lng->txt($key);
365  }
366 
367  $authority_description = [];
368  foreach ($authorities as $authority) {
369  switch ($authority->getOver()) {
371  $over_txt = $t["over_" . $authority->getOver()];
372  break;
373  default:
374  $over_txt = $this
375  ->getSingle($authority->getOver(), 'id')
376  ->getTitle();
377  break;
378  }
379 
380  $authority_description[] = implode(" ", [
381  $t["over"],
382  $over_txt,
383  $t["in"],
384  $t["scope_" . $authority->getScope()]
385  ]);
386  }
387 
388  return $authority_description;
389  }
390 }
$res
Definition: ltiservices.php:66
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
__construct(protected ilDBInterface $db, protected ilOrgUnitAuthorityDBRepository $authorityRepo, protected ilOrgUnitUserAssignmentDBRepository $assignmentRepo, protected ilLanguage $lng)
getAuthority(?int $id)
Gets or creates an authority object, as the authority repo is encapsulated into this repo...
getSingle(int|string $value, string $field)
Get a single position from a filtered query (see get())
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:28
getRows(Table\DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
withAuthorities(array $authorities)
getAllPositions(?Range $range=null, ?Order $order=null)
Returns all position objects.
getArray(?string $key=null, ?string $field=null)
Returns position data as an array, e.g.
getPositionsForOrgUnit(int $orgu_id)
Returns all core positions plus all positions with user assignments for a certain org unit (kept for ...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
global $lng
Definition: privfeed.php:31
store(ilOrgUnitPosition $position)
Saves position and its authorities.
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28
Class ilOrguAuthority.