lp_list_search.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Script to draw the results from a query
  5. * @package chamilo.learnpath
  6. * @author Diego Escalante Urrelo <diegoe@gmail.com>
  7. * @author Marco Antonio Villegas Vega <marvil07@gmail.com>
  8. * @author Julio Montoya <gugli100@gmail.com> bug fixing
  9. *
  10. */
  11. require api_get_path(LIBRARY_PATH).'search/search_widget.php';
  12. require api_get_path(LIBRARY_PATH).'search/ChamiloQuery.php';
  13. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  14. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  15. Event::event_access_tool(TOOL_SEARCH);
  16. if (isset($_SESSION['gradebook'])) {
  17. $gradebook = $_SESSION['gradebook'];
  18. }
  19. if (!empty($gradebook) && $gradebook == 'view') {
  20. $interbreadcrumb[] = array(
  21. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  22. 'name' => get_lang('ToolGradebook')
  23. );
  24. }
  25. $interbreadcrumb[] = array('url' => './index.php', 'name' => get_lang(ucfirst(TOOL_SEARCH)));
  26. search_widget_prepare($htmlHeadXtra);
  27. Display::display_header(null, 'Path');
  28. if (api_get_setting('search_enabled') !== 'true') {
  29. echo Display::return_message(get_lang('SearchFeatureNotEnabledComment'), 'error');
  30. } else {
  31. if (!empty($_GET['action'])) {
  32. search_widget_show($_GET['action']);
  33. } else {
  34. search_widget_show();
  35. }
  36. }
  37. // Initialize.
  38. $op = 'or';
  39. if (!empty($_REQUEST['operator']) && in_array($op, array('or', 'and'))) {
  40. $op = $_REQUEST['operator'];
  41. }
  42. $query = null;
  43. if (isset($_REQUEST['query'])) {
  44. $query = stripslashes(htmlspecialchars_decode($_REQUEST['query'], ENT_QUOTES));
  45. }
  46. $mode = 'default';
  47. if (isset($_GET['mode']) && in_array($_GET['mode'], array('gallery', 'default'))) {
  48. $mode = $_GET['mode'];
  49. }
  50. $term_array = array();
  51. $specific_fields = get_specific_field_list();
  52. foreach ($specific_fields as $specific_field) {
  53. if (!empty($_REQUEST['sf_'.$specific_field['code']])) {
  54. $values = $_REQUEST['sf_'.$specific_field['code']];
  55. if (in_array('__all__', $values)) {
  56. $sf_terms_for_code = xapian_get_all_terms(1000, $specific_field['code']);
  57. foreach ($sf_terms_for_code as $term) {
  58. if (!empty($term)) {
  59. $term_array[] = chamilo_get_boolean_query($term['name']); // Here name includes prefix.
  60. }
  61. }
  62. } else {
  63. foreach ($values as $term) {
  64. if (!empty($term)) {
  65. $prefix = $specific_field['code'];
  66. $term_array[] = chamilo_get_boolean_query($prefix.$term);
  67. }
  68. }
  69. }
  70. } else {
  71. $sf_terms_for_code = xapian_get_all_terms(1000, $specific_field['code']);
  72. foreach ($sf_terms_for_code as $term) {
  73. if (!empty($term)) {
  74. // Here name includes prefix.
  75. $term_array[] = chamilo_get_boolean_query($term['name']);
  76. }
  77. }
  78. }
  79. }
  80. // Get right group of terms to show on multiple select.
  81. $fixed_queries = array();
  82. $course_filter = null;
  83. if (($cid = api_get_course_id()) != -1) {
  84. // Results only from actual course.
  85. $course_filter = chamilo_get_boolean_query(XAPIAN_PREFIX_COURSEID.$cid);
  86. }
  87. if (count($term_array)) {
  88. $fixed_queries = chamilo_join_queries($term_array, null, $op);
  89. if ($course_filter != null) {
  90. $fixed_queries = chamilo_join_queries(
  91. $fixed_queries,
  92. $course_filter,
  93. 'and'
  94. );
  95. }
  96. } else {
  97. if (!empty($query)) {
  98. $fixed_queries = array($course_filter);
  99. }
  100. }
  101. if ($query) {
  102. list($count, $results) = chamilo_query_query(
  103. api_convert_encoding($query, 'UTF-8', $charset),
  104. 0,
  105. 1000,
  106. $fixed_queries
  107. );
  108. } else {
  109. $count = 0;
  110. $results = [];
  111. }
  112. // Prepare blocks to show.
  113. $blocks = array();
  114. if ($count > 0) {
  115. foreach ($results as $result) {
  116. // Fill the result array.
  117. if (empty($result['thumbnail'])) {
  118. $result['thumbnail'] = Display::returnIconPath('no_document_thumb.jpg');
  119. }
  120. if (!empty($result['url'])) {
  121. $a_prefix = '<a href="'.$result['url'].'">';
  122. $a_suffix = '</a>';
  123. } else {
  124. $a_prefix = '';
  125. $a_suffix = '';
  126. }
  127. if ($mode == 'gallery') {
  128. $title = $a_prefix.str_replace('_', ' ', $result['title']).$a_suffix;
  129. $blocks[] = array(1 =>
  130. $a_prefix.'<img src="'.$result['thumbnail'].'" />'.$a_suffix.'<br />'.$title.'<br />'.$result['author'],
  131. );
  132. } else {
  133. $title = '<div style="text-align:left;">'.$a_prefix.$result['title'].$a_suffix.(!empty($result['author']) ? ' '.$result['author'] : '').'<div>';
  134. $blocks[] = array(1 => $title);
  135. }
  136. }
  137. }
  138. // Show results.
  139. if (count($blocks) > 0) {
  140. $s = new SortableTableFromArray($blocks);
  141. $s->display_mode = $mode; // default
  142. $s->display_mode_params = 3;
  143. $s->per_page = 9;
  144. $additional_parameters = array(
  145. 'mode' => $mode,
  146. 'action' => 'search',
  147. 'query' => Security::remove_XSS($_REQUEST['query']),
  148. );
  149. $get_params = '';
  150. foreach ($specific_fields as $specific_field) {
  151. if (isset($_REQUEST['sf_'.$specific_field['code']])) {
  152. $values = $_REQUEST['sf_'.$specific_field['code']];
  153. //Sortable additional_parameters doesn't accept multi dimensional arrays
  154. //$additional_parameters[ 'sf_'. $specific_field['code'] ] = $values;
  155. foreach ($values as $value) {
  156. $get_params .= '&sf_'.$specific_field['code'].'[]='.$value;
  157. }
  158. $get_params .= '&';
  159. }
  160. }
  161. $additional_parameters['operator'] = $op;
  162. $s->additional_parameters = $additional_parameters;
  163. if ($mode == 'default') {
  164. $s->set_header(0, get_lang(ucfirst(TOOL_SEARCH)), false);
  165. }
  166. $search_link = '<a href="%ssearch/index.php?mode=%s&action=search&query=%s%s">';
  167. $iconGallery = (($mode == 'gallery') ? 'ButtonGallOn' : 'ButtonGallOff').'.png';
  168. $iconDefault = (($mode == 'default') ? 'ButtonListOn' : 'ButtonListOff').'.png';
  169. $mode_selector = '<div id="mode-selector">';
  170. $mode_selector .= sprintf($search_link, api_get_path(WEB_CODE_PATH), 'gallery', $_REQUEST['query'], $get_params);
  171. $mode_selector .= Display::return_icon($iconGallery).'</a>';
  172. $mode_selector .= sprintf($search_link, api_get_path(WEB_CODE_PATH), 'default', $_REQUEST['query'], $get_params);
  173. $mode_selector .= Display::return_icon($iconDefault).'</a>';
  174. $mode_selector .= '</div>';
  175. echo '<div id="search-results-container">';
  176. echo $mode_selector;
  177. $s->display();
  178. echo '</div>';
  179. }
  180. Display::display_footer();