ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilWebLinkDatabaseRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
26 public const string ITEMS_TABLE = 'webr_items';
27 public const string LISTS_TABLE = 'webr_lists';
28 public const string PARAMS_TABLE = 'webr_params';
29
30 protected int $webr_id;
31 protected ilDBInterface $db;
32 protected ilObjUser $user;
33
34 public function __construct(int $webr_id)
35 {
36 global $DIC;
37
38 $this->db = $DIC->database();
39 $this->user = $DIC->user();
40 $this->webr_id = $webr_id;
41 }
42
44 {
45 $next_link_id = $this->db->nextId(self::ITEMS_TABLE);
46
47 $this->validateInternalItemTarget($item);
48
49 //create the parameters
50 $new_parameters = [];
51 foreach ($item->getParameters() as $parameter) {
52 $next_param_id = $this->db->nextId(self::PARAMS_TABLE);
53
54 try {
55 $this->validateParameter($parameter);
56 } catch (Exception $e) {
57 continue;
58 }
59
60
61 $new_parameter = new ilWebLinkParameter(
62 $this->user,
63 $this->getWebrId(),
64 $next_link_id,
65 $next_param_id,
66 $parameter->getValue(),
67 $parameter->getName()
68 );
69
70 $this->db->insert(
71 self::PARAMS_TABLE,
72 [
73 'webr_id' => ['integer', $new_parameter->getWebrId()],
74 'link_id' => ['integer', $new_parameter->getLinkId()],
75 'param_id' => ['integer', $new_parameter->getParamId()],
76 'name' => ['text', $new_parameter->getName()],
77 'value' => ['integer', $new_parameter->getValue()]
78 ]
79 );
80
81 $new_parameters[] = $new_parameter;
82 }
83
84 //create the item with the new parameters
85 if ($item->isInternal()) {
86 $class = ilWebLinkItemInternal::class;
87 } else {
88 $class = ilWebLinkItemExternal::class;
89 }
90
91 $new_item = new $class(
92 $this->getWebrId(),
93 $next_link_id,
94 $item->getTitle(),
95 $item->getDescription(),
96 $item->getTarget(),
97 $item->isActive(),
98 $this->getNewDateTimeImmutable(),
99 $this->getNewDateTimeImmutable(),
100 $new_parameters
101 );
102
103 $this->db->insert(
104 self::ITEMS_TABLE,
105 [
106 'internal' => ['integer', (int) $new_item->isInternal()],
107 'webr_id' => ['integer', $new_item->getWebrId()],
108 'link_id' => ['integer', $new_item->getLinkId()],
109 'title' => ['text', $new_item->getTitle()],
110 'description' => ['text', $new_item->getDescription() ?? ''],
111 'target' => ['text', $new_item->getTarget()],
112 'active' => ['integer', (int) $new_item->isActive()],
113 'create_date' => ['integer', $new_item->getCreateDate()
114 ->getTimestamp()],
115 'last_update' => ['integer', $new_item->getLastUpdate()
116 ->getTimestamp()]
117 ]
118 );
119
120 return $new_item;
121 }
122
124 {
125 $new_list = new ilWebLinkList(
126 $this->getWebrId(),
127 $list->getTitle(),
128 $list->getDescription(),
129 $this->getNewDateTimeImmutable(),
130 $this->getNewDateTimeImmutable(),
131 );
132
133 $this->db->insert(
134 self::LISTS_TABLE,
135 [
136 'webr_id' => ['integer', $new_list->getWebrId()],
137 'title' => ['text', $new_list->getTitle()],
138 'description' => ['text', $new_list->getDescription() ?? ''],
139 'create_date' => ['integer', $new_list->getCreateDate()
140 ->getTimestamp()],
141 'last_update' => ['integer', $new_list->getLastUpdate()
142 ->getTimestamp()],
143 ]
144 );
145
146 return $new_list;
147 }
148
150 {
151 $new_items = [];
152
153 foreach ($container->getItems() as $item) {
154 $new_items[] = $this->createItem($item);
155 }
156
157 return new ilWebLinkItemsContainer(
158 $this->getWebrId(),
159 $new_items
160 );
161 }
162
163 public function getAllItemsAsContainer(bool $only_active = false): ilWebLinkItemsContainer
164 {
165 $query = "SELECT * FROM " . $this->db->quoteIdentifier(self::ITEMS_TABLE) . " " .
166 "WHERE webr_id = " . $this->db->quote($this->getWebrId(), 'integer');
167
168 if ($only_active) {
169 $query .= " AND active = " . $this->db->quote(1, 'integer');
170 }
171
172 $res = $this->db->query($query);
173 $items = [];
174
175 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
176 $parameters = $this->getParametersByLinkId((int) $row->link_id);
177
178 if ($row->internal) {
179 $class = ilWebLinkItemInternal::class;
180 } else {
181 $class = ilWebLinkItemExternal::class;
182 }
183
184 $items[] = new $class(
185 (int) $row->webr_id,
186 (int) $row->link_id,
187 (string) $row->title,
188 ((string) $row->description) !== '' ? (string) $row->description : null,
189 (string) $row->target,
190 (bool) $row->active,
191 $this->getNewDateTimeImmutable()->setTimestamp((int) $row->create_date),
192 $this->getNewDateTimeImmutable()->setTimestamp((int) $row->last_update),
193 $parameters
194 );
195 }
196
197 return new ilWebLinkItemsContainer(
198 $this->getWebrId(),
199 $items
200 );
201 }
202
203 public function getItemByLinkId(int $link_id): ilWebLinkItem
204 {
205 $query = "SELECT * FROM " . $this->db->quoteIdentifier(self::ITEMS_TABLE) . " " .
206 "WHERE webr_id = " . $this->db->quote($this->getWebrId(), 'integer') . " " .
207 "AND link_id = " . $this->db->quote($link_id, 'integer');
208
209 $res = $this->db->query($query);
210
211 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
212 $parameters = $this->getParametersByLinkId((int) $row->link_id);
213
214 if ($row->internal) {
215 $class = ilWebLinkItemInternal::class;
216 } else {
217 $class = ilWebLinkItemExternal::class;
218 }
219
220 return new $class(
221 (int) $row->webr_id,
222 (int) $row->link_id,
223 (string) $row->title,
224 ((string) $row->description) !== '' ? (string) $row->description : null,
225 (string) $row->target,
226 (bool) $row->active,
227 $this->getNewDateTimeImmutable()->setTimestamp((int) $row->create_date),
228 $this->getNewDateTimeImmutable()->setTimestamp((int) $row->last_update),
229 $parameters
230 );
231 }
232
234 'No item with the given link_id was found in this web link object.'
235 );
236 }
237
238 public function doesOnlyOneItemExist(bool $only_active = false): bool
239 {
240 $query = "SELECT COUNT(*) AS num FROM " . $this->db->quoteIdentifier(self::ITEMS_TABLE) . " " .
241 "WHERE webr_id = " . $this->db->quote($this->getWebrId(), 'integer');
242
243 if ($only_active) {
244 $query .= " AND active = " . $this->db->quote(1, 'integer');
245 }
246
247 $row = $this->db->query($query)
249
250 return $row->num == 1;
251 }
252
254 ilWebLinkItem $item,
255 int $param_id
257 $res = $this->db->query(
258 "SELECT * FROM " . $this->db->quoteIdentifier(self::PARAMS_TABLE) . " " .
259 "WHERE webr_id = " . $this->db->quote($this->getWebrId(), 'integer') . " " .
260 "AND link_id = " . $this->db->quote($item->getLinkId(), 'integer') . " " .
261 "AND param_id = " . $this->db->quote($param_id, 'integer')
262 );
263
264 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
265 return new ilWebLinkParameter(
266 $this->user,
267 (int) $row->webr_id,
268 (int) $row->link_id,
269 (int) $row->param_id,
270 (int) $row->value,
271 (string) $row->name
272 );
273 }
274
276 'In the given item of this web link object, no parameter with the given param_id was found.'
277 );
278 }
279
280 public function getList(): ilWebLinkList
281 {
282 $res = $this->db->query(
283 "SELECT * FROM " . $this->db->quoteIdentifier(self::LISTS_TABLE) . " " .
284 "WHERE webr_id = " . $this->db->quote($this->getWebrId(), 'integer')
285 );
286
287 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
288 return new ilWebLinkList(
289 (int) $row->webr_id,
290 (string) $row->title,
291 ((string) $row->description) !== '' ? (string) $row->description : null,
292 $this->getNewDateTimeImmutable()->setTimestamp((int) $row->create_date),
293 $this->getNewDateTimeImmutable()->setTimestamp((int) $row->last_update),
294 );
295 }
296
298 'No list exists in this web link object.'
299 );
300 }
301
302 public function doesListExist(): bool
303 {
304 $res = $this->db->query(
305 "SELECT COUNT(*) AS num FROM " . $this->db->quoteIdentifier(self::LISTS_TABLE) . " " .
306 "WHERE webr_id = " . $this->db->quote($this->getWebrId(), 'integer')
307 );
308
309 $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
310
311 return ((int) $row->num) > 0;
312 }
313
314 public function updateItem(
315 ilWebLinkItem $item,
316 ilWebLinkDraftItem $drafted_item
317 ): void {
318 if ($item->getWebrId() !== $this->getWebrId()) {
320 'Cannot update an item from a different web link object.'
321 );
322 }
323
324 $this->validateInternalItemTarget($drafted_item);
325
326 $this->db->update(
327 self::ITEMS_TABLE,
328 [
329 'title' => ['text', $drafted_item->getTitle()],
330 'description' => ['text', $drafted_item->getDescription() ?? ''],
331 'target' => ['text', $drafted_item->getTarget()],
332 'active' => ['integer', (int) $drafted_item->isActive()],
333 'internal' => ['integer', (int) $drafted_item->isInternal()],
334 'last_update' => ['integer', $this->getCurrentTime()]
335 ],
336 [
337 'webr_id' => ['integer', $item->getWebrId()],
338 'link_id' => ['integer', $item->getLinkId()]
339 ]
340 );
341
342 /*
343 * drafted parameters of the drafted item are either created or
344 * update an existing parameter
345 */
346 $param_ids = [];
347 foreach ($drafted_item->getParameters() as $parameter) {
348 if ($parameter instanceof ilWebLinkParameter) {
349 $param_ids[] = $parameter->getParamId();
350 continue;
351 }
352
353 try {
354 $this->validateParameter($parameter);
355 } catch (Exception $e) {
356 continue;
357 }
358
359 if ($old_parameter = $parameter->getOldParameter()) {
360 if (
361 $old_parameter->getLinkId() !== $item->getLinkId() ||
362 $old_parameter->getWebrId() !== $item->getWebrId()
363 ) {
365 'Cannot update a parameter from a different item.'
366 );
367 }
368
369 $this->db->update(
370 self::PARAMS_TABLE,
371 [
372 'name' => ['text', $drafted_item->getTitle()],
373 'value' => ['integer', $drafted_item->getTitle()]
374 ],
375 [
376 'webr_id' => ['integer', $item->getWebrId()],
377 'link_id' => ['integer', $item->getLinkId()],
378 'param_id' => ['integer', $parameter->getOldParameter()
379 ->getParamId()],
380 ]
381 );
382 continue;
383 }
384
385 $next_param_id = $this->db->nextId(self::PARAMS_TABLE);
386 $this->db->insert(
387 self::PARAMS_TABLE,
388 [
389 'webr_id' => ['integer', $this->getWebrId()],
390 'link_id' => ['integer', $item->getLinkId()],
391 'param_id' => ['integer', $next_param_id],
392 'name' => ['text', $parameter->getName()],
393 'value' => ['integer', $parameter->getValue()]
394 ]
395 );
396 }
397
398 /*
399 * parameters attached to the original item but not the drafted item
400 * are deleted
401 */
402 foreach ($item->getParameters() as $parameter) {
403 if (!in_array($parameter->getParamId(), $param_ids)) {
404 $this->deleteParameterByLinkIdAndParamId(
405 $item->getLinkId(),
406 $parameter->getParamId()
407 );
408 }
409 }
410 }
411
412 public function updateList(
413 ilWebLinkList $list,
414 ilWebLinkDraftList $drafted_list
415 ): void {
416 if ($list->getWebrId() !== $this->getWebrId()) {
418 'Cannot update a list from a different web link object.'
419 );
420 }
421
422 $this->db->update(
423 self::LISTS_TABLE,
424 [
425 'title' => ['text', $drafted_list->getTitle()],
426 'description' => ['text', $drafted_list->getDescription() ?? ''],
427 'last_update' => ['integer', $this->getCurrentTime()]
428 ],
429 [
430 'webr_id' => ['integer', $list->getWebrId()]
431 ]
432 );
433 }
434
435 public function deleteAllItems(): void
436 {
437 $this->db->manipulate(
438 "DELETE FROM " . $this->db->quoteIdentifier(self::ITEMS_TABLE) . " " .
439 "WHERE webr_id = " . $this->db->quote($this->getWebrId(), 'integer')
440 );
441
442 $this->db->manipulate(
443 "DELETE FROM " . $this->db->quoteIdentifier(self::PARAMS_TABLE) . " " .
444 "WHERE webr_id = " . $this->db->quote($this->getWebrId(), 'integer')
445 );
446 }
447
448 public function deleteItemByLinkID(int $link_id): void
449 {
450 $this->db->manipulate(
451 "DELETE FROM " . $this->db->quoteIdentifier(self::ITEMS_TABLE) . " " .
452 "WHERE webr_id = " . $this->db->quote($this->getWebrId(), 'integer') . " " .
453 "AND link_id = " . $this->db->quote($link_id, 'integer')
454 );
455
456 $this->db->manipulate(
457 "DELETE FROM " . $this->db->quoteIdentifier(self::PARAMS_TABLE) . " " .
458 "WHERE webr_id = " . $this->db->quote($this->getWebrId(), 'integer') . " " .
459 "AND link_id = " . $this->db->quote($link_id, 'integer')
460 );
461 }
462
464 int $link_id,
465 int $param_id
466 ): void {
467 $this->db->manipulate(
468 "DELETE FROM " . $this->db->quoteIdentifier(self::PARAMS_TABLE) . " " .
469 "WHERE webr_id = " . $this->db->quote($this->getWebrId(), 'integer') . " " .
470 "AND link_id = " . $this->db->quote($link_id, 'integer') . " " .
471 "AND param_id = " . $this->db->quote($param_id, 'integer')
472 );
473 }
474
475 public function deleteList(): void
476 {
477 $res = $this->db->manipulate(
478 "DELETE FROM " . $this->db->quoteIdentifier(self::LISTS_TABLE) . " " .
479 "WHERE webr_id = " . $this->db->quote($this->getWebrId(), 'integer')
480 );
481 }
482
486 protected function getParametersByLinkId(int $link_id): array
487 {
488 $res = $this->db->query(
489 "SELECT * FROM " . $this->db->quoteIdentifier(self::PARAMS_TABLE) . " " .
490 "WHERE webr_id = " . $this->db->quote($this->getWebrId(), 'integer') . " " .
491 "AND link_id = " . $this->db->quote($link_id, 'integer')
492 );
493 $parameters = [];
494
495 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
496 $parameter = new ilWebLinkParameter(
497 $this->user,
498 (int) $row->webr_id,
499 (int) $row->link_id,
500 (int) $row->param_id,
501 (int) $row->value,
502 (string) $row->name
503 );
504 try {
505 $this->validateParameter($parameter);
506 } catch (Exception $e) {
507 continue;
508 }
509 $parameters[] = $parameter;
510 }
511
512 return $parameters;
513 }
514
518 protected function validateParameter(ilWebLinkBaseParameter $parameter): void
519 {
520 if (!in_array(
521 $parameter->getValue(),
523 )) {
525 'The value of the parameter you are trying to create is invalid.'
526 );
527 }
528 }
529
533 protected function validateInternalItemTarget(ilWebLinkDraftItem $item): void
534 {
535 if (
536 $item->isInternal() &&
537 !$this->isInternalLink($item->getTarget())
538 ) {
540 'The target of this internal link item is not internal.'
541 );
542 }
543 }
544
545 protected function isInternalLink(string $value): bool
546 {
547 return ilLinkInputGUI::isInternalLink($value);
548 }
549
550 public function getWebrId(): int
551 {
552 return $this->webr_id;
553 }
554
555 protected function getCurrentTime(): int
556 {
557 return time();
558 }
559
560 protected function getNewDateTimeImmutable(): DateTimeImmutable
561 {
562 return new DateTimeImmutable();
563 }
564}
static isInternalLink(string $a_value)
User class.
Base class for parameters attached to Web Link items.
const array VALUES
TODO Once the GUI is updated, undefined can be dropped.
deleteParameterByLinkIdAndParamId(int $link_id, int $param_id)
validateInternalItemTarget(ilWebLinkDraftItem $item)
validateParameter(ilWebLinkBaseParameter $parameter)
createAllItemsInDraftContainer(ilWebLinkDraftItemsContainer $container)
updateItem(ilWebLinkItem $item, ilWebLinkDraftItem $drafted_item)
Updates an item.
getParameterinItemByParamId(ilWebLinkItem $item, int $param_id)
updateList(ilWebLinkList $list, ilWebLinkDraftList $drafted_list)
createItem(ilWebLinkDraftItem $item)
Creates a new item, complete with parameters.
Draft class for creating and updating a Web Link item.
Container class for drafted Web Link items.
Draft class for creating or updating a Web Link list.
Immutable class for Web Link items.
Immutable container class for Web Link items.
Immutable class for parameters attached to Web Link items.
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26
$container
@noRector
Definition: wac.php:37