lp_list_search.php 6.5 KB

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