search_suggestions.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Suggest words to search.
  5. *
  6. * @package chamilo.search
  7. */
  8. require_once __DIR__.'/../inc/global.inc.php';
  9. function get_suggestions_from_search_engine($q)
  10. {
  11. global $charset;
  12. $json = [];
  13. $table_sfv = Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD_VALUES);
  14. $q = Database::escape_string($q);
  15. $cid = api_get_course_id();
  16. $sql_add = '';
  17. if ($cid != -1) {
  18. $sql_add = " AND course_code = '".$cid."' ";
  19. }
  20. $sql = "SELECT * FROM $table_sfv where value LIKE '%$q%'".$sql_add."
  21. ORDER BY course_code, tool_id, ref_id, field_id";
  22. $sql_result = Database::query($sql);
  23. $data = [];
  24. $i = 0;
  25. while ($row = Database::fetch_array($sql_result)) {
  26. $json[] = [
  27. 'id' => api_convert_encoding($row['value'], 'UTF-8', $charset),
  28. 'value' => api_convert_encoding($row['value'], 'UTF-8', $charset),
  29. 'label' => api_convert_encoding($row['value'], 'UTF-8', $charset),
  30. ];
  31. if ($i < 20) {
  32. $data[$row['course_code']][$row['tool_id']][$row['ref_id']] = 1;
  33. }
  34. $i++;
  35. }
  36. // now that we have all the values corresponding to this search, we want to
  37. // make sure we get all the associated values that could match this one
  38. // initial value...
  39. $more_sugg = [];
  40. foreach ($data as $cc => $course_id) {
  41. foreach ($course_id as $ti => $item_tool_id) {
  42. foreach ($item_tool_id as $ri => $item_ref_id) {
  43. //natsort($item_ref_id);
  44. $output = [];
  45. $field_val = [];
  46. $sql2 = "SELECT * FROM $table_sfv
  47. WHERE course_code = '$cc' AND tool_id = '$ti' AND ref_id = '$ri'
  48. ORDER BY field_id";
  49. $res2 = Database::query($sql2);
  50. // TODO this code doesn't manage multiple terms in one same field just yet (should duplicate results in this case)
  51. $field_id = 0;
  52. while ($row2 = Database::fetch_array($res2)) {
  53. //TODO : this code is not perfect yet. It overrides the
  54. // first match set, so having 1:Yannick,Julio;2:Rectum;3:LASER
  55. // will actually never return: Yannick - Rectum - LASER
  56. // because it is overwriteen by Julio - Rectum - LASER
  57. // We should have recursivity here to avoid this problem!
  58. //Store the new set of results (only one per combination
  59. // of all fields)
  60. $field_val[$row2['field_id']] = $row2['value'];
  61. $current_field_val = '';
  62. foreach ($field_val as $id => $val) {
  63. $current_field_val .= $val.' - ';
  64. }
  65. //Check whether we have a field repetition or not. Results
  66. // have been ordered by field_id, so we should catch them
  67. // all here
  68. if ($field_id == $row2['field_id']) {
  69. //We found the same field id twice, split the output
  70. // array to allow for two sets of results (copy all
  71. // existing array elements into copies and update the
  72. // copies) eg. Yannick - Car - Driving in $output[1]
  73. // will create a copy as Yannick - Car - Speed
  74. // in $output[3]
  75. $c = count($output);
  76. for ($i = 0; $i < $c; $i++) {
  77. $output[($c + $i)] = $current_field_val;
  78. }
  79. } else {
  80. //no identical field id, continue as usual
  81. $c = count($output);
  82. if ($c == 0) {
  83. $output[] = $row2['value'].' - ';
  84. } else {
  85. foreach ($output as $i => $out) {
  86. //use the latest combination of fields
  87. $output[$i] .= $row2['value'].' - ';
  88. }
  89. }
  90. $field_id = $row2['field_id'];
  91. }
  92. }
  93. foreach ($output as $i => $out) {
  94. if (api_stristr($out, $q) === false) {
  95. continue;
  96. }
  97. $s = api_convert_encoding(substr($out, 0, -3), 'UTF-8', $charset);
  98. if (!in_array($s, $more_sugg)) {
  99. $more_sugg[] = $s;
  100. $json[] = [
  101. 'id' => $s,
  102. 'value' => $s,
  103. 'label' => $s,
  104. ];
  105. }
  106. }
  107. }
  108. }
  109. }
  110. echo json_encode($json);
  111. }
  112. if (isset($_GET['term'])) {
  113. $q = strtolower($_GET['term']);
  114. if (!$q) {
  115. return;
  116. }
  117. get_suggestions_from_search_engine($q);
  118. }