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