ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilDataCollectionDataSet.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
27  protected array $record_field_ids_2_storage = [];
44  protected array $caches
45  = [
46  'dcl' => [],
47  'il_dcl_table' => [],
48  'il_dcl_field' => [],
49  'il_dcl_field_prop' => [],
50  'il_dcl_sel_opts' => [],
51  'il_dcl_record' => [],
52  'il_dcl_record_field' => [],
53  'il_dcl_stloc1_value' => [],
54  'il_dcl_stloc2_value' => [],
55  'il_dcl_stloc3_value' => [],
56  'il_dcl_stloc1_default' => [],
57  'il_dcl_stloc2_default' => [],
58  'il_dcl_stloc3_default' => [],
59  'il_dcl_tfield_set' => [],
60  'il_dcl_tableview' => [],
61  'il_dcl_tview_set' => [],
62  ];
63 
65 
69  protected array $import_record_field_cache = [];
70  protected ilObjUser $user;
71  protected \ILIAS\Refinery\Factory $refinery;
72  protected array $import_temp_refs = [];
73  protected array $import_temp_refs_props = [];
74  protected array $import_temp_new_mob_ids = [];
75 
76  public function __construct()
77  {
78  global $DIC;
80  $this->db = $DIC->database();
81  $this->user = $DIC->user();
82  $this->refinery = $DIC->refinery();
83  }
84 
85  public function getSupportedVersions(): array
86  {
87  return ['4.5.0', '8.13'];
88  }
89 
94  public function getCache(string $a_entity): array
95  {
96  if (!in_array($a_entity, array_keys($this->caches))) {
97  throw new ilException("Entity '$a_entity' does not exist in Cache");
98  }
99 
100  return $this->caches[$a_entity];
101  }
102 
103  protected function getXmlNamespace(string $a_entity, string $a_schema_version): string
104  {
105  return 'https://www.ilias.de/xml/Modules/DataCollection/' . $a_entity;
106  }
107 
108  public function importRecord(
109  string $a_entity,
110  array $a_types,
111  array $a_rec,
112  ilImportMapping $a_mapping,
113  string $a_schema_version
114  ): void {
115  foreach ($a_rec as $key => &$value) {
116  $array = json_decode($value, true);
117  if (is_array($array)) {
118  $value = json_encode($this->escapeArray($array));
119  } else {
120  $value = $this->refinery->encode()->htmlSpecialCharsAsEntities()->transform($value);
121  }
122  }
123  switch ($a_entity) {
124  case 'dcl':
125  if ($new_id = $a_mapping->getMapping('components/ILIAS/Container', 'objs', $a_rec['id'])) {
126  $new_obj = ilObjectFactory::getInstanceByObjId((int) $new_id, false);
127  } else {
128  $new_obj = new ilObjDataCollection();
129  $new_obj->create(true);
130  }
131  $new_obj->setTitle($a_rec['title']);
132  $new_obj->setDescription($a_rec['description']);
133  $new_obj->setApproval($a_rec['approval'] === '1');
134  $new_obj->setPublicNotes($a_rec['public_notes'] === '1');
135  $new_obj->setNotification($a_rec['notification'] === '1');
136  $new_obj->setRating($a_rec['rating'] === '1');
137  $new_obj->update();
138  $this->import_dc_object = $new_obj;
139  $a_mapping->addMapping('components/ILIAS/DataCollection', 'dcl', $a_rec['id'], (string) $new_obj->getId());
140  $a_mapping->addMapping(
141  'components/ILIAS/MetaData',
142  'md',
143  $a_rec['id'] . ':0:dcl',
144  $new_obj->getId() . ':0:dcl'
145  );
146  break;
147  case 'il_dcl_table':
148  $table = new ilDclTable();
149  $table->setTitle($a_rec['title']);
150  $table->setObjId($this->import_dc_object->getId());
151  $table->setDescription($a_rec['description']);
152  $table->setAddPerm($a_rec['add_perm'] === '1');
153  $table->setEditPerm($a_rec['edit_perm'] === '1');
154  $table->setDeletePerm($a_rec['delete_perm'] === '1');
155  $table->setEditByOwner($a_rec['edit_by_owner'] === '1');
156  $table->setLimited($a_rec['limited'] === '1');
157  $table->setLimitStart($a_rec['limit_start']);
158  $table->setLimitEnd($a_rec['limit_end']);
159  $table->setIsVisible($a_rec['is_visible'] === '1');
160  $table->setExportEnabled($a_rec['export_enabled'] === '1');
161  $table->setImportEnabled($a_rec['import_enabled'] === '1');
162  $table->setDefaultSortField($a_rec['default_sort_field_id']);
163  $table->setDefaultSortFieldOrder($a_rec['default_sort_field_order']);
164  $table->setPublicCommentsEnabled($a_rec['public_comments'] === '1');
165  $table->setViewOwnRecordsPerm($a_rec['view_own_records_perm'] === '1');
166  $table->setDeleteByOwner($a_rec['delete_by_owner'] === '1');
167  $table->setSaveConfirmation($a_rec['save_confirmation'] === '1');
168  $table->setOrder((int) $a_rec['table_order']);
169  $table->doCreate(false, false); // false => Do not create views! They are imported later
170  $a_mapping->addMapping('components/ILIAS/DataCollection', 'il_dcl_table', $a_rec['id'], (string) $table->getId());
171  break;
172  case 'il_dcl_tableview':
173  $new_table_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_table', $a_rec['table_id']);
174  if ($new_table_id) {
175  $tableview = new ilDclTableView();
176  $tableview->setTitle($a_rec['title']);
177  $tableview->setTableId((int) $new_table_id);
178  $tableview->setDescription($a_rec['description']);
179  $tableview->setTableviewOrder((int) $a_rec['tableview_order']);
180  if (!is_array($a_rec['roles'])) {
181  $a_rec['roles'] = json_decode($a_rec['roles']);
182  }
183  $tableview->setRoles($a_rec['roles']);
184  $tableview->create(false); //do not create default setting as they are imported too
185 
186  $a_mapping->addMapping(
187  'components/ILIAS/DataCollection',
188  'il_dcl_tableview',
189  $a_rec['id'],
190  (string) $tableview->getId()
191  );
192  $a_mapping->addMapping('components/ILIAS/COPage', 'pg', 'dclf:' . $a_rec['id'], 'dclf:' . $tableview->getId());
193  }
194  break;
195  case 'il_dcl_field':
196  $new_table_id = (int) $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_table', $a_rec['table_id']);
197  if ($new_table_id > 0) {
198  $datatype_id = (int) $a_rec['datatype_id'];
199  $datatype = $a_rec['datatype_title'] ?? null;
200  $datatypes = ilDclDatatype::getAllDatatype();
201  if ($datatype !== null && ilDclFieldTypePlugin::isPluginDatatype($datatype)) {
202  $datatype_id = null;
203  foreach ($datatypes as $dt) {
204  if ($dt->getTitle() === $datatype) {
205  $datatype_id = $dt->getId();
206  }
207  }
208  }
209  if (in_array($datatype_id, array_keys($datatypes))) {
210  $field = new ilDclBaseFieldModel();
211  $field->setTableId($new_table_id);
212  $field->setDatatypeId($datatype_id);
213  $field->setTitle($a_rec['title']);
214  $field->setDescription($a_rec['description']);
215  $field->setUnique($a_rec['is_unique'] === '1');
216  $field->doCreate();
217  $a_mapping->addMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['id'], $field->getId());
218  }
219  }
220  break;
221  case 'il_dcl_tfield_set':
222  $new_table_id = (int) $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_table', $a_rec['table_id']);
223  $new_field_id = is_numeric($a_rec['field']) ? $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['field']) : $a_rec['field'];
224  if ($new_table_id > 0 && (is_string($new_field_id) || $new_field_id > 0)) {
226  $new_table_id,
227  $new_field_id
228  );
229  $setting->setFieldOrder((int) $a_rec['field_order']);
230  $setting->setExportable($a_rec['exportable'] === '1');
231  $setting->store();
232  }
233  break;
234  case 'il_dcl_tview_set':
235  $new_tableview_id = $a_mapping->getMapping(
236  'components/ILIAS/DataCollection',
237  'il_dcl_tableview',
238  $a_rec['tableview_id']
239  );
240  $new_field_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['field']);
241  if ($new_tableview_id) {
242  $setting = new ilDclTableViewFieldSetting();
243  $setting->setTableviewId((int) $new_tableview_id);
244  $setting->setVisible($a_rec['visible'] === '1');
245  $setting->setField($new_field_id ?: $a_rec['field']);
246  $setting->setInFilter($a_rec['in_filter'] === '1');
247  $setting->setFilterValue($a_rec['filter_value']);
248  $setting->setFilterChangeable($a_rec['filter_changeable'] === '1');
249  $setting->setRequiredCreate($a_rec['required_create'] === '1');
250  $setting->setLockedCreate($a_rec['locked_create'] === '1');
251  $setting->setVisibleCreate($a_rec['visible_create'] === '1');
252  $setting->setVisibleEdit($a_rec['visible_edit'] === '1');
253  $setting->setRequiredEdit($a_rec['required_edit'] === '1');
254  $setting->setLockedEdit($a_rec['locked_edit'] === '1');
255  $setting->setDefaultValue($a_rec['default_value']);
256  $setting->create();
257  $a_mapping->addMapping(
258  'components/ILIAS/DataCollection',
259  'il_dcl_tview_set',
260  $a_rec['id'],
261  (string) $setting->getId()
262  );
263  }
264  break;
265  case 'il_dcl_record':
266  $new_table_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_table', $a_rec['table_id']);
267  if ($new_table_id) {
268  $record = new ilDclBaseRecordModel();
269  $record->setTableId((int) $new_table_id);
270  $datetime = new ilDateTime(time(), IL_CAL_UNIX);
271  $record->setCreateDate($datetime);
272  $record->setLastUpdate($datetime);
273  $record->setOwner($this->user->getId());
274  $record->setLastEditBy($this->user->getId());
275  $record->doCreate();
276  $a_mapping->addMapping(
277  'components/ILIAS/DataCollection',
278  'il_dcl_record',
279  $a_rec['id'],
280  (string) $record->getId()
281  );
282  }
283  break;
284  case 'il_dcl_view':
285  $new_table_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_table', $a_rec['table_id']);
286  if ($new_table_id) {
287  //if import contains il_dcl_view, it must origin from an earlier ILIAS Version and therefore contains no tableviews
288  //->create standard view
289  $tableview = ilDclTableView::createOrGetStandardView((int) $new_table_id);
290  if ($a_rec['type'] == 0 && $a_rec['formtype'] == 0) { //set page_object to tableview
291  // This mapping is needed for the import handled by Services/COPage
292  $a_mapping->addMapping(
293  'components/ILIAS/COPage',
294  'pg',
295  'dclf:' . $a_rec['id'],
296  'dclf:' . $tableview->getId()
297  );
298  $a_mapping->addMapping(
299  'components/ILIAS/DataCollection',
300  'il_dcl_view',
301  $a_rec['id'],
302  (string) $tableview->getId()
303  );
304  } else {
305  $a_mapping->addMapping(
306  'components/ILIAS/DataCollection',
307  'il_dcl_view',
308  $a_rec['id'],
309  json_encode(['type' => $a_rec['type'],
310  'table_id' => $new_table_id,
311  'tableview_id' => $tableview->getId()
312  ])
313  );
314  }
315  }
316  break;
317  case 'il_dcl_viewdefinition':
318  $map = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_view', $a_rec['view_id']);
319  $new_field_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['field']);
320  $field = ($new_field_id) ?: $a_rec['field'];
321  switch ($map['type']) {
322  case 1: //visible
323  $viewfield_setting = ilDclTableViewFieldSetting::getInstance($map['tableview_id'], $field);
324  $viewfield_setting->setVisible($a_rec['is_set']);
325  $viewfield_setting->store();
326  break;
327  case 3: //in_filter
328  $viewfield_setting = ilDclTableViewFieldSetting::getInstance($map['tableview_id'], $field);
329  $viewfield_setting->setInFilter($a_rec['is_set']);
330  $viewfield_setting->store();
331  break;
332  case 4: //exportable
333  $tablefield_setting = ilDclTableFieldSetting::getInstance($map['table_id'], $field);
334  $tablefield_setting->setExportable($a_rec['is_set']);
335  $tablefield_setting->setFieldOrder($a_rec['field_order']);
336  $tablefield_setting->store();
337  break;
338  }
339  break;
340  case 'il_dcl_sel_opts':
341  $new_field_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['field_id']);
342  if ($new_field_id) {
343  $opt = new ilDclSelectionOption();
344  $opt->setFieldId((int) $new_field_id);
345  $opt->setOptId((int) $a_rec['opt_id']);
346  $opt->setSorting((int) $a_rec['sorting']);
347  $opt->setValue($a_rec['value']);
348  $opt->store();
349  }
350  break;
351  case 'il_dcl_field_prop':
352  $new_field_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['field_id']);
353  if ($new_field_id) {
354  $prop = new ilDclFieldProperty();
355  $prop->setFieldId((int) $new_field_id);
356 
357  // OLD IMPORT! Backwards-compatibility
358  $name = $a_rec['name'];
359  if (!isset($name) && isset($a_rec['datatype_prop_id'])) {
360  $properties = [
361  1 => 'length',
362  2 => 'regex',
363  3 => 'table_id',
364  4 => 'url',
365  5 => 'reference_link',
366  6 => 'width',
367  7 => 'height',
368  8 => 'learning_progress',
369  9 => 'ILIAS_reference_link',
370  10 => 'multiple_selection',
371  11 => 'expression',
372  12 => 'display_action_menu',
373  13 => 'link_detail_page',
374  14 => 'link_detail_page',
375  ];
376 
377  $name = $properties[$a_rec['datatype_prop_id']];
378  }
379 
380  $prop->setName($name);
381  $prop->setValue($a_rec['value']);
382  $prop->save();
383  $a_mapping->addMapping(
384  'components/ILIAS/DataCollection',
385  'il_dcl_field_prop',
386  $a_rec['id'],
387  (string) $prop->getId()
388  );
389  $this->import_temp_refs_props[$prop->getId()] = $a_rec['value'];
390  }
391  break;
392  case 'il_dcl_record_field':
393  $record_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_record', $a_rec['record_id']);
394  $field_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $a_rec['field_id']);
395  if ($record_id && $field_id) {
396  $record = ilDclCache::getRecordCache((int) $record_id);
397  $field = ilDclCache::getFieldCache((int) $field_id);
398  $record_field = new ilDclBaseRecordFieldModel($record, $field);
399  $a_mapping->addMapping(
400  'components/ILIAS/DataCollection',
401  'il_dcl_record_field',
402  $a_rec['id'],
403  (string) $record_field->getId()
404  );
405  $this->import_record_field_cache[$record_field->getId()] = $record_field;
406  }
407  break;
408  case 'il_dcl_stloc1_value':
409  case 'il_dcl_stloc2_value':
410  case 'il_dcl_stloc3_value':
411  $new_record_field_id = $a_mapping->getMapping(
412  'components/ILIAS/DataCollection',
413  'il_dcl_record_field',
414  $a_rec['record_field_id']
415  );
416  if ($new_record_field_id) {
418  $record_field = $this->import_record_field_cache[$new_record_field_id];
419  if (is_object($record_field)) {
420  // Need to rewrite internal references and lookup new objects if MOB or File
421  // For some fieldtypes it's better to reset the value, e.g. ILIAS_REF
422  switch ($record_field->getField()->getDatatypeId()) {
424  // Check if we got a mapping from old object
425  $new_mob_id = $a_mapping->getMapping('components/ILIAS/MediaObjects', 'mob', $a_rec['value']);
426  $value = ($new_mob_id) ? (int) $new_mob_id : null;
427  $this->import_temp_new_mob_ids[] = $new_mob_id;
428  break;
430  $new_file_id = $a_mapping->getMapping('components/ILIAS/File', 'file', $a_rec['value']);
431  $value = ($new_file_id) ? (int) $new_file_id : null;
432  break;
435  $value = $a_rec['value'];
436  $decode = json_decode($a_rec['value']);
437  if (is_array($decode)) {
438  foreach ($decode as $id) {
439  $this->import_temp_refs[$new_record_field_id][] = $id;
440  }
441  } else {
442  $this->import_temp_refs[$new_record_field_id] = $value;
443  }
444  break;
446  $value = null;
447  break;
449  $value = $a_rec['value'];
450  if ($value == '0000-00-00 00:00:00') {
451  $value = null;
452  }
453  break;
455  if (version_compare($a_schema_version, "8.13") < 0) {
456  $a_rec['value'] = str_replace('&lt;br /&gt;', '', $a_rec['value']);
457  }
458  // no break
459  default:
460  $value = $a_rec['value'];
461  if ($a_entity == 'il_dcl_stloc3_value' && empty($value)) {
462  $value = null;
463  }
464  }
465  $record_field->setValue($value, true);
466  $record_field->doUpdate();
467  }
468  }
469  break;
470  case 'il_dcl_stloc1_default':
471  case 'il_dcl_stloc2_default':
472  case 'il_dcl_stloc3_default':
473 
474  $tview_set_id = $a_mapping->getMapping(
475  'components/ILIAS/DataCollection',
476  'il_dcl_tview_set',
477  $a_rec['tview_set_id']
478  );
479 
480  if ($tview_set_id) {
481  $value = $a_rec['value'];
482  if ($value) {
483  $stloc_default = (new ilDclDefaultValueFactory())->createByTableName($a_entity);
485  $value = (int) $value;
486  }
487  $stloc_default->setValue($value);
488  $stloc_default->setTviewSetId((int) $tview_set_id);
489  $stloc_default->create();
490  }
491  }
492  break;
493  }
494  }
495 
496  protected function escapeArray(array $array): array
497  {
498  $new = [];
499  foreach ($array as $key => $value) {
500  $newkey = $key;
501  if (is_string($key)) {
502  $newkey = $this->refinery->encode()->htmlSpecialCharsAsEntities()->transform($key);
503  }
504  $newvalue = $value;
505  if (is_string($value)) {
506  $newvalue = $this->refinery->encode()->htmlSpecialCharsAsEntities()->transform($value);
507  }
508  if (is_array($value)) {
509  $newvalue = $this->escapeArray($value);
510  }
511  $new[$newkey] = $newvalue;
512  }
513  return $new;
514  }
515 
520  public function beforeFinishImport(ilImportMapping $a_mapping): void
521  {
522  foreach ($this->import_dc_object->getTables() as $table) {
523  if (is_numeric($table->getDefaultSortField())) {
524  $table->setDefaultSortField(
525  $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $table->getDefaultSortField())
526  );
527  $table->doUpdate();
528  }
529  }
530  foreach ($this->import_temp_new_mob_ids as $new_mob_id) {
531  if ($new_mob_id) {
532  ilObjMediaObject::_saveUsage((int) $new_mob_id, "dcl:html", $a_mapping->getTargetId());
533  }
534  }
535  foreach ($this->import_temp_refs as $record_field_id => $old_record_id) {
536  if (is_array($old_record_id)) {
537  $new_record_id = [];
538  foreach ($old_record_id as $id) {
539  $new_record_id[] = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_record', $id);
540  }
541  $value = $new_record_id;
542  } else {
543  $value = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_record', $old_record_id);
544  }
546  $record_field = $this->import_record_field_cache[$record_field_id];
547  $record_field->setValue($value, true);
548  $record_field->doUpdate();
549  }
550  foreach ($this->import_temp_refs_props as $field_prop_id => $prop_value) {
551  $new_field_id = $a_mapping->getMapping('components/ILIAS/DataCollection', 'il_dcl_field', $prop_value);
552  $value = ($new_field_id) ? (int) $new_field_id : $prop_value;
553 
554  $field_prop = new ilDclFieldProperty($field_prop_id);
555  $field_prop->setValue($value);
556  $field_prop->update();
557  }
558  }
559 
563  protected function getTypes(string $a_entity, string $a_version): array
564  {
565  switch ($a_entity) {
566  case 'dcl':
567  return [
568  "id" => "integer",
569  "title" => "text",
570  "description" => "text",
571  'is_online' => 'integer',
572  'rating' => 'integer',
573  'public_notes' => 'integer',
574  'approval' => 'integer',
575  'notification' => 'integer',
576  ];
577  case 'il_dcl_table':
578  return [
579  'id' => 'integer',
580  'obj_id' => 'integer',
581  'title' => 'text',
582  'add_perm' => 'integer',
583  'edit_perm' => 'integer',
584  'delete_perm' => 'integer',
585  'edit_by_owner' => 'integer',
586  'limited' => 'integer',
587  'limit_start' => 'text',
588  'limit_end' => 'text',
589  'is_visible' => 'integer',
590  'export_enabled' => 'integer',
591  'import_enabled' => 'integer',
592  'default_sort_field_id' => 'text',
593  'default_sort_field_order' => 'text',
594  'description' => 'text',
595  'public_comments' => 'integer',
596  'view_own_records_perm' => 'integer',
597  'delete_by_owner' => 'integer',
598  'save_confirmation' => 'integer',
599  'table_order' => 'integer',
600  ];
601  case 'il_dcl_tableview':
602  return [
603  'id' => 'integer',
604  'table_id' => 'integer',
605  'title' => 'text',
606  'roles' => 'text',
607  'description' => 'text',
608  'tableview_order' => 'integer',
609  ];
610  case 'il_dcl_field':
611  return [
612  'id' => 'integer',
613  'table_id' => 'integer',
614  'title' => 'text',
615  'description' => 'text',
616  'datatype_id' => 'integer',
617  'datatype_title' => 'text',
618  'is_unique' => 'integer',
619  ];
620  case 'il_dcl_tview_set':
621  return [
622  'id' => 'integer',
623  'tableview_id' => 'integer',
624  'field' => 'text',
625  'visible' => 'integer',
626  'in_filter' => 'integer',
627  'filter_value' => 'text',
628  'filter_changeable' => 'integer',
629  'required_create' => 'integer',
630  'required_edit' => 'integer',
631  'locked_create' => 'integer',
632  'locked_edit' => 'integer',
633  'visible_create' => 'integer',
634  'visible_edit' => 'integer',
635  'default_value' => 'text',
636  ];
637  case 'il_dcl_tfield_set':
638  return [
639  'id' => 'integer',
640  'table_id' => 'integer',
641  'field' => 'text',
642  'field_order' => 'integer',
643  'exportable' => 'integer',
644  ];
645  case 'il_dcl_field_prop':
646  return [
647  'id' => 'integer',
648  'field_id' => 'integer',
649  'name' => 'text',
650  'value' => 'integer',
651  ];
652  case 'il_dcl_sel_opts':
653  return [
654  'id' => 'integer',
655  'field_id' => 'integer',
656  'opt_id' => 'integer',
657  'sorting' => 'integer',
658  'value' => 'text',
659  ];
660  case 'il_dcl_record':
661  return [
662  'id' => 'integer',
663  'table_id' => 'integer',
664  ];
665  case 'il_dcl_record_field':
666  return [
667  'id' => 'integer',
668  'record_id' => 'integer',
669  'field_id' => 'integer',
670  ];
671  case 'il_dcl_stloc1_value':
672  case 'il_dcl_stloc2_value':
673  case 'il_dcl_stloc3_value':
674  return [
675  'id' => 'integer',
676  'record_field_id' => 'integer',
677  'value' => 'text',
678  ];
679  case 'il_dcl_stloc1_default':
680  case 'il_dcl_stloc2_default':
681  case 'il_dcl_stloc3_default':
682  return [
683  'id' => 'integer',
684  'tview_set_id' => 'integer',
685  'value' => 'text',
686  ];
687  default:
688  return [];
689  }
690  }
691 
700  protected function getDependencies(
701  string $a_entity,
702  string $a_version,
703  ?array $a_rec = null,
704  ?array $a_ids = null
705  ): array {
706  if (!$a_rec && !$a_ids) {
707  return [];
708  }
709  switch ($a_entity) {
710  case 'dcl':
711  $set = $this->db->query('SELECT * FROM il_dcl_table WHERE obj_id = ' . $this->db->quote(
712  $a_rec['id'],
713  'integer'
714  ) . ' ORDER BY id');
715  $ids = $this->buildCache('il_dcl_table', $set);
716 
717  return [
718  'il_dcl_table' => ['ids' => $ids],
719  ];
720  case 'il_dcl_table':
721  $set = $this->db->query('SELECT * FROM il_dcl_record WHERE table_id = ' . $this->db->quote(
722  $a_rec['id'],
723  'integer'
724  ));
725  $ids_records = $this->buildCache('il_dcl_record', $set);
726  $set = $this->db->query('SELECT il_dcl_field.*, il_dcl_datatype.title as datatype_title FROM il_dcl_field INNER JOIN il_dcl_datatype ON il_dcl_field.datatype_id = il_dcl_datatype.id WHERE table_id = ' . $this->db->quote(
727  $a_rec['id'],
728  'integer'
729  ));
730  $ids_fields = $this->buildCache('il_dcl_field', $set);
731  $set = $this->db->query('SELECT * FROM il_dcl_tableview WHERE table_id = ' . $this->db->quote(
732  $a_rec['id'],
733  'integer'
734  ));
735  $ids_tableviews = $this->buildCache('il_dcl_tableview', $set);
736  $set = $this->db->query('SELECT * FROM il_dcl_tfield_set WHERE table_id = ' . $this->db->quote(
737  $a_rec['id'],
738  'integer'
739  ));
740  $ids_tablefield_settings = $this->buildCache('il_dcl_tfield_set', $set);
741 
742  return [
743  'il_dcl_field' => ['ids' => $ids_fields],
744  'il_dcl_record' => ['ids' => $ids_records],
745  'il_dcl_tableview' => ['ids' => $ids_tableviews],
746  'il_dcl_tfield_set' => ['ids' => $ids_tablefield_settings],
747  ];
748  case 'il_dcl_field':
749  $set = $this->db->query('SELECT * FROM il_dcl_field_prop WHERE field_id = ' . $this->db->quote(
750  $a_rec['id'],
751  'integer'
752  ));
753  $prop_ids = $this->buildCache('il_dcl_field_prop', $set);
754 
755  $set = $this->db->query('SELECT * FROM il_dcl_sel_opts WHERE field_id = ' . $this->db->quote(
756  $a_rec['id'],
757  'integer'
758  ));
759  $opt_ids = $this->buildCache('il_dcl_sel_opts', $set);
760 
761  return [
762  'il_dcl_field_prop' => ['ids' => $prop_ids],
763  'il_dcl_sel_opts' => ['ids' => $opt_ids],
764  ];
765  case 'il_dcl_record':
766  $sql = 'SELECT rf.*, d.storage_location FROM il_dcl_record_field AS rf' . ' INNER JOIN il_dcl_field AS f ON (f.id = rf.field_id)'
767  . ' INNER JOIN il_dcl_datatype AS d ON (f.datatype_id = d.id) ' . ' WHERE rf.record_id = '
768  . $this->db->quote($a_rec['id'], 'integer');
769  $set = $this->db->query($sql);
770  $ids = $this->buildCache('il_dcl_record_field', $set);
771 
772  $set = $this->db->query($sql);
773  while ($rec = $this->db->fetchObject($set)) {
774  $this->record_field_ids_2_storage[$rec->id] = ilDclCache::getFieldCache($rec->field_id)->getStorageLocation();
775  }
776  // Also build a cache of all values, no matter in which table they are (il_dcl_stloc(1|2|3)_value)
777  $sql
778  = 'SELECT rf.id AS record_field_id, st1.value AS value1, st2.value AS value2, st3.value AS value3 FROM il_dcl_record_field AS rf '
779  . 'LEFT JOIN il_dcl_stloc1_value AS st1 ON (st1.record_field_id = rf.id) '
780  . 'LEFT JOIN il_dcl_stloc2_value AS st2 ON (st2.record_field_id = rf.id) '
781  . 'LEFT JOIN il_dcl_stloc3_value AS st3 ON (st3.record_field_id = rf.id) ' . 'WHERE rf.record_id = '
782  . $this->db->quote($a_rec['id'], 'integer');
783  $set = $this->db->query($sql);
784 
785  while ($rec = $this->db->fetchObject($set)) {
786  $stloc = $this->record_field_ids_2_storage[$rec->record_field_id];
787  $value = null;
788  if ($stloc != 0) {
789  $value = "value$stloc";
790  $value = $rec->{$value};
791  }
792  // Save reocrd field id. Internal ID is not used currently
793  $this->caches["il_dcl_stloc{$stloc}_value"][$rec->record_field_id] = [
794  'record_field_id' => $rec->record_field_id,
795  'value' => $value,
796  ];
797  }
798 
799  return [
800  'il_dcl_record_field' => ['ids' => $ids],
801  ];
802  case 'il_dcl_tableview':
803  $set = $this->db->query('SELECT * FROM il_dcl_tview_set WHERE tableview_id = ' . $this->db->quote(
804  $a_rec['id'],
805  'integer'
806  ));
807  $ids = $this->buildCache('il_dcl_tview_set', $set);
808 
809  return [
810  'il_dcl_tview_set' => ['ids' => $ids],
811  ];
812  case 'il_dcl_tview_set':
813 
814  if (!(int) $a_rec['field'] > 0) {
815  break;
816  }
817  // Also build a cache of all values, no matter in which table they are (il_dcl_stloc(1|2|3)_value)
818  $sql
819  = '
820  SELECT tview_set.id AS tview_set_id, st1.value AS value1, st2.value AS value2, st3.value AS value3,
821  st1.id AS id1, st2.id AS id2, st3.id AS id3
822  FROM il_dcl_tview_set AS tview_set
823  LEFT JOIN il_dcl_stloc1_default AS st1 ON (st1.tview_set_id = tview_set.id)
824  LEFT JOIN il_dcl_stloc2_default AS st2 ON (st2.tview_set_id = tview_set.id)
825  LEFT JOIN il_dcl_stloc3_default AS st3 ON (st3.tview_set_id = tview_set.id)
826  WHERE tview_set.id = ' . $this->db->quote($a_rec['id'], 'integer');
827  $set = $this->db->query($sql);
828 
829  while ($rec = $this->db->fetchObject($set)) {
830  $stloc = ilDclCache::getFieldCache((int) $a_rec['field'])->getStorageLocation();
831  if ($stloc != 0) {
832  $value_str = "value$stloc";
833  $value = $rec->{$value_str};
834  $id_str = "id$stloc";
835  $id = $rec->{$id_str};
836  $tview_set_id = $rec->tview_set_id;
837 
838  // Save reocrd field id. Internal ID is not used currently
839  $this->caches["il_dcl_stloc" . "$stloc" . "_default"][$rec->tview_set_id] = [
840  'id' => $id,
841  'tview_set_id' => $rec->tview_set_id,
842  'value' => $value,
843  ];
844 
845  return [
846  "il_dcl_stloc{$stloc}_default" => ['ids' => [$tview_set_id]],
847  ];
848  }
849  }
850  break;
851  case 'il_dcl_record_field':
852  $record_field_id = $a_rec['id'];
853  $storage_loc = $this->record_field_ids_2_storage[$record_field_id];
854 
855  return [
856  "il_dcl_stloc{$storage_loc}_value" => ['ids' => [$record_field_id]],
857  ];
858  }
859 
860  return [];
861  }
862 
867  public function readData(string $a_entity, string $a_version, array $a_ids): void
868  {
869  $this->data = [];
870  $this->_readData($a_entity, $a_ids);
871  }
872 
878  protected function _readData(string $a_entity, array $a_ids): void
879  {
880  switch ($a_entity) {
881  case 'dcl':
882  foreach ($a_ids as $dcl_id) {
883  if (ilObject::_lookupType((int) $dcl_id) === 'dcl') {
884  $obj = new ilObjDataCollection((int) $dcl_id, false);
885  $data = [
886  'id' => $dcl_id,
887  'title' => $obj->getTitle(),
888  'description' => $obj->getDescription(),
889  'is_online' => $obj->getOnline() ? '1' : '0',
890  'rating' => $obj->getRating() ? '1' : '0',
891  'public_notes' => $obj->getPublicNotes() ? '1' : '0',
892  'approval' => $obj->getApproval() ? '1' : '0',
893  'notification' => $obj->getNotification() ? '1' : '0',
894  ];
895  $this->caches['dcl'][$dcl_id] = $data;
896  $this->data[] = $data;
897  }
898  }
899  break;
900  default:
901  $data = $this->getCache($a_entity);
902  foreach ($a_ids as $id) {
903  $this->data[] = $data[$id];
904  }
905  }
906  }
907 
908  protected function buildCache(string $a_entity, ilDBStatement $set): array
909  {
910  $fields = array_keys($this->getTypes($a_entity, ''));
911  $ids = [];
912  while ($rec = $this->db->fetchObject($set)) {
913  $data = [];
914  foreach ($fields as $field) {
915  $data[$field] = $rec->{$field};
916  }
917  $id = $rec->id;
918  $this->caches[$a_entity][$id] = $data;
919  $ids[] = $id;
920  }
921 
922  return $ids;
923  }
924 }
getXmlNamespace(string $a_entity, string $a_schema_version)
static createOrGetStandardView(int $table_id)
getTypes(string $a_entity, string $a_version)
Map XML attributes of entities to datatypes (text, integer...)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
buildCache(string $a_entity, ilDBStatement $set)
static getFieldCache(int $field_id=0)
$datetime
static _saveUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Save usage of mob within another container (e.g.
static isPluginDatatype(string $datatype)
array $import_record_field_cache
Caches ilDclBaseRecordFieldModel objects.
const IL_CAL_UNIX
addMapping(string $a_comp, string $a_entity, string $a_old_id, string $a_new_id)
static getInstance(int $tableview_id, int $field_id)
getMapping(string $a_comp, string $a_entity, string $a_old_id)
readData(string $a_entity, string $a_version, array $a_ids)
Read data from Cache for a given entity and ID(s)
static getAllDatatype(bool $force=false)
Get all possible Datatypes.
getCache(string $a_entity)
Get cached data from a given entity.
importRecord(string $a_entity, array $a_types, array $a_rec, ilImportMapping $a_mapping, string $a_schema_version)
Needs to be overwritten for import use case.
global $DIC
Definition: shib_login.php:25
array ilObjDataCollection $import_dc_object
_readData(string $a_entity, array $a_ids)
Build data array, data is read from cache except dcl object itself.
static getInstance(int $table_id, string $field)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static getRecordCache(?int $record_id)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
__construct(Container $dic, ilPlugin $plugin)
getDependencies(string $a_entity, string $a_version, ?array $a_rec=null, ?array $a_ids=null)
Return dependencies form entities to other entities (in our case these are all the DB relations) ...
static _lookupType(int $id, bool $reference=false)