ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
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
36 public function __construct($a_record_id = 0)
37 {
38 global $ilDB;
39
40 $this->record_id = $a_record_id;
41 $this->db = $ilDB;
42
43 if($this->getRecordId())
44 {
45 $this->read();
46 }
47 }
48
57 public static function _getInstanceByRecordId($a_record_id)
58 {
59 if(isset(self::$instances[$a_record_id]))
60 {
61 return self::$instances[$a_record_id];
62 }
63 return self::$instances[$a_record_id] = new ilAdvancedMDRecord($a_record_id);
64 }
65
73 public static function _getActiveSearchableRecords()
74 {
75 global $ilDB;
76
77 $query = "SELECT DISTINCT(amr.record_id) FROM adv_md_record amr ".
78 "JOIN adv_mdf_definition amfd ON amr.record_id = amfd.record_id ".
79 "WHERE searchable = 1 AND active = 1 ";
80
81 $res = $ilDB->query($query);
82 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
83 {
85 }
86 return $records ? $records : array();
87 }
88
97 public static function _lookupTitle($a_record_id)
98 {
99 static $title_cache = array();
100
101 if(isset($title_cache[$a_record_id]))
102 {
103 return $title_cache[$a_record_id];
104 }
105
106 global $ilDB;
107
108 $query = "SELECT title FROM adv_md_record ".
109 "WHERE record_id = ".$ilDB->quote($a_record_id ,'integer')." ";
110 $res = $ilDB->query($query);
111 $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
112
113 return $title_cache[$a_record_id] = $row->title;
114 }
115
124 public static function _lookupRecordIdByImportId($a_ilias_id)
125 {
126 global $ilDB;
127
128 $query = "SELECT record_id FROM adv_md_record ".
129 "WHERE import_id = ".$ilDB->quote($a_ilias_id ,'text')." ";
130 $res = $ilDB->query($query);
131 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
132 {
133 return $row->record_id;
134 }
135 return 0;
136 }
137
144 public static function _getAssignableObjectTypes($a_include_text = false)
145 {
146 global $objDefinition, $lng;
147
148 $types = array();
149 $amet_types = $objDefinition->getAdvancedMetaDataTypes();
150
151 foreach ($amet_types as $at)
152 {
153 if ($a_include_text)
154 {
155 $text = $lng->txt("obj_".$at["obj_type"]);
156 if ($at["sub_type"] != "")
157 {
158 $lng->loadLanguageModule($at["obj_type"]);
159 $text.= ": ".$lng->txt($at["obj_type"]."_".$at["sub_type"]);
160 }
161 else
162 {
163 $at["sub_type"] = "-";
164 }
165 $at["text"] = $text;
166 }
167
168 $types[] = $at;
169 }
170
171 return $types;
172 return array('cat','crs','rcrs');
173 }
174
183 public static function _getActivatedObjTypes()
184 {
185 global $ilDB;
186
187 $query = "SELECT DISTINCT(obj_type) FROM adv_md_record_objs amo ".
188 "JOIN adv_md_record amr ON amo.record_id = amr.record_id ".
189 "WHERE active = 1 ";
190 $res = $ilDB->query($query);
191 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
192 {
193 $obj_types[] = $row->obj_type;
194 }
195 return $obj_types ? $obj_types : array();
196 }
197
206 public static function _getRecords()
207 {
208 global $ilDB;
209
210 $query = "SELECT record_id FROM adv_md_record ";
211 $res = $ilDB->query($query);
212 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
213 {
215 }
216 return $records ? $records : array();
217 }
218
227 public static function _getAllRecordsByObjectType()
228 {
229 global $ilDB;
230
231 $records = array();
232
233 $query = "SELECT * FROM adv_md_record_objs WHERE sub_type=".$ilDB->quote("-", "text");
234 $res = $ilDB->query($query);
235 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
236 {
237 $records[$row->obj_type][] = self::_getInstanceByRecordId($row->record_id);
238 }
239 return $records;
240 }
241
250 public static function _getActivatedRecordsByObjectType($a_obj_type, $a_sub_type = "")
251 {
252 global $ilDB;
253
254 $records = array();
255
256 if ($a_sub_type == "")
257 {
258 $a_sub_type = "-";
259 }
260
261 $query = "SELECT amro.record_id record_id FROM adv_md_record_objs amro ".
262 "JOIN adv_md_record amr ON amr.record_id = amro.record_id ".
263 "WHERE active = 1 ".
264 "AND obj_type = ".$ilDB->quote($a_obj_type ,'text')." ".
265 "AND sub_type = ".$ilDB->quote($a_sub_type ,'text');
266
267 $res = $ilDB->query($query);
268 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
269 {
271 }
272
273 return $records;
274 }
275
283 public static function _getSelectedRecordsByObject($a_obj_type, $a_obj_id, $a_sub_type = "")
284 {
285 global $ilDB;
286
287 $records = array();
288
289 if ($a_sub_type == "")
290 {
291 $a_sub_type = "-";
292 }
293
294 $query = "SELECT amro.record_id record_id FROM adv_md_record_objs amro ".
295 "JOIN adv_md_record amr ON (amr.record_id = amro.record_id) ".
296 "JOIN adv_md_obj_rec_select os ON (amr.record_id = os.rec_id AND amro.sub_type = os.sub_type) ".
297 "WHERE active = 1 ".
298 "AND amro.obj_type = ".$ilDB->quote($a_obj_type ,'text')." ".
299 "AND amro.sub_type = ".$ilDB->quote($a_sub_type ,'text')." ".
300 "AND os.obj_id = ".$ilDB->quote($a_obj_id ,'integer')
301 ;
302
303 $res = $ilDB->query($query);
304 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
305 {
307 }
308
309 return $records;
310 }
311
312
321 public static function _delete($a_record_id)
322 {
323 global $ilDB;
324
325 // Delete fields
326 foreach(ilAdvancedMDFieldDefinition::getInstancesByRecordId($a_record_id) as $field)
327 {
328 $field->delete();
329 }
330
331 $query = "DELETE FROM adv_md_record ".
332 "WHERE record_id = ".$ilDB->quote($a_record_id ,'integer')." ";
333 $res = $ilDB->manipulate($query);
334
335 $query = "DELETE FROM adv_md_record_objs ".
336 "WHERE record_id = ".$ilDB->quote($a_record_id ,'integer')." ";
337 $res = $ilDB->manipulate($query);
338 }
339
340
347 public function delete()
348 {
350 }
351
358 public function save()
359 {
360 global $ilDB;
361
362 // Save import id if given
363 $next_id = $ilDB->nextId('adv_md_record');
364
365 $query = "INSERT INTO adv_md_record (record_id,import_id,active,title,description) ".
366 "VALUES(".
367 $ilDB->quote($next_id,'integer').", ".
368 $this->db->quote($this->getImportId(),'text').", ".
369 $this->db->quote($this->isActive() ,'integer').", ".
370 $this->db->quote($this->getTitle() ,'text').", ".
371 $this->db->quote($this->getDescription() ,'text')." ".
372 ")";
373 $res = $ilDB->manipulate($query);
374 $this->record_id = $next_id;
375
376 if(!strlen($this->getImportId()))
377 {
378 // set import id to default value
379 $query = "UPDATE adv_md_record ".
380 "SET import_id = ".$this->db->quote($this->generateImportId() ,'text')." ".
381 "WHERE record_id = ".$this->db->quote($this->record_id ,'integer')." ";
382 $res = $ilDB->manipulate($query);
383 }
384
385 foreach($this->getAssignedObjectTypes() as $type)
386 {
387 global $ilDB;
388
389 $query = "INSERT INTO adv_md_record_objs (record_id,obj_type,sub_type) ".
390 "VALUES( ".
391 $this->db->quote($this->getRecordId() ,'integer').", ".
392 $this->db->quote($type["obj_type"] ,'text').", ".
393 $this->db->quote($type["sub_type"] ,'text')." ".
394 ")";
395 $res = $ilDB->manipulate($query);
396 }
397 }
398
405 public function update()
406 {
407 global $ilDB;
408
409 $query = "UPDATE adv_md_record ".
410 "SET active = ".$this->db->quote($this->isActive() ,'integer').", ".
411 "title = ".$this->db->quote($this->getTitle() ,'text').", ".
412 "description = ".$this->db->quote($this->getDescription() ,'text')." ".
413 "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
414 $res = $ilDB->manipulate($query);
415
416 // Delete assignments
417 $query = "DELETE FROM adv_md_record_objs ".
418 "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
419 $res = $ilDB->manipulate($query);
420
421 // Insert assignments
422 foreach($this->getAssignedObjectTypes() as $type)
423 {
424 $query = "INSERT INTO adv_md_record_objs (record_id,obj_type, sub_type) ".
425 "VALUES ( ".
426 $this->db->quote($this->getRecordId() ,'integer').", ".
427 $this->db->quote($type["obj_type"] ,'text').", ".
428 $this->db->quote($type["sub_type"] ,'text')." ".
429 ")";
430 $res = $ilDB->manipulate($query);
431 }
432 }
433
441 public function validate()
442 {
443 global $ilErr,$lng;
444
445 if(!strlen($this->getTitle()))
446 {
447 $ilErr->setMessage('fill_out_all_required_fields');
448 return false;
449 }
450 return true;
451 }
452
459 public function getRecordId()
460 {
461 return $this->record_id;
462 }
463
471 public function setActive($a_active)
472 {
473 $this->active = $a_active;
474 }
475
482 public function isActive()
483 {
484 return (bool) $this->active;
485 }
486
494 public function setTitle($a_title)
495 {
496 $this->title = $a_title;
497 }
498
505 public function getTitle()
506 {
507 return $this->title;
508 }
509
517 public function setDescription($a_description)
518 {
519 $this->description = $a_description;
520 }
521
528 public function getDescription()
529 {
530 return $this->description;
531 }
532
540 public function setImportId($a_id_string)
541 {
542 $this->import_id = $a_id_string;
543 }
544
551 public function getImportId()
552 {
553 return $this->import_id;
554 }
555
563 public function setAssignedObjectTypes($a_obj_types)
564 {
565 $this->obj_types = $a_obj_types;
566 }
567
575 public function appendAssignedObjectType($a_obj_type, $a_sub_type)
576 {
577 $this->obj_types[] = array("obj_type"=>$a_obj_type, "sub_type"=>$a_sub_type);
578 }
579
586 public function getAssignedObjectTypes()
587 {
588 return $this->obj_types ? $this->obj_types : array();
589 }
590
597 function isAssignedObjectType($a_obj_type, $a_sub_type)
598 {
599 foreach ($this->getAssignedObjectTypes() as $t)
600 {
601 if ($t["obj_type"] == $a_obj_type &&
602 $t["sub_type"] == $a_sub_type)
603 {
604 return true;
605 }
606 }
607 return false;
608 }
609
610
620 public function toXML(ilXmlWriter $writer)
621 {
622 $writer->xmlStartTag('Record',array('active' => $this->isActive() ? 1 : 0,
623 'id' => $this->generateImportId()));
624 $writer->xmlElement('Title',null,$this->getTitle());
625 $writer->xmlElement('Description',null,$this->getDescription());
626
627 foreach($this->getAssignedObjectTypes() as $obj_type)
628 {
629 if ($obj_type["sub_type"] == "")
630 {
631 $writer->xmlElement('ObjectType',null,$obj_type["obj_type"]);
632 }
633 else
634 {
635 $writer->xmlElement('ObjectType',null,$obj_type["obj_type"].":".$obj_type["sub_type"]);
636 }
637 }
638
639 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
641 {
642 $definition->toXML($writer);
643 }
644 $writer->xmlEndTag('Record');
645 }
646
654 private function read()
655 {
656 global $ilDB;
657
658 $query = "SELECT * FROM adv_md_record ".
659 "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
660 $res = $this->db->query($query);
661 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
662 {
663 $this->setImportId($row->import_id);
664 $this->setActive($row->active);
665 $this->setTitle($row->title);
666 $this->setDescription($row->description);
667 }
668 $query = "SELECT * FROM adv_md_record_objs ".
669 "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
670 $res = $this->db->query($query);
671 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
672 {
673 $this->obj_types[] = array("obj_type" => $row->obj_type,
674 "sub_type" => $row->sub_type);
675 }
676 }
677
684 protected function generateImportId()
685 {
686 return 'il_'.IL_INST_ID.'_adv_md_record_'.$this->getRecordId();
687 }
688
695 public function __destruct()
696 {
697 unset(self::$instances[$this->getRecordId()]);
698 }
699
706 static function saveObjRecSelection($a_obj_id, $a_sub_type = "", array $a_records = null, $a_delete_before = true)
707 {
708 global $ilDB;
709
710 if ($a_sub_type == "")
711 {
712 $a_sub_type = "-";
713 }
714
715 if((bool)$a_delete_before)
716 {
717 $ilDB->manipulate("DELETE FROM adv_md_obj_rec_select WHERE ".
718 " obj_id = ".$ilDB->quote($a_obj_id, "integer").
719 " AND sub_type = ".$ilDB->quote($a_sub_type, "text"));
720 }
721
722 if (is_array($a_records))
723 {
724 foreach ($a_records as $r)
725 {
726 if ($r > 0)
727 {
728 $ilDB->manipulate("INSERT INTO adv_md_obj_rec_select ".
729 "(obj_id, rec_id, sub_type) VALUES (".
730 $ilDB->quote($a_obj_id, "integer").",".
731 $ilDB->quote($r, "integer").",".
732 $ilDB->quote($a_sub_type, "text").
733 ")");
734 }
735 }
736 }
737 }
738
745 static function getObjRecSelection($a_obj_id, $a_sub_type = "")
746 {
747 global $ilDB;
748
749 if ($a_sub_type == "")
750 {
751 $a_sub_type = "-";
752 }
753
754 $recs = array();
755 $set = $ilDB->query($r = "SELECT * FROM adv_md_obj_rec_select ".
756 " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer").
757 " AND sub_type = ".$ilDB->quote($a_sub_type, "text")
758 );
759 while ($rec = $ilDB->fetchAssoc($set))
760 {
761 $recs[] = $rec["rec_id"];
762 }
763 return $recs;
764 }
765
766}
767?>
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.
static _getActivatedRecordsByObjectType($a_obj_type, $a_sub_type="")
Get activated records by object type.
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
isActive()
Check if record is active.
toXML(ilXmlWriter $writer)
To Xml.
generateImportId()
generate unique record id
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...
appendAssignedObjectType($a_obj_type, $a_sub_type)
append assigned object types
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.
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)
global $lng
Definition: privfeed.php:40
$records
Definition: simple_test.php:17
global $ilDB