lp_list_search.php 6.0 KB

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