ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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
4require_once './Modules/DataCollection/classes/Fields/Base/class.ilDclBaseRecordFieldModel.php';
5require_once './Modules/DataCollection/classes/Fields/Base/class.ilDclDatatype.php';
6require_once './Services/Exceptions/classes/class.ilException.php';
7require_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
81 private function fixDate($value)
82 {
83 return $value;
84 }
85
86
90 public function doUpdate($omit_notification = false)
91 {
92 global $DIC;
93 $ilDB = $DIC['ilDB'];
94
95 $values = array(
96 "table_id" => array(
97 "integer",
98 $this->getTableId(),
99 ),
100 "last_update" => array(
101 "date",
102 $this->fixDate($this->getLastUpdate()),
103 ),
104 "owner" => array(
105 "text",
106 $this->getOwner(),
107 ),
108 "last_edit_by" => array(
109 "text",
110 $this->getLastEditBy(),
111 ),
112 );
113 $ilDB->update(
114 "il_dcl_record",
115 $values,
116 array(
117 "id" => array(
118 "integer",
119 $this->id,
120 ),
121 )
122 );
123
124 foreach ($this->getRecordFields() as $recordfield) {
125 $recordfield->doUpdate();
126 }
127
128 //TODO: add event raise
129 if (!$omit_notification) {
130 ilObjDataCollection::sendNotification("update_record", $this->getTableId(), $this->id);
131 }
132 }
133
134
138 public function doRead()
139 {
140 global $DIC;
141 $ilDB = $DIC['ilDB'];
142 //build query
143 $query = "Select * From il_dcl_record WHERE id = " . $ilDB->quote($this->getId(), "integer") . " ORDER BY id";
144
145 $set = $ilDB->query($query);
146 $rec = $ilDB->fetchAssoc($set);
147
148 $this->setTableId($rec["table_id"]);
149 $this->setCreateDate($rec["create_date"]);
150 $this->setLastUpdate($rec["last_update"]);
151 $this->setOwner($rec["owner"]);
152 $this->setLastEditBy($rec["last_edit_by"]);
153 }
154
155
159 public function doCreate()
160 {
161 global $DIC;
162 $ilDB = $DIC['ilDB'];
163
164 if (!ilDclTable::_tableExists($this->getTableId())) {
165 throw new ilException("The field does not have a related table!");
166 }
167
168 $id = $ilDB->nextId("il_dcl_record");
169 $this->setId($id);
170 $query
171 = "INSERT INTO il_dcl_record (
172 id,
173 table_id,
174 create_date,
175 Last_update,
176 owner,
177 last_edit_by
178 ) VALUES (" . $ilDB->quote($this->getId(), "integer") . "," . $ilDB->quote($this->getTableId(), "integer") . ","
179 . $ilDB->quote($this->getCreateDate(), "timestamp") . "," . $ilDB->quote($this->getLastUpdate(), "timestamp") . ","
180 . $ilDB->quote($this->getOwner(), "integer") . "," . $ilDB->quote($this->getLastEditBy(), "integer") . "
181 )";
182 $ilDB->manipulate($query);
183
184 $this->loadRecordFields();
185 foreach ($this->getRecordFields() as $recordField) {
186 $recordField->doCreate();
187 }
188
189 $this->getTable()->loadRecords();
190 }
191
192
196 public function deleteField($field_id)
197 {
198 $this->loadRecordFields();
199 $this->recordfields[$field_id]->delete();
200 if (count($this->recordfields) == 1) {
201 $this->doDelete();
202 }
203 }
204
205
211 public function setId($a_id)
212 {
213 $this->id = $a_id;
214 }
215
216
222 public function getId()
223 {
224 return $this->id;
225 }
226
227
233 public function setTableId($a_id)
234 {
235 $this->table_id = $a_id;
236 }
237
238
244 public function getTableId()
245 {
246 return $this->table_id;
247 }
248
249
255 public function setCreateDate($a_datetime)
256 {
257 $this->create_date = $a_datetime;
258 }
259
260
266 public function getCreateDate()
267 {
268 return $this->create_date;
269 }
270
271
277 public function setLastUpdate($a_datetime)
278 {
279 $this->last_update = $a_datetime;
280 }
281
282
288 public function getLastUpdate()
289 {
290 return $this->last_update;
291 }
292
293
299 public function setOwner($a_id)
300 {
301 $this->owner = $a_id;
302 }
303
304
310 public function getOwner()
311 {
312 return $this->owner;
313 }
314
315
316 /*
317 * getLastEditBy
318 */
319 public function getLastEditBy()
320 {
321 return $this->last_edit_by;
322 }
323
324
325 /*
326 * setLastEditBy
327 */
329 {
330 $this->last_edit_by = $last_edit_by;
331 }
332
333
340 public function setRecordFieldValue($field_id, $value)
341 {
342 $this->loadRecordFields();
344 $this->setStandardField($field_id, $value);
345 } else {
346 $this->loadTable();
347 $record_field = $this->recordfields[$field_id];
348
349 $this->recordfields[$field_id]->setValue($value);
350 }
351 }
352
353
360 public function setRecordFieldValueFromForm($field_id, &$form)
361 {
362 $this->loadRecordFields();
364 $this->setStandardFieldFromForm($field_id, $form);
365 } else {
366 $this->loadTable();
367 $this->recordfields[$field_id]->setValueFromForm($form);
368 }
369 }
370
371
380 public function getRecordFieldValueFromExcel($excel, $row, $col, $field)
381 {
382 $this->loadRecordFields();
383
384 return $this->recordfields[$field->getId()]->getValueFromExcel($excel, $row, $col);
385 }
386
387
394 public function setStandardFieldValueFromExcel($excel, $row, $col, $field)
395 {
396 $value = $field->getValueFromExcel($excel, $row, $col);
397 if ($value) {
398 $this->{$field->getId()} = $value;
399 }
400 }
401
402
407 public function getRecordFieldValues()
408 {
409 $this->loadRecordFields();
410 $return = array();
411 foreach ($this->recordfields as $id => $record_field) {
412 $return[$id] = $record_field->getValue();
413 }
414
415 return $return;
416 }
417
418
426 public function getRecordFieldValue($field_id)
427 {
428 if ($field_id === null) {
429 return null;
430 }
431 $this->loadRecordFields();
433 return $this->getStandardField($field_id);
434 } else {
435 return $this->recordfields[$field_id]->getValue();
436 }
437 }
438
439
447 public function getRecordFieldRepresentationValue($field_id)
448 {
449 if ($field_id === null) {
450 return null;
451 }
452 $this->loadRecordFields();
454 return $this->getStandardField($field_id);
455 } else {
456 return $this->recordfields[$field_id]->getValueForRepresentation();
457 }
458 }
459
460
468 public function getRecordFieldExportValue($field_id)
469 {
470 $this->loadRecordFields();
472 return $this->getStandardFieldHTML($field_id);
473 } else {
474 return $this->recordfields[$field_id]->getExportValue();
475 }
476 }
477
478
486 public function getRecordFieldPlainText($field_id)
487 {
488 $this->loadRecordFields();
490 return $this->getStandardFieldHTML($field_id);
491 } else {
492 return $this->recordfields[$field_id]->getPlainText();
493 }
494 }
495
496
503 public function fillRecordFieldExcelExport(ilExcel $worksheet, &$row, &$col, $field_id)
504 {
505 $this->loadRecordFields();
507 if ($field_id == 'owner') {
508 $worksheet->setCell($row, $col, ilObjUser::_lookupLogin($this->getOwner()));
509 $col++;
510 $name_array = ilObjUser::_lookupName($this->getOwner());
511 $worksheet->setCell($row, $col, $name_array['lastname'] . ', ' . $name_array['firstname']);
512 $col++;
513 } else {
514 $worksheet->setCell($row, $col, $this->getStandardFieldHTML($field_id));
515 $col++;
516 }
517 } else {
518 $this->recordfields[$field_id]->fillExcelExport($worksheet, $row, $col);
519 }
520 }
521
522
528 public function getRecordFieldFormulaValue($field_id)
529 {
530 $this->loadRecordFields();
532 $value = $this->getStandardFieldFormulaValue($field_id);
533 } else {
534 if (is_object($this->recordfields[$field_id])) {
535 $value = $this->recordfields[$field_id]->getFormulaValue();
536 } else {
537 $value = '';
538 }
539 }
540
541 return $value;
542 }
543
550 public function getRecordFieldHTML($field_id, array $options = array())
551 {
552 $this->loadRecordFields();
554 $html = $this->getStandardFieldHTML($field_id, $options);
555 } else {
556 if (is_object($this->recordfields[$field_id])) {
557 $html = $this->recordfields[$field_id]->getRecordRepresentation()->getHTML();
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
571
578 public function getRecordFieldSortingValue($field_id, array $options = array())
579 {
580 $this->loadRecordFields();
582 $html = $this->getStandardFieldHTML($field_id, $options);
583 } else {
584 if (is_object($this->recordfields[$field_id])) {
585 $html = $this->recordfields[$field_id]->getSortingValue();
586 } else {
587 $html = '';
588 }
589 }
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 16.07.2014 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
599
606 public function getRecordFieldSingleHTML($field_id, array $options = array())
607 {
608 $this->loadRecordFields();
609
611 $html = $this->getStandardFieldHTML($field_id);
612 } else {
613 $field = $this->recordfields[$field_id];
618 $html = $field->getRecordRepresentation()->getSingleHTML($options, false);
619 }
620 // 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
621 // SW 14.10.2015 Uncommented again, as some fields are outputting javascript that was broken due to entity encode the curly brackets
622 // $html = str_ireplace("{", "&#123;", $html);
623 // $html = str_ireplace("}", "&#125;", $html);
624
625 return $html;
626 }
627
628
633 public function fillRecordFieldFormInput($field_id, &$form)
634 {
635 $this->loadRecordFields();
637 $this->fillStandardFieldFormInput($field_id, $form);
638 } else {
639 $this->recordfields[$field_id]->getRecordRepresentation()->fillFormInput($form);
640 }
641 }
642
643
648 protected function setStandardFieldFromForm($field_id, &$form)
649 {
650 if ($item = $form->getItemByPostVar("field_" . $field_id)) {
651 $this->setStandardField($item->getValue());
652 }
653 }
654
655
660 protected function setStandardField($field_id, $value)
661 {
662 switch ($field_id) {
663 case "last_edit_by":
664 $this->setLastEditBy($value);
665
666 return;
667 }
668 $this->$field_id = $value;
669 }
670
671
676 protected function fillStandardFieldFormInput($field_id, &$form)
677 {
678 if ($item = $form->getItemByPostVar('field_' . $field_id)) {
679 $item->setValue($this->getStandardField($field_id));
680 }
681 }
682
683
689 protected function getStandardField($field_id)
690 {
691 switch ($field_id) {
692 case "last_edit_by":
693 return $this->getLastEditBy();
694 break;
695 case 'owner':
696 $usr_data = ilObjUser::_lookupName($this->getOwner());
697
698 return $usr_data['login'];
699 break;
700 }
701
702 return $this->$field_id;
703 }
704
705
711 public function getStandardFieldFormulaValue($field_id)
712 {
713 return $this->getStandardFieldHTML($field_id);
714 }
715
722 public function getStandardFieldHTML($field_id, array $options = array())
723 {
724 switch ($field_id) {
725 case 'id':
726 return $this->getId();
727 case 'owner':
729 case 'last_edit_by':
731 case 'last_update':
733 case 'create_date':
735 case 'comments':
736 $nComments = count($this->getComments());
738 1,
739 $_GET['ref_id'],
740 'dcl',
741 $this->table->getCollectionObject()
742 ->getId(),
743 'dcl',
744 $this->getId()
745 );
746 $ajax_link = ilNoteGUI::getListCommentsJSCall($ajax_hash, '');
747
748 return "<a class='dcl_comment' href='#' onclick=\"return " . $ajax_link . "\">
749 <img src='" . ilUtil::getImagePath("comment_unlabeled.svg")
750 . "' alt='{$nComments} Comments'><span class='ilHActProp'>{$nComments}</span></a>";
751 }
752 }
753
759 public function getStandardFieldPlainText($field_id)
760 {
761 switch ($field_id) {
762 case 'comments':
763 return count(ilNote::_getNotesOfObject(
764 $this->table->getCollectionObject()->getId(),
765 $this->getId(),
766 "dcl",
767 2,
768 false,
769 "",
770 "y",
771 true,
772 true
773 ));
774 default:
775 return strip_tags($this->getStandardFieldHTML($field_id));
776 }
777 }
778
779
783 private function loadRecordFields()
784 {
785 if ($this->recordfields == null) {
786 $this->loadTable();
787 $recordfields = array();
788 foreach ($this->table->getRecordFields() as $field) {
789 if ($recordfields[$field->getId()] == null) {
790 $recordfields[$field->getId()] = ilDclCache::getRecordFieldCache($this, $field);
791 }
792 }
793
794 $this->recordfields = $recordfields;
795 }
796 }
797
798
802 private function loadTable()
803 {
804 if ($this->table == null) {
805 $this->table = ilDclCache::getTableCache($this->getTableId());
806 }
807 }
808
809
815 public function getRecordField($field_id)
816 {
817 $this->loadRecordFields();
818
819 return $this->recordfields[$field_id];
820 }
821
822
828 public function doDelete($omit_notification = false)
829 {
830 global $DIC;
831 $ilDB = $DIC['ilDB'];
832 $ilAppEventHandler = $DIC['ilAppEventHandler'];
833
834 $this->loadRecordFields();
835 foreach ($this->recordfields as $recordfield) {
836 if ($recordfield->getField()->getDatatypeId() == ilDclDatatype::INPUTFORMAT_FILE) {
837 $this->deleteFile($recordfield->getValue());
838 }
839
840 if ($recordfield->getField()->getDatatypeId() == ilDclDatatype::INPUTFORMAT_MOB) {
841 $this->deleteMob($recordfield->getValue());
842 }
843
844 $recordfield->delete();
845 }
846
847 $query = "DELETE FROM il_dcl_record WHERE id = " . $ilDB->quote($this->getId(), "integer");
848 $ilDB->manipulate($query);
849
850 $this->table->loadRecords();
851
852 if (!$omit_notification) {
853 ilObjDataCollection::sendNotification("delete_record", $this->getTableId(), $this->getId());
854
855 $ilAppEventHandler->raise(
856 'Modules/DataCollection',
857 'deleteRecord',
858 array(
859 'dcl' => ilDclCache::getTableCache($this->getTableId())->getCollectionObject(),
860 'table_id' => $this->table_id,
861 'record_id' => $this->getId(),
862 'record' => $this,
863 )
864 );
865 }
866 }
867
868
869 // TODO: Find better way to copy data (including all references)
870
871
876 public function cloneStructure($original_id, $new_fields)
877 {
878 $original = ilDclCache::getRecordCache($original_id);
879 $this->setCreateDate($original->getCreateDate());
880 $this->setLastEditBy($original->getLastEditBy());
881 $this->setLastUpdate($original->getLastUpdate());
882 $this->setOwner($original->getOwner());
883 $this->doCreate();
884 foreach ($new_fields as $old => $new) {
885 $old_rec_field = $original->getRecordField($old);
886 $new_rec_field = ilDclCache::getRecordFieldCache($this, $new);
887 $new_rec_field->cloneStructure($old_rec_field);
888 $this->recordfields[] = $new_rec_field;
889 }
890
891 // mandatory for all cloning functions
893 }
894
895
901 public function deleteFile($obj_id)
902 {
903 if (ilObject2::_exists($obj_id, false)) {
904 $file = new ilObjFile($obj_id, false);
905 $file->delete();
906 }
907 }
908
909
915 public function deleteMob($obj_id)
916 {
917 if (ilObject2::_lookupObjId($obj_id)) {
918 $mob = new ilObjMediaObject($obj_id);
919 $mob->delete();
920 }
921 }
922
923
929 public function passThroughFilter(array $filter)
930 {
931 $this->loadTable();
932 // If one field returns false, the whole record does not pass the filter #performance-improvements
933 foreach ($this->table->getFilterableFields() as $field) {
934 if (!isset($filter["filter_" . $field->getId()]) || !$filter["filter_" . $field->getId()]) {
935 continue;
936 }
937 if (!ilDclCache::getFieldRepresentation($field)->passThroughFilter($this, $filter["filter_" . $field->getId()])) {
938 return false;
939 }
940 }
941
942 return true;
943 }
944
945
951 public function hasPermissionToEdit($ref_id)
952 {
953 return $this->getTable()->hasPermissionToEditRecord($ref_id, $this);
954 }
955
956
962 public function hasPermissionToDelete($ref_id)
963 {
964 return $this->getTable()->hasPermissionToDeleteRecord($ref_id, $this);
965 }
966
967
973 public function hasPermissionToView($ref_id)
974 {
975 return $this->getTable()->hasPermissionToViewRecord($ref_id, $this);
976 }
977
978
982 public function getRecordFields()
983 {
984 $this->loadRecordFields();
985
986 return $this->recordfields;
987 }
988
989
993 public function getTable()
994 {
995 $this->loadTable();
996
997 return $this->table;
998 }
999
1000
1006 public function getComments()
1007 {
1008 if ($this->comments === null) {
1009 $this->comments = ilNote::_getNotesOfObject($this->table->getCollectionObject()->getId(), $this->getId(), 'dcl', IL_NOTE_PUBLIC);
1010 }
1011
1012 return $this->comments;
1013 }
1014}
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_DATETIME
const IL_NOTE_PUBLIC
Definition: class.ilNote.php:6
static buildAjaxHash( $a_node_type, $a_node_id, $a_obj_type, $a_obj_id, $a_sub_type=null, $a_sub_id=null, $a_news_id=0)
Build ajax hash.
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date @access public.
@classDescription Date and time handling
Class ilDclBaseRecordModel.
setStandardFieldFromForm($field_id, &$form)
fillRecordFieldFormInput($field_id, &$form)
getComments()
Get all comments of this record.
loadRecordFields()
Load record fields.
fillStandardFieldFormInput($field_id, &$form)
setLastUpdate($a_datetime)
Set Last Update Date.
deleteFile($obj_id)
Delete a file.
getRecordFieldExportValue($field_id)
Get Field Export Value.
setRecordFieldValueFromForm($field_id, &$form)
Set a field value.
cloneStructure($original_id, $new_fields)
getRecordFieldPlainText($field_id)
Get Field Export Value.
setCreateDate($a_datetime)
Set Creation Date.
doDelete($omit_notification=false)
Delete.
doUpdate($omit_notification=false)
doUpdate
setStandardFieldValueFromExcel($excel, $row, $col, $field)
getRecordFieldSortingValue($field_id, array $options=array())
getRecordFieldValueFromExcel($excel, $row, $col, $field)
getRecordFieldValue($field_id)
Get Field Value.
getLastUpdate()
Get Last Update Date.
getStandardFieldHTML($field_id, array $options=array())
getRecordFieldHTML($field_id, array $options=array())
getRecordFieldRepresentationValue($field_id)
Get Field Value for Representation in a Form.
fillRecordFieldExcelExport(ilExcel $worksheet, &$row, &$col, $field_id)
setRecordFieldValue($field_id, $value)
Set a field value.
static getRecordFieldCache($record, $field)
static getTableCache($table_id=0)
static getRecordCache($record_id=0)
static setCloneOf($old, $new, $type)
static getFieldRepresentation(ilDclBaseFieldModel $field)
static _isStandardField($field_id)
static _tableExists($table_id)
setCell($a_row, $a_col, $a_value, $a_datatype=null)
Set cell value.
Base class for ILIAS Exception handling.
static getListCommentsJSCall($a_hash, $a_update_code=null)
Get list comments js call.
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, $a_news_id=0)
get all notes related to a specific object
Class ilObjFile.
Class ilObjMediaObject.
static _lookupLogin($a_user_id)
lookup login
static _lookupName($a_user_id)
lookup user name
static _lookupObjId($a_id)
static _exists($a_id, $a_reference=false, $a_type=null)
checks if an object exists in object_data@access public
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:
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
$html
Definition: example_001.php:87
$row
$query
if(isset($_POST['submit'])) $form
global $DIC
Definition: saml.php:7
global $ilDB
$values