ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 $DIC;
83 
84  $ilDB = $DIC['ilDB'];
85 
86  if ($a_usr_id == ANONYMOUS_USER_ID) {
87  $this->isAnonymous = true;
88  }
89 
90  $this->db = $ilDB;
91  $this->usr_id = $a_usr_id;
92  $this->search_type = self::DEFAULT_SEARCH;
93  $this->read();
94  }
95 
104  public static function _getInstance($a_usr_id)
105  {
106  if (is_object(self::$instance) and self::$instance) {
107  return self::$instance;
108  }
109  return self::$instance = new ilUserSearchCache($a_usr_id);
110  }
111 
116  public function isAnonymous()
117  {
118  return $this->isAnonymous;
119  }
120 
129  public function switchSearchType($a_type)
130  {
131  $this->search_type = $a_type;
132  $this->read();
133  return true;
134  }
135 
142  public function getResults()
143  {
144  return $this->search_result ? $this->search_result : array();
145  }
146 
154  public function setResults($a_results)
155  {
156  $this->search_result = $a_results;
157  }
158 
166  public function addResult($a_result_item)
167  {
168  $this->search_result[$a_result_item['ref_id']]['ref_id'] = $a_result_item['ref_id'];
169  $this->search_result[$a_result_item['ref_id']]['obj_id'] = $a_result_item['obj_id'];
170  $this->search_result[$a_result_item['ref_id']]['type'] = $a_result_item['type'];
171  return true;
172  }
173 
181  public function appendToFailed($a_ref_id)
182  {
183  $this->failed[$a_ref_id] = $a_ref_id;
184  }
185 
193  public function isFailed($a_ref_id)
194  {
195  return in_array($a_ref_id, $this->failed) ? true : false;
196  }
197 
206  public function appendToChecked($a_ref_id, $a_obj_id)
207  {
208  $this->checked[$a_ref_id] = $a_obj_id;
209  }
210 
218  public function isChecked($a_ref_id)
219  {
220  return array_key_exists($a_ref_id, $this->checked) and $this->checked[$a_ref_id];
221  }
222 
230  public function getCheckedItems()
231  {
232  return $this->checked ? $this->checked : array();
233  }
234 
241  public function setResultPageNumber($a_number)
242  {
243  if ($a_number) {
244  $this->page_number = $a_number;
245  }
246  }
247 
254  public function getResultPageNumber()
255  {
256  return $this->page_number ? $this->page_number : 1;
257  }
258 
264  public function setQuery($a_query)
265  {
266  $this->query = $a_query;
267  }
268 
274  public function getQuery()
275  {
276  return $this->query;
277  }
278 
283  public function getUrlEncodedQuery()
284  {
285  if (is_array($this->getQuery())) {
286  $query = $this->getQuery();
287 
288  return urlencode(str_replace('"', '.', $query['lom_content']));
289  }
290  return urlencode(str_replace('"', '.', $this->getQuery()));
291  }
292 
298  public function setRoot($a_root)
299  {
300  $this->root = $a_root;
301  }
302 
308  public function getRoot()
309  {
310  return $this->root ? $this->root : ROOT_FOLDER_ID;
311  }
312 
313  public function setItemFilter($a_filter)
314  {
315  $this->item_filter = $a_filter;
316  }
317 
318  public function getItemFilter()
319  {
320  return (array) $this->item_filter;
321  }
322 
323  public function setMimeFilter($a_filter)
324  {
325  $this->mime_filter = $a_filter;
326  }
327 
328  public function getMimeFilter()
329  {
330  return (array) $this->mime_filter;
331  }
332 
333  // begin-patch create_date
334  public function setCreationFilter($a_filter)
335  {
336  $this->creation_filter = $a_filter;
337  }
338 
339  public function getCreationFilter()
340  {
341  return $this->creation_filter;
342  }
343  // end-patch create_date
344 
345 
351  public function deleteCachedEntries()
352  {
353  global $DIC;
354 
355  $ilDB = $DIC['ilDB'];
356 
357  if ($this->isAnonymous()) {
358  return $this->deleteCachedEntriesAnonymous();
359  }
360 
361 
362  $query = "SELECT COUNT(*) num FROM usr_search " .
363  "WHERE usr_id = " . $ilDB->quote($this->usr_id, 'integer') . " " .
364  "AND search_type = " . $ilDB->quote($this->search_type, 'integer');
365  $res = $ilDB->query($query);
367 
368  if ($row->num > 0) {
369  $ilDB->update(
370  'usr_search',
371  array(
372  'search_result' => array('clob',serialize(array(0))),
373  'checked' => array('clob',serialize(array(0))),
374  'failed' => array('clob',serialize(array(0))),
375  'page' => array('integer',0)),
376  array(
377  'usr_id' => array('integer',(int) $this->usr_id),
378  'search_type' => array('integer',(int) $this->search_type)
379  )
380  );
381  } else {
382  $ilDB->insert(
383  'usr_search',
384  array(
385  'search_result' => array('clob',serialize(array(0))),
386  'checked' => array('clob',serialize(array(0))),
387  'failed' => array('clob',serialize(array(0))),
388  'page' => array('integer',0),
389  'usr_id' => array('integer',(int) $this->usr_id),
390  'search_type' => array('integer',(int) $this->search_type)
391  )
392  );
393  }
394 
395  $this->setResultPageNumber(1);
396  $this->search_result = array();
397  $this->checked = array();
398  $this->failed = array();
399  }
400 
406  {
407  $this->setResultPageNumber(1);
408  $this->search_result = array();
409  $this->checked = array();
410  $this->failed = array();
411 
412  return true;
413  }
414 
415 
416 
423  public function delete()
424  {
425  global $DIC;
426 
427  $ilDB = $DIC['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 $DIC;
447 
448  $ilDB = $DIC['ilDB'];
449 
450  if ($this->isAnonymous()) {
451  return $this->saveForAnonymous();
452  }
453 
454  $query = "DELETE FROM usr_search " .
455  "WHERE usr_id = " . $ilDB->quote($this->usr_id, 'integer') . " " .
456  "AND ( search_type = " . $ilDB->quote($this->search_type, 'integer') . ' ' .
457  "OR search_type = " . $ilDB->quote(self::LAST_QUERY, 'integer') . ')';
458  $res = $ilDB->manipulate($query);
459 
460  $ilDB->insert('usr_search', array(
461  'usr_id' => array('integer',(int) $this->usr_id),
462  'search_result' => array('clob',serialize($this->search_result)),
463  'checked' => array('clob',serialize($this->checked)),
464  'failed' => array('clob',serialize($this->failed)),
465  'page' => array('integer',(int) $this->page_number),
466  'search_type' => array('integer',(int) $this->search_type),
467  'query' => array('clob',serialize($this->getQuery())),
468  'root' => array('integer',$this->getRoot()),
469  'item_filter' => array('text',serialize($this->getItemFilter())),
470  'mime_filter' => array('text', serialize($this->getMimeFilter())),
471  'creation_filter' => array('text', serialize($this->getCreationFilter()))
472  ));
473 
474 
475  // Write last query information
476  $ilDB->insert(
477  'usr_search',
478  array(
479  'usr_id' => array('integer',$this->usr_id),
480  'search_type' => array('integer',self::LAST_QUERY),
481  'query' => array('text',serialize($this->getQuery()))
482  )
483  );
484  }
485 
486  public function saveForAnonymous()
487  {
488  unset($_SESSION['usr_search_cache']);
489 
490  $_SESSION['usr_search_cache'][$this->search_type]['search_result'] = $this->search_result;
491  $_SESSION['usr_search_cache'][$this->search_type]['checked'] = $this->checked;
492  $_SESSION['usr_search_cache'][$this->search_type]['failed'] = $this->failed;
493  $_SESSION['usr_search_cache'][$this->search_type]['page'] = $this->page_number;
494  $_SESSION['usr_search_cache'][$this->search_type]['query'] = $this->getQuery();
495  $_SESSION['usr_search_cache'][$this->search_type]['root'] = $this->getRoot();
496  $_SESSION['usr_search_cache'][$this->search_type]['item_filter'] = $this->getItemFilter();
497  $_SESSION['usr_search_cache'][$this->search_type]['mime_filter'] = $this->getMimeFilter();
498  $_SESSION['usr_search_cache'][$this->search_type]['creation_filter'] = $this->getCreationFilter();
499 
500  $_SESSION['usr_search_cache'][self::LAST_QUERY]['query'] = $this->getQuery();
501 
502  return true;
503  }
504 
505 
512  private function read()
513  {
514  $this->failed = array();
515  $this->checked = array();
516  $this->search_result = array();
517  $this->page_number = 0;
518 
519  if ($this->isAnonymous()) {
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(ilDBConstants::FETCHMODE_OBJECT)) {
529  $this->search_result = unserialize(stripslashes($row->search_result));
530  if (strlen($row->checked)) {
531  $this->checked = unserialize(stripslashes($row->checked));
532  }
533  if (strlen($row->failed)) {
534  $this->failed = unserialize(stripslashes($row->failed));
535  }
536  $this->page_number = $row->page;
537  $this->setQuery(unserialize($row->query));
538  $this->setRoot($row->root);
539  $this->setItemFilter(unserialize($row->item_filter));
540  $this->setCreationFilter(unserialize($row->creation_filter));
541  }
542  return true;
543  }
544 
548  private function readAnonymous()
549  {
550  $this->search_result = (array) $_SESSION['usr_search_cache'][$this->search_type]['search_result'];
551  $this->checked = (array) $_SESSION['usr_search_cache'][$this->search_type]['checked'];
552  $this->failed = (array) $_SESSION['usr_search_cache'][$this->search_type]['failed'];
553  $this->page_number = $_SESSION['usr_search_cache'][$this->search_type]['page_number'];
554 
555  $this->setQuery($_SESSION['usr_search_cache'][$this->search_type]['query']);
556  $this->setRoot((string) $_SESSION['usr_search_cache'][$this->search_type]['root']);
557  $this->setItemFilter((array) $_SESSION['usr_search_cache'][$this->search_type]['item_filter']);
558  $this->setMimeFilter((array) $_SESSION['usr_search_cache'][$this->search_type]['mime_filter']);
559  $this->setCreationFilter((array) $_SESSION['usr_search_cache'][$this->search_type]['creation_filter']);
560 
561  return true;
562  }
563 }
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"]
global $DIC
Definition: saml.php:7
__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
$row
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.