ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSearchResultPresentation.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 
34 {
35  const MODE_LUCENE = 1;
36  const MODE_STANDARD = 2;
37 
38  protected $mode;
39 
40  protected $tpl;
41  protected $lng;
42 
43  private $results = array();
44  private $subitem_ids = array();
45  private $has_more_ref_ids = array();
46  private $all_references = null;
47  private $searcher = null;
48 
49  private $container = null;
50 
55  public function __construct($container = null, $a_mode = self::MODE_LUCENE)
56  {
57  global $tpl,$lng,$ilCtrl;
58 
59  $this->mode = $a_mode;
60  $this->lng = $lng;
61  $this->container = $container;
62  $this->ctrl = $ilCtrl;
63 
64  $this->initReferences();
65 
66  if(isset($_GET['details']))
67  {
68  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
69  ilSubItemListGUI::setShowDetails((int) $_GET['details']);
70  }
71  }
72 
76  public function getContainer()
77  {
78  return $this->container;
79  }
80 
81  public function getMode()
82  {
83  return $this->mode;
84  }
85 
90  public function setResults($a_result_data)
91  {
92  $this->results = $a_result_data;
93  }
94 
99  public function getResults()
100  {
101  return $this->results ? $this->results : array();
102  }
103 
110  public function setSubitemIds($a_subids)
111  {
112  $this->subitem_ids = $a_subids;
113  }
114 
119  public function getSubitemIds()
120  {
121  return $this->subitem_ids ? $this->subitem_ids : array();
122  }
123 
129  public function getSubitemIdsByObject($a_obj_id)
130  {
131  return (isset($this->subitem_ids[$a_obj_id]) and $this->subitem_ids[$a_obj_id]) ?
132  $this->subitem_ids[$a_obj_id] :
133  array();
134  }
135 
136 
137 
141  protected function parseResultReferences()
142  {
143  global $ilAccess;
144 
145  foreach($this->getResults() as $ref_id => $obj_id)
146  {
147  $this->all_references[$ref_id][] = $ref_id;
148  $counter = 0;
149  foreach(ilObject::_getAllReferences($obj_id) as $new_ref)
150  {
151  if($new_ref == $ref_id)
152  {
153  continue;
154  }
155  if(!$ilAccess->checkAccess('read','',$new_ref))
156  {
157  continue;
158  }
159  $this->all_references[$ref_id][] = $new_ref;
160  ++$counter;
161  }
162  $this->has_more_ref_ids[$ref_id] = $counter;
163  }
164  }
165 
166  protected function hasMoreReferences($a_ref_id)
167  {
168  if(!isset($this->has_more_ref_ids[$a_ref_id]) or
169  !$this->has_more_ref_ids[$a_ref_id] or
170  isset($_SESSION['vis_references'][$a_ref_id]))
171  {
172  return false;
173  }
174 
175  return $this->has_more_ref_ids[$a_ref_id];
176  }
177 
178  protected function getAllReferences($a_ref_id)
179  {
180  if(isset($_SESSION['vis_references'][$a_ref_id]))
181  {
182  return $this->all_references[$a_ref_id] ? $this->all_references[$a_ref_id] : array();
183  }
184  else
185  {
186  return array($a_ref_id);
187  }
188  }
189 
194  public function getHTML($a_new = false)
195  {
196  return $this->thtml;
197  }
198 
204  public function setSearcher($a_searcher)
205  {
206  $this->searcher = $a_searcher;
207  }
208 
214  public function render()
215  {
216  return $this->renderItemList();
217  }
218 
222  function setPreviousNext($a_p, $a_n)
223  {
224  $this->prev = $a_p;
225  $this->next = $a_n;
226  }
227 
228 
233  protected function renderItemList()
234  {
235  global $tree,$ilBench;
236 
237  $this->html = '';
238 
239  $ilBench->start('Lucene','2000_pr');
240  $this->parseResultReferences();
241  $ilBench->stop('Lucene','2000_pr');
242 
243  include_once("./Services/Object/classes/class.ilObjectListGUIPreloader.php");
245 
246  $set = array();
247  foreach($this->getResults() as $c_ref_id => $obj_id)
248  {
249  $ilBench->start('Lucene','2100_res');
250  foreach($this->getAllReferences($c_ref_id) as $ref_id)
251  {
252  $ilBench->start('Lucene','2120_tree');
253  if(!$tree->isInTree($ref_id))
254  {
255  continue;
256  }
257  $ilBench->stop('Lucene','2120_tree');
258 
259  $obj_type = ilObject::_lookupType($obj_id);
260 
261  $set[] = array(
262  "ref_id" => $ref_id,
263  "obj_id" => $obj_id,
264  "title" => $this->lookupTitle($obj_id,0),
265  "title_sort" => ilObject::_lookupTitle($obj_id),
266  "description" => $this->lookupDescription($obj_id,0),
267  "type" => $obj_type,
268  "relevance" => $this->getRelevance($obj_id),
269  "s_relevance" => sprintf("%03d",$this->getRelevance($obj_id))
270  );
271 
272  $preloader->addItem($obj_id, $obj_type, $ref_id);
273  }
274  $ilBench->stop('Lucene','2100_res');
275  }
276 
277  if(!count($set))
278  {
279  return false;
280  }
281 
282  $preloader->preload();
283  unset($preloader);
284 
285  $ilBench->start('Lucene','2900_tb');
286  include_once("./Services/Search/classes/class.ilSearchResultTableGUI.php");
287  $result_table = new ilSearchResultTableGUI($this->container, "showSavedResults", $this);
288  $result_table->setCustomPreviousNext($this->prev, $this->next);
289 
290  $result_table->setData($set);
291  $this->thtml = $result_table->getHTML();
292  $ilBench->stop('Lucene','2900_tb');
293 
294  return true;
295  }
296 
297 
298  // searcher
304  public function getRelevance($a_obj_id)
305  {
306  if($this->getMode() == self::MODE_LUCENE)
307  {
308  return $this->searcher->getResult()->getRelevance($a_obj_id);
309  }
310  return 0;
311  }
312 
318  public function lookupTitle($a_obj_id,$a_sub_id)
319  {
320  if($this->getMode() != self::MODE_LUCENE or !is_object($this->searcher->getHighlighter()))
321  {
322  return ilObject::_lookupTitle($a_obj_id);
323  }
324  if(strlen($title = $this->searcher->getHighlighter()->getTitle($a_obj_id,$a_sub_id)))
325  {
326  return $title;
327  }
328  return ilObject::_lookupTitle($a_obj_id);
329  }
330 
336  public function lookupDescription($a_obj_id,$a_sub_id)
337  {
338  if($this->getMode() != self::MODE_LUCENE or !is_object($this->searcher->getHighlighter()))
339  {
340  return ilObject::_lookupDescription($a_obj_id);
341  }
342  if(strlen($title = $this->searcher->getHighlighter()->getDescription($a_obj_id,$a_sub_id)))
343  {
344  return $title;
345  }
346  return ilObject::_lookupDescription($a_obj_id);
347  }
348 
354  public function lookupContent($a_obj_id,$a_sub_id)
355  {
356  if($this->getMode() != self::MODE_LUCENE or !is_object($this->searcher->getHighlighter()))
357  {
358  return '';
359  }
360  return $this->searcher->getHighlighter()->getContent($a_obj_id,$a_sub_id);
361  }
362 
366  public function appendAdditionalInformation($item_list_gui,$ref_id,$obj_id,$type)
367  {
368  $sub = $this->appendSubItems($item_list_gui,$ref_id,$obj_id,$type);
369  $path = $this->appendPath($ref_id);
370  $more = $this->appendMorePathes($ref_id);
371  #$rel = $this->appendRelevance($obj_id);
372 
373  if(!strlen($sub) and
374  !strlen($path) and
375  !strlen($more) and
376  !strlen($rel))
377  {
378  return '';
379  }
380  $tpl = new ilTemplate('tpl.lucene_additional_information.html',true,true,'Services/Search');
381  $tpl->setVariable('SUBITEM',$sub);
382  if(strlen($path)) {
383  $tpl->setVariable('PATH',$path);
384  }
385  if(strlen($more)) {
386  $tpl->setVariable('MORE_PATH',$more);
387  }
388  if(strlen($rel)) {
389  $tpl->setVariable('RELEVANCE',$rel);
390  }
391 
392  $item_list_gui->setAdditionalInformation($tpl->get());
393  //$item_list_gui->setAdditionalInformation("Hello");
394  }
395 
396 
401  protected function appendPath($a_ref_id)
402  {
403  include_once './Services/Tree/classes/class.ilPathGUI.php';
404  $path_gui = new ilPathGUI();
405  $path_gui->enableTextOnly(false);
406  $path_gui->setUseImages(false);
407 
408  $tpl = new ilTemplate('tpl.lucene_path.html',true,true,'Services/Search');
409  $tpl->setVariable('PATH_ITEM',$path_gui->getPath(ROOT_FOLDER_ID,$a_ref_id));
410  return $tpl->get();
411  }
412 
417  protected function appendMorePathes($a_ref_id)
418  {
419  if($this->getMode() != self::MODE_LUCENE)
420  {
421  return '';
422  }
423 
424 
425  if(!$num_refs = $this->hasMoreReferences($a_ref_id))
426  {
427  return '';
428  }
429  $tpl = new ilTemplate('tpl.lucene_more_references.html',true,true,'Services/Search');
430  $this->ctrl->setParameter($this->getContainer(),'refs',$a_ref_id);
431  $tpl->setVariable('MORE_REFS_LINK',$this->ctrl->getLinkTarget($this->getContainer(),''));
432  $this->ctrl->clearParameters($this->getContainer());
433 
434  $tpl->setVariable('TXT_MORE_REFS',sprintf($this->lng->txt('lucene_all_occurrences'),$num_refs));
435  return $tpl->get();
436  }
437 
442  protected function appendRelevance($a_obj_id)
443  {
444  if($this->getMode() != self::MODE_LUCENE)
445  {
446  return '';
447  }
448 
449  if(!((int) $this->getRelevance($a_obj_id)))
450  {
451  return '';
452  }
453 
454  include_once './Services/Search/classes/class.ilSearchSettings.php';
455  if(!ilSearchSettings::getInstance()->isRelevanceVisible())
456  {
457  return '';
458  }
459 
460  $tpl = new ilTemplate('tpl.lucene_relevance.html',true,true,'Services/Search');
461 
462  $width1 = (int) $this->getRelevance($a_obj_id);
463  $width2 = (int) (100 - $width1);
464 
465  $tpl->setCurrentBlock('relevance');
466  $tpl->setVariable('VAL_REL',sprintf("%d %%",$this->getRelevance($a_obj_id)));
467  $tpl->setVariable('WIDTH_A',$width1);
468  $tpl->setVariable('WIDTH_B',$width2);
469  $tpl->setVariable('IMG_A',ilUtil::getImagePath("relevance_blue.png"));
470  $tpl->setVariable('IMG_B',ilUtil::getImagePath("relevance_dark.png"));
471  $tpl->parseCurrentBlock();
472  $html = $tpl->get();
473  return $html;
474  }
475 
480  protected function appendSubItems($item_list_gui,$ref_id,$obj_id,$a_type)
481  {
482  $subitem_ids = array();
483  if($this->getMode() == self::MODE_STANDARD)
484  {
485  $subitem_ids = $this->getSubitemIdsByObject($obj_id);
486  $highlighter = null;
487  }
488  elseif(is_object($this->searcher->getHighlighter()))
489  {
490  $subitem_ids = $this->searcher->getHighlighter()->getSubitemIds($obj_id);
491  $highlighter = $this->searcher->getHighlighter();
492  }
493 
494  if(!count($subitem_ids))
495  {
496  return;
497  }
498 
499  // Build subitem list
500  include_once './Services/Search/classes/Lucene/class.ilLuceneSubItemListGUIFactory.php';
502  $sub_list->setHighlighter($highlighter);
503  $sub_list->init($item_list_gui,$ref_id,$subitem_ids);
504  return $sub_list->getHTML();
505 
506  }
507 
508  protected function initReferences()
509  {
510  if(isset($_REQUEST['refs']))
511  {
512  $_SESSION['vis_references'][(int) $_REQUEST['refs']] = (int) $_REQUEST['refs'];
513  }
514  }
515 }
516 ?>