ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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  // begin-patch create_date
71  private $creation_filter = array();
72  // end-patch create_date
73 
74 
75 
82  private function __construct($a_usr_id)
83  {
84  global $ilDB;
85 
86  if($a_usr_id == ANONYMOUS_USER_ID)
87  {
88  $this->isAnonymous = true;
89  }
90 
91  $this->db = $ilDB;
92  $this->usr_id = $a_usr_id;
93  $this->search_type = self::DEFAULT_SEARCH;
94  $this->read();
95  }
96 
105  public static function _getInstance($a_usr_id)
106  {
107  if(is_object(self::$instance) and self::$instance)
108  {
109  return self::$instance;
110  }
111  return self::$instance = new ilUserSearchCache($a_usr_id);
112  }
113 
118  public function isAnonymous()
119  {
120  return $this->isAnonymous;
121  }
122 
131  public function switchSearchType($a_type)
132  {
133  $this->search_type = $a_type;
134  $this->read();
135  return true;
136  }
137 
144  public function getResults()
145  {
146  return $this->search_result ? $this->search_result : array();
147  }
148 
156  public function setResults($a_results)
157  {
158  $this->search_result = $a_results;
159  }
160 
168  public function addResult($a_result_item)
169  {
170  $this->search_result[$a_result_item['ref_id']]['ref_id'] = $a_result_item['ref_id'];
171  $this->search_result[$a_result_item['ref_id']]['obj_id'] = $a_result_item['obj_id'];
172  $this->search_result[$a_result_item['ref_id']]['type'] = $a_result_item['type'];
173  return true;
174  }
175 
183  public function appendToFailed($a_ref_id)
184  {
185  $this->failed[$a_ref_id] = $a_ref_id;
186  }
187 
195  public function isFailed($a_ref_id)
196  {
197  return in_array($a_ref_id,$this->failed) ? true : false;
198  }
199 
208  public function appendToChecked($a_ref_id,$a_obj_id)
209  {
210  $this->checked[$a_ref_id] = $a_obj_id;
211  }
212 
220  public function isChecked($a_ref_id)
221  {
222  return array_key_exists($a_ref_id,$this->checked) and $this->checked[$a_ref_id];
223  }
224 
232  public function getCheckedItems()
233  {
234  return $this->checked ? $this->checked : array();
235  }
236 
243  public function setResultPageNumber($a_number)
244  {
245  if($a_number)
246  {
247  $this->page_number = $a_number;
248  }
249  }
250 
257  public function getResultPageNumber()
258  {
259  return $this->page_number ? $this->page_number : 1;
260  }
261 
267  public function setQuery($a_query)
268  {
269  $this->query = $a_query;
270  }
271 
277  public function getQuery()
278  {
279  return $this->query;
280  }
281 
286  public function getUrlEncodedQuery()
287  {
288  if(is_array($this->getQuery()))
289  {
290  $query = $this->getQuery();
291 
292  return urlencode(str_replace('"', '.', $query['lom_content']));
293  }
294  return urlencode(str_replace('"', '.', $this->getQuery()));
295  }
296 
302  public function setRoot($a_root)
303  {
304  $this->root = $a_root;
305  }
306 
312  public function getRoot()
313  {
314  return $this->root ? $this->root : ROOT_FOLDER_ID;
315  }
316 
317  public function setItemFilter($a_filter)
318  {
319  $this->item_filter = $a_filter;
320  }
321 
322  public function getItemFilter()
323  {
324  return (array) $this->item_filter;
325  }
326 
327  public function setMimeFilter($a_filter)
328  {
329  $this->mime_filter = $a_filter;
330  }
331 
332  public function getMimeFilter()
333  {
334  return (array) $this->mime_filter;
335  }
336 
337  // begin-patch create_date
338  public function setCreationFilter($a_filter)
339  {
340  $this->creation_filter = $a_filter;
341  }
342 
343  public function getCreationFilter()
344  {
345  return $this->creation_filter;
346  }
347  // end-patch create_date
348 
349 
355  public function deleteCachedEntries()
356  {
357  global $ilDB;
358 
359  if($this->isAnonymous())
360  {
361  return $this->deleteCachedEntriesAnonymous();
362  }
363 
364 
365  $query = "SELECT COUNT(*) num FROM usr_search ".
366  "WHERE usr_id = ".$ilDB->quote($this->usr_id,'integer')." ".
367  "AND search_type = ".$ilDB->quote($this->search_type,'integer');
368  $res = $ilDB->query($query);
369  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
370 
371  if($row->num > 0)
372  {
373  $ilDB->update('usr_search',
374  array(
375  'search_result' => array('clob',serialize(array(0))),
376  'checked' => array('clob',serialize(array(0))),
377  'failed' => array('clob',serialize(array(0))),
378  'page' => array('integer',0)),
379  array(
380  'usr_id' => array('integer',(int) $this->usr_id),
381  'search_type' => array('integer',(int) $this->search_type)
382  ));
383  }
384  else
385  {
386  $ilDB->insert('usr_search',
387  array(
388  'search_result' => array('clob',serialize(array(0))),
389  'checked' => array('clob',serialize(array(0))),
390  'failed' => array('clob',serialize(array(0))),
391  'page' => array('integer',0),
392  'usr_id' => array('integer',(int) $this->usr_id),
393  'search_type' => array('integer',(int) $this->search_type)
394  ));
395  }
396 
397  $this->setResultPageNumber(1);
398  $this->search_result = array();
399  $this->checked = array();
400  $this->failed = array();
401  }
402 
408  {
409  $this->setResultPageNumber(1);
410  $this->search_result = array();
411  $this->checked = array();
412  $this->failed = array();
413 
414  return true;
415  }
416 
417 
418 
425  public function delete()
426  {
427  global $ilDB;
428 
429  $query = "DELETE FROM usr_search ".
430  "WHERE usr_id = ".$this->db->quote($this->usr_id ,'integer')." ".
431  "AND search_type = ".$this->db->quote($this->search_type ,'integer');
432  $res = $ilDB->manipulate($query);
433 
434  $this->read();
435  return true;
436  }
437 
444  public function save()
445  {
446  global $ilDB;
447 
448  if($this->isAnonymous())
449  {
450  return $this->saveForAnonymous();
451  }
452 
453  $query = "DELETE FROM usr_search ".
454  "WHERE usr_id = ".$ilDB->quote($this->usr_id ,'integer')." ".
455  "AND ( search_type = ".$ilDB->quote($this->search_type ,'integer').' '.
456  "OR search_type = ".$ilDB->quote(self::LAST_QUERY,'integer'). ')';
457  $res = $ilDB->manipulate($query);
458 
459  $ilDB->insert('usr_search',array(
460  'usr_id' => array('integer',(int) $this->usr_id),
461  'search_result' => array('clob',serialize($this->search_result)),
462  'checked' => array('clob',serialize($this->checked)),
463  'failed' => array('clob',serialize($this->failed)),
464  'page' => array('integer',(int) $this->page_number),
465  'search_type' => array('integer',(int) $this->search_type),
466  'query' => array('clob',serialize($this->getQuery())),
467  'root' => array('integer',$this->getRoot()),
468  'item_filter' => array('text',serialize($this->getItemFilter())),
469  'mime_filter' => array('text', serialize($this->getMimeFilter())),
470  'creation_filter' => array('text', serialize($this->getCreationFilter()))
471  ));
472 
473 
474  // Write last query information
475  $ilDB->insert('usr_search',
476  array(
477  'usr_id' => array('integer',$this->usr_id),
478  'search_type' => array('integer',self::LAST_QUERY),
479  'query' => array('text',serialize($this->getQuery()))
480  )
481  );
482  }
483 
484  public function saveForAnonymous()
485  {
486  unset($_SESSION['usr_search_cache']);
487 
488  $_SESSION['usr_search_cache'][$this->search_type]['search_result'] = $this->search_result;
489  $_SESSION['usr_search_cache'][$this->search_type]['checked'] = $this->checked;
490  $_SESSION['usr_search_cache'][$this->search_type]['failed'] = $this->failed;
491  $_SESSION['usr_search_cache'][$this->search_type]['page'] = $this->page_number;
492  $_SESSION['usr_search_cache'][$this->search_type]['query'] = $this->getQuery();
493  $_SESSION['usr_search_cache'][$this->search_type]['root'] = $this->getRoot();
494  $_SESSION['usr_search_cache'][$this->search_type]['item_filter'] = $this->getItemFilter();
495  $_SESSION['usr_search_cache'][$this->search_type]['mime_filter'] = $this->getMimeFilter();
496  $_SESSION['usr_search_cache'][$this->search_type]['creation_filter'] = $this->getCreationFilter();
497 
498  $_SESSION['usr_search_cache'][self::LAST_QUERY]['query'] = $this->getQuery();
499 
500  return true;
501 
502  }
503 
504 
511  private function read()
512  {
513  $this->failed = array();
514  $this->checked = array();
515  $this->search_result = array();
516  $this->page_number = 0;
517 
518  if($this->isAnonymous())
519  {
520  return $this->readAnonymous();
521  }
522 
523  $query = "SELECT * FROM usr_search ".
524  "WHERE usr_id = ".$this->db->quote($this->usr_id ,'integer')." ".
525  "AND search_type = ".$this->db->quote($this->search_type ,'integer');
526 
527  $res = $this->db->query($query);
528  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
529  {
530  $this->search_result = unserialize(stripslashes($row->search_result));
531  if(strlen($row->checked))
532  {
533  $this->checked = unserialize(stripslashes($row->checked));
534  }
535  if(strlen($row->failed))
536  {
537  $this->failed = unserialize(stripslashes($row->failed));
538  }
539  $this->page_number = $row->page;
540  $this->setQuery(unserialize($row->query));
541  $this->setRoot($row->root);
542  $this->setItemFilter(unserialize($row->item_filter));
543  $this->setCreationFilter(unserialize($row->creation_filter));
544  }
545  return true;
546  }
547 
551  private function readAnonymous()
552  {
553  $this->search_result = (array) $_SESSION['usr_search_cache'][$this->search_type]['search_result'];
554  $this->checked = (array) $_SESSION['usr_search_cache'][$this->search_type]['checked'];
555  $this->failed = (array) $_SESSION['usr_search_cache'][$this->search_type]['failed'];
556  $this->page_number = $_SESSION['usr_search_cache'][$this->search_type]['page_number'];
557 
558  $this->setQuery($_SESSION['usr_search_cache'][$this->search_type]['query']);
559  $this->setRoot((string) $_SESSION['usr_search_cache'][$this->search_type]['root']);
560  $this->setItemFilter((array) $_SESSION['usr_search_cache'][$this->search_type]['item_filter']);
561  $this->setMimeFilter((array) $_SESSION['usr_search_cache'][$this->search_type]['mime_filter']);
562  $this->setCreationFilter((array) $_SESSION['usr_search_cache'][$this->search_type]['creation_filter']);
563 
564  return true;
565  }
566 }
567 ?>
setQuery($a_query)
set query
isChecked($a_ref_id)
Check if reference was already checked.
addResult($a_result_item)
Append result.
const DEFAULT_SEARCH
$_SESSION["AccountId"]
__construct($a_usr_id)
Constructor.
getCheckedItems()
Get all checked items.
deleteCachedEntries()
delete cached entries
switchSearchType($a_type)
switch to search type reads entries from database
deleteCachedEntriesAnonymous()
Delete cached entries for anonymous user.
readAnonymous()
Read from session for anonymous user.
getResultPageNumber()
get result page number
const DB_FETCHMODE_OBJECT
Definition: class.ilDB.php:11
getUrlEncodedQuery()
Urlencode query for further use in e.g glossariers (highlighting off search terms).
static _getInstance($a_usr_id)
Get singleton instance.
setResults($a_results)
Set results.
setRoot($a_root)
set root node of search
isFailed($a_ref_id)
check if reference has failed access
appendToFailed($a_ref_id)
Append failed id.
isAnonymous()
Check if current user is anonymous user.
global $ilDB
Class for storing search result.
appendToChecked($a_ref_id, $a_obj_id)
Append checked id.
read()
Read user entries.
setResultPageNumber($a_number)
Set result page number.