ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
5 include_once './Modules/DataCollection/classes/Fields/Base/class.ilDclStandardField.php';
6 include_once './Modules/DataCollection/classes/Fields/Base/class.ilDclBaseRecordModel.php';
7 include_once './Modules/DataCollection/classes/TableView/class.ilDclTableView.php';
8 
21 class ilDclTable {
22 
26  protected $id = 0;
30  protected $objId;
34  protected $obj;
38  protected $title;
42  protected $fields;
46  protected $stdFields;
50  protected $records;
54  protected $is_visible;
58  protected $add_perm;
62  protected $edit_perm;
66  protected $delete_perm;
70  protected $edit_by_owner;
74  protected $delete_by_owner;
78  protected $save_confirmation;
82  protected $limited;
86  protected $limit_start;
90  protected $limit_end;
94  protected $export_enabled;
98  protected $table_order;
102  protected $import_enabled;
108  protected $default_sort_field = 0;
114  protected $default_sort_field_order = 'asc';
120  protected $description = '';
126  protected $public_comments = 0;
132  protected $view_own_records_perm = 0;
133 
138  protected $all_fields = null;
139 
140 
144  public function __construct($a_id = 0) {
145  if ($a_id != 0) {
146  $this->id = $a_id;
147  $this->doRead();
148  }
149  }
150 
151 
155  public function doRead() {
156  global $DIC;
157  $ilDB = $DIC['ilDB'];
158 
159  $query = "SELECT * FROM il_dcl_table WHERE id = " . $ilDB->quote($this->getId(), "integer");
160  $set = $ilDB->query($query);
161  $rec = $ilDB->fetchAssoc($set);
162 
163  $this->setObjId($rec["obj_id"]);
164  $this->setTitle($rec["title"]);
165  $this->setAddPerm($rec["add_perm"]);
166  $this->setEditPerm($rec["edit_perm"]);
167  $this->setDeletePerm($rec["delete_perm"]);
168  $this->setEditByOwner($rec["edit_by_owner"]);
169  $this->setExportEnabled($rec["export_enabled"]);
170  $this->setImportEnabled($rec["import_enabled"]);
171  $this->setLimited($rec["limited"]);
172  $this->setLimitStart($rec["limit_start"]);
173  $this->setLimitEnd($rec["limit_end"]);
174  $this->setIsVisible($rec["is_visible"]);
175  $this->setDescription($rec['description']);
176  $this->setDefaultSortField($rec['default_sort_field_id']);
177  $this->setDefaultSortFieldOrder($rec['default_sort_field_order']);
178  $this->setPublicCommentsEnabled($rec['public_comments']);
179  $this->setViewOwnRecordsPerm($rec['view_own_records_perm']);
180  $this->setDeleteByOwner($rec['delete_by_owner']);
181  $this->setSaveConfirmation($rec['save_confirmation']);
182  $this->setOrder($rec['table_order']);
183  }
184 
185 
193  public function doDelete($delete_only_content = false, $omit_notification = false) {
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  global $DIC;
228  $ilDB = $DIC['ilDB'];
229 
230  $id = $ilDB->nextId("il_dcl_table");
231  $this->setId($id);
232  $query = "INSERT INTO il_dcl_table (" . "id" . ", obj_id" . ", title" . ", add_perm" . ", edit_perm" . ", delete_perm" . ", edit_by_owner"
233  . ", limited" . ", limit_start" . ", limit_end" . ", is_visible" . ", export_enabled" . ", import_enabled" . ", default_sort_field_id"
234  . ", default_sort_field_order" . ", description" . ", public_comments" . ", view_own_records_perm"
235  . ", delete_by_owner, save_confirmation , table_order ) VALUES (" . $ilDB->quote($this->getId(), "integer") . ","
236  . $ilDB->quote($this->getObjId(), "integer") . "," . $ilDB->quote($this->getTitle(), "text") . ","
237  . $ilDB->quote($this->getAddPerm() ? 1 : 0, "integer") . "," . $ilDB->quote($this->getEditPerm() ? 1 : 0, "integer") . ","
238  . $ilDB->quote($this->getDeletePerm() ? 1 : 0, "integer") . "," . $ilDB->quote($this->getEditByOwner() ? 1 : 0, "integer") . ","
239  . $ilDB->quote($this->getLimited() ? 1 : 0, "integer") . "," . $ilDB->quote($this->getLimitStart(), "timestamp") . ","
240  . $ilDB->quote($this->getLimitEnd(), "timestamp") . "," . $ilDB->quote($this->getIsVisible() ? 1 : 0, "integer") . ","
241  . $ilDB->quote($this->getExportEnabled() ? 1 : 0, "integer") . "," . $ilDB->quote($this->getImportEnabled() ? 1 : 0, "integer") . ","
242  . $ilDB->quote($this->getDefaultSortField(), "text") . "," . $ilDB->quote($this->getDefaultSortFieldOrder(), "text") . ","
243  . $ilDB->quote($this->getDescription(), "text") . "," . $ilDB->quote($this->getPublicCommentsEnabled(), "integer") . ","
244  . $ilDB->quote($this->getViewOwnRecordsPerm(), "integer") . "," . $ilDB->quote($this->getDeleteByOwner() ? 1 : 0, 'integer') . ","
245  . $ilDB->quote($this->getSaveConfirmation() ? 1 : 0, 'integer') . "," . $ilDB->quote($this->getOrder(), 'integer') . ")";
246 
247  $ilDB->manipulate($query);
248 
249  if ($create_standardview) {
250  //standard tableview
252  }
253 
254  if ($create_tablefield_setting) {
255  $this->buildOrderFields();
256  }
257  }
258 
259  /*
260  * doUpdate
261  */
262  public function doUpdate() {
263  global $DIC;
264  $ilDB = $DIC['ilDB'];
265 
266  $ilDB->update("il_dcl_table", array(
267  "obj_id" => array( "integer", $this->getObjId() ),
268  "title" => array( "text", $this->getTitle() ),
269  "add_perm" => array( "integer", (int)$this->getAddPerm() ),
270  "edit_perm" => array( "integer", (int)$this->getEditPerm() ),
271  "delete_perm" => array( "integer", (int)$this->getDeletePerm() ),
272  "edit_by_owner" => array( "integer", (int)$this->getEditByOwner() ),
273  "limited" => array( "integer", $this->getLimited() ),
274  "limit_start" => array( "timestamp", $this->getLimitStart() ),
275  "limit_end" => array( "timestamp", $this->getLimitEnd() ),
276  "is_visible" => array( "integer", $this->getIsVisible() ? 1 : 0 ),
277  "export_enabled" => array( "integer", $this->getExportEnabled() ? 1 : 0 ),
278  "import_enabled" => array( "integer", $this->getImportEnabled() ? 1 : 0 ),
279  "description" => array( "text", $this->getDescription() ),
280  "default_sort_field_id" => array( "text", $this->getDefaultSortField() ),
281  "default_sort_field_order" => array( "text", $this->getDefaultSortFieldOrder() ),
282  "public_comments" => array( "integer", $this->getPublicCommentsEnabled() ? 1 : 0 ),
283  "view_own_records_perm" => array( "integer", $this->getViewOwnRecordsPerm() ),
284  'delete_by_owner' => array( 'integer', $this->getDeleteByOwner() ? 1 : 0 ),
285  'save_confirmation' => array( 'integer', $this->getSaveConfirmation() ? 1 : 0 ),
286  'table_order' => array( 'integer', $this->getOrder() ),
287  ), array(
288  "id" => array( "integer", $this->getId() )
289  ));
290  }
291 
292 
298  public function setId($a_id) {
299  $this->id = $a_id;
300  }
301 
302 
308  public function getId() {
309  return $this->id;
310  }
311 
312 
316  public function setObjId($a_id) {
317  $this->objId = $a_id;
318  }
319 
320 
324  public function getObjId() {
325  return $this->objId;
326  }
327 
328 
332  public function setTitle($a_title) {
333  $this->title = $a_title;
334  }
335 
336 
340  public function getTitle() {
341  return $this->title;
342  }
343 
344 
348  public function getCollectionObject() {
349  $this->loadObj();
350 
351  return $this->obj;
352  }
353 
354 
355  protected function loadObj() {
356  if ($this->obj == NULL) {
357  $this->obj = new ilObjDataCollection($this->objId, false);
358  }
359  }
360 
361 
365  public function getRecords() {
366  if ($this->records == NULL) {
367  $this->loadRecords();
368  }
369 
370  return $this->records;
371  }
372 
373  public function loadRecords() {
374  global $DIC;
375  $ilDB = $DIC['ilDB'];
376 
377  $records = array();
378  $query = "SELECT id FROM il_dcl_record WHERE table_id = " . $ilDB->quote($this->id, "integer");
379  $set = $ilDB->query($query);
380 
381  while ($rec = $ilDB->fetchAssoc($set)) {
382  $records[$rec['id']] = ilDclCache::getRecordCache($rec['id']);
383  }
384 
385  $this->records = $records;
386  }
387 
391  public function deleteField($field_id) {
392  $field = ilDclCache::getFieldCache($field_id);
393  $records = $this->getRecords();
394 
395  foreach ($records as $record) {
396  $record->deleteField($field_id);
397  }
398 
399  $field->doDelete();
400  }
401 
402 
408  public function getField($field_id) {
409  $fields = $this->getFields();
410  $field = NULL;
411  foreach ($fields as $field_1) {
412  if ($field_1->getId() == $field_id) {
413  $field = $field_1;
414  }
415  }
416 
417  return $field;
418  }
419 
424  public function getFieldIds() {
425  $field_ids = array();
426  foreach ($this->getFields() as $field)
427  {
428  if ($field->getId())
429  {
430  $field_ids[] = $field->getId();
431  }
432  }
433  return $field_ids;
434  }
435 
436 
437  protected function loadCustomFields() {
438  if (!$this->fields) {
439  global $DIC;
440  $ilDB = $DIC['ilDB'];
444  $query = "SELECT DISTINCT il_dcl_field.*, il_dcl_tfield_set.field_order
445  FROM il_dcl_field
446  INNER JOIN il_dcl_tfield_set
447  ON ( il_dcl_tfield_set.field NOT IN ('owner',
448  'last_update',
449  'last_edit_by',
450  'id',
451  'create_date')
452  AND il_dcl_tfield_set.table_id = il_dcl_field.table_id
453  AND il_dcl_tfield_set.field = ".$ilDB->cast("il_dcl_field.id","text").")
454  WHERE il_dcl_field.table_id = %s
455  ORDER BY il_dcl_tfield_set.field_order ASC";
456 
457  $set = $ilDB->queryF($query, array('integer'), array((int)$this->getId()));
458  $fields = array();
459  while ($rec = $ilDB->fetchAssoc($set)) {
460  $field = ilDclCache::buildFieldFromRecord($rec);
461  $fields[] = $field;
462  }
463  $this->fields = $fields;
464 
466  }
467  }
468 
469  public function getCustomFields() {
470  if (!$this->fields) {
471  $this->loadCustomFields();
472  }
473  return $this->fields;
474  }
475 
476 
482  public function getNewFieldOrder() {
483  $fields = $this->getFields();
484  $place = 0;
485  foreach ($fields as $field) {
486  if (!$field->isStandardField()) {
487  $place = $field->getOrder() + 1;
488  }
489  }
490 
491  return $place;
492  }
493 
497  public function getNewTableviewOrder()
498  {
499  return (ilDclTableView::getCountForTableId($this->getId()) + 1) * 10;
500  }
501 
505  public function sortTableViews(array $tableviews = null)
506  {
507  if ($tableviews == null)
508  {
509  $tableviews = $this->getTableViews();
510  }
511 
512  $order = 10;
513  foreach($tableviews as $tableview)
514  {
515  $tableview->setTableviewOrder($order);
516  $tableview->update();
517  $order += 10;
518  }
519 
520  }
521 
522 
529  public function getFields() {
530  if($this->all_fields == null) {
531  $this->reloadFields();
532  }
533 
534  return $this->all_fields;
535  }
536 
537  public function reloadFields() {
538  $this->loadCustomFields();
539  $this->stdFields = $this->getStandardFields();
540  $fields = array_merge($this->fields, $this->stdFields);
541 
542  $this->sortByOrder($fields);
543 
544  $this->all_fields = $fields;
545  }
546 
550  public function getTableViews() {
551  return ilDclTableView::getAllForTableId($this->getId());
552  }
553 
561  public function getVisibleTableViews($ref_id, $with_active_detailedview = false, $user_id = 0) {
562  if (ilObjDataCollectionAccess::hasWriteAccess($ref_id, $user_id) && !$with_active_detailedview)
563  {
564  return $this->getTableViews();
565  }
566 
567  $visible_views = array();
568  foreach ($this->getTableViews() as $tableView)
569  {
570  if (ilObjDataCollectionAccess::hasAccessToTableView($tableView, $user_id))
571  {
572  if (!$with_active_detailedview || ilDclDetailedViewDefinition::isActive($tableView->getId()))
573  {
574  $visible_views[] = $tableView;
575  }
576  }
577  }
578  return $visible_views;
579  }
580 
588  public function getFirstTableViewId($ref_id, $user_id = 0) {
589  $tableview = array_shift($this->getVisibleTableViews($ref_id, false, $user_id));
590  return $tableview ? $tableview->getId() : false;
591  }
592 
598  public function getFieldsForFormula() {
599  $unsupported = array(
608  );
609 
610  $this->loadCustomFields();
611  $return = $this->getStandardFields();
615  foreach ($this->fields as $field) {
616  if (!in_array($field->getDatatypeId(), $unsupported)) {
617  $return[] = $field;
618  }
619  }
620 
621  return $return;
622  }
623 
624 
631  public function getStandardFields() {
632  if ($this->stdFields == NULL) {
633  $this->stdFields = ilDclStandardField::_getStandardFields($this->id);
634  // Don't return comments as field if this feature is not activated in the settings
635  if (!$this->getPublicCommentsEnabled()) {
637  foreach ($this->stdFields as $k => $field) {
638  if ($field->getId() == 'comments') {
639  unset($this->stdFields[$k]);
640  break;
641  }
642  }
643  }
644  }
645 
646  return $this->stdFields;
647  }
648 
649 
655  public function getRecordFields() {
656  $this->loadCustomFields();
657 
658  return $this->fields;
659  }
660 
664  public function getEditableFields() {
665  $fields = $this->getRecordFields();
666  $editableFields = array();
667 
668  foreach ($fields as $field) {
669  if (!$field->getLocked()) {
670  $editableFields[] = $field;
671  }
672  }
673 
674  return $editableFields;
675  }
676 
682  public function getExportableFields() {
683  $fields = $this->getFields();
684  $exportableFields = array();
685  foreach ($fields as $field) {
686  if ($field->getExportable()) {
687  $exportableFields[] = $field;
688  }
689  }
690 
691  return $exportableFields;
692  }
693 
694 
703  return false;
704  }
706  return true;
707  }
709  return false;
710  }
711  if (!$this->checkLimit()) {
712  return false;
713  }
714  if ($this->getEditPerm() && !$this->getEditByOwner()) {
715  return true;
716  }
717  if ($this->getEditByOwner()) {
718  return $this->doesRecordBelongToUser($record);
719  }
720 
721  return false;
722  }
723 
724 
733  return false;
734  }
736  return true;
737  }
739  return false;
740  }
741  if (!$this->checkLimit()) {
742  return false;
743  }
744  if ($this->getDeletePerm() && !$this->getDeleteByOwner()) {
745  return true;
746  }
747  if ($this->getDeleteByOwner()) {
748  return $this->doesRecordBelongToUser($record);
749  }
750 
751  return false;
752  }
753 
754 
762  return false;
763  }
766  }
767 
768 
776  public function hasPermissionToViewRecord($ref_id, $record, $user_id = 0) {
777  global $DIC;
778  $ilUser = $DIC['ilUser'];
780  return false;
781  }
783  return true;
784  }
786  // Check for view only own entries setting
787  if ($this->getViewOwnRecordsPerm() && ($user_id ? $user_id : $ilUser->getId()) != $record->getOwner()) {
788  return false;
789  }
790 
791  return true;
792  }
793 
794  return false;
795  }
796 
797 
803  protected function doesRecordBelongToUser(ilDclBaseRecordModel $record) {
804  global $DIC;
805  $ilUser = $DIC['ilUser'];
806 
807  return ($ilUser->getId() == $record->getOwner());
808  }
809 
810 
814  public function checkLimit() {
815  if ($this->getLimited()) {
816  $now = new ilDateTime(date("Y-m-d H:i:s"), IL_CAL_DATE);
817  $from = new ilDateTime($this->getLimitStart(), IL_CAL_DATE);
818  $to = new ilDateTime($this->getLimitEnd(), IL_CAL_DATE);
819 
820  return ($from <= $now && $now <= $to);
821  }
822 
823  return true;
824  }
825 
826 
830  public function updateFields() {
831  foreach ($this->getFields() as $field) {
832  $field->doUpdate();
833  }
834  }
835 
841  public function sortFields(&$fields) {
842  $this->sortByOrder($fields);
843  //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.
844  $named = array();
845  foreach ($fields as $field) {
846  $named[$field->getId()] = $field;
847  }
848 
849  $fields = $named;
850  }
851 
852 
857  protected function sortByOrder(&$array) {
858  // php-bug: https://bugs.php.net/bug.php?id=50688
859  // fixed in php 7 but for now we need the @ a workaround
860  @usort($array, array( $this, "compareOrder" ));
861  }
862 
863 
868  public function buildOrderFields() {
869  $fields = $this->getFields();
870  $this->sortByOrder($fields);
871  $count = 10;
872  $offset = 10;
873  foreach ($fields as $field) {
874  if (!is_null($field->getOrder())) {
875  $field->setOrder($count);
876  $count = $count + $offset;
877  $field->doUpdate();
878  }
879  }
880  }
881 
882 
890  public function getFieldByTitle($title) {
891  $return = NULL;
892  foreach ($this->getFields() as $field) {
893  if ($field->getTitle() == $title) {
894  $return = $field;
895  break;
896  }
897  }
898 
899  return $return;
900  }
901 
902 
906  public function setAddPerm($add_perm) {
907  $this->add_perm = $add_perm;
908  }
909 
910 
914  public function getAddPerm() {
915  return (bool)$this->add_perm;
916  }
917 
918 
922  public function setDeletePerm($delete_perm) {
923  $this->delete_perm = $delete_perm;
924  if (!$delete_perm) {
925  $this->setDeleteByOwner(false);
926  }
927  }
928 
929 
933  public function getDeletePerm() {
934 
935  return (bool)$this->delete_perm;
936  }
937 
938 
942  public function setEditByOwner($edit_by_owner) {
943  $this->edit_by_owner = $edit_by_owner;
944  if ($edit_by_owner) {
945  $this->setEditPerm(true);
946  }
947  }
948 
949 
953  public function getEditByOwner() {
954  return (bool)$this->edit_by_owner;
955  }
956 
957 
961  public function getDeleteByOwner() {
962  return (bool)$this->delete_by_owner;
963  }
964 
965 
970  $this->delete_by_owner = $delete_by_owner;
971  if ($delete_by_owner) {
972  $this->setDeletePerm(true);
973  }
974  }
975 
976 
980  public function setEditPerm($edit_perm) {
981  $this->edit_perm = $edit_perm;
982  if (!$edit_perm) {
983  $this->setEditByOwner(false);
984  }
985  }
986 
987 
991  public function getEditPerm() {
992  return (bool)$this->edit_perm;
993  }
994 
995 
999  public function setLimited($limited) {
1000  $this->limited = $limited;
1001  }
1002 
1003 
1007  public function getLimited() {
1008  return $this->limited;
1009  }
1010 
1011 
1015  public function setLimitEnd($limit_end) {
1016  $this->limit_end = $limit_end;
1017  }
1018 
1019 
1023  public function getLimitEnd() {
1024  return $this->limit_end;
1025  }
1026 
1027 
1031  public function setLimitStart($limit_start) {
1032  $this->limit_start = $limit_start;
1033  }
1034 
1035 
1039  public function getLimitStart() {
1040  return $this->limit_start;
1041  }
1042 
1043 
1047  public function setIsVisible($is_visible) {
1048  $this->is_visible = $is_visible;
1049  }
1050 
1051 
1055  public function getIsVisible() {
1056  return $this->is_visible;
1057  }
1058 
1059 
1063  public function setDescription($description) {
1064  $this->description = $description;
1065  }
1066 
1067 
1071  public function getDescription() {
1072  return $this->description;
1073  }
1074 
1075 
1082  $default_sort_field = ($default_sort_field) ? $default_sort_field : 0; // Change null or empty strings to zero
1083  $this->default_sort_field = $default_sort_field;
1084  }
1085 
1086 
1090  public function getDefaultSortField() {
1092  }
1093 
1094 
1099  if (!in_array($default_sort_field_order, array( 'asc', 'desc' ))) {
1100  $default_sort_field_order = 'asc';
1101  }
1102  $this->default_sort_field_order = $default_sort_field_order;
1103  }
1104 
1105 
1109  public function getDefaultSortFieldOrder() {
1111  }
1112 
1113 
1118  $this->public_comments = $public_comments;
1119  }
1120 
1121 
1125  public function getPublicCommentsEnabled() {
1126  return $this->public_comments;
1127  }
1128 
1129 
1133  public function setViewOwnRecordsPerm($view_own_perm) {
1134  $this->view_own_records_perm = (int)$view_own_perm;
1135  }
1136 
1137 
1141  public function getViewOwnRecordsPerm() {
1142  return (bool)$this->view_own_records_perm;
1143  }
1144 
1145 
1149  public function getSaveConfirmation() {
1150  return $this->save_confirmation;
1151  }
1152 
1153 
1158  $this->save_confirmation = $save_confirmation;
1159  }
1160 
1161 
1167  public function hasCustomFields() {
1168  $this->loadCustomFields();
1169 
1170  return (count($this->fields) > 0) ? true : false;
1171  }
1172 
1173 
1179  public function compareOrder($a, $b) {
1180  if (is_null($a->getOrder() == NULL) && is_null($b->getOrder() == NULL)) {
1181  return 0;
1182  }
1183  if (is_null($a->getOrder())) {
1184  return 1;
1185  }
1186  if (is_null($b->getOrder())) {
1187  return - 1;
1188  }
1189 
1190  return $a->getOrder() < $b->getOrder() ? - 1 : 1;
1191  }
1192 
1193 
1197  public function cloneStructure(ilDclTable $original) {
1198  $this->setTitle($original->getTitle());
1199  $this->setDescription($original->getDescription());
1200  $this->setIsVisible($original->getIsVisible());
1201  $this->setEditByOwner($original->getEditByOwner());
1202  $this->setAddPerm($original->getAddPerm());
1203  $this->setEditPerm($original->getEditPerm());
1204  $this->setDeleteByOwner($original->getDeleteByOwner());
1205  $this->setSaveConfirmation($original->getSaveConfirmation());
1206  $this->setDeletePerm($original->getDeletePerm());
1207  $this->setLimited($original->getLimited());
1208  $this->setLimitStart($original->getLimitStart());
1209  $this->setLimitEnd($original->getLimitEnd());
1210  $this->setViewOwnRecordsPerm($original->getViewOwnRecordsPerm());
1211  $this->setExportEnabled($original->getExportEnabled());
1212  $this->setImportEnabled($original->getImportEnabled());
1215  $this->setOrder($original->getOrder());
1216 
1217  $this->doCreate(true, false);
1218  // reset stdFields to get new for the created object
1219 
1220  $default_sort_field = 0;
1221  // Clone standard-fields
1222  $org_std_fields = $original->getStandardFields();
1223  foreach ($this->getStandardFields() as $element_key => $std_field) {
1224  $std_field->cloneStructure($org_std_fields[$element_key]);
1225  if ($std_field->getId() === $original->getDefaultSortField()) {
1226  $default_sort_field = $std_field->getId();
1227  }
1228  }
1229 
1230  // Clone fields
1231  $new_fields = array();
1232  foreach ($original->getFields() as $orig_field) {
1233  if (!$orig_field->isStandardField()) {
1234  $new_field = new ilDclBaseFieldModel();
1235  $new_field->setTableId($this->getId());
1236  $new_field->cloneStructure($orig_field->getId());
1237  $new_fields[$orig_field->getId()] = $new_field;
1238 
1239  if ($orig_field->getId() === $original->getDefaultSortField()) {
1240  $default_sort_field = $new_field->getId();
1241  }
1242  }
1243  }
1244 
1246  $this->doUpdate();
1247 
1248  // Clone Records with recordfields
1249  foreach($original->getRecords() as $orig_record){
1250  $new_record = new ilDclBaseRecordModel();
1251  $new_record->setTableId($this->getId());
1252  $new_record->cloneStructure($orig_record->getId(), $new_fields);
1253  }
1254 
1255  //clone tableviews (includes pageobjects)
1256  foreach ($original->getTableViews() as $orig_tableview) {
1257  $new_tableview = new ilDclTableView();
1258  $new_tableview->setTableId($this->getId());
1259  $new_tableview->cloneStructure($orig_tableview, $new_fields);
1260 
1261  }
1262 
1263  // mandatory for all cloning functions
1264  ilDclCache::setCloneOf($original->getId(), $this->getId(), ilDclCache::TYPE_TABLE);
1265  }
1266 
1267 
1271  public function afterClone() {
1272  foreach ($this->getFields() as $field) {
1273  $field->afterClone($this->getRecords());
1274  }
1275  }
1276 
1277 
1283  public function _hasRecords() {
1284  return (count($this->getRecords()) > 0) ? true : false;
1285  }
1286 
1287 
1291  public function addField($field) {
1292  $this->all_fields[$field->getId()] = $field;
1293  }
1294 
1295 
1301  public static function _tableExists($table_id) {
1302  global $DIC;
1303  $ilDB = $DIC['ilDB'];
1304  $query = "SELECT * FROM il_dcl_table WHERE id = " . $table_id;
1305  $result = $ilDB->query($query);
1306 
1307  return $result->numRows() != 0;
1308  }
1309 
1310 
1317  public static function _getTableIdByTitle($title, $obj_id) {
1318  global $DIC;
1319  $ilDB = $DIC['ilDB'];
1320  $result = $ilDB->query('SELECT id FROM il_dcl_table WHERE title = ' . $ilDB->quote($title, 'text') . ' AND obj_id = '
1321  . $ilDB->quote($obj_id, 'integer'));
1322  $id = 0;
1323  while ($rec = $ilDB->fetchAssoc($result)) {
1324  $id = $rec['id'];
1325  }
1326 
1327  return $id;
1328  }
1329 
1334  $this->export_enabled = $export_enabled;
1335  }
1336 
1337 
1341  public function getExportEnabled() {
1342  return $this->export_enabled;
1343  }
1344 
1348  public function getOrder()
1349  {
1350  if (!$this->table_order) {
1351  $this->updateOrder();
1352  }
1353  return $this->table_order;
1354  }
1355 
1359  public function updateOrder(){
1360  global $DIC;
1361  $ilDB = $DIC['ilDB'];
1362  $result = $ilDB->query('SELECT MAX(table_order) AS table_order FROM il_dcl_table WHERE obj_id = ' . $ilDB->quote($this->getCollectionObject()->getId(), 'integer'));
1363  $this->table_order = $ilDB->fetchObject($result)->table_order + 10;
1364  $ilDB->query('UPDATE il_dcl_table SET table_order = ' . $ilDB->quote($this->table_order, 'integer') . ' WHERE id = ' . $ilDB->quote($this->getId(), 'integer'));
1365  }
1366 
1370  public function setOrder($table_order)
1371  {
1372  $this->table_order = $table_order;
1373  }
1374 
1375 
1380  $this->import_enabled = $import_enabled;
1381  }
1382 
1383 
1387  public function getImportEnabled() {
1388  return $this->import_enabled;
1389  }
1390 
1391 
1400  public static function _hasFieldByTitle($title, $obj_id) {
1401  global $DIC;
1402  $ilDB = $DIC['ilDB'];
1403  $result = $ilDB->query('SELECT * FROM il_dcl_field WHERE table_id = ' . $ilDB->quote($obj_id, 'integer') . ' AND title = '
1404  . $ilDB->quote($title, 'text'));
1405 
1406  return ($ilDB->numRows($result)) ? true : false;
1407  }
1408 
1409 
1421  public function getPartialRecords($sort, $direction, $limit, $offset, array $filter = array()) {
1422  global $DIC;
1423  $ilDB = $DIC['ilDB'];
1427  $ilUser = $DIC['ilUser'];
1428  $rbacreview = $DIC['rbacreview'];
1429 
1430  $sort_field = ($sort) ? $this->getFieldByTitle($sort) : $this->getField('id');
1431  $direction = strtolower($direction);
1432  $direction = (in_array($direction, array( 'desc', 'asc' ))) ? $direction : 'asc';
1433 
1434  // Sorting by a status from an ILIAS Ref field. This column is added dynamically to the table, there is no field model
1435  $sort_by_status = false;
1436  if (substr($sort, 0, 8) == '_status_') {
1437  $sort_by_status = true;
1438  $sort_field = $this->getFieldByTitle(substr($sort, 8));
1439  }
1440 
1441  if (is_null($sort_field)) {
1442  $sort_field = $this->getField('id');
1443  }
1444 
1445  $sort_query_object = $sort_field->getRecordQuerySortObject($direction, $sort_by_status);
1446 
1447  $select_str = ($sort_query_object != NULL) ? $sort_query_object->getSelectStatement() : '';
1448  $join_str = ($sort_query_object != NULL) ? $sort_query_object->getJoinStatement() : '';
1449  $where_str = ($sort_query_object != NULL) ? $sort_query_object->getWhereStatement() : '';
1450  $order_str = ($sort_query_object != NULL) ? $sort_query_object->getOrderStatement() : '';
1451  $group_str = ($sort_query_object != NULL) ? $sort_query_object->getGroupStatement() : '';
1452 
1453  if (count($filter)) {
1454  foreach ($filter as $key => $filter_value) {
1455  $filter_field_id = substr($key, 7);
1456  $filter_field = $this->getField($filter_field_id);
1457  $filter_record_query_object = $filter_field->getRecordQueryFilterObject($filter_value, $sort_field);
1458 
1459  if ($filter_record_query_object) {
1460  $select_str .= $filter_record_query_object->getSelectStatement();
1461  $join_str .= $filter_record_query_object->getJoinStatement();
1462  $where_str .= $filter_record_query_object->getWhereStatement();
1463  $group_str .= $filter_record_query_object->getGroupStatement();
1464  }
1465  }
1466  }
1467 
1468  // Build the query string
1469  $sql = "SELECT DISTINCT record.id, record.owner";
1470  if ($select_str) {
1471  $sql .= ', ';
1472  }
1473 
1474  $as = $ilDB->getDBType() == 'oracle' ? '' : ' AS ';
1475 
1476  $sql .= rtrim($select_str, ',') . " FROM il_dcl_record {$as} record ";
1477  $sql .= $join_str;
1478  $sql .= " WHERE record.table_id = " . $ilDB->quote($this->getId(), 'integer');
1479 
1480  if (strlen($where_str) > 0) {
1481  $sql .= $where_str;
1482  }
1483 
1484  if (strlen($group_str) > 0) {
1485  $sql .= " GROUP BY " . $group_str;
1486  }
1487 
1488  if (strlen($order_str) > 0) {
1489  $sql .= " ORDER BY " . $order_str;
1490  }
1491 
1492  //var_dump($sql);
1493  /*global $DIC;
1494  /*$ilLog = $DIC['ilLog'];
1495  $ilLog->write($sql, ilLogLevel::CRITICAL);*/
1496 
1497  $set = $ilDB->query($sql);
1498  $total_record_ids = array();
1499  // Save record-ids in session to enable prev/next links in detail view
1500  $_SESSION['dcl_record_ids'] = array();
1501  $_SESSION['dcl_table_id'] = $this->getId();
1502  $ref = filter_input(INPUT_GET, 'ref_id');
1504  while ($rec = $ilDB->fetchAssoc($set)) {
1505  // Quick check if the current user is allowed to view the record
1506  if (!$is_allowed_to_view && ($this->getViewOwnRecordsPerm() && $ilUser->getId() != $rec['owner'])) {
1507  continue;
1508  }
1509  $total_record_ids[] = $rec['id'];
1510  $_SESSION['dcl_record_ids'][] = $rec['id'];
1511  }
1512 
1513  if ($sort_query_object != NULL) {
1514  $total_record_ids = $sort_query_object->applyCustomSorting($sort_field, $total_record_ids, $direction);
1515  }
1516 
1517  // Now slice the array to load only the needed records in memory
1518  $record_ids = array_slice($total_record_ids, $offset, $limit);
1519 
1520  $records = array();
1521  foreach ($record_ids as $id) {
1523  }
1524 
1525  return array( 'records' => $records, 'total' => count($total_record_ids) );
1526  }
1527 }
Class ilDclBaseFieldModel.
doRead()
Read table.
static setCloneOf($old, $new, $type)
setAddPerm($add_perm)
setId($a_id)
Set table id.
setTitle($a_title)
static buildFieldFromRecord($rec)
static _getTableIdByTitle($title, $obj_id)
$_SESSION["AccountId"]
$result
getVisibleTableViews($ref_id, $with_active_detailedview=false, $user_id=0)
For current user.
getFirstTableViewId($ref_id, $user_id=0)
get id of first (for current user) available view
hasPermissionToViewRecord($ref_id, $record, $user_id=0)
sortTableViews(array $tableviews=null)
static _hasFieldByTitle($title, $obj_id)
Checks if a table has a field with the given title.
static preloadFieldProperties(array $fields)
Preloads field properties.
static getFieldCache($field_id=0)
hasPermissionToDeleteRecord($ref_id, ilDclBaseRecordModel $record)
setEditByOwner($edit_by_owner)
static getAllForTableId($table_id)
static hasAccessToTableView($tableview, $user_id=0)
This only checks access to the tableview - if the full access check is required, use hasAccessTo($ref...
sortByOrder(&$array)
setPublicCommentsEnabled($public_comments)
setDefaultSortField($default_sort_field)
/**
setLimitEnd($limit_end)
static _lookupObjectId($a_ref_id)
setEditPerm($edit_perm)
deleteField($field_id)
getFields()
Returns all fields of this table including the standard fields.
hasPermissionToDeleteRecords($ref_id)
hasPermissionToEditRecord($ref_id, ilDclBaseRecordModel $record)
setDescription($description)
compareOrder($a, $b)
static getCountForTableId($table_id)
getId()
Get table id.
Class ilDclBaseFieldModel.
setExportEnabled($export_enabled)
doesRecordBelongToUser(ilDclBaseRecordModel $record)
$errors fields
Definition: imgupload.php:52
updateFields()
Update fields.
static _getStandardFields($table_id)
getExportableFields()
Return all the fields that are marked as exportable.
static _tableExists($table_id)
cloneStructure(ilDclTable $original)
hasCustomFields()
hasCustomFields
setDeletePerm($delete_perm)
static hasEditAccess($ref, $user_id=0)
Has permission to view and edit all entries event when he is not the owner.
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
static getRecordCache($record_id=0)
Class ilDclTableView.
getRecordFields()
Returns all fields of this table which are NOT standard fields.
setOrder($table_order)
Date and time handling
$ilUser
Definition: imgupload.php:18
sortFields(&$fields)
sortFields
_hasRecords()
_hasRecords
setSaveConfirmation($save_confirmation)
getFieldByTitle($title)
Get a field by title.
Create styles array
The data for the language used.
getField($field_id)
setLimitStart($limit_start)
static createOrGetStandardView($table_id, $create_default_settings=true)
setViewOwnRecordsPerm($view_own_perm)
const IL_CAL_DATE
Class ilDclBaseRecordModel.
$ref_id
Definition: sahs_server.php:39
global $ilDB
setIsVisible($is_visible)
global $DIC
doCreate($create_tablefield_setting=true, $create_standardview=true)
setImportEnabled($import_enabled)
buildOrderFields()
buildOrderFields orders the fields.
__construct($a_id=0)
Class ilObjDataCollection.
setDeleteByOwner($delete_by_owner)
setDefaultSortFieldOrder($default_sort_field_order)
setLimited($limited)
getNewFieldOrder()
getNewOrder