ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilAdvancedMDValues.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
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) {
116 $factory = ilADTFactory::getInstance();
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 // if we have a sub object and are in a copy process,
322 // the main object must have already copied its records
323 if (!is_null($a_source_sub_id) && $copy_id > 0) {
324 throw new ilException("ilAdvancedMDValues::_cloneValues must be called for parent object first.");
325 }
326
327 $tmp = array();
328 if ($a_source_id != $a_target_id) {
329 $new_records[$record->getRecordId()] = $record->_clone($tmp, $a_target_id);
330 $record_mapping[$record->getRecordId()] = $new_records[$record->getRecordId()]->getRecordId();
331 } else {
332 $new_records[$record->getRecordId()] = $record->getRecordId();
333 }
334 $fields_map[$record->getRecordId()] = $tmp;
335 }
336 }
337
338 // write mapping when not in sub-object
339 if ($copy_id > 0 && is_null($a_source_sub_id)) {
340 $cp_options = ilCopyWizardOptions::_getInstance($copy_id);
341 $cp_options->appendMapping(
342 $a_target_id . '_adv_rec',
343 $record_mapping
344 );
345 $cp_options->appendMapping(
346 $a_target_id . '_adv_rec_fields',
347 $fields_map
348 );
349 $cp_options->read(); // otherwise mapping will not be available for getMappings
350 }
351
352 // object record selection
353
354 $source_sel = ilAdvancedMDRecord::getObjRecSelection($a_source_id, (string) $a_sub_type);
355 if ($source_sel) {
356 $target_sel = array();
357 foreach ($source_sel as $record_id) {
358 // (local) record has been cloned
359 if (array_key_exists($record_id, $new_records)) {
360 $record_id = $new_records[$record_id]->getRecordId();
361 }
362 $target_sel[] = $record_id;
363 }
364 ilAdvancedMDRecord::saveObjRecSelection($a_target_id, (string) $a_sub_type, $target_sel);
365 }
366
367 // clone values
368
369 $source_primary = array("obj_id" => array("integer", $a_source_id));
370 $target_primary = array("obj_id" => array("integer", $a_target_id));
371
372 // sub-type support
373 if ($a_sub_type &&
374 $a_source_sub_id &&
375 $a_target_sub_id) {
376 $source_primary["sub_type"] = array("text", $a_sub_type);
377 $source_primary["sub_id"] = array("integer", $a_source_sub_id);
378 $target_primary["sub_type"] = array("text", $a_sub_type);
379 $target_primary["sub_id"] = array("integer", $a_target_sub_id);
380 }
381
382 ilADTFactory::getInstance()->initActiveRecordByType();
384 "adv_md_values",
385 array(
386 "obj_id" => "integer",
387 "sub_type" => "text",
388 "sub_id" => "integer",
389 "field_id" => "integer"
390 ),
391 $source_primary,
392 $target_primary,
393 array("disabled" => "integer")
394 );
395
396 // move values of local records to newly created fields
397
398 foreach ($fields_map as $source_record_id => $fields) {
399 // just to make sure
400 if (array_key_exists($source_record_id, $new_records)) {
401 foreach ($fields as $source_field_id => $target_field_id) {
402 // delete entry for old field id (was cloned above)
403 $del_target_primary = $target_primary;
404 $del_target_primary["field_id"] = array("integer", $source_field_id);
405 ilADTActiveRecordByType::deleteByPrimary("adv_md_values", $del_target_primary);
406
407 // create entry for new id
408 $fix_source_primary = $source_primary;
409 $fix_source_primary["field_id"] = array("integer", $source_field_id);
410 $fix_target_primary = $target_primary;
411 $fix_target_primary["field_id"] = array("integer", $target_field_id);
413 "adv_md_values",
414 array(
415 "obj_id" => "integer",
416 "sub_type" => "text",
417 "sub_id" => "integer",
418 "field_id" => "integer"
419 ),
420 $fix_source_primary,
421 $fix_target_primary,
422 array("disabled" => "integer")
423 );
424 }
425 }
426 }
427
428 if (!$has_cloned) {
429 $ilLog->write(__METHOD__ . ': No advanced meta data found.');
430 } else {
431 $ilLog->write(__METHOD__ . ': Start cloning advanced meta data.');
432 }
433 }
434
435 protected static function getParentMapping(int $copy_id, string $target): ?array
436 {
437 $cp_options = ilCopyWizardOptions::_getInstance($copy_id);
438 $mappings = $cp_options->getMappings();
439 $key1 = $target . '_adv_rec';
440 $key2 = $target . '_adv_rec_fields';
441 if (is_array($mappings) && isset($mappings[$key1])) {
442 return [
443 "records" => $mappings[$key1],
444 "fields" => $mappings[$key2] ?? []
445 ];
446 }
447 return null;
448 }
449
455 public static function _appendXMLByObjId(ilXmlWriter $a_xml_writer, int $a_obj_id): void
456 {
457 $a_xml_writer->xmlStartTag('AdvancedMetaData');
458
459 self::preloadByObjIds(array($a_obj_id));
460 $values_records = self::preloadedRead(ilObject::_lookupType($a_obj_id), $a_obj_id);
461
462 foreach ($values_records as $values_record) {
463 $defs = $values_record->getDefinitions();
464 foreach ($values_record->getADTGroup()->getElements() as $element_id => $element) {
465 $def = $defs[$element_id];
466
467 $value = null;
468 if (!$element->isNull()) {
469 $value = $def->getValueForXML($element);
470 }
471
472 $a_xml_writer->xmlElement(
473 'Value',
474 array('id' => $def->getImportId()),
475 $value
476 );
477 }
478 }
479 $a_xml_writer->xmlEndTag('AdvancedMetaData');
480 }
481
485 public static function queryForRecords(
486 int $adv_rec_obj_ref_id,
487 string $adv_rec_obj_type,
488 string $adv_rec_obj_subtype,
489 array $a_obj_id,
490 string $a_subtype,
491 array $a_records,
492 string $a_obj_id_key,
493 string $a_obj_subid_key,
494 ?array $a_amet_filter = null
495 ): array {
496 $results = array();
497
498 $sub_obj_ids = array();
499 foreach ($a_records as $rec) {
500 $sub_obj_ids[] = $rec[$a_obj_subid_key];
501 }
502
503 // preload adv data for object id(s)
504 ilADTFactory::getInstance()->initActiveRecordByType();
506 "adv_md_values",
507 array(
508 "obj_id" => array("integer", $a_obj_id),
509 "sub_type" => array("text", $a_subtype),
510 "sub_id" => array("integer", $sub_obj_ids)
511 )
512 );
513
514 $record_groups = array();
515
516 foreach ($a_records as $rec) {
517 $obj_id = (int) ($rec[$a_obj_id_key] ?? 0);
518 $sub_id = $rec[$a_obj_subid_key];
519
520 // get adv records
522 $adv_rec_obj_type,
523 $adv_rec_obj_ref_id,
524 $adv_rec_obj_subtype
525 ) as $adv_record) {
526 $record_id = $adv_record->getRecordId();
527
528 if (!isset($record_groups[$record_id])) {
530 $record_groups[$record_id] = ilAdvancedMDFieldDefinition::getADTGroupForDefinitions($defs);
531 $record_groups[$record_id] = ilADTFactory::getInstance()->getDBBridgeForInstance($record_groups[$record_id]);
532 $record_groups[$record_id]->setTable("adv_md_values");
533 }
534
535 // prepare ADT group for record id
536 $record_groups[$record_id]->setPrimary(array(
537 "obj_id" => array("integer", $obj_id),
538 "sub_type" => array("text", $a_subtype),
539 "sub_id" => array("integer", $sub_id)
540 ));
541 // multi-enum fakes single in adv md
542 foreach ($record_groups[$record_id]->getElements() as $element) {
543 if ($element->getADT()->getType() == "MultiEnum") {
544 $element->setFakeSingle(true);
545 }
546 }
547
548 // read (preloaded) data
549 $active_record = new ilADTActiveRecordByType($record_groups[$record_id]);
550 $active_record->setElementIdColumn("field_id", "integer");
551 $active_record->read();
552
553 $adt_group = $record_groups[$record_id]->getADT();
554 // filter against amet values
555 if ($a_amet_filter) {
556 foreach ($a_amet_filter as $field_id => $element) {
557 if ($adt_group->hasElement((string) $field_id)) {
558 if (!$element->isInCondition($adt_group->getElement((string) $field_id))) {
559 continue(3);
560 }
561 }
562 }
563 }
564 // add amet values to glossary term record
565 foreach ($adt_group->getElements() as $element_id => $element) {
566 if (!$element->isNull()) {
567 // we are reusing the ADT group for all $a_records, so we need to clone
568 $pb = ilADTFactory::getInstance()->getPresentationBridgeForInstance(clone $element);
569 $rec["md_" . $element_id] = $pb->getSortable();
570 $rec["md_" . $element_id . "_presentation"] = $pb;
571 } else {
572 $rec["md_" . $element_id] = null;
573 }
574 }
575 }
576
577 $results[] = $rec;
578 }
579
580 return $results;
581 }
582}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
ADT Active Record by type helper class This class expects a valid primary for all actions!
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.
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.
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:26
getType()
Get type (from class/instance)
Definition: class.ilADT.php:64
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.
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)
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)
write(?array $a_additional_data=null)
Write record values.
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 _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)
Base class for ILIAS Exception handling.
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.
$ref_id
Definition: ltiauth.php:66
$res
Definition: ltiservices.php:69
$results
global $DIC
Definition: shib_login.php:26