1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
5/**
6* Class ilObjectFactory
7*
8* This class offers methods to get instances of
9* the type-specific object classes (derived from
10* ilObject) by their object or reference id
11*
12* Note: The term "Ilias objects" means all
13* object types that are stored in the
14* database table "object_data"
15*
16* @author Alex Killing <alex.killing@gmx.de>
17* @version $Id$
18*
19*/
20class ilObjectFactory
21{
22    /**
23    * check if obj_id exists. To check for ref_ids use ilTree::isInTree()
24    *
25    * @param	int		$obj_id		object id
26    * @return	bool
27    */
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
42    /**
43     * returns all objects of an owner, filtered by type, objects are not deleted!
44     *
45     * @param unknown_type $object_type
46     * @param unknown_type $owner_id
47     * @return unknown
48     */
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
69    /**
70     * get an instance of an Ilias object by object id
71     * @param $a_obj_id
72     * @param bool $stop_on_error
73     * @return bool|ilObject
74     * @throws ilDatabaseException
75     * @throws ilObjectNotFoundException
76     */
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) {
88                throw new ilObjectNotFoundException($message);
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) {
102                throw new ilObjectNotFoundException($message);
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) {
115                throw new ilObjectNotFoundException($message);
116            }
117            return false;
118        }
119
120        // get location
121        $location = $objDefinition->getLocation($object_rec["type"]);
122
123        // create instance
124        include_once($location . "/class." . $class_name . ".php");
125        $obj = new $class_name(0, false);	// this avoids reading of data
126        $obj->setId($a_obj_id);
127        $obj->read();
128
129        return $obj;
130    }
131
132
133    /**
134     * get an instance of an Ilias object by reference id
135     * @param $a_ref_id
136     * @param bool $stop_on_error
137     * @return bool|ilObject
138     * @throws ilDatabaseException
139     * @throws ilObjectNotFoundException
140     */
141    public static function getInstanceByRefId($a_ref_id, $stop_on_error = true)
142    {
143        global $DIC;
144
145        $objDefinition = $DIC["objDefinition"];
146        $ilDB = $DIC->database();
147
148        // check reference id
149        if (!isset($a_ref_id)) {
150            if ($stop_on_error === true) {
151                $message = "ilObjectFactory::getInstanceByRefId(): No ref_id given!";
152                throw new ilObjectNotFoundException($message);
153            }
154
155            return false;
156        }
157
158        // read object data
159
160        $query = "SELECT * FROM object_data,object_reference " .
161            "WHERE object_reference.obj_id = object_data.obj_id " .
162            "AND object_reference.ref_id = " . $ilDB->quote($a_ref_id, 'integer');
163        $object_set = $ilDB->query($query);
164
165        // check number of records
166        if ($object_set->numRows() == 0) {
167            if ($stop_on_error === true) {
168                $message = "ilObjectFactory::getInstanceByRefId(): Object with ref_id " . $a_ref_id . " not found!";
169                throw new ilObjectNotFoundException($message);
170            }
171
172            return false;
173        }
174
175        $object_rec = $object_set->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
176        $class_name = "ilObj" . $objDefinition->getClassName($object_rec["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" . $object_rec["type"] . ".";
183                throw new ilObjectNotFoundException($message);
184            }
185
186            return false;
187        }
188
189        // get location
190        $location = $objDefinition->getLocation($object_rec["type"]);
191
192        // create instance
193        include_once($location . "/class." . $class_name . ".php");
194        $obj = new $class_name(0, false);	// this avoids reading of data
195        $obj->setId($object_rec["obj_id"]);
196        $obj->setRefId($a_ref_id);
197        $obj->read();
198        return $obj;
199    }
200
201    /**
202     * get object type by reference id
203     *
204     * @deprecated since version 5.3
205     * @param $a_ref_id
206     * @param bool $stop_on_error
207     * @return bool
208     * @throws ilDatabaseException
209     * @throws ilObjectNotFoundException
210     */
211    public static function getTypeByRefId($a_ref_id, $stop_on_error = true)
212    {
213        global $DIC;
214
215        $ilDB = $DIC->database();
216
217        // check reference id
218        if (!isset($a_ref_id)) {
219            if ($stop_on_error === true) {
220                $message = "ilObjectFactory::getTypeByRefId(): No ref_id given!";
221                throw new ilObjectNotFoundException($message);
222            }
223
224            return false;
225        }
226
227        // read object data
228        $q = "SELECT * FROM object_data " .
229             "LEFT JOIN object_reference ON object_data.obj_id=object_reference.obj_id " .
230             "WHERE object_reference.ref_id=" . $ilDB->quote($a_ref_id, 'integer');
231        $object_set = $ilDB->query($q);
232
233        if ($object_set->numRows() == 0) {
234            if ($stop_on_error === true) {
235                $message = "ilObjectFactory::getTypeByRefId(): Object with ref_id " . $a_ref_id . " not found!";
236                throw new ilObjectNotFoundException($message);
237            }
238
239            return false;
240        }
241
242        $object_rec = $object_set->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
243        return $object_rec["type"];
244    }
245
246    /**
247     * Get class by type
248     *
249     * @return
250     */
251    public static function getClassByType($a_obj_type)
252    {
253        global $DIC;
254
255        $objDefinition = $DIC["objDefinition"];
256
257        $location = $objDefinition->getLocation($a_obj_type);
258        $class_name = "ilObj" . $objDefinition->getClassName($a_obj_type);
259
260        // create instance
261        include_once($location . "/class." . $class_name . ".php");
262        return $class_name;
263    }
264}
265