ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilAdvancedMDValues.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once "Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php";
5
16{
17 protected $record_id; // [int]
18 protected $obj_id; // [int]
19 protected $sub_id; // [int]
20 protected $sub_type; // [int]
21
22 protected $defs; // [array]
23 protected $adt_group; // [ilADTGroup]
24 protected $active_record; // [ilADTActiveRecordByType]
25
26 protected $disabled; // [array]
27
28 protected static $preload_obj_records; // [array]
29
39 public function __construct($a_record_id, $a_obj_id, $a_sub_type = "-", $a_sub_id = 0)
40 {
41 $this->record_id = (int)$a_record_id;
42 $this->obj_id = (int)$a_obj_id;
43 $this->sub_type = $a_sub_type ? $a_sub_type : "-";
44 $this->sub_id = (int)$a_sub_id;
45 }
46
54 public static function getInstancesForObjectId($a_obj_id, $a_obj_type = null, $a_sub_type = "-", $a_sub_id = 0)
55 {
56 $res = array();
57
58 if(!$a_obj_type)
59 {
60 $a_obj_type = ilObject::_lookupType($a_obj_id);
61 }
62
63 include_once "Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php";
64 foreach(ilAdvancedMDRecord::_getSelectedRecordsByObject($a_obj_type, $a_obj_id, $a_sub_type) as $record)
65 {
66 $id = $record->getRecordId();
67 $res[$id] = new self($id, $a_obj_id, $a_sub_type, $a_sub_id);
68 }
69
70 return $res;
71 }
72
78 public function getDefinitions()
79 {
80 if(!is_array($this->defs))
81 {
82 $this->defs = ilAdvancedMDFieldDefinition::getInstancesByRecordId($this->record_id);
83 }
84 return $this->defs;
85 }
86
92 public function getADTGroup()
93 {
94 if(!$this->adt_group instanceof ilADTGroup)
95 {
97 }
98 return $this->adt_group;
99 }
100
106 protected function getActiveRecord()
107 {
108 if(!$this->active_record instanceof ilADTActiveRecordByType)
109 {
110 include_once "Services/ADT/classes/class.ilADTFactory.php";
111 $factory = ilADTFactory::getInstance();
112
113 $adt_group_db = $factory->getDBBridgeForInstance($this->getADTGroup());
114
115 $primary = array(
116 "obj_id" => array("integer", $this->obj_id),
117 "sub_type" => array("text", $this->sub_type),
118 "sub_id" => array("integer", $this->sub_id)
119 );
120 $adt_group_db->setPrimary($primary);
121 $adt_group_db->setTable("adv_md_values");
122
123 // multi-enum fakes single in adv md
124 foreach($adt_group_db->getElements() as $element)
125 {
126 if($element->getADT()->getType() == "MultiEnum")
127 {
128 $element->setFakeSingle(true);
129 }
130 }
131
132 $this->active_record = $factory->getActiveRecordByTypeInstance($adt_group_db);
133 $this->active_record->setElementIdColumn("field_id", "integer");
134 }
135
137 }
138
145 public static function findByObjectId($a_obj_id)
146 {
147 include_once "Services/ADT/classes/class.ilADTFactory.php";
149 return ilADTActiveRecordByType::readByPrimary("adv_md_values", array("obj_id"=>array("integer", $a_obj_id)));
150 }
151
152
153 //
154 // disabled
155 //
156
157 // to set disabled use self::write() with additional data
158
165 public function isDisabled($a_element_id)
166 {
167 if(is_array($this->disabled))
168 {
169 return in_array($a_element_id, $this->disabled);
170 }
171 }
172
173
174 //
175 // CRUD
176 //
177
181 public function read()
182 {
183 $this->disabled = array();
184
185 $tmp = $this->getActiveRecord()->read(true);
186 if($tmp)
187 {
188 foreach($tmp as $element_id => $data)
189 {
190 if($data["disabled"])
191 {
192 $this->disabled[] = $element_id;
193 }
194 }
195 }
196 }
197
203 public function write(array $a_additional_data = null)
204 {
205 $this->getActiveRecord()->write($a_additional_data);
206 }
207
215 public static function _deleteByFieldId($a_field_id, ilADT $a_adt)
216 {
217 ilADTFactory::getInstance()->initActiveRecordByType();
219 "adv_md_values",
220 array("field_id"=>array("integer", $a_field_id)),
221 $a_adt->getType());
222 }
223
229 public static function _deleteByObjId($a_obj_id)
230 {
231 ilADTFactory::getInstance()->initActiveRecordByType();
233 "adv_md_values",
234 array("obj_id"=>array("integer", $a_obj_id)));
235 }
236
237
238
239 //
240 // substitutions (aka list gui)
241 //
242
248 public static function preloadByObjIds(array $a_obj_ids)
249 {
250 global $ilDB;
251
252 // preload values
253 ilADTFactory::getInstance()->initActiveRecordByType();
255 "adv_md_values",
256 array("obj_id"=>array("integer", $a_obj_ids)));
257
258
259 // preload record ids for object types
260
261 self::$preload_obj_records = array();
262
263 // get active records for object types
264 $query = "SELECT amro.*".
265 " FROM adv_md_record_objs amro".
266 " JOIN adv_md_record amr ON (amr.record_id = amro.record_id)".
267 " WHERE active = ".$ilDB->quote(1, "integer");
268 $set = $ilDB->query($query);
269 while($row = $ilDB->fetchAssoc($set))
270 {
271 self::$preload_obj_records[$row["obj_type"]][] = array($row["record_id"], $row["optional"]);
272 }
273 }
274
275 public static function preloadedRead($a_type, $a_obj_id)
276 {
277 $res = array();
278
279 if(isset(self::$preload_obj_records[$a_type]))
280 {
281 foreach(self::$preload_obj_records[$a_type] as $item)
282 {
283 $record_id = $item[0];
284
285 // record is optional, check activation for object
286 if($item[1])
287 {
288 $found = false;
289 include_once "Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php";
290 foreach(ilAdvancedMDRecord::_getSelectedRecordsByObject($a_type, $a_obj_id) as $record)
291 {
292 if($record->getRecordId() == $item[0])
293 {
294 $found = true;
295 }
296 }
297 if(!$found)
298 {
299 continue;
300 }
301 }
302
303 $res[$record_id] = new self($record_id, $a_obj_id);
304 $res[$record_id]->read();
305 }
306 }
307
308 return $res;
309 }
310
311
312 //
313 // copy/export (import: ilAdvancedMDValueParser)
314 //
315
325 public static function _cloneValues($a_source_id,$a_target_id,$a_sub_type = null,$a_source_sub_id = null,$a_target_sub_id=null)
326 {
327 global $ilLog;
328
329 $source_primary = array("obj_id"=>array("integer", $a_source_id));
330 $target_primary = array("obj_id"=>array("integer", $a_target_id));
331
332 // sub-type support
333 if($a_sub_type &&
334 $a_source_sub_id &&
335 $a_target_sub_id)
336 {
337 $source_primary["sub_type"] = array("text", $a_sub_type);
338 $source_primary["sub_id"] = array("integer", $a_source_sub_id);
339 $target_primary["sub_type"] = array("text", $a_sub_type);
340 $target_primary["sub_id"] = array("integer", $a_target_sub_id);
341 }
342
343 ilADTFactory::getInstance()->initActiveRecordByType();
345 "adv_md_values",
346 array(
347 "obj_id" => "integer",
348 "sub_type" => "text",
349 "sub_id" => "integer",
350 "field_id" => "integer"
351 ),
352 $source_primary,
353 $target_primary,
354 array("disabled"=>"integer"));
355
356 if(!$has_cloned)
357 {
358 $ilLog->write(__METHOD__.': No advanced meta data found.');
359 }
360 else
361 {
362 $ilLog->write(__METHOD__.': Start cloning advanced meta data.');
363 }
364 return true;
365 }
366
373 public static function _appendXMLByObjId(ilXmlWriter $a_xml_writer, $a_obj_id)
374 {
375 $a_xml_writer->xmlStartTag('AdvancedMetaData');
376
377 self::preloadByObjIds(array($a_obj_id));
378 $values_records = self::preloadedRead(ilObject::_lookupType($a_obj_id), $a_obj_id);
379
380 foreach($values_records as $values_record)
381 {
382 $defs = $values_record->getDefinitions();
383 foreach($values_record->getADTGroup()->getElements() as $element_id => $element)
384 {
385 $def = $defs[$element_id];
386
387 $value = null;
388 if(!$element->isNull())
389 {
390 $value = $def->getValueForXML($element);
391 }
392
393 $a_xml_writer->xmlElement(
394 'Value',
395 array('id' => $def->getImportId()),
396 $value
397 );
398 }
399 }
400
401 $a_xml_writer->xmlEndTag('AdvancedMetaData');
402 }
403
404
405 //
406 // glossary (might be generic)
407 //
408
415 static public function queryForRecords($a_obj_id, $a_subtype, $a_records, $a_obj_id_key, $a_obj_subid_key, array $a_amet_filter = null)
416 {
417 $results = array();
418
419 if (!is_array($a_obj_id))
420 {
421 $a_obj_id = array($a_obj_id);
422 }
423
424 $sub_obj_ids = array();
425 foreach($a_records as $rec)
426 {
427 $sub_obj_ids[] = $rec[$a_obj_subid_key];
428 }
429
430 // preload adv data for object id(s)
431 ilADTFactory::getInstance()->initActiveRecordByType();
433 array(
434 "obj_id" => array("integer", $a_obj_id),
435 "sub_type" => array("text", $a_subtype),
436 "sub_id" => array("integer", $sub_obj_ids)
437 ));
438
439 $record_groups = array();
440
441 foreach($a_records as $rec)
442 {
443 $obj_id = $rec[$a_obj_id_key];
444 $sub_id = $rec[$a_obj_subid_key];
445
446 // only active amet records for glossary
448 {
449 $record_id = $adv_record->getRecordId();
450
451 if(!isset($record_groups[$record_id]))
452 {
455 $record_groups[$record_id] = ilADTFactory::getInstance()->getDBBridgeForInstance($record_groups[$record_id]);
456 $record_groups[$record_id]->setTable("adv_md_values");
457 }
458
459 // prepare ADT group for record id
460 $record_groups[$record_id]->setPrimary(array(
461 "obj_id" => array("integer", $obj_id),
462 "sub_type" => array("text", $a_subtype),
463 "sub_id" => array("integer", $sub_id)
464 ));
465
466 // multi-enum fakes single in adv md
467 foreach($record_groups[$record_id]->getElements() as $element)
468 {
469 if($element->getADT()->getType() == "MultiEnum")
470 {
471 $element->setFakeSingle(true);
472 }
473 }
474
475 // read (preloaded) data
476 $active_record = new ilADTActiveRecordByType($record_groups[$record_id]);
477 $active_record->setElementIdColumn("field_id", "integer");
478 $active_record->read();
479
480 $adt_group = $record_groups[$record_id]->getADT();
481
482 // filter against amet values
483 if($a_amet_filter)
484 {
485 foreach($a_amet_filter as $field_id => $element)
486 {
487 if($adt_group->hasElement($field_id))
488 {
489 if(!$element->isInCondition($adt_group->getElement($field_id)))
490 {
491 continue(3);
492 }
493 }
494 }
495 }
496
497 // add amet values to glossary term record
498 foreach($adt_group->getElements() as $element_id => $element)
499 {
500 if(!$element->isNull())
501 {
502 // we are reusing the ADT group for all $a_records, so we need to clone
503 $pb = ilADTFactory::getInstance()->getPresentationBridgeForInstance(clone $element);
504 $rec["md_".$element_id] = $pb->getSortable();
505 $rec["md_".$element_id."_presentation"] = $pb;
506
507 }
508 else
509 {
510 $rec["md_".$element_id] = null;
511 }
512 }
513 }
514
515 $results[] = $rec;
516 }
517
518 return $results;
519 }
520}
521
522?>
ADT Active Record by type helper class.
static preloadByPrimary($a_table, array $a_primary)
Read values by (partial) primary key.
static deleteByPrimary($a_table, array $a_primary, $a_type=null)
Delete values by (partial) primary key.
static readByPrimary($a_table, array $a_primary, $a_type=null)
Read directly.
static cloneByPrimary($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 getInstance()
Get singleton.
static initActiveRecordByType()
Init active record by type.
ADT base class.
Definition: class.ilADT.php:12
getType()
Get type (from class/instance)
Definition: class.ilADT.php:51
static getADTGroupForDefinitions(array $a_defs)
Init ADTGroup for definitions.
static getInstancesByRecordId($a_record_id, $a_only_searchable=false)
Get definitions by record id.
static _getSelectedRecordsByObject($a_obj_type, $a_obj_id, $a_sub_type="")
Get selected records by object.
static _deleteByObjId($a_obj_id)
Delete by objekt id.
static queryForRecords($a_obj_id, $a_subtype, $a_records, $a_obj_id_key, $a_obj_subid_key, array $a_amet_filter=null)
Query data for given object records.
static getInstancesForObjectId($a_obj_id, $a_obj_type=null, $a_sub_type="-", $a_sub_id=0)
Get instances for given object id.
__construct($a_record_id, $a_obj_id, $a_sub_type="-", $a_sub_id=0)
Constructor.
static preloadByObjIds(array $a_obj_ids)
Preload list gui data.
static preloadedRead($a_type, $a_obj_id)
static findByObjectId($a_obj_id)
Find all entries for object (regardless of sub-type/sub-id)
isDisabled($a_element_id)
Is element disabled?
static _cloneValues($a_source_id, $a_target_id, $a_sub_type=null, $a_source_sub_id=null, $a_target_sub_id=null)
Clone Advanced Meta Data.
getDefinitions()
Get record field definitions.
static _appendXMLByObjId(ilXmlWriter $a_xml_writer, $a_obj_id)
Get xml of object values.
getADTGroup()
Init ADT group for current record.
write(array $a_additional_data=null)
Write record values.
getActiveRecord()
Init ADT DB Bridge (aka active record helper class)
static _deleteByFieldId($a_field_id, ilADT $a_adt)
Delete values by field_id.
static _lookupType($a_id, $a_reference=false)
lookup object type
XML writer class.
xmlEndTag($tag)
Writes an endtag.
xmlStartTag($tag, $attrs=NULL, $empty=FALSE, $encode=TRUE, $escape=TRUE)
Writes a starttag.
xmlElement($tag, $attrs=NULL, $data=Null, $encode=TRUE, $escape=TRUE)
Writes a basic element (no children, just textual content)
$data
$results
global $ilDB