ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilShopSearchResult.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once 'Services/Search/classes/class.ilSearchResult.php';
5 
17 {
18  protected $result_page_number = 0;
19  protected $topics = array();
20  protected $presentation_results = array();
21  protected $search_type = '';
22 
23  public function ilShopSearchResult($a_search_type)
24  {
25  global $ilUser;
26 
27  $this->search_type = $a_search_type;
28 
29  parent::__construct($ilUser->getId());
30  }
31 
32  public function setSearchType($_search_type)
33  {
34  $this->search_type = $_search_type;
35 
36  return $this;
37  }
38 
39  public function getSearchType()
40  {
41  return $this->search_type;
42  }
43 
44  protected function assignEntries($a_entries)
45  {
46  $ordered_entries = array();
47 
48  $num_entries = count($a_entries);
49  foreach($this->topics as $oTopic)
50  {
51  foreach($a_entries as $aEntry)
52  {
53  if($oTopic->getId() == $aEntry['topic_id'])
54  {
55  $ordered_entries[$aEntry['ref_id']] = $aEntry;
56  }
57  }
58  }
59  if(count($ordered_entries) < $num_entries)
60  {
61  foreach($a_entries as $aEntry)
62  {
63  if(0 == $aEntry['topic_id'])
64  {
65  $ordered_entries[$aEntry['ref_id']] = $aEntry;
66  }
67  }
68  }
69 
70  return is_array($ordered_entries) ? $ordered_entries : array();
71  }
72 
85  public function filter($a_root_node, $check_and)
86  {
87  global $tree;
88 
90 
91  // get ref_ids and check access
92 
93  $tmp_entries = array();
94  foreach($this->getEntries() as $ref_id => $entry)
95  {
96  // boolean and failed continue
97  if($check_and && in_array(0, $entry['found']))
98  {
99  continue;
100  }
101  // Types like role, rolt, user do not need rbac checks
102  $type = ilObject::_lookupType($entry['obj_id']);
103  if($type == 'rolt' or $type == 'usr' or $type == 'role')
104  {
105  continue;
106  }
107 
108  // Check access
109  if($this->ilAccess->checkAccessOfUser($this->getUserId(),
110  $this->getRequiredPermission(),
111  '',
112  $ref_id))
113  {
114  if($a_root_node == ROOT_FOLDER_ID || $tree->isGrandChild($a_root_node, $ref_id))
115  {
116  if($this->callListeners($ref_id, $entry))
117  {
118  $entry['ref_id'] = $ref_id;
119  $entry['type'] = $type;
120  $entry['topic_id'] = ilPaymentObject::_lookupTopicId($ref_id);
121  $tmp_entries[$ref_id] = $entry;
122  }
123  }
124  }
125  }
126 
127  $this->results = array();
128 
129  $tmp_entries = $this->assignEntries($tmp_entries);
130 
131  foreach($tmp_entries as $entry)
132  {
133  if($this->callListeners($entry['ref_id'], $entry))
134  {
135  $this->addResult($entry['ref_id'], $entry['obj_id'], $entry['type']);
136  $this->search_cache->appendToChecked($entry['ref_id'], $entry['obj_id']);
137  $this->__updateResultChilds($entry['ref_id'], $entry['child']);
138  }
139  }
140  $this->search_cache->setResults($this->results);
141  return false;
142  }
143 
144  public function __initSearchSettingsObject()
145  {
146  include_once 'Services/Payment/classes/class.ilPaymentSettings.php';
147  $maxhits = ilPaymentSettings::_getInstance()->get('max_hits');
148  $this->setMaxHits(($maxhits > 0 ? $maxhits : 20));
149  }
150 
151  public function getResultsForPresentation()
152  {
153  global $lng, $tree;
154  $results = array();
155 
156  $offset_counter = 0;
157  $counter = 0;
158 
159  $objects_with_topics = array();
160  $objects_with_no_topcis = array();
161  $objects_of_subtree = array();
162 
163  foreach($this->getResults() as $result)
164  {
165  if($this->getMaxHits() * ($this->getResultPageNumber() - 1) > $offset_counter)
166  {
167  ++$offset_counter;
168  continue;
169  }
170 
171  $results[] = $result;
172  ++$counter;
173 
174  if($counter >= $this->getMaxHits())
175  {
176  break;
177  }
178  }
179  if($_GET['tree_ref_id'])
180  {
181  foreach($results as $result)
182  {
183  $tree_ref_id = $_GET['tree_ref_id'];
184 
185  $parent_id = $tree->getParentId($result['ref_id']);
186 
187  if($tree_ref_id == $parent_id)
188  {
189  $objects_of_subtree[$result['ref_id']] = $result;
190  }
191 
192  foreach($objects_of_subtree as $result)
193  {
194  switch($result['type'])
195  {
196  // learning material
197  case "sahs":
198  case "lm":
199  case "dbk":
200  case "htlm":
201  $type = "lres";
202  break;
203 
204  default:
205  $type = $result['type'];
206  break;
207  }
208  $title = ilObject::_lookupTitle($result['obj_id']);
209  $description = ilObject::_lookupDescription($result['obj_id']);
210 
211  $presentation_results[0][$type][] = array('ref_id' => $result['ref_id'],
212  'title' => $title,
213  'description' => $description,
214  'type' => $result['type'],
215  'obj_id' => $result['obj_id'],
216  'child' => $result['child']);
217 
218  $this->addPresentationResult($presentation_results[0][$type][count($presentation_results[0][$type]) - 1]);
219  }
220  }
221  }
222  else
223  {
224  foreach($this->getTopics() as $oTopic)
225  {
226  foreach($results as $result)
227  {
228  $topic_id = ilPaymentObject::_lookupTopicId($result['ref_id']);
229 // $parent_id = $tree->getParentId($result['ref_id']);
230 
231  if(!(int)$topic_id && !array_key_exists($result['ref_id'], $objects_with_no_topcis))
232  {
233  $objects_with_no_topcis[$result['ref_id']] = $result;
234  continue;
235  }
236 
237  if((int)$topic_id != $oTopic->getId())
238  {
239  continue;
240  }
241 
242  if(!array_key_exists($result['ref_id'], $objects_with_topics))
243  {
244  $objects_with_topics[$result['ref_id']] = $result;
245  }
246 
247  switch($result['type'])
248  {
249  // learning material
250  case "sahs":
251  case "lm":
252  case "dbk":
253  case "htlm":
254  $type = "lres";
255  break;
256 
257  default:
258  $type = $result['type'];
259  break;
260  }
261  $title = ilObject::_lookupTitle($result['obj_id']);
262  $description = ilObject::_lookupDescription($result['obj_id']);
263 
264  $subtype = '';
265  if($result['type'] == 'exc')
266  {
267  $check_sub = ilPaymentObject::_checkExcSubtype($result['ref_id']);
268  $subtype = ' ('.$lng->txt($check_sub[0]).')';
269  }
270 
271  $presentation_results[$oTopic->getId()][$type][] = array('ref_id' => $result['ref_id'],
272  'title' => $title.' '.$subtype,
273  'description' => $description,
274  'type' => $result['type'],
275  'obj_id' => $result['obj_id'],
276  'topic_id' => $topic_id,
277  'child' => $result['child']);
278  $this->addPresentationResult($presentation_results[$oTopic->getId()][$type][count($presentation_results[$oTopic->getId()][$type]) - 1]);
279  }
280  }
281  foreach($results as $result)
282  {
283  if(!array_key_exists($result['ref_id'], $objects_with_topics) &&
284  !array_key_exists($result['ref_id'], $objects_with_no_topcis))
285  {
286  $objects_with_no_topcis[$result['ref_id']] = $result;
287  }
288  }
289  foreach($objects_with_no_topcis as $result)
290  {
291  switch($result['type'])
292  {
293  // learning material
294  case "sahs":
295  case "lm":
296  case "dbk":
297  case "htlm":
298  $type = "lres";
299  break;
300 
301  default:
302  $type = $result['type'];
303  break;
304  }
305  $title = ilObject::_lookupTitle($result['obj_id']);
306  $description = ilObject::_lookupDescription($result['obj_id']);
307 
308  $presentation_results[0][$type][] = array('ref_id' => $result['ref_id'],
309  'title' => $title,
310  'description' => $description,
311  'type' => $result['type'],
312  'obj_id' => $result['obj_id'],
313  'child' => $result['child']);
314  $this->addPresentationResult($presentation_results[0][$type][count($presentation_results[0][$type]) - 1]);
315 
316  }
317  }
318  return $presentation_results ? $presentation_results : array();
319  }
320 
321  public function getTopics()
322  {
323  return is_array($this->topics) ? $this->topics : array();
324  }
325  public function setTopics($a_topics = array())
326  {
327  $this->topics = $a_topics;
328  }
329 
330  public function setResultPageNumber($a_result_page_number)
331  {
332  $this->result_page_number = (int)$a_result_page_number > 0 ? $a_result_page_number : 1;
333  }
334  public function getResultPageNumber()
335  {
337  }
338 
339  public function addPresentationResult($a_presentation_result = array())
340  {
341  $this->presentation_results[] = $a_presentation_result;
342  }
343  public function getPresentationResults()
344  {
345  return is_array($this->presentation_results) ? $this->presentation_results : array();
346  }
347 
348  protected function initUserSearchCache()
349  {
351 
352  $this->search_cache->switchSearchType($this->search_type);
353  }
354 
355  function addEntry($a_ref_id, $a_type, $found, $a_child_id = 0)
356  {
357  global $ilObjDataCache;
358 
359  $a_obj_id = $ilObjDataCache->lookupObjId($a_ref_id);
360 
361  // Create new entry if it not exists
362  if(!$this->entries[$a_ref_id])
363  {
364  $this->entries[$a_ref_id]['ref_id'] = $a_ref_id;
365  $this->entries[$a_ref_id]['obj_id'] = $a_obj_id;
366  $this->entries[$a_ref_id]['type'] = $a_type;
367  $this->entries[$a_ref_id]['found'] = $found;
368 
369  if($a_child_id and $a_child_id != $a_ref_id)
370  {
371  $this->entries[$a_ref_id]['child'][$a_child_id] = $a_child_id;
372  }
373  }
374  else
375  {
376  // replace or add child ('pg','st') id
377  if($a_child_id and $a_child_id != $a_obj_id)
378  {
379  $this->entries[$a_ref_id]['child'][$a_child_id] = $a_child_id;
380  }
381 
382  // UPDATE FOUND
383  $counter = 0;
384  foreach($found as $position)
385  {
386  if($position)
387  {
388  $this->entries[$a_ref_id]['found'][$counter] = $position;
389  }
390  $counter++;
391  }
392  }
393  return true;
394  }
395 
396  function __updateEntryChilds($a_ref_id,$a_childs)
397  {
398  if($this->entries[$a_ref_id] and is_array($a_childs))
399  {
400  foreach($a_childs as $child_id)
401  {
402  if($child_id)
403  {
404  $this->entries[$a_ref_id]['child'][$child_id] = $child_id;
405  }
406  }
407  return true;
408  }
409  return false;
410  }
411 
412  function mergeEntries(&$result_obj)
413  {
414  foreach($result_obj->getEntries() as $entry)
415  {
416  $this->addEntry($entry['ref_id'],$entry['type'],$entry['found']);
417  $this->__updateEntryChilds($entry['ref_id'],$entry['child']);
418  }
419  return true;
420  }
421 
422  function diffEntriesFromResult(&$result_obj)
423  {
424  $new_entries = $this->getEntries();
425  $this->entries = array();
426 
427  // Get all checked objects
428  foreach($this->search_cache->getCheckedItems() as $ref_id => $obj_id)
429  {
430  if(isset($new_entries[$ref_id]))
431  {
432  $this->addEntry($new_entries[$ref_id]['ref_id'],
433  $new_entries[$ref_id]['type'],
434  $new_entries[$ref_id]['found']);
435  $this->__updateEntryChilds($new_entries[$ref_id]['ref_id'],
436  $new_entries[$ref_id]['child']);
437  }
438  }
439  }
440 
441  function getUniqueResults()
442  {
443  return $this->results;
444  }
445 
446 }
447 ?>