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