search_suggestions.php 4.5 KB

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