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