ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
4
21{
28 public function ObjectIdExists($a_obj_id)
29 {
30 global $DIC;
31
32 $ilDB = $DIC->database();
33
34 $query = "SELECT * FROM object_data " .
35 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer');
36
37 $res = $ilDB->query($query);
38
39 return $res->numRows() ? true : false;
40 }
41
49 public function getObjectsForOwner($object_type, $owner_id)
50 {
51 global $DIC;
52
53 $ilDB = $DIC->database();
54
55 $query = "SELECT * FROM object_data,object_reference " .
56 "WHERE object_reference.obj_id = object_data.obj_id " .
57 " AND object_data.type=" . $ilDB->quote($object_type, 'text') .
58 " AND object_data.owner = " . $ilDB->quote($owner_id, 'integer');
59 $res = $ilDB->query($query);
60
61 $obj_ids = array();
62 while ($object_rec = $res->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
63 $obj_ids [] = $object_rec["obj_id"];
64 }
65
66 return $obj_ids;
67 }
68
77 public static function getInstanceByObjId($a_obj_id, $stop_on_error = true)
78 {
79 global $DIC;
80
81 $objDefinition = $DIC["objDefinition"];
82 $ilDB = $DIC->database();
83
84 // check object id
85 if (!isset($a_obj_id)) {
86 $message = "ilObjectFactory::getInstanceByObjId(): No obj_id given!";
87 if ($stop_on_error === true) {
89 }
90
91 return false;
92 }
93
94 // read object data
95 $q = "SELECT * FROM object_data " .
96 "WHERE obj_id = " . $ilDB->quote($a_obj_id, 'integer');
97 $object_set = $ilDB->query($q);
98 // check number of records
99 if ($object_set->numRows() == 0) {
100 $message = "ilObjectFactory::getInstanceByObjId(): Object with obj_id: " . $a_obj_id . " not found!";
101 if ($stop_on_error === true) {
103 }
104 return false;
105 }
106
107 $object_rec = $object_set->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
108 $class_name = "ilObj" . $objDefinition->getClassName($object_rec["type"]);
109
110 // check class
111 if ($class_name == "ilObj") {
112 $message = "ilObjectFactory::getInstanceByObjId(): Not able to determine object " .
113 "class for type" . $object_rec["type"] . ".";
114 if ($stop_on_error === true) {
116 }
117 return false;
118 }
119
120 (new self())->includeClassIfNotExists($class_name,$object_rec["type"],$objDefinition);
121
122 // create instance
123 $obj = new $class_name(0, false); // this avoids reading of data
124 $obj->setId($a_obj_id);
125 $obj->read();
126
127 return $obj;
128 }
129
130
139 public static function getInstanceByRefId($a_ref_id, $stop_on_error = true)
140 {
141 global $DIC;
142
143 $objDefinition = $DIC["objDefinition"];
144 $ilDB = $DIC->database();
145
146 // check reference id
147 if (!isset($a_ref_id)) {
148 if ($stop_on_error === true) {
149 $message = "ilObjectFactory::getInstanceByRefId(): No ref_id given!";
151 }
152
153 return false;
154 }
155
156 // read object data
157
158 $query = "SELECT * FROM object_data,object_reference " .
159 "WHERE object_reference.obj_id = object_data.obj_id " .
160 "AND object_reference.ref_id = " . $ilDB->quote($a_ref_id, 'integer');
161 $object_set = $ilDB->query($query);
162
163 // check number of records
164 if ($object_set->numRows() == 0) {
165 if ($stop_on_error === true) {
166 $message = "ilObjectFactory::getInstanceByRefId(): Object with ref_id " . $a_ref_id . " not found!";
168 }
169
170 return false;
171 }
172
173 $object_rec = $object_set->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
174 $class_name = "ilObj" . $objDefinition->getClassName($object_rec["type"]);
175
176 // check class
177 if ($class_name == "ilObj") {
178 if ($stop_on_error === true) {
179 $message = "ilObjectFactory::getInstanceByRefId(): Not able to determine object " .
180 "class for type" . $object_rec["type"] . ".";
182 }
183
184 return false;
185 }
186
187 (new self())->includeClassIfNotExists($class_name,$object_rec["type"],$objDefinition);
188
189 // create instance
190 $obj = new $class_name(0, false); // this avoids reading of data
191 $obj->setId($object_rec["obj_id"]);
192 $obj->setRefId($a_ref_id);
193 $obj->read();
194 return $obj;
195 }
196
207 public static function getTypeByRefId($a_ref_id, $stop_on_error = true)
208 {
209 global $DIC;
210
211 $ilDB = $DIC->database();
212
213 // check reference id
214 if (!isset($a_ref_id)) {
215 if ($stop_on_error === true) {
216 $message = "ilObjectFactory::getTypeByRefId(): No ref_id given!";
218 }
219
220 return false;
221 }
222
223 // read object data
224 $q = "SELECT * FROM object_data " .
225 "LEFT JOIN object_reference ON object_data.obj_id=object_reference.obj_id " .
226 "WHERE object_reference.ref_id=" . $ilDB->quote($a_ref_id, 'integer');
227 $object_set = $ilDB->query($q);
228
229 if ($object_set->numRows() == 0) {
230 if ($stop_on_error === true) {
231 $message = "ilObjectFactory::getTypeByRefId(): Object with ref_id " . $a_ref_id . " not found!";
233 }
234
235 return false;
236 }
237
238 $object_rec = $object_set->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
239 return $object_rec["type"];
240 }
241
247 public static function getClassByType($a_obj_type)
248 {
249 global $DIC;
250
251 $objDefinition = $DIC["objDefinition"];
252
253 $class_name = "ilObj" . $objDefinition->getClassName($a_obj_type);
254
255 (new self())->includeClassIfNotExists($class_name,$a_obj_type,$objDefinition);
256
257 // create instance
258 return $class_name;
259 }
260
271 protected function includeClassIfNotExists(string $class_name,string $a_obj_type, ilObjectDefinition $objDefinition){
272 if(!class_exists($class_name)){
273 $location = $objDefinition->getLocation($a_obj_type);
274 include_once($location."/class.".$class_name.".php");
275 }
276 }
277}
$location
Definition: buildRTE.php:44
An exception for terminatinating execution or to throw for unit testing.
parses the objects.xml it handles the xml-description of all ilias objects
getLocation($a_obj_name)
get location by type
Class ilObjectFactory.
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 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.
getObjectsForOwner($object_type, $owner_id)
returns all objects of an owner, filtered by type, objects are not deleted!
static getTypeByRefId($a_ref_id, $stop_on_error=true)
get object type by reference id
static getClassByType($a_obj_type)
Get class by type.
$query
foreach($_POST as $key=> $value) $res
global $ilDB
$message
Definition: xapiexit.php:14
$DIC
Definition: xapitoken.php:46