ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
4 include_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)
55  {
56  global $ilDB;
57 
58  $res = array();
59 
60  if(!$a_obj_type)
61  {
62  $a_obj_type = ilObject::_lookupType($a_obj_id);
63  }
64 
65  // get active records for object type
66  $query = "SELECT amro.record_id".
67  " FROM adv_md_record_objs amro".
68  " JOIN adv_md_record amr ON (amr.record_id = amro.record_id)".
69  " WHERE active = ".$ilDB->quote(1, "integer").
70  " AND obj_type = ".$ilDB->quote($a_obj_type, "text");
71  $set = $ilDB->query($query);
72  while($row = $ilDB->fetchAssoc($set))
73  {
74  $res[$row["record_id"]] = new self($row["record_id"], $a_obj_id);
75  }
76 
77  return $res;
78  }
79 
85  public function getDefinitions()
86  {
87  if(!is_array($this->defs))
88  {
89  $this->defs = ilAdvancedMDFieldDefinition::getInstancesByRecordId($this->record_id);
90  }
91  return $this->defs;
92  }
93 
99  public function getADTGroup()
100  {
101  if(!$this->adt_group instanceof ilADTGroup)
102  {
104  }
105  return $this->adt_group;
106  }
107 
113  protected function getActiveRecord()
114  {
115  if(!$this->active_record instanceof ilADTActiveRecordByType)
116  {
117  include_once "Services/ADT/classes/class.ilADTFactory.php";
118  $factory = ilADTFactory::getInstance();
119 
120  $adt_group_db = $factory->getDBBridgeForInstance($this->getADTGroup());
121 
122  $primary = array(
123  "obj_id" => array("integer", $this->obj_id),
124  "sub_type" => array("text", $this->sub_type),
125  "sub_id" => array("integer", $this->sub_id)
126  );
127  $adt_group_db->setPrimary($primary);
128  $adt_group_db->setTable("adv_md_values");
129 
130  // multi-enum fakes single in adv md
131  foreach($adt_group_db->getElements() as $element)
132  {
133  if($element->getADT()->getType() == "MultiEnum")
134  {
135  $element->setFakeSingle(true);
136  }
137  }
138 
139  $this->active_record = $factory->getActiveRecordByTypeInstance($adt_group_db);
140  $this->active_record->setElementIdColumn("field_id", "integer");
141  }
142 
143  return $this->active_record;
144  }
145 
152  public static function findByObjectId($a_obj_id)
153  {
154  include_once "Services/ADT/classes/class.ilADTFactory.php";
156  return ilADTActiveRecordByType::readByPrimary("adv_md_values", array("obj_id"=>array("integer", $a_obj_id)));
157  }
158 
159 
160  //
161  // disabled
162  //
163 
164  // to set disabled use self::write() with additional data
165 
172  public function isDisabled($a_element_id)
173  {
174  if(is_array($this->disabled))
175  {
176  return in_array($a_element_id, $this->disabled);
177  }
178  }
179 
180 
181  //
182  // CRUD
183  //
184 
188  public function read()
189  {
190  $this->disabled = array();
191 
192  $tmp = $this->getActiveRecord()->read(true);
193  if($tmp)
194  {
195  foreach($tmp as $element_id => $data)
196  {
197  if($data["disabled"])
198  {
199  $this->disabled[] = $element_id;
200  }
201  }
202  }
203  }
204 
210  public function write(array $a_additional_data = null)
211  {
212  $this->getActiveRecord()->write($a_additional_data);
213  }
214 
222  public static function _deleteByFieldId($a_field_id, ilADT $a_adt)
223  {
224  ilADTFactory::getInstance()->initActiveRecordByType();
226  "adv_md_values",
227  array("field_id"=>array("integer", $a_field_id)),
228  $a_adt->getType());
229  }
230 
236  public static function _deleteByObjId($a_obj_id)
237  {
238  ilADTFactory::getInstance()->initActiveRecordByType();
240  "adv_md_values",
241  array("obj_id"=>array("integer", $a_obj_id)));
242  }
243 
244 
245 
246  //
247  // substitutions (aka list gui)
248  //
249 
255  public static function preloadByObjIds(array $a_obj_ids)
256  {
257  global $ilDB;
258 
259  // preload values
260  ilADTFactory::getInstance()->initActiveRecordByType();
262  "adv_md_values",
263  array("obj_id"=>array("integer", $a_obj_ids)));
264 
265 
266  // preload record ids for object types
267 
268  self::$preload_obj_records = array();
269 
270  // get active records for object types
271  $query = "SELECT amro.*".
272  " FROM adv_md_record_objs amro".
273  " JOIN adv_md_record amr ON (amr.record_id = amro.record_id)".
274  " WHERE active = ".$ilDB->quote(1, "integer");
275  $set = $ilDB->query($query);
276  while($row = $ilDB->fetchAssoc($set))
277  {
278  self::$preload_obj_records[$row["obj_type"]][] = $row["record_id"];
279  }
280  }
281 
282  public static function preloadedRead($a_type, $a_obj_id)
283  {
284  $res = array();
285 
286  if(isset(self::$preload_obj_records[$a_type]))
287  {
288  foreach(self::$preload_obj_records[$a_type] as $record_id)
289  {
290  $res[$record_id] = new self($record_id, $a_obj_id);
291  $res[$record_id]->read();
292  }
293  }
294 
295  return $res;
296  }
297 
298 
299  //
300  // copy/export (import: ilAdvancedMDValueParser)
301  //
302 
312  public static function _cloneValues($a_source_id,$a_target_id,$a_sub_type = null,$a_source_sub_id = null,$a_target_sub_id=null)
313  {
314  global $ilLog;
315 
316  $source_primary = array("obj_id"=>array("integer", $a_source_id));
317  $target_primary = array("obj_id"=>array("integer", $a_target_id));
318 
319  // sub-type support
320  if($a_sub_type &&
321  $a_source_sub_id &&
322  $a_target_sub_id)
323  {
324  $source_primary["sub_type"] = array("text", $a_sub_type);
325  $source_primary["sub_id"] = array("integer", $a_source_sub_id);
326  $target_primary["sub_type"] = array("text", $a_sub_type);
327  $target_primary["sub_id"] = array("integer", $a_target_sub_id);
328  }
329 
330  ilADTFactory::getInstance()->initActiveRecordByType();
332  "adv_md_values",
333  array(
334  "obj_id" => "integer",
335  "sub_type" => "text",
336  "sub_id" => "integer",
337  "field_id" => "integer"
338  ),
339  $source_primary,
340  $target_primary,
341  array("disabled"=>"integer"));
342 
343  if(!$has_cloned)
344  {
345  $ilLog->write(__METHOD__.': No advanced meta data found.');
346  }
347  else
348  {
349  $ilLog->write(__METHOD__.': Start cloning advanced meta data.');
350  }
351  return true;
352  }
353 
360  public static function _appendXMLByObjId(ilXmlWriter $a_xml_writer, $a_obj_id)
361  {
362  $a_xml_writer->xmlStartTag('AdvancedMetaData');
363 
364  self::preloadByObjIds(array($a_obj_id));
365  $values_records = self::preloadedRead(ilObject::_lookupType($a_obj_id), $a_obj_id);
366 
367  foreach($values_records as $values_record)
368  {
369  $defs = $values_record->getDefinitions();
370  foreach($values_record->getADTGroup()->getElements() as $element_id => $element)
371  {
372  $def = $defs[$element_id];
373 
374  $value = null;
375  if(!$element->isNull())
376  {
377  $value = $def->getValueForXML($element);
378  }
379 
380  $a_xml_writer->xmlElement(
381  'Value',
382  array('id' => $def->getImportId()),
383  $value
384  );
385  }
386  }
387 
388  $a_xml_writer->xmlEndTag('AdvancedMetaData');
389  }
390 
391 
392  //
393  // glossary (might be generic)
394  //
395 
402  static public function queryForRecords($a_obj_id, $a_subtype, $a_records, $a_obj_id_key, $a_obj_subid_key, array $a_amet_filter = null)
403  {
404  $result = array();
405 
406  if (!is_array($a_obj_id))
407  {
408  $a_obj_id = array($a_obj_id);
409  }
410 
411  $sub_obj_ids = array();
412  foreach($a_records as $rec)
413  {
414  $sub_obj_ids[] = $rec[$a_obj_subid_key];
415  }
416 
417  // preload adv data for object id(s)
418  ilADTFactory::getInstance()->initActiveRecordByType();
420  array(
421  "obj_id" => array("integer", $a_obj_id),
422  "sub_type" => array("text", $a_subtype),
423  "sub_id" => array("integer", $sub_obj_ids)
424  ));
425 
426  $record_groups = array();
427 
428  foreach($a_records as $rec)
429  {
430  $obj_id = $rec[$a_obj_id_key];
431  $sub_id = $rec[$a_obj_subid_key];
432 
433  // only active amet records for glossary
435  {
436  $record_id = $adv_record->getRecordId();
437 
438  if(!isset($record_groups[$record_id]))
439  {
442  $record_groups[$record_id] = ilADTFactory::getInstance()->getDBBridgeForInstance($record_groups[$record_id]);
443  $record_groups[$record_id]->setTable("adv_md_values");
444  }
445 
446  // prepare ADT group for record id
447  $record_groups[$record_id]->setPrimary(array(
448  "obj_id" => array("integer", $obj_id),
449  "sub_type" => array("text", $a_subtype),
450  "sub_id" => array("integer", $sub_id)
451  ));
452 
453  // multi-enum fakes single in adv md
454  foreach($record_groups[$record_id]->getElements() as $element)
455  {
456  if($element->getADT()->getType() == "MultiEnum")
457  {
458  $element->setFakeSingle(true);
459  }
460  }
461 
462  // read (preloaded) data
463  $active_record = new ilADTActiveRecordByType($record_groups[$record_id]);
464  $active_record->setElementIdColumn("field_id", "integer");
465  $active_record->read();
466 
467  $adt_group = $record_groups[$record_id]->getADT();
468 
469  // filter against amet values
470  if($a_amet_filter)
471  {
472  foreach($a_amet_filter as $field_id => $element)
473  {
474  if($adt_group->hasElement($field_id))
475  {
476  if(!$element->isInCondition($adt_group->getElement($field_id)))
477  {
478  continue(3);
479  }
480  }
481  }
482  }
483 
484  // add amet values to glossary term record
485  foreach($adt_group->getElements() as $element_id => $element)
486  {
487  if(!$element->isNull())
488  {
489  // we are reusing the ADT group for all $a_records, so we need to clone
490  $pb = ilADTFactory::getInstance()->getPresentationBridgeForInstance(clone $element);
491  $rec["md_".$element_id] = $pb->getSortable();
492  $rec["md_".$element_id."_presentation"] = $pb;
493 
494  }
495  else
496  {
497  $rec["md_".$element_id] = null;
498  }
499  }
500  }
501 
502  $results[] = $rec;
503  }
504 
505  return $results;
506  }
507 }
508 
509 ?>