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