ILIAS  release_8 Revision v8.24
class.ilAdvancedMDValues.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
28{
29 protected int $record_id;
30 protected int $obj_id;
31 protected int $sub_id;
32 protected string $sub_type;
33
34 protected ?array $defs = null;
35 protected ?ilADTGroup $adt_group = null;
37
38 protected array $disabled = [];
39
40 protected static array $preload_obj_records = [];
41
42 public function __construct($a_record_id, $a_obj_id, $a_sub_type = "-", $a_sub_id = 0)
43 {
44 $this->record_id = (int) $a_record_id;
45 $this->obj_id = (int) $a_obj_id;
46 $this->sub_type = $a_sub_type ?: "-";
47 $this->sub_id = (int) $a_sub_id;
48 }
49
50 public static function getInstancesForObjectId(
51 int $a_obj_id,
52 ?string $a_obj_type = null,
53 string $a_sub_type = "-",
54 int $a_sub_id = 0
55 ): array {
56 $res = array();
57
58 if (!$a_obj_type) {
59 $a_obj_type = ilObject::_lookupType($a_obj_id);
60 }
61
62 // @todo refactor
63 $refs = ilObject::_getAllReferences($a_obj_id);
64 foreach ($refs as $ref_id) {
65 $records = ilAdvancedMDRecord::_getSelectedRecordsByObject($a_obj_type, $ref_id, $a_sub_type);
66 $orderings = new ilAdvancedMDRecordObjectOrderings();
67 $records = $orderings->sortRecords($records, $a_obj_id);
68
69 foreach ($records as $record) {
70 $id = $record->getRecordId();
71
72 if (!isset($res[$id])) {
73 $res[$id] = new self($id, $a_obj_id, $a_sub_type, $a_sub_id);
74 }
75 }
76 }
77 return $res;
78 }
79
80 public function setActiveRecordPrimary(int $a_obj_id, string $a_sub_type = "-", int $a_sub_id = 0): void
81 {
82 $this->obj_id = $a_obj_id;
83 $this->sub_type = $a_sub_type ?: "-";
84 $this->sub_id = $a_sub_id;
85
86 // make sure they get used
87 $this->active_record = null;
88 }
89
94 public function getDefinitions(): array
95 {
96 if (!is_array($this->defs)) {
97 $this->defs = ilAdvancedMDFieldDefinition::getInstancesByRecordId($this->record_id);
98 }
99 return $this->defs;
100 }
101
102 public function getADTGroup(): ilADTGroup
103 {
104 if (!$this->adt_group instanceof ilADTGroup) {
105 $this->adt_group = ilAdvancedMDFieldDefinition::getADTGroupForDefinitions($this->getDefinitions());
106 }
107 return $this->adt_group;
108 }
109
114 {
115 if (!$this->active_record instanceof ilADTActiveRecordByType) {
117
118 $adt_group_db = $factory->getDBBridgeForInstance($this->getADTGroup());
119
120 $primary = array(
121 "obj_id" => array("integer", $this->obj_id),
122 "sub_type" => array("text", $this->sub_type),
123 "sub_id" => array("integer", $this->sub_id)
124 );
125 $adt_group_db->setPrimary($primary);
126 $adt_group_db->setTable("adv_md_values");
127
128 // multi-enum fakes single in adv md
129 foreach ($adt_group_db->getElements() as $element) {
130 if ($element->getADT()->getType() == "MultiEnum") {
131 $element->setFakeSingle(true);
132 }
133 }
134
135 $this->active_record = $factory->getActiveRecordByTypeInstance($adt_group_db);
136 $this->active_record->setElementIdColumn("field_id", "integer");
137 }
138
139 return $this->active_record;
140 }
141
145 public static function findByObjectId(int $a_obj_id): ?array
146 {
148 return ilADTActiveRecordByType::readByPrimary("adv_md_values", array("obj_id" => array("integer", $a_obj_id)));
149 }
150
151
152 //
153 // disabled
154 //
155
156 // to set disabled use self::write() with additional data
157
158 public function isDisabled(string $a_element_id): ?bool
159 {
160 if (is_array($this->disabled)) {
161 return in_array($a_element_id, $this->disabled);
162 }
163 return null;
164 }
165
169 public function read(): void
170 {
171 $this->disabled = array();
172
173 $tmp = $this->getActiveRecord()->read(true);
174 if ($tmp) {
175 foreach ($tmp as $element_id => $data) {
176 if ($data["disabled"]) {
177 $this->disabled[] = $element_id;
178 }
179 }
180 }
181 }
182
186 public function write(array $a_additional_data = null): void
187 {
188 $this->getActiveRecord()->write($a_additional_data);
189 }
190
197 public static function _deleteByFieldId(int $a_field_id, ilADT $a_adt): void
198 {
199 ilADTFactory::getInstance()->initActiveRecordByType();
201 "adv_md_values",
202 array("field_id" => array("integer", $a_field_id)),
203 $a_adt->getType()
204 );
205 }
206
210 public static function _deleteByObjId(int $a_obj_id)
211 {
212 ilADTFactory::getInstance()->initActiveRecordByType();
214 "adv_md_values",
215 array("obj_id" => array("integer", $a_obj_id))
216 );
217 }
218
223 public static function preloadByObjIds(array $a_obj_ids): void
224 {
225 global $DIC;
226
227 $ilDB = $DIC['ilDB'];
228
229 // preload values
230 ilADTFactory::getInstance()->initActiveRecordByType();
232 "adv_md_values",
233 array("obj_id" => array("integer", $a_obj_ids))
234 );
235
236 // preload record ids for object types
237
238 self::$preload_obj_records = array();
239
240 // get active records for object types
241 $query = "SELECT amro.*" .
242 " FROM adv_md_record_objs amro" .
243 " JOIN adv_md_record amr ON (amr.record_id = amro.record_id)" .
244 " WHERE active = " . $ilDB->quote(1, "integer");
245 $set = $ilDB->query($query);
246 while ($row = $ilDB->fetchAssoc($set)) {
247 self::$preload_obj_records[(string) $row["obj_type"]][] = array((int) $row["record_id"],
248 (int) $row["optional"]
249 );
250 }
251 }
252
253 public static function preloadedRead(string $a_type, int $a_obj_id): array
254 {
255 $res = array();
256
257 if (isset(self::$preload_obj_records[$a_type])) {
258 foreach (self::$preload_obj_records[$a_type] as $item) {
259 $record_id = $item[0];
260
261 // record is optional, check activation for object
262 if ($item[1]) {
263 $found = false;
265 $a_type,
266 $a_obj_id,
267 '',
268 false
269 ) as $record) {
270 if ($record->getRecordId() == $item[0]) {
271 $found = true;
272 }
273 }
274 if (!$found) {
275 continue;
276 }
277 }
278
279 $res[$record_id] = new self($record_id, $a_obj_id);
280 $res[$record_id]->read();
281 }
282 }
283
284 return $res;
285 }
286
290 public static function _cloneValues(
291 int $copy_id,
292 int $a_source_id,
293 int $a_target_id,
294 ?string $a_sub_type = null,
295 ?int $a_source_sub_id = null,
296 ?int $a_target_sub_id = null
297 ): void {
298 global $DIC;
299
300 $ilLog = $DIC['ilLog'];
301
302 // clone local records
303
304 // new records are created automatically, only if source and target id differs.
305 $new_records = $fields_map = array();
306
307 // if we have a sub object and are in a copy process,
308 // get mapping from parent
309 $parent_mapping = null;
310 if (!is_null($a_source_sub_id) && $copy_id > 0) {
311 $parent_mapping = self::getParentMapping($copy_id, (string) $a_target_id);
312 if (!is_null($parent_mapping)) {
313 $new_records = $parent_mapping["records"];
314 $fields_map = $parent_mapping["fields"];
315 }
316 }
317
318 $record_mapping = [];
319 foreach (ilAdvancedMDRecord::_getRecords() as $record) {
320 if ($record->getParentObject() == $a_source_id && is_null($parent_mapping)) {
321
322 // if we have a sub object and are in a copy process,
323 // the main object must have already copied its records
324 if (!is_null($a_source_sub_id) && $copy_id > 0) {
325 throw new ilException("ilAdvancedMDValues::_cloneValues must be called for parent object first.");
326 }
327
328 $tmp = array();
329 if ($a_source_id != $a_target_id) {
330 $new_records[$record->getRecordId()] = $record->_clone($tmp, $a_target_id);
331 $record_mapping[$record->getRecordId()] = $new_records[$record->getRecordId()]->getRecordId();
332 } else {
333 $new_records[$record->getRecordId()] = $record->getRecordId();
334 }
335 $fields_map[$record->getRecordId()] = $tmp;
336 }
337 }
338
339 // write mapping when not in sub-object
340 if ($copy_id > 0 && is_null($a_source_sub_id)) {
341 $cp_options = ilCopyWizardOptions::_getInstance($copy_id);
342 $cp_options->appendMapping(
343 $a_target_id . '_adv_rec',
344 $record_mapping
345 );
346 $cp_options->appendMapping(
347 $a_target_id . '_adv_rec_fields',
348 $fields_map
349 );
350 $cp_options->read(); // otherwise mapping will not be available for getMappings
351 }
352
353 // object record selection
354
355 $source_sel = ilAdvancedMDRecord::getObjRecSelection($a_source_id, (string) $a_sub_type);
356 if ($source_sel) {
357 $target_sel = array();
358 foreach ($source_sel as $record_id) {
359 // (local) record has been cloned
360 if (array_key_exists($record_id, $new_records)) {
361 $record_id = $new_records[$record_id]->getRecordId();
362 }
363 $target_sel[] = $record_id;
364 }
365 ilAdvancedMDRecord::saveObjRecSelection($a_target_id, (string) $a_sub_type, $target_sel);
366 }
367
368 // clone values
369
370 $source_primary = array("obj_id" => array("integer", $a_source_id));
371 $target_primary = array("obj_id" => array("integer", $a_target_id));
372
373 // sub-type support
374 if ($a_sub_type &&
375 $a_source_sub_id &&
376 $a_target_sub_id) {
377 $source_primary["sub_type"] = array("text", $a_sub_type);
378 $source_primary["sub_id"] = array("integer", $a_source_sub_id);
379 $target_primary["sub_type"] = array("text", $a_sub_type);
380 $target_primary["sub_id"] = array("integer", $a_target_sub_id);
381 }
382
383 ilADTFactory::getInstance()->initActiveRecordByType();
385 "adv_md_values",
386 array(
387 "obj_id" => "integer",
388 "sub_type" => "text",
389 "sub_id" => "integer",
390 "field_id" => "integer"
391 ),
392 $source_primary,
393 $target_primary,
394 array("disabled" => "integer")
395 );
396
397 // move values of local records to newly created fields
398
399 foreach ($fields_map as $source_record_id => $fields) {
400 // just to make sure
401 if (array_key_exists($source_record_id, $new_records)) {
402 foreach ($fields as $source_field_id => $target_field_id) {
403 // delete entry for old field id (was cloned above)
404 $del_target_primary = $target_primary;
405 $del_target_primary["field_id"] = array("integer", $source_field_id);
406 ilADTActiveRecordByType::deleteByPrimary("adv_md_values", $del_target_primary);
407
408 // create entry for new id
409 $fix_source_primary = $source_primary;
410 $fix_source_primary["field_id"] = array("integer", $source_field_id);
411 $fix_target_primary = $target_primary;
412 $fix_target_primary["field_id"] = array("integer", $target_field_id);
414 "adv_md_values",
415 array(
416 "obj_id" => "integer",
417 "sub_type" => "text",
418 "sub_id" => "integer",
419 "field_id" => "integer"
420 ),
421 $fix_source_primary,
422 $fix_target_primary,
423 array("disabled" => "integer")
424 );
425 }
426 }
427 }
428
429 if (!$has_cloned) {
430 $ilLog->write(__METHOD__ . ': No advanced meta data found.');
431 } else {
432 $ilLog->write(__METHOD__ . ': Start cloning advanced meta data.');
433 }
434 }
435
436 protected static function getParentMapping(int $copy_id, string $target): ?array
437 {
438 $cp_options = ilCopyWizardOptions::_getInstance($copy_id);
439 $mappings = $cp_options->getMappings();
440 $key1 = $target . '_adv_rec';
441 $key2 = $target . '_adv_rec_fields';
442 if (is_array($mappings) && isset($mappings[$key1])) {
443 return [
444 "records" => $mappings[$key1],
445 "fields" => $mappings[$key2] ?? []
446 ];
447 }
448 return null;
449 }
450
456 public static function _appendXMLByObjId(ilXmlWriter $a_xml_writer, int $a_obj_id): void
457 {
458 $a_xml_writer->xmlStartTag('AdvancedMetaData');
459
460 self::preloadByObjIds(array($a_obj_id));
461 $values_records = self::preloadedRead(ilObject::_lookupType($a_obj_id), $a_obj_id);
462
463 foreach ($values_records as $values_record) {
464 $defs = $values_record->getDefinitions();
465 foreach ($values_record->getADTGroup()->getElements() as $element_id => $element) {
466 $def = $defs[$element_id];
467
468 $value = null;
469 if (!$element->isNull()) {
470 $value = $def->getValueForXML($element);
471 }
472
473 $a_xml_writer->xmlElement(
474 'Value',
475 array('id' => $def->getImportId()),
476 $value
477 );
478 }
479 }
480 $a_xml_writer->xmlEndTag('AdvancedMetaData');
481 }
482
486 public static function queryForRecords(
487 int $adv_rec_obj_ref_id,
488 string $adv_rec_obj_type,
489 string $adv_rec_obj_subtype,
490 array $a_obj_id,
491 string $a_subtype,
492 array $a_records,
493 string $a_obj_id_key,
494 string $a_obj_subid_key,
495 array $a_amet_filter = null
496 ): array {
497 $results = array();
498
499 $sub_obj_ids = array();
500 foreach ($a_records as $rec) {
501 $sub_obj_ids[] = $rec[$a_obj_subid_key];
502 }
503
504 // preload adv data for object id(s)
505 ilADTFactory::getInstance()->initActiveRecordByType();
507 "adv_md_values",
508 array(
509 "obj_id" => array("integer", $a_obj_id),
510 "sub_type" => array("text", $a_subtype),
511 "sub_id" => array("integer", $sub_obj_ids)
512 )
513 );
514
515 $record_groups = array();
516
517 foreach ($a_records as $rec) {
518 $obj_id = (int) ($rec[$a_obj_id_key] ?? 0);
519 $sub_id = $rec[$a_obj_subid_key];
520
521 // get adv records
523 $adv_rec_obj_type,
524 $adv_rec_obj_ref_id,
525 $adv_rec_obj_subtype
526 ) as $adv_record) {
527 $record_id = $adv_record->getRecordId();
528
529 if (!isset($record_groups[$record_id])) {
531 $record_groups[$record_id] = ilAdvancedMDFieldDefinition::getADTGroupForDefinitions($defs);
532 $record_groups[$record_id] = ilADTFactory::getInstance()->getDBBridgeForInstance($record_groups[$record_id]);
533 $record_groups[$record_id]->setTable("adv_md_values");
534 }
535
536 // prepare ADT group for record id
537 $record_groups[$record_id]->setPrimary(array(
538 "obj_id" => array("integer", $obj_id),
539 "sub_type" => array("text", $a_subtype),
540 "sub_id" => array("integer", $sub_id)
541 ));
542 // multi-enum fakes single in adv md
543 foreach ($record_groups[$record_id]->getElements() as $element) {
544 if ($element->getADT()->getType() == "MultiEnum") {
545 $element->setFakeSingle(true);
546 }
547 }
548
549 // read (preloaded) data
550 $active_record = new ilADTActiveRecordByType($record_groups[$record_id]);
551 $active_record->setElementIdColumn("field_id", "integer");
552 $active_record->read();
553
554 $adt_group = $record_groups[$record_id]->getADT();
555 // filter against amet values
556 if ($a_amet_filter) {
557 foreach ($a_amet_filter as $field_id => $element) {
558 if ($adt_group->hasElement((string) $field_id)) {
559 if (!$element->isInCondition($adt_group->getElement((string) $field_id))) {
560 continue(3);
561 }
562 }
563 }
564 }
565 // add amet values to glossary term record
566 foreach ($adt_group->getElements() as $element_id => $element) {
567 if (!$element->isNull()) {
568 // we are reusing the ADT group for all $a_records, so we need to clone
569 $pb = ilADTFactory::getInstance()->getPresentationBridgeForInstance(clone $element);
570 $rec["md_" . $element_id] = $pb->getSortable();
571 $rec["md_" . $element_id . "_presentation"] = $pb;
572 } else {
573 $rec["md_" . $element_id] = null;
574 }
575 }
576 }
577
578 $results[] = $rec;
579 }
580
581 return $results;
582 }
583}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static cloneByPrimary(string $a_table, array $a_primary_def, array $a_source_primary, array $a_target_primary, array $a_additional=null)
Clone values by (partial) primary key.
static readByPrimary(string $a_table, array $a_primary, ?string $a_type=null)
Read directly.
static preloadByPrimary(string $a_table, array $a_primary)
read(bool $a_return_additional_data=false)
Read record.
setElementIdColumn(string $a_name, string $a_type)
static deleteByPrimary(string $a_table, array $a_primary, string $a_type=null)
static initActiveRecordByType()
Init active record by type.
getElement(string $a_name)
hasElement(string $a_name)
ADT base class.
Definition: class.ilADT.php:12
getType()
Get type (from class/instance)
Definition: class.ilADT.php:50
static getInstancesByRecordId( $a_record_id, $a_only_searchable=false, string $language='')
Get definitions by record id.
static getADTGroupForDefinitions(array $a_defs)
Init ADTGroup for definitions.
static getObjRecSelection(int $a_obj_id, string $a_sub_type="")
Get repository object record selection.
static _getSelectedRecordsByObject(string $a_obj_type, int $a_id, string $a_sub_type="", bool $is_ref_id=true)
static _getRecords()
Get records @access public.
static saveObjRecSelection(int $a_obj_id, string $a_sub_type="", array $a_records=null, bool $a_delete_before=true)
Save repository object record selection.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
isDisabled(string $a_element_id)
setActiveRecordPrimary(int $a_obj_id, string $a_sub_type="-", int $a_sub_id=0)
ilADTActiveRecordByType $active_record
__construct($a_record_id, $a_obj_id, $a_sub_type="-", $a_sub_id=0)
static getInstancesForObjectId(int $a_obj_id, ?string $a_obj_type=null, string $a_sub_type="-", int $a_sub_id=0)
static _deleteByFieldId(int $a_field_id, ilADT $a_adt)
Delete values by field_id.
static preloadByObjIds(array $a_obj_ids)
Preload list gui data.
static preloadedRead(string $a_type, int $a_obj_id)
getDefinitions()
Get record field definitions.
static findByObjectId(int $a_obj_id)
Find all entries for object (regardless of sub-type/sub-id)
static queryForRecords(int $adv_rec_obj_ref_id, string $adv_rec_obj_type, string $adv_rec_obj_subtype, array $a_obj_id, string $a_subtype, array $a_records, string $a_obj_id_key, string $a_obj_subid_key, array $a_amet_filter=null)
write(array $a_additional_data=null)
Write record values.
static _deleteByObjId(int $a_obj_id)
Delete by objekt id.
getActiveRecord()
Init ADT DB Bridge (aka active record helper class)
static getParentMapping(int $copy_id, string $target)
static _cloneValues(int $copy_id, int $a_source_id, int $a_target_id, ?string $a_sub_type=null, ?int $a_source_sub_id=null, ?int $a_target_sub_id=null)
Clone Advanced Meta Data.
static _appendXMLByObjId(ilXmlWriter $a_xml_writer, int $a_obj_id)
Get xml of object values.
static _getInstance(int $a_copy_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupType(int $id, bool $reference=false)
static _getAllReferences(int $id)
get all reference ids for object ID
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
xmlElement(string $tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
xmlEndTag(string $tag)
Writes an endtag.
xmlStartTag(string $tag, ?array $attrs=null, bool $empty=false, bool $encode=true, bool $escape=true)
Writes a starttag.
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67
$res
Definition: ltiservices.php:69
$factory
Definition: metadata.php:75
disabled()
Example showing how to plug a disabled checkbox into a form.
Definition: disabled.php:10
$query
$results