ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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(DB_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);
112 $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
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(DB_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(DB_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(DB_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(DB_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(DB_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(DB_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
511 public function validate()
512 {
513 global $ilErr,$lng;
514
515 if(!strlen($this->getTitle()))
516 {
517 $ilErr->setMessage('fill_out_all_required_fields');
518 return false;
519 }
520 return true;
521 }
522
529 public function getRecordId()
530 {
531 return $this->record_id;
532 }
533
541 public function setActive($a_active)
542 {
543 $this->active = $a_active;
544 }
545
552 public function isActive()
553 {
554 return (bool) $this->active;
555 }
556
564 public function setTitle($a_title)
565 {
566 $this->title = $a_title;
567 }
568
575 public function getTitle()
576 {
577 return $this->title;
578 }
579
587 public function setDescription($a_description)
588 {
589 $this->description = $a_description;
590 }
591
598 public function getDescription()
599 {
600 return $this->description;
601 }
602
610 public function setImportId($a_id_string)
611 {
612 $this->import_id = $a_id_string;
613 }
614
621 public function getImportId()
622 {
623 return $this->import_id;
624 }
625
633 public function setAssignedObjectTypes($a_obj_types)
634 {
635 $this->obj_types = $a_obj_types;
636 }
637
645 public function appendAssignedObjectType($a_obj_type, $a_sub_type, $a_optional = false)
646 {
647 $this->obj_types[] = array(
648 "obj_type"=>$a_obj_type,
649 "sub_type"=>$a_sub_type,
650 "optional"=>(bool)$a_optional
651 );
652 }
653
660 public function getAssignedObjectTypes()
661 {
662 return $this->obj_types ? $this->obj_types : array();
663 }
664
671 function isAssignedObjectType($a_obj_type, $a_sub_type)
672 {
673 foreach ($this->getAssignedObjectTypes() as $t)
674 {
675 if ($t["obj_type"] == $a_obj_type &&
676 $t["sub_type"] == $a_sub_type)
677 {
678 return true;
679 }
680 }
681 return false;
682 }
683
684 function setParentObject($a_obj_id)
685 {
686 $this->parent_obj = $a_obj_id;
687 }
688
690 {
691 return $this->parent_obj;
692 }
693
703 public function toXML(ilXmlWriter $writer)
704 {
705 $writer->xmlStartTag('Record',array('active' => $this->isActive() ? 1 : 0,
706 'id' => $this->generateImportId()));
707 $writer->xmlElement('Title',null,$this->getTitle());
708 $writer->xmlElement('Description',null,$this->getDescription());
709
710 foreach($this->getAssignedObjectTypes() as $obj_type)
711 {
712 $optional = array("optional"=>$obj_type["optional"]);
713 if ($obj_type["sub_type"] == "")
714 {
715 $writer->xmlElement('ObjectType',$optional,$obj_type["obj_type"]);
716 }
717 else
718 {
719 $writer->xmlElement('ObjectType',$optional,$obj_type["obj_type"].":".$obj_type["sub_type"]);
720 }
721 }
722
723 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
725 {
726 $definition->toXML($writer);
727 }
728 $writer->xmlEndTag('Record');
729 }
730
738 private function read()
739 {
740 global $ilDB;
741
742 $query = "SELECT * FROM adv_md_record ".
743 "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
744 $res = $this->db->query($query);
745 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
746 {
747 $this->setImportId($row->import_id);
748 $this->setActive($row->active);
749 $this->setTitle($row->title);
750 $this->setDescription($row->description);
751 $this->setParentObject($row->parent_obj);
752 }
753 $query = "SELECT * FROM adv_md_record_objs ".
754 "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
755 $res = $this->db->query($query);
756 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
757 {
758 $this->obj_types[] = array(
759 "obj_type" => $row->obj_type,
760 "sub_type" => $row->sub_type,
761 "optional" => (bool)$row->optional
762 );
763 }
764 }
765
772 protected function generateImportId()
773 {
774 return 'il_'.IL_INST_ID.'_adv_md_record_'.$this->getRecordId();
775 }
776
783 public function __destruct()
784 {
785 unset(self::$instances[$this->getRecordId()]);
786 }
787
794 static function saveObjRecSelection($a_obj_id, $a_sub_type = "", array $a_records = null, $a_delete_before = true)
795 {
796 global $ilDB;
797
798 if ($a_sub_type == "")
799 {
800 $a_sub_type = "-";
801 }
802
803 if((bool)$a_delete_before)
804 {
805 $ilDB->manipulate("DELETE FROM adv_md_obj_rec_select WHERE ".
806 " obj_id = ".$ilDB->quote($a_obj_id, "integer").
807 " AND sub_type = ".$ilDB->quote($a_sub_type, "text"));
808 }
809
810 if (is_array($a_records))
811 {
812 foreach ($a_records as $r)
813 {
814 if ($r > 0)
815 {
816 $ilDB->manipulate("INSERT INTO adv_md_obj_rec_select ".
817 "(obj_id, rec_id, sub_type) VALUES (".
818 $ilDB->quote($a_obj_id, "integer").",".
819 $ilDB->quote($r, "integer").",".
820 $ilDB->quote($a_sub_type, "text").
821 ")");
822 }
823 }
824 }
825 }
826
833 static function getObjRecSelection($a_obj_id, $a_sub_type = "")
834 {
835 global $ilDB;
836
837 if ($a_sub_type == "")
838 {
839 $a_sub_type = "-";
840 }
841
842 $recs = array();
843 $set = $ilDB->query($r = "SELECT * FROM adv_md_obj_rec_select ".
844 " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer").
845 " AND sub_type = ".$ilDB->quote($a_sub_type, "text")
846 );
847 while ($rec = $ilDB->fetchAssoc($set))
848 {
849 $recs[] = $rec["rec_id"];
850 }
851 return $recs;
852 }
853
854}
855?>
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
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
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 Write error message to ilErr.
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.
__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.
_lookupContainerSetting($a_id, $a_keyword, $a_default_value=NULL)
Lookup a container setting.
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:40
$records
Definition: simple_test.php:17
global $ilDB