ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilDclTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
25{
26 protected int $id = 0;
27 protected ?int $objId = null;
28 protected ?ilObjDataCollection $obj = null;
29 protected string $title = "";
33 protected array $fields = [];
37 protected array $stdFields = [];
41 protected array $records = [];
42 protected bool $is_visible = false;
43 protected bool $add_perm = false;
44 protected bool $edit_perm = false;
45 protected bool $delete_perm = false;
46 protected bool $edit_by_owner = false;
47 protected bool $delete_by_owner = false;
48 protected bool $save_confirmation = false;
49 protected bool $limited = false;
50 protected string $limit_start = "";
51 protected string $limit_end = "";
52 protected bool $export_enabled = false;
53 protected int $table_order = 0;
54 protected bool $import_enabled = false;
55
56 protected string $default_sort_field = "0";
60 protected string $default_sort_field_order = 'asc';
64 protected string $description = '';
68 protected bool $public_comments = false;
72 protected int $view_own_records_perm = 0;
76 protected ?array $all_fields = null;
79 protected ilObjUser $user;
80 protected ilDBInterface $db;
81 protected bool $show_invalid = false;
82
83 public function __construct(int $a_id = 0)
84 {
85 global $DIC;
86
87 $this->http = $DIC->http();
88 $this->refinery = $DIC->refinery();
89 $this->db = $DIC->database();
90 $this->user = $DIC->user();
91
92 if ($a_id != 0) {
93 $this->id = $a_id;
94 $this->doRead();
95 }
96 }
97
101 public function doRead(): void
102 {
103 $query = "SELECT * FROM il_dcl_table WHERE id = " . $this->db->quote($this->getId(), "integer");
104 $set = $this->db->query($query);
105 $rec = $this->db->fetchAssoc($set);
106
107 $this->setObjId($rec["obj_id"]);
108 if (null !== $rec["title"]) {
109 $this->setTitle($rec["title"]);
110 }
111 $this->setAddPerm((bool) $rec["add_perm"]);
112 $this->setEditPerm((bool) $rec["edit_perm"]);
113 $this->setDeletePerm((bool) $rec["delete_perm"]);
114 $this->setEditByOwner((bool) $rec["edit_by_owner"]);
115 if (null !== $rec["export_enabled"]) {
116 $this->setExportEnabled((bool) $rec["export_enabled"]);
117 }
118 $this->setImportEnabled((bool) $rec["import_enabled"]);
119 $this->setLimited((bool) $rec["limited"]);
120 $this->setLimitStart((string) $rec["limit_start"]);
121 $this->setLimitEnd((string) $rec["limit_end"]);
122 $this->setIsVisible((bool) $rec["is_visible"]);
123 if (null !== $rec['description']) {
124 $this->setDescription($rec['description']);
125 }
126 $this->setDefaultSortField((string) $rec['default_sort_field_id']);
127 $this->setDefaultSortFieldOrder($rec['default_sort_field_order']);
128 $this->setPublicCommentsEnabled((bool) $rec['public_comments']);
129 $this->setViewOwnRecordsPerm((bool) $rec['view_own_records_perm']);
130 $this->setDeleteByOwner((bool) $rec['delete_by_owner']);
131 $this->setSaveConfirmation((bool) $rec['save_confirmation']);
132 if (null !== $rec['table_order']) {
133 $this->setOrder($rec['table_order']);
134 }
135 }
136
142 public function doDelete(bool $delete_only_content = false, bool $omit_notification = false): void
143 {
144 foreach ($this->getRecords() as $record) {
145 $record->doDelete($omit_notification);
146 }
147
148 foreach ($this->getRecordFields() as $field) {
149 $field->doDelete();
150 }
151
152 if (!$delete_only_content) {
153 $query = "DELETE FROM il_dcl_table WHERE id = " . $this->db->quote($this->getId(), "integer");
154 $this->db->manipulate($query);
155 }
156 }
157
158 public function doCreate(bool $create_tablefield_setting = true, bool $create_standardview = true): void
159 {
160 $id = $this->db->nextId("il_dcl_table");
161 $this->setId($id);
162 $query = "INSERT INTO il_dcl_table (" . "id" . ", obj_id" . ", title" . ", add_perm" . ", edit_perm" . ", delete_perm" . ", edit_by_owner"
163 . ", limited" . ", limit_start" . ", limit_end" . ", is_visible" . ", export_enabled" . ", import_enabled" . ", default_sort_field_id"
164 . ", default_sort_field_order" . ", description" . ", public_comments" . ", view_own_records_perm"
165 . ", delete_by_owner, save_confirmation , table_order ) VALUES (" . $this->db->quote(
166 $this->getId(),
167 "integer"
168 ) . ","
169 . $this->db->quote($this->getObjId(), "integer") . "," . $this->db->quote($this->getTitle(), "text") . ","
170 . $this->db->quote($this->getAddPerm() ? 1 : 0, "integer") . "," . $this->db->quote(
171 $this->getEditPerm() ? 1 : 0,
172 "integer"
173 ) . ","
174 . $this->db->quote(
175 $this->getDeletePerm() ? 1 : 0,
176 "integer"
177 ) . "," . $this->db->quote($this->getEditByOwner() ? 1 : 0, "integer") . ","
178 . $this->db->quote($this->getLimited() ? 1 : 0, "integer") . "," . $this->db->quote(
179 $this->getLimitStart(),
180 "timestamp"
181 ) . ","
182 . $this->db->quote($this->getLimitEnd(), "timestamp") . "," . $this->db->quote(
183 $this->getIsVisible() ? 1 : 0,
184 "integer"
185 ) . ","
186 . $this->db->quote(
187 $this->getExportEnabled() ? 1 : 0,
188 "integer"
189 ) . "," . $this->db->quote($this->getImportEnabled() ? 1 : 0, "integer") . ","
190 . $this->db->quote($this->getDefaultSortField(), "text") . "," . $this->db->quote(
192 "text"
193 ) . ","
194 . $this->db->quote($this->getDescription(), "text") . "," . $this->db->quote(
196 "integer"
197 ) . ","
198 . $this->db->quote(
199 $this->getViewOwnRecordsPerm(),
200 "integer"
201 ) . "," . $this->db->quote($this->getDeleteByOwner() ? 1 : 0, 'integer') . ","
202 . $this->db->quote($this->getSaveConfirmation() ? 1 : 0, 'integer') . "," . $this->db->quote(
203 $this->getOrder(),
204 'integer'
205 ) . ")";
206
207 $this->db->manipulate($query);
208
209 if ($create_standardview) {
210 //standard tableview
212 }
213
214 if ($create_tablefield_setting) {
215 $this->buildOrderFields();
216 }
217 }
218
219 public function doUpdate(): void
220 {
221 $this->db->update(
222 "il_dcl_table",
223 [
224 "obj_id" => ["integer", $this->getObjId()],
225 "title" => ["text", $this->getTitle()],
226 "add_perm" => ["integer", (int) $this->getAddPerm()],
227 "edit_perm" => ["integer", (int) $this->getEditPerm()],
228 "delete_perm" => ["integer", (int) $this->getDeletePerm()],
229 "edit_by_owner" => ["integer", (int) $this->getEditByOwner()],
230 "limited" => ["integer", $this->getLimited()],
231 "limit_start" => ["timestamp", $this->getLimitStart()],
232 "limit_end" => ["timestamp", $this->getLimitEnd()],
233 "is_visible" => ["integer", $this->getIsVisible() ? 1 : 0],
234 "export_enabled" => ["integer", $this->getExportEnabled() ? 1 : 0],
235 "import_enabled" => ["integer", $this->getImportEnabled() ? 1 : 0],
236 "description" => ["text", $this->getDescription()],
237 "default_sort_field_id" => ["text", $this->getDefaultSortField()],
238 "default_sort_field_order" => ["text", $this->getDefaultSortFieldOrder()],
239 "public_comments" => ["integer", $this->getPublicCommentsEnabled() ? 1 : 0],
240 "view_own_records_perm" => ["integer", $this->getViewOwnRecordsPerm()],
241 'delete_by_owner' => ['integer', $this->getDeleteByOwner() ? 1 : 0],
242 'save_confirmation' => ['integer', $this->getSaveConfirmation() ? 1 : 0],
243 'table_order' => ['integer', $this->getOrder()],
244 ],
245 [
246 "id" => ["integer", $this->getId()],
247 ]
248 );
249 }
250
254 public function setId(int $a_id): void
255 {
256 $this->id = $a_id;
257 }
258
262 public function getId(): int
263 {
264 return $this->id;
265 }
266
267 public function setObjId(int $a_id): void
268 {
269 $this->objId = $a_id;
270 }
271
272 public function getObjId(): ?int
273 {
274 return $this->objId;
275 }
276
277 public function setTitle(string $a_title): void
278 {
279 $this->title = $a_title;
280 }
281
282 public function getTitle(): string
283 {
284 return $this->title;
285 }
286
288 {
289 $this->loadObj();
290
291 return $this->obj;
292 }
293
294 protected function loadObj(): void
295 {
296 if ($this->obj == null) {
297 $this->obj = new ilObjDataCollection($this->objId, false);
298 }
299 }
300
304 public function getRecords(): array
305 {
306 if ($this->records == null) {
307 $this->loadRecords();
308 }
309
310 return $this->records;
311 }
312
313 public function loadRecords(): void
314 {
315 $records = [];
316 $query = "SELECT id FROM il_dcl_record WHERE table_id = " . $this->db->quote($this->id, "integer");
317 $set = $this->db->query($query);
318
319 while ($rec = $this->db->fetchAssoc($set)) {
320 $records[$rec['id']] = ilDclCache::getRecordCache($rec['id']);
321 }
322
323 $this->records = $records;
324 }
325
326 public function deleteField(int $field_id): void
327 {
328 $field = ilDclCache::getFieldCache($field_id);
329 $records = $this->getRecords();
330
331 foreach ($records as $record) {
332 $record->deleteField($field_id);
333 }
334
335 $field->doDelete();
336 if ($this->getDefaultSortField() === $field->getId()) {
337 $this->setDefaultSortField('');
338 $this->doUpdate();
339 }
340 }
341
342 public function getField(string $field_id): ?ilDclBaseFieldModel
343 {
344 $fields = $this->getFields();
345 $field = null;
346 foreach ($fields as $field_1) {
347 if ($field_1->getId() == $field_id) {
348 $field = $field_1;
349 }
350 }
351
352 return $field;
353 }
354
355 public function getFieldIds(): array
356 {
357 $field_ids = [];
358 foreach ($this->getFields() as $field) {
359 if ($field->getId()) {
360 $field_ids[] = $field->getId();
361 }
362 }
363
364 return $field_ids;
365 }
366
367 protected function loadCustomFields(): void
368 {
369 if (!$this->fields) {
370 $query
371 = "SELECT DISTINCT il_dcl_field.*, il_dcl_tfield_set.field_order
372 FROM il_dcl_field
373 INNER JOIN il_dcl_tfield_set
374 ON ( il_dcl_tfield_set.field NOT IN ('owner',
375 'last_update',
376 'last_edit_by',
377 'id',
378 'create_date')
379 AND il_dcl_tfield_set.table_id = il_dcl_field.table_id
380 AND il_dcl_tfield_set.field = " . $this->db->cast("il_dcl_field.id", "text") . ")
381 WHERE il_dcl_field.table_id = %s
382 ORDER BY il_dcl_tfield_set.field_order ASC";
383
384 $set = $this->db->queryF($query, ['integer'], [$this->getId()]);
385 $fields = [];
386 while ($rec = $this->db->fetchAssoc($set)) {
388 if ($this->show_invalid || in_array($field->getDatatypeId(), array_keys(ilDclDatatype::getAllDatatype()))) {
389 $fields[] = $field;
390 }
391 }
392 $this->fields = $fields;
393
395 }
396 }
397
401 public function getCustomFields(): array
402 {
403 if (!$this->fields) {
404 $this->loadCustomFields();
405 }
406
407 return $this->fields;
408 }
409
414 public function getNewFieldOrder(): int
415 {
416 $fields = $this->getFields();
417 $place = 0;
418 foreach ($fields as $field) {
419 if (!$field->isStandardField()) {
420 $place = $field->getOrder() + 1;
421 }
422 }
423
424 return $place;
425 }
426
432 public function getFields(): array
433 {
434 if ($this->all_fields == null) {
435 $this->reloadFields();
436 }
437
438 return $this->all_fields;
439 }
440
441 public function reloadFields(): void
442 {
443 $this->loadCustomFields();
444 $this->stdFields = $this->getStandardFields();
445 $fields = array_merge($this->fields, $this->stdFields);
446
447 $this->sortByOrder($fields);
448
449 $this->all_fields = $fields;
450 }
451
455 public function getTableViews(): array
456 {
458 }
459
463 public function getVisibleTableViews(int $user_id = 0, bool $with_active_detailedview = false): array
464 {
465 if (ilObjDataCollectionAccess::hasWriteAccess($this->getCollectionObject()->getRefId(), $user_id) && !$with_active_detailedview) {
466 return $this->getTableViews();
467 }
468
469 $visible_views = [];
470 foreach ($this->getTableViews() as $tableView) {
472 $page = new ilDclDetailedViewDefinitionGUI($tableView->getId());
473 if (!$with_active_detailedview || $page->getPageObject()->isActive()) {
474 $visible_views[] = $tableView;
475 }
476 }
477 }
478
479 return $visible_views;
480 }
481
482 public function getFirstTableViewId(int $user_id = 0, bool $with_detailed_view = false): ?int
483 {
484 $array = $this->getVisibleTableViews($user_id, $with_detailed_view);
485 $tableview = array_shift($array);
486
487 return $tableview?->getId();
488 }
489
494 public function getFieldsForFormula(): array
495 {
496 $syntax_chars = array_merge(
497 array_map(static fn(Operators $function): string => $function->value, Operators::cases()),
498 array_map(static fn(Functions $function): string => $function->value, Functions::cases()),
499 ['(', ')', ',']
500 );
501 foreach ($this->getFields() as $field) {
502 if (in_array($field->getDatatypeId(), ilDclFormulaFieldModel::SUPPORTED_FIELDS)) {
503 foreach ($syntax_chars as $element) {
504 if (str_contains($field->getTitle(), $element)) {
505 continue 2;
506 }
507 }
508 $return[] = $field;
509 }
510 }
511
512 return $return;
513 }
514
520 public function getStandardFields(): array
521 {
522 if ($this->stdFields == null) {
523 $this->stdFields = ilDclStandardField::_getStandardFields($this->id);
524 // Don't return comments as field if this feature is not activated in the settings
525 if (!$this->getPublicCommentsEnabled()) {
527 foreach ($this->stdFields as $k => $field) {
528 if ($field->getId() == 'comments') {
529 unset($this->stdFields[$k]);
530 break;
531 }
532 }
533 }
534 }
535
536 return $this->stdFields;
537 }
538
543 public function getRecordFields(): array
544 {
545 $this->loadCustomFields();
546
547 return $this->fields;
548 }
549
554 public function getEditableFields(bool $creation_mode): array
555 {
556 $fields = $this->getRecordFields();
557 $editableFields = [];
558
559 foreach ($fields as $field) {
560 $tableview_id = $this->http->wrapper()->post()->retrieve(
561 'tableview_id',
562 $this->refinery->kindlyTo()->int()
563 );
564 if (!$field->getViewSetting($tableview_id)->isLocked($creation_mode)) {
565 $editableFields[] = $field;
566 }
567 }
568
569 return $editableFields;
570 }
571
576 public function getExportableFields(): array
577 {
578 $fields = $this->getFields();
579 $exportableFields = [];
580 foreach ($fields as $field) {
581 if ($field->getExportable()) {
582 $exportableFields[] = $field;
583 }
584 }
585
586 return $exportableFields;
587 }
588
594 public function hasPermissionToEditRecord(int $ref_id, ilDclBaseRecordModel $record): bool
595 {
596 if ($this->getObjId() != ilObjDataCollection::_lookupObjectId($ref_id)) {
597 return false;
598 }
600 return true;
601 }
603 return false;
604 }
605 if ($this->getEditByOwner()) {
606 return $this->doesRecordBelongToUser($record);
607 }
608 if (!$this->checkLimit()) {
609 return false;
610 }
612 return true;
613 }
614 if ($this->getEditPerm() && !$this->getEditByOwner()) {
615 return true;
616 }
617
618 return false;
619 }
620
625 public function hasPermissionToDeleteRecord(int $ref_id, ilDclBaseRecordModel $record): bool
626 {
627 if ($this->getObjId() != ilObjDataCollection::_lookupObjectId($ref_id)) {
628 return false;
629 }
631 return true;
632 }
634 return false;
635 }
636 if (!$this->checkLimit()) {
637 return false;
638 }
639 if ($this->getDeletePerm() && !$this->getDeleteByOwner()) {
640 return true;
641 }
642 if ($this->getDeleteByOwner()) {
643 return $this->doesRecordBelongToUser($record);
644 }
645
646 return false;
647 }
648
653 public function hasPermissionToDeleteRecords(int $ref_id): bool
654 {
655 if ($this->getObjId() != ilObjDataCollection::_lookupObjectId($ref_id)) {
656 return false;
657 }
658
661 }
662
663 public function hasPermissionToViewRecord(int $ref_id, ilDclBaseRecordModel $record, int $user_id = 0): bool
664 {
665 if ($this->getObjId() != ilObjDataCollection::_lookupObjectId($ref_id)) {
666 return false;
667 }
669 $ref_id,
672 return true;
673 }
675 // Check for view only own entries setting
676 if ($this->getViewOwnRecordsPerm() && ($user_id ?: $this->user->getId()) != $record->getOwner()) {
677 return false;
678 }
679
680 return true;
681 }
682
683 return false;
684 }
685
686 protected function doesRecordBelongToUser(ilDclBaseRecordModel $record): bool
687 {
688 return ($this->user->getId() == $record->getOwner());
689 }
690
691 public function checkLimit(): bool
692 {
693 if ($this->getLimited()) {
694 $from = null;
695 $to = null;
696 $now = new ilDateTime(date("Y-m-d H:i:s"), IL_CAL_DATE);
697
698 if ($this->getLimitStart() != "") {
699 $from = new ilDateTime($this->getLimitStart(), IL_CAL_DATE);
700 }
701 if ($this->getLimitEnd() != "") {
702 $to = new ilDateTime($this->getLimitEnd(), IL_CAL_DATE);
703 }
704
705 if ($from == null && $to == null) {
706 return true;
707 }
708 if ($from <= $now && $now <= $to) {
709 return true;
710 }
711 if ($from <= $now && $to == null) {
712 return true;
713 }
714 if ($from == null && $now <= $to) {
715 return true;
716 }
717
718 return ($from <= $now && $now <= $to);
719 }
720
721 return true;
722 }
723
727 public function updateFields(): void
728 {
729 foreach ($this->getFields() as $field) {
730 $field->doUpdate();
731 }
732 }
733
738 public function sortFields(array &$fields): void
739 {
740 $this->sortByOrder($fields);
741 //After sorting the array loses it's keys respectivly their keys are set form $field->id to 1,2,3... so we reset the keys.
742 $named = [];
743 foreach ($fields as $field) {
744 $named[$field->getId()] = $field;
745 }
746
747 $fields = $named;
748 }
749
753 protected function sortByOrder(array &$array): void
754 {
755 // php-bug: https://bugs.php.net/bug.php?id=50688
756 // fixed in php 7 but for now we need the @ a workaround
757 usort($array, [$this, "compareOrder"]);
758 }
759
764 public function buildOrderFields(): void
765 {
766 $fields = $this->getFields();
767 $this->sortByOrder($fields);
768 $count = 10;
769 $offset = 10;
770 foreach ($fields as $field) {
771 if (!is_null($field->getOrder())) {
772 $field->setOrder($count);
773 $count = $count + $offset;
774 $field->doUpdate();
775 }
776 }
777 }
778
783 {
784 $return = null;
785 foreach ($this->getFields() as $field) {
786 if ($field->getTitle() == $title) {
787 $return = $field;
788 break;
789 }
790 }
791
792 return $return;
793 }
794
795 public function setAddPerm(bool $add_perm): void
796 {
797 $this->add_perm = $add_perm;
798 }
799
800 public function getAddPerm(): bool
801 {
802 return $this->add_perm;
803 }
804
805 public function setDeletePerm(bool $delete_perm): void
806 {
807 $this->delete_perm = $delete_perm;
808 if (!$delete_perm) {
809 $this->setDeleteByOwner(false);
810 }
811 }
812
813 public function getDeletePerm(): bool
814 {
815 return $this->delete_perm;
816 }
817
818 public function setEditByOwner(bool $edit_by_owner): void
819 {
820 $this->edit_by_owner = $edit_by_owner;
821 if ($edit_by_owner) {
822 $this->setEditPerm(true);
823 }
824 }
825
826 public function getEditByOwner(): bool
827 {
829 }
830
831 public function getDeleteByOwner(): bool
832 {
834 }
835
836 public function setDeleteByOwner(bool $delete_by_owner): void
837 {
838 $this->delete_by_owner = $delete_by_owner;
839 if ($delete_by_owner) {
840 $this->setDeletePerm(true);
841 }
842 }
843
844 public function setEditPerm(bool $edit_perm): void
845 {
846 $this->edit_perm = $edit_perm;
847 if (!$edit_perm) {
848 $this->setEditByOwner(false);
849 }
850 }
851
852 public function getEditPerm(): bool
853 {
854 return $this->edit_perm;
855 }
856
857 public function setLimited(bool $limited): void
858 {
859 $this->limited = $limited;
860 }
861
862 public function getLimited(): bool
863 {
864 return $this->limited;
865 }
866
867 public function setLimitEnd(string $limit_end): void
868 {
869 $this->limit_end = $limit_end;
870 }
871
872 public function getLimitEnd(): string
873 {
874 return $this->limit_end;
875 }
876
877 public function setLimitStart(string $limit_start): void
878 {
879 $this->limit_start = $limit_start;
880 }
881
882 public function getLimitStart(): string
883 {
884 return $this->limit_start;
885 }
886
887 public function setIsVisible(bool $is_visible): void
888 {
889 $this->is_visible = $is_visible;
890 }
891
892 public function getIsVisible(): bool
893 {
894 return $this->is_visible;
895 }
896
897 public function setDescription(string $description): void
898 {
899 $this->description = $description;
900 }
901
902 public function getDescription(): string
903 {
904 return $this->description;
905 }
906
907 public function setDefaultSortField(string $default_sort_field): void
908 {
909 $default_sort_field = ($default_sort_field) ?: ""; // Change null or empty strings to zero
910 $this->default_sort_field = $default_sort_field;
911 }
912
913 public function getDefaultSortField(): string
914 {
916 }
917
919 {
920 if (!in_array($default_sort_field_order, ['asc', 'desc'])) {
922 }
923 $this->default_sort_field_order = $default_sort_field_order;
924 }
925
926 public function getDefaultSortFieldOrder(): string
927 {
929 }
930
931 public function setPublicCommentsEnabled(bool $public_comments): void
932 {
933 $this->public_comments = $public_comments;
934 }
935
936 public function getPublicCommentsEnabled(): bool
937 {
939 }
940
941 public function setViewOwnRecordsPerm(bool $view_own_perm): void
942 {
943 $this->view_own_records_perm = (int) $view_own_perm;
944 }
945
946 public function getViewOwnRecordsPerm(): bool
947 {
948 return (bool) $this->view_own_records_perm;
949 }
950
951 public function getSaveConfirmation(): bool
952 {
954 }
955
956 public function setSaveConfirmation(bool $save_confirmation): void
957 {
958 $this->save_confirmation = $save_confirmation;
959 }
960
961 public function hasCustomFields(): bool
962 {
963 $this->loadCustomFields();
964
965 return count($this->fields) > 0;
966 }
967
969 {
970 if (is_null($a->getOrder() == null) && is_null($b->getOrder() == null)) {
971 return 0;
972 }
973 if (is_null($a->getOrder())) {
974 return 1;
975 }
976 if (is_null($b->getOrder())) {
977 return -1;
978 }
979
980 return $a->getOrder() < $b->getOrder() ? -1 : 1;
981 }
982
983 public function cloneStructure(ilDclTable $original): void
984 {
985 $this->setTitle($original->getTitle());
986 $this->setDescription($original->getDescription());
987 $this->setIsVisible($original->getIsVisible());
988 $this->setEditByOwner($original->getEditByOwner());
989 $this->setAddPerm($original->getAddPerm());
990 $this->setEditPerm($original->getEditPerm());
991 $this->setDeleteByOwner($original->getDeleteByOwner());
992 $this->setSaveConfirmation($original->getSaveConfirmation());
993 $this->setDeletePerm($original->getDeletePerm());
994 $this->setLimited($original->getLimited());
995 $this->setLimitStart($original->getLimitStart());
996 $this->setLimitEnd($original->getLimitEnd());
997 $this->setViewOwnRecordsPerm($original->getViewOwnRecordsPerm());
998 $this->setExportEnabled($original->getExportEnabled());
999 $this->setImportEnabled($original->getImportEnabled());
1002 $this->setOrder($original->getOrder());
1003
1004 $this->doCreate(true, false);
1005 // reset stdFields to get new for the created object
1006
1007 $default_sort_field = 0;
1008 // Clone standard-fields
1009 $org_std_fields = $original->getStandardFields();
1010 foreach ($this->getStandardFields() as $element_key => $std_field) {
1011 $std_field->clone($org_std_fields[$element_key]);
1012 if ($std_field->getId() === $original->getDefaultSortField()) {
1013 $default_sort_field = $std_field->getId();
1014 }
1015 }
1016
1017 // Clone fields
1018 $new_fields = [];
1019 foreach ($original->getFields() as $orig_field) {
1020 if (!$orig_field->isStandardField()) {
1021 $class_name = get_class($orig_field);
1022 $new_field = new $class_name();
1023 $new_field->setTableId($this->getId());
1024 $new_field->cloneStructure((int) $orig_field->getId());
1025 $new_fields[$orig_field->getId()] = $new_field;
1026
1027 if ($orig_field->getId() === $original->getDefaultSortField()) {
1028 $default_sort_field = $new_field->getId();
1029 }
1030 }
1031 }
1032
1033 $this->setDefaultSortField((string) $default_sort_field);
1034 $this->doUpdate();
1035
1036 // Clone Records with recordfields
1037 foreach ($original->getRecords() as $orig_record) {
1038 $new_record = new ilDclBaseRecordModel();
1039 $new_record->setTableId($this->getId());
1040 $new_record->cloneStructure($orig_record->getId(), $new_fields);
1041 }
1042
1043 //clone tableviews (includes pageobjects)
1044 foreach ($original->getTableViews() as $orig_tableview) {
1045 $new_tableview = new ilDclTableView();
1046 $new_tableview->setTableId($this->getId());
1047 $new_tableview->cloneStructure($orig_tableview, $new_fields);
1048 }
1049
1050 // mandatory for all cloning functions
1051 ilDclCache::setCloneOf($original->getId(), $this->getId(), ilDclCache::TYPE_TABLE);
1052 }
1053
1054 public function afterClone(): void
1055 {
1056 foreach ($this->getFields() as $field) {
1057 $field->afterClone($this->getRecords());
1058 }
1059 }
1060
1064 public function _hasRecords(): bool
1065 {
1066 return count($this->getRecords()) > 0;
1067 }
1068
1072 public function addField(ilDclBaseFieldModel $field): void
1073 {
1074 $this->all_fields[$field->getId()] = $field;
1075 }
1076
1080 public static function _tableExists(int $table_id): bool
1081 {
1082 global $DIC;
1083 $ilDB = $DIC['ilDB'];
1084
1085 $query = "SELECT * FROM il_dcl_table WHERE id = " . $table_id;
1086 $result = $ilDB->query($query);
1087
1088 return $result->numRows() != 0;
1089 }
1090
1094 public static function _getTableIdByTitle(string $title, int $obj_id): int
1095 {
1096 global $DIC;
1097 $ilDB = $DIC['ilDB'];
1098
1099 $result = $ilDB->query(
1100 'SELECT id FROM il_dcl_table WHERE title = ' . $ilDB->quote($title, 'text') . ' AND obj_id = '
1101 . $ilDB->quote($obj_id, 'integer')
1102 );
1103 $id = 0;
1104 while ($rec = $ilDB->fetchAssoc($result)) {
1105 $id = $rec['id'];
1106 }
1107
1108 return $id;
1109 }
1110
1111 public function setExportEnabled(bool $export_enabled): void
1112 {
1113 $this->export_enabled = $export_enabled;
1114 }
1115
1116 public function getExportEnabled(): bool
1117 {
1118 return $this->export_enabled;
1119 }
1120
1121 public function getOrder(): int
1122 {
1123 if (!$this->table_order) {
1124 $this->updateOrder();
1125 }
1126
1127 return $this->table_order;
1128 }
1129
1130 public function updateOrder(): void
1131 {
1132 $result = $this->db->query('SELECT MAX(table_order) AS table_order FROM il_dcl_table WHERE obj_id = ' . $this->db->quote(
1133 $this->getCollectionObject()->getId(),
1134 'integer'
1135 ));
1136 $this->table_order = $this->db->fetchObject($result)->table_order + 10;
1137 $this->db->query('UPDATE il_dcl_table SET table_order = ' . $this->db->quote(
1138 $this->table_order,
1139 'integer'
1140 ) . ' WHERE id = ' . $this->db->quote($this->getId(), 'integer'));
1141 }
1142
1143 public function setOrder(int $table_order): void
1144 {
1145 $this->table_order = $table_order;
1146 }
1147
1148 public function setImportEnabled(bool $import_enabled): void
1149 {
1150 $this->import_enabled = $import_enabled;
1151 }
1152
1153 public function getImportEnabled(): bool
1154 {
1155 return $this->import_enabled;
1156 }
1157
1163 public static function _hasFieldByTitle(string $title, int $obj_id): bool
1164 {
1165 global $DIC;
1166 $ilDB = $DIC['ilDB'];
1167 $result = $ilDB->query(
1168 'SELECT * FROM il_dcl_field WHERE table_id = ' . $ilDB->quote($obj_id, 'integer') . ' AND title = '
1169 . $ilDB->quote($title, 'text')
1170 );
1171
1172 return (bool) $ilDB->numRows($result);
1173 }
1174
1184 public function getPartialRecords(
1185 string $ref_id,
1186 string $sort,
1187 string $direction,
1188 ?int $limit,
1189 int $offset,
1190 array $filter = []
1191 ): array {
1192 $sort_field = $this->getFieldByTitle($sort);
1193 $direction = strtolower($direction);
1194 $direction = (in_array($direction, ['desc', 'asc'])) ? $direction : 'asc';
1195
1196 // Sorting by a status from an ILIAS Ref field. This column is added dynamically to the table, there is no field model
1197 $sort_by_status = false;
1198 if (substr($sort, 0, 8) == '_status_') {
1199 $sort_by_status = true;
1200 $sort_field = $this->getFieldByTitle(substr($sort, 8));
1201 }
1202
1203 if (is_null($sort_field)) {
1204 $sort_field = $this->getField('id');
1205 }
1206
1207 $sort_query_object = $sort_field->getRecordQuerySortObject($direction, $sort_by_status);
1208
1209 $select_str = ($sort_query_object != null) ? $sort_query_object->getSelectStatement() : '';
1210 $join_str = ($sort_query_object != null) ? $sort_query_object->getJoinStatement() : '';
1211 $where_str = ($sort_query_object != null) ? $sort_query_object->getWhereStatement() : '';
1212 $order_str = ($sort_query_object != null) ? $sort_query_object->getOrderStatement() : '';
1213 $group_str = ($sort_query_object != null) ? $sort_query_object->getGroupStatement() : '';
1214
1215 if (count($filter)) {
1216 foreach ($filter as $key => $filter_value) {
1217 $filter_field_id = substr($key, 7);
1218 $filter_field = $this->getField($filter_field_id);
1219 $filter_record_query_object = $filter_field->getRecordQueryFilterObject($filter_value, $sort_field);
1220
1221 if ($filter_record_query_object) {
1222 $select_str .= $filter_record_query_object->getSelectStatement();
1223 $join_str .= $filter_record_query_object->getJoinStatement();
1224 $where_str .= $filter_record_query_object->getWhereStatement();
1225 $group_str .= $filter_record_query_object->getGroupStatement();
1226 }
1227 }
1228 }
1229
1230 // Build the query string
1231 $sql = "SELECT DISTINCT record.id, record.owner";
1232 if ($select_str) {
1233 $sql .= ', ';
1234 }
1235
1236 $as = ' AS ';
1237
1238 $sql .= rtrim($select_str, ',') . " FROM il_dcl_record $as record ";
1239 $sql .= $join_str;
1240 $sql .= " WHERE record.table_id = " . $this->db->quote($this->getId(), 'integer');
1241
1242 if (strlen($where_str) > 0) {
1243 $sql .= $where_str;
1244 }
1245
1246 if (strlen($group_str) > 0) {
1247 $sql .= " GROUP BY " . $group_str;
1248 }
1249
1250 if (strlen($order_str) > 0) {
1251 $sql .= " ORDER BY " . $order_str;
1252 }
1253
1254 $set = $this->db->query($sql);
1255 $total_record_ids = [];
1256
1258 while ($rec = $this->db->fetchAssoc($set)) {
1259 // Quick check if the current user is allowed to view the record
1260 if (!$is_allowed_to_view && ($this->getViewOwnRecordsPerm() && $this->user->getId() != $rec['owner'])) {
1261 continue;
1262 }
1263 $total_record_ids[] = $rec['id'];
1264 }
1265 // Save record-ids in session to enable prev/next links in detail view
1266 ilSession::set('dcl_table_id', $this->getId());
1267 ilSession::set('dcl_record_ids', $total_record_ids);
1268
1269 if ($sort_query_object != null) {
1270 $total_record_ids = $sort_query_object->applyCustomSorting($sort_field, $total_record_ids, $direction);
1271 }
1272
1273 if ($sort === 'n_comments') {
1274 global $DIC;
1275 $comments_nr = [];
1276 foreach ($total_record_ids as $id) {
1277 $comments_nr[$id] = $DIC->notes()->domain()->getNrOfCommentsForContext($DIC->notes()->data()->context($this->getObjId(), $id, 'dcl'));
1278 }
1279 uasort($comments_nr, static fn($a, $b) => ($direction === 'asc' ? 1 : -1) * ($a <=> $b));
1280 $total_record_ids = array_keys($comments_nr);
1281 }
1282
1283 // Now slice the array to load only the needed records in memory
1284 $record_ids = array_slice($total_record_ids, $offset, $limit);
1285
1286 $records = [];
1287 foreach ($record_ids as $id) {
1289 }
1290
1291 return ['records' => $records, 'total' => count($total_record_ids)];
1292 }
1293
1294 public function showInvalidFields(bool $value): void
1295 {
1296 $this->show_invalid = $value;
1297 }
1298}
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:38
const IL_CAL_DATE
@classDescription Date and time handling
static getRecordCache(?int $record_id)
static getFieldCache(int $field_id=0)
static buildFieldFromRecord(array $rec)
static preloadFieldProperties(array $fields)
Preloads field properties.
static setCloneOf(int $old, int $new, string $type)
static getAllDatatype(bool $force=false)
Get all possible Datatypes.
@ilCtrl_Calls ilDclDetailedViewDefinitionGUI: ilPageEditorGUI, ilEditClipboardGUI,...
static _getStandardFields(int $table_id)
static createOrGetStandardView(int $table_id)
static getAllForTableId(int $table_id)
hasPermissionToDeleteRecords(int $ref_id)
int $view_own_records_perm
True if user can only view his/her own entries in the table.
updateFields()
Update fields.
setDefaultSortField(string $default_sort_field)
getFieldsForFormula()
Returns all fields of this table including the standard fields, wich are supported for formulas.
_hasRecords()
_hasRecords
ilObjUser $user
setEditByOwner(bool $edit_by_owner)
getRecordFields()
Returns all fields of this table which are NOT standard fields.
setDeletePerm(bool $delete_perm)
addField(ilDclBaseFieldModel $field)
setSaveConfirmation(bool $save_confirmation)
static _tableExists(int $table_id)
buildOrderFields()
buildOrderFields orders the fields.
setEditPerm(bool $edit_perm)
string $default_sort_field
doesRecordBelongToUser(ilDclBaseRecordModel $record)
setDeleteByOwner(bool $delete_by_owner)
showInvalidFields(bool $value)
getExportableFields()
Return all the fields that are marked as exportable.
string $limit_start
getFirstTableViewId(int $user_id=0, bool $with_detailed_view=false)
setIsVisible(bool $is_visible)
sortByOrder(array &$array)
deleteField(int $field_id)
setLimitStart(string $limit_start)
getNewFieldOrder()
getNewOrder
setOrder(int $table_order)
getField(string $field_id)
array $all_fields
table fields and std fields combined
sortFields(array &$fields)
sortFields
hasPermissionToEditRecord(int $ref_id, ilDclBaseRecordModel $record)
cloneStructure(ilDclTable $original)
setImportEnabled(bool $import_enabled)
setObjId(int $a_id)
compareOrder(ilDclBaseFieldModel $a, ilDclBaseFieldModel $b)
setTitle(string $a_title)
hasPermissionToDeleteRecord(int $ref_id, ilDclBaseRecordModel $record)
setId(int $a_id)
Set table id.
getPartialRecords(string $ref_id, string $sort, string $direction, ?int $limit, int $offset, array $filter=[])
Return only the needed subset of record objects for the table, according to sorting,...
ilObjDataCollection $obj
setDescription(string $description)
doCreate(bool $create_tablefield_setting=true, bool $create_standardview=true)
string $default_sort_field_order
Default sort-order (asc|desc)
getFields()
Returns all fields of this table including the standard fields.
getEditableFields(bool $creation_mode)
setDefaultSortFieldOrder(string $default_sort_field_order)
setPublicCommentsEnabled(bool $public_comments)
setAddPerm(bool $add_perm)
doRead()
Read table.
__construct(int $a_id=0)
getFieldByTitle(string $title)
Get a field by title.
setLimited(bool $limited)
getId()
Get table id.
ILIAS Refinery Factory $refinery
static _getTableIdByTitle(string $title, int $obj_id)
ilDBInterface $db
string $description
Description for this table displayed above records.
static _hasFieldByTitle(string $title, int $obj_id)
Checks if a table has a field with the given title.
bool $save_confirmation
doDelete(bool $delete_only_content=false, bool $omit_notification=false)
Delete table Attention this does not delete the maintable of it's the maintable of the collection.
ILIAS HTTP Services $http
setLimitEnd(string $limit_end)
hasPermissionToViewRecord(int $ref_id, ilDclBaseRecordModel $record, int $user_id=0)
setExportEnabled(bool $export_enabled)
bool $public_comments
True if users can add comments on each record of this table.
setViewOwnRecordsPerm(bool $view_own_perm)
getVisibleTableViews(int $user_id=0, bool $with_active_detailedview=false)
static hasWriteAccess(int $ref, ?int $user_id=0)
static hasReadAccess(int $ref, ?int $user_id=0)
static hasAccessToTableView(ilDclTableView $tableview, ?int $user_id=0)
static hasEditAccess(int $ref, ?int $user_id=null)
static hasAddRecordAccess(int $ref, ?int $user_id=0)
User class.
static _lookupObjectId(int $ref_id)
static set(string $a_var, $a_val)
Set a value.
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:66
static http()
Fetches the global http state from ILIAS.
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $DIC
Definition: shib_login.php:26