search_suggestions.php 4.4 KB

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