ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilDclBaseRecordModel.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once './Modules/DataCollection/classes/Fields/Base/class.ilDclBaseRecordFieldModel.php';
5 require_once './Modules/DataCollection/classes/Fields/Base/class.ilDclDatatype.php';
6 require_once './Services/Exceptions/classes/class.ilException.php';
7 require_once './Services/User/classes/class.ilUserUtil.php';
8 
22 {
23 
27  protected $recordfields;
31  protected $id;
35  protected $table_id;
39  protected $table;
45  protected $last_edit_by;
49  protected $owner;
53  protected $last_update;
57  protected $create_date;
61  protected $comments;
62 
63 
67  public function __construct($a_id = 0)
68  {
69  if ($a_id != 0) {
70  $this->id = $a_id;
71  $this->doRead();
72  }
73  }
74 
75 
80  private function fixDate($value)
81  {
82  global $DIC;
83  if ($DIC['ilDB']->getDBType() != ilDBConstants::TYPE_ORACLE) {
84  return $value;
85  }
86 
87  $date = explode(' ', $value);
88 
89  return $date[0];
90  }
91 
92 
96  public function doUpdate($omit_notification = false)
97  {
98  global $DIC;
99  $ilDB = $DIC['ilDB'];
100 
101  $values = array(
102  "table_id" => array(
103  "integer",
104  $this->getTableId()
105  ),
106  "last_update" => array(
107  "date",
108  $this->fixDate($this->getLastUpdate())
109  ),
110  "owner" => array(
111  "text",
112  $this->getOwner()
113  ),
114  "last_edit_by" => array(
115  "text",
116  $this->getLastEditBy()
117  )
118  );
119  $ilDB->update("il_dcl_record", $values, array(
120  "id" => array(
121  "integer",
122  $this->id
123  )
124  ));
125 
126  foreach ($this->getRecordFields() as $recordfield) {
127  $recordfield->doUpdate();
128  }
129 
130  //TODO: add event raise
131  if (!$omit_notification) {
132  ilObjDataCollection::sendNotification("update_record", $this->getTableId(), $this->id);
133  }
134  }
135 
136 
140  public function doRead()
141  {
142  global $DIC;
143  $ilDB = $DIC['ilDB'];
144  //build query
145  $query = "Select * From il_dcl_record WHERE id = " . $ilDB->quote($this->getId(), "integer") . " ORDER BY id";
146 
147  $set = $ilDB->query($query);
148  $rec = $ilDB->fetchAssoc($set);
149 
150  $this->setTableId($rec["table_id"]);
151  $this->setCreateDate($rec["create_date"]);
152  $this->setLastUpdate($rec["last_update"]);
153  $this->setOwner($rec["owner"]);
154  $this->setLastEditBy($rec["last_edit_by"]);
155  }
156 
157 
161  public function doCreate()
162  {
163  global $DIC;
164  $ilDB = $DIC['ilDB'];
165 
166  if (!ilDclTable::_tableExists($this->getTableId())) {
167  throw new ilException("The field does not have a related table!");
168  }
169 
170  $id = $ilDB->nextId("il_dcl_record");
171  $this->setId($id);
172  $query = "INSERT INTO il_dcl_record (
173  id,
174  table_id,
175  create_date,
176  Last_update,
177  owner,
178  last_edit_by
179  ) VALUES (" . $ilDB->quote($this->getId(), "integer") . "," . $ilDB->quote($this->getTableId(), "integer") . ","
180  . $ilDB->quote($this->getCreateDate(), "timestamp") . "," . $ilDB->quote($this->getLastUpdate(), "timestamp") . ","
181  . $ilDB->quote($this->getOwner(), "integer") . "," . $ilDB->quote($this->getLastEditBy(), "integer") . "
182  )";
183  $ilDB->manipulate($query);
184 
185  $this->loadRecordFields();
186  foreach ($this->getRecordFields() as $recordField) {
187  $recordField->doCreate();
188  }
189 
190  $this->getTable()->loadRecords();
191  }
192 
193 
197  public function deleteField($field_id)
198  {
199  $this->loadRecordFields();
200  $this->recordfields[$field_id]->delete();
201  if (count($this->recordfields) == 1) {
202  $this->doDelete();
203  }
204  }
205 
206 
212  public function setId($a_id)
213  {
214  $this->id = $a_id;
215  }
216 
217 
223  public function getId()
224  {
225  return $this->id;
226  }
227 
228 
234  public function setTableId($a_id)
235  {
236  $this->table_id = $a_id;
237  }
238 
239 
245  public function getTableId()
246  {
247  return $this->table_id;
248  }
249 
250 
256  public function setCreateDate($a_datetime)
257  {
258  $this->create_date = $a_datetime;
259  }
260 
261 
267  public function getCreateDate()
268  {
269  return $this->create_date;
270  }
271 
272 
278  public function setLastUpdate($a_datetime)
279  {
280  $this->last_update = $a_datetime;
281  }
282 
283 
289  public function getLastUpdate()
290  {
291  return $this->last_update;
292  }
293 
294 
300  public function setOwner($a_id)
301  {
302  $this->owner = $a_id;
303  }
304 
305 
311  public function getOwner()
312  {
313  return $this->owner;
314  }
315 
316 
317  /*
318  * getLastEditBy
319  */
320  public function getLastEditBy()
321  {
322  return $this->last_edit_by;
323  }
324 
325 
326  /*
327  * setLastEditBy
328  */
329  public function setLastEditBy($last_edit_by)
330  {
331  $this->last_edit_by = $last_edit_by;
332  }
333 
334 
341  public function setRecordFieldValue($field_id, $value)
342  {
343  $this->loadRecordFields();
344  if (ilDclStandardField::_isStandardField($field_id)) {
345  $this->setStandardField($field_id, $value);
346  } else {
347  $this->loadTable();
348  $record_field = $this->recordfields[$field_id];
349 
350 
351  $this->recordfields[$field_id]->setValue($value);
352  }
353  }
354 
361  public function setRecordFieldValueFromForm($field_id, &$form)
362  {
363  $this->loadRecordFields();
364  if (ilDclStandardField::_isStandardField($field_id)) {
365  $this->setStandardFieldFromForm($field_id, $form);
366  } else {
367  $this->loadTable();
368  $this->recordfields[$field_id]->setValueFromForm($form);
369  }
370  }
371 
379  public function getRecordFieldValueFromExcel($excel, $row, $col, $field)
380  {
381  $this->loadRecordFields();
382  return $this->recordfields[$field->getId()]->getValueFromExcel($excel, $row, $col);
383  }
384 
385 
392  public function setStandardFieldValueFromExcel($excel, $row, $col, $field)
393  {
394  $value = $field->getValueFromExcel($excel, $row, $col);
395  if ($value) {
396  $this->{$field->getId()} = $value;
397  }
398  }
399 
404  public function getRecordFieldValues()
405  {
406  $this->loadRecordFields();
407  $return = array();
408  foreach ($this->recordfields as $id => $record_field) {
409  $return[$id] = $record_field->getValue();
410  }
411 
412  return $return;
413  }
414 
422  public function getRecordFieldValue($field_id)
423  {
424  if ($field_id === null) {
425  return null;
426  }
427  $this->loadRecordFields();
428  if (ilDclStandardField::_isStandardField($field_id)) {
429  return $this->getStandardField($field_id);
430  } else {
431  return $this->recordfields[$field_id]->getValue();
432  }
433  }
434 
435 
443  public function getRecordFieldRepresentationValue($field_id)
444  {
445  if ($field_id === null) {
446  return null;
447  }
448  $this->loadRecordFields();
449  if (ilDclStandardField::_isStandardField($field_id)) {
450  return $this->getStandardField($field_id);
451  } else {
452  return $this->recordfields[$field_id]->getValueForRepresentation();
453  }
454  }
455 
456 
464  public function getRecordFieldExportValue($field_id)
465  {
466  $this->loadRecordFields();
467  if (ilDclStandardField::_isStandardField($field_id)) {
468  return $this->getStandardFieldHTML($field_id);
469  } else {
470  return $this->recordfields[$field_id]->getExportValue();
471  }
472  }
473 
481  public function getRecordFieldPlainText($field_id)
482  {
483  $this->loadRecordFields();
484  if (ilDclStandardField::_isStandardField($field_id)) {
485  return $this->getStandardFieldHTML($field_id);
486  } else {
487  return $this->recordfields[$field_id]->getPlainText();
488  }
489  }
490 
497  public function fillRecordFieldExcelExport(ilExcel $worksheet, &$row, &$col, $field_id)
498  {
499  $this->loadRecordFields();
500  if (ilDclStandardField::_isStandardField($field_id)) {
501  if ($field_id == 'owner') {
502  $worksheet->setCell($row, $col, ilObjUser::_lookupLogin($this->getOwner()));
503  $col++;
504  $name_array = ilObjUser::_lookupName($this->getOwner());
505  $worksheet->setCell($row, $col, $name_array['lastname'] . ', ' . $name_array['firstname']);
506  $col++;
507  } else {
508  $worksheet->setCell($row, $col, $this->getStandardFieldHTML($field_id));
509  $col++;
510  }
511  } else {
512  $this->recordfields[$field_id]->fillExcelExport($worksheet, $row, $col);
513  }
514  }
515 
522  public function getRecordFieldHTML($field_id, array $options = array())
523  {
524  $this->loadRecordFields();
525  if (ilDclStandardField::_isStandardField($field_id)) {
526  $html = $this->getStandardFieldHTML($field_id, $options);
527  } else {
528  if (is_object($this->recordfields[$field_id])) {
529  $html = $this->recordfields[$field_id]->getRecordRepresentation()->getHTML();
530  } else {
531  $html = '';
532  }
533  }
534 
535  // This is a workaround as templating in ILIAS currently has some issues with curly brackets.see: http://www.ilias.de/mantis/view.php?id=12681#bugnotes
536  // SW 16.07.2014 Uncommented again, as some fields are outputting javascript that was broken due to entity encode the curly brackets
537  // $html = str_ireplace("{", "&#123;", $html);
538  // $html = str_ireplace("}", "&#125;", $html);
539 
540  return $html;
541  }
542 
543 
550  public function getRecordFieldSortingValue($field_id, array $options = array())
551  {
552  $this->loadRecordFields();
553  if (ilDclStandardField::_isStandardField($field_id)) {
554  $html = $this->getStandardFieldHTML($field_id, $options);
555  } else {
556  if (is_object($this->recordfields[$field_id])) {
557  $html = $this->recordfields[$field_id]->getSortingValue();
558  } else {
559  $html = '';
560  }
561  }
562 
563  // This is a workaround as templating in ILIAS currently has some issues with curly brackets.see: http://www.ilias.de/mantis/view.php?id=12681#bugnotes
564  // SW 16.07.2014 Uncommented again, as some fields are outputting javascript that was broken due to entity encode the curly brackets
565  // $html = str_ireplace("{", "&#123;", $html);
566  // $html = str_ireplace("}", "&#125;", $html);
567 
568  return $html;
569  }
570 
577  public function getRecordFieldSingleHTML($field_id, array $options = array())
578  {
579  $this->loadRecordFields();
580 
581  if (ilDclStandardField::_isStandardField($field_id)) {
582  $html = $this->getStandardFieldHTML($field_id);
583  } else {
584  $field = $this->recordfields[$field_id];
589  $html = $field->getRecordRepresentation()->getSingleHTML($options, false);
590  }
591  // This is a workaround as templating in ILIAS currently has some issues with curly brackets.see: http://www.ilias.de/mantis/view.php?id=12681#bugnotes
592  // SW 14.10.2015 Uncommented again, as some fields are outputting javascript that was broken due to entity encode the curly brackets
593  // $html = str_ireplace("{", "&#123;", $html);
594  // $html = str_ireplace("}", "&#125;", $html);
595 
596  return $html;
597  }
598 
603  public function fillRecordFieldFormInput($field_id, &$form)
604  {
605  $this->loadRecordFields();
606  if (ilDclStandardField::_isStandardField($field_id)) {
607  $this->fillStandardFieldFormInput($field_id, $form);
608  } else {
609  $this->recordfields[$field_id]->getRecordRepresentation()->fillFormInput($form);
610  }
611  }
612 
613 
618  protected function setStandardFieldFromForm($field_id, &$form)
619  {
620  if ($item = $form->getItemByPostVar("field_" . $field_id)) {
621  $this->setStandardField($item->getValue());
622  }
623  }
624 
629  protected function setStandardField($field_id, $value)
630  {
631  switch ($field_id) {
632  case "last_edit_by":
633  $this->setLastEditBy($value);
634 
635  return;
636  }
637  $this->$field_id = $value;
638  }
639 
644  protected function fillStandardFieldFormInput($field_id, &$form)
645  {
646  if ($item = $form->getItemByPostVar('field_' . $field_id)) {
647  $item->setValue($this->getStandardField($field_id));
648  }
649  }
650 
651 
657  protected function getStandardField($field_id)
658  {
659  switch ($field_id) {
660  case "last_edit_by":
661  return $this->getLastEditBy();
662  break;
663  case 'owner':
664  $usr_data = ilObjUser::_lookupName($this->getOwner());
665 
666  return $usr_data['login'];
667  break;
668  }
669 
670  return $this->$field_id;
671  }
672 
673 
680  public function getStandardFieldHTML($field_id, array $options = array())
681  {
682  switch ($field_id) {
683  case 'id':
684  return $this->getId();
685  case 'owner':
686  return ilUserUtil::getNamePresentation($this->getOwner());
687  case 'last_edit_by':
689  case 'last_update':
691  case 'create_date':
693  case 'comments':
694  $nComments = count($this->getComments());
695  $ajax_hash = ilCommonActionDispatcherGUI::buildAjaxHash(1, $_GET['ref_id'], 'dcl', $this->table->getCollectionObject()
696  ->getId(), 'dcl', $this->getId());
697  $ajax_link = ilNoteGUI::getListCommentsJSCall($ajax_hash, '');
698 
699  return "<a class='dcl_comment' href='#' onclick=\"return " . $ajax_link . "\">
700  <img src='" . ilUtil::getImagePath("comment_unlabeled.svg")
701  . "' alt='{$nComments} Comments'><span class='ilHActProp'>{$nComments}</span></a>";
702  }
703  }
704 
710  public function getStandardFieldPlainText($field_id)
711  {
712  switch ($field_id) {
713  case 'comments':
714  return count(ilNote::_getNotesOfObject(
715  $this->table->getCollectionObject()->getId(),
716  $this->getId(),
717  "dcl",
718  2,
719  false,
720  "",
721  "y",
722  true,
723  true
724  ));
725  default:
726  return strip_tags($this->getStandardFieldHTML($field_id));
727  }
728  }
729 
730 
734  private function loadRecordFields()
735  {
736  if ($this->recordfields == null) {
737  $this->loadTable();
738  $recordfields = array();
739  foreach ($this->table->getRecordFields() as $field) {
740  if ($recordfields[$field->getId()] == null) {
741  $recordfields[$field->getId()] = ilDclCache::getRecordFieldCache($this, $field);
742  }
743  }
744 
745  $this->recordfields = $recordfields;
746  }
747  }
748 
749 
753  private function loadTable()
754  {
755  if ($this->table == null) {
756  $this->table = ilDclCache::getTableCache($this->getTableId());
757  }
758  }
759 
760 
766  public function getRecordField($field_id)
767  {
768  $this->loadRecordFields();
769 
770  return $this->recordfields[$field_id];
771  }
772 
773 
779  public function doDelete($omit_notification = false)
780  {
781  global $DIC;
782  $ilDB = $DIC['ilDB'];
783  $ilAppEventHandler = $DIC['ilAppEventHandler'];
784 
785  $this->loadRecordFields();
786  foreach ($this->recordfields as $recordfield) {
787  if ($recordfield->getField()->getDatatypeId() == ilDclDatatype::INPUTFORMAT_FILE) {
788  $this->deleteFile($recordfield->getValue());
789  }
790 
791  if ($recordfield->getField()->getDatatypeId() == ilDclDatatype::INPUTFORMAT_MOB) {
792  $this->deleteMob($recordfield->getValue());
793  }
794 
795  $recordfield->delete();
796  }
797 
798  $query = "DELETE FROM il_dcl_record WHERE id = " . $ilDB->quote($this->getId(), "integer");
799  $ilDB->manipulate($query);
800 
801  $this->table->loadRecords();
802 
803  if (!$omit_notification) {
804  ilObjDataCollection::sendNotification("delete_record", $this->getTableId(), $this->getId());
805 
806  $ilAppEventHandler->raise(
807  'Modules/DataCollection',
808  'deleteRecord',
809  array(
810  'dcl' => ilDclCache::getTableCache($this->getTableId())->getCollectionObject(),
811  'table_id' => $this->table_id,
812  'record_id' => $this->getId(),
813  'record' => $this,
814  )
815  );
816  }
817  }
818 
819 
820  // TODO: Find better way to copy data (including all references)
825  public function cloneStructure($original_id, $new_fields)
826  {
827  $original = ilDclCache::getRecordCache($original_id);
828  $this->setCreateDate($original->getCreateDate());
829  $this->setLastEditBy($original->getLastEditBy());
830  $this->setLastUpdate($original->getLastUpdate());
831  $this->setOwner($original->getOwner());
832  $this->doCreate();
833  foreach ($new_fields as $old => $new) {
834  $old_rec_field = $original->getRecordField($old);
835  $new_rec_field = ilDclCache::getRecordFieldCache($this, $new);
836  $new_rec_field->cloneStructure($old_rec_field);
837  $this->recordfields[] = $new_rec_field;
838  }
839 
840  // mandatory for all cloning functions
841  ilDclCache::setCloneOf($original_id, $this->getId(), ilDclCache::TYPE_RECORD);
842  }
843 
849  public function deleteFile($obj_id)
850  {
851  if (ilObject2::_exists($obj_id, false)) {
852  $file = new ilObjFile($obj_id, false);
853  $file->delete();
854  }
855  }
856 
857 
863  public function deleteMob($obj_id)
864  {
865  if (ilObject2::_lookupObjId($obj_id)) {
866  $mob = new ilObjMediaObject($obj_id);
867  $mob->delete();
868  }
869  }
870 
871 
877  public function passThroughFilter(array $filter)
878  {
879  $this->loadTable();
880  // If one field returns false, the whole record does not pass the filter #performance-improvements
881  foreach ($this->table->getFilterableFields() as $field) {
882  if (!isset($filter["filter_" . $field->getId()]) || !$filter["filter_" . $field->getId()]) {
883  continue;
884  }
885  if (!ilDclCache::getFieldRepresentation($field)->passThroughFilter($this, $filter["filter_" . $field->getId()])) {
886  return false;
887  }
888  }
889 
890  return true;
891  }
892 
893 
899  public function hasPermissionToEdit($ref_id)
900  {
901  return $this->getTable()->hasPermissionToEditRecord($ref_id, $this);
902  }
903 
904 
910  public function hasPermissionToDelete($ref_id)
911  {
912  return $this->getTable()->hasPermissionToDeleteRecord($ref_id, $this);
913  }
914 
915 
921  public function hasPermissionToView($ref_id)
922  {
923  return $this->getTable()->hasPermissionToViewRecord($ref_id, $this);
924  }
925 
926 
930  public function getRecordFields()
931  {
932  $this->loadRecordFields();
933 
934  return $this->recordfields;
935  }
936 
937 
941  public function getTable()
942  {
943  $this->loadTable();
944 
945  return $this->table;
946  }
947 
948 
954  public function getComments()
955  {
956  if ($this->comments === null) {
957  $this->comments = ilNote::_getNotesOfObject($this->table->getCollectionObject()->getId(), $this->getId(), 'dcl', IL_NOTE_PUBLIC);
958  }
959 
960  return $this->comments;
961  }
962 }
static _lookupLogin($a_user_id)
lookup login
static _lookupName($a_user_id)
lookup user name
static setCloneOf($old, $new, $type)
static _lookupObjId($a_id)
$worksheet
const IL_CAL_DATETIME
getCreateDate()
Get Creation Date.
getRecordFieldExportValue($field_id)
Get Field Export Value.
doDelete($omit_notification=false)
Delete.
global $DIC
Definition: saml.php:7
$_GET["client_id"]
setStandardFieldValueFromExcel($excel, $row, $col, $field)
getRecordFieldHTML($field_id, array $options=array())
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false)
Format a date public.
getStandardFieldHTML($field_id, array $options=array())
setRecordFieldValue($field_id, $value)
Set a field value.
static getTableCache($table_id=0)
setCreateDate($a_datetime)
Set Creation Date.
setLastUpdate($a_datetime)
Set Last Update Date.
loadRecordFields()
Load record fields.
static getFieldRepresentation(ilDclBaseFieldModel $field)
setStandardFieldFromForm($field_id, &$form)
cloneStructure($original_id, $new_fields)
if(isset($_POST['submit'])) $form
static _tableExists($table_id)
static buildAjaxHash($a_node_type, $a_node_id, $a_obj_type, $a_obj_id, $a_sub_type=null, $a_sub_id=null)
Build ajax hash.
const IL_NOTE_PUBLIC
Definition: class.ilNote.php:5
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
static getRecordCache($record_id=0)
Date and time handling
setCell($a_row, $a_col, $a_value, $a_datatype=null)
Set cell value.
Class ilObjMediaObject.
$query
static getNamePresentation( $a_user_id, $a_user_image=false, $a_profile_link=false, $a_profile_back_link="", $a_force_first_lastname=false, $a_omit_login=false, $a_sortable=true, $a_return_data_array=false, $a_ctrl_path="ilpublicuserprofilegui")
Default behaviour is:
$old
static _isStandardField($field_id)
Create styles array
The data for the language used.
fillRecordFieldExcelExport(ilExcel $worksheet, &$row, &$col, $field_id)
static getListCommentsJSCall($a_hash, $a_update_code=null)
Get list comments js call.
deleteFile($obj_id)
Delete a file.
getRecordFieldPlainText($field_id)
Get Field Export Value.
getLastUpdate()
Get Last Update Date.
static getRecordFieldCache($record, $field)
Class ilDclBaseRecordModel.
setRecordFieldValueFromForm($field_id, &$form)
Set a field value.
getRecordFieldValue($field_id)
Get Field Value.
getRecordFieldValueFromExcel($excel, $row, $col, $field)
getRecordFieldRepresentationValue($field_id)
Get Field Value for Representation in a Form.
global $ilDB
static _exists($a_id, $a_reference=false, $a_type=null)
doUpdate($omit_notification=false)
doUpdate
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
static _getNotesOfObject( $a_rep_obj_id, $a_obj_id, $a_obj_type, $a_type=IL_NOTE_PRIVATE, $a_incl_sub=false, $a_filter="", $a_all_public="y", $a_repository_mode=true, $a_sort_ascending=false)
get all notes related to a specific object
Add comments
$html
Definition: example_001.php:87
fillRecordFieldFormInput($field_id, &$form)
getRecordFieldSortingValue($field_id, array $options=array())
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20
fillStandardFieldFormInput($field_id, &$form)
getComments()
Get all comments of this record.