ILIAS  Release_4_1_x_branch Revision 61804
 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 {
18  const TITLE_LENGTH = 128;
19 
20 
26  var $ilias;
27 
33  var $lng;
34 
40  var $id; // true object_id!!!!
41  var $ref_id;// reference_id
42  var $type;
43  var $title;
44  // BEGIN WebDAV: WebDAV needs to access the untranslated title of an object
46  // END WebDAV: WebDAV needs to access the untranslated title of an object
47  var $desc;
49  var $owner;
53  var $register = false; // registering required for object? set to true to implement a subscription interface
54 
61 
68 
74 
79  var $max_desc;
80 
85  var $add_dots;
86 
91 
98  function ilObject($a_id = 0, $a_reference = true)
99  {
100  global $ilias, $lng, $ilBench;
101 
102  $ilBench->start("Core", "ilObject_Constructor");
103 
104  if (DEBUG)
105  {
106  echo "<br/><font color=\"red\">type(".$this->type.") id(".$a_id.") referenced(".$a_reference.")</font>";
107  }
108 
109  $this->ilias =& $ilias;
110  $this->lng =& $lng;
111 
112  $this->max_title = MAXLENGTH_OBJ_TITLE;
113  $this->max_desc = MAXLENGTH_OBJ_DESC;
114  $this->add_dots = true;
115 
116  $this->referenced = $a_reference;
117  $this->call_by_reference = $a_reference;
118 
119  if ($a_id == 0)
120  {
121  $this->referenced = false; // newly created objects are never referenced
122  } // they will get referenced if createReference() is called
123 
124  if ($this->referenced)
125  {
126  $this->ref_id = $a_id;
127  }
128  else
129  {
130  $this->id = $a_id;
131  }
132  // read object data
133  if ($a_id != 0)
134  {
135  $this->read();
136  }
137 
138  $ilBench->stop("Core", "ilObject_Constructor");
139  }
140 
144  function withReferences()
145  {
146  // both vars could differ. this method should always return true if one of them is true without changing their status
147  return ($this->call_by_reference) ? true : $this->referenced;
148  }
149 
150 
156  function read($a_force_db = false)
157  {
158  global $objDefinition, $ilBench, $ilDB, $log;
159 
160  $ilBench->start("Core", "ilObject_read");
161 
162  if (isset($this->obj_data_record) && !$a_force_db)
163  {
164  $obj = $this->obj_data_record;
165  }
166  else if ($this->referenced)
167  {
168  // check reference id
169  if (!isset($this->ref_id))
170  {
171  $message = "ilObject::read(): No ref_id given! (".$this->type.")";
172  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
173  }
174 
175  // read object data
176  $ilBench->start("Core", "ilObject_read_readData");
177 
178  $q = "SELECT * FROM object_data, object_reference WHERE object_data.obj_id=object_reference.obj_id ".
179  "AND object_reference.ref_id= ".$ilDB->quote($this->ref_id, "integer");
180  $object_set = $ilDB->query($q);
181  $ilBench->stop("Core", "ilObject_read_readData");
182 
183  // check number of records
184  if ($ilDB->numRows($object_set) == 0)
185  {
186  $message = "ilObject::read(): Object with ref_id ".$this->ref_id." not found! (".$this->type.")";
187  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
188  }
189 
190  $obj = $ilDB->fetchAssoc($object_set);
191  }
192  else
193  {
194  // check object id
195  if (!isset($this->id))
196  {
197  $message = "ilObject::read(): No obj_id given! (".$this->type.")";
198  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
199  }
200 
201  // read object data
202  $q = "SELECT * FROM object_data ".
203  "WHERE obj_id = ".$ilDB->quote($this->id, "integer");
204  $object_set = $ilDB->query($q);
205 
206  // check number of records
207  if ($ilDB->numRows($object_set) == 0)
208  {
209  $message = "ilObject::read(): Object with obj_id: ".$this->id." (".$this->type.") not found!";
210  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
211  }
212 
213  $obj = $ilDB->fetchAssoc($object_set);
214  }
215 
216  $this->id = $obj["obj_id"];
217 
218  // check type match (the "xxx" type is used for the unit test)
219  if ($this->type != $obj["type"] && $obj["type"] != "xxx")
220  {
221  $message = "ilObject::read(): Type mismatch. Object with obj_id: ".$this->id." ".
222  "was instantiated by type '".$this->type."'. DB type is: ".$obj["type"];
223 
224  // write log entry
225  $log->write($message);
226 
227  // raise error
228  $this->ilias->raiseError("ilObject::read(): Type mismatch. (".$this->type."/".$this->id.")",$this->ilias->error_obj->WARNING);
229  }
230 
231  $this->type = $obj["type"];
232  $this->title = $obj["title"];
233  // BEGIN WebDAV: WebDAV needs to access the untranslated title of an object
234  $this->untranslatedTitle = $obj["title"];
235  // END WebDAV: WebDAV needs to access the untranslated title of an object
236  $this->desc = $obj["description"];
237  $this->owner = $obj["owner"];
238  $this->create_date = $obj["create_date"];
239  $this->last_update = $obj["last_update"];
240  $this->import_id = $obj["import_id"];
241 
242  if($objDefinition->isRBACObject($this->getType()))
243  {
244  // Read long description
245  $query = "SELECT * FROM object_description WHERE obj_id = ".$ilDB->quote($this->id,'integer');
246  $res = $this->ilias->db->query($query);
247  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
248  {
249  if(strlen($row->description))
250  {
251  $this->setDescription($row->description);
252  }
253  }
254  }
255 
256  // multilingual support systemobjects (sys) & categories (db)
257  $ilBench->start("Core", "ilObject_Constructor_getTranslation");
258  $translation_type = $objDefinition->getTranslationType($this->type);
259 
260  if ($translation_type == "sys")
261  {
262  $this->title = $this->lng->txt("obj_".$this->type);
263  $this->desc = $this->lng->txt("obj_".$this->type."_desc");
264  }
265  elseif ($translation_type == "db")
266  {
267  $q = "SELECT title,description FROM object_translation ".
268  "WHERE obj_id = ".$ilDB->quote($this->id,'integer')." ".
269  "AND lang_code = ".$ilDB->quote($this->ilias->account->getCurrentLanguage(),'text')." ".
270  "AND NOT lang_default = 1";
271  $r = $this->ilias->db->query($q);
272  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
273 
274  if ($row)
275  {
276  $this->title = $row->title;
277  $this->setDescription($row->description);
278  #$this->desc = $row->description;
279  }
280  }
281 
282  $ilBench->stop("Core", "ilObject_Constructor_getTranslation");
283 
284  $ilBench->stop("Core", "ilObject_read");
285  }
286 
292  function getId()
293  {
294  return $this->id;
295  }
296 
302  function setId($a_id)
303  {
304  $this->id = $a_id;
305  }
306 
312  function setRefId($a_id)
313  {
314  $this->ref_id = $a_id;
315  $this->referenced = true;
316  }
317 
323  function getRefId()
324  {
325  return $this->ref_id;
326  }
327 
333  function getType()
334  {
335  return $this->type;
336  }
337 
343  function setType($a_type)
344  {
345  $this->type = $a_type;
346  }
347 
357  public function getPresentationTitle()
358  {
359  return $this->getTitle();
360  }
361 
362 
368  function getTitle()
369  {
370  return $this->title;
371  }
372  // BEGIN WebDAV: WebDAV needs to access the untranslated title of an object
379  {
381  }
382  // END WebDAV: WebDAV needs to access the untranslated title of an object
383 
390  function setTitle($a_title)
391  {
392  $this->title = ilUtil::shortenText($a_title, $this->max_title, $this->add_dots);
393  // BEGIN WebDAV: WebDAV needs to access the untranslated title of an object
394  $this->untranslatedTitle = $this->title;
395  // END WebDAV: WebDAV needs to access the untranslated title of an object
396  }
397 
404  function getDescription()
405  {
406  return $this->desc;
407  }
408 
415  function setDescription($a_desc)
416  {
417  // Shortened form is storted in object_data. Long form is stored in object_description
418  $this->desc = ilUtil::shortenText($a_desc, $this->max_desc, $this->add_dots);
419 
420  $this->long_desc = $a_desc;
421 
422  return true;
423  }
424 
432  {
433  return strlen($this->long_desc) ? $this->long_desc : $this->desc;
434  }
435 
442  function getImportId()
443  {
444  return $this->import_id;
445  }
446 
453  function setImportId($a_import_id)
454  {
455  $this->import_id = $a_import_id;
456  }
457 
458  function _lookupObjIdByImportId($a_import_id)
459  {
460  global $ilDB;
461 
462  $query = "SELECT * FROM object_data ".
463  "WHERE import_id = ".$ilDB->quote($a_import_id, "text")." ".
464  "ORDER BY create_date DESC";
465  $res = $ilDB->query($query);
466  while($row = $ilDB->fetchObject($res))
467  {
468  return $row->obj_id;
469  }
470  return 0;
471  }
472 
479  function getOwner()
480  {
481  return $this->owner;
482  }
483 
484  /*
485  * get full name of object owner
486  *
487  * @access public
488  * @return string owner name or unknown
489  */
490  function getOwnerName()
491  {
492  return ilObject::_lookupOwnerName($this->getOwner());
493  }
494 
498  function _lookupOwnerName($a_owner_id)
499  {
500  global $lng;
501 
502  if ($a_owner_id != -1)
503  {
504  if (ilObject::_exists($a_owner_id))
505  {
506  $owner = new ilObjUser($a_owner_id);
507  }
508  }
509 
510  if (is_object($owner))
511  {
512  $own_name = $owner->getFullname();
513  }
514  else
515  {
516  $own_name = $lng->txt("unknown");
517  }
518 
519  return $own_name;
520  }
521 
528  function setOwner($a_owner)
529  {
530  $this->owner = $a_owner;
531  }
532 
533 
534 
540  function getCreateDate()
541  {
542  return $this->create_date;
543  }
544 
550  function getLastUpdateDate()
551  {
552  return $this->last_update;
553  }
554 
555 
567  function getDiskUsage()
568  {
569  return null;
570  }
571 
580  function setObjDataRecord($a_record)
581  {
582  $this->obj_data_record = $a_record;
583  }
584 
593  function create()
594  {
595  global $ilDB, $log,$ilUser,$objDefinition;
596 
597  if (!isset($this->type))
598  {
599  $message = get_class($this)."::create(): No object type given!";
600  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
601  }
602 
603  // write log entry
604  $log->write("ilObject::create(), start");
605 
606  $this->title = ilUtil::shortenText($this->getTitle(), $this->max_title, $this->add_dots);
607  $this->desc = ilUtil::shortenText($this->getDescription(), $this->max_desc, $this->add_dots);
608 
609  // determine owner
610  if ($this->getOwner() > 0)
611  {
612  $owner = $this->getOwner();
613  }
614  elseif(is_object($ilUser))
615  {
616  $owner = $ilUser->getId();
617  }
618  else
619  {
620  $owner = 0;
621  }
622  $this->id = $ilDB->nextId("object_data");
623  $q = "INSERT INTO object_data ".
624  "(obj_id,type,title,description,owner,create_date,last_update,import_id) ".
625  "VALUES ".
626  "(".
627  $ilDB->quote($this->id, "integer").",".
628  $ilDB->quote($this->type, "text").",".
629  $ilDB->quote($this->getTitle(), "text").",".
630  $ilDB->quote($this->getDescription(), "text").",".
631  $ilDB->quote($owner, "integer").",".
632  $ilDB->now().",".
633  $ilDB->now().",".
634  $ilDB->quote($this->getImportId(), "text").")";
635 
636  $ilDB->manipulate($q);
637 
638  //$this->id = $ilDB->getLastInsertId();
639 
640 
641  // Save long form of description if is rbac object
642  if($objDefinition->isRBACObject($this->getType()))
643  {
644  $values = array(
645  'obj_id' => array('integer',$this->id),
646  'description' => array('clob', $this->getLongDescription()));
647 //var_dump($values);
648  $ilDB->insert('object_description',$values);
649  }
650 
651 
652  // the line ($this->read();) messes up meta data handling: meta data,
653  // that is not saved at this time, gets lost, so we query for the dates alone
654  //$this->read();
655  $q = "SELECT last_update, create_date FROM object_data".
656  " WHERE obj_id = ".$ilDB->quote($this->id, "integer");
657  $obj_set = $ilDB->query($q);
658  $obj_rec = $ilDB->fetchAssoc($obj_set);
659  $this->last_update = $obj_rec["last_update"];
660  $this->create_date = $obj_rec["create_date"];
661 
662  // set owner for new objects
663  $this->setOwner($owner);
664 
665  // write log entry
666  $log->write("ilObject::create(), finished, obj_id: ".$this->id.", type: ".
667  $this->type.", title: ".$this->getTitle());
668 
669  $GLOBALS['ilAppEventHandler']->raise(
670  'Services/Object',
671  'create',
672  array('obj_id' => $this->id,'obj_type' => $this->type));
673 
674  return $this->id;
675  }
676 
683  function update()
684  {
685  global $objDefinition, $ilDB;
686 
687  $q = "UPDATE object_data ".
688  "SET ".
689  "title = ".$ilDB->quote($this->getTitle(), "text").",".
690  "description = ".$ilDB->quote($this->getDescription(), "text").", ".
691  "import_id = ".$ilDB->quote($this->getImportId(), "text").",".
692  "last_update = ".$ilDB->now()." ".
693  "WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
694  $ilDB->manipulate($q);
695 
696  // the line ($this->read();) messes up meta data handling: meta data,
697  // that is not saved at this time, gets lost, so we query for the dates alone
698  //$this->read();
699  $q = "SELECT last_update FROM object_data".
700  " WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
701  $obj_set = $ilDB->query($q);
702  $obj_rec = $ilDB->fetchAssoc($obj_set);
703  $this->last_update = $obj_rec["last_update"];
704 
705  if($objDefinition->isRBACObject($this->getType()))
706  {
707  // Update long description
708  $res = $this->ilias->db->query("SELECT * FROM object_description WHERE obj_id = ".
709  $ilDB->quote($this->getId(),'integer'));
710  if($res->numRows())
711  {
712  $values = array(
713  'description' => array('clob',$this->getLongDescription())
714  );
715  $ilDB->update('object_description',$values,array('obj_id' => array('integer',$this->getId())));
716  }
717  else
718  {
719  $values = array(
720  'description' => array('clob',$this->getLongDescription()),
721  'obj_id' => array('integer',$this->getId()));
722  $ilDB->insert('object_description',$values);
723  }
724  }
725  $GLOBALS['ilAppEventHandler']->raise(
726  'Services/Object',
727  'update',
728  array('obj_id' => $this->getId(),
729  'obj_type' => $this->getType(),
730  'ref_id' => $this->getRefId()));
731 
732  return true;
733  }
734 
746  function MDUpdateListener($a_element)
747  {
748  include_once 'Services/MetaData/classes/class.ilMD.php';
749 
750  $GLOBALS['ilAppEventHandler']->raise(
751  'Services/Object',
752  'update',
753  array('obj_id' => $this->getId(),
754  'obj_type' => $this->getType(),
755  'ref_id' => $this->getRefId()));
756 
757  switch($a_element)
758  {
759  case 'General':
760 
761  // Update Title and description
762  $md = new ilMD($this->getId(),0, $this->getType());
763  if(!is_object($md_gen = $md->getGeneral()))
764  {
765  return false;
766  }
767  $this->setTitle($md_gen->getTitle());
768 
769  foreach($md_gen->getDescriptionIds() as $id)
770  {
771  $md_des = $md_gen->getDescription($id);
772  $this->setDescription($md_des->getDescription());
773  break;
774  }
775  $this->update();
776  break;
777 
778  default:
779  }
780 
781  return true;
782  }
783 
787  function createMetaData()
788  {
789  include_once 'Services/MetaData/classes/class.ilMDCreator.php';
790 
791  global $ilUser;
792 
793  $md_creator = new ilMDCreator($this->getId(),0,$this->getType());
794  $md_creator->setTitle($this->getTitle());
795  $md_creator->setTitleLanguage($ilUser->getPref('language'));
796  $md_creator->setDescription($this->getLongDescription());
797  $md_creator->setDescriptionLanguage($ilUser->getPref('language'));
798  $md_creator->setKeywordLanguage($ilUser->getPref('language'));
799  $md_creator->setLanguage($ilUser->getPref('language'));
800  $md_creator->create();
801 
802  return true;
803  }
804 
808  function updateMetaData()
809  {
810  include_once("Services/MetaData/classes/class.ilMD.php");
811  include_once("Services/MetaData/classes/class.ilMDGeneral.php");
812  include_once("Services/MetaData/classes/class.ilMDDescription.php");
813 
814  $md =& new ilMD($this->getId(), 0, $this->getType());
815  $md_gen =& $md->getGeneral();
816  // BEGIN WebDAV: meta data can be missing sometimes.
817  if ($md_gen == null)
818  {
819  $this->createMetaData();
820  $md =& new ilMD($this->getId(), 0, $this->getType());
821  $md_gen =& $md->getGeneral();
822  }
823  // END WebDAV: meta data can be missing sometimes.
824  $md_gen->setTitle($this->getTitle());
825 
826  // sets first description (maybe not appropriate)
827  $md_des_ids =& $md_gen->getDescriptionIds();
828  if (count($md_des_ids) > 0)
829  {
830  $md_des =& $md_gen->getDescription($md_des_ids[0]);
831  $md_des->setDescription($this->getLongDescription());
832  $md_des->update();
833  }
834  $md_gen->update();
835 
836  }
837 
841  function deleteMetaData()
842  {
843  // Delete meta data
844  include_once('Services/MetaData/classes/class.ilMD.php');
845  $md = new ilMD($this->getId(), 0, $this->getType());
846  $md->deleteAll();
847  }
848 
855  function updateOwner()
856  {
857  global $ilDB;
858 
859  $q = "UPDATE object_data ".
860  "SET ".
861  "owner = ".$ilDB->quote($this->getOwner(), "integer").", ".
862  "last_update = ".$ilDB->now()." ".
863  "WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
864  $ilDB->manipulate($q);
865 
866  $q = "SELECT last_update FROM object_data".
867  " WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
868  $obj_set = $ilDB->query($q);
869  $obj_rec = $ilDB->fetchAssoc($obj_set);
870  $this->last_update = $obj_rec["last_update"];
871 
872  return true;
873  }
874 
882  function _getIdForImportId($a_import_id)
883  {
884  global $ilDB;
885 
886  $ilDB->setLimit(1,0);
887  $q = "SELECT * FROM object_data WHERE import_id = ".$ilDB->quote($a_import_id, "text").
888  " ORDER BY create_date DESC";
889  $obj_set = $ilDB->query($q);
890 
891  if ($obj_rec = $ilDB->fetchAssoc($obj_set))
892  {
893  return $obj_rec["obj_id"];
894  }
895  else
896  {
897  return 0;
898  }
899  }
900 
906  public static function _getAllReferences($a_id)
907  {
908  global $ilDB;
909 
910  $query = "SELECT * FROM object_reference WHERE obj_id = ".
911  $ilDB->quote($a_id,'integer');
912 
913  $res = $ilDB->query($query);
914  $ref = array();
915  while($obj_rec = $ilDB->fetchAssoc($res))
916  {
917  $ref[$obj_rec["ref_id"]] = $obj_rec["ref_id"];
918  }
919 
920  return $ref;
921  }
922 
928  public static function _lookupTitle($a_id)
929  {
930  global $ilObjDataCache;
931 
932  $tit = $ilObjDataCache->lookupTitle($a_id);
933 //echo "<br>LOOKING-$a_id-:$tit";
934  return $tit;
935  }
936 
942  function _lookupOwner($a_id)
943  {
944  global $ilObjDataCache;
945 
946  $owner = $ilObjDataCache->lookupOwner($a_id);
947  return $owner;
948  }
949 
950  public static function _getIdsForTitle($title, $type = '', $partialmatch = false)
951  {
952  global $ilDB;
953 
954  $query = (!$partialmatch)
955  ? "SELECT obj_id FROM object_data WHERE title = ".$ilDB->quote($title, "text")
956  : "SELECT obj_id FROM object_data WHERE ".$ilDB->like("title", "text", '%'.$title.'%');
957  if($type != '')
958  {
959  $query .= " AND type = ".$ilDB->quote($type, "text");
960  }
961 
962  $result = $ilDB->query($query);
963 
964  $object_ids = array();
965  while($row = $ilDB->fetchAssoc($result))
966  {
967  $object_ids[] = $row['obj_id'];
968  }
969 
970  return is_array($object_ids) ? $object_ids : array();
971  }
972 
978  function _lookupDescription($a_id)
979  {
980  global $ilObjDataCache;
981 
982  return $ilObjDataCache->lookupDescription($a_id);
983  }
984 
990  function _lookupLastUpdate($a_id, $a_as_string = false)
991  {
992  global $ilObjDataCache;
993 
994  if ($a_as_string)
995  {
996  return ilDatePresentation::formatDate(new ilDateTime($ilObjDataCache->lookupLastUpdate($a_id),IL_CAL_DATETIME));
997  }
998  else
999  {
1000  return $ilObjDataCache->lookupLastUpdate($a_id);
1001  }
1002  }
1003 
1009  function _getLastUpdateOfObjects($a_objs)
1010  {
1011  global $ilDB;
1012 
1013  if (!is_array($a_objs))
1014  {
1015  $a_objs = array($a_objs);
1016  }
1017  $types = array();
1018  $set = $ilDB->query("SELECT max(last_update) as last_update FROM object_data ".
1019  "WHERE ".$ilDB->in("obj_id", $a_objs, false, "integer")." ");
1020  $rec = $ilDB->fetchAssoc($set);
1021 
1022  return ($rec["last_update"]);
1023  }
1024 
1025  public static function _lookupObjId($a_id)
1026  {
1027  global $ilObjDataCache;
1028 
1029  return (int) $ilObjDataCache->lookupObjId($a_id);
1030  }
1031 
1035  function _setDeletedDate($a_ref_id)
1036  {
1037  global $ilDB;
1038 
1039  $query = "UPDATE object_reference SET deleted= ".$ilDB->now().' '.
1040  "WHERE ref_id = ".$ilDB->quote($a_ref_id,'integer');
1041  $res = $ilDB->manipulate($query);
1042  }
1043 
1047  function _resetDeletedDate($a_ref_id)
1048  {
1049  global $ilDB;
1050 
1051  $query = "UPDATE object_reference SET deleted = ".$ilDB->quote(null,'timestamp').
1052  " WHERE ref_id = ".$ilDB->quote($a_ref_id,'integer');
1053  $res = $ilDB->manipulate($query);
1054  }
1055 
1059  function _lookupDeletedDate($a_ref_id)
1060  {
1061  global $ilDB;
1062 
1063  $query = "SELECT deleted FROM object_reference".
1064  " WHERE ref_id = ".$ilDB->quote($a_ref_id, "integer");
1065  $set = $ilDB->query($query);
1066  $rec = $ilDB->fetchAssoc($set);
1067 
1068  return $rec["deleted"];
1069  }
1070 
1071 
1079  function _writeTitle($a_obj_id, $a_title)
1080  {
1081  global $ilDB;
1082 
1083  $q = "UPDATE object_data ".
1084  "SET ".
1085  "title = ".$ilDB->quote($a_title, "text").",".
1086  "last_update = ".$ilDB->now()." ".
1087  "WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
1088 
1089  $ilDB->manipulate($q);
1090  }
1091 
1099  function _writeDescription($a_obj_id, $a_desc)
1100  {
1101  global $ilDB,$objDefinition;
1102 
1103 
1104  $desc = ilUtil::shortenText($a_desc,MAXLENGTH_OBJ_DESC,true);
1105 
1106  $q = "UPDATE object_data ".
1107  "SET ".
1108  "description = ".$ilDB->quote($desc, "text").",".
1109  "last_update = ".$ilDB->now()." ".
1110  "WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
1111 
1112  $ilDB->manipulate($q);
1113 
1114  if($objDefinition->isRBACObject($this->getType()))
1115  {
1116  // Update long description
1117  $res = $ilDB->query("SELECT * FROM object_description WHERE obj_id = ".
1118  $ilDB->quote($a_obj_id,'integer'));
1119 
1120  if($res->numRows())
1121  {
1122  $values = array(
1123  'description' => array('clob',$this->getLongDescription())
1124  );
1125  $ilDB->update('object_description',$values,array('obj_id' => array('integer',$this->getId())));
1126  }
1127  else
1128  {
1129  $values = array(
1130  'description' => array('clob',$this->getLongDescription()),
1131  'obj_id' => array('integer',$this->getId()));
1132  $ilDB->insert('object_description',$values);
1133  }
1134  }
1135  }
1136 
1144  function _writeImportId($a_obj_id, $a_import_id)
1145  {
1146  global $ilDB;
1147 
1148  $q = "UPDATE object_data ".
1149  "SET ".
1150  "import_id = ".$ilDB->quote($a_import_id, "text").",".
1151  "last_update = ".$ilDB->now()." ".
1152  "WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer");
1153 
1154  $ilDB->manipulate($q);
1155  }
1156 
1162  public static function _lookupType($a_id,$a_reference = false)
1163  {
1164  global $ilObjDataCache;
1165 
1166  if($a_reference)
1167  {
1168  return $ilObjDataCache->lookupType($ilObjDataCache->lookupObjId($a_id));
1169  }
1170  return $ilObjDataCache->lookupType($a_id);
1171 
1172  global $ilDB;
1173 
1174  if ($a_reference === true)
1175  {
1176  $q = "SELECT type FROM object_reference obr, object_data obd ".
1177  "WHERE obr.ref_id = ".$ilDB->quote($a_id, "integer")." ".
1178  "AND obr.obj_id = obd.obj_id ";
1179  }
1180  else
1181  {
1182  $q = "SELECT type FROM object_data WHERE obj_id = ".$ilDB->quote($a_id, "integer");
1183  }
1184 
1185  $obj_set = $ilDB->query($q);
1186  $obj_rec = $ilDB->fetchAssoc($obj_set);
1187 
1188  return $obj_rec["type"];
1189  }
1190 
1194  function _isInTrash($a_ref_id)
1195  {
1196  global $tree;
1197 
1198  return $tree->isSaved($a_ref_id);
1199  }
1200 
1204  function _hasUntrashedReference($a_obj_id)
1205  {
1206  $ref_ids = ilObject::_getAllReferences($a_obj_id);
1207  foreach($ref_ids as $ref_id)
1208  {
1209  if(!ilObject::_isInTrash($ref_id))
1210  {
1211  return true;
1212  }
1213  }
1214 
1215  return false;
1216  }
1217 
1223  function _lookupObjectId($a_ref_id)
1224  {
1225  global $ilObjDataCache;
1226 
1227  return (int) $ilObjDataCache->lookupObjId($a_ref_id);
1228  }
1229 
1240  function _getObjectsDataForType($a_type, $a_omit_trash = false)
1241  {
1242  global $ilDB;
1243 
1244  $q = "SELECT * FROM object_data WHERE type = ".$ilDB->quote($a_type, "text");
1245  $obj_set = $ilDB->query($q);
1246 
1247  $objects = array();
1248  while ($obj_rec = $ilDB->fetchAssoc($obj_set))
1249  {
1250  if ((!$a_omit_trash) || ilObject::_hasUntrashedReference($obj_rec["obj_id"]))
1251  {
1252  $objects[$obj_rec["title"].".".$obj_rec["obj_id"]] = array("id" => $obj_rec["obj_id"],
1253  "type" => $obj_rec["type"], "title" => $obj_rec["title"],
1254  "description" => $obj_rec["description"]);
1255  }
1256  }
1257  ksort($objects);
1258  return $objects;
1259  }
1260 
1266  function putInTree($a_parent_ref)
1267  {
1268  global $tree, $log;
1269 
1270  $tree->insertNode($this->getRefId(), $a_parent_ref);
1271 
1272  // write log entry
1273  $log->write("ilObject::putInTree(), parent_ref: $a_parent_ref, ref_id: ".
1274  $this->getRefId().", obj_id: ".$this->getId().", type: ".
1275  $this->getType().", title: ".$this->getTitle());
1276 
1277  }
1278 
1285  function setPermissions($a_parent_ref)
1286  {
1287  global $rbacadmin, $rbacreview;
1288 
1289  $parentRoles = $rbacreview->getParentRoleIds($a_parent_ref);
1290 
1291  foreach ($parentRoles as $parRol)
1292  {
1293  $ops = $rbacreview->getOperationsOfRole($parRol["obj_id"], $this->getType(), $parRol["parent"]);
1294  $rbacadmin->grantPermission($parRol["obj_id"], $ops, $this->getRefId());
1295  }
1296 
1297  $this->initDefaultRoles();
1298  }
1299 
1306  function createReference()
1307  {
1308  global $ilDB;
1309 
1310  if (!isset($this->id))
1311  {
1312  $message = "ilObject::createNewReference(): No obj_id given!";
1313  $this->raiseError($message,$this->ilias->error_obj->WARNING);
1314  }
1315 
1316  $next_id = $ilDB->nextId('object_reference');
1317  $query = "INSERT INTO object_reference ".
1318  "(ref_id, obj_id) VALUES (".$ilDB->quote($next_id,'integer').','.$ilDB->quote($this->id ,'integer').")";
1319  $this->ilias->db->query($query);
1320 
1321  $this->ref_id = $next_id;
1322  $this->referenced = true;
1323 
1324  return $this->ref_id;
1325  }
1326 
1327 
1334  function countReferences()
1335  {
1336  global $ilDB;
1337 
1338  if (!isset($this->id))
1339  {
1340  $message = "ilObject::countReferences(): No obj_id given!";
1341  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
1342  }
1343 
1344  $query = "SELECT COUNT(ref_id) num FROM object_reference ".
1345  "WHERE obj_id = ".$ilDB->quote($this->id,'integer')." ";
1346  $res = $ilDB->query($query);
1347  $row = $ilDB->fetchObject($res);
1348 
1349  return $row->num;
1350  }
1351 
1352 
1353 
1354 
1364  function delete()
1365  {
1366  global $rbacadmin, $log, $ilDB;
1367 
1368  $remove = false;
1369 
1370  // delete object_data entry
1371  if ((!$this->referenced) || ($this->countReferences() == 1))
1372  {
1373  // check type match
1374  $db_type = ilObject::_lookupType($this->getId());
1375  if ($this->type != $db_type)
1376  {
1377  $message = "ilObject::delete(): Type mismatch. Object with obj_id: ".$this->id." ".
1378  "was instantiated by type '".$this->type."'. DB type is: ".$db_type;
1379 
1380  // write log entry
1381  $log->write($message);
1382 
1383  // raise error
1384  $this->ilias->raiseError("ilObject::delete(): Type mismatch. (".$this->type."/".$this->id.")",$this->ilias->error_obj->WARNING);
1385  }
1386 
1387  // delete entry in object_data
1388  $q = "DELETE FROM object_data ".
1389  "WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
1390  $ilDB->manipulate($q);
1391 
1392  // delete long description
1393  $query = "DELETE FROM object_description WHERE obj_id = ".
1394  $ilDB->quote($this->getId(), "integer");
1395  $ilDB->manipulate($query);
1396 
1397  // write log entry
1398  $log->write("ilObject::delete(), deleted object, obj_id: ".$this->getId().", type: ".
1399  $this->getType().", title: ".$this->getTitle());
1400 
1401  // remove news
1402  include_once("./Services/News/classes/class.ilNewsItem.php");
1403  $news_item = new ilNewsItem();
1404  $news_item->deleteNewsOfContext($this->getId(), $this->getType());
1405  include_once("./Services/Block/classes/class.ilBlockSetting.php");
1407 
1408  $remove = true;
1409  }
1410  else
1411  {
1412  // write log entry
1413  $log->write("ilObject::delete(), object not deleted, number of references: ".
1414  $this->countReferences().", obj_id: ".$this->getId().", type: ".
1415  $this->getType().", title: ".$this->getTitle());
1416  }
1417 
1418  // delete object_reference entry
1419  if ($this->referenced)
1420  {
1421  // delete entry in object_reference
1422  $query = "DELETE FROM object_reference ".
1423  "WHERE ref_id = ".$ilDB->quote($this->getRefId(),'integer');
1424  $res = $ilDB->manipulate($query);
1425 
1426  // write log entry
1427  $log->write("ilObject::delete(), reference deleted, ref_id: ".$this->getRefId().
1428  ", obj_id: ".$this->getId().", type: ".
1429  $this->getType().", title: ".$this->getTitle());
1430 
1431  // DELETE PERMISSION ENTRIES IN RBAC_PA
1432  // DONE: method overwritten in ilObjRole & ilObjUser.
1433  // this call only applies for objects in rbac (not usr,role,rolt)
1434  // TODO: Do this for role templates too
1435  $rbacadmin->revokePermission($this->getRefId(),0,false);
1436 
1437  include_once "Services/AccessControl/classes/class.ilRbacLog.php";
1438  ilRbacLog::delete($this->getRefId());
1439 
1440  // Remove desktop items
1442  }
1443 
1444  // remove conditions
1445  if ($this->referenced)
1446  {
1447  $ch =& new ilConditionHandler();
1448  $ch->delete($this->getRefId());
1449  unset($ch);
1450  }
1451 
1452  // BEGIN WebDAV: Delete WebDAV properties
1453  $query = "DELETE FROM dav_property ".
1454  "WHERE obj_id = ".$ilDB->quote($this->getId(),'integer');
1455  $res = $ilDB->manipulate($query);
1456  // END WebDAV: Delete WebDAV properties
1457 
1458  include_once './Services/Tracking/classes/class.ilChangeEvent.php';
1459  ilChangeEvent::_delete($this->getId());
1460 
1461  return $remove;
1462  }
1463 
1471  function initDefaultRoles()
1472  {
1473  return array();
1474  }
1475 
1485  function createRoleFolder()
1486  {
1487  global $rbacreview;
1488 
1489  // does a role folder already exists?
1490  // (this check is only 'to be sure' that no second role folder is created under one object.
1491  // the if-construct should never return true)
1492  if ($rolf_data = $rbacreview->getRoleFolderofObject($this->getRefId()))
1493  {
1494  $rfoldObj = $this->ilias->obj_factory->getInstanceByRefId($rolf_data["ref_id"]);
1495  }
1496  else
1497  {
1498  include_once ("./Services/AccessControl/classes/class.ilObjRoleFolder.php");
1499  $rfoldObj = new ilObjRoleFolder();
1500  $rfoldObj->setTitle($this->getId());
1501  $rfoldObj->setDescription(" (ref_id ".$this->getRefId().")");
1502  $rfoldObj->create();
1503  $rfoldObj->createReference();
1504  $rfoldObj->putInTree($this->getRefId());
1505  $rfoldObj->setPermissions($this->getRefId());
1506  }
1507 
1508  return $rfoldObj;
1509  }
1510 
1519  function _exists($a_id, $a_reference = false)
1520  {
1521  global $ilias, $ilDB;
1522 
1523  if ($a_reference)
1524  {
1525  $q = "SELECT * FROM object_data ".
1526  "LEFT JOIN object_reference ON object_reference.obj_id=object_data.obj_id ".
1527  "WHERE object_reference.ref_id= ".$ilDB->quote($a_id, "integer");
1528  }
1529  else
1530  {
1531  $q = "SELECT * FROM object_data WHERE obj_id=".$ilDB->quote($a_id, "integer");
1532  }
1533 
1534  $r = $ilDB->query($q);
1535 
1536  return $ilDB->numRows($r) ? true : false;
1537  }
1538 
1551  function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
1552  {
1553  global $tree;
1554 
1555  $parent_id = (int) $tree->getParentId($a_node_id);
1556 
1557  if ($parent_id != 0)
1558  {
1559  $obj_data =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
1560  $obj_data->notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$parent_id,$a_params);
1561  }
1562 
1563  return true;
1564  }
1565 
1566  // toggle subscription interface
1567  function setRegisterMode($a_bool)
1568  {
1569  $this->register = (bool) $a_bool;
1570  }
1571 
1572  // check register status of current user
1573  // abstract method; overwrite in object type class
1574  function isUserRegistered($a_user_id = 0)
1575  {
1576  return false;
1577  }
1578 
1580  {
1581  return $this->register;
1582  }
1583 
1584 
1585  function getXMLZip()
1586  {
1587  return false;
1588  }
1589  function getHTMLDirectory()
1590  {
1591  return false;
1592  }
1593 
1597  static function _getObjectsByType($a_obj_type = "", $a_owner = "")
1598  {
1599  global $ilDB;
1600 
1601  $order = " ORDER BY title";
1602 
1603  // where clause
1604  if ($a_obj_type)
1605  {
1606  $where_clause = "WHERE type = ".
1607  $ilDB->quote($a_obj_type, "text");
1608 
1609  if ($a_owner != "")
1610  {
1611  $where_clause.= " AND owner = ".$ilDB->quote($a_owner, "integer");
1612  }
1613  }
1614 
1615  $q = "SELECT * FROM object_data ".$where_clause.$order;
1616  $r = $ilDB->query($q);
1617 
1618  $arr = array();
1619  if ($ilDB->numRows($r) > 0)
1620  {
1621  while ($row = $ilDB->fetchAssoc($r))
1622  {
1623  $row["desc"] = $row["description"];
1624  $arr[$row["obj_id"]] = $row;
1625  }
1626  }
1627 
1628  return $arr;
1629  }
1630 
1639  public static function _prepareCloneSelection($a_ref_ids,$new_type)
1640  {
1641  global $ilDB,$lng,$objDefinition;
1642 
1643  $query = "SELECT obj_data.title obj_title,path_data.title path_title,child FROM tree ".
1644  "JOIN object_reference obj_ref ON child = obj_ref.ref_id ".
1645  "JOIN object_data obj_data ON obj_ref.obj_id = obj_data.obj_id ".
1646  "JOIN object_reference path_ref ON parent = path_ref.ref_id ".
1647  "JOIN object_data path_data ON path_ref.obj_id = path_data.obj_id ".
1648  "WHERE ".$ilDB->in("child", $a_ref_ids, false, "integer")." ".
1649  "ORDER BY obj_data.title ";
1650  $res = $ilDB->query($query);
1651 
1652  if (!$objDefinition->isPlugin($new_type))
1653  {
1654  $options[0] = $lng->txt('obj_'.$new_type.'_select');
1655  }
1656  else
1657  {
1658  include_once("./Services/Component/classes/class.ilPlugin.php");
1659  $options[0] = ilPlugin::lookupTxt("rep_robj", $new_type, "obj_".$new_type."_select");
1660  }
1661 
1662  while($row = $ilDB->fetchObject($res))
1663  {
1664  if(strlen($title = $row->obj_title) > 40)
1665  {
1666  $title = substr($title,0,40).'...';
1667  }
1668  if(strlen($path = $row->path_title) > 40)
1669  {
1670  $path = substr($path,0,40).'...';
1671  }
1672 
1673 
1674  $options[$row->child] = ($title.' ('.$lng->txt('path').': '.$path.')');
1675  }
1676  return $options ? $options : array();
1677  }
1678 
1688  public function cloneObject($a_target_id,$a_copy_id = 0)
1689  {
1690  global $objDefinition,$ilUser,$rbacadmin, $ilDB;
1691 
1692  $location = $objDefinition->getLocation($this->getType());
1693  $class_name = ('ilObj'.$objDefinition->getClassName($this->getType()));
1694 
1695  $title = $this->appendCopyInfo($a_target_id,$a_copy_id);
1696 
1697  // create instance
1698  include_once($location."/class.".$class_name.".php");
1699  $new_obj = new $class_name(0, false);
1700  $new_obj->setOwner($ilUser->getId());
1701  $new_obj->setTitle($title);
1702  $new_obj->setDescription($this->getLongDescription());
1703  $new_obj->setType($this->getType());
1704  // Choose upload mode to avoid creation of additional settings, db entries ...
1705  $new_obj->create(true);
1706  $new_obj->createReference();
1707  $new_obj->putInTree($a_target_id);
1708  $new_obj->setPermissions($a_target_id);
1709 
1710  // copy local roles
1711  $rbacadmin->copyLocalRoles($this->getRefId(),$new_obj->getRefId());
1712 
1713  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
1714  ilAdvancedMDValues::_cloneValues($this->getId(),$new_obj->getId());
1715 
1716  // BEGIN WebDAV: Clone WebDAV properties
1717  $query = "INSERT INTO dav_property (obj_id,node_id,ns,name,value) ".
1718  "SELECT ".$ilDB->quote($new_obj->getId(),'integer').",node_id,ns,name,value ".
1719  "FROM dav_property ".
1720  "WHERE obj_id = ".$ilDB->quote($this->getId(),'integer');
1721  $res = $ilDB->manipulate($query);
1722  // END WebDAV: Clone WebDAV properties
1723 
1724  return $new_obj;
1725  }
1726 
1734  public function appendCopyInfo($a_target_id,$a_copy_id)
1735  {
1736  global $tree;
1737 
1738  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
1739  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
1740  if(!$cp_options->isRootNode($this->getRefId()))
1741  {
1742  return $this->getTitle();
1743  }
1744  $nodes = $tree->getChilds($a_target_id);
1745 
1746  $title_unique = false;
1747  require_once 'Modules/File/classes/class.ilObjFileAccess.php';
1748  $numberOfCopy = 1;
1750  while(!$title_unique)
1751  {
1752  $found = 0;
1753  foreach($nodes as $node)
1754  {
1755  if(($title == $node['title']) and ($this->getType() == $node['type']))
1756  {
1757  $found++;
1758  }
1759  }
1760  if($found > 0)
1761  {
1762  $title = ilObjFileAccess::_appendNumberOfCopyToFilename($this->getTitle(), ++$numberOfCopy);
1763  }
1764  else
1765  {
1766  break;
1767  }
1768  }
1769  return $title;
1770  }
1771 
1784  public function cloneDependencies($a_target_id,$a_copy_id)
1785  {
1786  return true;
1787 
1788  //include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
1789  //$cwo = ilCopyWizardOptions::_getInstance($a_copy_id);
1790  //$mappings = $cwo->getMappings();
1791 
1792  }
1793 
1801  public function cloneMetaData($target_obj)
1802  {
1803  include_once "./Services/MetaData/classes/class.ilMD.php";
1804  $md = new ilMD($this->getId(),0,$this->getType());
1805  $md->cloneMD($target_obj->getId(),0,$target_obj->getType());
1806  return true;
1807  }
1808 
1817  public static function _getIcon($a_obj_id = "", $a_size = "big", $a_type = "",
1818  $a_offline = false)
1819  {
1820  global $ilSetting, $objDefinition;
1821 
1822  if ($a_obj_id == "" && $a_type == "")
1823  {
1824  return "";
1825  }
1826 
1827  if ($a_type == "")
1828  {
1829  $a_type = ilObject::_lookupType($a_obj_id);
1830  }
1831 
1832  if ($a_size == "")
1833  {
1834  $a_size = "big";
1835  }
1836 
1837  if ($ilSetting->get("custom_icons") &&
1838  in_array($a_type, array("cat","grp","crs", "root")))
1839  {
1840  require_once("./Services/Container/classes/class.ilContainer.php");
1841  if (ilContainer::_lookupContainerSetting($a_obj_id, "icon_".$a_size))
1842  {
1843  $cont_dir = ilContainer::_getContainerDirectory($a_obj_id);
1844  $file_name = $cont_dir."/icon_".$a_size.".gif";
1845 
1846  if (is_file($file_name))
1847  {
1848  return $file_name;
1849  }
1850  }
1851  }
1852 
1853  switch($a_size)
1854  {
1855  case "small": $suff = ""; break;
1856  case "tiny": $suff = "_s"; break;
1857  default: $suff = "_b"; break;
1858  }
1859  if (!$a_offline)
1860  {
1861  if ($objDefinition->isRBACObject($a_type))
1862  {
1863  if (!$objDefinition->isPlugin($a_type))
1864  {
1865  return ilUtil::getImagePath("icon_".$a_type.$suff.".gif");
1866  }
1867  else
1868  {
1869  include_once("./Services/Repository/classes/class.ilRepositoryObjectPlugin.php");
1870  return ilRepositoryObjectPlugin::_getIcon($a_type, $a_size);
1871  }
1872  }
1873  return ilUtil::getImagePath("icon_".$a_type.$suff.".gif");
1874  }
1875  else
1876  {
1877  return "./images/icon_".$a_type.$suff.".gif";
1878  }
1879  }
1880 
1887  static final function collectDeletionDependencies(&$deps, $a_ref_id, $a_obj_id, $a_type, $a_depth = 0)
1888  {
1889  global $objDefinition, $tree;
1890 
1891  if ($a_depth == 0)
1892  {
1893  $deps["dep"] = array();
1894  }
1895 
1896  $deps["del_ids"][$a_obj_id] = $a_obj_id;
1897 
1898  if (!$objDefinition->isPlugin($type))
1899  {
1900  $class_name = "ilObj".$objDefinition->getClassName($a_type);
1901  $location = $objDefinition->getLocation($a_type);
1902  include_once($location."/class.".$class_name.".php");
1903  $odeps = call_user_func(array($class_name, "getDeletionDependencies"), $a_obj_id);
1904  if (is_array($odeps))
1905  {
1906  foreach ($odeps as $id => $message)
1907  {
1908  $deps["dep"][$id][$a_obj_id][] = $message;
1909  }
1910  }
1911 
1912  // get deletion dependency of childs
1913  foreach ($tree->getChilds($a_ref_id) as $c)
1914  {
1915  ilObject::collectDeletionDependencies($deps, $c["child"], $c["obj_id"], $c["type"], $a_depth + 1);
1916  }
1917  }
1918 
1919  // delete all dependencies to objects that will be deleted, too
1920  if ($a_depth == 0)
1921  {
1922  foreach ($deps["del_ids"] as $obj_id)
1923  {
1924  unset($deps["dep"][$obj_id]);
1925  }
1926  $deps = $deps["dep"];
1927  }
1928  }
1929 
1934  static function getDeletionDependencies($a_obj_id)
1935  {
1936  return false;
1937  }
1938 } // END class.ilObject
1939 ?>