ChamiloQuery.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script defining generic functions against a search engine api. Just only if one day the search engine changes.
  5. *
  6. * @package chamilo.include.search
  7. */
  8. require 'xapian/XapianQuery.php';
  9. /**
  10. * Wrapper for queries.
  11. *
  12. * @param string $query_string The search string
  13. * @param int $offset Offset to the first item to retrieve. Optional
  14. * @param int length Number of items to retrieve. Optional
  15. * @param array extra Extra queries to join with. Optional
  16. *
  17. * @return array
  18. */
  19. function chamilo_query_query($query_string, $offset = 0, $length = 10, $extra = null)
  20. {
  21. list($count, $results) = xapian_query($query_string, null, $offset, $length, $extra);
  22. return chamilo_preprocess_results($results);
  23. }
  24. function chamilo_query_simple_query($query_string, $offset = 0, $length = 10, $extra = null)
  25. {
  26. return xapian_query($query_string, null, $offset, $length, $extra);
  27. }
  28. /**
  29. * Wrapper for getting boolean queries.
  30. *
  31. * @param string $query_string The term string
  32. */
  33. function chamilo_get_boolean_query($term)
  34. {
  35. return xapian_get_boolean_query($term);
  36. }
  37. /**
  38. * Preprocess all results depending on the toolid.
  39. */
  40. function chamilo_preprocess_results($results)
  41. {
  42. // group by toolid
  43. $results_by_tool = [];
  44. if (count($results) > 0) {
  45. foreach ($results as $key => $row) {
  46. $results_by_tool[$row['toolid']][] = $row;
  47. }
  48. $processed_results = [];
  49. foreach ($results_by_tool as $toolid => $rows) {
  50. $tool_processor_class = $toolid.'_processor';
  51. $tool_processor_path = api_get_path(LIBRARY_PATH).'search/tool_processors/'.$tool_processor_class.'.class.php';
  52. if (file_exists($tool_processor_path)) {
  53. require_once $tool_processor_path;
  54. $tool_processor = new $tool_processor_class($rows);
  55. $processed_results = array_merge($tool_processor->process(), $processed_results);
  56. }
  57. }
  58. return [count($processed_results), $processed_results];
  59. }
  60. }
  61. /**
  62. * Wrapper for join xapian queries.
  63. *
  64. * @param XapianQuery|array $query1
  65. * @param XapianQuery|array $query2
  66. * @param string $op
  67. *
  68. * @return XapianQuery query joined
  69. */
  70. function chamilo_join_queries($query1, $query2 = null, $op = 'or')
  71. {
  72. return xapian_join_queries($query1, $query2, $op);
  73. }