ILIAS  eassessment Revision 61809
 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  $counter = 0;
93  $offset_counter = 0;
94 
95  $tmp_entries = array();
96  foreach($this->getEntries() as $ref_id => $entry)
97  {
98  // boolean and failed continue
99  if($check_and && in_array(0, $entry['found']))
100  {
101  continue;
102  }
103  // Types like role, rolt, user do not need rbac checks
104  $type = ilObject::_lookupType($entry['obj_id']);
105  if($type == 'rolt' or $type == 'usr' or $type == 'role')
106  {
107  continue;
108  }
109 
110  // Check access
111  if($this->ilAccess->checkAccessOfUser($this->getUserId(),
112  $this->getRequiredPermission(),
113  '',
114  $ref_id))
115  {
116  if($a_root_node == ROOT_FOLDER_ID || $tree->isGrandChild($a_root_node, $ref_id))
117  {
118  if($this->callListeners($ref_id, $entry))
119  {
120  $entry['ref_id'] = $ref_id;
121  $entry['type'] = $type;
122  $entry['topic_id'] = ilPaymentObject::_lookupTopicId($ref_id);
123  $tmp_entries[$ref_id] = $entry;
124  }
125  }
126  }
127  }
128 
129  $this->results = array();
130 
131  $tmp_entries = $this->assignEntries($tmp_entries);
132 
133  $counter = 0;
134  foreach($tmp_entries as $entry)
135  {
136  if($this->callListeners($entry['ref_id'], $entry))
137  {
138  $this->addResult($entry['ref_id'], $entry['obj_id'], $entry['type']);
139  $this->search_cache->appendToChecked($entry['ref_id'], $entry['obj_id']);
140  $this->__updateResultChilds($entry['ref_id'], $entry['child']);
141  }
142  }
143  $this->search_cache->setResults($this->results);
144  return false;
145  }
146 
147  public function __initSearchSettingsObject()
148  {
149  include_once 'Services/Payment/classes/class.ilGeneralSettings.php';
150  $maxhits = ilGeneralSettings::_getInstance()->get('max_hits');
151  $this->setMaxHits(($maxhits > 0 ? $maxhits : 20));
152  }
153 
154  public function getResultsForPresentation()
155  {
156  $results = array();
157 
158  $offset_counter = 0;
159  $counter = 0;
160 
161  foreach($this->getResults() as $result)
162  {
163  if($this->getMaxHits() * ($this->getResultPageNumber() - 1) > $offset_counter)
164  {
165  ++$offset_counter;
166  continue;
167  }
168 
169  $results[] = $result;
170  ++$counter;
171 
172  if($counter >= $this->getMaxHits())
173  {
174  break;
175  }
176  }
177 
178  $objects_with_topics = array();
179  $objects_with_no_topcis = array();
180  foreach($this->getTopics() as $oTopic)
181  {
182  foreach($results as $result)
183  {
184  $topic_id = ilPaymentObject::_lookupTopicId($result['ref_id']);
185 
186  if(!(int)$topic_id && !array_key_exists($result['ref_id'], $objects_with_no_topcis))
187  {
188  $objects_with_no_topcis[$result['ref_id']] = $result;
189  continue;
190  }
191 
192  if((int)$topic_id != $oTopic->getId())
193  {
194  continue;
195  }
196 
197  if(!array_key_exists($result['ref_id'], $objects_with_topics))
198  {
199  $objects_with_topics[$result['ref_id']] = $result;
200  }
201 
202  switch($result['type'])
203  {
204  // learning material
205  case "sahs":
206  case "lm":
207  case "dbk":
208  case "htlm":
209  $type = "lres";
210  break;
211 
212  default:
213  $type = $result['type'];
214  break;
215  }
216  $title = ilObject::_lookupTitle($result['obj_id']);
217  $description = ilObject::_lookupDescription($result['obj_id']);
218 
219  $presentation_results[$oTopic->getId()][$type][] = array('ref_id' => $result['ref_id'],
220  'title' => $title,
221  'description' => $description,
222  'type' => $result['type'],
223  'obj_id' => $result['obj_id'],
224  'topic_id' => $topic_id,
225  'child' => $result['child']);
226  $this->addPresentationResult($presentation_results[$oTopic->getId()][$type][count($presentation_results[$oTopic->getId()][$type]) - 1]);
227  }
228  }
229  foreach($results as $result)
230  {
231  if(!array_key_exists($result['ref_id'], $objects_with_topics) &&
232  !array_key_exists($result['ref_id'], $objects_with_no_topcis))
233  {
234  $objects_with_no_topcis[$result['ref_id']] = $result;
235  }
236  }
237  foreach($objects_with_no_topcis as $result)
238  {
239  switch($result['type'])
240  {
241  // learning material
242  case "sahs":
243  case "lm":
244  case "dbk":
245  case "htlm":
246  $type = "lres";
247  break;
248 
249  default:
250  $type = $result['type'];
251  break;
252  }
253  $title = ilObject::_lookupTitle($result['obj_id']);
254  $description = ilObject::_lookupDescription($result['obj_id']);
255 
256  $presentation_results[0][$type][] = array('ref_id' => $result['ref_id'],
257  'title' => $title,
258  'description' => $description,
259  'type' => $result['type'],
260  'obj_id' => $result['obj_id'],
261  'child' => $result['child']);
263 
264  }
265  return $presentation_results ? $presentation_results : array();
266  }
267 
268  public function getTopics()
269  {
270  return is_array($this->topics) ? $this->topics : array();
271  }
272  public function setTopics($a_topics = array())
273  {
274  $this->topics = $a_topics;
275  }
276 
277  public function setResultPageNumber($a_result_page_number)
278  {
279  $this->result_page_number = (int)$a_result_page_number > 0 ? $a_result_page_number : 1;
280  }
281  public function getResultPageNumber()
282  {
284  }
285 
286  public function addPresentationResult($a_presentation_result = array())
287  {
288  $this->presentation_results[] = $a_presentation_result;
289  }
290  public function getPresentationResults()
291  {
292  return is_array($this->presentation_results) ? $this->presentation_results : array();
293  }
294 
295  protected function initUserSearchCache()
296  {
298 
299  $this->search_cache->switchSearchType($this->search_type);
300  }
301 
302  function addEntry($a_ref_id, $a_type, $found, $a_child_id = 0)
303  {
304  global $ilObjDataCache;
305 
306  $a_obj_id = $ilObjDataCache->lookupObjId($a_ref_id);
307 
308  // Create new entry if it not exists
309  if(!$this->entries[$a_ref_id])
310  {
311  $this->entries[$a_ref_id]['ref_id'] = $a_ref_id;
312  $this->entries[$a_ref_id]['obj_id'] = $a_obj_id;
313  $this->entries[$a_ref_id]['type'] = $a_type;
314  $this->entries[$a_ref_id]['found'] = $found;
315 
316  if($a_child_id and $a_child_id != $a_ref_id)
317  {
318  $this->entries[$a_ref_id]['child'][$a_child_id] = $a_child_id;
319  }
320  }
321  else
322  {
323  // replace or add child ('pg','st') id
324  if($a_child_id and $a_child_id != $a_obj_id)
325  {
326  $this->entries[$a_ref_id]['child'][$a_child_id] = $a_child_id;
327  }
328 
329  // UPDATE FOUND
330  $counter = 0;
331  foreach($found as $position)
332  {
333  if($position)
334  {
335  $this->entries[$a_ref_id]['found'][$counter] = $position;
336  }
337  $counter++;
338  }
339  }
340  return true;
341  }
342 
343  function __updateEntryChilds($a_ref_id,$a_childs)
344  {
345  if($this->entries[$a_ref_id] and is_array($a_childs))
346  {
347  foreach($a_childs as $child_id)
348  {
349  if($child_id)
350  {
351  $this->entries[$a_ref_id]['child'][$child_id] = $child_id;
352  }
353  }
354  return true;
355  }
356  return false;
357  }
358 
359  function mergeEntries(&$result_obj)
360  {
361  foreach($result_obj->getEntries() as $entry)
362  {
363  $this->addEntry($entry['ref_id'],$entry['type'],$entry['found']);
364  $this->__updateEntryChilds($entry['ref_id'],$entry['child']);
365  }
366  return true;
367  }
368 
369  function diffEntriesFromResult(&$result_obj)
370  {
371  $new_entries = $this->getEntries();
372  $this->entries = array();
373 
374  // Get all checked objects
375  foreach($this->search_cache->getCheckedItems() as $ref_id => $obj_id)
376  {
377  if(isset($new_entries[$ref_id]))
378  {
379  $this->addEntry($new_entries[$ref_id]['ref_id'],
380  $new_entries[$ref_id]['type'],
381  $new_entries[$ref_id]['found']);
382  $this->__updateEntryChilds($new_entries[$ref_id]['ref_id'],
383  $new_entries[$ref_id]['child']);
384  }
385  }
386  }
387 
388  function getUniqueResults()
389  {
390  return $this->results;
391  }
392 
393 }
394 ?>