ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilObjectFactory.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
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  (new self())->includeClassIfNotExists($class_name, $row["type"], $objDefinition);
129 
130  // create instance
131  $obj = new $class_name(0, false); // this avoids reading of data
132  $obj->setId($obj_id);
133  $obj->read();
134 
135  return $obj;
136  }
137 
143  public static function getInstanceByRefId(int $ref_id, bool $stop_on_error = true): ?ilObject
144  {
145  global $DIC;
146  $objDefinition = $DIC["objDefinition"];
147  $ilDB = $DIC->database();
148 
149  // check reference id
150  if (!isset($ref_id)) {
151  if ($stop_on_error === true) {
152  $message = "ilObjectFactory::getInstanceByRefId(): No ref_id given!";
154  }
155  return null;
156  }
157 
158  // read object data
159  $sql =
160  "SELECT object_data.obj_id, object_data.type" . PHP_EOL
161  . "FROM object_data, object_reference" . PHP_EOL
162  . "WHERE object_reference.obj_id = object_data.obj_id" . PHP_EOL
163  . "AND object_reference.ref_id = " . $ilDB->quote($ref_id, 'integer') . PHP_EOL
164  ;
165 
166  $result = $ilDB->query($sql);
167 
168  // check number of records
169  if ($result->numRows() == 0) {
170  if ($stop_on_error === true) {
171  $message = "ilObjectFactory::getInstanceByRefId(): Object with ref_id " . $ref_id . " not found!";
173  }
174  return null;
175  }
176 
177  $row = $result->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
178  $class_name = "ilObj" . $objDefinition->getClassName($row["type"]);
179 
180  // check class
181  if ($class_name == "ilObj") {
182  if ($stop_on_error === true) {
183  $message = "ilObjectFactory::getInstanceByRefId(): Not able to determine object " .
184  "class for type" . $row["type"] . ".";
186  }
187  return null;
188  }
189 
190  (new self())->includeClassIfNotExists($class_name, $row["type"], $objDefinition);
191 
192  // create instance
193  $obj = new $class_name(0, false); // this avoids reading of data
194  $obj->setId((int) $row["obj_id"]);
195  $obj->setRefId($ref_id);
196  $obj->read();
197  return $obj;
198  }
199 
205  public static function getTypeByRefId(int $ref_id, bool $stop_on_error = true): ?string
206  {
207  global $DIC;
208  $ilDB = $DIC->database();
209 
210  // check reference id
211  if (!isset($ref_id)) {
212  if ($stop_on_error === true) {
213  $message = "ilObjectFactory::getTypeByRefId(): No ref_id given!";
215  }
216  return null;
217  }
218 
219  // read object data
220  $sql =
221  "SELECT object_data.obj_id, object_data.type" . PHP_EOL
222  . "FROM object_data" . PHP_EOL
223  . "LEFT JOIN object_reference ON object_data.obj_id=object_reference.obj_id " . PHP_EOL
224  . "WHERE object_reference.ref_id=" . $ilDB->quote($ref_id, 'integer') . PHP_EOL
225  ;
226  $result = $ilDB->query($sql);
227 
228  if ($result->numRows() == 0) {
229  if ($stop_on_error === true) {
230  $message = "ilObjectFactory::getTypeByRefId(): Object with ref_id " . $ref_id . " not found!";
232  }
233  return null;
234  }
235 
236  $row = $result->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
237  return $row["type"];
238  }
239 
240  public static function getClassByType(string $obj_type): string
241  {
242  global $DIC;
243  $objDefinition = $DIC["objDefinition"];
244 
245  $class_name = "ilObj" . $objDefinition->getClassName($obj_type);
246 
247  (new self())->includeClassIfNotExists($class_name, $obj_type, $objDefinition);
248 
249  return $class_name;
250  }
251 
260  protected function includeClassIfNotExists(
261  string $class_name,
262  string $a_obj_type,
263  ilObjectDefinition $objDefinition
264  ): void {
265  if (!class_exists($class_name)) {
266  $location = $objDefinition->getLocation($a_obj_type);
267  include_once($location . "/class." . $class_name . ".php");
268  }
269  }
270 }
$location
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: buildRTE.php:22
includeClassIfNotExists(string $class_name, string $a_obj_type, ilObjectDefinition $objDefinition)
Ensures a class is properly included.
ObjectIdExists(int $obj_id)
check if obj_id exists.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getTypeByRefId(int $ref_id, bool $stop_on_error=true)
get object type by reference id
global $DIC
Definition: feed.php:28
parses the objects.xml it handles the xml-description of all ilias objects
$ref_id
Definition: ltiauth.php:67
static getObjectsForOwner(string $object_type, int $owner_id)
returns all objects of an owner, filtered by type, objects are not deleted!
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
static getClassByType(string $obj_type)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
$message
Definition: xapiexit.php:32