ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilUserSearchCache.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
36 {
37  const DEFAULT_SEARCH = 0;
38  const ADVANCED_SEARCH = 1;
39  const SHOP_CONTENT = 2;
41  const ADVANCED_MD_SEARCH = 4;
42  const LUCENE_DEFAULT = 5;
43  const LUCENE_ADVANCED = 6;
44 
45  private static $instance = null;
46  private $db;
47 
48  private $usr_id;
50 
51  private $search_result = array();
52  private $checked = array();
53  private $failed = array();
54  private $page_number = 1;
55  private $query;
56  private $root = ROOT_FOLDER_ID;
57 
58  private $item_filter = array();
59 
60 
67  private function __construct($a_usr_id)
68  {
69  global $ilDB;
70 
71  $this->db = $ilDB;
72  $this->usr_id = $a_usr_id;
73  $this->search_type = self::DEFAULT_SEARCH;
74  $this->read();
75  }
76 
85  public static function _getInstance($a_usr_id)
86  {
87  if(is_object(self::$instance) and self::$instance)
88  {
89  return self::$instance;
90  }
91  return self::$instance = new ilUserSearchCache($a_usr_id);
92  }
93 
102  public function switchSearchType($a_type)
103  {
104  $this->search_type = $a_type;
105  $this->read();
106  return true;
107  }
108 
115  public function getResults()
116  {
117  return $this->search_result ? $this->search_result : array();
118  }
119 
127  public function setResults($a_results)
128  {
129  $this->search_result = $a_results;
130  }
131 
139  public function addResult($a_result_item)
140  {
141  $this->search_result[$a_result_item['ref_id']]['ref_id'] = $a_result_item['ref_id'];
142  $this->search_result[$a_result_item['ref_id']]['obj_id'] = $a_result_item['obj_id'];
143  $this->search_result[$a_result_item['ref_id']]['type'] = $a_result_item['type'];
144  return true;
145  }
146 
154  public function appendToFailed($a_ref_id)
155  {
156  $this->failed[$a_ref_id] = $a_ref_id;
157  }
158 
166  public function isFailed($a_ref_id)
167  {
168  return in_array($a_ref_id,$this->failed) ? true : false;
169  }
170 
179  public function appendToChecked($a_ref_id,$a_obj_id)
180  {
181  $this->checked[$a_ref_id] = $a_obj_id;
182  }
183 
191  public function isChecked($a_ref_id)
192  {
193  return array_key_exists($a_ref_id,$this->checked) and $this->checked[$a_ref_id];
194  }
195 
203  public function getCheckedItems()
204  {
205  return $this->checked ? $this->checked : array();
206  }
207 
214  public function setResultPageNumber($a_number)
215  {
216  if($a_number)
217  {
218  $this->page_number = $a_number;
219  }
220  }
221 
228  public function getResultPageNumber()
229  {
230  return $this->page_number ? $this->page_number : 1;
231  }
232 
238  public function setQuery($a_query)
239  {
240  $this->query = $a_query;
241  }
242 
248  public function getQuery()
249  {
250  return $this->query;
251  }
252 
258  public function setRoot($a_root)
259  {
260  $this->root = $a_root;
261  }
262 
268  public function getRoot()
269  {
270  return $this->root ? $this->root : ROOT_FOLDER_ID;
271  }
272 
273  public function setItemFilter($a_filter)
274  {
275  $this->item_filter = $a_filter;
276  }
277 
278  public function getItemFilter()
279  {
280  return (array) $this->item_filter;
281  }
282 
288  public function deleteCachedEntries()
289  {
290  global $ilDB;
291 
292  $query = "SELECT COUNT(*) num FROM usr_search ".
293  "WHERE usr_id = ".$ilDB->quote($this->usr_id,'integer')." ".
294  "AND search_type = ".$ilDB->quote($this->search_type,'integer');
295  $res = $ilDB->query($query);
296  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
297 
298  if($row->num > 0)
299  {
300  $ilDB->update('usr_search',
301  array(
302  'search_result' => array('clob',serialize(array(0))),
303  'checked' => array('clob',serialize(array(0))),
304  'failed' => array('clob',serialize(array(0))),
305  'page' => array('integer',0)),
306  array(
307  'usr_id' => array('integer',(int) $this->usr_id),
308  'search_type' => array('integer',(int) $this->search_type)
309  ));
310  }
311  else
312  {
313  $ilDB->insert('usr_search',
314  array(
315  'search_result' => array('clob',serialize(array(0))),
316  'checked' => array('clob',serialize(array(0))),
317  'failed' => array('clob',serialize(array(0))),
318  'page' => array('integer',0),
319  'usr_id' => array('integer',(int) $this->usr_id),
320  'search_type' => array('integer',(int) $this->search_type)
321  ));
322  }
323 
324  $this->setResultPageNumber(1);
325  $this->search_result = array();
326  $this->checked = array();
327  $this->failed = array();
328  }
329 
330 
337  public function delete()
338  {
339  global $ilDB;
340 
341  $query = "DELETE FROM usr_search ".
342  "WHERE usr_id = ".$this->db->quote($this->usr_id ,'integer')." ".
343  "AND search_type = ".$this->db->quote($this->search_type ,'integer');
344  $res = $ilDB->manipulate($query);
345 
346  $this->read();
347  return true;
348  }
349 
356  public function save()
357  {
358  global $ilDB,$ilLog;
359 
360  if($this->usr_id == ANONYMOUS_USER_ID)
361  {
362  return false;
363  }
364  if(!$this->usr_id) $this->usr_id = 0;
365  if(!$this->page_number) $this->page_number = 0;
366  if(!$this->search_type) $this->search_type = 0;
367 
368  $query = "DELETE FROM usr_search ".
369  "WHERE usr_id = ".$ilDB->quote($this->usr_id ,'integer')." ".
370  "AND search_type = ".$ilDB->quote($this->search_type ,'integer');
371  $res = $ilDB->manipulate($query);
372 
373  $ilDB->insert('usr_search',array(
374  'usr_id' => array('integer',$this->usr_id),
375  'search_result' => array('clob',serialize($this->search_result)),
376  'checked' => array('clob',serialize($this->checked)),
377  'failed' => array('clob',serialize($this->failed)),
378  'page' => array('integer',$this->page_number),
379  'search_type' => array('integer',$this->search_type),
380  'query' => array('clob',serialize($this->getQuery())),
381  'root' => array('integer',$this->getRoot())));
382 
383  #if($this->getItemFilter())
384  {
385  $_SESSION['lucene_item_filter'] = serialize($this->getItemFilter());
386  }
387 
388  }
389 
390 
397  private function read()
398  {
399  $this->failed = array();
400  $this->checked = array();
401  $this->search_result = array();
402  $this->page_number = 0;
403 
404  if($this->usr_id == ANONYMOUS_USER_ID)
405  {
406  return false;
407  }
408 
409  $query = "SELECT * FROM usr_search ".
410  "WHERE usr_id = ".$this->db->quote($this->usr_id ,'integer')." ".
411  "AND search_type = ".$this->db->quote($this->search_type ,'integer');
412 
413  $res = $this->db->query($query);
414  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
415  {
416  $this->search_result = unserialize(stripslashes($row->search_result));
417  if(strlen($row->checked))
418  {
419  $this->checked = unserialize(stripslashes($row->checked));
420  }
421  if(strlen($row->failed))
422  {
423  $this->failed = unserialize(stripslashes($row->failed));
424  }
425  $this->page_number = $row->page;
426  $this->setQuery(unserialize($row->query));
427  $this->setRoot($row->root);
428  }
429 
430  // Item filter
431  if($_SESSION['lucene_item_filter'])
432  {
433  $this->setItemFilter(unserialize($_SESSION['lucene_item_filter']));
434  }
435 
436  return true;
437  }
438 }
439 
440 
441 ?>