DokeosQuery.php 2.3 KB

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