ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilOrgUnitOperationContextDBRepository.php
Go to the documentation of this file.
1 <?php
18 declare(strict_types=1);
19 
21 {
22  public const TABLE_NAME = 'il_orgu_op_contexts';
23  protected ilDBInterface $db;
24 
25 
26  public function __construct(ilDBInterface $db)
27  {
28  $this->db = $db;
29  }
30 
31  public function get(string $context, ?string $parent_context): ilOrgUnitOperationContext
32  {
33  $found_context = $this->find($context);
34  if ($found_context) {
35  return $found_context;
36  }
37 
38  $parent_id = 0;
39  if ($parent_context !== null) {
40  $parent = $this->find($parent_context);
41  if (!$parent) {
42  throw new ilException("Parent context not found");
43  }
44  $parent_id = $parent->getId();
45  }
46 
47  $context = (new ilOrgUnitOperationContext())
48  ->withContext($context)
49  ->withParentContextId($parent_id);
50  $this->store($context);
51 
52  $context = $context
53  ->withPathNames([$context->getContext()])
54  ->withPathIds([$context->getId()]);
55  $context = $this->appendPath($context);
56 
57  return $context;
58  }
59 
60 
61  public function store(ilOrgUnitOperationContext $operation_context): ilOrgUnitOperationContext
62  {
63  if ($operation_context->getId() === 0) {
64  $operation_context = $this->insert($operation_context);
65  } else {
66  $this->update($operation_context);
67  $operation_context = $operation_context
68  ->withPathNames([$operation_context->getContext()])
69  ->withPathIds([$operation_context->getId()]);
70  $operation_context = $this->appendPath($operation_context);
71  }
72 
73  return $operation_context;
74  }
75 
76  private function insert(ilOrgUnitOperationContext $operation_context): ilOrgUnitOperationContext
77  {
78  $id = $this->db->nextId(self::TABLE_NAME);
79 
80  $values = [
81  'id' => [ 'integer', $id ],
82  'context' => [ 'string', $operation_context->getContext() ],
83  'parent_context_id' => [ 'integer', $operation_context->getParentContextId() ]
84  ];
85 
86  $this->db->insert(self::TABLE_NAME, $values);
87 
88  $ret = (new ilOrgUnitOperationContext($id))
89  ->withContext($operation_context->getContext())
90  ->withParentContextId($operation_context->getParentContextId())
91  ->withPathNames([$operation_context->getContext()])
92  ->withPathIds([$id]);
93  $ret = $this->appendPath($ret);
94 
95  return $ret;
96  }
97 
98  private function update(ilOrgUnitOperationContext $operation_context): void
99  {
100  $where = [ 'id' => [ 'integer', $operation_context->getId() ] ];
101 
102  $values = [
103  'context' => [ 'integer', $operation_context->getContext() ],
104  'parent_context_id' => [ 'integer', $operation_context->getParentContextId() ]
105  ];
106 
107  $this->db->update(self::TABLE_NAME, $values, $where);
108  }
109 
110  public function delete(string $context): bool
111  {
112  $operation_context = $this->find($context);
113  if (!$operation_context) {
114  return false;
115  }
116 
117  $query = 'DELETE FROM ' . self::TABLE_NAME . PHP_EOL
118  . ' WHERE id = ' . $this->db->quote($operation_context->getId(), 'integer');
119  $rows = $this->db->manipulate($query);
120  if ($rows > 0) {
121  return true;
122  }
123 
124  return false;
125  }
126 
127  public function find(string $context): ?ilOrgUnitOperationContext
128  {
129  $query = 'SELECT id, context, parent_context_id FROM' . PHP_EOL
130  . self::TABLE_NAME
131  . ' WHERE ' . self::TABLE_NAME . '.context = ' . $this->db->quote($context, 'string');
132  $res = $this->db->query($query);
133  if ($res->numRows() === 0) {
134  return null;
135  }
136 
137  $rec = $this->db->fetchAssoc($res);
138  $operation_context = (new ilOrgUnitOperationContext((int) $rec['id']))
139  ->withContext((string) $rec['context'])
140  ->withParentContextId((int) $rec['parent_context_id'])
141  ->withPathNames([(string) $rec['context']])
142  ->withPathIds([(int) $rec['id']]);
143  $operation_context = $this->appendPath($operation_context);
144 
145  return $operation_context;
146  }
147 
148  public function getById(int $id): ?ilOrgUnitOperationContext
149  {
150  $query = 'SELECT id, context, parent_context_id FROM' . PHP_EOL
151  . self::TABLE_NAME
152  . ' WHERE ' . self::TABLE_NAME . '.id = ' . $this->db->quote($id, 'integer');
153  $res = $this->db->query($query);
154  if ($res->numRows() === 0) {
155  return null;
156  }
157 
158  $rec = $this->db->fetchAssoc($res);
159  $operation_context = (new ilOrgUnitOperationContext((int) $rec['id']))
160  ->withContext((string) $rec['context'])
161  ->withParentContextId((int) $rec['parent_context_id'])
162  ->withPathNames([(string) $rec['context']])
163  ->withPathIds([(int) $rec['id']]);
164  $operation_context = $this->appendPath($operation_context);
165 
166  return $operation_context;
167  }
168 
170  {
171  $type_context = ilObject2::_lookupType($ref_id, true);
172  return $this->find($type_context);
173  }
174 
175  public function getByObjId(int $obj_id): ?ilOrgUnitOperationContext
176  {
177  $type_context = ilObject2::_lookupType($obj_id, false);
178  return $this->find($type_context);
179  }
180 
181 
182  private function appendPath(ilOrgUnitOperationContext $operation_context, int $next = null): ilOrgUnitOperationContext
183  {
184  $parent_context_id = ($next >= 0) ? $next : $operation_context->getParentContextId();
185  if ($parent_context_id > 0) {
186  $parent = $this->getById($parent_context_id);
187  if ($parent) {
188  $path_names = $operation_context->getPathNames();
189  $path_names[] = $parent->getContext();
190  $path_ids = $operation_context->getPathIds();
191  $path_ids[] = $parent->getId();
192 
193  $operation_context = $operation_context
194  ->withPathNames($path_names)
195  ->withPathIds($path_ids);
196 
197  $operation_context = $this->appendPath($operation_context, $parent->getId());
198  }
199  }
200  return $operation_context;
201  }
202 }
find(string $context)
Find an existing context Returns null if no context is found.
$res
Definition: ltiservices.php:69
$context
Definition: webdav.php:31
getByRefId(int $ref_id)
Get context by ref_id Returns null if no context is found.
insert(ilOrgUnitOperationContext $operation_context)
appendPath(ilOrgUnitOperationContext $operation_context, int $next=null)
getById(int $id)
Get context by id Returns null if no context is found.
$ref_id
Definition: ltiauth.php:67
store(ilOrgUnitOperationContext $operation_context)
Store context to db.
getByObjId(int $obj_id)
Get context by obj_id Returns null if no context is found.
update(ilOrgUnitOperationContext $operation_context)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static _lookupType(int $id, bool $reference=false)