00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 define('DEFAULT_SEARCH',0);
00024 define('ADVANCED_SEARCH',1);
00025
00036 class ilSearchResult
00037 {
00038 var $permission = 'visible';
00039
00040 var $user_id;
00041 var $entries = array();
00042 var $results = array();
00043 var $observers = array();
00044
00045
00046 var $ilias;
00047 var $ilAccess;
00048
00049
00050 var $limit_reached = false;
00051
00052
00053 var $result;
00058 function ilSearchResult($a_user_id = 0)
00059 {
00060 global $ilias,$ilAccess,$ilDB,$ilUser;
00061
00062 $this->ilAccess =& $ilAccess;
00063 if($a_user_id)
00064 {
00065 $this->user_id = $a_user_id;
00066 }
00067 else
00068 {
00069 $this->user_id = $ilUser->getId();
00070 }
00071
00072 $this->__initSearchSettingsObject();
00073 $this->setMaxHits($this->search_settings->getMaxHits());
00074
00075 $this->db =& $ilDB;
00076 }
00077
00081 function setRequiredPermission($a_permission)
00082 {
00083 $this->permission = $a_permission;
00084 }
00085
00086 function getRequiredPermission()
00087 {
00088 return $this->permission;
00089 }
00090
00091
00092 function setUserId($a_user_id)
00093 {
00094 $this->user_id = $a_user_id;
00095 }
00096 function getUserId()
00097 {
00098 return $this->user_id;
00099 }
00100
00101 function getEntries()
00102 {
00103 return $this->entries ? $this->entries : array();
00104 }
00105
00106 function isLimitReached()
00107 {
00108 return $this->limit_reached ? true : false;
00109 }
00110
00111 function setMaxHits($a_max_hits)
00112 {
00113 $this->max_hits = $a_max_hits;
00114 }
00115 function getMaxHits()
00116 {
00117 return $this->max_hits;
00118 }
00119
00120
00131 function addEntry($a_obj_id,$a_type,$found,$a_child_id = 0)
00132 {
00133
00134 if(!$this->entries[$a_obj_id])
00135 {
00136 $this->entries[$a_obj_id]['obj_id'] = $a_obj_id;
00137 $this->entries[$a_obj_id]['type'] = $a_type;
00138 $this->entries[$a_obj_id]['found'] = $found;
00139
00140 if($a_child_id and $a_child_id != $a_obj_id)
00141 {
00142 $this->entries[$a_obj_id]['child'][$a_child_id] = $a_child_id;
00143 }
00144 }
00145 else
00146 {
00147
00148 if($a_child_id and $a_child_id != $a_obj_id)
00149 {
00150 $this->entries[$a_obj_id]['child'][$a_child_id] = $a_child_id;
00151 }
00152
00153
00154 $counter = 0;
00155 foreach($found as $position)
00156 {
00157 if($position)
00158 {
00159 $this->entries[$a_obj_id]['found'][$counter] = $position;
00160 }
00161 $counter++;
00162 }
00163 }
00164 return true;
00165 }
00166
00172 function numEntries()
00173 {
00174 return count($this->getEntries());
00175 }
00176
00183 function mergeEntries(&$result_obj)
00184 {
00185 foreach($result_obj->getEntries() as $entry)
00186 {
00187 $this->addEntry($entry['obj_id'],$entry['type'],$entry['found']);
00188 $this->__updateEntryChilds($entry['obj_id'],$entry['child']);
00189 }
00190 return true;
00191 }
00192
00200 function diffEntriesFromResult(&$result_obj)
00201 {
00202 $new_entries = $this->getEntries();
00203 $this->entries = array();
00204
00205 foreach($result_obj->getResults() as $result)
00206 {
00207 $obj_id = $result['obj_id'];
00208 if(isset($new_entries[$obj_id]))
00209 {
00210 $this->addEntry($new_entries[$obj_id]['obj_id'],
00211 $new_entries[$obj_id]['type'],
00212 $new_entries[$obj_id]['found']);
00213 $this->__updateEntryChilds($new_entries[$obj_id]['obj_id'],
00214 $new_entries[$obj_id]['child']);
00215 }
00216 }
00217 }
00218
00225 function intersectEntries(&$result_obj)
00226 {
00227 $new_entries = $this->getEntries();
00228 $this->entries = array();
00229
00230 foreach($result_obj->getEntries() as $entry)
00231 {
00232 $obj_id = $entry['obj_id'];
00233 if(isset($new_entries[$obj_id]))
00234 {
00235 $this->addEntry($new_entries[$obj_id]['obj_id'],
00236 $new_entries[$obj_id]['type'],
00237 $new_entries[$obj_id]['found']);
00238
00239 $this->__updateEntryChilds($new_entries[$obj_id]['obj_id'],
00240 $new_entries[$obj_id]['child']);
00241 }
00242 }
00243 }
00244
00245
00255 function addResult($a_ref_id,$a_obj_id,$a_type)
00256 {
00257 $this->results[$a_ref_id]['ref_id'] = $a_ref_id;
00258 $this->results[$a_ref_id]['obj_id'] = $a_obj_id;
00259 $this->results[$a_ref_id]['type'] = $a_type;
00260 }
00261
00262 function getResults()
00263 {
00264 return $this->results ? $this->results : array();
00265 }
00266
00267
00274 function getUniqueResults()
00275 {
00276 $obj_ids = array();
00277 foreach($this->results as $result)
00278 {
00279 if(in_array($result['obj_id'],$obj_ids))
00280 {
00281 continue;
00282 }
00283 $obj_ids[] = $result['obj_id'];
00284 $objects[] = $result;
00285 }
00286 return $objects ? $objects : array();
00287 }
00288
00289 function getResultsForPresentation()
00290 {
00291 foreach($this->getResults() as $result)
00292 {
00293 switch($result['type'])
00294 {
00295
00296 case "sahs":
00297 case "lm":
00298 case "dbk":
00299 case "htlm":
00300 $type = "lres";
00301 break;
00302
00303 default:
00304 $type = $result['type'];
00305 break;
00306 }
00307 $title = ilObject::_lookupTitle($result['obj_id']);
00308 $description = ilObject::_lookupDescription($result['obj_id']);
00309
00310 $presentation_result[$type][] = array('ref_id' => $result['ref_id'],
00311 'title' => $title,
00312 'description' => $description,
00313 'type' => $result['type'],
00314 'obj_id' => $result['obj_id'],
00315 'child' => $result['child']);
00316 }
00317 return $presentation_result ? $presentation_result : array();
00318 }
00319
00320 function filter($a_root_node,$check_and)
00321 {
00322 global $tree;
00323
00324 $this->__initSearchSettingsObject();
00325
00326
00327 $counter = 0;
00328 foreach($this->getEntries() as $entry)
00329 {
00330 if($check_and and in_array(0,$entry['found']))
00331 {
00332 continue;
00333 }
00334
00335 $type = ilObject::_lookupType($entry['obj_id']);
00336 if($type == 'rolt' or $type == 'usr' or $type == 'role')
00337 {
00338 if($this->callListeners($entry['obj_id'],$entry))
00339 {
00340 $this->addResult($entry['obj_id'],$entry['obj_id'],$type);
00341 $counter += count($entry['child']);
00342
00343 if(++$counter > $this->getMaxHits())
00344 {
00345 $this->limit_reached = true;
00346 return true;
00347 }
00348 }
00349 continue;
00350 }
00351
00352
00353 foreach(ilObject::_getAllReferences($entry['obj_id']) as $ref_id)
00354 {
00355 $type = ilObject::_lookupType($ref_id, true);
00356 if($this->ilAccess->checkAccessOfUser($this->getUserId(),
00357 $this->getRequiredPermission(),
00358 '',
00359 $ref_id,
00360 $type,
00361 $entry['obj_id']))
00362 {
00363 if($a_root_node == ROOT_FOLDER_ID or $tree->isGrandChild($a_root_node,$ref_id))
00364 {
00365
00366 if($this->callListeners($ref_id,$entry))
00367 {
00368 $this->addResult($ref_id,$entry['obj_id'],$type);
00369 $this->__updateResultChilds($ref_id,$entry['child']);
00370
00371 $counter += count($entry['child']);
00372
00373 if(++$counter > $this->getMaxHits())
00374 {
00375 $this->limit_reached = true;
00376 return true;
00377 }
00378 }
00379 }
00380 }
00381 }
00382 }
00383 return false;
00384 }
00385
00386
00392 function filterResults($a_root_node)
00393 {
00394 global $tree;
00395
00396 $tmp_results = $this->getResults();
00397 $this->results = array();
00398 foreach($tmp_results as $result)
00399 {
00400 if($tree->isGrandChild($a_root_node,$result['ref_id']) and $tree->isInTree($result['ref_id']))
00401 {
00402 $this->addResult($result['ref_id'],$result['obj_id'],$result['type']);
00403 $this->__updateResultChilds($result['ref_id'],$result['child']);
00404 }
00405 }
00406
00407 return true;
00408 }
00409
00410
00417 function save($a_type = DEFAULT_SEARCH)
00418 {
00419 if ($this->getUserId() and $this->getUserId() != ANONYMOUS_USER_ID)
00420 {
00421 $query = "DELETE FROM usr_search WHERE usr_id = '".$this->getUserId()."' ".
00422 "AND search_type = '".$a_type."'";
00423 $this->db->query($query);
00424
00425
00426 $query = "INSERT INTO usr_search ".
00427 "VALUES('".$this->getUserId()."','".addslashes(serialize($this->getResults()))."','".$a_type."')";
00428
00429 $res = $this->db->query($query);
00430
00431 return true;
00432 }
00433
00434 return false;
00435 }
00442 function read($a_type = DEFAULT_SEARCH)
00443 {
00444 if($this->getUserId() and $this->getUserId() != ANONYMOUS_USER_ID)
00445 {
00446 $query = "SELECT search_result FROM usr_search ".
00447 "WHERE usr_id = '".$this->getUserId()."' ".
00448 "AND search_type = '".$a_type."'";
00449
00450 $res = $this->db->query($query);
00451
00452 if($res->numRows())
00453 {
00454 $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
00455 $this->results = unserialize(stripslashes($row->search_result));
00456 }
00457 }
00458 }
00459
00460
00468 function __updateEntryChilds($a_obj_id,$a_childs)
00469 {
00470 if($this->entries[$a_obj_id] and is_array($a_childs))
00471 {
00472 foreach($a_childs as $child_id)
00473 {
00474 if($child_id)
00475 {
00476 $this->entries[$a_obj_id]['child'][$child_id] = $child_id;
00477 }
00478 }
00479 return true;
00480 }
00481 return false;
00482 }
00490 function __updateResultChilds($a_ref_id,$a_childs)
00491 {
00492 if($this->results[$a_ref_id] and is_array($a_childs))
00493 {
00494 foreach($a_childs as $child_id)
00495 {
00496 $this->results[$a_ref_id]['child'][$child_id] = $child_id;
00497 }
00498 return true;
00499 }
00500 return false;
00501 }
00502
00503
00504
00505 function __initSearchSettingsObject()
00506 {
00507 include_once 'Services/Search/classes/class.ilSearchSettings.php';
00508
00509 $this->search_settings = new ilSearchSettings();
00510 }
00511
00521 function addObserver(&$a_class,$a_method)
00522 {
00523 $this->observers[] = array('class' => $a_class,
00524 'method' => $a_method);
00525 return true;
00526 }
00527 function callListeners($a_ref_id,&$a_data)
00528 {
00529 foreach($this->observers as $observer)
00530 {
00531 $class =& $observer['class'];
00532 $method = $observer['method'];
00533
00534 if(!$class->$method($a_ref_id,$a_data))
00535 {
00536 return false;
00537 }
00538 }
00539 return true;
00540 }
00541 }
00542 ?>