ChamiloQuery.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * @package chamilo.include.search
  6. */
  7. /**
  8. * Code
  9. */
  10. require 'xapian/XapianQuery.php';
  11. /**
  12. * Wrapper for queries
  13. *
  14. * @param string $query_string The search string
  15. * @param int $offset Offset to the first item to retrieve. Optional
  16. * @param int length Number of items to retrieve. Optional
  17. * @param array extra Extra queries to join with. Optional
  18. * @return array
  19. */
  20. function chamilo_query_query($query_string, $offset = 0, $length = 10, $extra = NULL) {
  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. return xapian_query($query_string, NULL, $offset, $length, $extra);
  26. }
  27. /**
  28. * Wrapper for getting boolean queries
  29. *
  30. * @param string $query_string The term string
  31. */
  32. function chamilo_get_boolean_query($term) {
  33. return xapian_get_boolean_query($term);
  34. }
  35. /**
  36. * Preprocess all results depending on the toolid
  37. */
  38. function chamilo_preprocess_results($results) {
  39. // group by toolid
  40. $results_by_tool = array();
  41. if (count($results) > 0) {
  42. foreach ($results as $key => $row) {
  43. $results_by_tool[$row['toolid']][] = $row;
  44. }
  45. $processed_results = array();
  46. foreach ($results_by_tool as $toolid => $rows) {
  47. $tool_processor_class = $toolid.'_processor';
  48. $tool_processor_path = api_get_path(LIBRARY_PATH).'search/tool_processors/'.$tool_processor_class.'.class.php';
  49. if (file_exists($tool_processor_path)) {
  50. require_once($tool_processor_path);
  51. $tool_processor = new $tool_processor_class($rows);
  52. $processed_results = array_merge($tool_processor->process(), $processed_results);
  53. }
  54. }
  55. return array(count($processed_results), $processed_results);
  56. }
  57. }
  58. /**
  59. * Wrapper for join xapian queries
  60. *
  61. * @param XapianQuery|array $query1
  62. * @param XapianQuery|array $query2
  63. * @param string $op
  64. * @return XapianQuery query joined
  65. */
  66. function chamilo_join_queries($query1, $query2 = NULL, $op = 'or') {
  67. return xapian_join_queries($query1, $query2, $op);
  68. }