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