ILIAS  Release_3_10_x_branch Revision 61812
 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 
43  private static $instance = null;
44  private $db;
45 
46  private $usr_id;
48 
49  private $search_result = array();
50  private $checked = array();
51  private $failed = array();
52  private $page_number = 1;
53 
54 
61  private function __construct($a_usr_id)
62  {
63  global $ilDB;
64 
65  $this->db = $ilDB;
66  $this->usr_id = $a_usr_id;
67  $this->search_type = self::DEFAULT_SEARCH;
68  $this->read();
69  }
70 
79  public static function _getInstance($a_usr_id)
80  {
81  if(is_object(self::$instance) and self::$instance)
82  {
83  return self::$instance;
84  }
85  return self::$instance = new ilUserSearchCache($a_usr_id);
86  }
87 
96  public function switchSearchType($a_type)
97  {
98  $this->search_type = $a_type;
99  $this->read();
100  return true;
101  }
102 
109  public function getResults()
110  {
111  return $this->search_result ? $this->search_result : array();
112  }
113 
121  public function setResults($a_results)
122  {
123  $this->search_result = $a_results;
124  }
125 
133  public function addResult($a_result_item)
134  {
135  $this->search_result[$a_result_item['ref_id']]['ref_id'] = $a_result_item['ref_id'];
136  $this->search_result[$a_result_item['ref_id']]['obj_id'] = $a_result_item['obj_id'];
137  $this->search_result[$a_result_item['ref_id']]['type'] = $a_result_item['type'];
138  return true;
139  }
140 
148  public function appendToFailed($a_ref_id)
149  {
150  $this->failed[$a_ref_id] = $a_ref_id;
151  }
152 
160  public function isFailed($a_ref_id)
161  {
162  return in_array($a_ref_id,$this->failed) ? true : false;
163  }
164 
173  public function appendToChecked($a_ref_id,$a_obj_id)
174  {
175  $this->checked[$a_ref_id] = $a_obj_id;
176  }
177 
185  public function isChecked($a_ref_id)
186  {
187  return array_key_exists($a_ref_id,$this->checked) and $this->checked[$a_ref_id];
188  }
189 
197  public function getCheckedItems()
198  {
199  return $this->checked ? $this->checked : array();
200  }
201 
208  public function setResultPageNumber($a_number)
209  {
210  if($a_number)
211  {
212  $this->page_number = $a_number;
213  }
214  }
215 
222  public function getResultPageNumber()
223  {
224  return $this->page_number ? $this->page_number : 1;
225  }
226 
233  public function delete()
234  {
235  $query = "DELETE FROM usr_search ".
236  "WHERE usr_id = ".$this->db->quote($this->usr_id)." ".
237  "AND search_type = ".$this->db->quote($this->search_type);
238 
239  $res = $this->db->query($query);
240  $this->read();
241  return true;
242  }
243 
250  public function save()
251  {
252  if($this->usr_id == ANONYMOUS_USER_ID)
253  {
254  return false;
255  }
256 
257  $query = "DELETE FROM usr_search ".
258  "WHERE usr_id = ".$this->db->quote($this->usr_id)." ".
259  "AND search_type = ".$this->db->quote($this->search_type);
260  $res = $this->db->query($query);
261 
262  $query = "INSERT INTO usr_search ".
263  "SET usr_id = ".$this->db->quote($this->usr_id).", ".
264  "search_result = '".addslashes(serialize($this->search_result))."', ".
265  "checked = '".addslashes(serialize($this->checked))."', ".
266  "failed = '".addslashes(serialize($this->failed))."', ".
267  "page = ".$this->db->quote($this->page_number).", ".
268  "search_type = ".$this->db->quote($this->search_type);
269  $res = $this->db->query($query);
270  }
271 
272 
279  private function read()
280  {
281  $this->failed = array();
282  $this->checked = array();
283  $this->search_result = array();
284  $this->page_number = 0;
285 
286  if($this->usr_id == ANONYMOUS_USER_ID)
287  {
288  return false;
289  }
290 
291  $query = "SELECT * FROM usr_search ".
292  "WHERE usr_id = ".$this->db->quote($this->usr_id)." ".
293  "AND search_type = ".$this->db->quote($this->search_type);
294 
295  $res = $this->db->query($query);
296  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
297  {
298  $this->search_result = unserialize(stripslashes($row->search_result));
299  if(strlen($row->checked))
300  {
301  $this->checked = unserialize(stripslashes($row->checked));
302  }
303  if(strlen($row->failed))
304  {
305  $this->failed = unserialize(stripslashes($row->failed));
306  }
307  $this->page_number = $row->page;
308  }
309  return true;
310  }
311 }
312 
313 
314 ?>