search_suggestions.php 4.5 KB

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