00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00034 include_once('Services/Search/classes/class.ilUserSearchCache.php');
00035
00036 define('DEFAULT_SEARCH',0);
00037 define('ADVANCED_SEARCH',1);
00038 define('ADVANCED_MD_SEARCH',4);
00039
00040 class ilSearchResult
00041 {
00042 var $permission = 'visible';
00043
00044 var $user_id;
00045 var $entries = array();
00046 var $results = array();
00047 var $observers = array();
00048
00049 protected $search_cache = null;
00050 protected $offset = 0;
00051
00052
00053 var $ilias;
00054 var $ilAccess;
00055
00056
00057 var $limit_reached = false;
00058 var $result;
00059
00064 function ilSearchResult($a_user_id = 0)
00065 {
00066 global $ilias,$ilAccess,$ilDB,$ilUser;
00067
00068 $this->ilAccess =& $ilAccess;
00069 if($a_user_id)
00070 {
00071 $this->user_id = $a_user_id;
00072 }
00073 else
00074 {
00075 $this->user_id = $ilUser->getId();
00076 }
00077 $this->__initSearchSettingsObject();
00078 $this->initUserSearchCache();
00079
00080 $this->db =& $ilDB;
00081 }
00082
00086 function setRequiredPermission($a_permission)
00087 {
00088 $this->permission = $a_permission;
00089 }
00090
00091 function getRequiredPermission()
00092 {
00093 return $this->permission;
00094 }
00095
00096
00097 function setUserId($a_user_id)
00098 {
00099 $this->user_id = $a_user_id;
00100 }
00101 function getUserId()
00102 {
00103 return $this->user_id;
00104 }
00105
00106 function getEntries()
00107 {
00108 return $this->entries ? $this->entries : array();
00109 }
00110
00111 function isLimitReached()
00112 {
00113 return $this->limit_reached ? true : false;
00114 }
00115
00116 function setMaxHits($a_max_hits)
00117 {
00118 $this->max_hits = $a_max_hits;
00119 }
00120 function getMaxHits()
00121 {
00122 return $this->max_hits;
00123 }
00124
00132 public function isOffsetReached($a_counter)
00133 {
00134 return ($a_counter < $this->offset) ? false : true;
00135 }
00136
00147 function addEntry($a_obj_id,$a_type,$found,$a_child_id = 0)
00148 {
00149
00150 if(!$this->entries[$a_obj_id])
00151 {
00152 $this->entries[$a_obj_id]['obj_id'] = $a_obj_id;
00153 $this->entries[$a_obj_id]['type'] = $a_type;
00154 $this->entries[$a_obj_id]['found'] = $found;
00155
00156 if($a_child_id and $a_child_id != $a_obj_id)
00157 {
00158 $this->entries[$a_obj_id]['child'][$a_child_id] = $a_child_id;
00159 }
00160 }
00161 else
00162 {
00163
00164 if($a_child_id and $a_child_id != $a_obj_id)
00165 {
00166 $this->entries[$a_obj_id]['child'][$a_child_id] = $a_child_id;
00167 }
00168
00169
00170 $counter = 0;
00171 foreach($found as $position)
00172 {
00173 if($position)
00174 {
00175 $this->entries[$a_obj_id]['found'][$counter] = $position;
00176 }
00177 $counter++;
00178 }
00179 }
00180 return true;
00181 }
00182
00188 function numEntries()
00189 {
00190 return count($this->getEntries());
00191 }
00192
00199 function mergeEntries(&$result_obj)
00200 {
00201 foreach($result_obj->getEntries() as $entry)
00202 {
00203 $this->addEntry($entry['obj_id'],$entry['type'],$entry['found']);
00204 $this->__updateEntryChilds($entry['obj_id'],$entry['child']);
00205 }
00206 return true;
00207 }
00208
00216 function diffEntriesFromResult(&$result_obj)
00217 {
00218 $new_entries = $this->getEntries();
00219 $this->entries = array();
00220
00221
00222 foreach($this->search_cache->getCheckedItems() as $ref_id => $obj_id)
00223 {
00224 if(isset($new_entries[$obj_id]))
00225 {
00226 $this->addEntry($new_entries[$obj_id]['obj_id'],
00227 $new_entries[$obj_id]['type'],
00228 $new_entries[$obj_id]['found']);
00229 $this->__updateEntryChilds($new_entries[$obj_id]['obj_id'],
00230 $new_entries[$obj_id]['child']);
00231 }
00232 }
00233 }
00234
00241 function intersectEntries(&$result_obj)
00242 {
00243 $new_entries = $this->getEntries();
00244 $this->entries = array();
00245
00246 foreach($result_obj->getEntries() as $entry)
00247 {
00248 $obj_id = $entry['obj_id'];
00249 if(isset($new_entries[$obj_id]))
00250 {
00251 $this->addEntry($new_entries[$obj_id]['obj_id'],
00252 $new_entries[$obj_id]['type'],
00253 $new_entries[$obj_id]['found']);
00254
00255 $this->__updateEntryChilds($new_entries[$obj_id]['obj_id'],
00256 $new_entries[$obj_id]['child']);
00257 }
00258 }
00259 }
00260
00261
00271 function addResult($a_ref_id,$a_obj_id,$a_type)
00272 {
00273 $this->results[$a_ref_id]['ref_id'] = $a_ref_id;
00274 $this->results[$a_ref_id]['obj_id'] = $a_obj_id;
00275 $this->results[$a_ref_id]['type'] = $a_type;
00276 }
00277
00278 function getResults()
00279 {
00280 return $this->results ? $this->results : array();
00281 }
00282
00283 function getResultsByObjId()
00284 {
00285 $tmp_res = array();
00286 foreach($this->getResults() as $ref_id => $res_data)
00287 {
00288 $tmp_res[$res_data['obj_id']][] = $ref_id;
00289 }
00290 return $tmp_res ? $tmp_res : array();
00291 }
00292
00293
00300 function getUniqueResults()
00301 {
00302 $obj_ids = array();
00303 foreach($this->results as $result)
00304 {
00305 if(in_array($result['obj_id'],$obj_ids))
00306 {
00307 continue;
00308 }
00309 $obj_ids[] = $result['obj_id'];
00310 $objects[] = $result;
00311 }
00312 return $objects ? $objects : array();
00313 }
00314
00315 function getResultsForPresentation()
00316 {
00317 foreach($this->getResults() as $result)
00318 {
00319 switch($result['type'])
00320 {
00321
00322 case "sahs":
00323 case "lm":
00324 case "dbk":
00325 case "htlm":
00326 $type = "lres";
00327 break;
00328
00329 default:
00330 $type = $result['type'];
00331 break;
00332 }
00333 $title = ilObject::_lookupTitle($result['obj_id']);
00334 $description = ilObject::_lookupDescription($result['obj_id']);
00335
00336 $presentation_result[$type][] = array('ref_id' => $result['ref_id'],
00337 'title' => $title,
00338 'description' => $description,
00339 'type' => $result['type'],
00340 'obj_id' => $result['obj_id'],
00341 'child' => $result['child']);
00342 }
00343 return $presentation_result ? $presentation_result : array();
00344 }
00345
00358 public function filter($a_root_node,$check_and)
00359 {
00360 global $tree;
00361
00362
00363 $counter = 0;
00364 $offset_counter = 0;
00365 foreach($this->getEntries() as $entry)
00366 {
00367
00368 if($check_and and in_array(0,$entry['found']))
00369 {
00370 continue;
00371 }
00372
00373 $type = ilObject::_lookupType($entry['obj_id']);
00374 if($type == 'rolt' or $type == 'usr' or $type == 'role')
00375 {
00376 if($this->callListeners($entry['obj_id'],$entry))
00377 {
00378 $this->addResult($entry['obj_id'],$entry['obj_id'],$type);
00379 $counter += count($entry['child']);
00380
00381 if(++$counter > $this->getMaxHits())
00382 {
00383 $this->limit_reached = true;
00384 return true;
00385 }
00386 }
00387 continue;
00388 }
00389
00390 foreach(ilObject::_getAllReferences($entry['obj_id']) as $ref_id)
00391 {
00392
00393 if($this->search_cache->isFailed($ref_id))
00394 {
00395 continue;
00396 }
00397
00398 if($this->search_cache->isChecked($ref_id) and !$this->isOffsetReached($offset_counter))
00399 {
00400 ++$offset_counter;
00401 continue;
00402 }
00403
00404 $type = ilObject::_lookupType($ref_id, true);
00405 if($this->ilAccess->checkAccessOfUser($this->getUserId(),
00406 $this->getRequiredPermission(),
00407 '',
00408 $ref_id,
00409 $type,
00410 $entry['obj_id']))
00411 {
00412 if($a_root_node == ROOT_FOLDER_ID or $tree->isGrandChild($a_root_node,$ref_id))
00413 {
00414
00415 if($this->callListeners($ref_id,$entry))
00416 {
00417 $this->addResult($ref_id,$entry['obj_id'],$type);
00418 $this->search_cache->appendToChecked($ref_id,$entry['obj_id']);
00419 $this->__updateResultChilds($ref_id,$entry['child']);
00420
00421 $counter++;
00422 $offset_counter++;
00423
00424
00425 if($counter >= $this->getMaxHits())
00426 {
00427 $this->limit_reached = true;
00428 $this->search_cache->setResults($this->results);
00429 return true;
00430 }
00431 }
00432 }
00433 continue;
00434 }
00435 $this->search_cache->appendToFailed($ref_id);
00436 }
00437 }
00438 $this->search_cache->setResults($this->results);
00439 return false;
00440 }
00441
00447 function filterResults($a_root_node)
00448 {
00449 global $tree;
00450
00451 $tmp_results = $this->getResults();
00452 $this->results = array();
00453 foreach($tmp_results as $result)
00454 {
00455 if($tree->isGrandChild($a_root_node,$result['ref_id']) and $tree->isInTree($result['ref_id']))
00456 {
00457 $this->addResult($result['ref_id'],$result['obj_id'],$result['type']);
00458 $this->__updateResultChilds($result['ref_id'],$result['child']);
00459 }
00460 }
00461
00462 return true;
00463 }
00464
00465
00472 function save($a_type = DEFAULT_SEARCH)
00473 {
00474 $this->search_cache->save();
00475 return false;
00476 }
00483 function read($a_type = DEFAULT_SEARCH)
00484 {
00485 $this->results = $this->search_cache->getResults();
00486 }
00487
00488
00496 function __updateEntryChilds($a_obj_id,$a_childs)
00497 {
00498 if($this->entries[$a_obj_id] and is_array($a_childs))
00499 {
00500 foreach($a_childs as $child_id)
00501 {
00502 if($child_id)
00503 {
00504 $this->entries[$a_obj_id]['child'][$child_id] = $child_id;
00505 }
00506 }
00507 return true;
00508 }
00509 return false;
00510 }
00518 function __updateResultChilds($a_ref_id,$a_childs)
00519 {
00520 if($this->results[$a_ref_id] and is_array($a_childs))
00521 {
00522 foreach($a_childs as $child_id)
00523 {
00524 $this->results[$a_ref_id]['child'][$child_id] = $child_id;
00525 }
00526 return true;
00527 }
00528 return false;
00529 }
00530
00531
00532
00533 function __initSearchSettingsObject()
00534 {
00535 include_once 'Services/Search/classes/class.ilSearchSettings.php';
00536
00537 $this->search_settings = new ilSearchSettings();
00538 $this->setMaxHits($this->search_settings->getMaxHits());
00539 #$this->setMaxHits(2);
00540 }
00541
00548 private function initUserSearchCache()
00549 {
00550 include_once('Services/Search/classes/class.ilUserSearchCache.php');
00551 $this->search_cache = ilUserSearchCache::_getInstance($this->getUserId());
00552 $this->offset = $this->getMaxHits() * ($this->search_cache->getResultPageNumber() - 1) ;
00553 }
00554
00564 function addObserver(&$a_class,$a_method)
00565 {
00566 $this->observers[] = array('class' => $a_class,
00567 'method' => $a_method);
00568 return true;
00569 }
00570 function callListeners($a_ref_id,&$a_data)
00571 {
00572 foreach($this->observers as $observer)
00573 {
00574 $class =& $observer['class'];
00575 $method = $observer['method'];
00576
00577 if(!$class->$method($a_ref_id,$a_data))
00578 {
00579 return false;
00580 }
00581 }
00582 return true;
00583 }
00584 }
00585 ?>