00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00035 class ilObject
00036 {
00042 var $ilias;
00043
00049 var $lng;
00050
00056 var $id;
00057 var $ref_id;
00058 var $type;
00059 var $title;
00060 var $desc;
00061 var $owner;
00062 var $create_date;
00063 var $last_update;
00064 var $import_id;
00065 var $register = false;
00066
00072 var $referenced;
00073
00079 var $objectList;
00080
00085 var $max_title;
00086
00091 var $max_desc;
00092
00097 var $add_dots;
00098
00102 var $obj_data_record;
00103
00110 function ilObject($a_id = 0, $a_reference = true)
00111 {
00112 global $ilias, $lng, $ilBench;
00113
00114 $ilBench->start("Core", "ilObject_Constructor");
00115
00116 if (DEBUG)
00117 {
00118 echo "<br/><font color=\"red\">type(".$this->type.") id(".$a_id.") referenced(".$a_reference.")</font>";
00119 }
00120
00121 $this->ilias =& $ilias;
00122 $this->lng =& $lng;
00123
00124 $this->max_title = MAXLENGTH_OBJ_TITLE;
00125 $this->max_desc = MAXLENGTH_OBJ_DESC;
00126 $this->add_dots = true;
00127
00128 $this->referenced = $a_reference;
00129 $this->call_by_reference = $a_reference;
00130
00131 if ($a_id == 0)
00132 {
00133 $this->referenced = false;
00134 }
00135
00136 if ($this->referenced)
00137 {
00138 $this->ref_id = $a_id;
00139 }
00140 else
00141 {
00142 $this->id = $a_id;
00143 }
00144
00145 if ($a_id != 0)
00146 {
00147 $this->read();
00148 }
00149
00150 $ilBench->stop("Core", "ilObject_Constructor");
00151 }
00152
00156 function withReferences()
00157 {
00158
00159 return ($this->call_by_reference) ? true : $this->referenced;
00160 }
00161
00162
00168 function read($a_force_db = false)
00169 {
00170 global $objDefinition, $ilBench;
00171
00172 $ilBench->start("Core", "ilObject_read");
00173
00174 if (isset($this->obj_data_record) && !$a_force_db)
00175 {
00176 $obj = $this->obj_data_record;
00177 }
00178 else if ($this->referenced)
00179 {
00180
00181 if (!isset($this->ref_id))
00182 {
00183 $message = "ilObject::read(): No ref_id given!";
00184 $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
00185 }
00186
00187
00188 $ilBench->start("Core", "ilObject_read_readData");
00189
00190
00191
00192
00193 $q = "SELECT * FROM object_data, object_reference WHERE object_data.obj_id=object_reference.obj_id ".
00194 "AND object_reference.ref_id='".$this->ref_id."'";
00195 $object_set = $this->ilias->db->query($q);
00196 $ilBench->stop("Core", "ilObject_read_readData");
00197
00198
00199 if ($object_set->numRows() == 0)
00200 {
00201 $message = "ilObject::read(): Object with ref_id ".$this->ref_id." not found!";
00202 $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
00203 }
00204
00205 $obj = $object_set->fetchRow(DB_FETCHMODE_ASSOC);
00206 }
00207 else
00208 {
00209
00210 if (!isset($this->id))
00211 {
00212 $message = "ilObject::read(): No obj_id given!";
00213 $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
00214 }
00215
00216
00217 $q = "SELECT * FROM object_data ".
00218 "WHERE obj_id = '".$this->id."'";
00219 $object_set = $this->ilias->db->query($q);
00220
00221
00222 if ($object_set->numRows() == 0)
00223 {
00224 $message = "ilObject::read(): Object with obj_id: ".$this->id." not found!";
00225 $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
00226 }
00227
00228 $obj = $object_set->fetchRow(DB_FETCHMODE_ASSOC);
00229 }
00230
00231 $this->id = $obj["obj_id"];
00232 $this->type = $obj["type"];
00233 $this->title = $obj["title"];
00234 $this->desc = $obj["description"];
00235 $this->owner = $obj["owner"];
00236 $this->create_date = $obj["create_date"];
00237 $this->last_update = $obj["last_update"];
00238 $this->import_id = $obj["import_id"];
00239
00240
00241 $ilBench->start("Core", "ilObject_Constructor_getTranslation");
00242 $translation_type = $objDefinition->getTranslationType($this->type);
00243
00244 if ($translation_type == "sys")
00245 {
00246 $this->title = $this->lng->txt("obj_".$this->type);
00247 $this->desc = $this->lng->txt("obj_".$this->type."_desc");
00248 }
00249 elseif ($translation_type == "db")
00250 {
00251 $q = "SELECT title,description FROM object_translation ".
00252 "WHERE obj_id = ".$this->id." ".
00253 "AND lang_code = '".$this->ilias->account->getPref("language")."' ".
00254 "AND NOT lang_default = 1";
00255 $r = $this->ilias->db->query($q);
00256 $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
00257
00258 if ($row)
00259 {
00260 $this->title = $row->title;
00261 $this->desc = $row->description;
00262 }
00263 }
00264
00265 $ilBench->stop("Core", "ilObject_Constructor_getTranslation");
00266
00267 $ilBench->stop("Core", "ilObject_read");
00268 }
00269
00275 function getId()
00276 {
00277 return $this->id;
00278 }
00279
00285 function setId($a_id)
00286 {
00287 $this->id = $a_id;
00288 }
00289
00295 function setRefId($a_id)
00296 {
00297 $this->ref_id = $a_id;
00298 $this->referenced = true;
00299 }
00300
00306 function getRefId()
00307 {
00308 return $this->ref_id;
00309 }
00310
00316 function getType()
00317 {
00318 return $this->type;
00319 }
00320
00326 function setType($a_type)
00327 {
00328 $this->type = $a_type;
00329 }
00330
00336 function getTitle()
00337 {
00338 return $this->title;
00339 }
00340
00347 function setTitle($a_title)
00348 {
00349 if ($a_title == "")
00350 {
00351 $a_title = "NO TITLE";
00352 }
00353
00354 $this->title = ilUtil::shortenText($a_title, $this->max_title, $this->add_dots);
00355 }
00356
00363 function getDescription()
00364 {
00365 return $this->desc;
00366 }
00367
00374 function setDescription($a_desc)
00375 {
00376 $this->desc = ilUtil::shortenText($a_desc, $this->max_desc, $this->add_dots);
00377 }
00378
00385 function getImportId()
00386 {
00387 return $this->import_id;
00388 }
00389
00396 function setImportId($a_import_id)
00397 {
00398 $this->import_id = $a_import_id;
00399 }
00400
00407 function getOwner()
00408 {
00409 return $this->owner;
00410 }
00411
00412
00413
00414
00415
00416
00417
00418 function getOwnerName()
00419 {
00420 return ilObject::_lookupOwnerName($this->getOwner());
00421 }
00422
00426 function _lookupOwnerName($a_owner_id)
00427 {
00428 global $lng;
00429
00430 if ($a_owner_id != -1)
00431 {
00432 if (ilObject::_exists($a_owner_id))
00433 {
00434 $owner = new ilObjUser($a_owner_id);
00435 }
00436 }
00437
00438 if (is_object($owner))
00439 {
00440 $own_name = $owner->getFullname();
00441 }
00442 else
00443 {
00444 $own_name = $lng->txt("unknown");
00445 }
00446
00447 return $own_name;
00448 }
00449
00456 function setOwner($a_owner)
00457 {
00458 $this->owner = $a_owner;
00459 }
00460
00466 function getCreateDate()
00467 {
00468 return $this->create_date;
00469 }
00470
00476 function getLastUpdateDate()
00477 {
00478 return $this->last_update;
00479 }
00480
00489 function setObjDataRecord($a_record)
00490 {
00491 $this->obj_data_record = $a_record;
00492 }
00493
00502 function create()
00503 {
00504 global $ilDB, $log,$ilUser;
00505
00506 if (!isset($this->type))
00507 {
00508 $message = get_class($this)."::create(): No object type given!";
00509 $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
00510 }
00511
00512
00513 $log->write("ilObject::create(), start");
00514
00515 $this->title = ilUtil::shortenText($this->getTitle(), $this->max_title, $this->add_dots);
00516 $this->desc = ilUtil::shortenText($this->getDescription(), $this->max_desc, $this->add_dots);
00517
00518 $q = "INSERT INTO object_data ".
00519 "(type,title,description,owner,create_date,last_update,import_id) ".
00520 "VALUES ".
00521 "('".$this->type."',".$ilDB->quote($this->getTitle()).",'".ilUtil::prepareDBString($this->getDescription())."',".
00522 "'".$ilUser->getId()."',now(),now(),'".
00523 $this->getImportId()."')";
00524
00525 $ilDB->query($q);
00526
00527 $this->id = $ilDB->getLastInsertId();
00528
00529
00530
00531
00532 $q = "SELECT last_update, create_date FROM object_data".
00533 " WHERE obj_id = '".$this->id."'";
00534 $obj_set = $this->ilias->db->query($q);
00535 $obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC);
00536 $this->last_update = $obj_rec["last_update"];
00537 $this->create_date = $obj_rec["create_date"];
00538
00539
00540 $this->setOwner($ilUser->getId());
00541
00542
00543 $log->write("ilObject::create(), finished, obj_id: ".$this->id.", type: ".
00544 $this->type.", title: ".$this->getTitle());
00545
00546 return $this->id;
00547 }
00548
00555 function update()
00556 {
00557
00558 $q = "UPDATE object_data ".
00559 "SET ".
00560 "title = '".ilUtil::prepareDBString($this->getTitle())."',".
00561 "description = '".ilUtil::prepareDBString($this->getDescription())."', ".
00562 "import_id = '".$this->getImportId()."', ".
00563 "last_update = now() ".
00564 "WHERE obj_id = '".$this->getId()."'";
00565 $this->ilias->db->query($q);
00566
00567
00568
00569
00570 $q = "SELECT last_update FROM object_data".
00571 " WHERE obj_id = '".$this->getId()."'";
00572 $obj_set = $this->ilias->db->query($q);
00573 $obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC);
00574 $this->last_update = $obj_rec["last_update"];
00575
00576 return true;
00577 }
00578
00585 function updateOwner()
00586 {
00587 $q = "UPDATE object_data ".
00588 "SET ".
00589 "owner = '".$this->getOwner()."', ".
00590 "last_update = now() ".
00591 "WHERE obj_id = '".$this->getId()."'";
00592 $this->ilias->db->query($q);
00593
00594 $q = "SELECT last_update FROM object_data".
00595 " WHERE obj_id = '".$this->getId()."'";
00596 $obj_set = $this->ilias->db->query($q);
00597 $obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC);
00598 $this->last_update = $obj_rec["last_update"];
00599
00600 return true;
00601 }
00602
00610 function _getIdForImportId($a_import_id)
00611 {
00612 global $ilDB;
00613
00614 $q = "SELECT * FROM object_data WHERE import_id = '".$a_import_id."'".
00615 " ORDER BY create_date DESC LIMIT 1";
00616 $obj_set = $ilDB->query($q);
00617
00618 if ($obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC))
00619 {
00620 return $obj_rec["obj_id"];
00621 }
00622 else
00623 {
00624 return 0;
00625 }
00626 }
00627
00633 function _getAllReferences($a_id)
00634 {
00635 global $ilDB;
00636
00637 $q = "SELECT * FROM object_reference WHERE obj_id = '".$a_id."'";
00638 $obj_set = $ilDB->query($q);
00639 $ref = array();
00640
00641 while ($obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC))
00642 {
00643 $ref[$obj_rec["ref_id"]] = $obj_rec["ref_id"];
00644 }
00645
00646 return $ref;
00647 }
00648
00654 function _lookupTitle($a_id)
00655 {
00656 global $ilDB;
00657
00658 $q = "SELECT title FROM object_data WHERE obj_id = '".$a_id."'";
00659 $obj_set = $ilDB->query($q);
00660 $obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC);
00661
00662 return $obj_rec["title"];
00663 }
00664
00665 function _lookupObjId($a_id)
00666 {
00667 global $ilDB;
00668
00669 $query = "SELECT obj_id FROM object_reference ".
00670 "WHERE ref_id = '".$a_id."'";
00671
00672 $res = $ilDB->query($query);
00673 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00674 {
00675 return $row->obj_id;
00676 }
00677 return 0;
00678 }
00679
00687 function _writeTitle($a_obj_id, $a_title)
00688 {
00689 global $ilDB;
00690
00691 $q = "UPDATE object_data ".
00692 "SET ".
00693 "title = ".$ilDB->quote($a_title).",".
00694 "last_update = now() ".
00695 "WHERE obj_id = ".$ilDB->quote($a_obj_id);
00696
00697 $ilDB->query($q);
00698 }
00699
00707 function _writeDescription($a_obj_id, $a_desc)
00708 {
00709 global $ilDB;
00710
00711 $q = "UPDATE object_data ".
00712 "SET ".
00713 "description = ".$ilDB->quote($a_desc).",".
00714 "last_update = now() ".
00715 "WHERE obj_id = ".$ilDB->quote($a_obj_id);
00716
00717 $ilDB->query($q);
00718 }
00719
00725 function _lookupType($a_id,$a_reference = false)
00726 {
00727 global $ilDB;
00728
00729 if ($a_reference === true)
00730 {
00731 $q = "SELECT type FROM object_reference as obr, object_data as obd ".
00732 "WHERE obr.ref_id = '".$a_id."' ".
00733 "AND obr.obj_id = obd.obj_id ";
00734
00735 #$q = "SELECT type FROM object_data as obj ".
00736 # "LEFT JOIN object_reference as ref ON ref.obj_id=obj.obj_id ".
00737 # "WHERE ref.ref_id = '".$a_id."'";
00738 }
00739 else
00740 {
00741 $q = "SELECT type FROM object_data WHERE obj_id = '".$a_id."'";
00742 }
00743
00744 $obj_set = $ilDB->query($q);
00745 $obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC);
00746
00747 return $obj_rec["type"];
00748 }
00749
00753 function _isInTrash($a_ref_id)
00754 {
00755 global $tree;
00756
00757 return $tree->isSaved($a_ref_id);
00758 }
00759
00763 function _hasUntrashedReference($a_obj_id)
00764 {
00765 $ref_ids = ilObject::_getAllReferences($a_obj_id);
00766 foreach($ref_ids as $ref_id)
00767 {
00768 if(!ilObject::_isInTrash($ref_id))
00769 {
00770 return true;
00771 }
00772 }
00773
00774 return false;
00775 }
00776
00782 function _lookupObjectId($a_ref_id)
00783 {
00784 global $ilDB;
00785
00786 $q = "SELECT obj_id FROM object_reference WHERE ref_id = '".$a_ref_id."'";
00787 $obj_set = $ilDB->query($q);
00788 $obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC);
00789
00790 return $obj_rec["obj_id"];
00791 }
00792
00803 function _getObjectsDataForType($a_type, $a_omit_trash = false)
00804 {
00805 global $ilDB;
00806
00807 $q = "SELECT * FROM object_data WHERE type = ".$ilDB->quote($a_type);
00808 $obj_set = $ilDB->query($q);
00809
00810 $objects = array();
00811 while ($obj_rec = $obj_set->fetchRow(DB_FETCHMODE_ASSOC))
00812 {
00813 if ((!$a_omit_trash) || ilObject::_hasUntrashedReference($obj_rec["obj_id"]))
00814 {
00815 $objects[$obj_rec["title"].".".$obj_rec["obj_id"]] = array("id" => $obj_rec["obj_id"],
00816 "type" => $obj_rec["type"], "title" => $obj_rec["title"],
00817 "description" => $obj_rec["description"]);
00818 }
00819 }
00820 ksort($objects);
00821 return $objects;
00822 }
00823
00829 function putInTree($a_parent_ref)
00830 {
00831 global $tree, $log;
00832
00833 $tree->insertNode($this->getRefId(), $a_parent_ref);
00834
00835
00836 $log->write("ilObject::putInTree(), parent_ref: $a_parent_ref, ref_id: ".
00837 $this->getRefId().", obj_id: ".$this->getId().", type: ".
00838 $this->getType().", title: ".$this->getTitle());
00839
00840 }
00841
00848 function setPermissions($a_parent_ref)
00849 {
00850 global $rbacadmin, $rbacreview;
00851
00852 $parentRoles = $rbacreview->getParentRoleIds($a_parent_ref);
00853
00854 foreach ($parentRoles as $parRol)
00855 {
00856 $ops = $rbacreview->getOperationsOfRole($parRol["obj_id"], $this->getType(), $parRol["parent"]);
00857 $rbacadmin->grantPermission($parRol["obj_id"], $ops, $this->getRefId());
00858 }
00859 }
00860
00867 function createReference()
00868 {
00869 global $ilDB;
00870
00871 if (!isset($this->id))
00872 {
00873 $message = "ilObject::createNewReference(): No obj_id given!";
00874 $this->raiseError($message,$this->ilias->error_obj->WARNING);
00875 }
00876
00877 $q = "INSERT INTO object_reference ".
00878 "(obj_id) VALUES ('".$this->id."')";
00879 $this->ilias->db->query($q);
00880
00881 $this->ref_id = $ilDB->getLastInsertId();
00882
00883 return $this->ref_id;
00884 }
00885
00886
00893 function countReferences()
00894 {
00895 if (!isset($this->id))
00896 {
00897 $message = "ilObject::countReferences(): No obj_id given!";
00898 $this->ilias->raiseError($message,$this->ilias->error_obj->WARNING);
00899 }
00900
00901 $q = "SELECT COUNT(ref_id) AS num FROM object_reference ".
00902 "WHERE obj_id = '".$this->id."'";
00903 $row = $this->ilias->db->getRow($q);
00904
00905 return $row->num;
00906 }
00907
00908
00919 function ilClone($a_parent_ref)
00920 {
00921 global $log;
00922
00923 $new_obj = new ilObject();
00924 $new_obj->setTitle($this->getTitle());
00925 $new_obj->setType($this->getType());
00926 $new_obj->setDescription($this->getDescription());
00927 $new_obj->create();
00928 $new_ref_id = $new_obj->createReference();
00929 $new_obj->putInTree($a_parent_ref);
00930 $new_obj->setPermissions($a_parent_ref);
00931
00932 unset($new_obj);
00933
00934
00935 $log->write("ilObject::ilClone(), ref_id: ".$this->getRefId().",obj_id: ".$this->getId().", type: ".
00936 $this->getType().", title: ".$this->getTitle().
00937 ", new ref_id: ".$new_obj->getRefId().", new obj_id:".$new_obj->getId());
00938
00939
00940 return $new_ref_id;
00941 }
00942
00943
00953 function delete()
00954 {
00955 global $rbacadmin, $log;
00956
00957 $remove = false;
00958
00959
00960 if ((!$this->referenced) || ($this->countReferences() == 1))
00961 {
00962
00963 $q = "DELETE FROM object_data ".
00964 "WHERE obj_id = '".$this->getId()."'";
00965 $this->ilias->db->query($q);
00966
00967
00968 $log->write("ilObject::delete(), deleted object, obj_id: ".$this->getId().", type: ".
00969 $this->getType().", title: ".$this->getTitle());
00970
00971 $remove = true;
00972 }
00973 else
00974 {
00975
00976 $log->write("ilObject::delete(), object not deleted, number of references: ".
00977 $this->countReferences().", obj_id: ".$this->getId().", type: ".
00978 $this->getType().", title: ".$this->getTitle());
00979 }
00980
00981
00982 if ($this->referenced)
00983 {
00984
00985 $q = "DELETE FROM object_reference ".
00986 "WHERE ref_id = '".$this->getRefId()."'";
00987 $this->ilias->db->query($q);
00988
00989
00990 $log->write("ilObject::delete(), reference deleted, ref_id: ".$this->getRefId().
00991 ", obj_id: ".$this->getId().", type: ".
00992 $this->getType().", title: ".$this->getTitle());
00993
00994
00995
00996
00997
00998 $rbacadmin->revokePermission($this->getRefId());
00999 }
01000
01001
01002 if ($this->referenced)
01003 {
01004 $ch =& new ilConditionHandler();
01005 $ch->delete($this->getRefId());
01006 unset($ch);
01007 }
01008
01009 return $remove;
01010 }
01011
01019 function initDefaultRoles()
01020 {
01021 return array();
01022 }
01023
01033 function createRoleFolder()
01034 {
01035 global $rbacreview;
01036
01037
01038
01039
01040 if ($rolf_data = $rbacreview->getRoleFolderofObject($this->getRefId()))
01041 {
01042 $rfoldObj = $this->ilias->obj_factory->getInstanceByRefId($rolf_data["ref_id"]);
01043 }
01044 else
01045 {
01046 include_once ("classes/class.ilObjRoleFolder.php");
01047 $rfoldObj = new ilObjRoleFolder();
01048 $rfoldObj->setTitle($this->getId());
01049 $rfoldObj->setDescription(" (ref_id ".$this->getRefId().")");
01050 $rfoldObj->create();
01051 $rfoldObj->createReference();
01052 $rfoldObj->putInTree($this->getRefId());
01053 $rfoldObj->setPermissions($this->getRefId());
01054 }
01055
01056 return $rfoldObj;
01057 }
01058
01067 function _exists($a_id, $a_reference = false)
01068 {
01069 global $ilias;
01070
01071 if ($a_reference)
01072 {
01073 $q = "SELECT * FROM object_data ".
01074 "LEFT JOIN object_reference ON object_reference.obj_id=object_data.obj_id ".
01075 "WHERE object_reference.ref_id='".$a_id."'";
01076 }
01077 else
01078 {
01079 $q = "SELECT * FROM object_data WHERE obj_id='".$a_id."'";
01080 }
01081
01082 $r = $ilias->db->query($q);
01083
01084 return $r->numRows() ? true : false;
01085 }
01086
01099 function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
01100 {
01101 global $tree;
01102
01103 $parent_id = (int) $tree->getParentId($a_node_id);
01104
01105 if ($parent_id != 0)
01106 {
01107 $obj_data =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
01108 $obj_data->notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$parent_id,$a_params);
01109 }
01110
01111 return true;
01112 }
01113
01114
01115 function setRegisterMode($a_bool)
01116 {
01117 $this->register = (bool) $a_bool;
01118 }
01119
01120
01121
01122 function isUserRegistered($a_user_id = 0)
01123 {
01124 return false;
01125 }
01126
01127 function requireRegistration()
01128 {
01129 return $this->register;
01130 }
01131
01132
01133 function getXMLZip()
01134 {
01135 return false;
01136 }
01137 function getHTMLDirectory()
01138 {
01139 return false;
01140 }
01141
01142
01143 function _checkCondition($a_obj_id,$a_operator,$a_value)
01144 {
01145 switch($a_operator)
01146 {
01147 default:
01148 return true;
01149 }
01150 }
01151
01152
01153 }
01154 ?>