ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilObjectFactory.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
16{
22 public function ObjectIdExists($a_obj_id)
23 {
24 global $DIC;
25
26 $ilDB = $DIC->database();
27
28 $query = "SELECT * FROM object_data " .
29 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer');
30
31 $res = $ilDB->query($query);
32
33 return $res->numRows() ? true : false;
34 }
35
42 public static function getObjectsForOwner($object_type, $owner_id)
43 {
44 global $DIC;
45
46 $ilDB = $DIC->database();
47
48 $query = "SELECT * FROM object_data,object_reference " .
49 "WHERE object_reference.obj_id = object_data.obj_id " .
50 " AND object_data.type=" . $ilDB->quote($object_type, 'text') .
51 " AND object_data.owner = " . $ilDB->quote($owner_id, 'integer');
52 $res = $ilDB->query($query);
53
54 $obj_ids = array();
55 while ($object_rec = $res->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
56 $obj_ids [] = $object_rec["obj_id"];
57 }
58
59 return $obj_ids;
60 }
61
70 public static function getInstanceByObjId($a_obj_id, $stop_on_error = true)
71 {
72 global $DIC;
73
74 $objDefinition = $DIC["objDefinition"];
75 $ilDB = $DIC->database();
76
77 // check object id
78 if (!isset($a_obj_id)) {
79 $message = "ilObjectFactory::getInstanceByObjId(): No obj_id given!";
80 if ($stop_on_error === true) {
82 }
83
84 return false;
85 }
86
87 // read object data
88 $q = "SELECT * FROM object_data " .
89 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer');
90 $object_set = $ilDB->query($q);
91 // check number of records
92 if ($object_set->numRows() == 0) {
93 $message = "ilObjectFactory::getInstanceByObjId(): Object with obj_id: " . $a_obj_id . " not found!";
94 if ($stop_on_error === true) {
96 }
97 return false;
98 }
99
100 $object_rec = $object_set->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
101 $class_name = "ilObj" . $objDefinition->getClassName($object_rec["type"]);
102
103 // check class
104 if ($class_name == "ilObj") {
105 $message = "ilObjectFactory::getInstanceByObjId(): Not able to determine object " .
106 "class for type" . $object_rec["type"] . ".";
107 if ($stop_on_error === true) {
109 }
110 return false;
111 }
112
113 (new self())->includeClassIfNotExists($class_name, $object_rec["type"], $objDefinition);
114
115 // create instance
116 $obj = new $class_name(0, false); // this avoids reading of data
117 $obj->setId($a_obj_id);
118 $obj->read();
119
120 return $obj;
121 }
122
131 public static function getInstanceByRefId($a_ref_id, $stop_on_error = true)
132 {
133 global $DIC;
134
135 $objDefinition = $DIC["objDefinition"];
136 $ilDB = $DIC->database();
137
138 // check reference id
139 if (!isset($a_ref_id)) {
140 if ($stop_on_error === true) {
141 $message = "ilObjectFactory::getInstanceByRefId(): No ref_id given!";
143 }
144
145 return false;
146 }
147
148 // read object data
149
150 $query = "SELECT * FROM object_data,object_reference " .
151 "WHERE object_reference.obj_id = object_data.obj_id " .
152 "AND object_reference.ref_id = " . $ilDB->quote($a_ref_id, 'integer');
153 $object_set = $ilDB->query($query);
154
155 // check number of records
156 if ($object_set->numRows() == 0) {
157 if ($stop_on_error === true) {
158 $message = "ilObjectFactory::getInstanceByRefId(): Object with ref_id " . $a_ref_id . " not found!";
160 }
161
162 return false;
163 }
164
165 $object_rec = $object_set->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
166 $class_name = "ilObj" . $objDefinition->getClassName($object_rec["type"]);
167
168 // check class
169 if ($class_name == "ilObj") {
170 if ($stop_on_error === true) {
171 $message = "ilObjectFactory::getInstanceByRefId(): Not able to determine object " .
172 "class for type" . $object_rec["type"] . ".";
174 }
175
176 return false;
177 }
178
179 (new self())->includeClassIfNotExists($class_name, $object_rec["type"], $objDefinition);
180
181 // create instance
182 $obj = new $class_name(0, false); // this avoids reading of data
183 $obj->setId($object_rec["obj_id"]);
184 $obj->setRefId($a_ref_id);
185 $obj->read();
186 return $obj;
187 }
188
198 public static function getTypeByRefId($a_ref_id, $stop_on_error = true)
199 {
200 global $DIC;
201
202 $ilDB = $DIC->database();
203
204 // check reference id
205 if (!isset($a_ref_id)) {
206 if ($stop_on_error === true) {
207 $message = "ilObjectFactory::getTypeByRefId(): No ref_id given!";
209 }
210
211 return false;
212 }
213
214 // read object data
215 $q = "SELECT * FROM object_data " .
216 "LEFT JOIN object_reference ON object_data.obj_id=object_reference.obj_id " .
217 "WHERE object_reference.ref_id=" . $ilDB->quote($a_ref_id, 'integer');
218 $object_set = $ilDB->query($q);
219
220 if ($object_set->numRows() == 0) {
221 if ($stop_on_error === true) {
222 $message = "ilObjectFactory::getTypeByRefId(): Object with ref_id " . $a_ref_id . " not found!";
224 }
225
226 return false;
227 }
228
229 $object_rec = $object_set->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
230 return $object_rec["type"];
231 }
232
237 public static function getClassByType($a_obj_type)
238 {
239 global $DIC;
240
241 $objDefinition = $DIC["objDefinition"];
242
243 $class_name = "ilObj" . $objDefinition->getClassName($a_obj_type);
244
245 (new self())->includeClassIfNotExists($class_name, $a_obj_type, $objDefinition);
246
247 // create instance
248 return $class_name;
249 }
250
259 protected function includeClassIfNotExists(
260 string $class_name,
261 string $a_obj_type,
262 ilObjectDefinition $objDefinition
263 ) {
264 if (!class_exists($class_name)) {
265 $location = $objDefinition->getLocation($a_obj_type);
266 include_once($location . "/class." . $class_name . ".php");
267 }
268 }
269}
$location
Definition: buildRTE.php:44
An exception for terminatinating execution or to throw for unit testing.
return true
Flag indicating whether or not HTTP headers will be sent when outputting captcha image/audio.
parses the objects.xml it handles the xml-description of all ilias objects
getLocation($a_obj_name)
get location by type
Class ilObjectFactory This class offers methods to get instances of the type-specific object classes ...
includeClassIfNotExists(string $class_name, string $a_obj_type, ilObjectDefinition $objDefinition)
Ensures a class is properly included.
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
static getObjectsForOwner($object_type, $owner_id)
returns all objects of an owner, filtered by type, objects are not deleted!
static getInstanceByRefId($a_ref_id, $stop_on_error=true)
get an instance of an Ilias object by reference id
ObjectIdExists($a_obj_id)
check if obj_id exists.
static getTypeByRefId($a_ref_id, $stop_on_error=true)
get object type by reference id
static getClassByType($a_obj_type)
Get class by type.
global $DIC
Definition: goto.php:24
$query
foreach($_POST as $key=> $value) $res
global $ilDB
$message
Definition: xapiexit.php:14