ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilOrgUnitOperationDBRepository.php
Go to the documentation of this file.
1 <?php
18 declare(strict_types=1);
19 
21 {
22  public const TABLE_NAME = 'il_orgu_operations';
23  protected ilDBInterface $db;
25 
26 
28  {
29  $this->db = $db;
30  $this->contextRepo = $contextRepo;
31  }
32 
33  public function get(string $operation_string, string $description, array $contexts, int $list_order = 0): array
34  {
35  $operations = [];
36  foreach ($contexts as $context) {
37  $operation = $this->find($operation_string, $context);
38  if ($operation) {
39  $operations[] = $operation;
40  continue;
41  }
42 
43  $operation_context = $this->contextRepo->get($context, null);
44 
45  $new_operation = (new ilOrgUnitOperation())
46  ->withOperationString($operation_string)
47  ->withDescription($description)
48  ->withListOrder($list_order)
49  ->withContextId($operation_context->getId());
50  $new_operation = $this->store($new_operation);
51 
52  $operations[] = $new_operation;
53  }
54 
55  return $operations;
56  }
57 
58  public function store(ilOrgUnitOperation $operation): ilOrgUnitOperation
59  {
60  if ($operation->getOperationId() === 0) {
61  $operation = $this->insert($operation);
62  } else {
63  $this->update($operation);
64  }
65 
66  return $operation;
67  }
68 
69  private function insert(ilOrgUnitOperation $operation): ilOrgUnitOperation
70  {
71  $id = $this->db->nextId(self::TABLE_NAME);
72 
73  $values = [
74  'operation_id' => [ 'integer', $id ],
75  'operation_string' => [ 'string', $operation->getOperationString()],
76  'description' => [ 'string', $operation->getDescription()],
77  'list_order' => [ 'integer', $operation->getListOrder() ],
78  'context_id' => [ 'integer', $operation->getContextId() ]
79  ];
80 
81  $this->db->insert(self::TABLE_NAME, $values);
82 
83  return (new ilOrgUnitOperation($id))
84  ->withOperationString($operation->getOperationString())
85  ->withDescription($operation->getDescription())
86  ->withListOrder($operation->getListOrder())
87  ->withContextId($operation->getContextId());
88  }
89 
90  private function update(ilOrgUnitOperation $operation): void
91  {
92  $where = [ 'operation_id' => [ 'integer', $operation->getOperationId() ] ];
93 
94  $values = [
95  'operation_string' => [ 'string', $operation->getOperationString()],
96  'description' => [ 'string', $operation->getDescription()],
97  'list_order' => [ 'integer', $operation->getListOrder() ],
98  'context_id' => [ 'integer', $operation->getContextId() ]
99  ];
100 
101  $this->db->update(self::TABLE_NAME, $values, $where);
102  }
103 
104  public function delete(ilOrgUnitOperation $operation): bool
105  {
106  if ($operation->getOperationId() === 0) {
107  return false;
108  }
109 
110  $query = 'DELETE FROM ' . self::TABLE_NAME . PHP_EOL
111  . ' WHERE operation_id = ' . $this->db->quote($operation->getOperationId(), 'integer');
112  $rows = $this->db->manipulate($query);
113  if ($rows > 0) {
114  return true;
115  }
116 
117  return false;
118  }
119 
120  public function find(string $operation_string, string $context): ?ilOrgUnitOperation
121  {
122  $context = $this->contextRepo->find($context);
123  if (!$context) {
124  return null;
125  }
126 
127  $query = 'SELECT operation_id, operation_string, description, list_order, context_id FROM' . PHP_EOL
128  . self::TABLE_NAME
129  . ' WHERE ' . self::TABLE_NAME . '.operation_string = ' . $this->db->quote($operation_string, 'string') . PHP_EOL
130  . ' AND ' . self::TABLE_NAME . '.context_id = ' . $this->db->quote($context->getId(), 'integer');
131 
132  $res = $this->db->query($query);
133  if ($res->numRows() === 0) {
134  return null;
135  }
136 
137  $rec = $this->db->fetchAssoc($res);
138  return (new ilOrgUnitOperation((int) $rec['operation_id']))
139  ->withOperationString((string) $rec['operation_string'])
140  ->withDescription((string) $rec["description"])
141  ->withListOrder((int) $rec["list_order"])
142  ->withContextId((int) $rec['context_id']);
143  }
144 
145  public function getById(int $operation_id): ?ilOrgUnitOperation
146  {
147  $query = 'SELECT operation_id, operation_string, description, list_order, context_id FROM' . PHP_EOL
148  . self::TABLE_NAME
149  . ' WHERE ' . self::TABLE_NAME . '.operation_id = ' . $this->db->quote($operation_id, 'integer');
150 
151  $res = $this->db->query($query);
152  if ($res->numRows() === 0) {
153  return null;
154  }
155 
156  $rec = $this->db->fetchAssoc($res);
157  return (new ilOrgUnitOperation((int) $rec['operation_id']))
158  ->withOperationString((string) $rec['operation_string'])
159  ->withDescription((string) $rec["description"])
160  ->withListOrder((int) $rec["list_order"])
161  ->withContextId((int) $rec['context_id']);
162  }
163 
164  public function getByName(string $operation_string): array
165  {
166  $query = 'SELECT operation_id, operation_string, description, list_order, context_id FROM' . PHP_EOL
167  . self::TABLE_NAME
168  . ' WHERE ' . self::TABLE_NAME . '.operation_string = ' . $this->db->quote($operation_string, 'string');
169 
170  $res = $this->db->query($query);
171  if ($res->numRows() === 0) {
172  return [];
173  }
174 
175  $ret = [];
176  while ($rec = $this->db->fetchAssoc($res)) {
177  $operation = (new ilOrgUnitOperation((int)$rec['operation_id']))
178  ->withOperationString((string)$rec['operation_string'])
179  ->withDescription((string)$rec["description"])
180  ->withListOrder((int)$rec["list_order"])
181  ->withContextId((int)$rec['context_id']);
182  $ret[] = $operation;
183  }
184 
185  return $ret;
186  }
187 
188  public function getOperationsByContextId(int $context_id): array
189  {
190  $operation_context = $this->contextRepo->getById($context_id);
191  if (!$operation_context) {
192  throw new ilException('Context with id ' . $context_id . ' does not exist!');
193  }
194 
195  $query = 'SELECT operation_id, operation_string, description, list_order, context_id FROM' . PHP_EOL
196  . self::TABLE_NAME
197  . ' WHERE ' . self::TABLE_NAME . '.context_id = ' . $this->db->quote($operation_context->getId(), 'integer');
198  $res = $this->db->query($query);
199 
200  $ret = [];
201  while ($rec = $this->db->fetchAssoc($res)) {
202  $operation = (new ilOrgUnitOperation((int)$rec['operation_id']))
203  ->withOperationString((string)$rec['operation_string'])
204  ->withDescription((string)$rec["description"])
205  ->withListOrder((int)$rec["list_order"])
206  ->withContextId((int)$rec['context_id']);
207  $ret[] = $operation;
208  }
209 
210  return $ret;
211  }
212 
213  public function getOperationsByContextName(string $context): array
214  {
215  $operation_context = $this->contextRepo->find($context);
216  if (!$operation_context) {
217  throw new ilException('Context ' . $context . ' does not exist!');
218  }
219 
220  $query = 'SELECT operation_id, operation_string, description, list_order, context_id FROM' . PHP_EOL
221  . self::TABLE_NAME
222  . ' WHERE ' . self::TABLE_NAME . '.context_id = ' . $this->db->quote($operation_context->getId(), 'integer');
223  $res = $this->db->query($query);
224 
225  $ret = [];
226  while ($rec = $this->db->fetchAssoc($res)) {
227  $operation = (new ilOrgUnitOperation((int)$rec['operation_id']))
228  ->withOperationString((string)$rec['operation_string'])
229  ->withDescription((string)$rec["description"])
230  ->withListOrder((int)$rec["list_order"])
231  ->withContextId((int)$rec['context_id']);
232  $ret[] = $operation;
233  }
234 
235  return $ret;
236  }
237 }
$res
Definition: ltiservices.php:69
ilOrgUnitOperationContextDBRepository $contextRepo
$context
Definition: webdav.php:31
getOperationsByContextName(string $context)
Get operations by context name.
getById(int $operation_id)
Get operation by id Returns null if no operation is found.
__construct(ilDBInterface $db, ilOrgUnitOperationContextDBRepository $contextRepo)
find(string $operation_string, string $context)
Find an existing operation for a specified context Returns null if no operation is found...
getOperationsByContextId(int $context_id)
Get operations by context id.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
store(ilOrgUnitOperation $operation)
Store operation to db.
getByName(string $operation_string)
Get operation(s) by name.