ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObject.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
33 class ilObject
34 {
40  var $ilias;
41 
47  var $lng;
48 
54  var $id; // true object_id!!!!
55  var $ref_id;// reference_id
56  var $type;
57  var $title;
58  // BEGIN WebDAV: WebDAV needs to access the untranslated title of an object
60  // END WebDAV: WebDAV needs to access the untranslated title of an object
61  var $desc;
63  var $owner;
67  var $register = false; // registering required for object? set to true to implement a subscription interface
68 
75 
82 
88 
93  var $max_desc;
94 
99  var $add_dots;
100 
105 
112  function ilObject($a_id = 0, $a_reference = true)
113  {
114  global $ilias, $lng, $ilBench;
115 
116  $ilBench->start("Core", "ilObject_Constructor");
117 
118  if (DEBUG)
119  {
120  echo "<br/><font color=\"red\">type(".$this->type.") id(".$a_id.") referenced(".$a_reference.")</font>";
121  }
122 
123  $this->ilias =& $ilias;
124  $this->lng =& $lng;
125 
126  $this->max_title = MAXLENGTH_OBJ_TITLE;
127  $this->max_desc = MAXLENGTH_OBJ_DESC;
128  $this->add_dots = true;
129 
130  $this->referenced = $a_reference;
131  $this->call_by_reference = $a_reference;
132 
133  if ($a_id == 0)
134  {
135  $this->referenced = false; // newly created objects are never referenced
136  } // they will get referenced if createReference() is called
137 
138  if ($this->referenced)
139  {
140  $this->ref_id = $a_id;
141  }
142  else
143  {
144  $this->id = $a_id;
145  }
146  // read object data
147  if ($a_id != 0)
148  {
149  $this->read();
150  }
151 
152  $ilBench->stop("Core", "ilObject_Constructor");
153  }
154 
158  function withReferences()
159  {
160  // both vars could differ. this method should always return true if one of them is true without changing their status
161  return ($this->call_by_reference) ? true : $this->referenced;
162  }
163 
164 
170  function read($a_force_db = false)
171  {
172  global $objDefinition, $ilBench, $ilDB, $log;
173 
174  $ilBench->start("Core", "ilObject_read");
175 
176  if (isset($this->obj_data_record) && !$a_force_db)
177  {
178  $obj = $this->obj_data_record;
179  }
180  else if ($this->referenced)
181  {
182  // check reference id
183  if (!isset($this->ref_id))
184  {
185  $message = "ilObject::read(): No ref_id given! (".$this->type.")";
186  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
187  }
188 
189  // read object data
190  $ilBench->start("Core", "ilObject_read_readData");
191  /* old query (very slow)
192  $q = "SELECT * FROM object_data ".
193  "LEFT JOIN object_reference ON object_data.obj_id=object_reference.obj_id ".
194  "WHERE object_reference.ref_id='".$this->ref_id."'"; */
195 
196  $q = "SELECT * FROM object_data, object_reference WHERE object_data.obj_id=object_reference.obj_id ".
197  "AND object_reference.ref_id= ".$ilDB->quote($this->ref_id);
198  $object_set = $this->ilias->db->query($q);
199  $ilBench->stop("Core", "ilObject_read_readData");
200 
201  // check number of records
202  if ($object_set->numRows() == 0)
203  {
204  $message = "ilObject::read(): Object with ref_id ".$this->ref_id." not found! (".$this->type.")";
205  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
206  }
207 
208  $obj = $object_set->fetchRow(DB_FETCHMODE_ASSOC);
209  }
210  else
211  {
212  // check object id
213  if (!isset($this->id))
214  {
215  $message = "ilObject::read(): No obj_id given! (".$this->type.")";
216  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
217  }
218 
219  // read object data
220  $q = "SELECT * FROM object_data ".
221  "WHERE obj_id = ".$ilDB->quote($this->id);
222  $object_set = $ilDB->query($q);
223 
224  // check number of records
225  if ($object_set->numRows() == 0)
226  {
227  $message = "ilObject::read(): Object with obj_id: ".$this->id." (".$this->type.") not found!";
228  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
229  }
230 
231  $obj = $object_set->fetchRow(DB_FETCHMODE_ASSOC);
232  }
233 
234  $this->id = $obj["obj_id"];
235 
236  // check type match
237  if ($this->type != $obj["type"])
238  {
239  $message = "ilObject::read(): Type mismatch. Object with obj_id: ".$this->id." ".
240  "was instantiated by type '".$this->type."'. DB type is: ".$obj["type"];
241 
242  // write log entry
243  $log->write($message);
244 
245  // raise error
246  $this->ilias->raiseError("ilObject::read(): Type mismatch. (".$this->type."/".$this->id.")",$this->ilias->error_obj->WARNING);
247  }
248 
249  $this->type = $obj["type"];
250  $this->title = $obj["title"];
251  // BEGIN WebDAV: WebDAV needs to access the untranslated title of an object
252  $this->untranslatedTitle = $obj["title"];
253  // END WebDAV: WebDAV needs to access the untranslated title of an object
254  $this->desc = $obj["description"];
255  $this->owner = $obj["owner"];
256  $this->create_date = $obj["create_date"];
257  $this->last_update = $obj["last_update"];
258  $this->import_id = $obj["import_id"];
259 
260  if($objDefinition->isRBACObject($this->getType()))
261  {
262  // Read long description
263  $query = "SELECT * FROM object_description WHERE obj_id = ".$ilDB->quote($this->id);
264  $res = $this->ilias->db->query($query);
265  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
266  {
267  $this->setDescription($row->description);
268  }
269  }
270 
271  // multilingual support systemobjects (sys) & categories (db)
272  $ilBench->start("Core", "ilObject_Constructor_getTranslation");
273  $translation_type = $objDefinition->getTranslationType($this->type);
274 
275  if ($translation_type == "sys")
276  {
277  $this->title = $this->lng->txt("obj_".$this->type);
278  $this->desc = $this->lng->txt("obj_".$this->type."_desc");
279  }
280  elseif ($translation_type == "db")
281  {
282  $q = "SELECT title,description FROM object_translation ".
283  "WHERE obj_id = ".$ilDB->quote($this->id)." ".
284  "AND lang_code = ".$ilDB->quote($this->ilias->account->getCurrentLanguage())." ".
285  "AND NOT lang_default = 1";
286  $r = $this->ilias->db->query($q);
287  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
288 
289  if ($row)
290  {
291  $this->title = $row->title;
292  $this->setDescription($row->description);
293  #$this->desc = $row->description;
294  }
295  }
296 
297  $ilBench->stop("Core", "ilObject_Constructor_getTranslation");
298 
299  $ilBench->stop("Core", "ilObject_read");
300  }
301 
307  function getId()
308  {
309  return $this->id;
310  }
311 
317  function setId($a_id)
318  {
319  $this->id = $a_id;
320  }
321 
327  function setRefId($a_id)
328  {
329  $this->ref_id = $a_id;
330  $this->referenced = true;
331  }
332 
338  function getRefId()
339  {
340  return $this->ref_id;
341  }
342 
348  function getType()
349  {
350  return $this->type;
351  }
352 
358  function setType($a_type)
359  {
360  $this->type = $a_type;
361  }
362 
372  public function getPresentationTitle()
373  {
374  return $this->getTitle();
375  }
376 
377 
383  function getTitle()
384  {
385  return $this->title;
386  }
387  // BEGIN WebDAV: WebDAV needs to access the untranslated title of an object
394  {
396  }
397  // END WebDAV: WebDAV needs to access the untranslated title of an object
398 
405  function setTitle($a_title)
406  {
407  if ($a_title == "")
408  {
409  $a_title = "NO TITLE";
410  }
411 
412  $this->title = ilUtil::shortenText($a_title, $this->max_title, $this->add_dots);
413  // BEGIN WebDAV: WebDAV needs to access the untranslated title of an object
414  $this->untranslatedTitle = $this->title;
415  // END WebDAV: WebDAV needs to access the untranslated title of an object
416  }
417 
424  function getDescription()
425  {
426  return $this->desc;
427  }
428 
435  function setDescription($a_desc)
436  {
437  // Shortened form is storted in object_data. Long form is stored in object_description
438  $this->desc = ilUtil::shortenText($a_desc, $this->max_desc, $this->add_dots);
439 
440  $this->long_desc = $a_desc;
441 
442  return true;
443  }
444 
452  {
453  return strlen($this->long_desc) ? $this->long_desc : $this->desc;
454  }
455 
462  function getImportId()
463  {
464  return $this->import_id;
465  }
466 
473  function setImportId($a_import_id)
474  {
475  $this->import_id = $a_import_id;
476  }
477 
478  function _lookupObjIdByImportId($a_import_id)
479  {
480  global $ilDB;
481 
482  $query = "SELECT * FROM object_data ".
483  "WHERE import_id = ".$ilDB->quote($a_import_id)." ".
484  "ORDER BY create_date DESC";
485  $res = $ilDB->query($query);
486  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
487  {
488  return $row->obj_id;
489  }
490  return 0;
491  }
492 
499  function getOwner()
500  {
501  return $this->owner;
502  }
503 
504  /*
505  * get full name of object owner
506  *
507  * @access public
508  * @return string owner name or unknown
509  */
510  function getOwnerName()
511  {
512  return ilObject::_lookupOwnerName($this->getOwner());
513  }
514 
518  function _lookupOwnerName($a_owner_id)
519  {
520  global $lng;
521 
522  if ($a_owner_id != -1)
523  {
524  if (ilObject::_exists($a_owner_id))
525  {
526  $owner = new ilObjUser($a_owner_id);
527  }
528  }
529 
530  if (is_object($owner))
531  {
532  $own_name = $owner->getFullname();
533  }
534  else
535  {
536  $own_name = $lng->txt("unknown");
537  }
538 
539  return $own_name;
540  }
541 
548  function setOwner($a_owner)
549  {
550  $this->owner = $a_owner;
551  }
552 
553 
554 
560  function getCreateDate()
561  {
562  return $this->create_date;
563  }
564 
570  function getLastUpdateDate()
571  {
572  return $this->last_update;
573  }
574 
583  function setObjDataRecord($a_record)
584  {
585  $this->obj_data_record = $a_record;
586  }
587 
596  function create()
597  {
598  global $ilDB, $log,$ilUser,$objDefinition;
599 
600  if (!isset($this->type))
601  {
602  $message = get_class($this)."::create(): No object type given!";
603  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
604  }
605 
606  // write log entry
607  $log->write("ilObject::create(), start");
608 
609  $this->title = ilUtil::shortenText($this->getTitle(), $this->max_title, $this->add_dots);
610  $this->desc = ilUtil::shortenText($this->getDescription(), $this->max_desc, $this->add_dots);
611 
612  // determine owner
613  if ($this->getOwner() > 0)
614  {
615  $owner = $this->getOwner();
616  }
617  elseif(is_object($ilUser))
618  {
619  $owner = $ilUser->getId();
620  }
621  else
622  {
623  $owner = 0;
624  }
625  $q = "INSERT INTO object_data ".
626  "(type,title,description,owner,create_date,last_update,import_id) ".
627  "VALUES ".
628  "(".
629  $ilDB->quote($this->type).",".
630  $ilDB->quote($this->getTitle()).",".
631  $ilDB->quote($this->getDescription()).",".
632  $ilDB->quote($owner).",now(),now(),".
633  $ilDB->quote($this->getImportId()).")";
634 
635  $ilDB->query($q);
636 
637  $this->id = $ilDB->getLastInsertId();
638 
639 
640 
641  // Save long form of description if is rbac object
642  if($objDefinition->isRBACObject($this->getType()))
643  {
644  $query = "INSERT INTO object_description SET ".
645  "obj_id = ".$ilDB->quote($this->id).",".
646  "description = ".$ilDB->quote($this->getLongDescription());
647 
648  $ilDB->query($query);
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);
657  $obj_set = $this->ilias->db->query($q);
658  $obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC);
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  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()).",".
685  "description = ".$ilDB->quote($this->getDescription()).", ".
686  "import_id = ".$ilDB->quote($this->getImportId()).",".
687  "last_update = now() ".
688  "WHERE obj_id = ".$ilDB->quote($this->getId());
689  $this->ilias->db->query($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());
696  $obj_set = $this->ilias->db->query($q);
697  $obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC);
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()));
705  if($res->numRows())
706  {
707  $query = "UPDATE object_description SET description = ".
708  $ilDB->quote($this->getLongDescription())." ".
709  "WHERE obj_id = ".$ilDB->quote($this->getId());
710  }
711  else
712  {
713  $query = "INSERT INTO object_description SET obj_id = ".$ilDB->quote($this->getId()).", ".
714  "description = ".$ilDB->quote($this->getLongDescription());
715  }
716  $this->ilias->db->query($query);
717  }
718 
719  return true;
720  }
721 
733  function MDUpdateListener($a_element)
734  {
735  include_once 'Services/MetaData/classes/class.ilMD.php';
736 
737  switch($a_element)
738  {
739  case 'General':
740 
741  // Update Title and description
742  $md = new ilMD($this->getId(),0, $this->getType());
743  if(!is_object($md_gen = $md->getGeneral()))
744  {
745  return false;
746  }
747 
748  ilObject::_writeTitle($this->getId(),$md_gen->getTitle());
749  $this->setTitle($md_gen->getTitle());
750 
751  foreach($md_gen->getDescriptionIds() as $id)
752  {
753  $md_des = $md_gen->getDescription($id);
754  ilObject::_writeDescription($this->getId(),$md_des->getDescription());
755  $this->setDescription($md_des->getDescription());
756  break;
757  }
758 
759  break;
760 
761  default:
762  }
763  return true;
764  }
765 
769  function createMetaData()
770  {
771  include_once 'Services/MetaData/classes/class.ilMDCreator.php';
772 
773  global $ilUser;
774 
775  $md_creator = new ilMDCreator($this->getId(),0,$this->getType());
776  $md_creator->setTitle($this->getTitle());
777  $md_creator->setTitleLanguage($ilUser->getPref('language'));
778  $md_creator->setDescription($this->getLongDescription());
779  $md_creator->setDescriptionLanguage($ilUser->getPref('language'));
780  $md_creator->setKeywordLanguage($ilUser->getPref('language'));
781  $md_creator->setLanguage($ilUser->getPref('language'));
782  $md_creator->create();
783 
784  return true;
785  }
786 
790  function updateMetaData()
791  {
792  include_once("Services/MetaData/classes/class.ilMD.php");
793  include_once("Services/MetaData/classes/class.ilMDGeneral.php");
794  include_once("Services/MetaData/classes/class.ilMDDescription.php");
795 
796  $md =& new ilMD($this->getId(), 0, $this->getType());
797  $md_gen =& $md->getGeneral();
798  // BEGIN WebDAV: meta data can be missing sometimes.
799  if ($md_gen == null)
800  {
801  $this->createMetaData();
802  $md =& new ilMD($this->getId(), 0, $this->getType());
803  $md_gen =& $md->getGeneral();
804  }
805  // END WebDAV: meta data can be missing sometimes.
806  $md_gen->setTitle($this->getTitle());
807 
808  // sets first description (maybe not appropriate)
809  $md_des_ids =& $md_gen->getDescriptionIds();
810  if (count($md_des_ids) > 0)
811  {
812  $md_des =& $md_gen->getDescription($md_des_ids[0]);
813  $md_des->setDescription($this->getLongDescription());
814  $md_des->update();
815  }
816  $md_gen->update();
817 
818  }
819 
823  function deleteMetaData()
824  {
825  // Delete meta data
826  include_once('Services/MetaData/classes/class.ilMD.php');
827  $md = new ilMD($this->getId(), 0, $this->getType());
828  $md->deleteAll();
829  }
830 
837  function updateOwner()
838  {
839  global $ilDB;
840 
841  $q = "UPDATE object_data ".
842  "SET ".
843  "owner = ".$ilDB->quote($this->getOwner()).", ".
844  "last_update = now() ".
845  "WHERE obj_id = ".$ilDB->quote($this->getId());
846  $this->ilias->db->query($q);
847 
848  $q = "SELECT last_update FROM object_data".
849  " WHERE obj_id = ".$ilDB->quote($this->getId());
850  $obj_set = $this->ilias->db->query($q);
851  $obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC);
852  $this->last_update = $obj_rec["last_update"];
853 
854  return true;
855  }
856 
864  function _getIdForImportId($a_import_id)
865  {
866  global $ilDB;
867 
868  $q = "SELECT * FROM object_data WHERE import_id = ".$ilDB->quote($a_import_id).
869  " ORDER BY create_date DESC LIMIT 1";
870  $obj_set = $ilDB->query($q);
871 
872  if ($obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC))
873  {
874  return $obj_rec["obj_id"];
875  }
876  else
877  {
878  return 0;
879  }
880  }
881 
887  function _getAllReferences($a_id)
888  {
889  global $ilDB;
890 
891  $q = "SELECT * FROM object_reference WHERE obj_id = ".
892  $ilDB->quote($a_id);
893  $obj_set = $ilDB->query($q);
894  $ref = array();
895 
896  while ($obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC))
897  {
898  $ref[$obj_rec["ref_id"]] = $obj_rec["ref_id"];
899  }
900 
901  return $ref;
902  }
903 
909  function _lookupTitle($a_id)
910  {
911  global $ilObjDataCache;
912 
913  $tit = $ilObjDataCache->lookupTitle($a_id);
914 //echo "<br>LOOKING-$a_id-:$tit";
915  return $tit;
916  }
917 
918  public static function _getIdsForTitle($title, $type = '')
919  {
920  global $ilDB;
921 
922  $query = "SELECT obj_id FROM object_data WHERE title = ?";
923  $datatypes = array('text');
924  $values = array($title);
925  if($type != '')
926  {
927  $query .= " AND type = ?";
928  $datatypes[] = 'text';
929  $values[] = $type;
930  }
931 
932  $statement = $ilDB->prepare($query, $datatypes);
933  $result = $ilDB->execute($statement, $values);
934 
935  $object_ids = array();
936  while($row = $ilDB->fetchAssoc($result))
937  {
938  $object_ids[] = $row['obj_id'];
939  }
940 
941  return is_array($object_ids) ? $object_ids : array();
942  }
943 
949  function _lookupDescription($a_id)
950  {
951  global $ilObjDataCache;
952 
953  return $ilObjDataCache->lookupDescription($a_id);
954  }
955 
961  function _lookupLastUpdate($a_id, $a_as_string = false)
962  {
963  global $ilObjDataCache;
964 
965  if ($a_as_string)
966  {
967  return ilDatePresentation::formatDate(new ilDateTime($ilObjDataCache->lookupLastUpdate($a_id),IL_CAL_DATETIME));
968  }
969  else
970  {
971  return $ilObjDataCache->lookupLastUpdate($a_id);
972  }
973  }
974 
980  function _getLastUpdateOfObjects($a_objs)
981  {
982  global $ilDB;
983 
984  if (!is_array($a_objs))
985  {
986  $a_objs = array($a_objs);
987  }
988  $types = array();
989  $st = $ilDB->prepare("SELECT max(last_update) as last_update FROM object_data ".
990  "WHERE ".$ilDB->getInClause("obj_id", $a_objs),
991  $ilDB->addTypesToArray($types, "integer", count($a_objs)));
992  $set = $ilDB->execute($st, $a_objs);
993  $rec = $ilDB->fetchAssoc($set);
994 
995  return ($rec["last_update"]);
996  }
997 
998  function _lookupObjId($a_id)
999  {
1000  global $ilObjDataCache;
1001 
1002  return (int) $ilObjDataCache->lookupObjId($a_id);
1003  }
1004 
1008  function _setDeletedDate($a_ref_id)
1009  {
1010  global $ilDB;
1011 
1012  $q = "UPDATE object_reference SET deleted=now() ".
1013  "WHERE ref_id = ".$ilDB->quote($a_ref_id);
1014  $ilDB->query($q);
1015  }
1016 
1020  function _resetDeletedDate($a_ref_id)
1021  {
1022  global $ilDB;
1023 
1024  $q = "UPDATE object_reference SET deleted= ".$ilDB->quote("0000-00-00 00:00:00").
1025  " WHERE ref_id = ".$ilDB->quote($a_ref_id);
1026  $ilDB->query($q);
1027  }
1028 
1036  function _writeTitle($a_obj_id, $a_title)
1037  {
1038  global $ilDB;
1039 
1040  $q = "UPDATE object_data ".
1041  "SET ".
1042  "title = ".$ilDB->quote($a_title).",".
1043  "last_update = now() ".
1044  "WHERE obj_id = ".$ilDB->quote($a_obj_id);
1045 
1046  $ilDB->query($q);
1047  }
1048 
1056  function _writeDescription($a_obj_id, $a_desc)
1057  {
1058  global $ilDB,$objDefinition;
1059 
1060 
1061  $desc = ilUtil::shortenText($a_desc,MAXLENGTH_OBJ_DESC,true);
1062 
1063  $q = "UPDATE object_data ".
1064  "SET ".
1065  "description = ".$ilDB->quote($desc).",".
1066  "last_update = now() ".
1067  "WHERE obj_id = ".$ilDB->quote($a_obj_id);
1068 
1069  $ilDB->query($q);
1070 
1071  if($objDefinition->isRBACObject($this->getType()))
1072  {
1073  // Update long description
1074  $res = $ilDB->query("SELECT * FROM object_description WHERE obj_id = ".
1075  $ilDB->quote($a_obj_id));
1076  if($res->numRows())
1077  {
1078  $query = "UPDATE object_description SET description = ".
1079  $ilDB->quote($a_desc)." ".
1080  "WHERE obj_id = ".$ilDB->quote($this->getId());
1081  }
1082  else
1083  {
1084  $query = "INSERT INTO object_description SET obj_id = ".
1085  $ilDB->quote($this->getId()).", ".
1086  "description = ".$ilDB->quote($a_desc);
1087  }
1088  $ilDB->query($query);
1089  }
1090  }
1091 
1099  function _writeImportId($a_obj_id, $a_import_id)
1100  {
1101  global $ilDB;
1102 
1103  $q = "UPDATE object_data ".
1104  "SET ".
1105  "import_id = ".$ilDB->quote($a_import_id).",".
1106  "last_update = now() ".
1107  "WHERE obj_id = ".$ilDB->quote($a_obj_id);
1108 
1109  $ilDB->query($q);
1110  }
1111 
1117  function _lookupType($a_id,$a_reference = false)
1118  {
1119  global $ilObjDataCache;
1120 
1121  if($a_reference)
1122  {
1123  return $ilObjDataCache->lookupType($ilObjDataCache->lookupObjId($a_id));
1124  }
1125  return $ilObjDataCache->lookupType($a_id);
1126 
1127  global $ilDB;
1128 
1129  if ($a_reference === true)
1130  {
1131  $q = "SELECT type FROM object_reference as obr, object_data as obd ".
1132  "WHERE obr.ref_id = ".$ilDB->quote($a_id)." ".
1133  "AND obr.obj_id = obd.obj_id ";
1134 
1135  #$q = "SELECT type FROM object_data as obj ".
1136  # "LEFT JOIN object_reference as ref ON ref.obj_id=obj.obj_id ".
1137  # "WHERE ref.ref_id = '".$a_id."'";
1138  }
1139  else
1140  {
1141  $q = "SELECT type FROM object_data WHERE obj_id = ".$ilDB->quote($a_id);
1142  }
1143 
1144  $obj_set = $ilDB->query($q);
1145  $obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC);
1146 
1147  return $obj_rec["type"];
1148  }
1149 
1153  function _isInTrash($a_ref_id)
1154  {
1155  global $tree;
1156 
1157  return $tree->isSaved($a_ref_id);
1158  }
1159 
1163  function _hasUntrashedReference($a_obj_id)
1164  {
1165  $ref_ids = ilObject::_getAllReferences($a_obj_id);
1166  foreach($ref_ids as $ref_id)
1167  {
1168  if(!ilObject::_isInTrash($ref_id))
1169  {
1170  return true;
1171  }
1172  }
1173 
1174  return false;
1175  }
1176 
1182  function _lookupObjectId($a_ref_id)
1183  {
1184  global $ilObjDataCache;
1185 
1186  return (int) $ilObjDataCache->lookupObjId($a_ref_id);
1187  }
1188 
1199  function _getObjectsDataForType($a_type, $a_omit_trash = false)
1200  {
1201  global $ilDB;
1202 
1203  $q = "SELECT * FROM object_data WHERE type = ".$ilDB->quote($a_type);
1204  $obj_set = $ilDB->query($q);
1205 
1206  $objects = array();
1207  while ($obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC))
1208  {
1209  if ((!$a_omit_trash) || ilObject::_hasUntrashedReference($obj_rec["obj_id"]))
1210  {
1211  $objects[$obj_rec["title"].".".$obj_rec["obj_id"]] = array("id" => $obj_rec["obj_id"],
1212  "type" => $obj_rec["type"], "title" => $obj_rec["title"],
1213  "description" => $obj_rec["description"]);
1214  }
1215  }
1216  ksort($objects);
1217  return $objects;
1218  }
1219 
1225  function putInTree($a_parent_ref)
1226  {
1227  global $tree, $log;
1228 
1229  $tree->insertNode($this->getRefId(), $a_parent_ref);
1230 
1231  // write log entry
1232  $log->write("ilObject::putInTree(), parent_ref: $a_parent_ref, ref_id: ".
1233  $this->getRefId().", obj_id: ".$this->getId().", type: ".
1234  $this->getType().", title: ".$this->getTitle());
1235 
1236  }
1237 
1244  function setPermissions($a_parent_ref)
1245  {
1246  global $rbacadmin, $rbacreview;
1247 
1248  $parentRoles = $rbacreview->getParentRoleIds($a_parent_ref);
1249 
1250  foreach ($parentRoles as $parRol)
1251  {
1252  $ops = $rbacreview->getOperationsOfRole($parRol["obj_id"], $this->getType(), $parRol["parent"]);
1253  $rbacadmin->grantPermission($parRol["obj_id"], $ops, $this->getRefId());
1254  }
1255  }
1256 
1263  function createReference()
1264  {
1265  global $ilDB;
1266 
1267  if (!isset($this->id))
1268  {
1269  $message = "ilObject::createNewReference(): No obj_id given!";
1270  $this->raiseError($message,$this->ilias->error_obj->WARNING);
1271  }
1272 
1273  $q = "INSERT INTO object_reference ".
1274  "(obj_id) VALUES (".$ilDB->quote($this->id).")";
1275  $this->ilias->db->query($q);
1276 
1277  $this->ref_id = $ilDB->getLastInsertId();
1278  $this->referenced = true;
1279 
1280  return $this->ref_id;
1281  }
1282 
1283 
1290  function countReferences()
1291  {
1292  if (!isset($this->id))
1293  {
1294  $message = "ilObject::countReferences(): No obj_id given!";
1295  $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
1296  }
1297 
1298  $q = "SELECT COUNT(ref_id) AS num FROM object_reference ".
1299  "WHERE obj_id = '".$this->id."'";
1300  $row = $this->ilias->db->getRow($q);
1301 
1302  return $row->num;
1303  }
1304 
1305 
1306 
1307 
1317  function delete()
1318  {
1319  global $rbacadmin, $log, $ilDB;
1320 
1321  $remove = false;
1322 
1323  // delete object_data entry
1324  if ((!$this->referenced) || ($this->countReferences() == 1))
1325  {
1326  // check type match
1327  $db_type = ilObject::_lookupType($this->getId());
1328  if ($this->type != $db_type)
1329  {
1330  $message = "ilObject::delete(): Type mismatch. Object with obj_id: ".$this->id." ".
1331  "was instantiated by type '".$this->type."'. DB type is: ".$db_type;
1332 
1333  // write log entry
1334  $log->write($message);
1335 
1336  // raise error
1337  $this->ilias->raiseError("ilObject::delete(): Type mismatch. (".$this->type."/".$this->id.")",$this->ilias->error_obj->WARNING);
1338  }
1339 
1340  // delete entry in object_data
1341  $q = "DELETE FROM object_data ".
1342  "WHERE obj_id = ".$ilDB->quote($this->getId());
1343  $this->ilias->db->query($q);
1344 
1345  // delete long description
1346  $query = "DELETE FROM object_description WHERE obj_id = ".
1347  $ilDB->quote($this->getId());
1348  $this->ilias->db->query($query);
1349 
1350  // write log entry
1351  $log->write("ilObject::delete(), deleted object, obj_id: ".$this->getId().", type: ".
1352  $this->getType().", title: ".$this->getTitle());
1353 
1354  // remove news
1355  include_once("./Services/News/classes/class.ilNewsItem.php");
1356  $news_item = new ilNewsItem();
1357  $news_item->deleteNewsOfContext($this->getId(), $this->getType());
1358  include_once("./Services/Block/classes/class.ilBlockSetting.php");
1360 
1361  $remove = true;
1362  }
1363  else
1364  {
1365  // write log entry
1366  $log->write("ilObject::delete(), object not deleted, number of references: ".
1367  $this->countReferences().", obj_id: ".$this->getId().", type: ".
1368  $this->getType().", title: ".$this->getTitle());
1369  }
1370 
1371  // delete object_reference entry
1372  if ($this->referenced)
1373  {
1374  // delete entry in object_reference
1375  $q = "DELETE FROM object_reference ".
1376  "WHERE ref_id = ".$ilDB->quote($this->getRefId());
1377  $this->ilias->db->query($q);
1378 
1379  // write log entry
1380  $log->write("ilObject::delete(), reference deleted, ref_id: ".$this->getRefId().
1381  ", obj_id: ".$this->getId().", type: ".
1382  $this->getType().", title: ".$this->getTitle());
1383 
1384  // DELETE PERMISSION ENTRIES IN RBAC_PA
1385  // DONE: method overwritten in ilObjRole & ilObjUser.
1386  // this call only applies for objects in rbac (not usr,role,rolt)
1387  // TODO: Do this for role templates too
1388  $rbacadmin->revokePermission($this->getRefId(),0,false);
1389 
1390  // Remove desktop items
1392  }
1393 
1394  // remove conditions
1395  if ($this->referenced)
1396  {
1397  $ch =& new ilConditionHandler();
1398  $ch->delete($this->getRefId());
1399  unset($ch);
1400  }
1401 
1402  // BEGIN WebDAV: Delete WebDAV properties
1403  $q = "DELETE FROM dav_property ".
1404  "WHERE obj_id = ".$ilDB->quote($this->getId());
1405  $this->ilias->db->query($q);
1406  // END WebDAV: Delete WebDAV properties
1407 
1408  // BEGIN ChangeEvent: Delete read and write events
1409  $q = "DELETE FROM write_event ".
1410  "WHERE obj_id = ".$ilDB->quote($this->getId());
1411  $this->ilias->db->query($q);
1412  $q = "DELETE FROM read_event ".
1413  "WHERE obj_id = ".$ilDB->quote($this->getId());
1414  $this->ilias->db->query($q);
1415  // END ChangeEvent: Delete read and write events
1416 
1417  return $remove;
1418  }
1419 
1427  function initDefaultRoles()
1428  {
1429  return array();
1430  }
1431 
1441  function createRoleFolder()
1442  {
1443  global $rbacreview;
1444 
1445  // does a role folder already exists?
1446  // (this check is only 'to be sure' that no second role folder is created under one object.
1447  // the if-construct should never return true)
1448  if ($rolf_data = $rbacreview->getRoleFolderofObject($this->getRefId()))
1449  {
1450  $rfoldObj = $this->ilias->obj_factory->getInstanceByRefId($rolf_data["ref_id"]);
1451  }
1452  else
1453  {
1454  include_once ("./Services/AccessControl/classes/class.ilObjRoleFolder.php");
1455  $rfoldObj = new ilObjRoleFolder();
1456  $rfoldObj->setTitle($this->getId());
1457  $rfoldObj->setDescription(" (ref_id ".$this->getRefId().")");
1458  $rfoldObj->create();
1459  $rfoldObj->createReference();
1460  $rfoldObj->putInTree($this->getRefId());
1461  $rfoldObj->setPermissions($this->getRefId());
1462  }
1463 
1464  return $rfoldObj;
1465  }
1466 
1475  function _exists($a_id, $a_reference = false)
1476  {
1477  global $ilias, $ilDB;
1478 
1479  if ($a_reference)
1480  {
1481  $q = "SELECT * FROM object_data ".
1482  "LEFT JOIN object_reference ON object_reference.obj_id=object_data.obj_id ".
1483  "WHERE object_reference.ref_id= ".$ilDB->quote($a_id);
1484  }
1485  else
1486  {
1487  $q = "SELECT * FROM object_data WHERE obj_id=".$ilDB->quote($a_id);
1488  }
1489 
1490  $r = $ilDB->query($q);
1491 
1492  return $r->numRows() ? true : false;
1493  }
1494 
1507  function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
1508  {
1509  global $tree;
1510 
1511  $parent_id = (int) $tree->getParentId($a_node_id);
1512 
1513  if ($parent_id != 0)
1514  {
1515  $obj_data =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
1516  $obj_data->notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$parent_id,$a_params);
1517  }
1518 
1519  return true;
1520  }
1521 
1522  // toggle subscription interface
1523  function setRegisterMode($a_bool)
1524  {
1525  $this->register = (bool) $a_bool;
1526  }
1527 
1528  // check register status of current user
1529  // abstract method; overwrite in object type class
1530  function isUserRegistered($a_user_id = 0)
1531  {
1532  return false;
1533  }
1534 
1536  {
1537  return $this->register;
1538  }
1539 
1540 
1541  function getXMLZip()
1542  {
1543  return false;
1544  }
1545  function getHTMLDirectory()
1546  {
1547  return false;
1548  }
1549 
1553  static function _getObjectsByType($a_obj_type = "")
1554  {
1555  global $ilDB;
1556 
1557  $order = " ORDER BY title";
1558 
1559  // where clause
1560  if ($a_obj_type)
1561  {
1562  $where_clause = "WHERE type = ".
1563  $ilDB->quote($a_obj_type);
1564  }
1565 
1566  $q = "SELECT * FROM object_data ".$where_clause.$order;
1567  $r = $ilDB->query($q);
1568 
1569  $arr = array();
1570  if ($r->numRows() > 0)
1571  {
1572  while ($row = $r->fetchRow(DB_FETCHMODE_ASSOC))
1573  {
1574  $row["desc"] = $row["description"];
1575  $arr[$row["obj_id"]] = $row;
1576  }
1577  }
1578 
1579  return $arr;
1580  }
1581 
1590  public static function _prepareCloneSelection($a_ref_ids,$new_type)
1591  {
1592  global $ilDB,$lng;
1593 
1594  $query = "SELECT obj_data.title as obj_title,path_data.title as path_title,child FROM tree ".
1595  "JOIN object_reference as obj_ref ON child = obj_ref.ref_id ".
1596  "JOIN object_data as obj_data ON obj_ref.obj_id = obj_data.obj_id ".
1597  "JOIN object_reference as path_ref ON parent = path_ref.ref_id ".
1598  "JOIN object_data as path_data ON path_ref.obj_id = path_data.obj_id ".
1599  "WHERE child IN (".implode(',',ilUtil::quoteArray($a_ref_ids)).") ".
1600  "ORDER BY obj_data.title ";
1601  $res = $ilDB->query($query);
1602 
1603  $options[0] = $lng->txt('obj_'.$new_type.'_select');
1604  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
1605  {
1606  if(strlen($title = $row->obj_title) > 40)
1607  {
1608  $title = substr($title,0,40).'...';
1609  }
1610  if(strlen($path = $row->path_title) > 40)
1611  {
1612  $path = substr($path,0,40).'...';
1613  }
1614 
1615 
1616  $options[$row->child] = ($title.' ('.$lng->txt('path').': '.$path.')');
1617  }
1618  return $options ? $options : array();
1619  }
1620 
1630  public function cloneObject($a_target_id,$a_copy_id = 0)
1631  {
1632  global $objDefinition,$ilUser,$rbacadmin, $ilDB;
1633 
1634  $location = $objDefinition->getLocation($this->getType());
1635  $class_name = ('ilObj'.$objDefinition->getClassName($this->getType()));
1636 
1637  $title = $this->appendCopyInfo($a_target_id,$a_copy_id);
1638 
1639  // create instance
1640  include_once($location."/class.".$class_name.".php");
1641  $new_obj = new $class_name(0, false);
1642  $new_obj->setOwner($ilUser->getId());
1643  $new_obj->setTitle($title);
1644  $new_obj->setDescription($this->getDescription());
1645  $new_obj->setType($this->getType());
1646  // Choose upload mode to avoid creation of additional settings, db entries ...
1647  $new_obj->create(true);
1648  $new_obj->createReference();
1649  $new_obj->putInTree($a_target_id);
1650  $new_obj->setPermissions($a_target_id);
1651  $new_obj->initDefaultRoles();
1652 
1653  // copy local roles
1654  $rbacadmin->copyLocalRoles($this->getRefId(),$new_obj->getRefId());
1655 
1656  include_once('./Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
1657  ilAdvancedMDValues::_cloneValues($this->getId(),$new_obj->getId());
1658 
1659  // BEGIN WebDAV: Clone WebDAV properties
1660  $q = "INSERT INTO dav_property (obj_id,node_id,ns,name,value) ".
1661  "SELECT ".$ilDB->quote($new_obj->getId()).",node_id,ns,name,value ".
1662  "FROM dav_property ".
1663  "WHERE obj_id = ".$ilDB->quote($this->getId());
1664  $this->ilias->db->query($q);
1665  // END WebDAV: Clone WebDAV properties
1666 
1667  return $new_obj;
1668  }
1669 
1677  public function appendCopyInfo($a_target_id,$a_copy_id)
1678  {
1679  global $tree;
1680 
1681  include_once('Services/CopyWizard/classes/class.ilCopyWizardOptions.php');
1682  $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
1683  if(!$cp_options->isRootNode($this->getRefId()))
1684  {
1685  return $this->getTitle();
1686  }
1687  $nodes = $tree->getChilds($a_target_id);
1688 
1689  $title_unique = false;
1690  require_once 'Modules/File/classes/class.ilObjFileAccess.php';
1691  $numberOfCopy = 1;
1693  while(!$title_unique)
1694  {
1695  $found = 0;
1696  foreach($nodes as $node)
1697  {
1698  if(($title == $node['title']) and ($this->getType() == $node['type']))
1699  {
1700  $found++;
1701  }
1702  }
1703  if($found > 0)
1704  {
1705  $title = ilObjFileAccess::_appendNumberOfCopyToFilename($this->getTitle(), ++$numberOfCopy);
1706  }
1707  else
1708  {
1709  break;
1710  }
1711  }
1712  return $title;
1713  }
1714 
1723  public function cloneDependencies($a_target_id,$a_copy_id)
1724  {
1725  return true;
1726  }
1727 
1735  public function cloneMetaData($target_obj)
1736  {
1737  include_once "./Services/MetaData/classes/class.ilMD.php";
1738  $md = new ilMD($this->getId(),0,$this->getType());
1739  $md->cloneMD($target_obj->getId(),0,$target_obj->getType());
1740  return true;
1741  }
1742 
1743  public static function _getIcon($a_obj_id = "", $a_size = "big", $a_type = "",
1744  $a_offline = false)
1745  {
1746  global $ilSetting;
1747 
1748  if ($a_obj_id == "" && $a_type == "")
1749  {
1750  return "";
1751  }
1752 
1753  if ($a_type == "")
1754  {
1755  $a_type = ilObject::_lookupType($a_obj_id);
1756  }
1757 
1758  if ($a_size == "")
1759  {
1760  $a_size = "big";
1761  }
1762 
1763  if ($ilSetting->get("custom_icons") &&
1764  in_array($a_type, array("cat","grp","crs", "root")))
1765  {
1766  require_once("./Services/Container/classes/class.ilContainer.php");
1767  if (ilContainer::_lookupContainerSetting($a_obj_id, "icon_".$a_size))
1768  {
1769  $cont_dir = ilContainer::_getContainerDirectory($a_obj_id);
1770  $file_name = $cont_dir."/icon_".$a_size.".gif";
1771 
1772  if (is_file($file_name))
1773  {
1774  return $file_name;
1775  }
1776  }
1777  }
1778 
1779  switch($a_size)
1780  {
1781  case "small": $suff = ""; break;
1782  case "tiny": $suff = "_s"; break;
1783  default: $suff = "_b"; break;
1784  }
1785 
1786  if (!$a_offline)
1787  {
1788  return ilUtil::getImagePath("icon_".$a_type.$suff.".gif");
1789  }
1790  else
1791  {
1792  return "./images/icon_".$a_type.$suff.".gif";
1793  }
1794  }
1795 
1796 } // END class.ilObject
1797 ?>