ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules Pages
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  {
84  $records[] = self::_getInstanceByRecordId($row->record_id);
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  {
270  $records[] = self::_getInstanceByRecordId($row->record_id);
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  {
306  $records[] = self::_getInstanceByRecordId($row->record_id);
307  }
308 
309  return $records;
310  }
311 
312 
321  public static function _delete($a_record_id)
322  {
323  global $ilDB;
324 
325  // Delete fields
327 
328  $query = "DELETE FROM adv_md_record ".
329  "WHERE record_id = ".$ilDB->quote($a_record_id ,'integer')." ";
330  $res = $ilDB->manipulate($query);
331 
332  $query = "DELETE FROM adv_md_record_objs ".
333  "WHERE record_id = ".$ilDB->quote($a_record_id ,'integer')." ";
334  $res = $ilDB->manipulate($query);
335  }
336 
337 
344  public function delete()
345  {
347  }
348 
355  public function save()
356  {
357  global $ilDB;
358 
359  // Save import id if given
360  $next_id = $ilDB->nextId('adv_md_record');
361 
362  $query = "INSERT INTO adv_md_record (record_id,import_id,active,title,description) ".
363  "VALUES(".
364  $ilDB->quote($next_id,'integer').", ".
365  $this->db->quote($this->getImportId(),'text').", ".
366  $this->db->quote($this->isActive() ,'integer').", ".
367  $this->db->quote($this->getTitle() ,'text').", ".
368  $this->db->quote($this->getDescription() ,'text')." ".
369  ")";
370  $res = $ilDB->manipulate($query);
371  $this->record_id = $next_id;
372 
373  if(!strlen($this->getImportId()))
374  {
375  // set import id to default value
376  $query = "UPDATE adv_md_record ".
377  "SET import_id = ".$this->db->quote($this->generateImportId() ,'text')." ".
378  "WHERE record_id = ".$this->db->quote($this->record_id ,'integer')." ";
379  $res = $ilDB->manipulate($query);
380  }
381 
382  foreach($this->getAssignedObjectTypes() as $type)
383  {
384  global $ilDB;
385 
386  $query = "INSERT INTO adv_md_record_objs (record_id,obj_type,sub_type) ".
387  "VALUES( ".
388  $this->db->quote($this->getRecordId() ,'integer').", ".
389  $this->db->quote($type["obj_type"] ,'text').", ".
390  $this->db->quote($type["sub_type"] ,'text')." ".
391  ")";
392  $res = $ilDB->manipulate($query);
393  }
394  }
395 
402  public function update()
403  {
404  global $ilDB;
405 
406  $query = "UPDATE adv_md_record ".
407  "SET active = ".$this->db->quote($this->isActive() ,'integer').", ".
408  "title = ".$this->db->quote($this->getTitle() ,'text').", ".
409  "description = ".$this->db->quote($this->getDescription() ,'text')." ".
410  "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
411  $res = $ilDB->manipulate($query);
412 
413  // Delete assignments
414  $query = "DELETE FROM adv_md_record_objs ".
415  "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
416  $res = $ilDB->manipulate($query);
417 
418  // Insert assignments
419  foreach($this->getAssignedObjectTypes() as $type)
420  {
421  $query = "INSERT INTO adv_md_record_objs (record_id,obj_type, sub_type) ".
422  "VALUES ( ".
423  $this->db->quote($this->getRecordId() ,'integer').", ".
424  $this->db->quote($type["obj_type"] ,'text').", ".
425  $this->db->quote($type["sub_type"] ,'text')." ".
426  ")";
427  $res = $ilDB->manipulate($query);
428  }
429  }
430 
438  public function validate()
439  {
440  global $ilErr,$lng;
441 
442  if(!strlen($this->getTitle()))
443  {
444  $ilErr->setMessage('fill_out_all_required_fields');
445  return false;
446  }
447  return true;
448  }
449 
456  public function getRecordId()
457  {
458  return $this->record_id;
459  }
460 
468  public function setActive($a_active)
469  {
470  $this->active = $a_active;
471  }
472 
479  public function isActive()
480  {
481  return (bool) $this->active;
482  }
483 
491  public function setTitle($a_title)
492  {
493  $this->title = $a_title;
494  }
495 
502  public function getTitle()
503  {
504  return $this->title;
505  }
506 
514  public function setDescription($a_description)
515  {
516  $this->description = $a_description;
517  }
518 
525  public function getDescription()
526  {
527  return $this->description;
528  }
529 
537  public function setImportId($a_id_string)
538  {
539  $this->import_id = $a_id_string;
540  }
541 
548  public function getImportId()
549  {
550  return $this->import_id;
551  }
552 
560  public function setAssignedObjectTypes($a_obj_types)
561  {
562  $this->obj_types = $a_obj_types;
563  }
564 
572  public function appendAssignedObjectType($a_obj_type, $a_sub_type)
573  {
574  $this->obj_types[] = array("obj_type"=>$a_obj_type, "sub_type"=>$a_sub_type);
575  }
576 
583  public function getAssignedObjectTypes()
584  {
585  return $this->obj_types ? $this->obj_types : array();
586  }
587 
594  function isAssignedObjectType($a_obj_type, $a_sub_type)
595  {
596  foreach ($this->getAssignedObjectTypes() as $t)
597  {
598  if ($t["obj_type"] == $a_obj_type &&
599  $t["sub_type"] == $a_sub_type)
600  {
601  return true;
602  }
603  }
604  return false;
605  }
606 
607 
617  public function toXML(ilXmlWriter $writer)
618  {
619  $writer->xmlStartTag('Record',array('active' => $this->isActive() ? 1 : 0,
620  'id' => $this->generateImportId()));
621  $writer->xmlElement('Title',null,$this->getTitle());
622  $writer->xmlElement('Description',null,$this->getDescription());
623 
624  foreach($this->getAssignedObjectTypes() as $obj_type)
625  {
626  if ($obj_type["sub_type"] == "")
627  {
628  $writer->xmlElement('ObjectType',null,$obj_type["obj_type"]);
629  }
630  else
631  {
632  $writer->xmlElement('ObjectType',null,$obj_type["obj_type"].":".$obj_type["sub_type"]);
633  }
634  }
635 
636  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
638  {
639  $definition->toXML($writer);
640  }
641  $writer->xmlEndTag('Record');
642  }
643 
651  private function read()
652  {
653  global $ilDB;
654 
655  $query = "SELECT * FROM adv_md_record ".
656  "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
657  $res = $this->db->query($query);
658  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
659  {
660  $this->setImportId($row->import_id);
661  $this->setActive($row->active);
662  $this->setTitle($row->title);
663  $this->setDescription($row->description);
664  }
665  $query = "SELECT * FROM adv_md_record_objs ".
666  "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
667  $res = $this->db->query($query);
668  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
669  {
670  $this->obj_types[] = array("obj_type" => $row->obj_type,
671  "sub_type" => $row->sub_type);
672  }
673  }
674 
681  protected function generateImportId()
682  {
683  return 'il_'.IL_INST_ID.'_adv_md_record_'.$this->getRecordId();
684  }
685 
692  public function __destruct()
693  {
694  unset(self::$instances[$this->getRecordId()]);
695  }
696 
703  static function saveObjRecSelection($a_obj_id, $a_sub_type = "", $a_records)
704  {
705  global $ilDB;
706 
707  if ($a_sub_type == "")
708  {
709  $a_sub_type = "-";
710  }
711 
712  $ilDB->manipulate("DELETE FROM adv_md_obj_rec_select WHERE ".
713  " obj_id = ".$ilDB->quote($a_obj_id, "integer").
714  " AND sub_type = ".$ilDB->quote($a_sub_type, "text"));
715 
716  if (is_array($a_records))
717  {
718  foreach ($a_records as $r)
719  {
720  if ($r > 0)
721  {
722  $ilDB->manipulate("INSERT INTO adv_md_obj_rec_select ".
723  "(obj_id, rec_id, sub_type) VALUES (".
724  $ilDB->quote($a_obj_id, "integer").",".
725  $ilDB->quote($r, "integer").",".
726  $ilDB->quote($a_sub_type, "text").
727  ")");
728  }
729  }
730  }
731  }
732 
739  static function getObjRecSelection($a_obj_id, $a_sub_type = "")
740  {
741  global $ilDB;
742 
743  if ($a_sub_type == "")
744  {
745  $a_sub_type = "-";
746  }
747 
748  $recs = array();
749  $set = $ilDB->query($r = "SELECT * FROM adv_md_obj_rec_select ".
750  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer").
751  " AND sub_type = ".$ilDB->quote($a_sub_type, "text")
752  );
753  while ($rec = $ilDB->fetchAssoc($set))
754  {
755  $recs[] = $rec["rec_id"];
756  }
757  return $recs;
758  }
759 
760 }
761 ?>
static _getDefinitionsByRecordId($a_record_id)
get definitions
appendAssignedObjectType($a_obj_type, $a_sub_type)
append assigned object types
static _deleteByRecordId($a_record_id)
Delete all fields by record_id.
static _getRecords()
Get records.
getDescription()
get description
setActive($a_active)
Set active.
xmlStartTag($tag, $attrs=NULL, $empty=FALSE, $encode=TRUE, $escape=TRUE)
Writes a starttag.
XML writer class.
xmlElement($tag, $attrs=NULL, $data=Null, $encode=TRUE, $escape=TRUE)
Writes a basic element (no children, just textual content)
static _getActiveSearchableRecords()
Get active searchable records.
$records
Definition: simple_test.php:22
static _getActivatedRecordsByObjectType($a_obj_type, $a_sub_type="")
Get activated records by object type.
xmlEndTag($tag)
Writes an endtag.
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
isActive()
Check if record is active.
setDescription($a_description)
set description
static _delete($a_record_id)
Delete record and all related data.
toXML(ilXmlWriter $writer)
To Xml.
static _getInstanceByRecordId($a_record_id)
Get instance by record id.
__construct($a_record_id=0)
Singleton constructor To create an array of new records (without saving them) call the constructor di...
static _getSelectedRecordsByObject($a_obj_type, $a_obj_id, $a_sub_type="")
Get selected records by object.
validate()
Validate settings Write error message to ilErr.
getAssignedObjectTypes()
Get assigned object types.
static _getAllRecordsByObjectType()
Get records by obj_type Note: this returns only records with no sub types! public.
setImportId($a_id_string)
set import id
static _lookupTitle($a_record_id)
Lookup title.
setTitle($a_title)
Set title.
read()
read record and assiged object types
static _getAssignableObjectTypes($a_include_text=false)
Get assignable object type.
setAssignedObjectTypes($a_obj_types)
Set assigned object types.
global $lng
Definition: privfeed.php:40
static _getActivatedObjTypes()
get activated obj types
isAssignedObjectType($a_obj_type, $a_sub_type)
Is assigned object type?
static saveObjRecSelection($a_obj_id, $a_sub_type="", $a_records)
Save repository object record selection.
static getObjRecSelection($a_obj_id, $a_sub_type="")
Get repository object record selection.
static _lookupRecordIdByImportId($a_ilias_id)
Lookup record Id by import id.
$r
generateImportId()
generate unique record id