ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
22 {
23 
27  protected $id = 0;
31  protected $objId;
35  protected $obj;
39  protected $title;
43  protected $fields;
47  protected $stdFields;
51  protected $records;
55  protected $is_visible;
59  protected $add_perm;
63  protected $edit_perm;
67  protected $delete_perm;
71  protected $edit_by_owner;
75  protected $delete_by_owner;
79  protected $save_confirmation;
83  protected $limited;
87  protected $limit_start;
91  protected $limit_end;
95  protected $export_enabled;
99  protected $table_order;
103  protected $import_enabled;
109  protected $default_sort_field = 0;
115  protected $default_sort_field_order = 'asc';
121  protected $description = '';
127  protected $public_comments = 0;
133  protected $view_own_records_perm = 0;
139  protected $all_fields = null;
140 
141 
145  public function __construct($a_id = 0)
146  {
147  if ($a_id != 0) {
148  $this->id = $a_id;
149  $this->doRead();
150  }
151  }
152 
153 
157  public function doRead()
158  {
159  global $DIC;
160  $ilDB = $DIC['ilDB'];
161 
162  $query = "SELECT * FROM il_dcl_table WHERE id = " . $ilDB->quote($this->getId(), "integer");
163  $set = $ilDB->query($query);
164  $rec = $ilDB->fetchAssoc($set);
165 
166  $this->setObjId($rec["obj_id"]);
167  $this->setTitle($rec["title"]);
168  $this->setAddPerm($rec["add_perm"]);
169  $this->setEditPerm($rec["edit_perm"]);
170  $this->setDeletePerm($rec["delete_perm"]);
171  $this->setEditByOwner($rec["edit_by_owner"]);
172  $this->setExportEnabled($rec["export_enabled"]);
173  $this->setImportEnabled($rec["import_enabled"]);
174  $this->setLimited($rec["limited"]);
175  $this->setLimitStart($rec["limit_start"]);
176  $this->setLimitEnd($rec["limit_end"]);
177  $this->setIsVisible($rec["is_visible"]);
178  $this->setDescription($rec['description']);
179  $this->setDefaultSortField($rec['default_sort_field_id']);
180  $this->setDefaultSortFieldOrder($rec['default_sort_field_order']);
181  $this->setPublicCommentsEnabled($rec['public_comments']);
182  $this->setViewOwnRecordsPerm($rec['view_own_records_perm']);
183  $this->setDeleteByOwner($rec['delete_by_owner']);
184  $this->setSaveConfirmation($rec['save_confirmation']);
185  $this->setOrder($rec['table_order']);
186  }
187 
188 
196  public function doDelete($delete_only_content = false, $omit_notification = false)
197  {
198  global $DIC;
199  $ilDB = $DIC['ilDB'];
200 
202  foreach ($this->getRecords() as $record) {
203  $record->doDelete($omit_notification);
204  }
205 
206  foreach ($this->getRecordFields() as $field) {
207  $field->doDelete();
208  }
209 
210  // // SW: Fix #12794 und #11405
211  // // Problem is that when the DC object gets deleted, $this::getCollectionObject() tries to load the DC but it's not in the DB anymore
212  // // If $delete_main_table is true, avoid getting the collection object
213  // $exec_delete = false;
214  // if ($delete_main_table) {
215  // $exec_delete = true;
216  // }
217  // if (!$exec_delete && $this->getCollectionObject()->getFirstVisibleTableId() != $this->getId()) {
218  // $exec_delete = true;
219  // }
220  if (!$delete_only_content) {
221  $query = "DELETE FROM il_dcl_table WHERE id = " . $ilDB->quote($this->getId(), "integer");
222  $ilDB->manipulate($query);
223  }
224  }
225 
226 
230  public function doCreate($create_tablefield_setting = true, $create_standardview = true)
231  {
232  global $DIC;
233  $ilDB = $DIC['ilDB'];
234 
235  $id = $ilDB->nextId("il_dcl_table");
236  $this->setId($id);
237  $query = "INSERT INTO il_dcl_table (" . "id" . ", obj_id" . ", title" . ", add_perm" . ", edit_perm" . ", delete_perm" . ", edit_by_owner"
238  . ", limited" . ", limit_start" . ", limit_end" . ", is_visible" . ", export_enabled" . ", import_enabled" . ", default_sort_field_id"
239  . ", default_sort_field_order" . ", description" . ", public_comments" . ", view_own_records_perm"
240  . ", delete_by_owner, save_confirmation , table_order ) VALUES (" . $ilDB->quote($this->getId(), "integer") . ","
241  . $ilDB->quote($this->getObjId(), "integer") . "," . $ilDB->quote($this->getTitle(), "text") . ","
242  . $ilDB->quote($this->getAddPerm() ? 1 : 0, "integer") . "," . $ilDB->quote($this->getEditPerm() ? 1 : 0, "integer") . ","
243  . $ilDB->quote($this->getDeletePerm() ? 1 : 0, "integer") . "," . $ilDB->quote($this->getEditByOwner() ? 1 : 0, "integer") . ","
244  . $ilDB->quote($this->getLimited() ? 1 : 0, "integer") . "," . $ilDB->quote($this->getLimitStart(), "timestamp") . ","
245  . $ilDB->quote($this->getLimitEnd(), "timestamp") . "," . $ilDB->quote($this->getIsVisible() ? 1 : 0, "integer") . ","
246  . $ilDB->quote($this->getExportEnabled() ? 1 : 0, "integer") . "," . $ilDB->quote($this->getImportEnabled() ? 1 : 0, "integer") . ","
247  . $ilDB->quote($this->getDefaultSortField(), "text") . "," . $ilDB->quote($this->getDefaultSortFieldOrder(), "text") . ","
248  . $ilDB->quote($this->getDescription(), "text") . "," . $ilDB->quote($this->getPublicCommentsEnabled(), "integer") . ","
249  . $ilDB->quote($this->getViewOwnRecordsPerm(), "integer") . "," . $ilDB->quote($this->getDeleteByOwner() ? 1 : 0, 'integer') . ","
250  . $ilDB->quote($this->getSaveConfirmation() ? 1 : 0, 'integer') . "," . $ilDB->quote($this->getOrder(), 'integer') . ")";
251 
252  $ilDB->manipulate($query);
253 
254  if ($create_standardview) {
255  //standard tableview
257  }
258 
259  if ($create_tablefield_setting) {
260  $this->buildOrderFields();
261  }
262  }
263 
264 
265  /*
266  * doUpdate
267  */
268  public function doUpdate()
269  {
270  global $DIC;
271  $ilDB = $DIC['ilDB'];
272 
273  $ilDB->update(
274  "il_dcl_table",
275  array(
276  "obj_id" => array("integer", $this->getObjId()),
277  "title" => array("text", $this->getTitle()),
278  "add_perm" => array("integer", (int) $this->getAddPerm()),
279  "edit_perm" => array("integer", (int) $this->getEditPerm()),
280  "delete_perm" => array("integer", (int) $this->getDeletePerm()),
281  "edit_by_owner" => array("integer", (int) $this->getEditByOwner()),
282  "limited" => array("integer", $this->getLimited()),
283  "limit_start" => array("timestamp", $this->getLimitStart()),
284  "limit_end" => array("timestamp", $this->getLimitEnd()),
285  "is_visible" => array("integer", $this->getIsVisible() ? 1 : 0),
286  "export_enabled" => array("integer", $this->getExportEnabled() ? 1 : 0),
287  "import_enabled" => array("integer", $this->getImportEnabled() ? 1 : 0),
288  "description" => array("text", $this->getDescription()),
289  "default_sort_field_id" => array("text", $this->getDefaultSortField()),
290  "default_sort_field_order" => array("text", $this->getDefaultSortFieldOrder()),
291  "public_comments" => array("integer", $this->getPublicCommentsEnabled() ? 1 : 0),
292  "view_own_records_perm" => array("integer", $this->getViewOwnRecordsPerm()),
293  'delete_by_owner' => array('integer', $this->getDeleteByOwner() ? 1 : 0),
294  'save_confirmation' => array('integer', $this->getSaveConfirmation() ? 1 : 0),
295  'table_order' => array('integer', $this->getOrder()),
296  ),
297  array(
298  "id" => array("integer", $this->getId()),
299  )
300  );
301  }
302 
303 
309  public function setId($a_id)
310  {
311  $this->id = $a_id;
312  }
313 
314 
320  public function getId()
321  {
322  return $this->id;
323  }
324 
325 
329  public function setObjId($a_id)
330  {
331  $this->objId = $a_id;
332  }
333 
334 
338  public function getObjId()
339  {
340  return $this->objId;
341  }
342 
343 
347  public function setTitle($a_title)
348  {
349  $this->title = $a_title;
350  }
351 
352 
356  public function getTitle()
357  {
358  return $this->title;
359  }
360 
361 
365  public function getCollectionObject()
366  {
367  $this->loadObj();
368 
369  return $this->obj;
370  }
371 
372 
373  protected function loadObj()
374  {
375  if ($this->obj == null) {
376  $this->obj = new ilObjDataCollection($this->objId, false);
377  }
378  }
379 
380 
384  public function getRecords()
385  {
386  if ($this->records == null) {
387  $this->loadRecords();
388  }
389 
390  return $this->records;
391  }
392 
393 
394  public function loadRecords()
395  {
396  global $DIC;
397  $ilDB = $DIC['ilDB'];
398 
399  $records = array();
400  $query = "SELECT id FROM il_dcl_record WHERE table_id = " . $ilDB->quote($this->id, "integer");
401  $set = $ilDB->query($query);
402 
403  while ($rec = $ilDB->fetchAssoc($set)) {
404  $records[$rec['id']] = ilDclCache::getRecordCache($rec['id']);
405  }
406 
407  $this->records = $records;
408  }
409 
410 
414  public function deleteField($field_id)
415  {
416  $field = ilDclCache::getFieldCache($field_id);
417  $records = $this->getRecords();
418 
419  foreach ($records as $record) {
420  $record->deleteField($field_id);
421  }
422 
423  $field->doDelete();
424  }
425 
426 
432  public function getField($field_id)
433  {
434  $fields = $this->getFields();
435  $field = null;
436  foreach ($fields as $field_1) {
437  if ($field_1->getId() == $field_id) {
438  $field = $field_1;
439  }
440  }
441 
442  return $field;
443  }
444 
445 
451  public function getFieldIds()
452  {
453  $field_ids = array();
454  foreach ($this->getFields() as $field) {
455  if ($field->getId()) {
456  $field_ids[] = $field->getId();
457  }
458  }
459 
460  return $field_ids;
461  }
462 
463 
464  protected function loadCustomFields()
465  {
466  if (!$this->fields) {
467  global $DIC;
468  $ilDB = $DIC['ilDB'];
472  $query
473  = "SELECT DISTINCT il_dcl_field.*, il_dcl_tfield_set.field_order
474  FROM il_dcl_field
475  INNER JOIN il_dcl_tfield_set
476  ON ( il_dcl_tfield_set.field NOT IN ('owner',
477  'last_update',
478  'last_edit_by',
479  'id',
480  'create_date')
481  AND il_dcl_tfield_set.table_id = il_dcl_field.table_id
482  AND il_dcl_tfield_set.field = " . $ilDB->cast("il_dcl_field.id", "text") . ")
483  WHERE il_dcl_field.table_id = %s
484  ORDER BY il_dcl_tfield_set.field_order ASC";
485 
486  $set = $ilDB->queryF($query, array('integer'), array((int) $this->getId()));
487  $fields = array();
488  while ($rec = $ilDB->fetchAssoc($set)) {
489  $field = ilDclCache::buildFieldFromRecord($rec);
490  $fields[] = $field;
491  }
492  $this->fields = $fields;
493 
495  }
496  }
497 
498 
499  public function getCustomFields()
500  {
501  if (!$this->fields) {
502  $this->loadCustomFields();
503  }
504 
505  return $this->fields;
506  }
507 
508 
514  public function getNewFieldOrder()
515  {
516  $fields = $this->getFields();
517  $place = 0;
518  foreach ($fields as $field) {
519  if (!$field->isStandardField()) {
520  $place = $field->getOrder() + 1;
521  }
522  }
523 
524  return $place;
525  }
526 
527 
531  public function getNewTableviewOrder()
532  {
533  return (ilDclTableView::getCountForTableId($this->getId()) + 1) * 10;
534  }
535 
536 
540  public function sortTableViews(array $tableviews = null)
541  {
542  if ($tableviews == null) {
543  $tableviews = $this->getTableViews();
544  }
545 
546  $order = 10;
547  foreach ($tableviews as $tableview) {
548  $tableview->setTableviewOrder($order);
549  $tableview->update();
550  $order += 10;
551  }
552  }
553 
554 
562  public function getFields()
563  {
564  if ($this->all_fields == null) {
565  $this->reloadFields();
566  }
567 
568  return $this->all_fields;
569  }
570 
571 
572  public function reloadFields()
573  {
574  $this->loadCustomFields();
575  $this->stdFields = $this->getStandardFields();
576  $fields = array_merge($this->fields, $this->stdFields);
577 
578  $this->sortByOrder($fields);
579 
580  $this->all_fields = $fields;
581  }
582 
583 
587  public function getTableViews()
588  {
589  return ilDclTableView::getAllForTableId($this->getId());
590  }
591 
592 
601  public function getVisibleTableViews($ref_id, $with_active_detailedview = false, $user_id = 0)
602  {
603  if (ilObjDataCollectionAccess::hasWriteAccess($ref_id, $user_id) && !$with_active_detailedview) {
604  return $this->getTableViews();
605  }
606 
607  $visible_views = array();
608  foreach ($this->getTableViews() as $tableView) {
609  if (ilObjDataCollectionAccess::hasAccessToTableView($tableView, $user_id)) {
610  if (!$with_active_detailedview || ilDclDetailedViewDefinition::isActive($tableView->getId())) {
611  $visible_views[] = $tableView;
612  }
613  }
614  }
615 
616  return $visible_views;
617  }
618 
619 
628  public function getFirstTableViewId($ref_id, $user_id = 0)
629  {
630  $tableview = array_shift($this->getVisibleTableViews($ref_id, false, $user_id));
631 
632  return $tableview ? $tableview->getId() : false;
633  }
634 
635 
641  public function getFieldsForFormula()
642  {
643  $unsupported = array(
651  );
652 
653  $this->loadCustomFields();
654  $return = $this->getStandardFields();
658  foreach ($this->fields as $field) {
659  if (!in_array($field->getDatatypeId(), $unsupported)) {
660  $return[] = $field;
661  }
662  }
663 
664  return $return;
665  }
666 
667 
674  public function getStandardFields()
675  {
676  if ($this->stdFields == null) {
677  $this->stdFields = ilDclStandardField::_getStandardFields($this->id);
678  // Don't return comments as field if this feature is not activated in the settings
679  if (!$this->getPublicCommentsEnabled()) {
681  foreach ($this->stdFields as $k => $field) {
682  if ($field->getId() == 'comments') {
683  unset($this->stdFields[$k]);
684  break;
685  }
686  }
687  }
688  }
689 
690  return $this->stdFields;
691  }
692 
693 
699  public function getRecordFields()
700  {
701  $this->loadCustomFields();
702 
703  return $this->fields;
704  }
705 
706 
710  public function getEditableFields()
711  {
712  $fields = $this->getRecordFields();
713  $editableFields = array();
714 
715  foreach ($fields as $field) {
716  if (!$field->getLocked()) {
717  $editableFields[] = $field;
718  }
719  }
720 
721  return $editableFields;
722  }
723 
724 
730  public function getExportableFields()
731  {
732  $fields = $this->getFields();
733  $exportableFields = array();
734  foreach ($fields as $field) {
735  if ($field->getExportable()) {
736  $exportableFields[] = $field;
737  }
738  }
739 
740  return $exportableFields;
741  }
742 
743 
750  public function hasPermissionToEditRecord($ref_id, ilDclBaseRecordModel $record)
751  {
752  if ($this->getObjId() != ilObjDataCollection::_lookupObjectId($ref_id)) {
753  return false;
754  }
756  return true;
757  }
759  return false;
760  }
761  if (!$this->checkLimit()) {
762  return false;
763  }
764  if ($this->getEditPerm() && !$this->getEditByOwner()) {
765  return true;
766  }
767  if ($this->getEditByOwner()) {
768  return $this->doesRecordBelongToUser($record);
769  }
770 
771  return false;
772  }
773 
774 
781  public function hasPermissionToDeleteRecord($ref_id, ilDclBaseRecordModel $record)
782  {
783  if ($this->getObjId() != ilObjDataCollection::_lookupObjectId($ref_id)) {
784  return false;
785  }
787  return true;
788  }
790  return false;
791  }
792  if (!$this->checkLimit()) {
793  return false;
794  }
795  if ($this->getDeletePerm() && !$this->getDeleteByOwner()) {
796  return true;
797  }
798  if ($this->getDeleteByOwner()) {
799  return $this->doesRecordBelongToUser($record);
800  }
801 
802  return false;
803  }
804 
805 
811  public function hasPermissionToDeleteRecords($ref_id)
812  {
813  if ($this->getObjId() != ilObjDataCollection::_lookupObjectId($ref_id)) {
814  return false;
815  }
816  return ((ilObjDataCollectionAccess::hasAddRecordAccess($ref_id) && $this->getDeletePerm())
818  }
819 
820 
828  public function hasPermissionToViewRecord($ref_id, $record, $user_id = 0)
829  {
830  global $DIC;
831  $ilUser = $DIC['ilUser'];
832  if ($this->getObjId() != ilObjDataCollection::_lookupObjectId($ref_id)) {
833  return false;
834  }
835  if (ilObjDataCollectionAccess::hasWriteAccess($ref_id, $user_id) || ilObjDataCollectionAccess::hasEditAccess($ref_id, $user_id)) {
836  return true;
837  }
839  // Check for view only own entries setting
840  if ($this->getViewOwnRecordsPerm() && ($user_id ? $user_id : $ilUser->getId()) != $record->getOwner()) {
841  return false;
842  }
843 
844  return true;
845  }
846 
847  return false;
848  }
849 
850 
856  protected function doesRecordBelongToUser(ilDclBaseRecordModel $record)
857  {
858  global $DIC;
859  $ilUser = $DIC['ilUser'];
860 
861  return ($ilUser->getId() == $record->getOwner());
862  }
863 
864 
868  public function checkLimit()
869  {
870  if ($this->getLimited()) {
871  $now = new ilDateTime(date("Y-m-d H:i:s"), IL_CAL_DATE);
872  $from = new ilDateTime($this->getLimitStart(), IL_CAL_DATE);
873  $to = new ilDateTime($this->getLimitEnd(), IL_CAL_DATE);
874 
875  return ($from <= $now && $now <= $to);
876  }
877 
878  return true;
879  }
880 
881 
885  public function updateFields()
886  {
887  foreach ($this->getFields() as $field) {
888  $field->doUpdate();
889  }
890  }
891 
892 
898  public function sortFields(&$fields)
899  {
900  $this->sortByOrder($fields);
901  //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.
902  $named = array();
903  foreach ($fields as $field) {
904  $named[$field->getId()] = $field;
905  }
906 
907  $fields = $named;
908  }
909 
910 
915  protected function sortByOrder(&$array)
916  {
917  // php-bug: https://bugs.php.net/bug.php?id=50688
918  // fixed in php 7 but for now we need the @ a workaround
919  @usort($array, array($this, "compareOrder"));
920  }
921 
922 
927  public function buildOrderFields()
928  {
929  $fields = $this->getFields();
930  $this->sortByOrder($fields);
931  $count = 10;
932  $offset = 10;
933  foreach ($fields as $field) {
934  if (!is_null($field->getOrder())) {
935  $field->setOrder($count);
936  $count = $count + $offset;
937  $field->doUpdate();
938  }
939  }
940  }
941 
942 
950  public function getFieldByTitle($title)
951  {
952  $return = null;
953  foreach ($this->getFields() as $field) {
954  if ($field->getTitle() == $title) {
955  $return = $field;
956  break;
957  }
958  }
959 
960  return $return;
961  }
962 
963 
967  public function setAddPerm($add_perm)
968  {
969  $this->add_perm = $add_perm;
970  }
971 
972 
976  public function getAddPerm()
977  {
978  return (bool) $this->add_perm;
979  }
980 
981 
985  public function setDeletePerm($delete_perm)
986  {
987  $this->delete_perm = $delete_perm;
988  if (!$delete_perm) {
989  $this->setDeleteByOwner(false);
990  }
991  }
992 
993 
997  public function getDeletePerm()
998  {
999  return (bool) $this->delete_perm;
1000  }
1001 
1002 
1007  {
1008  $this->edit_by_owner = $edit_by_owner;
1009  if ($edit_by_owner) {
1010  $this->setEditPerm(true);
1011  }
1012  }
1013 
1014 
1018  public function getEditByOwner()
1019  {
1020  return (bool) $this->edit_by_owner;
1021  }
1022 
1023 
1027  public function getDeleteByOwner()
1028  {
1029  return (bool) $this->delete_by_owner;
1030  }
1031 
1032 
1037  {
1038  $this->delete_by_owner = $delete_by_owner;
1039  if ($delete_by_owner) {
1040  $this->setDeletePerm(true);
1041  }
1042  }
1043 
1044 
1048  public function setEditPerm($edit_perm)
1049  {
1050  $this->edit_perm = $edit_perm;
1051  if (!$edit_perm) {
1052  $this->setEditByOwner(false);
1053  }
1054  }
1055 
1056 
1060  public function getEditPerm()
1061  {
1062  return (bool) $this->edit_perm;
1063  }
1064 
1065 
1069  public function setLimited($limited)
1070  {
1071  $this->limited = $limited;
1072  }
1073 
1074 
1078  public function getLimited()
1079  {
1080  return $this->limited;
1081  }
1082 
1083 
1087  public function setLimitEnd($limit_end)
1088  {
1089  $this->limit_end = $limit_end;
1090  }
1091 
1092 
1096  public function getLimitEnd()
1097  {
1098  return $this->limit_end;
1099  }
1100 
1101 
1105  public function setLimitStart($limit_start)
1106  {
1107  $this->limit_start = $limit_start;
1108  }
1109 
1110 
1114  public function getLimitStart()
1115  {
1116  return $this->limit_start;
1117  }
1118 
1119 
1123  public function setIsVisible($is_visible)
1124  {
1125  $this->is_visible = $is_visible;
1126  }
1127 
1128 
1132  public function getIsVisible()
1133  {
1134  return $this->is_visible;
1135  }
1136 
1137 
1141  public function setDescription($description)
1142  {
1143  $this->description = $description;
1144  }
1145 
1146 
1150  public function getDescription()
1151  {
1152  return $this->description;
1153  }
1154 
1155 
1162  {
1163  $default_sort_field = ($default_sort_field) ? $default_sort_field : 0; // Change null or empty strings to zero
1164  $this->default_sort_field = $default_sort_field;
1165  }
1166 
1167 
1171  public function getDefaultSortField()
1172  {
1174  }
1175 
1176 
1181  {
1182  if (!in_array($default_sort_field_order, array('asc', 'desc'))) {
1183  $default_sort_field_order = 'asc';
1184  }
1185  $this->default_sort_field_order = $default_sort_field_order;
1186  }
1187 
1188 
1192  public function getDefaultSortFieldOrder()
1193  {
1195  }
1196 
1197 
1202  {
1203  $this->public_comments = $public_comments;
1204  }
1205 
1206 
1210  public function getPublicCommentsEnabled()
1211  {
1212  return $this->public_comments;
1213  }
1214 
1215 
1219  public function setViewOwnRecordsPerm($view_own_perm)
1220  {
1221  $this->view_own_records_perm = (int) $view_own_perm;
1222  }
1223 
1224 
1228  public function getViewOwnRecordsPerm()
1229  {
1230  return (bool) $this->view_own_records_perm;
1231  }
1232 
1233 
1237  public function getSaveConfirmation()
1238  {
1239  return $this->save_confirmation;
1240  }
1241 
1242 
1247  {
1248  $this->save_confirmation = $save_confirmation;
1249  }
1250 
1251 
1257  public function hasCustomFields()
1258  {
1259  $this->loadCustomFields();
1260 
1261  return (count($this->fields) > 0) ? true : false;
1262  }
1263 
1264 
1271  public function compareOrder($a, $b)
1272  {
1273  if (is_null($a->getOrder() == null) && is_null($b->getOrder() == null)) {
1274  return 0;
1275  }
1276  if (is_null($a->getOrder())) {
1277  return 1;
1278  }
1279  if (is_null($b->getOrder())) {
1280  return -1;
1281  }
1282 
1283  return $a->getOrder() < $b->getOrder() ? -1 : 1;
1284  }
1285 
1286 
1290  public function cloneStructure(ilDclTable $original)
1291  {
1292  $this->setTitle($original->getTitle());
1293  $this->setDescription($original->getDescription());
1294  $this->setIsVisible($original->getIsVisible());
1295  $this->setEditByOwner($original->getEditByOwner());
1296  $this->setAddPerm($original->getAddPerm());
1297  $this->setEditPerm($original->getEditPerm());
1298  $this->setDeleteByOwner($original->getDeleteByOwner());
1299  $this->setSaveConfirmation($original->getSaveConfirmation());
1300  $this->setDeletePerm($original->getDeletePerm());
1301  $this->setLimited($original->getLimited());
1302  $this->setLimitStart($original->getLimitStart());
1303  $this->setLimitEnd($original->getLimitEnd());
1304  $this->setViewOwnRecordsPerm($original->getViewOwnRecordsPerm());
1305  $this->setExportEnabled($original->getExportEnabled());
1306  $this->setImportEnabled($original->getImportEnabled());
1309  $this->setOrder($original->getOrder());
1310 
1311  $this->doCreate(true, false);
1312  // reset stdFields to get new for the created object
1313 
1314  $default_sort_field = 0;
1315  // Clone standard-fields
1316  $org_std_fields = $original->getStandardFields();
1317  foreach ($this->getStandardFields() as $element_key => $std_field) {
1318  $std_field->cloneStructure($org_std_fields[$element_key]);
1319  if ($std_field->getId() === $original->getDefaultSortField()) {
1320  $default_sort_field = $std_field->getId();
1321  }
1322  }
1323 
1324  // Clone fields
1325  $new_fields = array();
1326  foreach ($original->getFields() as $orig_field) {
1327  if (!$orig_field->isStandardField()) {
1328  $class_name = get_class($orig_field);
1329  $new_field = new $class_name();
1330  $new_field->setTableId($this->getId());
1331  $new_field->cloneStructure($orig_field->getId());
1332  $new_fields[$orig_field->getId()] = $new_field;
1333 
1334  if ($orig_field->getId() === $original->getDefaultSortField()) {
1335  $default_sort_field = $new_field->getId();
1336  }
1337  }
1338  }
1339 
1341  $this->doUpdate();
1342 
1343  // Clone Records with recordfields
1344  foreach ($original->getRecords() as $orig_record) {
1345  $new_record = new ilDclBaseRecordModel();
1346  $new_record->setTableId($this->getId());
1347  $new_record->cloneStructure($orig_record->getId(), $new_fields);
1348  }
1349 
1350  //clone tableviews (includes pageobjects)
1351  foreach ($original->getTableViews() as $orig_tableview) {
1352  $new_tableview = new ilDclTableView();
1353  $new_tableview->setTableId($this->getId());
1354  $new_tableview->cloneStructure($orig_tableview, $new_fields);
1355  }
1356 
1357  // mandatory for all cloning functions
1358  ilDclCache::setCloneOf($original->getId(), $this->getId(), ilDclCache::TYPE_TABLE);
1359  }
1360 
1361 
1365  public function afterClone()
1366  {
1367  foreach ($this->getFields() as $field) {
1368  $field->afterClone($this->getRecords());
1369  }
1370  }
1371 
1372 
1378  public function _hasRecords()
1379  {
1380  return (count($this->getRecords()) > 0) ? true : false;
1381  }
1382 
1383 
1387  public function addField($field)
1388  {
1389  $this->all_fields[$field->getId()] = $field;
1390  }
1391 
1392 
1398  public static function _tableExists($table_id)
1399  {
1400  global $DIC;
1401  $ilDB = $DIC['ilDB'];
1402  $query = "SELECT * FROM il_dcl_table WHERE id = " . $table_id;
1403  $result = $ilDB->query($query);
1404 
1405  return $result->numRows() != 0;
1406  }
1407 
1408 
1415  public static function _getTableIdByTitle($title, $obj_id)
1416  {
1417  global $DIC;
1418  $ilDB = $DIC['ilDB'];
1419  $result = $ilDB->query(
1420  'SELECT id FROM il_dcl_table WHERE title = ' . $ilDB->quote($title, 'text') . ' AND obj_id = '
1421  . $ilDB->quote($obj_id, 'integer')
1422  );
1423  $id = 0;
1424  while ($rec = $ilDB->fetchAssoc($result)) {
1425  $id = $rec['id'];
1426  }
1427 
1428  return $id;
1429  }
1430 
1431 
1436  {
1437  $this->export_enabled = $export_enabled;
1438  }
1439 
1440 
1444  public function getExportEnabled()
1445  {
1446  return $this->export_enabled;
1447  }
1448 
1449 
1453  public function getOrder()
1454  {
1455  if (!$this->table_order) {
1456  $this->updateOrder();
1457  }
1458 
1459  return $this->table_order;
1460  }
1461 
1462 
1466  public function updateOrder()
1467  {
1468  global $DIC;
1469  $ilDB = $DIC['ilDB'];
1470  $result = $ilDB->query('SELECT MAX(table_order) AS table_order FROM il_dcl_table WHERE obj_id = ' . $ilDB->quote($this->getCollectionObject()->getId(), 'integer'));
1471  $this->table_order = $ilDB->fetchObject($result)->table_order + 10;
1472  $ilDB->query('UPDATE il_dcl_table SET table_order = ' . $ilDB->quote($this->table_order, 'integer') . ' WHERE id = ' . $ilDB->quote($this->getId(), 'integer'));
1473  }
1474 
1475 
1479  public function setOrder($table_order)
1480  {
1481  $this->table_order = $table_order;
1482  }
1483 
1484 
1489  {
1490  $this->import_enabled = $import_enabled;
1491  }
1492 
1493 
1497  public function getImportEnabled()
1498  {
1499  return $this->import_enabled;
1500  }
1501 
1502 
1511  public static function _hasFieldByTitle($title, $obj_id)
1512  {
1513  global $DIC;
1514  $ilDB = $DIC['ilDB'];
1515  $result = $ilDB->query(
1516  'SELECT * FROM il_dcl_field WHERE table_id = ' . $ilDB->quote($obj_id, 'integer') . ' AND title = '
1517  . $ilDB->quote($title, 'text')
1518  );
1519 
1520  return ($ilDB->numRows($result)) ? true : false;
1521  }
1522 
1523 
1535  public function getPartialRecords($sort, $direction, $limit, $offset, array $filter = array())
1536  {
1537  global $DIC;
1538  $ilDB = $DIC['ilDB'];
1542  $ilUser = $DIC['ilUser'];
1543  $rbacreview = $DIC['rbacreview'];
1544 
1545  $sort_field = ($sort) ? $this->getFieldByTitle($sort) : $this->getField('id');
1546  $direction = strtolower($direction);
1547  $direction = (in_array($direction, array('desc', 'asc'))) ? $direction : 'asc';
1548 
1549  // Sorting by a status from an ILIAS Ref field. This column is added dynamically to the table, there is no field model
1550  $sort_by_status = false;
1551  if (substr($sort, 0, 8) == '_status_') {
1552  $sort_by_status = true;
1553  $sort_field = $this->getFieldByTitle(substr($sort, 8));
1554  }
1555 
1556  if (is_null($sort_field)) {
1557  $sort_field = $this->getField('id');
1558  }
1559 
1560  $sort_query_object = $sort_field->getRecordQuerySortObject($direction, $sort_by_status);
1561 
1562  $select_str = ($sort_query_object != null) ? $sort_query_object->getSelectStatement() : '';
1563  $join_str = ($sort_query_object != null) ? $sort_query_object->getJoinStatement() : '';
1564  $where_str = ($sort_query_object != null) ? $sort_query_object->getWhereStatement() : '';
1565  $order_str = ($sort_query_object != null) ? $sort_query_object->getOrderStatement() : '';
1566  $group_str = ($sort_query_object != null) ? $sort_query_object->getGroupStatement() : '';
1567 
1568  if (count($filter)) {
1569  foreach ($filter as $key => $filter_value) {
1570  $filter_field_id = substr($key, 7);
1571  $filter_field = $this->getField($filter_field_id);
1572  $filter_record_query_object = $filter_field->getRecordQueryFilterObject($filter_value, $sort_field);
1573 
1574  if ($filter_record_query_object) {
1575  $select_str .= $filter_record_query_object->getSelectStatement();
1576  $join_str .= $filter_record_query_object->getJoinStatement();
1577  $where_str .= $filter_record_query_object->getWhereStatement();
1578  $group_str .= $filter_record_query_object->getGroupStatement();
1579  }
1580  }
1581  }
1582 
1583  // Build the query string
1584  $sql = "SELECT DISTINCT record.id, record.owner";
1585  if ($select_str) {
1586  $sql .= ', ';
1587  }
1588 
1589  $as = ' AS ';
1590 
1591  $sql .= rtrim($select_str, ',') . " FROM il_dcl_record {$as} record ";
1592  $sql .= $join_str;
1593  $sql .= " WHERE record.table_id = " . $ilDB->quote($this->getId(), 'integer');
1594 
1595  if (strlen($where_str) > 0) {
1596  $sql .= $where_str;
1597  }
1598 
1599  if (strlen($group_str) > 0) {
1600  $sql .= " GROUP BY " . $group_str;
1601  }
1602 
1603  if (strlen($order_str) > 0) {
1604  $sql .= " ORDER BY " . $order_str;
1605  }
1606 
1607  //var_dump($sql);
1608  /*global $DIC;
1609  /*$ilLog = $DIC['ilLog'];
1610  $ilLog->write($sql, ilLogLevel::CRITICAL);*/
1611 
1612  $set = $ilDB->query($sql);
1613  $total_record_ids = array();
1614  // Save record-ids in session to enable prev/next links in detail view
1615  $_SESSION['dcl_record_ids'] = array();
1616  $_SESSION['dcl_table_id'] = $this->getId();
1617  $ref = filter_input(INPUT_GET, 'ref_id');
1619  while ($rec = $ilDB->fetchAssoc($set)) {
1620  // Quick check if the current user is allowed to view the record
1621  if (!$is_allowed_to_view && ($this->getViewOwnRecordsPerm() && $ilUser->getId() != $rec['owner'])) {
1622  continue;
1623  }
1624  $total_record_ids[] = $rec['id'];
1625  $_SESSION['dcl_record_ids'][] = $rec['id'];
1626  }
1627 
1628  if ($sort_query_object != null) {
1629  $total_record_ids = $sort_query_object->applyCustomSorting($sort_field, $total_record_ids, $direction);
1630  }
1631 
1632  // Now slice the array to load only the needed records in memory
1633  $record_ids = array_slice($total_record_ids, $offset, $limit);
1634 
1635  $records = array();
1636  foreach ($record_ids as $id) {
1638  }
1639 
1640  return array('records' => $records, 'total' => count($total_record_ids));
1641  }
1642 }
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
global $DIC
Definition: saml.php:7
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)
$from
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:51
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.
$as
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
$query
_hasRecords()
_hasRecords
setSaveConfirmation($save_confirmation)
getFieldByTitle($title)
Get a field by title.
getField($field_id)
setLimitStart($limit_start)
static createOrGetStandardView($table_id, $create_default_settings=true)
setViewOwnRecordsPerm($view_own_perm)
const IL_CAL_DATE
Class ilDclBaseRecordModel.
global $ilDB
setIsVisible($is_visible)
doCreate($create_tablefield_setting=true, $create_standardview=true)
setImportEnabled($import_enabled)
buildOrderFields()
buildOrderFields orders the fields.
__construct($a_id=0)
$key
Definition: croninfo.php:18
Class ilObjDataCollection.
setDeleteByOwner($delete_by_owner)
setDefaultSortFieldOrder($default_sort_field_order)
setLimited($limited)
getNewFieldOrder()
getNewOrder