ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObject.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
13 class ilObject
14 {
20  var $ilias;
21 
27  var $lng;
28 
34  var $id; // true object_id!!!!
35  var $ref_id;// reference_id
36  var $type;
37  var $title;
38  // BEGIN WebDAV: WebDAV needs to access the untranslated title of an object
40  // END WebDAV: WebDAV needs to access the untranslated title of an object
41  var $desc;
43  var $owner;
47  var $register = false; // registering required for object? set to true to implement a subscription interface
48 
55 
62 
68 
73  var $max_desc;
74 
79  var $add_dots;
80 
85 
92  function ilObject($a_id = 0, $a_reference = true)
93  {
94  global $ilias, $lng, $ilBench;
95 
96  $ilBench->start("Core", "ilObject_Constructor");
97 
98  if (DEBUG)
99  {
100  echo "<br/><font color=\"red\">type(".$this->type.") id(".$a_id.") referenced(".$a_reference.")</font>";
101  }
102 
103  $this->ilias =& $ilias;
104  $this->lng =& $lng;
105 
106  $this->max_title = MAXLENGTH_OBJ_TITLE;
107  $this->max_desc = MAXLENGTH_OBJ_DESC;
108  $this->add_dots = true;
109 
110  $this->referenced = $a_reference;
111  $this->call_by_reference = $a_reference;
112 
113  if ($a_id == 0)
114  {
115  $this->referenced = false; // newly created objects are never referenced
116  } // they will get referenced if createReference() is called
117 
118  if ($this->referenced)
119  {
120  $this->ref_id = $a_id;
121  }
122  else
123  {
124  $this->id = $a_id;
125  }
126  // read object data
127  if ($a_id != 0)
128  {
129  $this->read();
130  }
131 
132  $ilBench->stop("Core", "ilObject_Constructor");
133  }
134 
138  function withReferences()
139  {
140  // both vars could differ. this method should always return true if one of them is true without changing their status
141  return ($this->call_by_reference) ? true : $this->referenced;
142  }
143 
144 
150  function read($a_force_db = false)
151  {
152  global $objDefinition, $ilBench, $ilDB, $log;
153 
154  $ilBench->start("Core", "ilObject_read");
155 
156  if (isset($this->obj_data_record) && !$a_force_db)
157  {
158  $obj = $this->obj_data_record;
159  }
160  else if ($this->referenced)
161  {
162  // check reference id
163  if (!isset($this->ref_id))
164  {
165  $message = "ilObject::read(): No ref_id given! (".$this->type.")";
166  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
167  }
168 
169  // read object data
170  $ilBench->start("Core", "ilObject_read_readData");
171 
172  $q = "SELECT * FROM object_data, object_reference WHERE object_data.obj_id=object_reference.obj_id ".
173  "AND object_reference.ref_id= ".$ilDB->quote($this->ref_id, "integer");
174  $object_set = $ilDB->query($q);
175  $ilBench->stop("Core", "ilObject_read_readData");
176 
177  // check number of records
178  if ($ilDB->numRows($object_set) == 0)
179  {
180  $message = "ilObject::read(): Object with ref_id ".$this->ref_id." not found! (".$this->type.")";
181  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
182  }
183 
184  $obj = $ilDB->fetchAssoc($object_set);
185  }
186  else
187  {
188  // check object id
189  if (!isset($this->id))
190  {
191  $message = "ilObject::read(): No obj_id given! (".$this->type.")";
192  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
193  }
194 
195  // read object data
196  $q = "SELECT * FROM object_data ".
197  "WHERE obj_id = ".$ilDB->quote($this->id, "integer");
198  $object_set = $ilDB->query($q);
199 
200  // check number of records
201  if ($ilDB->numRows($object_set) == 0)
202  {
203  $message = "ilObject::read(): Object with obj_id: ".$this->id." (".$this->type.") not found!";
204  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
205  }
206 
207  $obj = $ilDB->fetchAssoc($object_set);
208  }
209 
210  $this->id = $obj["obj_id"];
211 
212  // check type match (the "xxx" type is used for the unit test)
213  if ($this->type != $obj["type"] && $obj["type"] != "xxx")
214  {
215  $message = "ilObject::read(): Type mismatch. Object with obj_id: ".$this->id." ".
216  "was instantiated by type '".$this->type."'. DB type is: ".$obj["type"];
217 
218  // write log entry
219  $log->write($message);
220 
221  // raise error
222  $this->ilias->raiseError("ilObject::read(): Type mismatch. (".$this->type."/".$this->id.")",$this->ilias->error_obj->WARNING);
223  }
224 
225  $this->type = $obj["type"];
226  $this->title = $obj["title"];
227  // BEGIN WebDAV: WebDAV needs to access the untranslated title of an object
228  $this->untranslatedTitle = $obj["title"];
229  // END WebDAV: WebDAV needs to access the untranslated title of an object
230  $this->desc = $obj["description"];
231  $this->owner = $obj["owner"];
232  $this->create_date = $obj["create_date"];
233  $this->last_update = $obj["last_update"];
234  $this->import_id = $obj["import_id"];
235 
236  if($objDefinition->isRBACObject($this->getType()))
237  {
238  // Read long description
239  $query = "SELECT * FROM object_description WHERE obj_id = ".$ilDB->quote($this->id,'integer');
240  $res = $this->ilias->db->query($query);
241  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
242  {
243  if(strlen($row->description))
244  {
245  $this->setDescription($row->description);
246  }
247  }
248  }
249 
250  // multilingual support systemobjects (sys) & categories (db)
251  $ilBench->start("Core", "ilObject_Constructor_getTranslation");
252  $translation_type = $objDefinition->getTranslationType($this->type);
253 
254  if ($translation_type == "sys")
255  {
256  $this->title = $this->lng->txt("obj_".$this->type);
257  $this->desc = $this->lng->txt("obj_".$this->type."_desc");
258  }
259  elseif ($translation_type == "db")
260  {
261  $q = "SELECT title,description FROM object_translation ".
262  "WHERE obj_id = ".$ilDB->quote($this->id,'integer')." ".
263  "AND lang_code = ".$ilDB->quote($this->ilias->account->getCurrentLanguage(),'text')." ".
264  "AND NOT lang_default = 1";
265  $r = $this->ilias->db->query($q);
266  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
267 
268  if ($row)
269  {
270  $this->title = $row->title;
271  $this->setDescription($row->description);
272  #$this->desc = $row->description;
273  }
274  }
275 
276  $ilBench->stop("Core", "ilObject_Constructor_getTranslation");
277 
278  $ilBench->stop("Core", "ilObject_read");
279  }
280 
286  function getId()
287  {
288  return $this->id;
289  }
290 
296  function setId($a_id)
297  {
298  $this->id = $a_id;
299  }
300 
306  function setRefId($a_id)
307  {
308  $this->ref_id = $a_id;
309  $this->referenced = true;
310  }
311 
317  function getRefId()
318  {
319  return $this->ref_id;
320  }
321 
327  function getType()
328  {
329  return $this->type;
330  }
331 
337  function setType($a_type)
338  {
339  $this->type = $a_type;
340  }
341 
351  public function getPresentationTitle()
352  {
353  return $this->getTitle();
354  }
355 
356 
362  function getTitle()
363  {
364  return $this->title;
365  }
366  // BEGIN WebDAV: WebDAV needs to access the untranslated title of an object
373  {
375  }
376  // END WebDAV: WebDAV needs to access the untranslated title of an object
377 
384  function setTitle($a_title)
385  {
386  $this->title = ilUtil::shortenText($a_title, $this->max_title, $this->add_dots);
387  // BEGIN WebDAV: WebDAV needs to access the untranslated title of an object
388  $this->untranslatedTitle = $this->title;
389  // END WebDAV: WebDAV needs to access the untranslated title of an object
390  }
391 
398  function getDescription()
399  {
400  return $this->desc;
401  }
402 
409  function setDescription($a_desc)
410  {
411  // Shortened form is storted in object_data. Long form is stored in object_description
412  $this->desc = ilUtil::shortenText($a_desc, $this->max_desc, $this->add_dots);
413 
414  $this->long_desc = $a_desc;
415 
416  return true;
417  }
418 
426  {
427  return strlen($this->long_desc) ? $this->long_desc : $this->desc;
428  }
429 
436  function getImportId()
437  {
438  return $this->import_id;
439  }
440 
447  function setImportId($a_import_id)
448  {
449  $this->import_id = $a_import_id;
450  }
451 
452  function _lookupObjIdByImportId($a_import_id)
453  {
454  global $ilDB;
455 
456  $query = "SELECT * FROM object_data ".
457  "WHERE import_id = ".$ilDB->quote($a_import_id, "text")." ".
458  "ORDER BY create_date DESC";
459  $res = $ilDB->query($query);
460  while($row = $ilDB->fetchObject($res))
461  {
462  return $row->obj_id;
463  }
464  return 0;
465  }
466 
473  function getOwner()
474  {
475  return $this->owner;
476  }
477 
478  /*
479  * get full name of object owner
480  *
481  * @access public
482  * @return string owner name or unknown
483  */
484  function getOwnerName()
485  {
486  return ilObject::_lookupOwnerName($this->getOwner());
487  }
488 
492  function _lookupOwnerName($a_owner_id)
493  {
494  global $lng;
495 
496  if ($a_owner_id != -1)
497  {
498  if (ilObject::_exists($a_owner_id))
499  {
500  $owner = new ilObjUser($a_owner_id);
501  }
502  }
503 
504  if (is_object($owner))
505  {
506  $own_name = $owner->getFullname();
507  }
508  else
509  {
510  $own_name = $lng->txt("unknown");
511  }
512 
513  return $own_name;
514  }
515 
522  function setOwner($a_owner)
523  {
524  $this->owner = $a_owner;
525  }
526 
527 
528 
534  function getCreateDate()
535  {
536  return $this->create_date;
537  }
538 
544  function getLastUpdateDate()
545  {
546  return $this->last_update;
547  }
548 
549 
561  function getDiskUsage()
562  {
563  return null;
564  }
565 
574  function setObjDataRecord($a_record)
575  {
576  $this->obj_data_record = $a_record;
577  }
578 
587  function create()
588  {
589  global $ilDB, $log,$ilUser,$objDefinition;
590 
591  if (!isset($this->type))
592  {
593  $message = get_class($this)."::create(): No object type given!";
594  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
595  }
596 
597  // write log entry
598  $log->write("ilObject::create(), start");
599 
600  $this->title = ilUtil::shortenText($this->getTitle(), $this->max_title, $this->add_dots);
601  $this->desc = ilUtil::shortenText($this->getDescription(), $this->max_desc, $this->add_dots);
602 
603  // determine owner
604  if ($this->getOwner() > 0)
605  {
606  $owner = $this->getOwner();
607  }
608  elseif(is_object($ilUser))
609  {
610  $owner = $ilUser->getId();
611  }
612  else
613  {
614  $owner = 0;
615  }
616  $this->id = $ilDB->nextId("object_data");
617  $q = "INSERT INTO object_data ".
618  "(obj_id,type,title,description,owner,create_date,last_update,import_id) ".
619  "VALUES ".
620  "(".
621  $ilDB->quote($this->id, "integer").",".
622  $ilDB->quote($this->type, "text").",".
623  $ilDB->quote($this->getTitle(), "text").",".
624  $ilDB->quote($this->getDescription(), "text").",".
625  $ilDB->quote($owner, "integer").",".
626  $ilDB->now().",".
627  $ilDB->now().",".
628  $ilDB->quote($this->getImportId(), "text").")";
629 
630  $ilDB->manipulate($q);
631 
632  //$this->id = $ilDB->getLastInsertId();
633 
634 
635 
636  // Save long form of description if is rbac object
637  if($objDefinition->isRBACObject($this->getType()))
638  {
639  $values = array(
640  'obj_id' => array('integer',$this->id),
641  'description' => array('clob', $this->getLongDescription()));
642 //var_dump($values);
643  $ilDB->insert('object_description',$values);
644  }
645 
646 
647  // the line ($this->read();) messes up meta data handling: meta data,
648  // that is not saved at this time, gets lost, so we query for the dates alone
649  //$this->read();
650  $q = "SELECT last_update, create_date FROM object_data".
651  " WHERE obj_id = ".$ilDB->quote($this->id, "integer");
652  $obj_set = $ilDB->query($q);
653  $obj_rec = $ilDB->fetchAssoc($obj_set);
654  $this->last_update = $obj_rec["last_update"];
655  $this->create_date = $obj_rec["create_date"];
656 
657  // set owner for new objects
658  $this->setOwner($owner);
659 
660  // write log entry
661  $log->write("ilObject::create(), finished, obj_id: ".$this->id.", type: ".
662  $this->type.", title: ".$this->getTitle());
663 
664  $GLOBALS['ilAppEventHandler']->raise(
665  'Services/Object',
666  'create',
667  array('obj_id' => $this->id,'obj_type' => $this->type));
668 
669  return $this->id;
670  }
671 
678  function update()
679  {
680  global $objDefinition, $ilDB;
681 
682  $q = "UPDATE object_data ".
683  "SET ".
684  "title = ".$ilDB->quote($this->getTitle(), "text").",".
685  "description = ".$ilDB->quote($this->getDescription(), "text").", ".
686  "import_id = ".$ilDB->quote($this->getImportId(), "text").",".
687  "last_update = ".$ilDB->now()." ".
688  "WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
689  $ilDB->manipulate($q);
690 
691  // the line ($this->read();) messes up meta data handling: meta data,
692  // that is not saved at this time, gets lost, so we query for the dates alone
693  //$this->read();
694  $q = "SELECT last_update FROM object_data".
695  " WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
696  $obj_set = $ilDB->query($q);
697  $obj_rec = $ilDB->fetchAssoc($obj_set);
698  $this->last_update = $obj_rec["last_update"];
699 
700  if($objDefinition->isRBACObject($this->getType()))
701  {
702  // Update long description
703  $res = $this->ilias->db->query("SELECT * FROM object_description WHERE obj_id = ".
704  $ilDB->quote($this->getId(),'integer'));
705  if($res->numRows())
706  {
707  $values = array(
708  'description' => array('clob',$this->getLongDescription())
709  );
710  $ilDB->update('object_description',$values,array('obj_id' => array('integer',$this->getId())));
711  }
712  else
713  {
714  $values = array(
715  'description' => array('clob',$this->getLongDescription()),
716  'obj_id' => array('integer',$this->getId()));
717  $ilDB->insert('object_description',$values);
718  }
719  }
720  $GLOBALS['ilAppEventHandler']->raise(
721  'Services/Object',
722  'update',
723  array('obj_id' => $this->getId(),
724  'obj_type' => $this->getType(),
725  'ref_id' => $this->getRefId()));
726 
727  return true;
728  }
729 
741  function MDUpdateListener($a_element)
742  {
743  include_once 'Services/MetaData/classes/class.ilMD.php';
744 
745  $GLOBALS['ilAppEventHandler']->raise(
746  'Services/Object',
747  'update',
748  array('obj_id' => $this->getId(),
749  'obj_type' => $this->getType(),
750  'ref_id' => $this->getRefId()));
751 
752  switch($a_element)
753  {
754  case 'General':
755 
756  // Update Title and description
757  $md = new ilMD($this->getId(),0, $this->getType());
758  if(!is_object($md_gen = $md->getGeneral()))
759  {
760  return false;
761  }
762  $this->setTitle($md_gen->getTitle());
763 
764  foreach($md_gen->getDescriptionIds() as $id)
765  {
766  $md_des = $md_gen->getDescription($id);
767  $this->setDescription($md_des->getDescription());
768  break;
769  }
770  $this->update();
771  break;
772 
773  default:
774  }
775 
776  return true;
777  }
778 
782  function createMetaData()
783  {
784  include_once 'Services/MetaData/classes/class.ilMDCreator.php';
785 
786  global $ilUser;
787 
788  $md_creator = new ilMDCreator($this->getId(),0,$this->getType());
789  $md_creator->setTitle($this->getTitle());
790  $md_creator->setTitleLanguage($ilUser->getPref('language'));
791  $md_creator->setDescription($this->getLongDescription());
792  $md_creator->setDescriptionLanguage($ilUser->getPref('language'));
793  $md_creator->setKeywordLanguage($ilUser->getPref('language'));
794  $md_creator->setLanguage($ilUser->getPref('language'));
795  $md_creator->create();
796 
797  return true;
798  }
799 
803  function updateMetaData()
804  {
805  include_once("Services/MetaData/classes/class.ilMD.php");
806  include_once("Services/MetaData/classes/class.ilMDGeneral.php");
807  include_once("Services/MetaData/classes/class.ilMDDescription.php");
808 
809  $md =& new ilMD($this->getId(), 0, $this->getType());
810  $md_gen =& $md->getGeneral();
811  // BEGIN WebDAV: meta data can be missing sometimes.
812  if ($md_gen == null)
813  {
814  $this->createMetaData();
815  $md =& new ilMD($this->getId(), 0, $this->getType());
816  $md_gen =& $md->getGeneral();
817  }
818  // END WebDAV: meta data can be missing sometimes.
819  $md_gen->setTitle($this->getTitle());
820 
821  // sets first description (maybe not appropriate)
822  $md_des_ids =& $md_gen->getDescriptionIds();
823  if (count($md_des_ids) > 0)
824  {
825  $md_des =& $md_gen->getDescription($md_des_ids[0]);
826  $md_des->setDescription($this->getLongDescription());
827  $md_des->update();
828  }
829  $md_gen->update();
830 
831  }
832 
836  function deleteMetaData()
837  {
838  // Delete meta data
839  include_once('Services/MetaData/classes/class.ilMD.php');
840  $md = new ilMD($this->getId(), 0, $this->getType());
841  $md->deleteAll();
842  }
843 
850  function updateOwner()
851  {
852  global $ilDB;
853 
854  $q = "UPDATE object_data ".
855  "SET ".
856  "owner = ".$ilDB->quote($this->getOwner(), "integer").", ".
857  "last_update = ".$ilDB->now()." ".
858  "WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
859  $ilDB->manipulate($q);
860 
861  $q = "SELECT last_update FROM object_data".
862  " WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
863  $obj_set = $ilDB->query($q);
864  $obj_rec = $ilDB->fetchAssoc($obj_set);
865  $this->last_update = $obj_rec["last_update"];
866 
867  return true;
868  }
869 
877  function _getIdForImportId($a_import_id)
878  {
879  global $ilDB;
880 
881  $ilDB->setLimit(1,0);
882  $q = "SELECT * FROM object_data WHERE import_id = ".$ilDB->quote($a_import_id, "text").
883  " ORDER BY create_date DESC";
884  $obj_set = $ilDB->query($q);
885 
886  if ($obj_rec = $ilDB->fetchAssoc($obj_set))
887  {
888  return $obj_rec["obj_id"];
889  }
890  else
891  {
892  return 0;
893  }
894  }
895 
901  function _getAllReferences($a_id)
902  {
903  global $ilDB;
904 
905  $query = "SELECT * FROM object_reference WHERE obj_id = ".
906  $ilDB->quote($a_id,'integer');
907 
908  $res = $ilDB->query($query);
909  $ref = array();
910  while($obj_rec = $ilDB->fetchAssoc($res))
911  {
912  $ref[$obj_rec["ref_id"]] = $obj_rec["ref_id"];
913  }
914 
915  return $ref;
916  }
917 
923  public static function _lookupTitle($a_id)
924  {
925  global $ilObjDataCache;
926 
927  $tit = $ilObjDataCache->lookupTitle($a_id);
928 //echo "<br>LOOKING-$a_id-:$tit";
929  return $tit;
930  }
931 
937  function _lookupOwner($a_id)
938  {
939  global $ilObjDataCache;
940 
941  $owner = $ilObjDataCache->lookupOwner($a_id);
942  return $owner;
943  }
944 
945  public static function _getIdsForTitle($title, $type = '', $partialmatch = false)
946  {
947  global $ilDB;
948 
949  $query = (!$partialmatch)
950  ? "SELECT obj_id FROM object_data WHERE title = ".$ilDB->quote($title, "text")
951  : "SELECT obj_id FROM object_data WHERE ".$ilDB->like("title", "text", '%'.$title.'%');
952  if($type != '')
953  {
954  $query .= " AND type = ".$ilDB->quote($type, "text");
955  }
956 
957  $result = $ilDB->query($query);
958 
959  $object_ids = array();
960  while($row = $ilDB->fetchAssoc($result))
961  {
962  $object_ids[] = $row['obj_id'];
963  }
964 
965  return is_array($object_ids) ? $object_ids : array();
966  }
967 
973  function _lookupDescription($a_id)
974  {
975  global $ilObjDataCache;
976 
977  return $ilObjDataCache->lookupDescription($a_id);
978  }
979 
985  function _lookupLastUpdate($a_id, $a_as_string = false)
986  {
987  global $ilObjDataCache;
988 
989  if ($a_as_string)
990  {
991  return ilDatePresentation::formatDate(new ilDateTime($ilObjDataCache->lookupLastUpdate($a_id),IL_CAL_DATETIME));
992  }
993  else
994  {
995  return $ilObjDataCache->lookupLastUpdate($a_id);
996  }
997  }
998 
1004  function _getLastUpdateOfObjects($a_objs)
1005  {
1006  global $ilDB;
1007 
1008  if (!is_array($a_objs))
1009  {
1010  $a_objs = array($a_objs);
1011  }
1012  $types = array();
1013  $set = $ilDB->query("SELECT max(last_update) as last_update FROM object_data ".
1014  "WHERE ".$ilDB->in("obj_id", $a_objs, false, "integer")." ");
1015  $rec = $ilDB->fetchAssoc($set);
1016 
1017  return ($rec["last_update"]);
1018  }
1019 
1020  public static function _lookupObjId($a_id)
1021  {
1022  global $ilObjDataCache;
1023 
1024  return (int) $ilObjDataCache->lookupObjId($a_id);
1025  }
1026 
1030  function _setDeletedDate($a_ref_id)
1031  {
1032  global $ilDB;
1033 
1034  $query = "UPDATE object_reference SET deleted= ".$ilDB->now().' '.
1035  "WHERE ref_id = ".$ilDB->quote($a_ref_id,'integer');
1036  $res = $ilDB->manipulate($query);
1037  }
1038 
1042  function _resetDeletedDate($a_ref_id)
1043  {
1044  global $ilDB;
1045 
1046  $query = "UPDATE object_reference SET deleted = ".$ilDB->quote(null,'timestamp').
1047  " WHERE ref_id = ".$ilDB->quote($a_ref_id,'integer');
1048  $res = $ilDB->manipulate($query);
1049  }
1050 
1054  function _lookupDeletedDate($a_ref_id)
1055  {
1056  global $ilDB;
1057 
1058  $query = "SELECT deleted FROM object_reference".
1059  " WHERE ref_id = ".$ilDB->quote($a_ref_id, "integer");
1060  $set = $ilDB->query($query);
1061  $rec = $ilDB->fetchAssoc($set);
1062 
1063  return $rec["deleted"];
1064  }
1065 
1066 
1074  function _writeTitle($a_obj_id, $a_title)
1075  {
1076  global $ilDB;
1077 
1078  $q = "UPDATE object_data ".
1079  "SET ".
1080  "title = ".$ilDB->quote($a_title, "text").",".
1081  "last_update = ".$ilDB->now()." ".
1082  "WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
1083 
1084  $ilDB->manipulate($q);
1085  }
1086 
1094  function _writeDescription($a_obj_id, $a_desc)
1095  {
1096  global $ilDB,$objDefinition;
1097 
1098 
1099  $desc = ilUtil::shortenText($a_desc,MAXLENGTH_OBJ_DESC,true);
1100 
1101  $q = "UPDATE object_data ".
1102  "SET ".
1103  "description = ".$ilDB->quote($desc, "text").",".
1104  "last_update = ".$ilDB->now()." ".
1105  "WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
1106 
1107  $ilDB->manipulate($q);
1108 
1109  if($objDefinition->isRBACObject($this->getType()))
1110  {
1111  // Update long description
1112  $res = $ilDB->query("SELECT * FROM object_description WHERE obj_id = ".
1113  $ilDB->quote($a_obj_id,'integer'));
1114 
1115  if($res->numRows())
1116  {
1117  $values = array(
1118  'description' => array('clob',$this->getLongDescription())
1119  );
1120  $ilDB->update('object_description',$values,array('obj_id' => array('integer',$this->getId())));
1121  }
1122  else
1123  {
1124  $values = array(
1125  'description' => array('clob',$this->getLongDescription()),
1126  'obj_id' => array('integer',$this->getId()));
1127  $ilDB->insert('object_description',$values);
1128  }
1129  }
1130  }
1131 
1139  function _writeImportId($a_obj_id, $a_import_id)
1140  {
1141  global $ilDB;
1142 
1143  $q = "UPDATE object_data ".
1144  "SET ".
1145  "import_id = ".$ilDB->quote($a_import_id, "text").",".
1146  "last_update = ".$ilDB->now()." ".
1147  "WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
1148 
1149  $ilDB->manipulate($q);
1150  }
1151 
1157  public static function _lookupType($a_id,$a_reference = false)
1158  {
1159  global $ilObjDataCache;
1160 
1161  if($a_reference)
1162  {
1163  return $ilObjDataCache->lookupType($ilObjDataCache->lookupObjId($a_id));
1164  }
1165  return $ilObjDataCache->lookupType($a_id);
1166 
1167  global $ilDB;
1168 
1169  if ($a_reference === true)
1170  {
1171  $q = "SELECT type FROM object_reference obr, object_data obd ".
1172  "WHERE obr.ref_id = ".$ilDB->quote($a_id, "integer")." ".
1173  "AND obr.obj_id = obd.obj_id ";
1174  }
1175  else
1176  {
1177  $q = "SELECT type FROM object_data WHERE obj_id = ".$ilDB->quote($a_id, "integer");
1178  }
1179 
1180  $obj_set = $ilDB->query($q);
1181  $obj_rec = $ilDB->fetchAssoc($obj_set);
1182 
1183  return $obj_rec["type"];
1184  }
1185 
1189  function _isInTrash($a_ref_id)
1190  {
1191  global $tree;
1192 
1193  return $tree->isSaved($a_ref_id);
1194  }
1195 
1199  function _hasUntrashedReference($a_obj_id)
1200  {
1201  $ref_ids = ilObject::_getAllReferences($a_obj_id);
1202  foreach($ref_ids as $ref_id)
1203  {
1204  if(!ilObject::_isInTrash($ref_id))
1205  {
1206  return true;
1207  }
1208  }
1209 
1210  return false;
1211  }
1212 
1218  function _lookupObjectId($a_ref_id)
1219  {
1220  global $ilObjDataCache;
1221 
1222  return (int) $ilObjDataCache->lookupObjId($a_ref_id);
1223  }
1224 
1235  function _getObjectsDataForType($a_type, $a_omit_trash = false)
1236  {
1237  global $ilDB;
1238 
1239  $q = "SELECT * FROM object_data WHERE type = ".$ilDB->quote($a_type, "text");
1240  $obj_set = $ilDB->query($q);
1241 
1242  $objects = array();
1243  while ($obj_rec = $ilDB->fetchAssoc($obj_set))
1244  {
1245  if ((!$a_omit_trash) || ilObject::_hasUntrashedReference($obj_rec["obj_id"]))
1246  {
1247  $objects[$obj_rec["title"].".".$obj_rec["obj_id"]] = array("id" => $obj_rec["obj_id"],
1248  "type" => $obj_rec["type"], "title" => $obj_rec["title"],
1249  "description" => $obj_rec["description"]);
1250  }
1251  }
1252  ksort($objects);
1253  return $objects;
1254  }
1255 
1261  function putInTree($a_parent_ref)
1262  {
1263  global $tree, $log;
1264 
1265  $tree->insertNode($this->getRefId(), $a_parent_ref);
1266 
1267  // write log entry
1268  $log->write("ilObject::putInTree(), parent_ref: $a_parent_ref, ref_id: ".
1269  $this->getRefId().", obj_id: ".$this->getId().", type: ".
1270  $this->getType().", title: ".$this->getTitle());
1271 
1272  }
1273 
1280  function setPermissions($a_parent_ref)
1281  {
1282  global $rbacadmin, $rbacreview;
1283 
1284  $parentRoles = $rbacreview->getParentRoleIds($a_parent_ref);
1285 
1286  foreach ($parentRoles as $parRol)
1287  {
1288  $ops = $rbacreview->getOperationsOfRole($parRol["obj_id"], $this->getType(), $parRol["parent"]);
1289  $rbacadmin->grantPermission($parRol["obj_id"], $ops, $this->getRefId());
1290  }
1291  }
1292 
1299  function createReference()
1300  {
1301  global $ilDB;
1302 
1303  if (!isset($this->id))
1304  {
1305  $message = "ilObject::createNewReference(): No obj_id given!";
1306  $this->raiseError($message,$this->ilias->error_obj->WARNING);
1307  }
1308 
1309  $next_id = $ilDB->nextId('object_reference');
1310  $query = "INSERT INTO object_reference ".
1311  "(ref_id, obj_id) VALUES (".$ilDB->quote($next_id,'integer').','.$ilDB->quote($this->id ,'integer').")";
1312  $this->ilias->db->query($query);
1313 
1314  $this->ref_id = $next_id;
1315  $this->referenced = true;
1316 
1317  return $this->ref_id;
1318  }
1319 
1320 
1327  function countReferences()
1328  {
1329  global $ilDB;
1330 
1331  if (!isset($this->id))
1332  {
1333  $message = "ilObject::countReferences(): No obj_id given!";
1334  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
1335  }
1336 
1337  $query = "SELECT COUNT(ref_id) num FROM object_reference ".
1338  "WHERE obj_id = ".$ilDB->quote($this->id,'integer')." ";
1339  $res = $ilDB->query($query);
1340  $row = $ilDB->fetchObject($res);
1341 
1342  return $row->num;
1343  }
1344 
1345 
1346 
1347 
1357  function delete()
1358  {
1359  global $rbacadmin, $log, $ilDB;
1360 
1361  $remove = false;
1362 
1363  // delete object_data entry
1364  if ((!$this->referenced) || ($this->countReferences() == 1))
1365  {
1366  // check type match
1367  $db_type = ilObject::_lookupType($this->getId());
1368  if ($this->type != $db_type)
1369  {
1370  $message = "ilObject::delete(): Type mismatch. Object with obj_id: ".$this->id." ".
1371  "was instantiated by type '".$this->type."'. DB type is: ".$db_type;
1372 
1373  // write log entry
1374  $log->write($message);
1375 
1376  // raise error
1377  $this->ilias->raiseError("ilObject::delete(): Type mismatch. (".$this->type."/".$this->id.")",$this->ilias->error_obj->WARNING);
1378  }
1379 
1380  // delete entry in object_data
1381  $q = "DELETE FROM object_data ".
1382  "WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
1383  $ilDB->manipulate($q);
1384 
1385  // delete long description
1386  $query = "DELETE FROM object_description WHERE obj_id = ".
1387  $ilDB->quote($this->getId(), "integer");
1388  $ilDB->manipulate($query);
1389 
1390  // write log entry
1391  $log->write("ilObject::delete(), deleted object, obj_id: ".$this->getId().", type: ".
1392  $this->getType().", title: ".$this->getTitle());
1393 
1394  // remove news
1395  include_once("./Services/News/classes/class.ilNewsItem.php");
1396  $news_item = new ilNewsItem();
1397  $news_item->deleteNewsOfContext($this->getId(), $this->getType());
1398  include_once("./Services/Block/classes/class.ilBlockSetting.php");
1400 
1401  $remove = true;
1402  }
1403  else
1404  {
1405  // write log entry
1406  $log->write("ilObject::delete(), object not deleted, number of references: ".
1407  $this->countReferences().", obj_id: ".$this->getId().", type: ".
1408  $this->getType().", title: ".$this->getTitle());
1409  }
1410 
1411  // delete object_reference entry
1412  if ($this->referenced)
1413  {
1414  // delete entry in object_reference
1415  $query = "DELETE FROM object_reference ".
1416  "WHERE ref_id = ".$ilDB->quote($this->getRefId(),'integer');
1417  $res = $ilDB->manipulate($query);
1418 
1419  // write log entry
1420  $log->write("ilObject::delete(), reference deleted, ref_id: ".$this->getRefId().
1421  ", obj_id: ".$this->getId().", type: ".
1422  $this->getType().", title: ".$this->getTitle());
1423 
1424  // DELETE PERMISSION ENTRIES IN RBAC_PA
1425  // DONE: method overwritten in ilObjRole & ilObjUser.
1426  // this call only applies for objects in rbac (not usr,role,rolt)
1427  // TODO: Do this for role templates too
1428  $rbacadmin->revokePermission($this->getRefId(),0,false);
1429 
1430  // Remove desktop items
1432  }
1433 
1434  // remove conditions
1435  if ($this->referenced)
1436  {
1437  $ch =& new ilConditionHandler();
1438  $ch->delete($this->getRefId());
1439  unset($ch);
1440  }
1441 
1442  // BEGIN WebDAV: Delete WebDAV properties
1443  $query = "DELETE FROM dav_property ".
1444  "WHERE obj_id = ".$ilDB->quote($this->getId(),'integer');
1445  $res = $ilDB->manipulate($query);
1446  // END WebDAV: Delete WebDAV properties
1447 
1448  include_once './Services/Tracking/classes/class.ilChangeEvent.php';
1449  ilChangeEvent::_delete($this->getId());
1450 
1451  return $remove;
1452  }
1453 
1461  function initDefaultRoles()
1462  {
1463  return array();
1464  }
1465 
1475  function createRoleFolder()
1476  {
1477  global $rbacreview;
1478 
1479  // does a role folder already exists?
1480  // (this check is only 'to be sure' that no second role folder is created under one object.
1481  // the if-construct should never return true)
1482  if ($rolf_data = $rbacreview->getRoleFolderofObject($this->getRefId()))
1483  {
1484  $rfoldObj = $this->ilias->obj_factory->getInstanceByRefId($rolf_data["ref_id"]);
1485  }
1486  else
1487  {
1488  include_once ("./Services/AccessControl/classes/class.ilObjRoleFolder.php");
1489  $rfoldObj = new ilObjRoleFolder();
1490  $rfoldObj->setTitle($this->getId());
1491  $rfoldObj->setDescription(" (ref_id ".$this->getRefId().")");
1492  $rfoldObj->create();
1493  $rfoldObj->createReference();
1494  $rfoldObj->putInTree($this->getRefId());
1495  $rfoldObj->setPermissions($this->getRefId());
1496  }
1497 
1498  return $rfoldObj;
1499  }
1500 
1509  function _exists($a_id, $a_reference = false)
1510  {
1511  global $ilias, $ilDB;
1512 
1513  if ($a_reference)
1514  {
1515  $q = "SELECT * FROM object_data ".
1516  "LEFT JOIN object_reference ON object_reference.obj_id=object_data.obj_id ".
1517  "WHERE object_reference.ref_id= ".$ilDB->quote($a_id, "integer");
1518  }
1519  else
1520  {
1521  $q = "SELECT * FROM object_data WHERE obj_id=".$ilDB->quote($a_id, "integer");
1522  }
1523 
1524  $r = $ilDB->query($q);
1525 
1526  return $ilDB->numRows($r) ? true : false;
1527  }
1528 
1541  function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
1542  {
1543  global $tree;
1544 
1545  $parent_id = (int) $tree->getParentId($a_node_id);
1546 
1547  if ($parent_id != 0)
1548  {
1549  $obj_data =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
1550  $obj_data->notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$parent_id,$a_params);
1551  }
1552 
1553  return true;
1554  }
1555 
1556  // toggle subscription interface
1557  function setRegisterMode($a_bool)
1558  {
1559  $this->register = (bool) $a_bool;
1560  }
1561 
1562  // check register status of current user
1563  // abstract method; overwrite in object type class
1564  function isUserRegistered($a_user_id = 0)
1565  {
1566  return false;
1567  }
1568 
1570  {
1571  return $this->register;
1572  }
1573 
1574 
1575  function getXMLZip()
1576  {
1577  return false;
1578  }
1579  function getHTMLDirectory()
1580  {
1581  return false;
1582  }
1583 
1587  static function _getObjectsByType($a_obj_type = "", $a_owner = "")
1588  {
1589  global $ilDB;
1590 
1591  $order = " ORDER BY title";
1592 
1593  // where clause
1594  if ($a_obj_type)
1595  {
1596  $where_clause = "WHERE type = ".
1597  $ilDB->quote($a_obj_type, "text");
1598 
1599  if ($a_owner != "")
1600  {
1601  $where_clause.= " AND owner = ".$ilDB->quote($a_owner, "integer");
1602  }
1603  }
1604 
1605  $q = "SELECT * FROM object_data ".$where_clause.$order;
1606  $r = $ilDB->query($q);
1607 
1608  $arr = array();
1609  if ($ilDB->numRows($r) > 0)
1610  {
1611  while ($row = $ilDB->fetchAssoc($r))
1612  {
1613  $row["desc"] = $row["description"];
1614  $arr[$row["obj_id"]] = $row;
1615  }
1616  }
1617 
1618  return $arr;
1619  }
1620 
1629  public static function _prepareCloneSelection($a_ref_ids,$new_type)
1630  {
1631  global $ilDB,$lng,$objDefinition;
1632 
1633  $query = "SELECT obj_data.title obj_title,path_data.title path_title,child FROM tree ".
1634  "JOIN object_reference obj_ref ON child = obj_ref.ref_id ".
1635  "JOIN object_data obj_data ON obj_ref.obj_id = obj_data.obj_id ".
1636  "JOIN object_reference path_ref ON parent = path_ref.ref_id ".
1637  "JOIN object_data path_data ON path_ref.obj_id = path_data.obj_id ".
1638  "WHERE ".$ilDB->in("child", $a_ref_ids, false, "integer")." ".
1639  "ORDER BY obj_data.title ";
1640  $res = $ilDB->query($query);
1641 
1642  if (!$objDefinition->isPlugin($new_type))
1643  {
1644  $options[0] = $lng->txt('obj_'.$new_type.'_select');
1645  }
1646  else
1647  {
1648  include_once("./Services/Component/classes/class.ilPlugin.php");
1649  $options[0] = ilPlugin::lookupTxt("rep_robj", $new_type, "obj_".$new_type."_select");
1650  }
1651 
1652  while($row = $ilDB->fetchObject($res))
1653  {
1654  if(strlen($title = $row->obj_title) > 40)
1655  {
1656  $title = substr($title,0,40).'...';
1657  }
1658  if(strlen($path = $row->path_title) > 40)
1659  {
1660  $path = substr($path,0,40).'...';
1661  }
1662 
1663 
1664  $options[$row->child] = ($title.' ('.$lng->txt('path').': '.$path.')');
1665  }
1666  return $options ? $options : array();
1667  }
1668 
1678  public function cloneObject($a_target_id,$a_copy_id = 0)
1679  {
1680  global $objDefinition,$ilUser,$rbacadmin, $ilDB;
1681 
1682  $location = $objDefinition->getLocation($this->getType());
1683  $class_name = ('ilObj'.$objDefinition->getClassName($this->getType()));
1684 
1685  $title = $this->appendCopyInfo($a_target_id,$a_copy_id);
1686 
1687  // create instance
1688  include_once($location."/class.".$class_name.".php");
1689  $new_obj = new $class_name(0, false);
1690  $new_obj->setOwner($ilUser->getId());
1691  $new_obj->setTitle($title);
1692  $new_obj->setDescription($this->getDescription());
1693  $new_obj->setType($this->getType());
1694  // Choose upload mode to avoid creation of additional settings, db entries ...
1695  $new_obj->create(true);
1696  $new_obj->createReference();
1697  $new_obj->putInTree($a_target_id);
1698  $new_obj->setPermissions($a_target_id);
1699  $new_obj->initDefaultRoles();
1700 
1701  // copy local roles
1702  $rbacadmin->copyLocalRoles($this->getRefId(),$new_obj->getRefId());
1703 
1704  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
1705  ilAdvancedMDValues::_cloneValues($this->getId(),$new_obj->getId());
1706 
1707  // BEGIN WebDAV: Clone WebDAV properties
1708  $query = "INSERT INTO dav_property (obj_id,node_id,ns,name,value) ".
1709  "SELECT ".$ilDB->quote($new_obj->getId(),'integer').",node_id,ns,name,value ".
1710  "FROM dav_property ".
1711  "WHERE obj_id = ".$ilDB->quote($this->getId(),'integer');
1712  $res = $ilDB->manipulate($query);
1713  // END WebDAV: Clone WebDAV properties
1714 
1715  return $new_obj;
1716  }
1717 
1725  public function appendCopyInfo($a_target_id,$a_copy_id)
1726  {
1727  global $tree;
1728 
1729  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
1730  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
1731  if(!$cp_options->isRootNode($this->getRefId()))
1732  {
1733  return $this->getTitle();
1734  }
1735  $nodes = $tree->getChilds($a_target_id);
1736 
1737  $title_unique = false;
1738  require_once 'Modules/File/classes/class.ilObjFileAccess.php';
1739  $numberOfCopy = 1;
1741  while(!$title_unique)
1742  {
1743  $found = 0;
1744  foreach($nodes as $node)
1745  {
1746  if(($title == $node['title']) and ($this->getType() == $node['type']))
1747  {
1748  $found++;
1749  }
1750  }
1751  if($found > 0)
1752  {
1753  $title = ilObjFileAccess::_appendNumberOfCopyToFilename($this->getTitle(), ++$numberOfCopy);
1754  }
1755  else
1756  {
1757  break;
1758  }
1759  }
1760  return $title;
1761  }
1762 
1775  public function cloneDependencies($a_target_id,$a_copy_id)
1776  {
1777  return true;
1778 
1779  //include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
1780  //$cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
1781  //$mappings = $cwo->getMappings();
1782 
1783  }
1784 
1792  public function cloneMetaData($target_obj)
1793  {
1794  include_once "./Services/MetaData/classes/class.ilMD.php";
1795  $md = new ilMD($this->getId(),0,$this->getType());
1796  $md->cloneMD($target_obj->getId(),0,$target_obj->getType());
1797  return true;
1798  }
1799 
1808  public static function _getIcon($a_obj_id = "", $a_size = "big", $a_type = "",
1809  $a_offline = false)
1810  {
1811  global $ilSetting, $objDefinition;
1812 
1813  if ($a_obj_id == "" && $a_type == "")
1814  {
1815  return "";
1816  }
1817 
1818  if ($a_type == "")
1819  {
1820  $a_type = ilObject::_lookupType($a_obj_id);
1821  }
1822 
1823  if ($a_size == "")
1824  {
1825  $a_size = "big";
1826  }
1827 
1828  if ($ilSetting->get("custom_icons") &&
1829  in_array($a_type, array("cat","grp","crs", "root")))
1830  {
1831  require_once("./Services/Container/classes/class.ilContainer.php");
1832  if (ilContainer::_lookupContainerSetting($a_obj_id, "icon_".$a_size))
1833  {
1834  $cont_dir = ilContainer::_getContainerDirectory($a_obj_id);
1835  $file_name = $cont_dir."/icon_".$a_size.".gif";
1836 
1837  if (is_file($file_name))
1838  {
1839  return $file_name;
1840  }
1841  }
1842  }
1843 
1844  switch($a_size)
1845  {
1846  case "small": $suff = ""; break;
1847  case "tiny": $suff = "_s"; break;
1848  default: $suff = "_b"; break;
1849  }
1850  if (!$a_offline)
1851  {
1852  if ($objDefinition->isRBACObject($a_type))
1853  {
1854  if (!$objDefinition->isPlugin($a_type))
1855  {
1856  return ilUtil::getImagePath("icon_".$a_type.$suff.".gif");
1857  }
1858  else
1859  {
1860  include_once("./Services/Repository/classes/class.ilRepositoryObjectPlugin.php");
1861  return ilRepositoryObjectPlugin::_getIcon($a_type, $a_size);
1862  }
1863  }
1864  return ilUtil::getImagePath("icon_".$a_type.$suff.".gif");
1865  }
1866  else
1867  {
1868  return "./images/icon_".$a_type.$suff.".gif";
1869  }
1870  }
1871 
1872 } // END class.ilObject
1873 ?>