ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilDclTable.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4
18{
19
23 protected $id = 0;
27 protected $objId;
31 protected $obj;
35 protected $title;
39 protected $fields;
43 protected $stdFields;
47 protected $records;
51 protected $is_visible;
55 protected $add_perm;
59 protected $edit_perm;
63 protected $delete_perm;
67 protected $edit_by_owner;
79 protected $limited;
83 protected $limit_start;
87 protected $limit_end;
91 protected $export_enabled;
95 protected $table_order;
99 protected $import_enabled;
105 protected $default_sort_field = 0;
111 protected $default_sort_field_order = 'asc';
117 protected $description = '';
123 protected $public_comments = 0;
135 protected $all_fields = null;
136
137
141 public function __construct($a_id = 0)
142 {
143 if ($a_id != 0) {
144 $this->id = $a_id;
145 $this->doRead();
146 }
147 }
148
149
153 public function doRead()
154 {
155 global $DIC;
156 $ilDB = $DIC['ilDB'];
157
158 $query = "SELECT * FROM il_dcl_table WHERE id = " . $ilDB->quote($this->getId(), "integer");
159 $set = $ilDB->query($query);
160 $rec = $ilDB->fetchAssoc($set);
161
162 $this->setObjId($rec["obj_id"]);
163 $this->setTitle($rec["title"]);
164 $this->setAddPerm($rec["add_perm"]);
165 $this->setEditPerm($rec["edit_perm"]);
166 $this->setDeletePerm($rec["delete_perm"]);
167 $this->setEditByOwner($rec["edit_by_owner"]);
168 $this->setExportEnabled($rec["export_enabled"]);
169 $this->setImportEnabled($rec["import_enabled"]);
170 $this->setLimited($rec["limited"]);
171 $this->setLimitStart($rec["limit_start"]);
172 $this->setLimitEnd($rec["limit_end"]);
173 $this->setIsVisible($rec["is_visible"]);
174 $this->setDescription($rec['description']);
175 $this->setDefaultSortField($rec['default_sort_field_id']);
176 $this->setDefaultSortFieldOrder($rec['default_sort_field_order']);
177 $this->setPublicCommentsEnabled($rec['public_comments']);
178 $this->setViewOwnRecordsPerm($rec['view_own_records_perm']);
179 $this->setDeleteByOwner($rec['delete_by_owner']);
180 $this->setSaveConfirmation($rec['save_confirmation']);
181 $this->setOrder($rec['table_order']);
182 }
183
184
192 public function doDelete($delete_only_content = false, $omit_notification = false)
193 {
194 global $DIC;
195 $ilDB = $DIC['ilDB'];
196
198 foreach ($this->getRecords() as $record) {
199 $record->doDelete($omit_notification);
200 }
201
202 foreach ($this->getRecordFields() as $field) {
203 $field->doDelete();
204 }
205
206 // // SW: Fix #12794 und #11405
207 // // Problem is that when the DC object gets deleted, $this::getCollectionObject() tries to load the DC but it's not in the DB anymore
208 // // If $delete_main_table is true, avoid getting the collection object
209 // $exec_delete = false;
210 // if ($delete_main_table) {
211 // $exec_delete = true;
212 // }
213 // if (!$exec_delete && $this->getCollectionObject()->getFirstVisibleTableId() != $this->getId()) {
214 // $exec_delete = true;
215 // }
216 if (!$delete_only_content) {
217 $query = "DELETE FROM il_dcl_table WHERE id = " . $ilDB->quote($this->getId(), "integer");
218 $ilDB->manipulate($query);
219 }
220 }
221
222
226 public function doCreate($create_tablefield_setting = true, $create_standardview = true)
227 {
228 global $DIC;
229 $ilDB = $DIC['ilDB'];
230
231 $id = $ilDB->nextId("il_dcl_table");
232 $this->setId($id);
233 $query = "INSERT INTO il_dcl_table (" . "id" . ", obj_id" . ", title" . ", add_perm" . ", edit_perm" . ", delete_perm" . ", edit_by_owner"
234 . ", limited" . ", limit_start" . ", limit_end" . ", is_visible" . ", export_enabled" . ", import_enabled" . ", default_sort_field_id"
235 . ", default_sort_field_order" . ", description" . ", public_comments" . ", view_own_records_perm"
236 . ", delete_by_owner, save_confirmation , table_order ) VALUES (" . $ilDB->quote($this->getId(), "integer") . ","
237 . $ilDB->quote($this->getObjId(), "integer") . "," . $ilDB->quote($this->getTitle(), "text") . ","
238 . $ilDB->quote($this->getAddPerm() ? 1 : 0, "integer") . "," . $ilDB->quote($this->getEditPerm() ? 1 : 0, "integer") . ","
239 . $ilDB->quote($this->getDeletePerm() ? 1 : 0, "integer") . "," . $ilDB->quote($this->getEditByOwner() ? 1 : 0, "integer") . ","
240 . $ilDB->quote($this->getLimited() ? 1 : 0, "integer") . "," . $ilDB->quote($this->getLimitStart(), "timestamp") . ","
241 . $ilDB->quote($this->getLimitEnd(), "timestamp") . "," . $ilDB->quote($this->getIsVisible() ? 1 : 0, "integer") . ","
242 . $ilDB->quote($this->getExportEnabled() ? 1 : 0, "integer") . "," . $ilDB->quote($this->getImportEnabled() ? 1 : 0, "integer") . ","
243 . $ilDB->quote($this->getDefaultSortField(), "text") . "," . $ilDB->quote($this->getDefaultSortFieldOrder(), "text") . ","
244 . $ilDB->quote($this->getDescription(), "text") . "," . $ilDB->quote($this->getPublicCommentsEnabled(), "integer") . ","
245 . $ilDB->quote($this->getViewOwnRecordsPerm(), "integer") . "," . $ilDB->quote($this->getDeleteByOwner() ? 1 : 0, 'integer') . ","
246 . $ilDB->quote($this->getSaveConfirmation() ? 1 : 0, 'integer') . "," . $ilDB->quote($this->getOrder(), 'integer') . ")";
247
248 $ilDB->manipulate($query);
249
250 if ($create_standardview) {
251 //standard tableview
253 }
254
255 if ($create_tablefield_setting) {
256 $this->buildOrderFields();
257 }
258 }
259
260
261 /*
262 * doUpdate
263 */
264 public function doUpdate()
265 {
266 global $DIC;
267 $ilDB = $DIC['ilDB'];
268
269 $ilDB->update(
270 "il_dcl_table",
271 array(
272 "obj_id" => array("integer", $this->getObjId()),
273 "title" => array("text", $this->getTitle()),
274 "add_perm" => array("integer", (int) $this->getAddPerm()),
275 "edit_perm" => array("integer", (int) $this->getEditPerm()),
276 "delete_perm" => array("integer", (int) $this->getDeletePerm()),
277 "edit_by_owner" => array("integer", (int) $this->getEditByOwner()),
278 "limited" => array("integer", $this->getLimited()),
279 "limit_start" => array("timestamp", $this->getLimitStart()),
280 "limit_end" => array("timestamp", $this->getLimitEnd()),
281 "is_visible" => array("integer", $this->getIsVisible() ? 1 : 0),
282 "export_enabled" => array("integer", $this->getExportEnabled() ? 1 : 0),
283 "import_enabled" => array("integer", $this->getImportEnabled() ? 1 : 0),
284 "description" => array("text", $this->getDescription()),
285 "default_sort_field_id" => array("text", $this->getDefaultSortField()),
286 "default_sort_field_order" => array("text", $this->getDefaultSortFieldOrder()),
287 "public_comments" => array("integer", $this->getPublicCommentsEnabled() ? 1 : 0),
288 "view_own_records_perm" => array("integer", $this->getViewOwnRecordsPerm()),
289 'delete_by_owner' => array('integer', $this->getDeleteByOwner() ? 1 : 0),
290 'save_confirmation' => array('integer', $this->getSaveConfirmation() ? 1 : 0),
291 'table_order' => array('integer', $this->getOrder()),
292 ),
293 array(
294 "id" => array("integer", $this->getId()),
295 )
296 );
297 }
298
299
305 public function setId($a_id)
306 {
307 $this->id = $a_id;
308 }
309
310
316 public function getId()
317 {
318 return $this->id;
319 }
320
321
325 public function setObjId($a_id)
326 {
327 $this->objId = $a_id;
328 }
329
330
334 public function getObjId()
335 {
336 return $this->objId;
337 }
338
339
343 public function setTitle($a_title)
344 {
345 $this->title = $a_title;
346 }
347
348
352 public function getTitle()
353 {
354 return $this->title;
355 }
356
357
361 public function getCollectionObject()
362 {
363 $this->loadObj();
364
365 return $this->obj;
366 }
367
368
369 protected function loadObj()
370 {
371 if ($this->obj == null) {
372 $this->obj = new ilObjDataCollection($this->objId, false);
373 }
374 }
375
376
380 public function getRecords()
381 {
382 if ($this->records == null) {
383 $this->loadRecords();
384 }
385
386 return $this->records;
387 }
388
389
390 public function loadRecords()
391 {
392 global $DIC;
393 $ilDB = $DIC['ilDB'];
394
395 $records = array();
396 $query = "SELECT id FROM il_dcl_record WHERE table_id = " . $ilDB->quote($this->id, "integer");
397 $set = $ilDB->query($query);
398
399 while ($rec = $ilDB->fetchAssoc($set)) {
400 $records[$rec['id']] = ilDclCache::getRecordCache($rec['id']);
401 }
402
403 $this->records = $records;
404 }
405
406
410 public function deleteField($field_id)
411 {
412 $field = ilDclCache::getFieldCache($field_id);
413 $records = $this->getRecords();
414
415 foreach ($records as $record) {
416 $record->deleteField($field_id);
417 }
418
419 $field->doDelete();
420 }
421
422
428 public function getField($field_id)
429 {
430 $fields = $this->getFields();
431 $field = null;
432 foreach ($fields as $field_1) {
433 if ($field_1->getId() == $field_id) {
434 $field = $field_1;
435 }
436 }
437
438 return $field;
439 }
440
441
447 public function getFieldIds()
448 {
449 $field_ids = array();
450 foreach ($this->getFields() as $field) {
451 if ($field->getId()) {
452 $field_ids[] = $field->getId();
453 }
454 }
455
456 return $field_ids;
457 }
458
459
460 protected function loadCustomFields()
461 {
462 if (!$this->fields) {
463 global $DIC;
464 $ilDB = $DIC['ilDB'];
468 $query
469 = "SELECT DISTINCT il_dcl_field.*, il_dcl_tfield_set.field_order
470 FROM il_dcl_field
471 INNER JOIN il_dcl_tfield_set
472 ON ( il_dcl_tfield_set.field NOT IN ('owner',
473 'last_update',
474 'last_edit_by',
475 'id',
476 'create_date')
477 AND il_dcl_tfield_set.table_id = il_dcl_field.table_id
478 AND il_dcl_tfield_set.field = " . $ilDB->cast("il_dcl_field.id", "text") . ")
479 WHERE il_dcl_field.table_id = %s
480 ORDER BY il_dcl_tfield_set.field_order ASC";
481
482 $set = $ilDB->queryF($query, array('integer'), array((int) $this->getId()));
483 $fields = array();
484 while ($rec = $ilDB->fetchAssoc($set)) {
486 $fields[] = $field;
487 }
488 $this->fields = $fields;
489
491 }
492 }
493
494
495 public function getCustomFields()
496 {
497 if (!$this->fields) {
498 $this->loadCustomFields();
499 }
500
501 return $this->fields;
502 }
503
504
510 public function getNewFieldOrder()
511 {
512 $fields = $this->getFields();
513 $place = 0;
514 foreach ($fields as $field) {
515 if (!$field->isStandardField()) {
516 $place = $field->getOrder() + 1;
517 }
518 }
519
520 return $place;
521 }
522
523
527 public function getNewTableviewOrder()
528 {
529 return (ilDclTableView::getCountForTableId($this->getId()) + 1) * 10;
530 }
531
532
536 public function sortTableViews(array $tableviews = null)
537 {
538 if ($tableviews == null) {
539 $tableviews = $this->getTableViews();
540 }
541
542 $order = 10;
543 foreach ($tableviews as $tableview) {
544 $tableview->setTableviewOrder($order);
545 $tableview->update();
546 $order += 10;
547 }
548 }
549
550
558 public function getFields()
559 {
560 if ($this->all_fields == null) {
561 $this->reloadFields();
562 }
563
564 return $this->all_fields;
565 }
566
567
568 public function reloadFields()
569 {
570 $this->loadCustomFields();
571 $this->stdFields = $this->getStandardFields();
572 $fields = array_merge($this->fields, $this->stdFields);
573
574 $this->sortByOrder($fields);
575
576 $this->all_fields = $fields;
577 }
578
579
583 public function getTableViews()
584 {
586 }
587
588
597 public function getVisibleTableViews($ref_id, $with_active_detailedview = false, $user_id = 0)
598 {
599 if (ilObjDataCollectionAccess::hasWriteAccess($ref_id, $user_id) && !$with_active_detailedview) {
600 return $this->getTableViews();
601 }
602
603 $visible_views = array();
604 foreach ($this->getTableViews() as $tableView) {
605 if (ilObjDataCollectionAccess::hasAccessToTableView($tableView, $user_id)) {
606 if (!$with_active_detailedview || ilDclDetailedViewDefinition::isActive($tableView->getId())) {
607 $visible_views[] = $tableView;
608 }
609 }
610 }
611
612 return $visible_views;
613 }
614
615
624 public function getFirstTableViewId($ref_id, $user_id = 0)
625 {
626 $tableview = array_shift($this->getVisibleTableViews($ref_id, false, $user_id));
627
628 return $tableview ? $tableview->getId() : false;
629 }
630
631
637 public function getFieldsForFormula()
638 {
639 $unsupported = array(
647 );
648
649 $this->loadCustomFields();
650 $return = $this->getStandardFields();
654 foreach ($this->fields as $field) {
655 if (!in_array($field->getDatatypeId(), $unsupported)) {
656 $return[] = $field;
657 }
658 }
659
660 return $return;
661 }
662
663
670 public function getStandardFields()
671 {
672 if ($this->stdFields == null) {
673 $this->stdFields = ilDclStandardField::_getStandardFields($this->id);
674 // Don't return comments as field if this feature is not activated in the settings
675 if (!$this->getPublicCommentsEnabled()) {
677 foreach ($this->stdFields as $k => $field) {
678 if ($field->getId() == 'comments') {
679 unset($this->stdFields[$k]);
680 break;
681 }
682 }
683 }
684 }
685
686 return $this->stdFields;
687 }
688
689
695 public function getRecordFields()
696 {
697 $this->loadCustomFields();
698
699 return $this->fields;
700 }
701
702
706 public function getEditableFields()
707 {
708 $fields = $this->getRecordFields();
709 $editableFields = array();
710
711 foreach ($fields as $field) {
712 if (!$field->getLocked()) {
713 $editableFields[] = $field;
714 }
715 }
716
717 return $editableFields;
718 }
719
720
726 public function getExportableFields()
727 {
728 $fields = $this->getFields();
729 $exportableFields = array();
730 foreach ($fields as $field) {
731 if ($field->getExportable()) {
732 $exportableFields[] = $field;
733 }
734 }
735
736 return $exportableFields;
737 }
738
739
746 public function hasPermissionToEditRecord($ref_id, ilDclBaseRecordModel $record)
747 {
748 if ($this->getObjId() != ilObjDataCollection::_lookupObjectId($ref_id)) {
749 return false;
750 }
752 return true;
753 }
755 return false;
756 }
757 if (!$this->checkLimit()) {
758 return false;
759 }
760 if ($this->getEditPerm() && !$this->getEditByOwner()) {
761 return true;
762 }
763 if ($this->getEditByOwner()) {
764 return $this->doesRecordBelongToUser($record);
765 }
766
767 return false;
768 }
769
770
777 public function hasPermissionToDeleteRecord($ref_id, ilDclBaseRecordModel $record)
778 {
779 if ($this->getObjId() != ilObjDataCollection::_lookupObjectId($ref_id)) {
780 return false;
781 }
783 return true;
784 }
786 return false;
787 }
788 if (!$this->checkLimit()) {
789 return false;
790 }
791 if ($this->getDeletePerm() && !$this->getDeleteByOwner()) {
792 return true;
793 }
794 if ($this->getDeleteByOwner()) {
795 return $this->doesRecordBelongToUser($record);
796 }
797
798 return false;
799 }
800
801
807 public function hasPermissionToDeleteRecords($ref_id)
808 {
809 if ($this->getObjId() != ilObjDataCollection::_lookupObjectId($ref_id)) {
810 return false;
811 }
812
815 }
816
817
825 public function hasPermissionToViewRecord($ref_id, $record, $user_id = 0)
826 {
827 global $DIC;
828 $ilUser = $DIC['ilUser'];
829 if ($this->getObjId() != ilObjDataCollection::_lookupObjectId($ref_id)) {
830 return false;
831 }
832 if (ilObjDataCollectionAccess::hasWriteAccess($ref_id, $user_id) || ilObjDataCollectionAccess::hasEditAccess($ref_id, $user_id)) {
833 return true;
834 }
836 // Check for view only own entries setting
837 if ($this->getViewOwnRecordsPerm() && ($user_id ? $user_id : $ilUser->getId()) != $record->getOwner()) {
838 return false;
839 }
840
841 return true;
842 }
843
844 return false;
845 }
846
847
854 {
855 global $DIC;
856 $ilUser = $DIC['ilUser'];
857
858 return ($ilUser->getId() == $record->getOwner());
859 }
860
861
865 public function checkLimit()
866 {
867 if ($this->getLimited()) {
868 $now = new ilDateTime(date("Y-m-d H:i:s"), IL_CAL_DATE);
869 $from = new ilDateTime($this->getLimitStart(), IL_CAL_DATE);
870 $to = new ilDateTime($this->getLimitEnd(), IL_CAL_DATE);
871
872 return ($from <= $now && $now <= $to);
873 }
874
875 return true;
876 }
877
878
882 public function updateFields()
883 {
884 foreach ($this->getFields() as $field) {
885 $field->doUpdate();
886 }
887 }
888
889
895 public function sortFields(&$fields)
896 {
897 $this->sortByOrder($fields);
898 //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.
899 $named = array();
900 foreach ($fields as $field) {
901 $named[$field->getId()] = $field;
902 }
903
904 $fields = $named;
905 }
906
907
912 protected function sortByOrder(&$array)
913 {
914 // php-bug: https://bugs.php.net/bug.php?id=50688
915 // fixed in php 7 but for now we need the @ a workaround
916 @usort($array, array($this, "compareOrder"));
917 }
918
919
924 public function buildOrderFields()
925 {
926 $fields = $this->getFields();
927 $this->sortByOrder($fields);
928 $count = 10;
929 $offset = 10;
930 foreach ($fields as $field) {
931 if (!is_null($field->getOrder())) {
932 $field->setOrder($count);
933 $count = $count + $offset;
934 $field->doUpdate();
935 }
936 }
937 }
938
939
947 public function getFieldByTitle($title)
948 {
949 $return = null;
950 foreach ($this->getFields() as $field) {
951 if ($field->getTitle() == $title) {
952 $return = $field;
953 break;
954 }
955 }
956
957 return $return;
958 }
959
960
964 public function setAddPerm($add_perm)
965 {
966 $this->add_perm = $add_perm;
967 }
968
969
973 public function getAddPerm()
974 {
975 return (bool) $this->add_perm;
976 }
977
978
983 {
984 $this->delete_perm = $delete_perm;
985 if (!$delete_perm) {
986 $this->setDeleteByOwner(false);
987 }
988 }
989
990
994 public function getDeletePerm()
995 {
996 return (bool) $this->delete_perm;
997 }
998
999
1004 {
1005 $this->edit_by_owner = $edit_by_owner;
1006 if ($edit_by_owner) {
1007 $this->setEditPerm(true);
1008 }
1009 }
1010
1011
1015 public function getEditByOwner()
1016 {
1017 return (bool) $this->edit_by_owner;
1018 }
1019
1020
1024 public function getDeleteByOwner()
1025 {
1026 return (bool) $this->delete_by_owner;
1027 }
1028
1029
1034 {
1035 $this->delete_by_owner = $delete_by_owner;
1036 if ($delete_by_owner) {
1037 $this->setDeletePerm(true);
1038 }
1039 }
1040
1041
1045 public function setEditPerm($edit_perm)
1046 {
1047 $this->edit_perm = $edit_perm;
1048 if (!$edit_perm) {
1049 $this->setEditByOwner(false);
1050 }
1051 }
1052
1053
1057 public function getEditPerm()
1058 {
1059 return (bool) $this->edit_perm;
1060 }
1061
1062
1066 public function setLimited($limited)
1067 {
1068 $this->limited = $limited;
1069 }
1070
1071
1075 public function getLimited()
1076 {
1077 return $this->limited;
1078 }
1079
1080
1084 public function setLimitEnd($limit_end)
1085 {
1086 $this->limit_end = $limit_end;
1087 }
1088
1089
1093 public function getLimitEnd()
1094 {
1095 return $this->limit_end;
1096 }
1097
1098
1103 {
1104 $this->limit_start = $limit_start;
1105 }
1106
1107
1111 public function getLimitStart()
1112 {
1113 return $this->limit_start;
1114 }
1115
1116
1120 public function setIsVisible($is_visible)
1121 {
1122 $this->is_visible = $is_visible;
1123 }
1124
1125
1129 public function getIsVisible()
1130 {
1131 return $this->is_visible;
1132 }
1133
1134
1139 {
1140 $this->description = $description;
1141 }
1142
1143
1147 public function getDescription()
1148 {
1149 return $this->description;
1150 }
1151
1152
1159 {
1160 $default_sort_field = ($default_sort_field) ? $default_sort_field : 0; // Change null or empty strings to zero
1161 $this->default_sort_field = $default_sort_field;
1162 }
1163
1164
1168 public function getDefaultSortField()
1169 {
1171 }
1172
1173
1178 {
1179 if (!in_array($default_sort_field_order, array('asc', 'desc'))) {
1181 }
1182 $this->default_sort_field_order = $default_sort_field_order;
1183 }
1184
1185
1190 {
1192 }
1193
1194
1199 {
1200 $this->public_comments = $public_comments;
1201 }
1202
1203
1208 {
1210 }
1211
1212
1216 public function setViewOwnRecordsPerm($view_own_perm)
1217 {
1218 $this->view_own_records_perm = (int) $view_own_perm;
1219 }
1220
1221
1225 public function getViewOwnRecordsPerm()
1226 {
1227 return (bool) $this->view_own_records_perm;
1228 }
1229
1230
1234 public function getSaveConfirmation()
1235 {
1237 }
1238
1239
1244 {
1245 $this->save_confirmation = $save_confirmation;
1246 }
1247
1248
1254 public function hasCustomFields()
1255 {
1256 $this->loadCustomFields();
1257
1258 return (count($this->fields) > 0) ? true : false;
1259 }
1260
1261
1268 public function compareOrder($a, $b)
1269 {
1270 if (is_null($a->getOrder() == null) && is_null($b->getOrder() == null)) {
1271 return 0;
1272 }
1273 if (is_null($a->getOrder())) {
1274 return 1;
1275 }
1276 if (is_null($b->getOrder())) {
1277 return -1;
1278 }
1279
1280 return $a->getOrder() < $b->getOrder() ? -1 : 1;
1281 }
1282
1283
1287 public function cloneStructure(ilDclTable $original)
1288 {
1289 $this->setTitle($original->getTitle());
1290 $this->setDescription($original->getDescription());
1291 $this->setIsVisible($original->getIsVisible());
1292 $this->setEditByOwner($original->getEditByOwner());
1293 $this->setAddPerm($original->getAddPerm());
1294 $this->setEditPerm($original->getEditPerm());
1295 $this->setDeleteByOwner($original->getDeleteByOwner());
1296 $this->setSaveConfirmation($original->getSaveConfirmation());
1297 $this->setDeletePerm($original->getDeletePerm());
1298 $this->setLimited($original->getLimited());
1299 $this->setLimitStart($original->getLimitStart());
1300 $this->setLimitEnd($original->getLimitEnd());
1301 $this->setViewOwnRecordsPerm($original->getViewOwnRecordsPerm());
1302 $this->setExportEnabled($original->getExportEnabled());
1303 $this->setImportEnabled($original->getImportEnabled());
1306 $this->setOrder($original->getOrder());
1307
1308 $this->doCreate(true, false);
1309 // reset stdFields to get new for the created object
1310
1312 // Clone standard-fields
1313 $org_std_fields = $original->getStandardFields();
1314 foreach ($this->getStandardFields() as $element_key => $std_field) {
1315 $std_field->cloneStructure($org_std_fields[$element_key]);
1316 if ($std_field->getId() === $original->getDefaultSortField()) {
1317 $default_sort_field = $std_field->getId();
1318 }
1319 }
1320
1321 // Clone fields
1322 $new_fields = array();
1323 foreach ($original->getFields() as $orig_field) {
1324 if (!$orig_field->isStandardField()) {
1325 $class_name = get_class($orig_field);
1326 $new_field = new $class_name();
1327 $new_field->setTableId($this->getId());
1328 $new_field->cloneStructure($orig_field->getId());
1329 $new_fields[$orig_field->getId()] = $new_field;
1330
1331 if ($orig_field->getId() === $original->getDefaultSortField()) {
1332 $default_sort_field = $new_field->getId();
1333 }
1334 }
1335 }
1336
1338 $this->doUpdate();
1339
1340 // Clone Records with recordfields
1341 foreach ($original->getRecords() as $orig_record) {
1342 $new_record = new ilDclBaseRecordModel();
1343 $new_record->setTableId($this->getId());
1344 $new_record->cloneStructure($orig_record->getId(), $new_fields);
1345 }
1346
1347 //clone tableviews (includes pageobjects)
1348 foreach ($original->getTableViews() as $orig_tableview) {
1349 $new_tableview = new ilDclTableView();
1350 $new_tableview->setTableId($this->getId());
1351 $new_tableview->cloneStructure($orig_tableview, $new_fields);
1352 }
1353
1354 // mandatory for all cloning functions
1355 ilDclCache::setCloneOf($original->getId(), $this->getId(), ilDclCache::TYPE_TABLE);
1356 }
1357
1358
1362 public function afterClone()
1363 {
1364 foreach ($this->getFields() as $field) {
1365 $field->afterClone($this->getRecords());
1366 }
1367 }
1368
1369
1375 public function _hasRecords()
1376 {
1377 return (count($this->getRecords()) > 0) ? true : false;
1378 }
1379
1380
1384 public function addField($field)
1385 {
1386 $this->all_fields[$field->getId()] = $field;
1387 }
1388
1389
1395 public static function _tableExists($table_id)
1396 {
1397 global $DIC;
1398 $ilDB = $DIC['ilDB'];
1399 $query = "SELECT * FROM il_dcl_table WHERE id = " . $table_id;
1400 $result = $ilDB->query($query);
1401
1402 return $result->numRows() != 0;
1403 }
1404
1405
1412 public static function _getTableIdByTitle($title, $obj_id)
1413 {
1414 global $DIC;
1415 $ilDB = $DIC['ilDB'];
1416 $result = $ilDB->query(
1417 'SELECT id FROM il_dcl_table WHERE title = ' . $ilDB->quote($title, 'text') . ' AND obj_id = '
1418 . $ilDB->quote($obj_id, 'integer')
1419 );
1420 $id = 0;
1421 while ($rec = $ilDB->fetchAssoc($result)) {
1422 $id = $rec['id'];
1423 }
1424
1425 return $id;
1426 }
1427
1428
1433 {
1434 $this->export_enabled = $export_enabled;
1435 }
1436
1437
1441 public function getExportEnabled()
1442 {
1443 return $this->export_enabled;
1444 }
1445
1446
1450 public function getOrder()
1451 {
1452 if (!$this->table_order) {
1453 $this->updateOrder();
1454 }
1455
1456 return $this->table_order;
1457 }
1458
1459
1463 public function updateOrder()
1464 {
1465 global $DIC;
1466 $ilDB = $DIC['ilDB'];
1467 $result = $ilDB->query('SELECT MAX(table_order) AS table_order FROM il_dcl_table WHERE obj_id = ' . $ilDB->quote($this->getCollectionObject()->getId(), 'integer'));
1468 $this->table_order = $ilDB->fetchObject($result)->table_order + 10;
1469 $ilDB->query('UPDATE il_dcl_table SET table_order = ' . $ilDB->quote($this->table_order, 'integer') . ' WHERE id = ' . $ilDB->quote($this->getId(), 'integer'));
1470 }
1471
1472
1476 public function setOrder($table_order)
1477 {
1478 $this->table_order = $table_order;
1479 }
1480
1481
1486 {
1487 $this->import_enabled = $import_enabled;
1488 }
1489
1490
1494 public function getImportEnabled()
1495 {
1496 return $this->import_enabled;
1497 }
1498
1499
1508 public static function _hasFieldByTitle($title, $obj_id)
1509 {
1510 global $DIC;
1511 $ilDB = $DIC['ilDB'];
1512 $result = $ilDB->query(
1513 'SELECT * FROM il_dcl_field WHERE table_id = ' . $ilDB->quote($obj_id, 'integer') . ' AND title = '
1514 . $ilDB->quote($title, 'text')
1515 );
1516
1517 return ($ilDB->numRows($result)) ? true : false;
1518 }
1519
1520
1532 public function getPartialRecords($sort, $direction, $limit, $offset, array $filter = array())
1533 {
1534 global $DIC;
1535 $ilDB = $DIC['ilDB'];
1539 $ilUser = $DIC['ilUser'];
1540 $rbacreview = $DIC['rbacreview'];
1541
1542 $sort_field = ($sort) ? $this->getFieldByTitle($sort) : $this->getField('id');
1543 $direction = strtolower($direction);
1544 $direction = (in_array($direction, array('desc', 'asc'))) ? $direction : 'asc';
1545
1546 // Sorting by a status from an ILIAS Ref field. This column is added dynamically to the table, there is no field model
1547 $sort_by_status = false;
1548 if (substr($sort, 0, 8) == '_status_') {
1549 $sort_by_status = true;
1550 $sort_field = $this->getFieldByTitle(substr($sort, 8));
1551 }
1552
1553 if (is_null($sort_field)) {
1554 $sort_field = $this->getField('id');
1555 }
1556
1557 $sort_query_object = $sort_field->getRecordQuerySortObject($direction, $sort_by_status);
1558
1559 $select_str = ($sort_query_object != null) ? $sort_query_object->getSelectStatement() : '';
1560 $join_str = ($sort_query_object != null) ? $sort_query_object->getJoinStatement() : '';
1561 $where_str = ($sort_query_object != null) ? $sort_query_object->getWhereStatement() : '';
1562 $order_str = ($sort_query_object != null) ? $sort_query_object->getOrderStatement() : '';
1563 $group_str = ($sort_query_object != null) ? $sort_query_object->getGroupStatement() : '';
1564
1565 if (count($filter)) {
1566 foreach ($filter as $key => $filter_value) {
1567 $filter_field_id = substr($key, 7);
1568 $filter_field = $this->getField($filter_field_id);
1569 $filter_record_query_object = $filter_field->getRecordQueryFilterObject($filter_value, $sort_field);
1570
1571 if ($filter_record_query_object) {
1572 $select_str .= $filter_record_query_object->getSelectStatement();
1573 $join_str .= $filter_record_query_object->getJoinStatement();
1574 $where_str .= $filter_record_query_object->getWhereStatement();
1575 $group_str .= $filter_record_query_object->getGroupStatement();
1576 }
1577 }
1578 }
1579
1580 // Build the query string
1581 $sql = "SELECT DISTINCT record.id, record.owner";
1582 if ($select_str) {
1583 $sql .= ', ';
1584 }
1585
1586 $as = ' AS ';
1587
1588 $sql .= rtrim($select_str, ',') . " FROM il_dcl_record {$as} record ";
1589 $sql .= $join_str;
1590 $sql .= " WHERE record.table_id = " . $ilDB->quote($this->getId(), 'integer');
1591
1592 if (strlen($where_str) > 0) {
1593 $sql .= $where_str;
1594 }
1595
1596 if (strlen($group_str) > 0) {
1597 $sql .= " GROUP BY " . $group_str;
1598 }
1599
1600 if (strlen($order_str) > 0) {
1601 $sql .= " ORDER BY " . $order_str;
1602 }
1603
1604 //var_dump($sql);
1605 /*global $DIC;
1606 /*$ilLog = $DIC['ilLog'];
1607 $ilLog->write($sql, ilLogLevel::CRITICAL);*/
1608
1609 $set = $ilDB->query($sql);
1610 $total_record_ids = array();
1611 // Save record-ids in session to enable prev/next links in detail view
1612 $_SESSION['dcl_record_ids'] = array();
1613 $_SESSION['dcl_table_id'] = $this->getId();
1614 $ref = filter_input(INPUT_GET, 'ref_id');
1616 while ($rec = $ilDB->fetchAssoc($set)) {
1617 // Quick check if the current user is allowed to view the record
1618 if (!$is_allowed_to_view && ($this->getViewOwnRecordsPerm() && $ilUser->getId() != $rec['owner'])) {
1619 continue;
1620 }
1621 $total_record_ids[] = $rec['id'];
1622 $_SESSION['dcl_record_ids'][] = $rec['id'];
1623 }
1624
1625 if ($sort_query_object != null) {
1626 $total_record_ids = $sort_query_object->applyCustomSorting($sort_field, $total_record_ids, $direction);
1627 }
1628
1629 // Now slice the array to load only the needed records in memory
1630 $record_ids = array_slice($total_record_ids, $offset, $limit);
1631
1632 $records = array();
1633 foreach ($record_ids as $id) {
1635 }
1636
1637 return array('records' => $records, 'total' => count($total_record_ids));
1638 }
1639}
$result
$_SESSION["AccountId"]
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_DATE
@classDescription Date and time handling
Class ilDclBaseRecordModel.
static getRecordCache($record_id=0)
static buildFieldFromRecord($rec)
static setCloneOf($old, $new, $type)
static getFieldCache($field_id=0)
static preloadFieldProperties(array $fields)
Preloads field properties.
static _getStandardFields($table_id)
Class ilDclTableView.
static getCountForTableId($table_id)
static createOrGetStandardView($table_id, $create_default_settings=true)
static getAllForTableId($table_id)
Class ilDclBaseFieldModel.
updateFields()
Update fields.
setViewOwnRecordsPerm($view_own_perm)
_hasRecords()
_hasRecords
getRecordFields()
Returns all fields of this table which are NOT standard fields.
setId($a_id)
Set table id.
buildOrderFields()
buildOrderFields orders the fields.
sortFields(&$fields)
sortFields
hasPermissionToDeleteRecord($ref_id, ilDclBaseRecordModel $record)
setPublicCommentsEnabled($public_comments)
setDefaultSortField($default_sort_field)
/**
hasPermissionToViewRecord($ref_id, $record, $user_id=0)
doesRecordBelongToUser(ilDclBaseRecordModel $record)
getField($field_id)
getExportableFields()
Return all the fields that are marked as exportable.
setLimited($limited)
getFirstTableViewId($ref_id, $user_id=0)
get id of first (for current user) available view
hasPermissionToEditRecord($ref_id, ilDclBaseRecordModel $record)
setEditByOwner($edit_by_owner)
getVisibleTableViews($ref_id, $with_active_detailedview=false, $user_id=0)
For current user.
hasCustomFields()
hasCustomFields
sortTableViews(array $tableviews=null)
setDeleteByOwner($delete_by_owner)
setDefaultSortFieldOrder($default_sort_field_order)
setSaveConfirmation($save_confirmation)
setImportEnabled($import_enabled)
setExportEnabled($export_enabled)
getNewFieldOrder()
getNewOrder
deleteField($field_id)
cloneStructure(ilDclTable $original)
static _getTableIdByTitle($title, $obj_id)
static _tableExists($table_id)
getFieldByTitle($title)
Get a field by title.
setLimitEnd($limit_end)
setOrder($table_order)
setDescription($description)
sortByOrder(&$array)
__construct($a_id=0)
getFields()
Returns all fields of this table including the standard fields.
compareOrder($a, $b)
setEditPerm($edit_perm)
setDeletePerm($delete_perm)
doCreate($create_tablefield_setting=true, $create_standardview=true)
doRead()
Read table.
getId()
Get table id.
setAddPerm($add_perm)
hasPermissionToDeleteRecords($ref_id)
setTitle($a_title)
static _hasFieldByTitle($title, $obj_id)
Checks if a table has a field with the given title.
setIsVisible($is_visible)
setLimitStart($limit_start)
static hasAccessToTableView($tableview, $user_id=0)
This only checks access to the tableview - if the full access check is required, use hasAccessTo($ref...
static hasEditAccess($ref, $user_id=0)
Has permission to view and edit all entries event when he is not the owner.
Class ilObjDataCollection.
static _lookupObjectId($a_ref_id)
lookup object id
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$query
global $ilDB
$errors fields
Definition: imgupload.php:51
$ilUser
Definition: imgupload.php:18
$DIC
Definition: xapitoken.php:46