ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilOrgUnitAuthorityDBRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
23 public const TABLE_NAME = 'il_orgu_authority';
24 protected ilDBInterface $db;
25
26 public function __construct(ilDBInterface $db)
27 {
28 $this->db = $db;
29 }
30
31 public function get(int $id, string $field): array
32 {
34 throw new Exception("Invalid field: " . $field);
35 }
36 $query = 'SELECT id, ' . self::TABLE_NAME . '.over, scope, position_id FROM' . PHP_EOL
37 . self::TABLE_NAME
38 . ' WHERE ' . self::TABLE_NAME . '.' . $field . ' = ' . $this->db->quote($id, 'integer');
39 $res = $this->db->query($query);
40 $ret = [];
41 while ($rec = $this->db->fetchAssoc($res)) {
42 $ret[] = (new ilOrgUnitAuthority((int) $rec['id']))
43 ->withOver((int) $rec['over'])
44 ->withScope((int) $rec['scope'])
45 ->withPositionId((int) $rec['position_id']);
46 }
47
48 return $ret;
49 }
50
51 public function create(): ilOrgUnitAuthority
52 {
53 return new ilOrgUnitAuthority();
54 }
55
56 public function store(ilOrgUnitAuthority $authority): ilOrgUnitAuthority
57 {
58 if ($authority->getId() === 0) {
59 $authority = $this->insert($authority);
60 } else {
61 $this->update($authority);
62 }
63
64 return $authority;
65 }
66
67 private function insert(ilOrgUnitAuthority $authority): ilOrgUnitAuthority
68 {
69 $id = $this->db->nextId(self::TABLE_NAME);
70
71 $values = [
72 'id' => [ 'integer', $id],
73 'over' => [ 'integer', $authority->getOver() ],
74 'scope' => [ 'integer', $authority->getScope() ],
75 'position_id' => [ 'integer', $authority->getPositionId() ]
76 ];
77
78 $this->db->insert(self::TABLE_NAME, $values);
79
80 $ret = (new ilOrgUnitAuthority($id))
81 ->withOver($authority->getOver())
82 ->withScope($authority->getScope())
83 ->withPositionId($authority->getPositionId());
84
85 return $ret;
86 }
87
88 private function update(ilOrgUnitAuthority $authority): void
89 {
90 $where = [ 'id' => [ 'integer', $authority->getId() ] ];
91
92 $values = [
93 'over' => [ 'integer', $authority->getOver() ],
94 'scope' => [ 'integer', $authority->getScope() ],
95 'position_id' => [ 'integer', $authority->getPositionId() ]
96 ];
97
98 $this->db->update(self::TABLE_NAME, $values, $where);
99 }
100
101 public function delete(int $id): void
102 {
103 $query = 'DELETE FROM ' . self::TABLE_NAME . PHP_EOL
104 . ' WHERE id = ' . $this->db->quote($id, 'integer');
105
106 $this->db->manipulate($query);
107 }
108
112 public function deleteByPositionId(int $position_id): void
113 {
114 $query = 'DELETE FROM ' . self::TABLE_NAME . PHP_EOL
115 . ' WHERE position_id = ' . $this->db->quote($position_id, 'integer');
116
117 $this->db->manipulate($query);
118 }
119
123 public function deleteLeftoverAuthorities(array $ids, int $position_id): void
124 {
125 $query = 'DELETE FROM ' . self::TABLE_NAME . PHP_EOL
126 . ' WHERE position_id = ' . $this->db->quote($position_id, 'integer') . PHP_EOL
127 . ' AND ' . $this->db->in('id', $ids, true, 'integer');
128
129 $this->db->manipulate($query);
130 }
131}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
deleteByPositionId(int $position_id)
Deletes all authorities for a position.
deleteLeftoverAuthorities(array $ids, int $position_id)
Deletes orphaned authorities on position save.
Class ilOrguAuthority.
Interface ilDBInterface.
$res
Definition: ltiservices.php:69