lp_list_search.php 6.7 KB

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