ILIAS  release_8 Revision v8.24
class.ilDclBaseRecordModel.php
Go to the documentation of this file.
1<?php
2
20
22{
23 protected \ILIAS\UI\Factory $ui_factory;
24 protected \ILIAS\UI\Renderer $renderer;
25 protected Service $notes;
26
30 protected ?array $recordfields = null;
31 protected int $id = 0;
32 protected int $table_id;
33 protected ?ilDclTable $table = null;
34 protected ?int $last_edit_by = null;
35 protected int $owner = 0;
38 protected ?int $nr_of_comments = null;
41 protected ilDBInterface $db;
44
45 public function __construct(?int $a_id = 0)
46 {
47 global $DIC;
48 $this->db = $DIC->database();
49 $this->event = $DIC->event();
50 $this->user = $DIC->user();
51 $this->ui_factory = $DIC->ui()->factory();
52 $this->renderer = $DIC->ui()->renderer();
53
54 if ($a_id && $a_id != 0) {
55 $this->id = $a_id;
56 $this->doRead();
57 }
58
59 $this->notes = $DIC->notes();
60 $this->http = $DIC->http();
61 $this->refinery = $DIC->refinery();
62 }
63
64 public function doUpdate(bool $omit_notification = false): void
65 {
66 $values = [
67 "table_id" => [
68 "integer",
69 $this->getTableId(),
70 ],
71 "last_update" => [
72 "date",
73 $this->getLastUpdate()->get(IL_CAL_DATETIME),
74 ],
75 "owner" => [
76 "integer",
77 $this->getOwner(),
78 ],
79 "last_edit_by" => [
80 "integer",
81 $this->getLastEditBy(),
82 ],
83 ];
84 $this->db->update(
85 "il_dcl_record",
86 $values,
87 [
88 "id" => [
89 "integer",
90 $this->id,
91 ],
92 ]
93 );
94
95 foreach ($this->getRecordFields() as $recordfield) {
96 $recordfield->doUpdate();
97 }
98
99 //TODO: add event raise
100 if (!$omit_notification) {
101 $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
102 $objDataCollection = new ilObjDataCollection($ref_id);
103 $objDataCollection->sendNotification("update_record", $this->getTableId(), $this->id);
104 }
105 }
106
107 public function doRead(): void
108 {
109 //build query
110 $query = "Select * From il_dcl_record WHERE id = " . $this->db->quote($this->getId(), "integer") . " ORDER BY id";
111
112 $set = $this->db->query($query);
113 $rec = $this->db->fetchAssoc($set);
114
115 if (!$rec) {
116 $this->id = 0;
117 return;
118 }
119
120 $this->setTableId((int) $rec["table_id"]);
121 $this->loadTable();
122 if (null !== $rec["create_date"]) {
123 $this->setCreateDate(new ilDateTime($rec["create_date"], IL_CAL_DATETIME));
124 }
125 if (null !== $rec["last_update"]) {
126 $this->setLastUpdate(new ilDateTime($rec["last_update"], IL_CAL_DATETIME));
127 } else {
128 $this->setLastUpdate(new ilDateTime($rec["create_date"], IL_CAL_DATETIME));
129 }
130 $this->setOwner((int) $rec["owner"]);
131 if (null !== $rec["last_edit_by"]) {
132 $this->setLastEditBy((int) $rec["last_edit_by"]);
133 }
134 }
135
139 public function doCreate(): void
140 {
141 if (!ilDclTable::_tableExists($this->getTableId())) {
142 throw new ilException("The field does not have a related table!");
143 }
144
145 $id = $this->db->nextId("il_dcl_record");
146 $this->setId($id);
147 $query
148 = "INSERT INTO il_dcl_record (
149 id,
150 table_id,
151 create_date,
152 Last_update,
153 owner,
154 last_edit_by
155 ) VALUES (" . $this->db->quote($this->getId(), "integer") . "," . $this->db->quote(
156 $this->getTableId(),
157 "integer"
158 ) . ","
159 . $this->db->quote($this->getCreateDate()->get(IL_CAL_DATETIME), "timestamp") . "," . $this->db->quote(
160 $this->getLastUpdate()->get(IL_CAL_DATETIME),
161 "timestamp"
162 ) . ","
163 . $this->db->quote($this->getOwner(), "integer") . "," . $this->db->quote($this->getLastEditBy(), "integer") . "
164 )";
165 $this->db->manipulate($query);
166
167 $this->loadRecordFields();
168 foreach ($this->getRecordFields() as $recordField) {
169 $recordField->doCreate();
170 }
171
172 $this->getTable()->loadRecords();
173 }
174
175 public function deleteField(int $field_id): void
176 {
177 $this->loadRecordFields();
178 $this->recordfields[$field_id]->delete();
179 if (count($this->recordfields) == 1) {
180 $this->doDelete();
181 }
182 }
183
184 public function setId(int $a_id): void
185 {
186 $this->id = $a_id;
187 }
188
189 public function getId(): ?int
190 {
191 return $this->id;
192 }
193
194 public function setTableId(int $a_id): void
195 {
196 $this->table_id = $a_id;
197 }
198
199 public function getTableId(): int
200 {
201 return $this->table_id;
202 }
203
204 public function setCreateDate(ilDateTime $a_datetime): void
205 {
206 $this->create_date = $a_datetime;
207 }
208
209 public function getCreateDate(): ilDateTime
210 {
211 return $this->create_date;
212 }
213
214 public function setLastUpdate(ilDateTime $a_datetime): void
215 {
216 $this->last_update = $a_datetime;
217 }
218
219 public function getLastUpdate(): ilDateTime
220 {
221 return $this->last_update;
222 }
223
224 public function setOwner(int $a_id): void
225 {
226 $this->owner = $a_id;
227 }
228
229 public function getOwner(): int
230 {
231 return $this->owner;
232 }
233
234 public function getLastEditBy(): ?int
235 {
236 return $this->last_edit_by;
237 }
238
239 public function setLastEditBy(?int $last_edit_by): void
240 {
241 $this->last_edit_by = $last_edit_by;
242 }
243
249 public function setRecordFieldValue($field_id, $value): void
250 {
251 $this->loadRecordFields();
253 $this->setStandardField($field_id, $value);
254 } else {
255 $this->loadTable();
256 $this->recordfields[$field_id]->setValue($value);
257 }
258 }
259
264 public function setRecordFieldValueFromForm(int $field_id, ilPropertyFormGUI $form): void
265 {
266 $this->loadRecordFields();
268 $this->setStandardFieldFromForm($field_id, $form);
269 } else {
270 $this->loadTable();
271 $this->recordfields[$field_id]->setValueFromForm($form);
272 }
273 }
274
278 public function getRecordFieldValueFromExcel(ilExcel $excel, int $row, int $col, ilDclBaseFieldModel $field)
279 {
280 $this->loadRecordFields();
281
282 return $this->recordfields[$field->getId()]->getValueFromExcel($excel, $row, $col);
283 }
284
286 ilExcel $excel,
287 int $row,
288 int $col,
290 ): void {
291 $value = $field->getValueFromExcel($excel, $row, $col);
292 if ($value) {
293 $this->{$field->getId()} = $value;
294 }
295 }
296
297 public function getRecordFieldValues(): array
298 {
299 $this->loadRecordFields();
300 $return = [];
301 foreach ($this->recordfields as $id => $record_field) {
302 $return[$id] = $record_field->getValue();
303 }
304
305 return $return;
306 }
307
312 public function getRecordFieldValue(?int $field_id)
313 {
314 if ($field_id === null) {
315 return null;
316 }
317 $this->loadRecordFields();
319 return $this->getStandardField($field_id);
320 } else {
321 return $this->recordfields[$field_id]->getValue();
322 }
323 }
324
330 public function getRecordFieldRepresentationValue($field_id)
331 {
332 if ($field_id === null) {
333 return null;
334 }
335 $this->loadRecordFields();
337 return $this->getStandardField($field_id);
338 } else {
339 return $this->recordfields[$field_id]->getValueForRepresentation();
340 }
341 }
342
348 public function getRecordFieldExportValue($field_id)
349 {
350 $this->loadRecordFields();
352 return $this->getStandardFieldHTML($field_id);
353 } else {
354 return $this->recordfields[$field_id]->getExportValue();
355 }
356 }
357
363 public function getRecordFieldPlainText($field_id)
364 {
365 $this->loadRecordFields();
367 return $this->getStandardFieldHTML($field_id);
368 } else {
369 return $this->recordfields[$field_id]->getPlainText();
370 }
371 }
372
376 public function fillRecordFieldExcelExport(ilExcel $worksheet, int &$row, int &$col, $field_id): void
377 {
378 $this->loadRecordFields();
380 if ($field_id == 'owner') {
381 $worksheet->setCell($row, $col, ilObjUser::_lookupLogin($this->getOwner()));
382 $col++;
383 $name_array = ilObjUser::_lookupName($this->getOwner());
384 $worksheet->setCell($row, $col, $name_array['lastname'] . ', ' . $name_array['firstname']);
385 } elseif ('last_update') {
386 $date_time = $this->getLastUpdate()->get(IL_CAL_DATETIME, '', $this->user->getTimeZone());
387 $worksheet->setCell($row, $col, $date_time);
388 } elseif ('create_date') {
389 $date_time = $this->getCreateDate()->get(IL_CAL_DATETIME, '', $this->user->getTimeZone());
390 $worksheet->setCell($row, $col, $date_time);
391 } else {
392 $worksheet->setCell($row, $col, $this->getStandardFieldHTML($field_id));
393 }
394 $col++;
395 } else {
396 $this->recordfields[$field_id]->fillExcelExport($worksheet, $row, $col);
397 }
398 }
399
403 public function getRecordFieldFormulaValue($field_id): string
404 {
405 $this->loadRecordFields();
407 $value = $this->getStandardFieldFormulaValue($field_id);
408 } else {
409 if (is_object($this->recordfields[$field_id])) {
410 $value = $this->recordfields[$field_id]->getFormulaValue();
411 } else {
412 $value = '';
413 }
414 }
415
416 return $value;
417 }
418
422 public function getRecordFieldHTML($field_id, array $options = []): string
423 {
424 $this->loadRecordFields();
426 $html = $this->getStandardFieldHTML($field_id, $options);
427 } else {
428 if (array_key_exists($field_id, $this->recordfields) && is_object($this->recordfields[$field_id])) {
429 $html = $this->recordfields[$field_id]->getRecordRepresentation()->getHTML(true, $options);
430 } else {
431 $html = '';
432 }
433 }
434
435 return $html;
436 }
437
441 public function getRecordFieldSortingValue($field_id, array $options = []): string
442 {
443 $this->loadRecordFields();
445 $html = $this->getStandardFieldHTML($field_id, $options);
446 } else {
447 if (is_object($this->recordfields[$field_id])) {
448 $html = $this->recordfields[$field_id]->getSortingValue();
449 } else {
450 $html = '';
451 }
452 }
453
454 return $html;
455 }
456
460 public function getRecordFieldSingleHTML($field_id, array $options = []): string
461 {
462 $this->loadRecordFields();
463
465 $html = $this->getStandardFieldHTML($field_id);
466 } else {
467 $field = $this->recordfields[$field_id];
472 $html = $field->getRecordRepresentation()->getSingleHTML($options, false);
473 }
474
475 return $html;
476 }
477
481 public function fillRecordFieldFormInput($field_id, ilPropertyFormGUI $form): void
482 {
483 $this->loadRecordFields();
485 $this->fillStandardFieldFormInput($field_id, $form);
486 } else {
487 $this->recordfields[$field_id]->getRecordRepresentation()->fillFormInput($form);
488 }
489 }
490
494 protected function setStandardFieldFromForm($field_id, ilPropertyFormGUI $form): void
495 {
496 if ($item = $form->getItemByPostVar("field_" . $field_id)) {
497 $this->setStandardField($field_id, $item->getValue());
498 }
499 }
500
505 protected function setStandardField($field_id, $value)
506 {
507 if ($field_id == "last_edit_by") {
508 $this->setLastEditBy($value);
509 return;
510 }
511 $this->{$field_id} = $value;
512 }
513
517 protected function fillStandardFieldFormInput($field_id, ilPropertyFormGUI $form): void
518 {
519 if ($item = $form->getItemByPostVar('field_' . $field_id)) {
520 $item->setValue($this->getStandardField($field_id));
521 }
522 }
523
527 protected function getStandardField($field_id): string
528 {
529 switch ($field_id) {
530 case "last_edit_by":
531 return $this->getLastEditBy();
532 case 'owner':
533 $usr_data = ilObjUser::_lookupName($this->getOwner());
534 return $usr_data['login'];
535 }
536
537 return $this->{$field_id};
538 }
539
543 public function getStandardFieldFormulaValue($field_id): string
544 {
545 return $this->getStandardFieldHTML($field_id);
546 }
547
548 public function getStandardFieldHTML(string $field_id, array $options = []): string
549 {
550 switch ($field_id) {
551 case 'id':
552 return $this->getId();
553 case 'owner':
554 return ilUserUtil::getNamePresentation($this->getOwner());
555 case 'last_edit_by':
556 return ilUserUtil::getNamePresentation($this->getLastEditBy());
557 case 'last_update':
558 return ilDatePresentation::formatDate($this->getLastUpdate());
559 case 'create_date':
560 return ilDatePresentation::formatDate($this->getCreateDate());
561 case 'comments':
562
563 $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
564
566 1,
567 $ref_id,
568 'dcl',
569 $this->table->getCollectionObject()
570 ->getId(),
571 'dcl',
572 $this->getId()
573 );
574 $update_code = "il.UI.counter.getCounterObject($(\".ilc_page_Page\")).incrementStatusCount(1);";
575 $ajax_link = ilNoteGUI::getListCommentsJSCall($ajax_hash, $update_code);
576
577 $nr_comments = $this->getNrOfComments();
578
579 $comment_glyph = $this->ui_factory->symbol()->glyph()->comment()->withCounter(
580 $this->ui_factory->counter()->status($nr_comments)
581 )->withAdditionalOnLoadCode(function ($id) use ($ajax_link): string {
582 return "document.getElementById('$id').onclick = function (event) { $ajax_link; };";
583 });
584 return $this->renderer->render($comment_glyph);
585 }
586
587 return "";
588 }
589
594 public function getStandardFieldPlainText(string $field_id)
595 {
596 switch ($field_id) {
597 case 'comments':
598 return $this->getNrOfComments();
599 default:
600 return strip_tags($this->getStandardFieldHTML($field_id));
601 }
602 }
603
604 private function loadRecordFields(): void
605 {
606 if ($this->recordfields == null) {
607 $this->loadTable();
608 $recordfields = [];
609 foreach ($this->table->getRecordFields() as $field) {
610 if (($recordfields[$field->getId()] ?? null) === null) {
611 $recordfields[$field->getId()] = ilDclCache::getRecordFieldCache($this, $field);
612 }
613 }
614
615 $this->recordfields = $recordfields;
616 }
617 }
618
619 private function loadTable(): void
620 {
621 if ($this->table === null) {
622 $this->table = ilDclCache::getTableCache($this->getTableId());
623 }
624 }
625
626 public function getRecordField(int $field_id): ilDclBaseRecordFieldModel
627 {
628 $this->loadRecordFields();
629
630 return $this->recordfields[$field_id];
631 }
632
633 public function doDelete(bool $omit_notification = false): void
634 {
635 $this->loadRecordFields();
636 foreach ($this->recordfields as $recordfield) {
637 if ($recordfield->getField()->getDatatypeId() == ilDclDatatype::INPUTFORMAT_FILE) {
638 $this->deleteFile((int) $recordfield->getValue());
639 }
640
641 if ($recordfield->getField()->getDatatypeId() == ilDclDatatype::INPUTFORMAT_MOB) {
642 $this->deleteMob((int) $recordfield->getValue());
643 }
644
645 $recordfield->delete();
646 }
647
648 $query = "DELETE FROM il_dcl_record WHERE id = " . $this->db->quote($this->getId(), "integer");
649 $this->db->manipulate($query);
650
651 $this->table->loadRecords();
652
653 if (!$omit_notification) {
654 $ref_id = $this->http->wrapper()->query()->retrieve('ref_id', $this->refinery->kindlyTo()->int());
655 $objDataCollection = new ilObjDataCollection($ref_id);
656 $objDataCollection->sendNotification("delete_record", $this->getTableId(), $this->getId());
657
658 $this->event->raise(
659 'Modules/DataCollection',
660 'deleteRecord',
661 [
662 'dcl' => ilDclCache::getTableCache($this->getTableId())->getCollectionObject(),
663 'table_id' => $this->table_id,
664 'record_id' => $this->getId(),
665 'record' => $this,
666 ]
667 );
668 }
669 }
670
671 // TODO: Find better way to copy data (including all references)
672 public function cloneStructure(int $original_id, array $new_fields): void
673 {
674 $original = ilDclCache::getRecordCache($original_id);
675 $this->setCreateDate($original->getCreateDate());
676 $this->setLastEditBy($original->getLastEditBy());
677 $this->setLastUpdate($original->getLastUpdate());
678 $this->setOwner($original->getOwner());
679 $this->doCreate();
680 foreach ($new_fields as $old => $new) {
681 $old_rec_field = $original->getRecordField($old);
682 $new_rec_field = ilDclCache::getRecordFieldCache($this, $new);
683 $new_rec_field->cloneStructure($old_rec_field);
684 $this->recordfields[] = $new_rec_field;
685 }
686
687 // mandatory for all cloning functions
689 }
690
691 public function deleteFile(int $obj_id): void
692 {
693 if (ilObject2::_exists($obj_id, false)) {
694 $file = new ilObjFile($obj_id, false);
695 $file->delete();
696 }
697 }
698
699 public function deleteMob(int $obj_id): void
700 {
701 if (ilObject2::_lookupObjId($obj_id)) {
702 $mob = new ilObjMediaObject($obj_id);
703 $mob->delete();
704 }
705 }
706
707 public function hasPermissionToEdit(int $ref_id): bool
708 {
709 return $this->getTable()->hasPermissionToEditRecord($ref_id, $this);
710 }
711
712 public function hasPermissionToDelete(int $ref_id): bool
713 {
714 return $this->getTable()->hasPermissionToDeleteRecord($ref_id, $this);
715 }
716
717 public function hasPermissionToView(int $ref_id): bool
718 {
719 return $this->getTable()->hasPermissionToViewRecord($ref_id, $this);
720 }
721
725 public function getRecordFields(): array
726 {
727 $this->loadRecordFields();
728
729 return $this->recordfields;
730 }
731
732 public function getTable(): ilDclTable
733 {
734 $this->loadTable();
735
736 return $this->table;
737 }
738
742 public function getNrOfComments(): int
743 {
744 if ($this->nr_of_comments === null) {
745 $context = $this->notes
746 ->data()
747 ->context(
748 $this->table->getCollectionObject()->getId(),
749 $this->getId(),
750 'dcl'
751 );
752 $this->nr_of_comments = $this->notes
753 ->domain()
754 ->getNrOfCommentsForContext($context);
755 }
756
757 return $this->nr_of_comments;
758 }
759}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds data types.
Definition: Factory.php:21
Class Services.
Definition: Services.php:38
const IL_CAL_DATETIME
Global event handler.
static buildAjaxHash(int $node_type, ?int $node_id, string $obj_type, int $obj_id, string $sub_type=null, int $sub_id=null, int $news_id=0)
Build ajax hash.
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false)
@classDescription Date and time handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setLastUpdate(ilDateTime $a_datetime)
getNrOfComments()
Get nr of comments of this record.
doDelete(bool $omit_notification=false)
fillRecordFieldExcelExport(ilExcel $worksheet, int &$row, int &$col, $field_id)
getRecordFieldSortingValue($field_id, array $options=[])
fillRecordFieldFormInput($field_id, ilPropertyFormGUI $form)
fillStandardFieldFormInput($field_id, ilPropertyFormGUI $form)
getRecordFieldExportValue($field_id)
Get Field Export Value.
getRecordFieldPlainText($field_id)
Get Field Export Value.
getRecordFieldHTML($field_id, array $options=[])
setStandardFieldValueFromExcel(ilExcel $excel, int $row, int $col, ilDclBaseFieldModel $field)
getRecordFieldValueFromExcel(ilExcel $excel, int $row, int $col, ilDclBaseFieldModel $field)
getStandardFieldHTML(string $field_id, array $options=[])
setCreateDate(ilDateTime $a_datetime)
ILIAS Refinery Factory $refinery
getRecordFieldValue(?int $field_id)
Get Field Value.
getRecordFieldRepresentationValue($field_id)
Get Field Value for Representation in a Form.
getStandardFieldPlainText(string $field_id)
setStandardFieldFromForm($field_id, ilPropertyFormGUI $form)
cloneStructure(int $original_id, array $new_fields)
doUpdate(bool $omit_notification=false)
setRecordFieldValueFromForm(int $field_id, ilPropertyFormGUI $form)
Set a field value.
setRecordFieldValue($field_id, $value)
static getRecordCache(?int $record_id)
static getRecordFieldCache(object $record, object $field)
static getTableCache(int $table_id=null)
static setCloneOf(int $old, int $new, string $type)
static _isStandardField($field_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _tableExists(int $table_id)
setCell(int $a_row, int $a_col, $a_value, ?string $a_datatype=null)
Set cell value.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getListCommentsJSCall(string $a_hash, string $a_update_code=null)
Get list comments js call.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilObjFile.
User class.
static _lookupName(int $a_user_id)
lookup user name
static _lookupLogin(int $a_user_id)
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
static _lookupObjId(int $ref_id)
This class represents a property form user interface.
getItemByPostVar(string $a_post_var)
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link="", bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path="ilpublicuserprofilegui")
Default behaviour is:
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$ref_id
Definition: ltiauth.php:67
static http()
Fetches the global http state from ILIAS.
$query
$context
Definition: webdav.php:29