ILIAS  Release_4_0_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  $item_html = array();
241  $this->parseResultReferences();
242  $ilBench->stop('Lucene','2000_pr');
243 
244 
245  $set = array();
246  foreach($this->getResults() as $c_ref_id => $res_data)
247  {
248  $ilBench->start('Lucene','2100_res');
249  foreach($this->getAllReferences($c_ref_id) as $ref_id)
250  {
251  $ilBench->start('Lucene','2120_tree');
252  if(!$tree->isInTree($ref_id))
253  {
254  continue;
255  }
256  $ilBench->stop('Lucene','2120_tree');
257 
258  $set[] = array(
259  "ref_id" => $ref_id,
260  "obj_id" => $res_data,
261  "title" => $this->lookupTitle($res_data,0),
262  "title_sort"=> ilObject::_lookupTitle($res_data),
263  "description" => $this->lookupDescription($res_data,0),
264  "type" => ilObject::_lookupType($res_data),
265  "relevance" => $this->getRelevance($res_data),
266  "s_relevance" => sprintf("%03d",$this->getRelevance($res_data))
267  );
268  }
269  $ilBench->stop('Lucene','2100_res');
270  }
271 
272  if(!count($set))
273  {
274  return false;
275  }
276 
277  $ilBench->start('Lucene','2900_tb');
278  include_once("./Services/Search/classes/class.ilSearchResultTableGUI.php");
279  $result_table = new ilSearchResultTableGUI($this->container, "showSavedResults", $this);
280  $result_table->setCustomPreviousNext($this->prev, $this->next);
281 
282  $result_table->setData($set);
283  $this->thtml = $result_table->getHTML();
284  $ilBench->stop('Lucene','2900_tb');
285 
286  return true;
287  }
288 
289 
290  // searcher
296  public function getRelevance($a_obj_id)
297  {
298  if($this->getMode() == self::MODE_LUCENE)
299  {
300  return $this->searcher->getResult()->getRelevance($a_obj_id);
301  }
302  return 0;
303  }
304 
310  public function lookupTitle($a_obj_id,$a_sub_id)
311  {
312  if($this->getMode() != self::MODE_LUCENE or !is_object($this->searcher->getHighlighter()))
313  {
314  return ilObject::_lookupTitle($a_obj_id);
315  }
316  if(strlen($title = $this->searcher->getHighlighter()->getTitle($a_obj_id,$a_sub_id)))
317  {
318  return $title;
319  }
320  return ilObject::_lookupTitle($a_obj_id);
321  }
322 
328  public function lookupDescription($a_obj_id,$a_sub_id)
329  {
330  if($this->getMode() != self::MODE_LUCENE or !is_object($this->searcher->getHighlighter()))
331  {
332  return ilObject::_lookupDescription($a_obj_id);
333  }
334  if(strlen($title = $this->searcher->getHighlighter()->getDescription($a_obj_id,$a_sub_id)))
335  {
336  return $title;
337  }
338  return ilObject::_lookupDescription($a_obj_id);
339  }
340 
346  public function lookupContent($a_obj_id,$a_sub_id)
347  {
348  if($this->getMode() != self::MODE_LUCENE or !is_object($this->searcher->getHighlighter()))
349  {
350  return '';
351  }
352  return $this->searcher->getHighlighter()->getContent($a_obj_id,$a_sub_id);
353  }
354 
358  public function appendAdditionalInformation($item_list_gui,$ref_id,$obj_id,$type)
359  {
360  $sub = $this->appendSubItems($item_list_gui,$ref_id,$obj_id,$type);
361  $path = $this->appendPath($ref_id);
362  $more = $this->appendMorePathes($ref_id);
363  #$rel = $this->appendRelevance($obj_id);
364 
365  if(!strlen($sub) and
366  !strlen($path) and
367  !strlen($more) and
368  !strlen($rel))
369  {
370  return '';
371  }
372  $tpl = new ilTemplate('tpl.lucene_additional_information.html',true,true,'Services/Search');
373  $tpl->setVariable('SUBITEM',$sub);
374  if(strlen($path)) {
375  $tpl->setVariable('PATH',$path);
376  }
377  if(strlen($more)) {
378  $tpl->setVariable('MORE_PATH',$more);
379  }
380  if(strlen($rel)) {
381  $tpl->setVariable('RELEVANCE',$rel);
382  }
383 
384  $item_list_gui->setAdditionalInformation($tpl->get());
385  //$item_list_gui->setAdditionalInformation("Hello");
386  }
387 
388 
393  protected function appendPath($a_ref_id)
394  {
395  include_once './Services/Tree/classes/class.ilPathGUI.php';
396  $path_gui = new ilPathGUI();
397  $path_gui->enableTextOnly(false);
398  $path_gui->setUseImages(false);
399 
400  $tpl = new ilTemplate('tpl.lucene_path.html',true,true,'Services/Search');
401  $tpl->setVariable('PATH_ITEM',$path_gui->getPath(ROOT_FOLDER_ID,$a_ref_id));
402  return $tpl->get();
403  }
404 
409  protected function appendMorePathes($a_ref_id)
410  {
411  if($this->getMode() != self::MODE_LUCENE)
412  {
413  return '';
414  }
415 
416 
417  if(!$num_refs = $this->hasMoreReferences($a_ref_id))
418  {
419  return '';
420  }
421  $tpl = new ilTemplate('tpl.lucene_more_references.html',true,true,'Services/Search');
422  $this->ctrl->setParameter($this->getContainer(),'refs',$a_ref_id);
423  $tpl->setVariable('MORE_REFS_LINK',$this->ctrl->getLinkTarget($this->getContainer(),''));
424  $this->ctrl->clearParameters($this->getContainer());
425 
426  $tpl->setVariable('TXT_MORE_REFS',sprintf($this->lng->txt('lucene_all_occurrences'),$num_refs));
427  return $tpl->get();
428  }
429 
434  protected function appendRelevance($a_obj_id)
435  {
436  if($this->getMode() != self::MODE_LUCENE)
437  {
438  return '';
439  }
440 
441  if(!((int) $this->getRelevance($a_obj_id)))
442  {
443  return '';
444  }
445 
446  include_once './Services/Search/classes/class.ilSearchSettings.php';
447  if(!ilSearchSettings::getInstance()->isRelevanceVisible())
448  {
449  return '';
450  }
451 
452  $tpl = new ilTemplate('tpl.lucene_relevance.html',true,true,'Services/Search');
453 
454  $width1 = (int) $this->getRelevance($a_obj_id);
455  $width2 = (int) (100 - $width1);
456 
457  $tpl->setCurrentBlock('relevance');
458  $tpl->setVariable('VAL_REL',sprintf("%d %%",$this->getRelevance($a_obj_id)));
459  $tpl->setVariable('WIDTH_A',$width1);
460  $tpl->setVariable('WIDTH_B',$width2);
461  $tpl->setVariable('IMG_A',ilUtil::getImagePath("relevance_blue.gif"));
462  $tpl->setVariable('IMG_B',ilUtil::getImagePath("relevance_dark.gif"));
463  $tpl->parseCurrentBlock();
464  $html = $tpl->get();
465  return $html;
466  }
467 
472  protected function appendSubItems($item_list_gui,$ref_id,$obj_id,$a_type)
473  {
474  $subitem_ids = array();
475  if($this->getMode() == self::MODE_STANDARD)
476  {
477  $subitem_ids = $this->getSubitemIdsByObject($obj_id);
478  $highlighter = null;
479  }
480  elseif(is_object($this->searcher->getHighlighter()))
481  {
482  $subitem_ids = $this->searcher->getHighlighter()->getSubitemIds($obj_id);
483  $highlighter = $this->searcher->getHighlighter();
484  }
485 
486  if(!count($subitem_ids))
487  {
488  return;
489  }
490 
491  // Build subitem list
492  include_once './Services/Search/classes/Lucene/class.ilLuceneSubItemListGUIFactory.php';
494  $sub_list->setHighlighter($highlighter);
495  $sub_list->init($item_list_gui,$ref_id,$subitem_ids);
496  return $sub_list->getHTML();
497 
498  }
499 
500  protected function initReferences()
501  {
502  if(isset($_REQUEST['refs']))
503  {
504  $_SESSION['vis_references'][(int) $_REQUEST['refs']] = (int) $_REQUEST['refs'];
505  }
506  }
507 }
508 ?>