lp_list_search.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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> Lots of bug fixing
  9. *
  10. */
  11. /**
  12. * Code
  13. */
  14. require api_get_path(LIBRARY_PATH).'search/search_widget.php';
  15. require api_get_path(LIBRARY_PATH).'search/ChamiloQuery.php';
  16. require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
  17. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  18. Event::event_access_tool(TOOL_SEARCH);
  19. if (isset($_SESSION['gradebook'])){
  20. $gradebook = $_SESSION['gradebook'];
  21. }
  22. if (!empty($gradebook) && $gradebook == 'view') {
  23. $interbreadcrumb[]= array (
  24. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  25. 'name' => get_lang('ToolGradebook')
  26. );
  27. }
  28. $interbreadcrumb[] = array('url' => './index.php', 'name' => get_lang(ucfirst(TOOL_SEARCH)));
  29. search_widget_prepare($htmlHeadXtra);
  30. Display::display_header(null, 'Path');
  31. if (api_get_setting('search_enabled') !== 'true') {
  32. Display::display_error_message(get_lang('SearchFeatureNotEnabledComment'));
  33. } else {
  34. if (!empty($_GET['action'])) {
  35. search_widget_show($_GET['action']);
  36. } else {
  37. search_widget_show();
  38. }
  39. }
  40. // Initialize.
  41. $op = 'or';
  42. if (!empty($_REQUEST['operator']) && in_array($op, array('or', 'and'))) {
  43. $op = $_REQUEST['operator'];
  44. }
  45. $query = stripslashes(htmlspecialchars_decode($_REQUEST['query'], ENT_QUOTES));
  46. $mode = 'default';
  47. if (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. $term_array[] = chamilo_get_boolean_query($term['name']); // Here name includes prefix.
  75. }
  76. }
  77. }
  78. }
  79. // Get right group of terms to show on multiple select.
  80. $fixed_queries = array();
  81. $course_filter = NULL;
  82. if ( ($cid=api_get_course_id()) != -1 ) {
  83. // Results only from actual course.
  84. $course_filter = chamilo_get_boolean_query(XAPIAN_PREFIX_COURSEID . $cid);
  85. }
  86. if (count($term_array)) {
  87. $fixed_queries = chamilo_join_queries($term_array, null, $op);
  88. if ($course_filter != NULL) {
  89. $fixed_queries = chamilo_join_queries($fixed_queries, $course_filter, 'and');
  90. }
  91. } else {
  92. if (!empty($query)) {
  93. $fixed_queries = array($course_filter);
  94. }
  95. }
  96. //var_dump($fixed_queries);
  97. list($count, $results) = chamilo_query_query(api_convert_encoding($query, 'UTF-8', $charset), 0, 1000, $fixed_queries);
  98. // Prepare blocks to show.
  99. $blocks = array();
  100. if ($count > 0) {
  101. foreach ($results as $result) {
  102. // Fill the result array.
  103. if (empty($result['thumbnail'])) {
  104. $result['thumbnail'] = Display::return_icon('no_document_thumb.jpg');
  105. }
  106. if (!empty($result['url'])) {
  107. $a_prefix = '<a href="'.$result['url'].'">';
  108. $a_sufix = '</a>';
  109. } else {
  110. $a_prefix = '';
  111. $a_sufix = '';
  112. }
  113. if ($mode == 'gallery') {
  114. $title = $a_prefix.str_replace('_',' ',$result['title']). $a_sufix;
  115. $blocks[] = array(
  116. $a_prefix .'<img src="'.$result['thumbnail'].'" />'. $a_sufix .'<br />'.$title.'<br />'.$result['author'],
  117. );
  118. } else {
  119. $title = '<div style="text-align:left;">'. $a_prefix . $result['title']. $a_sufix .(!empty($result['author']) ? ' '.$result['author'] : '').'<div>';
  120. $blocks[] = array($title);
  121. }
  122. }
  123. }
  124. // Show results.
  125. if (count($blocks) > 0) {
  126. $s = new SortableTableFromArray($blocks);
  127. $s->display_mode = $mode; // default
  128. $s->display_mode_params = 3;
  129. $s->per_page = 9;
  130. $additional_parameters = array (
  131. 'mode' => $mode,
  132. 'action' => 'search',
  133. 'query' => Security::remove_XSS($_REQUEST['query']),
  134. );
  135. $get_params = '';
  136. foreach ($specific_fields as $specific_field) {
  137. if (isset($_REQUEST[ 'sf_'. $specific_field['code'] ])) {
  138. $values = $_REQUEST[ 'sf_'. $specific_field['code'] ];
  139. //Sortable additional_parameters doesn't accept multi dimensional arrays
  140. //$additional_parameters[ 'sf_'. $specific_field['code'] ] = $values;
  141. foreach ( $values as $value ) {
  142. $get_params .= '&sf_' . $specific_field['code'] .'[]='. $value;
  143. }
  144. $get_params .= '&';
  145. }
  146. }
  147. $additional_parameters['operator'] = $op;
  148. $s->additional_parameters = $additional_parameters;
  149. if ($mode == 'default') {
  150. $s->set_header(0, get_lang(ucfirst(TOOL_SEARCH)), false);
  151. }
  152. $search_link = '<a href="%ssearch/index.php?mode=%s&action=search&query=%s%s">';
  153. $mode_selector = '<div id="mode-selector">';
  154. $mode_selector .= sprintf($search_link, api_get_path(WEB_CODE_PATH), 'gallery', $_REQUEST['query'], $get_params);
  155. $mode_selector .= Display::return_icon((($mode=='gallery')?'ButtonGallOn':'ButtonGallOff') .'.png').'</a>';
  156. $mode_selector .= sprintf($search_link, api_get_path(WEB_CODE_PATH), 'default', $_REQUEST['query'], $get_params);
  157. $mode_selector .= Display::return_icon((($mode=='default')?'ButtonListOn':'ButtonListOff') .'.png').'</a>';
  158. $mode_selector .= '</div>';
  159. echo '<div id="search-results-container">';
  160. echo $mode_selector;
  161. $s->display();
  162. echo '</div>';
  163. }
  164. Display::display_footer();