ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDataCollectionTable.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/class.ilDataCollectionStandardField.php';
6 include_once './Modules/DataCollection/classes/class.ilDataCollectionRecord.php';
7 
20 {
21  protected $id; // [int]
22  protected $objId; // [int]
23  protected $obj;
24  protected $title; // [string]
25  private $fields; // [array][ilDataCollectionField]
26  private $stdFields;
27  private $records;
28 
32  private $is_visible;
33 
37  private $add_perm;
38 
42  private $edit_perm;
46  private $delete_perm;
50  private $edit_by_owner;
51 
55  private $limited;
59  private $limit_start;
63  private $limit_end;
64 
68  protected $export_enabled;
69 
76  public function __construct($a_id = 0)
77  {
78  if($a_id != 0)
79  {
80  $this->id = $a_id;
81  $this->doRead();
82  }
83  }
84 
85 
89  public function doRead()
90  {
91  global $ilDB;
92 
93  $query = "SELECT * FROM il_dcl_table WHERE id = ".$ilDB->quote($this->getId(),"integer");
94  $set = $ilDB->query($query);
95  $rec = $ilDB->fetchAssoc($set);
96 
97  $this->setObjId($rec["obj_id"]);
98  $this->setTitle($rec["title"]);
99  $this->setAddPerm($rec["add_perm"]);
100  $this->setEditPerm($rec["edit_perm"]);
101  $this->setDeletePerm($rec["delete_perm"]);
102  $this->setEditByOwner($rec["edit_by_owner"]);
103  $this->setExportEnabled($rec["export_enabled"]);
104  $this->setLimited($rec["limited"]);
105  $this->setLimitStart($rec["limit_start"]);
106  $this->setLimitEnd($rec["limit_end"]);
107  $this->setIsVisible($rec["is_visible"]);
108  }
109 
116  public function doDelete($delete_main_table = false)
117  {
118  global $ilDB;
119 
120  foreach($this->getRecords() as $record)
121  {
122  $record->doDelete();
123  }
124 
125  foreach($this->getRecordFields() as $field)
126  {
127  $field->doDelete();
128  }
129 
130  // SW: Fix #12794 und #11405
131  // Problem is that when the DC object gets deleted, $this::getCollectionObject() tries to load the DC but it's not in the DB anymore
132  // If $delete_main_table is true, avoid getting the collection object
133  $exec_delete = false;
134  if ($delete_main_table) {
135  $exec_delete = true;
136  }
137  if (!$exec_delete && $this->getCollectionObject()->getMainTableId() != $this->getId()) {
138  $exec_delete = true;
139  }
140  if ($exec_delete) {
141  $query = "DELETE FROM il_dcl_table WHERE id = ".$ilDB->quote($this->getId(), "integer");
142  $ilDB->manipulate($query);
143  }
144  }
145 
149  public function doCreate()
150  {
151  global $ilDB;
152 
153  $id = $ilDB->nextId("il_dcl_table");
154  $this->setId($id);
155  $query = "INSERT INTO il_dcl_table (".
156  "id".
157  ", obj_id".
158  ", title".
159  ", add_perm".
160  ", edit_perm".
161  ", delete_perm".
162  ", edit_by_owner".
163  ", limited".
164  ", limit_start".
165  ", limit_end".
166  ", is_visible".
167  ", export_enabled".
168  " ) VALUES (".
169  $ilDB->quote($this->getId(), "integer")
170  .",".$ilDB->quote($this->getObjId(), "integer")
171  .",".$ilDB->quote($this->getTitle(), "text")
172  .",".$ilDB->quote($this->getAddPerm()?1:0, "integer")
173  .",".$ilDB->quote($this->getEditPerm()?1:0, "integer")
174  .",".$ilDB->quote($this->getDeletePerm()?1:0, "integer")
175  .",".$ilDB->quote($this->getEditByOwner()?1:0, "integer")
176  .",".$ilDB->quote($this->getLimited()?1:0, "integer")
177  .",".$ilDB->quote($this->getLimitStart(), "timestamp")
178  .",".$ilDB->quote($this->getLimitEnd(), "timestamp")
179  .",".$ilDB->quote($this->getIsVisible()?1:0, "integer")
180  .",".$ilDB->quote($this->getExportEnabled()?1:0, "integer")
181  .")";
182  $ilDB->manipulate($query);
183 
184  //add view definition
185  $view_id = $ilDB->nextId("il_dcl_view");
186  $query = "INSERT INTO il_dcl_view (id, table_id, type, formtype) VALUES (".$ilDB->quote($view_id, "integer").", ".$ilDB->quote($this->id, "integer").", ".$ilDB->quote(ilDataCollectionField::VIEW_VIEW, "integer").", ".$ilDB->quote(1, "integer").")";
187  $ilDB->manipulate($query);
188 
189  //add edit definition
190  $view_id = $ilDB->nextId("il_dcl_view");
191  $query = "INSERT INTO il_dcl_view (id, table_id, type, formtype) VALUES (".$ilDB->quote($view_id, "integer").", ".$ilDB->quote($this->id, "integer").", ".$ilDB->quote(ilDataCollectionField::EDIT_VIEW, "integer").", ".$ilDB->quote(1, "integer").")";
192  $ilDB->manipulate($query);
193 
194  //add filter definition
195  $view_id = $ilDB->nextId("il_dcl_view");
196  $query = "INSERT INTO il_dcl_view (id, table_id, type, formtype) VALUES (".$ilDB->quote($view_id, "integer").", ".$ilDB->quote($this->id, "integer").", ".$ilDB->quote(ilDataCollectionField::FILTER_VIEW, "integer").", ".$ilDB->quote(1, "integer").")";
197  $ilDB->manipulate($query);
198 
199  $this->buildOrderFields();
200  }
201 
202  /*
203  * doUpdate
204  */
205  public function doUpdate()
206  {
207  global $ilDB;
208 
209  $ilDB->update("il_dcl_table", array(
210  "obj_id" => array("integer", $this->getObjId()),
211  "title" => array("text", $this->getTitle()),
212  "add_perm" => array("integer",$this->getAddPerm()),
213  "edit_perm" => array("integer",$this->getEditPerm()),
214  "delete_perm" => array("integer",$this->getDeletePerm()),
215  "edit_by_owner" => array("integer",$this->getEditByOwner()),
216  "limited" => array("integer",$this->getLimited()),
217  "limit_start" => array("timestamp",$this->getLimitStart()),
218  "limit_end" => array("timestamp",$this->getLimitEnd()),
219  "is_visible" => array("integer",$this->getIsVisible()?1:0),
220  "export_enabled" => array("integer",$this->getExportEnabled()?1:0)
221  ), array(
222  "id" => array("integer", $this->getId())
223  ));
224  }
225 
231  public function setId($a_id)
232  {
233  $this->id = $a_id;
234  }
235 
241  public function getId()
242  {
243  return $this->id;
244  }
245 
251  public function setObjId($a_id)
252  {
253  $this->objId = $a_id;
254  }
255 
261  public function getObjId()
262  {
263  return $this->objId;
264  }
265 
271  public function setTitle($a_title)
272  {
273  $this->title = $a_title;
274  }
275 
281  public function getTitle()
282  {
283  return $this->title;
284  }
285 
290  public function getCollectionObject()
291  {
292  $this->loadObj();
293 
294  return $this->obj;
295  }
296 
297  /*
298  * loadObj
299  */
300  private function loadObj()
301  {
302  if($this->obj == NULL)
303  {
304  $this->obj = new ilObjDataCollection($this->objId, false);
305  }
306  }
307 
311  public function getRecords()
312  {
313  $this->loadRecords();
314 
315  return $this->records;
316  }
317 
327  public function getRecordsByFilter(array $filter=array())
328  {
329  $this->loadRecords();
330 
331  // Only pass records trough filter if there is filtering required #performance-improvements
332  if (!count($filter)) {
333  return $this->records;
334  }
335  $filtered = array();
336  foreach($this->getRecords() as $record) {
337  if($record->passThroughFilter($filter)) {
338  $filtered[] = $record;
339  }
340  }
341  return $filtered;
342  }
343 
344  /*
345  * loadRecords
346  */
347  private function loadRecords()
348  {
349  if($this->records == NULL)
350  {
351  global $ilDB;
352 
353  $records = array();
354  $query = "SELECT id FROM il_dcl_record WHERE table_id = ".$ilDB->quote($this->id, "integer");
355  $set = $ilDB->query($query);
356 
357  while($rec = $ilDB->fetchAssoc($set))
358  {
359  $records[$rec['id']] = ilDataCollectionCache::getRecordCache($rec['id']);
360  }
361 
362  $this->records = $records;
363  }
364  }
365 
366  //TODO: replace this method with DataCollection->getTables()
373  public function getAll($a_id)
374  {
375  global $ilDB;
376 
377  //build query
378  $query = "SELECT *
379  FROM il_dcl_table
380  WHERE obj_id = ".$ilDB->quote($a_id,"integer");
381  $set = $ilDB->query($query);
382 
383  $all = array();
384  while($rec = $ilDB->fetchAssoc($set))
385  {
386  $all[$rec['id']] = $rec;
387  }
388 
389  return $all;
390  }
391 
392 
393  /*
394  * deleteField
395  */
396  public function deleteField($field_id)
397  {
398  $field = ilDataCollectionCache::getFieldCache($field_id);
399  $records = $this->getRecords();
400 
401  foreach($records as $record)
402  {
403  $record->deleteField($field_id);
404  }
405 
406  $field->doDelete();
407  }
408 
409 
410  /*
411  * getField
412  */
413  public function getField($field_id)
414  {
415  $fields = $this->getFields();
416  $field = NULL;
417  foreach($fields as $field_1)
418  {
419  if($field_1->getId() == $field_id)
420  {
421  $field = $field_1;
422  }
423  }
424 
425  return $field;
426  }
427 
428  /*
429  * getFieldIds
430  */
431  public function getFieldIds()
432  {
433  return array_keys($this->getFields());
434  }
435 
436  /*
437  * loadFields
438  */
439  private function loadFields()
440  {
441  if($this->fields == NULL)
442  {
443  global $ilDB;
444 
445  $query = "SELECT field.id, field.table_id, field.title, field.description, field.datatype_id, field.required, field.is_unique, field.is_locked FROM il_dcl_field field INNER JOIN il_dcl_view view ON view.table_id = field.table_id INNER JOIN il_dcl_viewdefinition def ON def.view_id = view.id WHERE field.table_id =".$ilDB->quote($this->getId(), "integer")." ORDER BY def.field_order DESC";
446  $fields = array();
447  $set = $ilDB->query($query);
448 
449  while($rec = $ilDB->fetchAssoc($set))
450  {
452 // $field = new ilDataCollectionField();
453 // $field->buildFromDBRecord($rec);
454  $fields[$field->getId()] = $field;
455  }
456  $this->sortByOrder($fields);
457  $this->fields = $fields;
458  }
459  }
460 
465  public function getNewOrder()
466  {
467  $fields = $this->getFields();
468  $place = 0;
469  foreach($fields as $field)
470  {
471  if($field->isVisible())
472  {
473  $place = $field->getOrder() + 1;
474  }
475  }
476 
477  return $place;
478  }
479 
484  public function getFields()
485  {
486  $this->loadFields();
487  if($this->stdFields == NULL)
488  {
489  $this->stdFields = ilDataCollectionStandardField::_getStandardFields($this->id);
490  }
491  $fields = array_merge($this->fields, $this->stdFields);
492  $this->sortByOrder($fields);
493 
494  return $fields;
495  }
496 
501  public function getRecordFields()
502  {
503  $this->loadFields();
504 
505  return $this->fields;
506  }
507 
512  public function getVisibleFields()
513  {
514  $fields = $this->getFields();
515 
516  $visibleFields = array();
517 
518  foreach($fields as $field)
519  {
520  if($field->isVisible())
521  {
522  $visibleFields[] = $field;
523  }
524  }
525 
526  return $visibleFields;
527  }
528 
529  /*
530  * getEditableFields
531  */
532  public function getEditableFields()
533  {
534  $fields = $this->getRecordFields();
535  $editableFields = array();
536 
537  foreach($fields as $field)
538  {
539  if(!$field->getLocked())
540  {
541  array_push($editableFields, $field);
542  }
543  }
544 
545  return $editableFields;
546  }
547 
553  public function getFilterableFields()
554  {
555  $fields = $this->getFields();
556  $filterableFields = array();
557 
558  foreach($fields as $field)
559  {
560  if($field->isFilterable())
561  {
562  array_push($filterableFields, $field);
563  }
564  }
565 
566  return $filterableFields;
567  }
568 
569  /*
570  * hasPermissionToFields
571  */
573  {
575  }
576 
577  /*
578  * hasPermissionToAddTable
579  */
581  {
583  }
584 
585 
586  public function hasPermissionToAddRecord($ref)
587  {
589  }
590 
596  public function hasPermissionToEditRecord($ref, $record)
597  {
598  return ($this->getEditPerm() && ilObjDataCollection::_hasReadAccess($ref) && $this->checkEditByOwner($record) && $this->checkLimit()) || ilObjDataCollection::_hasWriteAccess($ref);
599  }
600 
606  public function hasPermissionToDeleteRecord($ref, $record)
607  {
608  return ($this->getDeletePerm() && ilObjDataCollection::_hasReadAccess($ref) && $this->checkEditByOwner($record) && $this->checkLimit()) || ilObjDataCollection::_hasWriteAccess($ref);
609  }
610 
611  /*
612  * checkEditByOwner
613  */
614  private function checkEditByOwner($record)
615  {
616  global $ilUser;
617 
618  if($this->getEditByOwner() && $ilUser->getId() != $record->getOwner())
619  {
620  return false;
621  }
622 
623  return true;
624  }
625 
626  /*
627  * checkLimit
628  */
629  private function checkLimit()
630  {
631  if($this->getLimited())
632  {
633  $now = new ilDateTime(time(), IL_CAL_UNIX);
634  $from = new ilDateTime($this->getLimitStart(), IL_CAL_DATE);
635  $to = new ilDateTime($this->getLimitEnd(), IL_CAL_DATE);
636 
637  if(!($from <= $now && $now <= $to))
638  {
639  return false;
640  }
641  }
642  return true;
643  }
644 
645  /*
646  * updateFields
647  */
648  public function updateFields()
649  {
650  foreach($this->getFields() as $field)
651  {
652  $field->doUpdate();
653  }
654  }
655 
660  public function sortFields(&$fields)
661  {
662  $this->sortByOrder($fields);
663 
664  //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.
665  $named = array();
666  foreach($fields as $field)
667  {
668  $named[$field->getId()] = $field;
669  }
670 
671  $fields = $named;
672  }
673 
678  private function sortByOrder(&$array)
679  {
680  usort($array, array($this, "compareOrder"));
681  }
682 
687  public function buildOrderFields()
688  {
689  $fields = $this->getFields();
690 
691  $this->sortByOrder($fields);
692 
693  $count = 10;
694  $offset = 10;
695 
696  foreach($fields as $field)
697  {
698  if(!is_null($field->getOrder()))
699  {
700  $field->setOrder($count);
701  $count = $count + $offset;
702  $field->doUpdate();
703  }
704  }
705  }
706 
711  public function getFieldByTitle($name){
712  $return = null;
713  foreach($this->getFields() as $field)
714  if($field->getTitle() == $name){
715  $return = $field;
716  break;
717  }
718  return $return;
719  }
720 
724  public function setAddPerm($add_perm)
725  {
726  $this->add_perm = $add_perm;
727  }
728 
732  public function getAddPerm()
733  {
734  return $this->add_perm;
735  }
736 
740  public function setDeletePerm($delete_perm)
741  {
742  $this->delete_perm = $delete_perm;
743  }
744 
748  public function getDeletePerm()
749  {
750  return $this->delete_perm;
751  }
752 
757  {
758  $this->edit_by_owner = $edit_by_owner;
759  }
760 
764  public function getEditByOwner()
765  {
766  return $this->edit_by_owner;
767  }
768 
772  public function setEditPerm($edit_perm)
773  {
774  $this->edit_perm = $edit_perm;
775  }
776 
780  public function getEditPerm()
781  {
782  return $this->edit_perm;
783  }
784 
788  public function setLimited($limited)
789  {
790  $this->limited = $limited;
791  }
792 
796  public function getLimited()
797  {
798  return $this->limited;
799  }
800 
804  public function setLimitEnd($limit_end)
805  {
806  $this->limit_end = $limit_end;
807  }
808 
812  public function getLimitEnd()
813  {
814  return $this->limit_end;
815  }
816 
820  public function setLimitStart($limit_start)
821  {
822  $this->limit_start = $limit_start;
823  }
824 
828  public function getLimitStart()
829  {
830  return $this->limit_start;
831  }
832 
836  public function setIsVisible($is_visible)
837  {
838  $this->is_visible = $is_visible;
839  }
840 
844  public function getIsVisible()
845  {
846  return $this->is_visible;
847  }
848 
853  public function hasCustomFields()
854  {
855  $this->loadFields();
856 
857  return (count($this->fields) > 0) ? true : false;
858  }
859 
860  function compareOrder($a, $b)
861  {
862  if(is_null($a->getOrder() == NULL) && is_null($b->getOrder() == NULL))
863  {
864  return 0;
865  }
866  if(is_null($a->getOrder()))
867  {
868  return 1;
869  }
870  if(is_null($b->getOrder()))
871  {
872  return -1;
873  }
874 
875  return $a->getOrder() < $b->getOrder() ? -1 : 1;
876  }
877 
878  /*
879  * cloneStructure
880  */
881  public function cloneStructure($original_id)
882  {
883  $original = ilDataCollectionCache::getTableCache($original_id);
884  $this->setEditByOwner($original->getEditByOwner());
885  $this->setAddPerm($original->getAddPerm());
886  $this->setEditPerm($original->getEditPerm());
887  $this->setDeletePerm($original->getDeletePerm());
888  $this->setLimited($original->getLimited());
889  $this->setLimitStart($original->getLimitStart());
890  $this->setLimitEnd($original->getLimitEnd());
891  $this->setTitle($original->getTitle());
892  $this->doCreate();
893 
894  //clone fields.
895  foreach($original->getRecordFields() as $field)
896  {
897  $new_field = new ilDataCollectionField();
898  $new_field->setTableId($this->getId());
899  $new_field->cloneStructure($field->getId());
900  }
901 
902  if($old_view_id = ilDataCollectionRecordViewViewdefinition::getIdByTableId($original_id)){
903  $old_view = new ilDataCollectionRecordViewViewdefinition($old_view_id, $original_id);
904  $viewdef = new ilDataCollectionRecordViewViewdefinition(0, $this->id);
905  $viewdef->setXMLContent($old_view->getXMLContent(false));
906  $viewdef->create();
907  }
908  }
909 
914  public function _hasRecords()
915  {
916  return (count($this->getRecords()) > 0) ? true : false;
917  }
918 
922  public function addField($field){
923  $this->fields[$field->getId()] = $field;
924  }
925 
930  public static function _tableExists($table_id){
931  global $ilDB;
932  $query = "SELECT * FROM il_dcl_table WHERE id = ".$table_id;
933  $result = $ilDB->query($query);
934  return $result->numRows() != 0;
935  }
936 
942  public static function _getTableIdByTitle($title, $obj_id) {
943  global $ilDB;
944  $result = $ilDB->query('SELECT id FROM il_dcl_table WHERE title = ' . $ilDB->quote($title, 'text') . ' AND obj_id = ' . $ilDB->quote($obj_id, 'integer'));
945  $id = 0;
946  while($rec = $ilDB->fetchAssoc($result)) {
947  $id = $rec['id'];
948  }
949  return $id;
950  }
951 
952  public function buildTableAsArray(){
953  global $ilDB;
954  $fields = $this->getVisibleFields();
955  $table = array();
956  $query = " SELECT stloc.value AS val rec_field AS FROM il_dcl_stloc1_value stloc
957  INNER JOIN il_dcl_record_field rec_field ON rec_field.field_id = 2
958  WHERE stloc.record_field_id = rec_field.id";
959  }
960 
965  {
966  $this->export_enabled = $export_enabled;
967  }
968 
972  public function getExportEnabled()
973  {
974  return $this->export_enabled;
975  }
976 
983  public static function _hasFieldByTitle($title, $obj_id) {
984  global $ilDB;
985  $result = $ilDB->query('SELECT * FROM il_dcl_field WHERE table_id = ' . $ilDB->quote($obj_id, 'integer') . ' AND title = ' . $ilDB->quote($title, 'text'));
986  return ($ilDB->numRows($result)) ? true : false;
987  }
988 
999  public function getPartialRecords($sort, $direction, $limit, $offset, array $filter = array()) {
1000  global $ilDB;
1001 
1002  $sortField = ($sort) ? $sortField = $this->getFieldByTitle($sort) : $sortField = $this->getFieldByTitle('id');
1003  $direction = strtolower($direction);
1004  $direction = (in_array($direction, array('desc', 'asc'))) ? $direction : 'asc';
1005 
1006  // Sorting by a status from an ILIAS Ref field. This column is added dynamically to the table, there is no field model
1007  $sortByStatus = false;
1008  if (substr($sort, 0, 8) == '_status_') {
1009  $sortByStatus = true;
1010  $sortField = $this->getFieldByTitle(substr($sort, 8));
1011  }
1012 
1013  if (is_null($sortField)) $sortField = $this->getFieldByTitle('id');
1014 
1015  $id = $sortField->getId();
1016  $stl = $sortField->getStorageLocation();
1017  $selectStr = '';
1018  $joinStr = '';
1019  $where_additions = '';
1020 
1021  if ($sortField->isStandardField()) {
1022  if ($id == 'owner' || $id == 'last_edit_by') {
1023  $joinStr .= "LEFT JOIN usr_data AS sort_usr_data_{$id} ON (sort_usr_data_{$id}.usr_id = record.{$id})";
1024  $selectStr .= " sort_usr_data_{$id}.login AS field_{$id},";
1025  } else {
1026  $selectStr .= " record.{$id} AS field_{$id},";
1027  }
1028  } else {
1029  switch ($sortField->getDatatypeId()) {
1031  $joinStr .= "LEFT JOIN (SELECT AVG(sort_avg_rating.rating) AS avg_rating, sort_avg_rating.obj_id AS obj_id FROM il_rating as sort_avg_rating WHERE sort_avg_rating.sub_obj_id = {$sortField->getId()} GROUP BY sort_avg_rating.obj_id) AS sort_avg_rating on sort_avg_rating.obj_id = record.id ";
1032  $selectStr .= " sort_avg_rating.avg_rating AS field_{$id},";
1033  break;
1035  $joinStr .= "LEFT JOIN il_dcl_record_field AS sort_record_field_{$id} ON (sort_record_field_{$id}.record_id = record.id AND sort_record_field_{$id}.field_id = " . $ilDB->quote($sortField->getId(), 'integer') .") ";
1036  $joinStr .= "LEFT JOIN il_dcl_stloc{$stl}_value AS sort_stloc_{$id} ON (sort_stloc_{$id}.record_field_id = sort_record_field_{$id}.id) ";
1037  $joinStr .= "LEFT JOIN object_reference AS sort_object_reference_{$id} ON (sort_object_reference_{$id}.ref_id = sort_stloc_{$id}.value AND sort_object_reference_{$id}.deleted IS NULL)";
1038  $joinStr .= "LEFT JOIN object_data AS sort_object_data_{$id} ON (sort_object_data_{$id}.obj_id = sort_object_reference_{$id}.obj_id)";
1039  if ($sortByStatus) {
1040  global $ilUser;
1041  $joinStr .= "LEFT JOIN ut_lp_marks AS ut ON (ut.obj_id = sort_object_data_{$id}.obj_id AND ut.usr_id = " . $ilDB->quote($ilUser->getId(), 'integer') . ") ";
1042  }
1043  $selectStr .= (!$sortByStatus) ? " sort_object_data_{$id}.title AS field_{$id}," : " ut.status AS field_{$id}";
1044  break;
1047  $joinStr .= "LEFT JOIN il_dcl_record_field AS sort_record_field_{$id} ON (sort_record_field_{$id}.record_id = record.id AND sort_record_field_{$id}.field_id = " . $ilDB->quote($sortField->getId(), 'integer') .") ";
1048  $joinStr .= "LEFT JOIN il_dcl_stloc{$stl}_value AS sort_stloc_{$id} ON (sort_stloc_{$id}.record_field_id = sort_record_field_{$id}.id) ";
1049  $joinStr .= "LEFT JOIN object_data AS sort_object_data_{$id} ON (sort_object_data_{$id}.obj_id = sort_stloc_{$id}.value) ";
1050  $selectStr .= " sort_object_data_{$id}.title AS field_{$id},";
1051  break;
1053  $prop = $sortField->getPropertyvalues();
1054  $refField = ilDataCollectionCache::getFieldCache($sortField->getFieldRef());
1055  $selectStr .= "stloc_{$id}_joined.value AS field_{$id},";
1056  $joinStr .= "LEFT JOIN il_dcl_record_field AS record_field_{$id} ON (record_field_{$id}.record_id = record.id AND record_field_{$id}.field_id = " . $ilDB->quote($sortField->getId(), 'integer') .") ";
1057  $joinStr .= "LEFT JOIN il_dcl_stloc{$stl}_value AS stloc_{$id} ON (stloc_{$id}.record_field_id = record_field_{$id}.id) ";
1058  $joinStr .= "LEFT JOIN il_dcl_record_field AS record_field_{$id}_joined ON (record_field_{$id}_joined.record_id = stloc_{$id}.value AND record_field_{$id}_joined.field_id = " . $ilDB->quote($refField->getId(), 'integer') .") ";
1059  $joinStr .= "LEFT JOIN il_dcl_stloc{$refField->getStorageLocation()}_value AS stloc_{$id}_joined ON (stloc_{$id}_joined.record_field_id = record_field_{$id}_joined.id) ";
1060  break;
1065  $selectStr .= " sort_stloc_{$id}.value AS field_{$id},";
1066  $joinStr .= "LEFT JOIN il_dcl_record_field AS sort_record_field_{$id} ON (sort_record_field_{$id}.record_id = record.id AND sort_record_field_{$id}.field_id = " . $ilDB->quote($sortField->getId(), 'integer') .") ";
1067  $joinStr .= "LEFT JOIN il_dcl_stloc{$stl}_value AS sort_stloc_{$id} ON (sort_stloc_{$id}.record_field_id = sort_record_field_{$id}.id) ";
1068  break;
1069  }
1070  }
1071 
1072  if(count($filter)) {
1073  foreach($filter as $key => $filter_value)
1074  {
1075  $filter_field_id = substr($key, 7);
1076  $filterField = $this->getField($filter_field_id);
1077  switch ($filterField->getDatatypeId()) {
1079  $joinStr .= "INNER JOIN (SELECT AVG(avg_rating.rating) AS avg_rating, avg_rating.obj_id AS obj_id FROM il_rating as avg_rating WHERE avg_rating.sub_obj_id = {$filter_field_id} GROUP BY avg_rating.obj_id) AS avg_rating on avg_rating.avg_rating >= ".$ilDB->quote($filter_value, 'integer') ." AND avg_rating.obj_id = record.id ";
1080  break;
1082  $joinStr .= "INNER JOIN il_dcl_record_field AS filter_record_field_{$filter_field_id} ON (filter_record_field_{$filter_field_id}.record_id = record.id AND filter_record_field_{$filter_field_id}.field_id = " . $ilDB->quote($filter_field_id, 'integer') .") ";
1083  $joinStr .= "INNER JOIN il_dcl_stloc{$filterField->getStorageLocation()}_value AS filter_stloc_{$filter_field_id} ON (filter_stloc_{$filter_field_id}.record_field_id = filter_record_field_{$filter_field_id}.id) ";
1084  $joinStr .= "INNER JOIN object_reference AS filter_object_reference_{$filter_field_id} ON (filter_object_reference_{$filter_field_id}.ref_id = filter_stloc_{$filter_field_id}.value ) ";
1085  $joinStr .= "INNER JOIN object_data AS filter_object_data_{$filter_field_id} ON (filter_object_data_{$filter_field_id}.obj_id = filter_object_reference_{$filter_field_id}.obj_id AND filter_object_data_{$filter_field_id}.title LIKE " . $ilDB->quote("%$filter_value%", 'text') .") ";
1086  break;
1089  $joinStr .= "INNER JOIN il_dcl_record_field AS filter_record_field_{$filter_field_id} ON (filter_record_field_{$filter_field_id}.record_id = record.id AND filter_record_field_{$filter_field_id}.field_id = " . $ilDB->quote($filter_field_id, 'integer') .") ";
1090  $joinStr .= "INNER JOIN il_dcl_stloc{$filterField->getStorageLocation()}_value AS filter_stloc_{$filter_field_id} ON (filter_stloc_{$filter_field_id}.record_field_id = filter_record_field_{$filter_field_id}.id) ";
1091  $joinStr .= "INNER JOIN object_data AS filter_object_data_{$filter_field_id} ON (filter_object_data_{$filter_field_id}.obj_id = filter_stloc_{$filter_field_id}.value AND filter_object_data_{$filter_field_id}.title LIKE " . $ilDB->quote("%$filter_value%", 'text') .") ";
1092  break;
1094  $dateFrom = (isset($filter_value['from']) && is_object($filter_value['from'])) ? $filter_value['from'] : null;
1095  $dateTo = (isset($filter_value['to']) && is_object($filter_value['to'])) ? $filter_value['to'] : null;
1096  if ($filterField->isStandardField()) {
1097  if ($dateFrom) $where_additions .= " AND (record.{$filter_field_id} >= " . $ilDB->quote($dateFrom, 'date') . ")";
1098  if ($dateTo) $where_additions .= " AND (record.{$filter_field_id} <= " . $ilDB->quote($dateTo, 'date') . ")";
1099  } else {
1100  $joinStr .= "INNER JOIN il_dcl_record_field AS filter_record_field_{$filter_field_id} ON (filter_record_field_{$filter_field_id}.record_id = record.id AND filter_record_field_{$filter_field_id}.field_id = " . $ilDB->quote($filter_field_id, 'integer') .") ";
1101  $joinStr .= "INNER JOIN il_dcl_stloc{$filterField->getStorageLocation()}_value AS filter_stloc_{$filter_field_id} ON (filter_stloc_{$filter_field_id}.record_field_id = filter_record_field_{$filter_field_id}.id ";
1102  if ($dateFrom) $joinStr .= "AND filter_stloc_{$filter_field_id}.value >= " . $ilDB->quote($dateFrom, 'date') . " ";
1103  if ($dateTo) $joinStr .= "AND filter_stloc_{$filter_field_id}.value <= " . $ilDB->quote($dateTo, 'date') . " ";
1104  $joinStr .= ") ";
1105  }
1106  break;
1108  $from = (isset($filter_value['from'])) ? (int) $filter_value['from'] : null;
1109  $to = (isset($filter_value['to'])) ? (int) $filter_value['to'] : null;
1110  if ($filterField->isStandardField()) {
1111  if (!is_null($from)) $where_additions .= " AND record.{$filter_field_id} >= " . $ilDB->quote($from, 'integer');
1112  if (!is_null($to)) $where_additions .= " AND record.{$filter_field_id} <= " . $ilDB->quote($to, 'integer');
1113  } else {
1114  $joinStr .= "INNER JOIN il_dcl_record_field AS filter_record_field_{$filter_field_id} ON (filter_record_field_{$filter_field_id}.record_id = record.id AND filter_record_field_{$filter_field_id}.field_id = " . $ilDB->quote($filter_field_id, 'integer') .") ";
1115  $joinStr .= "INNER JOIN il_dcl_stloc{$filterField->getStorageLocation()}_value AS filter_stloc_{$filter_field_id} ON (filter_stloc_{$filter_field_id}.record_field_id = filter_record_field_{$filter_field_id}.id";
1116  if (!is_null($from)) $joinStr .= " AND filter_stloc_{$filter_field_id}.value >= " . $ilDB->quote($from, 'integer');
1117  if (!is_null($to)) $joinStr .= " AND filter_stloc_{$filter_field_id}.value <= " . $ilDB->quote($to, 'integer');
1118  $joinStr .= ") ";
1119  }
1120  break;
1122  if($filter_value == "checked") {
1123  $joinStr .= "INNER JOIN il_dcl_record_field AS filter_record_field_{$filter_field_id} ON (filter_record_field_{$filter_field_id}.record_id = record.id AND filter_record_field_{$filter_field_id}.field_id = " . $ilDB->quote($filter_field_id, 'integer') .") ";
1124  $joinStr .= "INNER JOIN il_dcl_stloc{$filterField->getStorageLocation()}_value AS filter_stloc_{$filter_field_id} ON (filter_stloc_{$filter_field_id}.record_field_id = filter_record_field_{$filter_field_id}.id";
1125  $joinStr .= " AND filter_stloc_{$filter_field_id}.value = " . $ilDB->quote(1, 'integer');
1126  } else {
1127  $joinStr .= "INNER JOIN il_dcl_record_field AS filter_record_field_{$filter_field_id} ON (filter_record_field_{$filter_field_id}.record_id = record.id AND filter_record_field_{$filter_field_id}.field_id = " . $ilDB->quote($filter_field_id, 'integer') .") ";
1128  $joinStr .= "LEFT JOIN il_dcl_stloc{$filterField->getStorageLocation()}_value AS filter_stloc_{$filter_field_id} ON (filter_stloc_{$filter_field_id}.record_field_id = filter_record_field_{$filter_field_id}.id";
1129  $where_additions .= " AND (filter_stloc_{$filter_field_id}.value <> " . $ilDB->quote(1, 'integer')." OR filter_stloc_{$filter_field_id}.value is NULL)";
1130  }
1131  $joinStr .= " ) ";
1132  break;
1134  if ($filterField->isStandardField()) {
1135  $joinStr .= "INNER JOIN usr_data AS filter_usr_data_{$filter_field_id} ON (filter_usr_data_{$filter_field_id}.usr_id = record.{$filter_field_id} AND filter_usr_data_{$filter_field_id}.login LIKE " . $ilDB->quote("%$filter_value%", 'text') .") ";
1136  } else {
1137  $joinStr .= " INNER JOIN il_dcl_record_field AS filter_record_field_{$filter_field_id} ON (filter_record_field_{$filter_field_id}.record_id = record.id AND filter_record_field_{$filter_field_id}.field_id = " . $ilDB->quote($filter_field_id, 'integer') .") ";
1138  $joinStr .= " INNER JOIN il_dcl_stloc{$filterField->getStorageLocation()}_value AS filter_stloc_{$filter_field_id} ON (filter_stloc_{$filter_field_id}.record_field_id = filter_record_field_{$filter_field_id}.id AND filter_stloc_{$filter_field_id}.value LIKE " . $ilDB->quote("%$filter_value%", 'text') .") ";
1139  }
1140  break;
1142  $joinStr .= " INNER JOIN il_dcl_record_field AS filter_record_field_{$filter_field_id} ON (filter_record_field_{$filter_field_id}.record_id = record.id AND filter_record_field_{$filter_field_id}.field_id = " . $ilDB->quote($filter_field_id, 'integer') .") ";
1143  $joinStr .= " INNER JOIN il_dcl_stloc{$filterField->getStorageLocation()}_value AS filter_stloc_{$filter_field_id} ON (filter_stloc_{$filter_field_id}.record_field_id = filter_record_field_{$filter_field_id}.id AND filter_stloc_{$filter_field_id}.value = " . $ilDB->quote($filter_value, 'integer') .") ";
1144  break;
1145  }
1146  }
1147  }
1148 
1149  // Build the query string
1150  $sql = "SELECT DISTINCT record.id, ";
1151  $sql .= rtrim($selectStr, ',') . " FROM il_dcl_record AS record ";
1152  $sql .= $joinStr;
1153  $sql .= " WHERE record.table_id = " . $ilDB->quote($this->getId(), 'integer') . $where_additions;
1154  $sql .= " ORDER BY field_{$id} {$direction}";
1155 // echo $sql;die();
1156  $set = $ilDB->query($sql);
1157  $totalRecordIds = array();
1158  while ($rec = $ilDB->fetchAssoc($set)) {
1159  $totalRecordIds[] = $rec['id'];
1160  }
1161  // Now slice the array to load only the needed records in memory
1162  $recordIds = array_slice($totalRecordIds, $offset, $limit);
1163  $records = array();
1164  foreach ($recordIds as $id) {
1166  }
1167 
1168  return array('records' => $records, 'total' => count($totalRecordIds));
1169  }
1170 }
1171 
1172 
1173 
1174 ?>