search_suggestions.php 4.9 KB

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