ChamiloIndexer.class.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class wrapper.
  5. */
  6. class ChamiloIndexer extends XapianIndexer
  7. {
  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. *
  19. * @return bool False on error or nothing to do, true otherwise
  20. */
  21. public function set_terms(
  22. $terms_string,
  23. $prefix,
  24. $course_code,
  25. $tool_id,
  26. $ref_id_high_level,
  27. $ref_id_second_level,
  28. $search_did
  29. ) {
  30. $terms_string = trim($terms_string);
  31. $terms = explode(',', $terms_string);
  32. array_walk($terms, 'trim_value');
  33. $stored_terms = $this->get_terms_on_db($prefix, $course_code, $tool_id, $ref_id_high_level);
  34. // don't do anything if no change, verify only at DB, not the search engine
  35. if ((count(array_diff($terms, $stored_terms)) == 0) &&
  36. (count(array_diff($stored_terms, $terms)) == 0)
  37. ) {
  38. return false;
  39. }
  40. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  41. // compare terms
  42. $doc = $this->get_document($search_did);
  43. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  44. $xterms = [];
  45. foreach ($xapian_terms as $xapian_term) {
  46. $xterms[] = substr($xapian_term['name'], 1);
  47. }
  48. $dterms = $terms;
  49. $missing_terms = array_diff($dterms, $xterms);
  50. $deprecated_terms = array_diff($xterms, $dterms);
  51. // save it to search engine
  52. foreach ($missing_terms as $term) {
  53. $this->add_term_to_doc($prefix.$term, $doc);
  54. }
  55. foreach ($deprecated_terms as $term) {
  56. $this->remove_term_from_doc($prefix.$term, $doc);
  57. }
  58. // don't do anything if no change
  59. if ((count($missing_terms) > 0) || (count($deprecated_terms) > 0)) {
  60. $this->replace_document($doc, (int) $search_did);
  61. }
  62. return true;
  63. }
  64. /**
  65. * Get the terms stored at database.
  66. *
  67. * @return array Array of terms
  68. */
  69. public function get_terms_on_db($prefix, $course_code, $tool_id, $ref_id)
  70. {
  71. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  72. $terms = get_specific_field_values_list_by_prefix(
  73. $prefix,
  74. $course_code,
  75. $tool_id,
  76. $ref_id
  77. );
  78. $prefix_terms = [];
  79. foreach ($terms as $term) {
  80. $prefix_terms[] = $term['value'];
  81. }
  82. return $prefix_terms;
  83. }
  84. }