ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilAdvancedMDRecord.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4
16{
17 private static $instances = array();
18
19 protected $record_id;
20 protected $import_id;
21 protected $active;
22 protected $title;
23 protected $description;
24 protected $obj_types = array();
25 protected $db = null;
26 protected $parent_obj; // [int]
27
37 public function __construct($a_record_id = 0)
38 {
39 global $ilDB;
40
41 $this->record_id = $a_record_id;
42 $this->db = $ilDB;
43
44 if($this->getRecordId())
45 {
46 $this->read();
47 }
48 }
49
58 public static function _getInstanceByRecordId($a_record_id)
59 {
60 if(isset(self::$instances[$a_record_id]))
61 {
62 return self::$instances[$a_record_id];
63 }
64 return self::$instances[$a_record_id] = new ilAdvancedMDRecord($a_record_id);
65 }
66
74 public static function _getActiveSearchableRecords()
75 {
76 global $ilDB;
77
78 $query = "SELECT DISTINCT(amr.record_id) FROM adv_md_record amr ".
79 "JOIN adv_mdf_definition amfd ON amr.record_id = amfd.record_id ".
80 "WHERE searchable = 1 AND active = 1 ";
81
82 $res = $ilDB->query($query);
83 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
84 {
86 }
87 return $records ? $records : array();
88 }
89
98 public static function _lookupTitle($a_record_id)
99 {
100 static $title_cache = array();
101
102 if(isset($title_cache[$a_record_id]))
103 {
104 return $title_cache[$a_record_id];
105 }
106
107 global $ilDB;
108
109 $query = "SELECT title FROM adv_md_record ".
110 "WHERE record_id = ".$ilDB->quote($a_record_id ,'integer')." ";
111 $res = $ilDB->query($query);
113
114 return $title_cache[$a_record_id] = $row->title;
115 }
116
125 public static function _lookupRecordIdByImportId($a_ilias_id)
126 {
127 global $ilDB;
128
129 $query = "SELECT record_id FROM adv_md_record ".
130 "WHERE import_id = ".$ilDB->quote($a_ilias_id ,'text')." ";
131 $res = $ilDB->query($query);
132 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
133 {
134 return $row->record_id;
135 }
136 return 0;
137 }
138
145 public static function _getAssignableObjectTypes($a_include_text = false)
146 {
147 global $objDefinition, $lng;
148
149 $types = array();
150 $amet_types = $objDefinition->getAdvancedMetaDataTypes();
151
152 foreach ($amet_types as $at)
153 {
154 if ($a_include_text)
155 {
156 $text = $lng->txt("obj_".$at["obj_type"]);
157 if ($at["sub_type"] != "")
158 {
159 $lng->loadLanguageModule($at["obj_type"]);
160 $text.= ": ".$lng->txt($at["obj_type"]."_".$at["sub_type"]);
161 }
162 else
163 {
164 $at["sub_type"] = "-";
165 }
166 $at["text"] = $text;
167 }
168
169 $types[] = $at;
170 }
171
172 sort($types);
173 return $types;
174 }
175
184 public static function _getActivatedObjTypes()
185 {
186 global $ilDB;
187
188 $query = "SELECT DISTINCT(obj_type) FROM adv_md_record_objs amo ".
189 "JOIN adv_md_record amr ON amo.record_id = amr.record_id ".
190 "WHERE active = 1 ";
191 $res = $ilDB->query($query);
192 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
193 {
194 $obj_types[] = $row->obj_type;
195 }
196 return $obj_types ? $obj_types : array();
197 }
198
207 public static function _getRecords()
208 {
209 global $ilDB;
210
211 $query = "SELECT record_id FROM adv_md_record ";
212 $res = $ilDB->query($query);
213 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
214 {
216 }
217 return $records ? $records : array();
218 }
219
228 public static function _getAllRecordsByObjectType()
229 {
230 global $ilDB;
231
232 $records = array();
233
234 $query = "SELECT * FROM adv_md_record_objs WHERE sub_type=".$ilDB->quote("-", "text");
235 $res = $ilDB->query($query);
236 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
237 {
238 $records[$row->obj_type][] = self::_getInstanceByRecordId($row->record_id);
239 }
240 return $records;
241 }
242
251 public static function _getActivatedRecordsByObjectType($a_obj_type, $a_sub_type = "", $a_only_optional = false)
252 {
253 global $ilDB;
254
255 $records = array();
256
257 if ($a_sub_type == "")
258 {
259 $a_sub_type = "-";
260 }
261
262 $query = "SELECT amro.record_id record_id FROM adv_md_record_objs amro ".
263 "JOIN adv_md_record amr ON amr.record_id = amro.record_id ".
264 "WHERE active = 1 ".
265 "AND obj_type = ".$ilDB->quote($a_obj_type ,'text')." ".
266 "AND sub_type = ".$ilDB->quote($a_sub_type ,'text');
267
268 if($a_only_optional)
269 {
270 $query .= " AND optional =".$ilDB->quote(1, 'integer');
271 }
272
273 // #16428
274 $query .= "ORDER by parent_obj DESC, record_id";
275
276 $res = $ilDB->query($query);
277 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
278 {
280 }
281
282 return $records;
283 }
284
292 public static function _getSelectedRecordsByObject($a_obj_type, $a_obj_id, $a_sub_type = "")
293 {
294 $records = array();
295
296 if ($a_sub_type == "")
297 {
298 $a_sub_type = "-";
299 }
300
301 // object-wide metadata configuration setting
302 include_once 'Services/Container/classes/class.ilContainer.php';
303 include_once 'Services/Object/classes/class.ilObjectServiceSettingsGUI.php';
304 $config_setting = ilContainer::_lookupContainerSetting(
305 $a_obj_id,
307 false);
308
309 $optional = array();
310 foreach(self::_getActivatedRecordsByObjectType($a_obj_type, $a_sub_type) as $record)
311 {
312 foreach($record->getAssignedObjectTypes() as $item)
313 {
314 if($record->getParentObject())
315 {
316 // only matching local records
317 if($record->getParentObject() != $a_obj_id)
318 {
319 continue;
320 }
321 // if object-wide setting is off, ignore local records
322 else if(!$config_setting)
323 {
324 continue;
325 }
326 }
327
328 if($item['obj_type'] == $a_obj_type &&
329 $item['sub_type'] == $a_sub_type)
330 {
331 if($item['optional'])
332 {
333 $optional[] = $record->getRecordId();
334 }
335 $records[$record->getRecordId()] = $record;
336 }
337 }
338 }
339
340 if($optional)
341 {
342 if(!$config_setting && !in_array($a_sub_type, array("orgu_type", "prg_type"))) //#16925 + #17777
343 {
344 $selected = array();
345 }
346 else
347 {
348 $selected = self::getObjRecSelection($a_obj_id, $a_sub_type);
349 }
350 foreach($optional as $record_id)
351 {
352 if(!in_array($record_id, $selected))
353 {
354 unset($records[$record_id]);
355 }
356 }
357 }
358
359 /* v1 obsolete
360 $query = "SELECT amro.record_id record_id FROM adv_md_record_objs amro ".
361 "JOIN adv_md_record amr ON (amr.record_id = amro.record_id) ".
362 "JOIN adv_md_obj_rec_select os ON (amr.record_id = os.rec_id AND amro.sub_type = os.sub_type) ".
363 "WHERE active = 1 ".
364 "AND amro.obj_type = ".$ilDB->quote($a_obj_type ,'text')." ".
365 "AND amro.sub_type = ".$ilDB->quote($a_sub_type ,'text')." ".
366 "AND os.obj_id = ".$ilDB->quote($a_obj_id ,'integer')
367 ;
368
369 $res = $ilDB->query($query);
370 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
371 {
372 $records[] = self::_getInstanceByRecordId($row->record_id);
373 }
374 */
375
376 return $records;
377 }
378
379
388 public static function _delete($a_record_id)
389 {
390 global $ilDB;
391
392 // Delete fields
393 foreach(ilAdvancedMDFieldDefinition::getInstancesByRecordId($a_record_id) as $field)
394 {
395 $field->delete();
396 }
397
398 $query = "DELETE FROM adv_md_record ".
399 "WHERE record_id = ".$ilDB->quote($a_record_id ,'integer')." ";
400 $res = $ilDB->manipulate($query);
401
402 $query = "DELETE FROM adv_md_record_objs ".
403 "WHERE record_id = ".$ilDB->quote($a_record_id ,'integer')." ";
404 $res = $ilDB->manipulate($query);
405 }
406
407
414 public function delete()
415 {
417 }
418
425 public function save()
426 {
427 global $ilDB;
428
429 // Save import id if given
430 $next_id = $ilDB->nextId('adv_md_record');
431
432 $query = "INSERT INTO adv_md_record (record_id,import_id,active,title,description,parent_obj) ".
433 "VALUES(".
434 $ilDB->quote($next_id,'integer').", ".
435 $this->db->quote($this->getImportId(),'text').", ".
436 $this->db->quote($this->isActive() ,'integer').", ".
437 $this->db->quote($this->getTitle() ,'text').", ".
438 $this->db->quote($this->getDescription() ,'text').", ".
439 $this->db->quote($this->getParentObject() ,'integer')." ".
440 ")";
441 $res = $ilDB->manipulate($query);
442 $this->record_id = $next_id;
443
444 if(!strlen($this->getImportId()))
445 {
446 // set import id to default value
447 $query = "UPDATE adv_md_record ".
448 "SET import_id = ".$this->db->quote($this->generateImportId() ,'text')." ".
449 "WHERE record_id = ".$this->db->quote($this->record_id ,'integer')." ";
450 $res = $ilDB->manipulate($query);
451 }
452
453 foreach($this->getAssignedObjectTypes() as $type)
454 {
455 global $ilDB;
456
457 $query = "INSERT INTO adv_md_record_objs (record_id,obj_type,sub_type,optional) ".
458 "VALUES( ".
459 $this->db->quote($this->getRecordId() ,'integer').", ".
460 $this->db->quote($type["obj_type"] ,'text').", ".
461 $this->db->quote($type["sub_type"] ,'text').", ".
462 $this->db->quote($type["optional"] ,'integer')." ".
463 ")";
464 $res = $ilDB->manipulate($query);
465 }
466 }
467
474 public function update()
475 {
476 global $ilDB;
477
478 $query = "UPDATE adv_md_record ".
479 "SET active = ".$this->db->quote($this->isActive() ,'integer').", ".
480 "title = ".$this->db->quote($this->getTitle() ,'text').", ".
481 "description = ".$this->db->quote($this->getDescription() ,'text')." ".
482 "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
483 $res = $ilDB->manipulate($query);
484
485 // Delete assignments
486 $query = "DELETE FROM adv_md_record_objs ".
487 "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
488 $res = $ilDB->manipulate($query);
489
490 // Insert assignments
491 foreach($this->getAssignedObjectTypes() as $type)
492 {
493 $query = "INSERT INTO adv_md_record_objs (record_id,obj_type,sub_type,optional) ".
494 "VALUES ( ".
495 $this->db->quote($this->getRecordId() ,'integer').", ".
496 $this->db->quote($type["obj_type"] ,'text').", ".
497 $this->db->quote($type["sub_type"] ,'text').", ".
498 $this->db->quote($type["optional"] ,'integer')." ".
499 ")";
500 $res = $ilDB->manipulate($query);
501 }
502 }
503
510 public function validate()
511 {
512 if(!strlen($this->getTitle()))
513 {
514 return false;
515 }
516 return true;
517 }
518
525 public function getRecordId()
526 {
527 return $this->record_id;
528 }
529
537 public function setActive($a_active)
538 {
539 $this->active = $a_active;
540 }
541
548 public function isActive()
549 {
550 return (bool) $this->active;
551 }
552
560 public function setTitle($a_title)
561 {
562 $this->title = $a_title;
563 }
564
571 public function getTitle()
572 {
573 return $this->title;
574 }
575
583 public function setDescription($a_description)
584 {
585 $this->description = $a_description;
586 }
587
594 public function getDescription()
595 {
596 return $this->description;
597 }
598
606 public function setImportId($a_id_string)
607 {
608 $this->import_id = $a_id_string;
609 }
610
617 public function getImportId()
618 {
619 return $this->import_id;
620 }
621
629 public function setAssignedObjectTypes($a_obj_types)
630 {
631 $this->obj_types = $a_obj_types;
632 }
633
641 public function appendAssignedObjectType($a_obj_type, $a_sub_type, $a_optional = false)
642 {
643 $this->obj_types[] = array(
644 "obj_type"=>$a_obj_type,
645 "sub_type"=>$a_sub_type,
646 "optional"=>(bool)$a_optional
647 );
648 }
649
656 public function getAssignedObjectTypes()
657 {
658 return $this->obj_types ? $this->obj_types : array();
659 }
660
667 function isAssignedObjectType($a_obj_type, $a_sub_type)
668 {
669 foreach ($this->getAssignedObjectTypes() as $t)
670 {
671 if ($t["obj_type"] == $a_obj_type &&
672 $t["sub_type"] == $a_sub_type)
673 {
674 return true;
675 }
676 }
677 return false;
678 }
679
680 function setParentObject($a_obj_id)
681 {
682 $this->parent_obj = $a_obj_id;
683 }
684
686 {
687 return $this->parent_obj;
688 }
689
699 public function toXML(ilXmlWriter $writer)
700 {
701 $writer->xmlStartTag('Record',array('active' => $this->isActive() ? 1 : 0,
702 'id' => $this->generateImportId()));
703 $writer->xmlElement('Title',null,$this->getTitle());
704 $writer->xmlElement('Description',null,$this->getDescription());
705
706 foreach($this->getAssignedObjectTypes() as $obj_type)
707 {
708 $optional = array("optional"=>$obj_type["optional"]);
709 if ($obj_type["sub_type"] == "")
710 {
711 $writer->xmlElement('ObjectType',$optional,$obj_type["obj_type"]);
712 }
713 else
714 {
715 $writer->xmlElement('ObjectType',$optional,$obj_type["obj_type"].":".$obj_type["sub_type"]);
716 }
717 }
718
719 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
721 {
722 $definition->toXML($writer);
723 }
724 $writer->xmlEndTag('Record');
725 }
726
734 private function read()
735 {
736 global $ilDB;
737
738 $query = "SELECT * FROM adv_md_record ".
739 "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
740 $res = $this->db->query($query);
741 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
742 {
743 $this->setImportId($row->import_id);
744 $this->setActive($row->active);
745 $this->setTitle($row->title);
746 $this->setDescription($row->description);
747 $this->setParentObject($row->parent_obj);
748 }
749 $query = "SELECT * FROM adv_md_record_objs ".
750 "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
751 $res = $this->db->query($query);
752 while($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT))
753 {
754 $this->obj_types[] = array(
755 "obj_type" => $row->obj_type,
756 "sub_type" => $row->sub_type,
757 "optional" => (bool)$row->optional
758 );
759 }
760 }
761
768 protected function generateImportId()
769 {
770 return 'il_'.IL_INST_ID.'_adv_md_record_'.$this->getRecordId();
771 }
772
779 public function __destruct()
780 {
781 unset(self::$instances[$this->getRecordId()]);
782 }
783
790 static function saveObjRecSelection($a_obj_id, $a_sub_type = "", array $a_records = null, $a_delete_before = true)
791 {
792 global $ilDB;
793
794 if ($a_sub_type == "")
795 {
796 $a_sub_type = "-";
797 }
798
799 if((bool)$a_delete_before)
800 {
801 $ilDB->manipulate("DELETE FROM adv_md_obj_rec_select WHERE ".
802 " obj_id = ".$ilDB->quote($a_obj_id, "integer").
803 " AND sub_type = ".$ilDB->quote($a_sub_type, "text"));
804 }
805
806 if (is_array($a_records))
807 {
808 foreach ($a_records as $r)
809 {
810 if ($r > 0)
811 {
812 $ilDB->manipulate("INSERT INTO adv_md_obj_rec_select ".
813 "(obj_id, rec_id, sub_type) VALUES (".
814 $ilDB->quote($a_obj_id, "integer").",".
815 $ilDB->quote($r, "integer").",".
816 $ilDB->quote($a_sub_type, "text").
817 ")");
818 }
819 }
820 }
821 }
822
829 static function getObjRecSelection($a_obj_id, $a_sub_type = "")
830 {
831 global $ilDB;
832
833 if ($a_sub_type == "")
834 {
835 $a_sub_type = "-";
836 }
837
838 $recs = array();
839 $set = $ilDB->query($r = "SELECT * FROM adv_md_obj_rec_select ".
840 " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer").
841 " AND sub_type = ".$ilDB->quote($a_sub_type, "text")
842 );
843 while ($rec = $ilDB->fetchAssoc($set))
844 {
845 $recs[] = $rec["rec_id"];
846 }
847 return $recs;
848 }
849
857 public function _clone(array &$a_fields_map, $a_parent_obj_id = null)
858 {
859 $new_obj = new self();
860 $new_obj->setActive($this->isActive());
861 $new_obj->setTitle($this->getTitle());
862 $new_obj->setDescription($this->getDescription());
863 $new_obj->setParentObject($a_parent_obj_id
864 ? $a_parent_obj_id
865 : $this->getParentObject());
866 $new_obj->setAssignedObjectTypes($this->getAssignedObjectTypes());
867 $new_obj->save();
868
869 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
871 {
872 $new_def = $definition->_clone($new_obj->getRecordId());
873 $a_fields_map[$definition->getFieldId()] = $new_def->getFieldId();
874 }
875
876 return $new_obj;
877 }
878
887 public static function getSharedRecords($a_obj1_id, $a_obj2_id, $a_sub_type = "-")
888 {
889 $obj_type = ilObject::_lookupType($a_obj1_id);
890 $sel = array_intersect(
891 ilAdvancedMDRecord::getObjRecSelection($a_obj1_id, $a_sub_type),
892 ilAdvancedMDRecord::getObjRecSelection($a_obj2_id, $a_sub_type)
893 );
894
895 $res = array();
896
897 foreach(self::_getRecords() as $record)
898 {
899 // local records cannot be shared
900 if($record->getParentObject())
901 {
902 continue;
903 }
904
905 // :TODO: inactive records can be ignored?
906 if(!$record->isActive())
907 {
908 continue;
909 }
910
911 // parse assigned types
912 foreach($record->getAssignedObjectTypes() as $item)
913 {
914 if($item["obj_type"] == $obj_type &&
915 $item["sub_type"] == $a_sub_type)
916 {
917 // mandatory
918 if(!$item["optional"])
919 {
920 $res[] = $record->getRecordId();
921 }
922 // optional
923 else if(in_array($record->getRecordId(), $sel))
924 {
925 $res[] = $record->getRecordId();
926 }
927 }
928 }
929 }
930
931 return $res;
932 }
933}
934
935?>
An exception for terminatinating execution or to throw for unit testing.
static getInstancesByRecordId($a_record_id, $a_only_searchable=false)
Get definitions by record id.
static _lookupTitle($a_record_id)
Lookup title.
isAssignedObjectType($a_obj_type, $a_sub_type)
Is assigned object type?
static _getSelectedRecordsByObject($a_obj_type, $a_obj_id, $a_sub_type="")
Get selected records by object.
setTitle($a_title)
Set title.
static saveObjRecSelection($a_obj_id, $a_sub_type="", array $a_records=null, $a_delete_before=true)
Save repository object record selection.
static getObjRecSelection($a_obj_id, $a_sub_type="")
Get repository object record selection.
setDescription($a_description)
set description
static getSharedRecords($a_obj1_id, $a_obj2_id, $a_sub_type="-")
Get shared records.
appendAssignedObjectType($a_obj_type, $a_sub_type, $a_optional=false)
append assigned object types
isActive()
Check if record is active.
toXML(ilXmlWriter $writer)
To Xml.
generateImportId()
generate unique record id
static _getActivatedRecordsByObjectType($a_obj_type, $a_sub_type="", $a_only_optional=false)
Get activated records by object type.
static _getAllRecordsByObjectType()
Get records by obj_type Note: this returns only records with no sub types! @access public.
static _getInstanceByRecordId($a_record_id)
Get instance by record id.
static _getActivatedObjTypes()
get activated obj types
setActive($a_active)
Set active.
read()
read record and assiged object types
static _delete($a_record_id)
Delete record and all related data.
validate()
Validate settings.
static _getRecords()
Get records.
setImportId($a_id_string)
set import id
getAssignedObjectTypes()
Get assigned object types.
static _getAssignableObjectTypes($a_include_text=false)
Get assignable object type.
_clone(array &$a_fields_map, $a_parent_obj_id=null)
Clone record.
__construct($a_record_id=0)
Singleton constructor To create an array of new records (without saving them) call the constructor di...
setAssignedObjectTypes($a_obj_types)
Set assigned object types.
static _lookupRecordIdByImportId($a_ilias_id)
Lookup record Id by import id.
static _getActiveSearchableRecords()
Get active searchable records.
static _lookupContainerSetting($a_id, $a_keyword, $a_default_value=NULL)
Lookup a container setting.
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)
$text
$r
Definition: example_031.php:79
global $lng
Definition: privfeed.php:17
$records
Definition: simple_test.php:22
global $ilDB