ILIAS  eassessment Revision 61809
 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  private static $instance = null;
48  private $db;
49 
50  private $usr_id;
52 
53  private $search_result = array();
54  private $checked = array();
55  private $failed = array();
56  private $page_number = 1;
57  private $query;
58  private $root = ROOT_FOLDER_ID;
59 
60  private $item_filter = array();
61 
62  private $isAnonymous = false;
63 
64 
71  private function __construct($a_usr_id)
72  {
73  global $ilDB;
74 
75  if($a_usr_id == ANONYMOUS_USER_ID)
76  {
77  $this->isAnonymous = true;
78  }
79 
80  $this->db = $ilDB;
81  $this->usr_id = $a_usr_id;
82  $this->search_type = self::DEFAULT_SEARCH;
83  $this->read();
84  }
85 
94  public static function _getInstance($a_usr_id)
95  {
96  if(is_object(self::$instance) and self::$instance)
97  {
98  return self::$instance;
99  }
100  return self::$instance = new ilUserSearchCache($a_usr_id);
101  }
102 
107  public function isAnonymous()
108  {
109  return $this->isAnonymous;
110  }
111 
120  public function switchSearchType($a_type)
121  {
122  $this->search_type = $a_type;
123  $this->read();
124  return true;
125  }
126 
133  public function getResults()
134  {
135  return $this->search_result ? $this->search_result : array();
136  }
137 
145  public function setResults($a_results)
146  {
147  $this->search_result = $a_results;
148  }
149 
157  public function addResult($a_result_item)
158  {
159  $this->search_result[$a_result_item['ref_id']]['ref_id'] = $a_result_item['ref_id'];
160  $this->search_result[$a_result_item['ref_id']]['obj_id'] = $a_result_item['obj_id'];
161  $this->search_result[$a_result_item['ref_id']]['type'] = $a_result_item['type'];
162  return true;
163  }
164 
172  public function appendToFailed($a_ref_id)
173  {
174  $this->failed[$a_ref_id] = $a_ref_id;
175  }
176 
184  public function isFailed($a_ref_id)
185  {
186  return in_array($a_ref_id,$this->failed) ? true : false;
187  }
188 
197  public function appendToChecked($a_ref_id,$a_obj_id)
198  {
199  $this->checked[$a_ref_id] = $a_obj_id;
200  }
201 
209  public function isChecked($a_ref_id)
210  {
211  return array_key_exists($a_ref_id,$this->checked) and $this->checked[$a_ref_id];
212  }
213 
221  public function getCheckedItems()
222  {
223  return $this->checked ? $this->checked : array();
224  }
225 
232  public function setResultPageNumber($a_number)
233  {
234  if($a_number)
235  {
236  $this->page_number = $a_number;
237  }
238  }
239 
246  public function getResultPageNumber()
247  {
248  return $this->page_number ? $this->page_number : 1;
249  }
250 
256  public function setQuery($a_query)
257  {
258  $this->query = $a_query;
259  }
260 
266  public function getQuery()
267  {
268  return $this->query;
269  }
270 
275  public function getUrlEncodedQuery()
276  {
277  if(is_array($this->getQuery()))
278  {
279  $query = $this->getQuery();
280 
281  return urlencode(str_replace('"', '.', $query['lom_content']));
282  }
283  return urlencode(str_replace('"', '.', $this->getQuery()));
284  }
285 
291  public function setRoot($a_root)
292  {
293  $this->root = $a_root;
294  }
295 
301  public function getRoot()
302  {
303  return $this->root ? $this->root : ROOT_FOLDER_ID;
304  }
305 
306  public function setItemFilter($a_filter)
307  {
308  $this->item_filter = $a_filter;
309  }
310 
311  public function getItemFilter()
312  {
313  return (array) $this->item_filter;
314  }
315 
321  public function deleteCachedEntries()
322  {
323  global $ilDB;
324 
325  if($this->isAnonymous())
326  {
327  return $this->deleteCachedEntriesAnonymous();
328  }
329 
330 
331  $query = "SELECT COUNT(*) num FROM usr_search ".
332  "WHERE usr_id = ".$ilDB->quote($this->usr_id,'integer')." ".
333  "AND search_type = ".$ilDB->quote($this->search_type,'integer');
334  $res = $ilDB->query($query);
335  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
336 
337  if($row->num > 0)
338  {
339  $ilDB->update('usr_search',
340  array(
341  'search_result' => array('clob',serialize(array(0))),
342  'checked' => array('clob',serialize(array(0))),
343  'failed' => array('clob',serialize(array(0))),
344  'page' => array('integer',0)),
345  array(
346  'usr_id' => array('integer',(int) $this->usr_id),
347  'search_type' => array('integer',(int) $this->search_type)
348  ));
349  }
350  else
351  {
352  $ilDB->insert('usr_search',
353  array(
354  'search_result' => array('clob',serialize(array(0))),
355  'checked' => array('clob',serialize(array(0))),
356  'failed' => array('clob',serialize(array(0))),
357  'page' => array('integer',0),
358  'usr_id' => array('integer',(int) $this->usr_id),
359  'search_type' => array('integer',(int) $this->search_type)
360  ));
361  }
362 
363  $this->setResultPageNumber(1);
364  $this->search_result = array();
365  $this->checked = array();
366  $this->failed = array();
367  }
368 
374  {
375  $this->setResultPageNumber(1);
376  $this->search_result = array();
377  $this->checked = array();
378  $this->failed = array();
379 
380  return true;
381  }
382 
383 
384 
391  public function delete()
392  {
393  global $ilDB;
394 
395  $query = "DELETE FROM usr_search ".
396  "WHERE usr_id = ".$this->db->quote($this->usr_id ,'integer')." ".
397  "AND search_type = ".$this->db->quote($this->search_type ,'integer');
398  $res = $ilDB->manipulate($query);
399 
400  $this->read();
401  return true;
402  }
403 
410  public function save()
411  {
412  global $ilDB,$ilLog;
413 
414  if($this->isAnonymous())
415  {
416  return $this->saveForAnonymous();
417  }
418 
419  $query = "DELETE FROM usr_search ".
420  "WHERE usr_id = ".$ilDB->quote($this->usr_id ,'integer')." ".
421  "AND ( search_type = ".$ilDB->quote($this->search_type ,'integer').' '.
422  "OR search_type = ".$ilDB->quote(self::LAST_QUERY,'integer'). ')';
423  $res = $ilDB->manipulate($query);
424 
425  $ilDB->insert('usr_search',array(
426  'usr_id' => array('integer',(int) $this->usr_id),
427  'search_result' => array('clob',serialize($this->search_result)),
428  'checked' => array('clob',serialize($this->checked)),
429  'failed' => array('clob',serialize($this->failed)),
430  'page' => array('integer',(int) $this->page_number),
431  'search_type' => array('integer',(int) $this->search_type),
432  'query' => array('clob',serialize($this->getQuery())),
433  'root' => array('integer',$this->getRoot()),
434  'item_filter' => array('text',serialize($this->getItemFilter()))));
435 
436 
437  // Write last query information
438  $ilDB->insert('usr_search',
439  array(
440  'usr_id' => array('integer',$this->usr_id),
441  'search_type' => array('integer',self::LAST_QUERY),
442  'query' => array('text',serialize($this->getQuery()))
443  )
444  );
445  }
446 
447  public function saveForAnonymous()
448  {
449  unset($_SESSION['usr_search_cache']);
450 
451  $_SESSION['usr_search_cache'][$this->search_type]['search_result'] = $this->search_result;
452  $_SESSION['usr_search_cache'][$this->search_type]['checked'] = $this->checked;
453  $_SESSION['usr_search_cache'][$this->search_type]['failed'] = $this->failed;
454  $_SESSION['usr_search_cache'][$this->search_type]['page'] = $this->page_number;
455  $_SESSION['usr_search_cache'][$this->search_type]['query'] = $this->getQuery();
456  $_SESSION['usr_search_cache'][$this->search_type]['root'] = $this->getRoot();
457  $_SESSION['usr_search_cache'][$this->search_type]['item_filter'] = $this->getItemFilter();
458 
459  $_SESSION['usr_search_cache'][self::LAST_QUERY]['query'] = $this->getQuery();
460 
461  return true;
462 
463  }
464 
465 
472  private function read()
473  {
474  $this->failed = array();
475  $this->checked = array();
476  $this->search_result = array();
477  $this->page_number = 0;
478 
479  if($this->isAnonymous())
480  {
481  return $this->readAnonymous();
482  }
483 
484  $query = "SELECT * FROM usr_search ".
485  "WHERE usr_id = ".$this->db->quote($this->usr_id ,'integer')." ".
486  "AND search_type = ".$this->db->quote($this->search_type ,'integer');
487 
488  $res = $this->db->query($query);
489  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
490  {
491  $this->search_result = unserialize(stripslashes($row->search_result));
492  if(strlen($row->checked))
493  {
494  $this->checked = unserialize(stripslashes($row->checked));
495  }
496  if(strlen($row->failed))
497  {
498  $this->failed = unserialize(stripslashes($row->failed));
499  }
500  $this->page_number = $row->page;
501  $this->setQuery(unserialize($row->query));
502  $this->setRoot($row->root);
503  $this->setItemFilter(unserialize($row->item_filter));
504  }
505  return true;
506  }
507 
511  private function readAnonymous()
512  {
513  $this->search_result = (array) $_SESSION['usr_search_cache'][$this->search_type]['search_result'];
514  $this->checked = (array) $_SESSION['usr_search_cache'][$this->search_type]['checked'];
515  $this->failed = (array) $_SESSION['usr_search_cache'][$this->search_type]['failed'];
516  $this->page_number = $_SESSION['usr_search_cache'][$this->search_type]['page_number'];
517 
518  $this->setQuery((string) $_SESSION['usr_search_cache'][$this->search_type]['query']);
519  $this->setRoot((string) $_SESSION['usr_search_cache'][$this->search_type]['root']);
520  $this->setItemFilter((array) $_SESSION['usr_search_cache'][$this->search_type]['item_filter']);
521 
522  return true;
523  }
524 }
525 ?>