ILIAS  Release_4_4_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  const LAST_QUERY = 7;
46 
47  const LUCENE_USER_SEARCH = 8;
48 
49  private static $instance = null;
50  private $db;
51 
52  private $usr_id;
54 
55  private $search_result = array();
56  private $checked = array();
57  private $failed = array();
58  private $page_number = 1;
59  private $query;
60  private $root = ROOT_FOLDER_ID;
61 
62  private $item_filter = array();
63 
64  private $isAnonymous = false;
65 
66  // begin-patch mime_filter
67  private $mime_filter = array();
68  // end-patch mime_filter
69 
70 
77  private function __construct($a_usr_id)
78  {
79  global $ilDB;
80 
81  if($a_usr_id == ANONYMOUS_USER_ID)
82  {
83  $this->isAnonymous = true;
84  }
85 
86  $this->db = $ilDB;
87  $this->usr_id = $a_usr_id;
88  $this->search_type = self::DEFAULT_SEARCH;
89  $this->read();
90  }
91 
100  public static function _getInstance($a_usr_id)
101  {
102  if(is_object(self::$instance) and self::$instance)
103  {
104  return self::$instance;
105  }
106  return self::$instance = new ilUserSearchCache($a_usr_id);
107  }
108 
113  public function isAnonymous()
114  {
115  return $this->isAnonymous;
116  }
117 
126  public function switchSearchType($a_type)
127  {
128  $this->search_type = $a_type;
129  $this->read();
130  return true;
131  }
132 
139  public function getResults()
140  {
141  return $this->search_result ? $this->search_result : array();
142  }
143 
151  public function setResults($a_results)
152  {
153  $this->search_result = $a_results;
154  }
155 
163  public function addResult($a_result_item)
164  {
165  $this->search_result[$a_result_item['ref_id']]['ref_id'] = $a_result_item['ref_id'];
166  $this->search_result[$a_result_item['ref_id']]['obj_id'] = $a_result_item['obj_id'];
167  $this->search_result[$a_result_item['ref_id']]['type'] = $a_result_item['type'];
168  return true;
169  }
170 
178  public function appendToFailed($a_ref_id)
179  {
180  $this->failed[$a_ref_id] = $a_ref_id;
181  }
182 
190  public function isFailed($a_ref_id)
191  {
192  return in_array($a_ref_id,$this->failed) ? true : false;
193  }
194 
203  public function appendToChecked($a_ref_id,$a_obj_id)
204  {
205  $this->checked[$a_ref_id] = $a_obj_id;
206  }
207 
215  public function isChecked($a_ref_id)
216  {
217  return array_key_exists($a_ref_id,$this->checked) and $this->checked[$a_ref_id];
218  }
219 
227  public function getCheckedItems()
228  {
229  return $this->checked ? $this->checked : array();
230  }
231 
238  public function setResultPageNumber($a_number)
239  {
240  if($a_number)
241  {
242  $this->page_number = $a_number;
243  }
244  }
245 
252  public function getResultPageNumber()
253  {
254  return $this->page_number ? $this->page_number : 1;
255  }
256 
262  public function setQuery($a_query)
263  {
264  $this->query = $a_query;
265  }
266 
272  public function getQuery()
273  {
274  return $this->query;
275  }
276 
281  public function getUrlEncodedQuery()
282  {
283  if(is_array($this->getQuery()))
284  {
285  $query = $this->getQuery();
286 
287  return urlencode(str_replace('"', '.', $query['lom_content']));
288  }
289  return urlencode(str_replace('"', '.', $this->getQuery()));
290  }
291 
297  public function setRoot($a_root)
298  {
299  $this->root = $a_root;
300  }
301 
307  public function getRoot()
308  {
309  return $this->root ? $this->root : ROOT_FOLDER_ID;
310  }
311 
312  public function setItemFilter($a_filter)
313  {
314  $this->item_filter = $a_filter;
315  }
316 
317  public function getItemFilter()
318  {
319  return (array) $this->item_filter;
320  }
321 
322  public function setMimeFilter($a_filter)
323  {
324  $this->mime_filter = $a_filter;
325  }
326 
327  public function getMimeFilter()
328  {
329  return (array) $this->mime_filter;
330  }
331 
337  public function deleteCachedEntries()
338  {
339  global $ilDB;
340 
341  if($this->isAnonymous())
342  {
343  return $this->deleteCachedEntriesAnonymous();
344  }
345 
346 
347  $query = "SELECT COUNT(*) num FROM usr_search ".
348  "WHERE usr_id = ".$ilDB->quote($this->usr_id,'integer')." ".
349  "AND search_type = ".$ilDB->quote($this->search_type,'integer');
350  $res = $ilDB->query($query);
351  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
352 
353  if($row->num > 0)
354  {
355  $ilDB->update('usr_search',
356  array(
357  'search_result' => array('clob',serialize(array(0))),
358  'checked' => array('clob',serialize(array(0))),
359  'failed' => array('clob',serialize(array(0))),
360  'page' => array('integer',0)),
361  array(
362  'usr_id' => array('integer',(int) $this->usr_id),
363  'search_type' => array('integer',(int) $this->search_type)
364  ));
365  }
366  else
367  {
368  $ilDB->insert('usr_search',
369  array(
370  'search_result' => array('clob',serialize(array(0))),
371  'checked' => array('clob',serialize(array(0))),
372  'failed' => array('clob',serialize(array(0))),
373  'page' => array('integer',0),
374  'usr_id' => array('integer',(int) $this->usr_id),
375  'search_type' => array('integer',(int) $this->search_type)
376  ));
377  }
378 
379  $this->setResultPageNumber(1);
380  $this->search_result = array();
381  $this->checked = array();
382  $this->failed = array();
383  }
384 
390  {
391  $this->setResultPageNumber(1);
392  $this->search_result = array();
393  $this->checked = array();
394  $this->failed = array();
395 
396  return true;
397  }
398 
399 
400 
407  public function delete()
408  {
409  global $ilDB;
410 
411  $query = "DELETE FROM usr_search ".
412  "WHERE usr_id = ".$this->db->quote($this->usr_id ,'integer')." ".
413  "AND search_type = ".$this->db->quote($this->search_type ,'integer');
414  $res = $ilDB->manipulate($query);
415 
416  $this->read();
417  return true;
418  }
419 
426  public function save()
427  {
428  global $ilDB,$ilLog;
429 
430  if($this->isAnonymous())
431  {
432  return $this->saveForAnonymous();
433  }
434 
435  $query = "DELETE FROM usr_search ".
436  "WHERE usr_id = ".$ilDB->quote($this->usr_id ,'integer')." ".
437  "AND ( search_type = ".$ilDB->quote($this->search_type ,'integer').' '.
438  "OR search_type = ".$ilDB->quote(self::LAST_QUERY,'integer'). ')';
439  $res = $ilDB->manipulate($query);
440 
441  $ilDB->insert('usr_search',array(
442  'usr_id' => array('integer',(int) $this->usr_id),
443  'search_result' => array('clob',serialize($this->search_result)),
444  'checked' => array('clob',serialize($this->checked)),
445  'failed' => array('clob',serialize($this->failed)),
446  'page' => array('integer',(int) $this->page_number),
447  'search_type' => array('integer',(int) $this->search_type),
448  'query' => array('clob',serialize($this->getQuery())),
449  'root' => array('integer',$this->getRoot()),
450  'item_filter' => array('text',serialize($this->getItemFilter())),
451  'mime_filter' => array('text', serialize($this->getMimeFilter()))
452  ));
453 
454 
455  // Write last query information
456  $ilDB->insert('usr_search',
457  array(
458  'usr_id' => array('integer',$this->usr_id),
459  'search_type' => array('integer',self::LAST_QUERY),
460  'query' => array('text',serialize($this->getQuery()))
461  )
462  );
463  }
464 
465  public function saveForAnonymous()
466  {
467  unset($_SESSION['usr_search_cache']);
468 
469  $_SESSION['usr_search_cache'][$this->search_type]['search_result'] = $this->search_result;
470  $_SESSION['usr_search_cache'][$this->search_type]['checked'] = $this->checked;
471  $_SESSION['usr_search_cache'][$this->search_type]['failed'] = $this->failed;
472  $_SESSION['usr_search_cache'][$this->search_type]['page'] = $this->page_number;
473  $_SESSION['usr_search_cache'][$this->search_type]['query'] = $this->getQuery();
474  $_SESSION['usr_search_cache'][$this->search_type]['root'] = $this->getRoot();
475  $_SESSION['usr_search_cache'][$this->search_type]['item_filter'] = $this->getItemFilter();
476  $_SESSION['usr_search_cache'][$this->search_type]['mime_filter'] = $this->getMimeFilter();
477 
478  $_SESSION['usr_search_cache'][self::LAST_QUERY]['query'] = $this->getQuery();
479 
480  return true;
481 
482  }
483 
484 
491  private function read()
492  {
493  $this->failed = array();
494  $this->checked = array();
495  $this->search_result = array();
496  $this->page_number = 0;
497 
498  if($this->isAnonymous())
499  {
500  return $this->readAnonymous();
501  }
502 
503  $query = "SELECT * FROM usr_search ".
504  "WHERE usr_id = ".$this->db->quote($this->usr_id ,'integer')." ".
505  "AND search_type = ".$this->db->quote($this->search_type ,'integer');
506 
507  $res = $this->db->query($query);
508  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
509  {
510  $this->search_result = unserialize(stripslashes($row->search_result));
511  if(strlen($row->checked))
512  {
513  $this->checked = unserialize(stripslashes($row->checked));
514  }
515  if(strlen($row->failed))
516  {
517  $this->failed = unserialize(stripslashes($row->failed));
518  }
519  $this->page_number = $row->page;
520  $this->setQuery(unserialize($row->query));
521  $this->setRoot($row->root);
522  $this->setItemFilter(unserialize($row->item_filter));
523  }
524  return true;
525  }
526 
530  private function readAnonymous()
531  {
532  $this->search_result = (array) $_SESSION['usr_search_cache'][$this->search_type]['search_result'];
533  $this->checked = (array) $_SESSION['usr_search_cache'][$this->search_type]['checked'];
534  $this->failed = (array) $_SESSION['usr_search_cache'][$this->search_type]['failed'];
535  $this->page_number = $_SESSION['usr_search_cache'][$this->search_type]['page_number'];
536 
537  $this->setQuery($_SESSION['usr_search_cache'][$this->search_type]['query']);
538  $this->setRoot((string) $_SESSION['usr_search_cache'][$this->search_type]['root']);
539  $this->setItemFilter((array) $_SESSION['usr_search_cache'][$this->search_type]['item_filter']);
540  $this->setMimeFilter((array) $_SESSION['usr_search_cache'][$this->search_type]['mime_filter']);
541 
542  return true;
543  }
544 }
545 ?>