ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjectDataCache.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
14 {
15  var $db = null;
16  var $reference_cache = array();
17  var $object_data_cache = array();
18 
19  function ilObjectDataCache()
20  {
21  global $ilDB;
22 
23  $this->db =& $ilDB;
24  }
25 
26  function deleteCachedEntry($a_obj_id)
27  {
28  unset($this->object_data_cache[$a_obj_id]);
29  }
30 
31  function lookupObjId($a_ref_id)
32  {
33  if(!$this->__isReferenceCached($a_ref_id))
34  {
35 //echo"-objidmissed-$a_ref_id-";
36  $obj_id = $this->__storeReference($a_ref_id);
37  $this->__storeObjectData($obj_id);
38  }
39  return (int) @$this->reference_cache[$a_ref_id];
40  }
41 
42  function lookupTitle($a_obj_id)
43  {
44  if(!$this->__isObjectCached($a_obj_id))
45  {
46  $this->__storeObjectData($a_obj_id);
47  }
48  return @$this->object_data_cache[$a_obj_id]['title'];
49  }
50 
51  function lookupType($a_obj_id)
52  {
53  if(!$this->__isObjectCached($a_obj_id))
54  {
55 //echo"-typemissed-$a_obj_id-";
56  $this->__storeObjectData($a_obj_id);
57  }
58  return @$this->object_data_cache[$a_obj_id]['type'];
59  }
60 
61  function lookupOwner($a_obj_id)
62  {
63  if(!$this->__isObjectCached($a_obj_id))
64  {
65  $this->__storeObjectData($a_obj_id);
66  }
67  return @$this->object_data_cache[$a_obj_id]['owner'];
68  }
69 
70  function lookupDescription($a_obj_id)
71  {
72  if(!$this->__isObjectCached($a_obj_id))
73  {
74  $this->__storeObjectData($a_obj_id);
75  }
76  return @$this->object_data_cache[$a_obj_id]['description'];
77  }
78 
79  function lookupLastUpdate($a_obj_id)
80  {
81  if(!$this->__isObjectCached($a_obj_id))
82  {
83  $this->__storeObjectData($a_obj_id);
84  }
85  return @$this->object_data_cache[$a_obj_id]['last_update'];
86  }
87  // PRIVATE
88 
96  function __isReferenceCached($a_ref_id)
97  {
98  #return false;
99  #static $cached = 0;
100  #static $not_cached = 0;
101 
102  if(@$this->reference_cache[$a_ref_id])
103  {
104  #echo "Reference ". ++$cached ."cached<br>";
105  return true;
106  }
107  #echo "Reference ". ++$not_cached ." not cached<br>";
108  return false;
109 
110  }
111 
119  function __isObjectCached($a_obj_id)
120  {
121  static $cached = 0;
122  static $not_cached = 0;
123 
124 
125  if(@$this->object_data_cache[$a_obj_id])
126  {
127  #echo "Object ". ++$cached ."cached<br>";
128  return true;
129  }
130  #echo "Object ". ++$not_cached ." not cached<br>";
131  return false;
132  }
133 
134 
144  function __storeReference($a_ref_id)
145  {
146  global $ilDB;
147 
148  $query = "SELECT obj_id FROM object_reference WHERE ref_id = ".$ilDB->quote($a_ref_id,'integer');
149  $res = $this->db->query($query);
150  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
151  {
152  $this->reference_cache[$a_ref_id] = $row['obj_id'];
153  }
154  return (int) @$this->reference_cache[$a_ref_id];
155  }
156 
164  function __storeObjectData($a_obj_id, $a_lang = "")
165  {
166  global $ilDB, $objDefinition, $ilUser;
167 
168  if (is_object($ilUser) && $a_lang == "")
169  {
170  $a_lang = $ilUser->getLanguage();
171  }
172 
173  $query = "SELECT * FROM object_data WHERE obj_id = ".
174  $ilDB->quote($a_obj_id ,'integer');
175  $res = $this->db->query($query);
176  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
177  {
178  $this->object_data_cache[$a_obj_id]['title'] = $row->title;
179  $this->object_data_cache[$a_obj_id]['description'] = $row->description;
180  $this->object_data_cache[$a_obj_id]['type'] = $row->type;
181  $this->object_data_cache[$a_obj_id]['owner'] = $row->owner;
182  $this->object_data_cache[$a_obj_id]['last_update'] = $row->last_update;
183 
184  if (is_object($objDefinition))
185  {
186  $translation_type = $objDefinition->getTranslationType($row->type);
187  }
188 
189  if ($translation_type == "db")
190  {
191  if (!$this->trans_loaded[$a_obj_id])
192  {
193  $q = "SELECT title,description FROM object_translation ".
194  "WHERE obj_id = ".$ilDB->quote($a_obj_id,'integer')." ".
195  "AND lang_code = ".$ilDB->quote($a_lang,'text')." ".
196  "AND NOT lang_default = 1";
197  $r = $ilDB->query($q);
198 
199  $row = $r->fetchRow(DB_FETCHMODE_OBJECT);
200  if ($row)
201  {
202  $this->object_data_cache[$a_obj_id]['title'] = $row->title;
203  $this->object_data_cache[$a_obj_id]['description'] = $row->description;
204  }
205  $this->trans_loaded[$a_obj_id] = true;
206  }
207  }
208  }
209 
210  return true;
211  }
212 
220  function preloadObjectCache($a_obj_ids, $a_lang = "")
221  {
222  global $ilDB, $objDefinition, $ilUser, $tree;
223 
224  if (is_object($ilUser) && $a_lang == "")
225  {
226  $a_lang = $ilUser->getLanguage();
227  }
228 //echo "<br>-preloading-"; var_dump($a_obj_ids);
229  if (!is_array($a_obj_ids)) return;
230  if (count($a_obj_ids) == 0) return;
231 
232 
233  $query = "SELECT * FROM object_data ".
234  "WHERE ".$ilDB->in('obj_id',$a_obj_ids,false,'integer');
235  $res = $ilDB->query($query);
236  $db_trans = array();
237  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
238  {
239 //echo "<br>store_obj-".$row->obj_id."-".$row->type."-".$row->title."-";
240  $this->object_data_cache[$row->obj_id]['title'] = $row->title;
241  $this->object_data_cache[$row->obj_id]['description'] = $row->description;
242  $this->object_data_cache[$row->obj_id]['type'] = $row->type;
243  $this->object_data_cache[$row->obj_id]['owner'] = $row->owner;
244  $this->object_data_cache[$row->obj_id]['last_update'] = $row->last_update;
245 
246  if (is_object($objDefinition))
247  {
248  $translation_type = $objDefinition->getTranslationType($row->type);
249  }
250 
251  if ($translation_type == "db")
252  {
253  $db_trans[$row->obj_id] = $row->obj_id;
254  }
255  }
256  if (count($db_trans) > 0)
257  {
258  $this->preloadTranslations($db_trans, $a_lang);
259  }
260  }
261 
267  function preloadTranslations($a_obj_ids, $a_lang)
268  {
269  global $ilDB, $tree;
270 
271  $obj_ids = array();
272  foreach ($a_obj_ids as $id)
273  {
274  // do not load an id more than one time
275  if (!$this->trans_loaded[$id])
276  {
277  $obj_ids[] = $id;
278  $this->trans_loaded[$id] = true;
279  }
280  }
281  if (count($obj_ids) > 0)
282  {
283  $q = "SELECT obj_id, title, description FROM object_translation ".
284  "WHERE ".$ilDB->in('obj_id', $obj_ids, false, 'integer')." ".
285  "AND lang_code = ".$ilDB->quote($a_lang, 'text')." ".
286  "AND NOT lang_default = 1";
287  $r = $ilDB->query($q);
288  while ($row2 = $r->fetchRow(DB_FETCHMODE_OBJECT))
289  {
290  $this->object_data_cache[$row2->obj_id]['title'] = $row2->title;
291  $this->object_data_cache[$row2->obj_id]['description'] = $row2->description;
292  }
293  }
294  }
295 
296  function preloadReferenceCache($a_ref_ids, $a_incl_obj = true)
297  {
298  global $ilDB;
299 
300  if (!is_array($a_ref_ids)) return;
301  if (count($a_ref_ids) == 0) return;
302 
303  $query = "SELECT ref_id, obj_id FROM object_reference ".
304  "WHERE ".$ilDB->in('ref_id',$a_ref_ids,false,'integer');
305  $res = $ilDB->query($query);
306 
307  $obj_ids = array();
308  while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
309  {
310  $this->reference_cache[$row['ref_id']] = $row['obj_id'];
311 //echo "<br>store_ref-".$row['ref_id']."-".$row['obj_id']."-";
312  $obj_ids[] = $row['obj_id'];
313  }
314  if ($a_incl_obj)
315  {
316  $this->preloadObjectCache($obj_ids);
317  }
318  }
319 
320 }
321 ?>