search_suggestions.php 5.4 KB

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