ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  function ObjectIdExists($a_obj_id)
29  {
30  global $ilias, $ilDB;
31 
32  $query = "SELECT * FROM object_data ".
33  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer');
34 
35  $res = $ilias->db->query($query);
36 
37  return $res->numRows() ? true : false;
38  }
39 
47  function getObjectsForOwner ($object_type, $owner_id)
48  {
49  global $ilias, $ilDB;
50 
51  $query = "SELECT * FROM object_data,object_reference ".
52  "WHERE object_reference.obj_id = object_data.obj_id ".
53  " AND object_data.type=".$ilDB->quote($object_type,'text').
54  " AND object_data.owner = ".$ilDB->quote($owner_id,'integer');
55  $res = $ilDB->query($query);
56 
57  $obj_ids = array();
58  while($object_rec = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
59  $obj_ids [] = $object_rec["obj_id"];
60  }
61 
62  return $obj_ids;
63 
64  }
65 
72  function getInstanceByObjId($a_obj_id,$stop_on_error = true)
73  {
74  global $ilias, $objDefinition, $ilDB;
75 
76  // check object id
77  if (!isset($a_obj_id))
78  {
79  $message = "ilObjectFactory::getInstanceByObjId(): No obj_id given!";
80  if ($stop_on_error === true)
81  {
82  $ilias->raiseError($message,$ilias->error_obj->WARNING);
83  exit();
84  }
85  #var_dump("<pre>",$message,"<pre>");
86 
87  return false;
88  }
89 
90  // read object data
91  $q = "SELECT * FROM object_data ".
92  "WHERE obj_id = ".$ilDB->quote($a_obj_id ,'integer');
93  $object_set = $ilias->db->query($q);
94  // check number of records
95  if ($object_set->numRows() == 0)
96  {
97  $message = "ilObjectFactory::getInstanceByObjId(): Object with obj_id: ".$a_obj_id." not found!";
98  if ($stop_on_error === true)
99  {
100  $ilias->raiseError($message,$ilias->error_obj->WARNING);
101  exit();
102  }
103  #var_dump("<pre>",$message,"<pre>");
104  return false;
105  }
106 
107  $object_rec = $object_set->fetchRow(DB_FETCHMODE_ASSOC);
108  $class_name = "ilObj".$objDefinition->getClassName($object_rec["type"]);
109 
110  // check class
111  if ($class_name == "ilObj")
112  {
113  $message = "ilObjectFactory::getInstanceByObjId(): Not able to determine object ".
114  "class for type".$object_rec["type"].".";
115  if ($stop_on_error === true)
116  {
117  $ilias->raiseError($message,$ilias->error_obj->WARNING);
118  exit();
119  }
120  return false;
121  }
122 
123  // get location
124  $location = $objDefinition->getLocation($object_rec["type"]);
125 
126  // create instance
127  include_once($location."/class.".$class_name.".php");
128  $obj = new $class_name(0, false); // this avoids reading of data
129  $obj->setId($a_obj_id);
130  $obj->setObjDataRecord($object_rec);
131  $obj->read();
132 
133  return $obj;
134  }
135 
136 
143  function getInstanceByRefId($a_ref_id,$stop_on_error = true)
144  {
145  global $ilias, $objDefinition, $ilDB;
146 
147  // check reference id
148  if (!isset($a_ref_id))
149  {
150  if ($stop_on_error === true)
151  {
152  $message = "ilObjectFactory::getInstanceByRefId(): No ref_id given!";
153  $ilias->raiseError($message,$ilias->error_obj->WARNING);
154  exit();
155  }
156 
157  return false;
158  }
159 
160  // read object data
161 
162  $query = "SELECT * FROM object_data,object_reference ".
163  "WHERE object_reference.obj_id = object_data.obj_id ".
164  "AND object_reference.ref_id = ".$ilDB->quote($a_ref_id,'integer');
165  $object_set = $ilDB->query($query);
166 
167  // check number of records
168  if ($object_set->numRows() == 0)
169  {
170  if ($stop_on_error === true)
171  {
172  $message = "ilObjectFactory::getInstanceByRefId(): Object with ref_id ".$a_ref_id." not found!";
173  $ilias->raiseError($message,$ilias->error_obj->WARNING);
174  exit();
175  }
176 
177  return false;
178  }
179 
180  $object_rec = $object_set->fetchRow(DB_FETCHMODE_ASSOC);
181  $class_name = "ilObj".$objDefinition->getClassName($object_rec["type"]);
182 
183  // check class
184  if ($class_name == "ilObj")
185  {
186  if ($stop_on_error === true)
187  {
188  $message = "ilObjectFactory::getInstanceByRefId(): Not able to determine object ".
189  "class for type".$object_rec["type"].".";
190  $ilias->raiseError($message,$ilias->error_obj->WARNING);
191  exit();
192  }
193 
194  return false;
195  }
196 
197  // get location
198  $location = $objDefinition->getLocation($object_rec["type"]);
199 
200  // create instance
201  include_once($location."/class.".$class_name.".php");
202  $obj = new $class_name(0, false); // this avoids reading of data
203  $obj->setId($object_rec["obj_id"]);
204  $obj->setRefId($a_ref_id);
205  $obj->setObjDataRecord($object_rec);
206  $obj->read();
207  return $obj;
208  }
209 
216  function getTypeByRefId($a_ref_id,$stop_on_error = true)
217  {
218  global $ilias, $objDefinition, $ilDB;
219 
220  // check reference id
221  if (!isset($a_ref_id))
222  {
223  if ($stop_on_error === true)
224  {
225  $message = "ilObjectFactory::getTypeByRefId(): No ref_id given!";
226  $ilias->raiseError($message,$ilias->error_obj->WARNING);
227  exit();
228  }
229 
230  return false;
231  }
232 
233  // read object data
234  $q = "SELECT * FROM object_data ".
235  "LEFT JOIN object_reference ON object_data.obj_id=object_reference.obj_id ".
236  "WHERE object_reference.ref_id=".$ilDB->quote($a_ref_id,'integer');
237  $object_set = $ilias->db->query($q);
238 
239  if ($object_set->numRows() == 0)
240  {
241  if ($stop_on_error === true)
242  {
243  $message = "ilObjectFactory::getTypeByRefId(): Object with ref_id ".$a_ref_id." not found!";
244  $ilias->raiseError($message,$ilias->error_obj->WARNING);
245  exit();
246  }
247 
248  return false;
249  }
250 
251  $object_rec = $object_set->fetchRow(DB_FETCHMODE_ASSOC);
252  return $object_rec["type"];
253  }
254 
260  public static function getClassByType($a_obj_type)
261  {
262  global $objDefinition;
263 
264  $location = $objDefinition->getLocation($a_obj_type);
265  $class_name = "ilObj".$objDefinition->getClassName($a_obj_type);
266 
267  // create instance
268  include_once($location."/class.".$class_name.".php");
269  return $class_name;
270  }
271 }
272 ?>