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