ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilOrgUnitPositionDBRepository.php
Go to the documentation of this file.
1 <?php
19 declare(strict_types=1);
20 
22 {
23  public const TABLE_NAME = 'il_orgu_positions';
24  private const TABLE_NAME_UA = 'il_orgu_ua';
25  protected ilDBInterface $db;
28 
30  {
31  $this->db = $db;
32  $this->authorityRepo = $authorityRepo;
33  $this->assignmentRepo = $assignmentRepo;
34  }
35 
42  public function get(int|string $value, string $field): array
43  {
44  $fields = [
45  'id' => 'integer',
46  'core_identifier' => 'integer',
47  'title' => 'string'
48  ];
49  if (!in_array($field, array_keys($fields))) {
50  throw new Exception("Invalid field: " . $field);
51  }
52 
53  $query = 'SELECT id, title, description, core_position, core_identifier FROM' . PHP_EOL
54  . self::TABLE_NAME
55  . ' WHERE ' . self::TABLE_NAME . '.' . $field . ' = ' . $this->db->quote($value, $fields[$field]);
56  $res = $this->db->query($query);
57  $ret = [];
58  while ($rec = $this->db->fetchAssoc($res)) {
59  $ret[] = (new ilOrgUnitPosition((int) $rec['id']))
60  ->withTitle((string) $rec['title'])
61  ->withDescription((string) $rec['description'])
62  ->withCorePosition((bool) $rec['core_position'])
63  ->withCoreIdentifier((int) $rec['core_identifier'])
64  ->withAuthorities($this->authorityRepo->get((int) $rec['id'], ilOrgUnitAuthority::POSITION_ID));
65  }
66 
67  return $ret;
68  }
69 
73  public function getSingle(int|string $value, string $field): ?ilOrgUnitPosition
74  {
75  $pos = $this->get($value, $field);
76  if (count($pos) === 0) {
77  return null;
78  }
79 
80  return (array_shift($pos));
81  }
82 
88  public function getAllPositions(): array
89  {
90  $query = 'SELECT id, title, description, core_position, core_identifier FROM' . PHP_EOL
91  . self::TABLE_NAME . PHP_EOL
92  . 'WHERE 1';
93  $res = $this->db->query($query);
94  $ret = [];
95  while ($rec = $this->db->fetchAssoc($res)) {
96  $ret[] = (new ilOrgUnitPosition((int) $rec['id']))
97  ->withTitle((string) $rec['title'])
98  ->withDescription((string) $rec['description'])
99  ->withCorePosition((bool) $rec['core_position'])
100  ->withCoreIdentifier((int) $rec['core_identifier'])
101  ->withAuthorities($this->authorityRepo->get((int) $rec['id'], ilOrgUnitAuthority::POSITION_ID));
102  }
103 
104  return $ret;
105  }
106 
112  public function getArray(?string $key = null, ?string $field = null): array
113  {
114  if (!in_array($key, ['id', null])) {
115  throw new Exception("Invalid key: " . $field);
116  }
117 
118  $fields = [
119  'id' => 'int',
120  'title' => 'string',
121  'description' => 'string',
122  'core_identifier' => 'int',
123  'core_position' => 'int'
124  ];
125  if (!in_array($field, array_keys($fields)) && $field !== null) {
126  throw new Exception("Invalid field: " . $field);
127  }
128 
129  if ($field !== null && $this->db->tableColumnExists(self::TABLE_NAME, $field)) {
130  $query = 'SELECT id, ' . $field . ' FROM' . PHP_EOL
131  . self::TABLE_NAME
132  . ' WHERE 1';
133 
134  $res = $this->db->query($query);
135  $ret = [];
136  while ($rec = $this->db->fetchAssoc($res)) {
137  $value = $rec[$field];
138  if ($fields[$field] == 'int') {
139  $value = (int) $value;
140  } elseif ($fields[$field] == 'string') {
141  $value = (string) $value;
142  }
143  if ($key !== null) {
144  $ret[$rec[$key]] = $value;
145  } else {
146  $ret[] = $value;
147  }
148  }
149  } else {
150  $query = 'SELECT id, title, description, core_identifier, core_position FROM' . PHP_EOL
151  . self::TABLE_NAME
152  . ' WHERE 1';
153  $res = $this->db->query($query);
154  $ret = [];
155  while ($rec = $this->db->fetchAssoc($res)) {
156  if ($key !== null) {
157  $ret[$key] = $rec;
158  } else {
159  $ret[] = $rec;
160  }
161  }
162  }
163 
164  return $ret;
165  }
166 
173  public function getPositionsForOrgUnit(int $orgu_id): array
174  {
175  $query = 'SELECT DISTINCT ' . self::TABLE_NAME . '.id, ' . self::TABLE_NAME . '.*' . PHP_EOL
176  . 'FROM ' . self::TABLE_NAME . PHP_EOL
177  . 'LEFT JOIN ' . self::TABLE_NAME_UA . PHP_EOL
178  . 'ON ' . self::TABLE_NAME . '.id = ' . self::TABLE_NAME_UA . '.position_id' . PHP_EOL
179  . 'AND ' . self::TABLE_NAME_UA . '.orgu_id = ' . $this->db->quote($orgu_id, 'integer') . PHP_EOL
180  . 'WHERE ' . self::TABLE_NAME_UA . '.user_id IS NOT NULL' . PHP_EOL
181  . 'OR ' . self::TABLE_NAME . '.core_position = 1';
182  $res = $this->db->query($query);
183  $ret = [];
184  while ($rec = $this->db->fetchAssoc($res)) {
185  $ret[] = (new ilOrgUnitPosition((int) $rec['id']))
186  ->withTitle((string) $rec['title'])
187  ->withDescription((string) $rec['description'])
188  ->withCorePosition((bool) $rec['core_position'])
189  ->withCoreIdentifier((int) $rec['core_identifier'])
190  ->withAuthorities($this->authorityRepo->get((int) $rec['id'], ilOrgUnitAuthority::POSITION_ID));
191  }
192 
193  return $ret;
194  }
195 
196  public function create(): ilOrgUnitPosition
197  {
198  return new ilOrgUnitPosition();
199  }
200 
204  public function store(ilOrgUnitPosition $position): ilOrgUnitPosition
205  {
206  if ($position->getId() === 0) {
207  $position = $this->insert($position);
208  } else {
209  $this->update($position);
210  }
211 
212  $authorities = $position->getAuthorities();
213  $ids = [];
214  $new_authorities = [];
215  foreach ($authorities as $authority) {
216  $auth = $this->authorityRepo->store($authority->withPositionId($position->getId()));
217  $ids[] = $auth->getId();
218  $new_authorities[] = $auth;
219  }
220  $position = $position->withAuthorities($new_authorities);
221 
222  if (count($ids) > 0) {
223  $this->authorityRepo->deleteLeftoverAuthorities($ids, $position->getId());
224  }
225  if (count($ids) === 0) {
226  $authorities = $this->authorityRepo->get($position->getId(), 'position_id');
227  foreach ($authorities as $authority) {
228  $this->authorityRepo->delete($authority->getId());
229  }
230  }
231 
232  return $position;
233  }
234 
235  private function insert(ilOrgUnitPosition $position): ilOrgUnitPosition
236  {
237  $id = $this->db->nextId(self::TABLE_NAME);
238 
239  $values = [
240  'id' => [ 'integer', $id],
241  'title' => [ 'text', $position->getTitle() ],
242  'description' => [ 'text', $position->getDescription() ],
243  'core_position' => [ 'integer', ($position->isCorePosition()) ? 1 : 0 ],
244  'core_identifier' => [ 'integer', $position->getCoreIdentifier() ]
245  ];
246 
247  $this->db->insert(self::TABLE_NAME, $values);
248 
249  $ret = (new ilOrgUnitPosition($id))
250  ->withTitle($position->getTitle())
251  ->withDescription($position->getDescription())
252  ->withCorePosition($position->isCorePosition())
253  ->withCoreIdentifier($position->getCoreIdentifier())
254  ->withAuthorities($position->getAuthorities());
255 
256  return $ret;
257  }
258 
259  private function update(ilOrgUnitPosition $position): void
260  {
261  $where = [ 'id' => [ 'integer', $position->getId() ] ];
262 
263  $values = [
264  'title' => [ 'text', $position->getTitle() ],
265  'description' => [ 'text', $position->getDescription() ],
266  'core_position' => [ 'integer', (($position->isCorePosition()) ? 1 : 0) ],
267  'core_identifier' => [ 'integer', $position->getCoreIdentifier() ]
268  ];
269 
270  $this->db->update(self::TABLE_NAME, $values, $where);
271  }
272 
276  public function delete(int $position_id): void
277  {
278  $query = 'DELETE FROM ' . self::TABLE_NAME . PHP_EOL
279  . ' WHERE id = ' . $this->db->quote($position_id, 'integer');
280 
281  $this->db->manipulate($query);
282 
283  $authorities = $this->authorityRepo->get($position_id, 'position_id');
284  foreach ($authorities as $authority) {
285  $this->authorityRepo->delete($authority->getId());
286  }
287 
288  $assignments = $this->assignmentRepo->getByPosition($position_id);
289  foreach ($assignments as $assignment) {
290  $this->assignmentRepo->delete($assignment);
291  }
292  }
293 
297  public function getAuthority(?int $id): ilOrgUnitAuthority
298  {
299  if ($id === null) {
300  return $this->authorityRepo->create();
301  } else {
302  $authority = $this->authorityRepo->get($id, 'id');
303  return array_shift($authority);
304  }
305  }
306 }
getAllPositions()
Returns all position objects.
$res
Definition: ltiservices.php:69
__construct(ilDBInterface $db, ilOrgUnitAuthorityDBRepository $authorityRepo, ilOrgUnitUserAssignmentDBRepository $assignmentRepo)
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())
withAuthorities(array $authorities)
string $key
Consumer key/client ID value.
Definition: System.php:193
ilOrgUnitUserAssignmentDBRepository $assignmentRepo
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
store(ilOrgUnitPosition $position)
Saves position and its authorities.
Class ilOrguAuthority.