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