ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 ADVANCED_MD_SEARCH = 4;
40  const LUCENE_DEFAULT = 5;
41  const LUCENE_ADVANCED = 6;
42 
43  const LAST_QUERY = 7;
44 
45  const LUCENE_USER_SEARCH = 8;
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  // begin-patch mime_filter
65  private $mime_filter = array();
66  // end-patch mime_filter
67 
68  // begin-patch create_date
69  private $creation_filter = array();
70  // end-patch create_date
71 
72 
73 
80  private function __construct($a_usr_id)
81  {
82  global $ilDB;
83 
84  if ($a_usr_id == ANONYMOUS_USER_ID) {
85  $this->isAnonymous = true;
86  }
87 
88  $this->db = $ilDB;
89  $this->usr_id = $a_usr_id;
90  $this->search_type = self::DEFAULT_SEARCH;
91  $this->read();
92  }
93 
102  public static function _getInstance($a_usr_id)
103  {
104  if (is_object(self::$instance) and self::$instance) {
105  return self::$instance;
106  }
107  return self::$instance = new ilUserSearchCache($a_usr_id);
108  }
109 
114  public function isAnonymous()
115  {
116  return $this->isAnonymous;
117  }
118 
127  public function switchSearchType($a_type)
128  {
129  $this->search_type = $a_type;
130  $this->read();
131  return true;
132  }
133 
140  public function getResults()
141  {
142  return $this->search_result ? $this->search_result : array();
143  }
144 
152  public function setResults($a_results)
153  {
154  $this->search_result = $a_results;
155  }
156 
164  public function addResult($a_result_item)
165  {
166  $this->search_result[$a_result_item['ref_id']]['ref_id'] = $a_result_item['ref_id'];
167  $this->search_result[$a_result_item['ref_id']]['obj_id'] = $a_result_item['obj_id'];
168  $this->search_result[$a_result_item['ref_id']]['type'] = $a_result_item['type'];
169  return true;
170  }
171 
179  public function appendToFailed($a_ref_id)
180  {
181  $this->failed[$a_ref_id] = $a_ref_id;
182  }
183 
191  public function isFailed($a_ref_id)
192  {
193  return in_array($a_ref_id, $this->failed) ? true : false;
194  }
195 
204  public function appendToChecked($a_ref_id, $a_obj_id)
205  {
206  $this->checked[$a_ref_id] = $a_obj_id;
207  }
208 
216  public function isChecked($a_ref_id)
217  {
218  return array_key_exists($a_ref_id, $this->checked) and $this->checked[$a_ref_id];
219  }
220 
228  public function getCheckedItems()
229  {
230  return $this->checked ? $this->checked : array();
231  }
232 
239  public function setResultPageNumber($a_number)
240  {
241  if ($a_number) {
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  $query = $this->getQuery();
285 
286  return urlencode(str_replace('"', '.', $query['lom_content']));
287  }
288  return urlencode(str_replace('"', '.', $this->getQuery()));
289  }
290 
296  public function setRoot($a_root)
297  {
298  $this->root = $a_root;
299  }
300 
306  public function getRoot()
307  {
308  return $this->root ? $this->root : ROOT_FOLDER_ID;
309  }
310 
311  public function setItemFilter($a_filter)
312  {
313  $this->item_filter = $a_filter;
314  }
315 
316  public function getItemFilter()
317  {
318  return (array) $this->item_filter;
319  }
320 
321  public function setMimeFilter($a_filter)
322  {
323  $this->mime_filter = $a_filter;
324  }
325 
326  public function getMimeFilter()
327  {
328  return (array) $this->mime_filter;
329  }
330 
331  // begin-patch create_date
332  public function setCreationFilter($a_filter)
333  {
334  $this->creation_filter = $a_filter;
335  }
336 
337  public function getCreationFilter()
338  {
339  return $this->creation_filter;
340  }
341  // end-patch create_date
342 
343 
349  public function deleteCachedEntries()
350  {
351  global $ilDB;
352 
353  if ($this->isAnonymous()) {
354  return $this->deleteCachedEntriesAnonymous();
355  }
356 
357 
358  $query = "SELECT COUNT(*) num FROM usr_search " .
359  "WHERE usr_id = " . $ilDB->quote($this->usr_id, 'integer') . " " .
360  "AND search_type = " . $ilDB->quote($this->search_type, 'integer');
361  $res = $ilDB->query($query);
363 
364  if ($row->num > 0) {
365  $ilDB->update(
366  'usr_search',
367  array(
368  'search_result' => array('clob',serialize(array(0))),
369  'checked' => array('clob',serialize(array(0))),
370  'failed' => array('clob',serialize(array(0))),
371  'page' => array('integer',0)),
372  array(
373  'usr_id' => array('integer',(int) $this->usr_id),
374  'search_type' => array('integer',(int) $this->search_type)
375  )
376  );
377  } else {
378  $ilDB->insert(
379  'usr_search',
380  array(
381  'search_result' => array('clob',serialize(array(0))),
382  'checked' => array('clob',serialize(array(0))),
383  'failed' => array('clob',serialize(array(0))),
384  'page' => array('integer',0),
385  'usr_id' => array('integer',(int) $this->usr_id),
386  'search_type' => array('integer',(int) $this->search_type)
387  )
388  );
389  }
390 
391  $this->setResultPageNumber(1);
392  $this->search_result = array();
393  $this->checked = array();
394  $this->failed = array();
395  }
396 
402  {
403  $this->setResultPageNumber(1);
404  $this->search_result = array();
405  $this->checked = array();
406  $this->failed = array();
407 
408  return true;
409  }
410 
411 
412 
419  public function delete()
420  {
421  global $ilDB;
422 
423  $query = "DELETE FROM usr_search " .
424  "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
425  "AND search_type = " . $this->db->quote($this->search_type, 'integer');
426  $res = $ilDB->manipulate($query);
427 
428  $this->read();
429  return true;
430  }
431 
438  public function save()
439  {
440  global $ilDB;
441 
442  if ($this->isAnonymous()) {
443  return $this->saveForAnonymous();
444  }
445 
446  $query = "DELETE FROM usr_search " .
447  "WHERE usr_id = " . $ilDB->quote($this->usr_id, 'integer') . " " .
448  "AND ( search_type = " . $ilDB->quote($this->search_type, 'integer') . ' ' .
449  "OR search_type = " . $ilDB->quote(self::LAST_QUERY, 'integer') . ')';
450  $res = $ilDB->manipulate($query);
451 
452  $ilDB->insert('usr_search', array(
453  'usr_id' => array('integer',(int) $this->usr_id),
454  'search_result' => array('clob',serialize($this->search_result)),
455  'checked' => array('clob',serialize($this->checked)),
456  'failed' => array('clob',serialize($this->failed)),
457  'page' => array('integer',(int) $this->page_number),
458  'search_type' => array('integer',(int) $this->search_type),
459  'query' => array('clob',serialize($this->getQuery())),
460  'root' => array('integer',$this->getRoot()),
461  'item_filter' => array('text',serialize($this->getItemFilter())),
462  'mime_filter' => array('text', serialize($this->getMimeFilter())),
463  'creation_filter' => array('text', serialize($this->getCreationFilter()))
464  ));
465 
466 
467  // Write last query information
468  $ilDB->insert(
469  'usr_search',
470  array(
471  'usr_id' => array('integer',$this->usr_id),
472  'search_type' => array('integer',self::LAST_QUERY),
473  'query' => array('text',serialize($this->getQuery()))
474  )
475  );
476  }
477 
478  public function saveForAnonymous()
479  {
480  unset($_SESSION['usr_search_cache']);
481 
482  $_SESSION['usr_search_cache'][$this->search_type]['search_result'] = $this->search_result;
483  $_SESSION['usr_search_cache'][$this->search_type]['checked'] = $this->checked;
484  $_SESSION['usr_search_cache'][$this->search_type]['failed'] = $this->failed;
485  $_SESSION['usr_search_cache'][$this->search_type]['page'] = $this->page_number;
486  $_SESSION['usr_search_cache'][$this->search_type]['query'] = $this->getQuery();
487  $_SESSION['usr_search_cache'][$this->search_type]['root'] = $this->getRoot();
488  $_SESSION['usr_search_cache'][$this->search_type]['item_filter'] = $this->getItemFilter();
489  $_SESSION['usr_search_cache'][$this->search_type]['mime_filter'] = $this->getMimeFilter();
490  $_SESSION['usr_search_cache'][$this->search_type]['creation_filter'] = $this->getCreationFilter();
491 
492  $_SESSION['usr_search_cache'][self::LAST_QUERY]['query'] = $this->getQuery();
493 
494  return true;
495  }
496 
497 
504  private function read()
505  {
506  $this->failed = array();
507  $this->checked = array();
508  $this->search_result = array();
509  $this->page_number = 0;
510 
511  if ($this->isAnonymous()) {
512  return $this->readAnonymous();
513  }
514 
515  $query = "SELECT * FROM usr_search " .
516  "WHERE usr_id = " . $this->db->quote($this->usr_id, 'integer') . " " .
517  "AND search_type = " . $this->db->quote($this->search_type, 'integer');
518 
519  $res = $this->db->query($query);
520  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
521  $this->search_result = unserialize(stripslashes($row->search_result));
522  if (strlen($row->checked)) {
523  $this->checked = unserialize(stripslashes($row->checked));
524  }
525  if (strlen($row->failed)) {
526  $this->failed = unserialize(stripslashes($row->failed));
527  }
528  $this->page_number = $row->page;
529  $this->setQuery(unserialize($row->query));
530  $this->setRoot($row->root);
531  $this->setItemFilter(unserialize($row->item_filter));
532  $this->setCreationFilter(unserialize($row->creation_filter));
533  }
534  return true;
535  }
536 
540  private function readAnonymous()
541  {
542  $this->search_result = (array) $_SESSION['usr_search_cache'][$this->search_type]['search_result'];
543  $this->checked = (array) $_SESSION['usr_search_cache'][$this->search_type]['checked'];
544  $this->failed = (array) $_SESSION['usr_search_cache'][$this->search_type]['failed'];
545  $this->page_number = $_SESSION['usr_search_cache'][$this->search_type]['page_number'];
546 
547  $this->setQuery($_SESSION['usr_search_cache'][$this->search_type]['query']);
548  $this->setRoot((string) $_SESSION['usr_search_cache'][$this->search_type]['root']);
549  $this->setItemFilter((array) $_SESSION['usr_search_cache'][$this->search_type]['item_filter']);
550  $this->setMimeFilter((array) $_SESSION['usr_search_cache'][$this->search_type]['mime_filter']);
551  $this->setCreationFilter((array) $_SESSION['usr_search_cache'][$this->search_type]['creation_filter']);
552 
553  return true;
554  }
555 }
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
getUrlEncodedQuery()
Urlencode query for further use in e.g glossariers (highlighting off search terms).
$a_type
Definition: workflow.php:92
static _getInstance($a_usr_id)
Get singleton instance.
setResults($a_results)
Set results.
foreach($_POST as $key=> $value) $res
setRoot($a_root)
set root node of search
Create styles array
The data for the language used.
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.