ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSearchGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once 'Services/Search/classes/class.ilSearchBaseGUI.php';
5 
6 define('SEARCH_FAST',1);
7 define('SEARCH_DETAILS',2);
8 define('SEARCH_AND','and');
9 define('SEARCH_OR','or');
10 
11 
27 {
28  protected $search_cache = null;
29 
32  var $string;
33  var $type;
34 
35 
40  function ilSearchGUI()
41  {
42  global $ilUser, $lng;
43 
44  $lng->loadLanguageModule("search");
45 
46  $this->obj_types = array (
47  "lms" => $lng->txt("learning_resources"),
48  "glo" => $lng->txt("objs_glo"),
49  "wiki" => $lng->txt("objs_wiki"),
50  "mcst" => $lng->txt("objs_mcst"),
51  "fil" => $lng->txt("objs_file"),
52  "frm" => $lng->txt("objs_frm"),
53  "exc" => $lng->txt("objs_exc"),
54  "tst" => $lng->txt("search_tst_svy"),
55  "mep" => $lng->txt("objs_mep")
56  );
57 
58  // put form values into "old" post variables
59  $this->initStandardSearchForm();
60  $this->form->checkInput();
61  reset($this->obj_types);
62 
63  $new_search = isset($_POST['cmd']['performSearch']) ? true : false;
64 
65  foreach($this->obj_types as $k => $t)
66  {
67  $_POST["search"]["details"][$k] = $_POST[$k];
68  }
69  $_POST["search"]["string"] = $_POST["term"];
70  $_POST["search"]["combination"] = $_POST["combination"];
71  $_POST["search"]["type"] = $_POST["type"];
72  $_SESSION['search_root'] = $_POST["area"];
73 
74  $this->root_node = $_SESSION['search_root'] ? $_SESSION['search_root'] : ROOT_FOLDER_ID;
75  $this->setType($_POST['search']['type'] ? $_POST['search']['type'] : $_SESSION['search']['type']);
76  $this->setCombination($_POST['search']['combination'] ? $_POST['search']['combination'] : $_SESSION['search']['combination']);
77  $this->setString($_POST['search']['string'] ? $_POST['search']['string'] : $_SESSION['search']['string']);
78  #$this->setDetails($_POST['search']['details'] ? $_POST['search']['details'] : $_SESSION['search']['details']);
79  $this->setDetails($new_search ? $_POST['search']['details'] : $_SESSION['search']['details']);
80 
82  }
83 
84 
89  function setType($a_type)
90  {
91  $_SESSION['search']['type'] = $this->type = $a_type;
92  }
93  function getType()
94  {
95  return $this->type ? $this->type : SEARCH_FAST;
96  }
101  function setCombination($a_combination)
102  {
103  $_SESSION['search']['combination'] = $this->combination = $a_combination;
104  }
105  function getCombination()
106  {
107  return $this->combination ? $this->combination : SEARCH_OR;
108  }
113  function setString($a_str)
114  {
115  $_SESSION['search']['string'] = $this->string = $a_str;
116  }
117  function getString()
118  {
119  return $this->string;
120  }
125  function setDetails($a_details)
126  {
127  $_SESSION['search']['details'] = $this->details = $a_details;
128  }
129  function getDetails()
130  {
131  return $this->details ? $this->details : array();
132  }
133 
134 
135  function getRootNode()
136  {
137  return $this->root_node ? $this->root_node : ROOT_FOLDER_ID;
138  }
139  function setRootNode($a_node_id)
140  {
141  $_SESSION['search_root'] = $this->root_node = $a_node_id;
142  }
143 
148  function &executeCommand()
149  {
150  global $rbacsystem, $ilCtrl;
151 
152 
153  $next_class = $this->ctrl->getNextClass($this);
154  $cmd = $this->ctrl->getCmd();
155 
156  switch($next_class)
157  {
158  case "ilpropertyformgui":
159  $this->initStandardSearchForm();
160  $this->prepareOutput();
161  $ilCtrl->setReturn($this, "");
162  return $ilCtrl->forwardCommand($this->form);
163  break;
164 
165  case 'ilobjectcopygui':
166  $this->prepareOutput();
167  include_once './Services/Object/classes/class.ilObjectCopyGUI.php';
168  $cp = new ilObjectCopyGUI($this);
169  $this->ctrl->forwardCommand($cp);
170  break;
171 
172  default:
173  $this->initUserSearchCache();
174  if(!$cmd)
175  {
176  $cmd = "showSavedResults";
177  }
178  $this->prepareOutput();
179  $this->handleCommand($cmd);
180  break;
181  }
182  return true;
183  }
184 
185  function remoteSearch()
186  {
187  $this->setString(ilUtil::stripSlashes($_POST['queryString']));
188  $this->setRootNode((int) $_POST['root_id']);
189  $this->performSearch();
190 
191  }
192 
196  function autoComplete()
197  {
198  $q = $_REQUEST["query"];
199  include_once("./Services/Search/classes/class.ilSearchAutoComplete.php");
200  $list = ilSearchAutoComplete::getList($q);
201  echo $list;
202  exit;
203 
204  }
205 
206  function showSearch()
207  {
208  global $ilLocator, $ilCtrl;
209 
210 
211  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.search.html','Services/Search');
212 
213  $this->initStandardSearchForm();
214  $this->tpl->setVariable("FORM", $this->form->getHTML());
215 
216  return true;
217  }
218 
219  function showSelectRoot()
220  {
221  global $tree;
222 
223  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.search_root_selector.html','Services/Search');
224 
225  include_once 'Services/Search/classes/class.ilSearchRootSelector.php';
226 
227  ilUtil::sendInfo($this->lng->txt('search_area_info'));
228 
229  $exp = new ilSearchRootSelector($this->ctrl->getLinkTarget($this,'showSelectRoot'));
230  $exp->setExpand($_GET["search_root_expand"] ? $_GET["search_root_expand"] : $tree->readRootId());
231  $exp->setExpandTarget($this->ctrl->getLinkTarget($this,'showSelectRoot'));
232 
233  // build html-output
234  $exp->setOutput(0);
235 
236  $this->tpl->setVariable("EXPLORER",$exp->getOutput());
237  }
238 
239  function selectRoot()
240  {
241  $this->setRootNode((int) $_GET['root_id']);
242  $this->showSavedResults();
243 
244  return true;
245  }
246 
250  public function initStandardSearchForm()
251  {
252  global $lng, $ilCtrl;
253 
254  include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
255  $this->form = new ilPropertyFormGUI();
256 
257  // search term
258  $ti = new ilTextInputGUI($lng->txt("search_search_term"), "term");
259  $ti->setMaxLength(200);
260  $ti->setSize(30);
261  $ti->setValue($this->getString());
262  $dsSchema = array("resultsList" => 'response.results',
263  "fields" => array('term'));
264  $ti->setDataSource($ilCtrl->getLinkTarget($this, "autoComplete", "", false, false));
265  $ti->setDataSourceSchema($dsSchema);
266  $ti->setDataSourceResultFormat($dsFormatCallback);
267  $ti->setDataSourceDelimiter($dsDelimiter);
268  $this->form->addItem($ti);
269 
270  // term combination
271  $radg = new ilRadioGroupInputGUI($lng->txt("search_term_combination"),
272  "combination");
273  $radg->setValue(($this->getCombination() == SEARCH_AND) ? "and" : "or");
274  $op1 = new ilRadioOption($lng->txt("search_any_word"), "or");
275  $radg->addOption($op1);
276  $op2 = new ilRadioOption($lng->txt("search_all_words"), "and");
277  $radg->addOption($op2);
278  $this->form->addItem($radg);
279 
280  // search area
281  include_once("./Services/Form/classes/class.ilRepositorySelectorInputGUI.php");
282  $ti = new ilRepositorySelectorInputGUI($lng->txt("search_area"), "area");
283  $ti->setSelectText($lng->txt("search_select_search_area"));
284  $this->form->addItem($ti);
285  $ti->readFromSession();
286 
287  // search type
288  $radg = new ilRadioGroupInputGUI($lng->txt("search_type"), "type");
289  $radg->setValue($this->getType() == SEARCH_FAST ? SEARCH_FAST : SEARCH_DETAILS);
290  $op1 = new ilRadioOption($lng->txt("search_fast_info"), SEARCH_FAST);
291  $radg->addOption($op1);
292  $op2 = new ilRadioOption($lng->txt("search_details_info"), SEARCH_DETAILS);
293 
294  // resource types
295  $details = $this->getDetails();
296  reset($this->obj_types);
297  foreach ($this->obj_types as $k => $t)
298  {
299  $cb = new ilCheckboxInputGUI($t, $k);
300  $cb->setChecked($details[$k]);
301  $op2->addSubItem($cb);
302  }
303 
304  $radg->addOption($op2);
305  $this->form->addItem($radg);
306 
307 
308  // search command
309  $this->form->addCommandButton("performSearch", $lng->txt("search"));
310 
311  $this->form->setTitle($lng->txt("search"));
312  $this->form->setFormAction($ilCtrl->getFormAction($this,'performSearch'));
313 
314  }
315 
316  function showSavedResults()
317  {
318  global $ilUser;
319 
320  // Read old result sets
321  include_once 'Services/Search/classes/class.ilSearchResult.php';
322 
323  $result_obj = new ilSearchResult($ilUser->getId());
324  $result_obj->read();
325  $result_obj->filterResults($this->getRootNode());
326 
327  $this->showSearch();
328 
329  // Show them
330  if(count($result_obj->getResults()))
331  {
332  $this->addPager($result_obj,'max_page');
333 
334  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
336  $presentation->setResults($result_obj->getResultsForPresentation());
337  $presentation->setSubitemIds($result_obj->getSubitemIds());
338  $presentation->setPreviousNext($this->prev_link, $this->next_link);
339  #$presentation->setSearcher($searcher);
340 
341  if($presentation->render())
342  {
343 // $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML());
344  $this->tpl->setVariable('RESULTS_TABLE',$presentation->getHTML(true));
345  }
346  }
347 
348  return true;
349  }
350 
351  function searchInResults()
352  {
353  $this->search_mode = 'in_results';
354  $this->search_cache->setResultPageNumber(1);
355  unset($_SESSION['max_page']);
356  $this->performSearch();
357 
358  return true;
359  }
360 
361 
365  function performSearch()
366  {
367  global $ilUser;
368 
369  if(!isset($_GET['page_number']) and $this->search_mode != 'in_results' )
370  {
371  unset($_SESSION['max_page']);
372  $this->search_cache->deleteCachedEntries();
373  }
374 
375  if($this->getType() == SEARCH_DETAILS and !$this->getDetails())
376  {
377  ilUtil::sendInfo($this->lng->txt('search_choose_object_type'));
378  $this->showSearch();
379 
380  return false;
381  }
382 
383  // Step 1: parse query string
384  if(!is_object($query_parser =& $this->__parseQueryString()))
385  {
386  ilUtil::sendInfo($query_parser);
387  $this->showSearch();
388 
389  return false;
390  }
391  // Step 2: perform object search. Get an ObjectSearch object via factory. Depends on fulltext or like search type.
392  $result =& $this->__searchObjects($query_parser);
393 
394  // Step 3: perform meta keyword search. Get an MetaDataSearch object.
395  $result_meta =& $this->__searchMeta($query_parser,'keyword');
396  $result->mergeEntries($result_meta);
397 
398  $result_meta =& $this->__searchMeta($query_parser,'contribute');
399  $result->mergeEntries($result_meta);
400 
401  $result_meta =& $this->__searchMeta($query_parser,'title');
402  $result->mergeEntries($result_meta);
403 
404  $result_meta =& $this->__searchMeta($query_parser,'description');
405  $result->mergeEntries($result_meta);
406 
407  // Perform details search in object specific tables
408  if($this->getType() == SEARCH_DETAILS)
409  {
410  $result = $this->__performDetailsSearch($query_parser,$result);
411  }
412  // Step 5: Search in results
413  if($this->search_mode == 'in_results')
414  {
415  include_once 'Services/Search/classes/class.ilSearchResult.php';
416 
417  $old_result_obj = new ilSearchResult($ilUser->getId());
418  $old_result_obj->read();
419 
420  $result->diffEntriesFromResult($old_result_obj);
421  }
422 
423 
424  // Step 4: merge and validate results
425  $result->filter($this->getRootNode(),$query_parser->getCombination() == 'and');
426  $result->save();
427  $this->showSearch();
428 
429  if(!count($result->getResults()))
430  {
431  ilUtil::sendInfo($this->lng->txt('search_no_match'));
432  }
433 
434  if($result->isLimitReached())
435  {
436  #$message = sprintf($this->lng->txt('search_limit_reached'),$this->settings->getMaxHits());
437  #ilUtil::sendInfo($message);
438  }
439 
440  // Step 6: show results
441  $this->addPager($result,'max_page');
442 
443  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
445  $presentation->setResults($result->getResultsForPresentation());
446  $presentation->setSubitemIds($result->getSubitemIds());
447  $presentation->setPreviousNext($this->prev_link, $this->next_link);
448 
449  if($presentation->render())
450  {
451 // $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML());
452  $this->tpl->setVariable('RESULTS_TABLE',$presentation->getHTML(true));
453  }
454 
455  return true;
456  }
457 
458 
459 
460  function prepareOutput()
461  {
462  global $ilTabs;
463 
465 
466 // $this->tpl->addBlockFile("TABS","tabs","tpl.tabs.html");
467 
468  $ilTabs->addTab("search", $this->lng->txt("search"),
469  $this->ctrl->getLinkTarget($this));
470 
471  /*$this->tpl->setCurrentBlock("tab");
472  $this->tpl->setVariable("TAB_TYPE","tabactive");
473  $this->tpl->setVariable("TAB_LINK",$this->ctrl->getLinkTarget($this));
474  $this->tpl->setVariable("TAB_TEXT",$this->lng->txt("search"));
475  $this->tpl->parseCurrentBlock();*/
476 
477  if (!$this->settings->getHideAdvancedSearch())
478  {
479  /*$this->tpl->setCurrentBlock("tab");
480  $this->tpl->setVariable("TAB_TYPE","tabinactive");
481  $this->tpl->setVariable("TAB_LINK",$this->ctrl->getLinkTargetByClass('iladvancedsearchgui'));
482  $this->tpl->setVariable("TAB_TEXT",$this->lng->txt("search_advanced"));
483  $this->tpl->parseCurrentBlock();*/
484  $ilTabs->addTab("adv_search", $this->lng->txt("search_advanced"),
485  $this->ctrl->getLinkTargetByClass('iladvancedsearchgui'));
486  }
487 
488  $ilTabs->activateTab("search");
489  }
490 
491  // PRIVATE
492  function &__performDetailsSearch(&$query_parser,&$result)
493  {
494  foreach($this->getDetails() as $type => $enabled)
495  {
496  if(!$enabled)
497  {
498  continue;
499  }
500 
501  switch($type)
502  {
503  case 'lms':
504  $content_search =& ilObjectSearchFactory::_getLMContentSearchInstance($query_parser);
505  $content_search->setFilter($this->__getFilter());
506  $result->mergeEntries($content_search->performSearch());
507  break;
508 
509  case 'frm':
510  $forum_search =& ilObjectSearchFactory::_getForumSearchInstance($query_parser);
511  $forum_search->setFilter($this->__getFilter());
512  $result->mergeEntries($forum_search->performSearch());
513  break;
514 
515  case 'glo':
516  // Glossary term definition pages
517  $gdf_search =& ilObjectSearchFactory::_getLMContentSearchInstance($query_parser);
518  $gdf_search->setFilter(array('gdf'));
519  $result->mergeEntries($gdf_search->performSearch());
520  // Glossary terms
521  $gdf_term_search =& ilObjectSearchFactory::_getGlossaryDefinitionSearchInstance($query_parser);
522  $result->mergeEntries($gdf_term_search->performSearch());
523  break;
524 
525  case 'exc':
526  $exc_search =& ilObjectSearchFactory::_getExerciseSearchInstance($query_parser);
527  $exc_search->setFilter($this->__getFilter());
528  $result->mergeEntries($exc_search->performSearch());
529  break;
530 
531  case 'mcst':
532  $mcst_search =& ilObjectSearchFactory::_getMediaCastSearchInstance($query_parser);
533  $result->mergeEntries($mcst_search->performSearch());
534  break;
535 
536  case 'tst':
537  $tst_search =& ilObjectSearchFactory::_getTestSearchInstance($query_parser);
538  $tst_search->setFilter($this->__getFilter());
539  $result->mergeEntries($tst_search->performSearch());
540  break;
541 
542  case 'mep':
543  $mep_search =& ilObjectSearchFactory::_getMediaPoolSearchInstance($query_parser);
544  $mep_search->setFilter($this->__getFilter());
545  $result->mergeEntries($mep_search->performSearch());
546 
547  // Mob keyword search
548  $mob_search = ilObjectSearchFactory::_getMediaPoolSearchInstance($query_parser);
549  $mob_search->setFilter($this->__getFilter());
550  $result->mergeEntries($mob_search->performKeywordSearch());
551 
552  break;
553 
554  case 'wiki':
555  $wiki_search =& ilObjectSearchFactory::_getWikiContentSearchInstance($query_parser);
556  $wiki_search->setFilter($this->__getFilter());
557  $result->mergeEntries($wiki_search->performSearch());
558 
559  /*$result_meta =& $this->__searchMeta($query_parser,'title');
560  $result->mergeEntries($result_meta);
561  $result_meta =& $this->__searchMeta($query_parser,'description');
562  $result->mergeEntries($result_meta);*/
563  break;
564 
565  }
566  }
567  return $result;
568  }
569 
575  function &__parseQueryString()
576  {
577  include_once 'Services/Search/classes/class.ilQueryParser.php';
578 
579  $query_parser = new ilQueryParser(ilUtil::stripSlashes($this->getString()));
580  $query_parser->setCombination($this->getCombination());
581  $query_parser->parse();
582 
583  if(!$query_parser->validate())
584  {
585  return $query_parser->getMessage();
586  }
587  return $query_parser;
588  }
594  function &__searchObjects(&$query_parser)
595  {
596  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
597 
598  $obj_search =& ilObjectSearchFactory::_getObjectSearchInstance($query_parser);
599  if($this->getType() == SEARCH_DETAILS)
600  {
601  $obj_search->setFilter($this->__getFilter());
602  }
603  return $obj_search->performSearch();
604  }
605 
606 
612  function &__searchMeta(&$query_parser,$a_type)
613  {
614  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
615 
616  $meta_search =& ilObjectSearchFactory::_getMetaDataSearchInstance($query_parser);
617  if($this->getType() == SEARCH_DETAILS)
618  {
619  $meta_search->setFilter($this->__getFilter());
620  }
621  switch($a_type)
622  {
623  case 'keyword':
624  $meta_search->setMode('keyword');
625  break;
626 
627  case 'contribute':
628  $meta_search->setMode('contribute');
629  break;
630 
631  case 'title':
632  $meta_search->setMode('title');
633  break;
634 
635  case 'description':
636  $meta_search->setMode('description');
637  break;
638  }
639  return $meta_search->performSearch();
640  }
646  function __getFilter()
647  {
648  if($this->getType() != SEARCH_DETAILS)
649  {
650  return false;
651  }
652 
653  foreach($this->getDetails() as $key => $detail_type)
654  {
655  if(!$detail_type)
656  {
657  continue;
658  }
659 
660  switch($key)
661  {
662  case 'lms':
663  $filter[] = 'lm';
664  $filter[] = 'dbk';
665  $filter[] = 'pg';
666  $filter[] = 'st';
667  $filter[] = 'sahs';
668  $filter[] = 'htlm';
669  break;
670 
671  case 'frm':
672  $filter[] = 'frm';
673  break;
674 
675  case 'glo':
676  $filter[] = 'glo';
677  break;
678 
679  case 'exc':
680  $filter[] = 'exc';
681  break;
682 
683  case 'mcst':
684  $filter[] = 'mcst';
685  break;
686 
687  case 'tst':
688  $filter[] = 'tst';
689  $filter[] = 'svy';
690  $filter[] = 'qpl';
691  $filter[] = 'spl';
692  break;
693 
694  case 'mep':
695  $filter[] = 'mep';
696  $filter[] = 'mob';
697  break;
698 
699  case 'fil':
700  $filter[] = 'file';
701  break;
702 
703  case 'wiki':
704  $filter[] = 'wpg';
705  break;
706  }
707  }
708  return $filter ? $filter : array();
709  }
710 
717  protected function initUserSearchCache()
718  {
719  global $ilUser;
720 
721  include_once('Services/Search/classes/class.ilUserSearchCache.php');
722  $this->search_cache = ilUserSearchCache::_getInstance($ilUser->getId());
723  $this->search_cache->switchSearchType(ilUserSearchCache::DEFAULT_SEARCH);
724  if($_GET['page_number'])
725  {
726  $this->search_cache->setResultPageNumber((int) $_GET['page_number']);
727  }
728  if(isset($_POST['cmd']['performSearch']))
729  {
730  $this->search_cache->setQuery(ilUtil::stripSlashes($_POST['term']));
731  $this->search_cache->save();
732  }
733 
734  }
735 
736 }
737 ?>