DokeosIndexer.class.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. require_once dirname(__FILE__) . '/../../global.inc.php';
  3. include_once 'xapian/XapianIndexer.class.php';
  4. /**
  5. * Class wrapper
  6. */
  7. class DokeosIndexer extends XapianIndexer {
  8. /**
  9. * Set terms on search_did given
  10. *
  11. * @param string $terms_string Comma-separated list of terms from input form
  12. * @param string $prefix Search engine prefix
  13. * @param string $course_code Course code
  14. * @param string $tool_id Tool id from mainapi.lib.php
  15. * @param int $ref_id_high_level Main id of the entity to index (Ex. lp_id)
  16. * @param int $ref_id_second_level Secondary id of the entity to index (Ex. lp_item)
  17. * @param int $search_did Search engine document id from search_engine_ref table
  18. * @return boolean False on error or nothing to do, true otherwise
  19. */
  20. function set_terms($terms_string, $prefix, $course_code, $tool_id, $ref_id_high_level, $ref_id_second_level, $search_did) {
  21. $terms_string = trim($terms_string);
  22. $terms = explode(',', $terms_string);
  23. array_walk($terms, 'trim_value');
  24. $stored_terms = $this->get_terms_on_db($prefix, $course_code, $tool_id, $ref_id_high_level);
  25. // don't do anything if no change, verify only at DB, not the search engine
  26. if ( (count(array_diff($terms, $stored_terms))==0) && (count(array_diff($stored_terms, $terms))==0) )
  27. return FALSE;
  28. require_once(api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php');
  29. // compare terms
  30. $doc = $this->get_document($search_did);
  31. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  32. $xterms = array();
  33. foreach ($xapian_terms as $xapian_term) $xterms[] = substr($xapian_term['name'],1);
  34. $dterms = $terms;
  35. $missing_terms = array_diff($dterms, $xterms);
  36. $deprecated_terms = array_diff($xterms, $dterms);
  37. // save it to search engine
  38. foreach ($missing_terms as $term)
  39. {
  40. $this->add_term_to_doc($prefix. $term, $doc);
  41. }
  42. foreach ($deprecated_terms as $term)
  43. {
  44. $this->remove_term_from_doc($prefix.$term, $doc);
  45. }
  46. // don't do anything if no change
  47. if ( (count($missing_terms) > 0) || (count($deprecated_terms) > 0)) {
  48. $this->replace_document($doc, (int)$search_did);
  49. }
  50. return TRUE;
  51. }
  52. /**
  53. * Get the terms stored at database
  54. * @return array Array of terms
  55. */
  56. function get_terms_on_db($prefix, $course_code, $tool_id, $ref_id) {
  57. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  58. $terms = get_specific_field_values_list_by_prefix($prefix, $course_code, $tool_id, $ref_id);
  59. $prefix_terms = array();
  60. foreach($terms as $term)
  61. {
  62. $prefix_terms[] = $term['value'];
  63. }
  64. return $prefix_terms;
  65. }
  66. }
  67. if (!function_exists('trim_value')) {
  68. function trim_value(&$value) {
  69. $value = trim($value);
  70. }
  71. }
  72. ?>