ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilObjectFactory.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
33{
37 public function ObjectIdExists(int $obj_id): bool
38 {
39 global $DIC;
40 $ilDB = $DIC->database();
41
42 $sql =
43 "SELECT obj_id, type, title, description, owner, create_date, last_update, import_id, offline" . PHP_EOL
44 . "FROM object_data" . PHP_EOL
45 . "WHERE obj_id = " . $ilDB->quote($obj_id, 'integer') . PHP_EOL
46 ;
47
48 $result = $ilDB->query($sql);
49
50 return (bool) $result->numRows();
51 }
52
56 public static function getObjectsForOwner(string $object_type, int $owner_id): array
57 {
58 global $DIC;
59 $ilDB = $DIC->database();
60
61 $sql =
62 "SELECT object_data.obj_id" . PHP_EOL
63 . "FROM object_data, object_reference" . PHP_EOL
64 . "WHERE object_reference.obj_id = object_data.obj_id" . PHP_EOL
65 . "AND object_data.type = " . $ilDB->quote($object_type, 'text') . PHP_EOL
66 . "AND object_data.owner = " . $ilDB->quote($owner_id, 'integer') . PHP_EOL
67 ;
68
69 $result = $ilDB->query($sql);
70
71 $obj_ids = [];
72 while ($row = $result->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
73 $obj_ids [] = $row["obj_id"];
74 }
75
76 return $obj_ids;
77 }
78
84 public static function getInstanceByObjId(?int $obj_id, bool $stop_on_error = true): ?ilObject
85 {
86 global $DIC;
87 $objDefinition = $DIC["objDefinition"];
88 $ilDB = $DIC->database();
89
90 // check object id
91 if (!isset($obj_id)) {
92 $message = "ilObjectFactory::getInstanceByObjId(): No obj_id given!";
93 if ($stop_on_error === true) {
95 }
96 return null;
97 }
98
99 // read object data
100 $sql =
101 "SELECT obj_id, type, title, description, owner, create_date, last_update, import_id, offline" . PHP_EOL
102 . "FROM object_data" . PHP_EOL
103 . "WHERE obj_id = " . $ilDB->quote($obj_id, 'integer') . PHP_EOL
104 ;
105 $result = $ilDB->query($sql);
106 // check number of records
107 if ($result->numRows() == 0) {
108 $message = "ilObjectFactory::getInstanceByObjId(): Object with obj_id: " . $obj_id . " not found!";
109 if ($stop_on_error === true) {
111 }
112 return null;
113 }
114
115 $row = $result->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
116 $class_name = "ilObj" . $objDefinition->getClassName($row["type"]);
117
118 // check class
119 if ($class_name == "ilObj") {
120 $message = "ilObjectFactory::getInstanceByObjId(): Not able to determine object " .
121 "class for type" . $row["type"] . ".";
122 if ($stop_on_error === true) {
124 }
125 return null;
126 }
127
128 // create instance
129 $obj = new $class_name(0, false); // this avoids reading of data
130 $obj->setId($obj_id);
131 $obj->read();
132
133 return $obj;
134 }
135
141 public static function getInstanceByRefId(int $ref_id, bool $stop_on_error = true): ?ilObject
142 {
143 global $DIC;
144 $objDefinition = $DIC["objDefinition"];
145 $ilDB = $DIC->database();
146
147 // check reference id
148 if (!isset($ref_id)) {
149 if ($stop_on_error === true) {
150 $message = "ilObjectFactory::getInstanceByRefId(): No ref_id given!";
152 }
153 return null;
154 }
155
156 // read object data
157 $sql =
158 "SELECT object_data.obj_id, object_data.type" . PHP_EOL
159 . "FROM object_data, object_reference" . PHP_EOL
160 . "WHERE object_reference.obj_id = object_data.obj_id" . PHP_EOL
161 . "AND object_reference.ref_id = " . $ilDB->quote($ref_id, 'integer') . PHP_EOL
162 ;
163
164 $result = $ilDB->query($sql);
165
166 // check number of records
167 if ($result->numRows() == 0) {
168 if ($stop_on_error === true) {
169 $message = "ilObjectFactory::getInstanceByRefId(): Object with ref_id " . $ref_id . " not found!";
171 }
172 return null;
173 }
174
175 $row = $result->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
176 $class_name = "ilObj" . $objDefinition->getClassName($row["type"]);
177
178 // check class
179 if ($class_name == "ilObj") {
180 if ($stop_on_error === true) {
181 $message = "ilObjectFactory::getInstanceByRefId(): Not able to determine object " .
182 "class for type" . $row["type"] . ".";
184 }
185 return null;
186 }
187
188 // create instance
189 $obj = new $class_name(0, false); // this avoids reading of data
190 $obj->setId((int) $row["obj_id"]);
191 $obj->setRefId($ref_id);
192 $obj->read();
193 return $obj;
194 }
195
201 public static function getTypeByRefId(int $ref_id, bool $stop_on_error = true): ?string
202 {
203 global $DIC;
204 $ilDB = $DIC->database();
205
206 // check reference id
207 if (!isset($ref_id)) {
208 if ($stop_on_error === true) {
209 $message = "ilObjectFactory::getTypeByRefId(): No ref_id given!";
211 }
212 return null;
213 }
214
215 // read object data
216 $sql =
217 "SELECT object_data.obj_id, object_data.type" . PHP_EOL
218 . "FROM object_data" . PHP_EOL
219 . "LEFT JOIN object_reference ON object_data.obj_id=object_reference.obj_id " . PHP_EOL
220 . "WHERE object_reference.ref_id=" . $ilDB->quote($ref_id, 'integer') . PHP_EOL
221 ;
222 $result = $ilDB->query($sql);
223
224 if ($result->numRows() == 0) {
225 if ($stop_on_error === true) {
226 $message = "ilObjectFactory::getTypeByRefId(): Object with ref_id " . $ref_id . " not found!";
228 }
229 return null;
230 }
231
232 $row = $result->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
233 return $row["type"];
234 }
235
236 public static function getClassByType(string $obj_type): string
237 {
238 global $DIC;
239 $objDefinition = $DIC["objDefinition"];
240
241 $class_name = "ilObj" . $objDefinition->getClassName($obj_type);
242
243 return $class_name;
244 }
245}
Class ilObjectFactory This class offers methods to get instances of the type-specific object classes ...
static getClassByType(string $obj_type)
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
static getTypeByRefId(int $ref_id, bool $stop_on_error=true)
get object type by reference id
ObjectIdExists(int $obj_id)
check if obj_id exists.
static getObjectsForOwner(string $object_type, int $owner_id)
returns all objects of an owner, filtered by type, objects are not deleted!
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
Class ilObject Basic functions for all objects.
$ref_id
Definition: ltiauth.php:66
global $DIC
Definition: shib_login.php:26
$message
Definition: xapiexit.php:31