search_widget.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Search widget. Shows the search screen contents.
  5. * @package chamilo.include.search
  6. */
  7. /**
  8. * Code
  9. */
  10. require_once dirname(__FILE__) . '/IndexableChunk.class.php';
  11. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  12. /**
  13. * Add some required CSS and JS to html's head.
  14. *
  15. * Note that $htmlHeadXtra should be passed by reference and not value,
  16. * otherwise this function will have no effect and your form will be broken.
  17. *
  18. * @param array $htmlHeadXtra A reference to the doc $htmlHeadXtra
  19. */
  20. function search_widget_prepare(&$htmlHeadXtra) {
  21. $htmlHeadXtra[] = '
  22. <!-- script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_JS_PATH).'jquery.autocomplete.js"></script -->
  23. <script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_JS_PATH).'search/search_widget.js"></script>
  24. <link rel="stylesheet" type="text/css" href="'.api_get_path(
  25. WEB_LIBRARY_JS_PATH
  26. ).'jquery.autocomplete.css" />
  27. <link rel="stylesheet" type="text/css" href="'.api_get_path(
  28. WEB_LIBRARY_JS_PATH
  29. ).'search/search_widget.css" />';
  30. }
  31. /**
  32. * Get one term html select
  33. */
  34. function format_one_specific_field_select($prefix, $sf_term_array, $op, $extra_select_attr='size="7" class="sf-select-multiple"') {
  35. global $charset;
  36. $multiple_select = '<select '. $extra_select_attr .' title="'. $prefix .'" id="sf-'. $prefix .'" name="sf_'. $prefix .'[]">';
  37. $all_selected = '';
  38. if (!empty($_REQUEST['sf_'. $prefix]) ) {
  39. if (in_array('__all__', $_REQUEST['sf_'. $prefix])) {
  40. $all_selected = 'selected="selected"';
  41. }
  42. }
  43. if ($op == 'and') {
  44. $all_selected_name = get_lang('All');
  45. } else if ($op == 'or') {
  46. $all_selected_name = get_lang('Any');
  47. }
  48. $multiple_select .= '<option value="__all__" '. $all_selected .' >-- '. $all_selected_name .' --</option>';
  49. foreach ($sf_term_array as $raw_term) {
  50. $term = substr($raw_term, 1);
  51. if (empty($term)) continue;
  52. $html_term = htmlspecialchars($term, ENT_QUOTES, $charset);
  53. $selected = '';
  54. if (!empty($_REQUEST['sf_'.$prefix]) && is_array($_REQUEST['sf_'.$prefix]) && in_array($term,$_REQUEST['sf_'.$prefix])) {
  55. $selected = 'selected="selected"';
  56. }
  57. $multiple_select .= '<option value="'. $html_term .'" '.$selected.'>'. $html_term .'</option>';
  58. }
  59. $multiple_select .= '</select>';
  60. return $multiple_select;
  61. }
  62. /**
  63. * Get terms html selects
  64. */
  65. function format_specific_fields_selects($sf_terms, $op, $prefilter_prefix='') {
  66. // Process each prefix type term
  67. $i = 0;
  68. $max = count($sf_terms);
  69. $multiple_selects ='';
  70. foreach ($sf_terms as $prefix => $sf_term_array) {
  71. if ($prefix == $prefilter_prefix) continue;
  72. $multiple_select = '';
  73. if ($i>0) {
  74. //print "+" image
  75. $multiple_select .= '<td><img class="sf-select-splitter" src="../img/search-big-plus.gif" alt="plus-sign-decoration"/></td>';
  76. }
  77. //sorting the array of terms
  78. $temp = array();
  79. if (!empty($sf_term_array)) {
  80. foreach ($sf_term_array as $key => $value) {
  81. $temp[trim(stripslashes($value['name']))] = $key;
  82. }
  83. }
  84. $temp = array_flip($temp);
  85. unset($sf_term_array);
  86. natcasesort($temp);
  87. $sf_term_array = $temp;
  88. $sf_copy = $sf_term_array;
  89. // get specific field name
  90. $sf_value = get_specific_field_list(array( 'code' => "'$prefix'" ));
  91. $sf_value = array_shift($sf_value);
  92. $multiple_select .= '<td><label class="sf-select-multiple-title" for="sf_'. $prefix .'[]">' . $sf_value['name'].'</label><br />';
  93. $multiple_select .= format_one_specific_field_select($prefix, $sf_term_array, $op, 'multiple="multiple" size="7" class="sf-select-multiple"');
  94. $multiple_select .= '</td>';
  95. $multiple_selects .= $multiple_select;
  96. $i++;
  97. }
  98. return $multiple_selects;
  99. }
  100. /**
  101. * Build the normal form.
  102. *
  103. * First, natural way.
  104. */
  105. function search_widget_normal_form($action, $show_thesaurus, $sf_terms, $op) {
  106. $thesaurus_icon = Display::return_icon('thesaurus.gif', get_lang('SearchAdvancedOptions'), array('id'=>'thesaurus-icon'));
  107. $advanced_options = '<a id="tags-toggle" href="#">'. get_lang('SearchAdvancedOptions') .'</a>';
  108. $display_thesaurus = ($show_thesaurus==true? 'block': 'none');
  109. $help = '<h3>'. get_lang('SearchKeywordsHelpTitle') .'</h3>'. get_lang('SearchKeywordsHelpComment');
  110. $mode = (!empty($_REQUEST['mode'])? htmlentities($_REQUEST['mode']): 'gallery');
  111. $type = (!empty($_REQUEST['type'])? htmlentities($_REQUEST['type']): 'normal');
  112. /**
  113. * POST avoid long urls, but we are using GET because
  114. * SortableTableFromArray pagination is done with simple links, so now we
  115. * could not send a form in pagination
  116. */
  117. if (isset($_GET['action']) && strcmp(trim($_GET['action']),'search')===0) {
  118. $action='index.php';
  119. }
  120. $navigator_info = api_get_navigator();
  121. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  122. $submit_button1 = '<input type="submit" id="submit" value="'. get_lang('Search') .'" />';
  123. $submit_button2 = '<input class="lower-submit" type="submit" value="'. get_lang('Search') .'" />';
  124. $reset_button = '<input type="submit" id="tags-clean" value="'. get_lang('SearchResetKeywords') .'" />';
  125. } else {
  126. $submit_button1 = '<button class="search" type="submit" id="submit" value="'. get_lang("Search") .'" /> '. get_lang('Search') .'</button>';
  127. $submit_button2 = '<button class="search" type="submit" value="'. get_lang('Search') .'" />'. get_lang('Search') .'</button>';
  128. $reset_button = '<button class="save" type="submit" id="tags-clean" value="'. get_lang('SearchResetKeywords') .'" />'. get_lang('SearchResetKeywords') .'</button> ';
  129. }
  130. $query = isset($_REQUEST['query']) ? Security::remove_XSS($_REQUEST['query']) : null;
  131. $form = '<form id="chamilo_search" action="'. $action .'" method="GET">
  132. <input type="text" id="query" name="query" size="40" value="' . $query . '" />
  133. <input type="hidden" name="mode" value="'. $mode .'"/>
  134. <input type="hidden" name="type" value="'. $type .'"/>
  135. <input type="hidden" name="tablename_page_nr" value="1" />
  136. '.$submit_button1.'
  137. <br /><br />';
  138. $list = get_specific_field_list();
  139. if(!empty($list)) {
  140. $form .= '<span class="search-links-box">'. $advanced_options .'&nbsp;</span>
  141. <div id="tags" class="tags" style="display:'. $display_thesaurus .';">
  142. <div class="search-help-box">'. $help .'</div>
  143. <table>
  144. <tr>';
  145. $form .= format_specific_fields_selects($sf_terms, $op);
  146. $or_checked = '';
  147. $and_checked = '';
  148. if ($op == 'or') {
  149. $or_checked = 'checked="checked"';
  150. } else if ($op == 'and') {
  151. $and_checked = 'checked="checked"';
  152. }
  153. $form .= '</tr>
  154. <tr>
  155. <td id="operator-select">
  156. '. get_lang('SearchCombineSearchWith') .':<br />
  157. <input type="radio" class="search-operator" name="operator" value="or" '. $or_checked .'>'. api_strtoupper(get_lang('Or')) .'</input>
  158. <input type="radio" class="search-operator" name="operator" value="and" '. $and_checked .'>'. api_strtoupper(get_lang('And')) .'</input>
  159. </td>
  160. <td></td>
  161. <td>
  162. <br />
  163. '.$reset_button.'
  164. '. $submit_button2.'
  165. </td>
  166. </tr>
  167. </table>
  168. </div>';
  169. }
  170. $form .='</form>
  171. <br style="clear: both;"/>';
  172. return $form;
  173. }
  174. /**
  175. * Build the prefilter form.
  176. *
  177. * This type allow filter all other multiple select terms by one term in a dinamic way
  178. */
  179. function search_widget_prefilter_form($action, $show_thesaurus, $sf_terms, $op, $prefilter_prefix=NULL) {
  180. $thesaurus_icon = Display::return_icon('thesaurus.gif', get_lang('SearchAdvancedOptions'), array('id'=>'thesaurus-icon'));
  181. $advanced_options = '<a id="tags-toggle" href="#">'. get_lang('SearchAdvancedOptions') .'</a>';
  182. $display_thesaurus = ($show_thesaurus==true? 'block': 'none');
  183. $help = '<h3>'. get_lang('SearchKeywordsHelpTitle') .'</h3>'. get_lang('SearchKeywordsHelpComment');
  184. $mode = (!empty($_REQUEST['mode'])? htmlentities($_REQUEST['mode']): 'gallery');
  185. $type = (!empty($_REQUEST['type'])? htmlentities($_REQUEST['type']): 'normal');
  186. /**
  187. * POST avoid long urls, but we are using GET because
  188. * SortableTableFromArray pagination is done with simple links, so now we
  189. * could not send a form in pagination
  190. */
  191. if (isset($_GET['action']) && strcmp(trim($_GET['action']),'search')===0) {
  192. $action='index.php';
  193. }
  194. $form = '
  195. <form id="chamilo_search" action="'. $action .'" method="GET">
  196. <input type="text" id="query" name="query" size="40" />
  197. <input type="hidden" name="mode" value="'. $mode .'"/>
  198. <input type="hidden" name="type" value="'. $type .'"/>
  199. <input type="hidden" name="tablename_page_nr" value="1" />
  200. <input type="submit" id="submit" value="'. get_lang("Search") .'" />
  201. <br /><br />';
  202. $list = get_specific_field_list();
  203. if(!empty($list)) {
  204. $form .=' <span class="search-links-box">'. $thesaurus_icon . $advanced_options .'&nbsp;</span>
  205. <div id="tags" class="tags" style="display:'. $display_thesaurus .';">
  206. <div class="search-help-box">'. $help .'</div>
  207. <table>
  208. <tr>';
  209. if (!is_null($prefilter_prefix)) {
  210. //sorting the array of terms
  211. $temp = array();
  212. foreach ($sf_terms[$prefilter_prefix] as $key => $value) {
  213. $temp[trim(stripslashes($value['name']))] = $key;
  214. }
  215. $temp = array_flip($temp);
  216. unset($sf_term_array);
  217. natcasesort($temp);
  218. $sf_term_array = $temp;
  219. // get specific field name
  220. $sf_value = get_specific_field_list(array( 'code' => "'$prefilter_prefix'" ));
  221. $sf_value = array_shift($sf_value);
  222. $form .= '<label class="sf-select-multiple-title" for="sf_'. $prefix .'[]">'.$icons_for_search_terms[$prefix].' '.$sf_value['name'].'</label><br />';
  223. $form .= format_one_specific_field_select($prefilter_prefix, $sf_term_array, $op, 'id="prefilter"');
  224. $form .= format_specific_fields_selects($sf_terms, $op, $prefilter_prefix);
  225. } else {
  226. $form .= format_specific_fields_selects($sf_terms, $op);
  227. }
  228. $or_checked = '';
  229. $and_checked = '';
  230. if ($op == 'or') {
  231. $or_checked = 'checked="checked"';
  232. } else if ($op == 'and') {
  233. $and_checked = 'checked="checked"';
  234. }
  235. $form .= '
  236. </tr>
  237. <tr>
  238. <td id="operator-select">
  239. '. get_lang('SearchCombineSearchWith') .':<br />
  240. <input type="radio" class="search-operator" name="operator" value="or" '. $or_checked .'>'. api_strtoupper(get_lang('Or')) .'</input>
  241. <input type="radio" class="search-operator" name="operator" value="and" '. $and_checked .'>'. api_strtoupper(get_lang('And')) .'</input>
  242. </td>
  243. <td></td>
  244. <td>
  245. <br />
  246. <input class="lower-submit" type="submit" value="'. get_lang('Search') .'" />
  247. <input type="submit" id="tags-clean" value="'. get_lang('SearchResetKeywords') .'" />
  248. </td>
  249. </tr>
  250. </table>
  251. </div>';
  252. }
  253. $form .= '
  254. </form>
  255. <br style="clear: both;"/>';
  256. return $form;
  257. }
  258. /**
  259. * Show search form
  260. */
  261. function display_search_form($action, $show_thesaurus, $sf_terms, $op) {
  262. $type = (!empty($_REQUEST['type'])? htmlentities($_REQUEST['type']): 'normal');
  263. switch ($type) {
  264. case 'prefilter':
  265. $prefilter_prefix = api_get_setting('search_prefilter_prefix');
  266. $form = search_widget_prefilter_form($action, $show_thesaurus, $sf_terms, $op, $prefilter_prefix);
  267. break;
  268. case 'normal':
  269. default:
  270. $form = search_widget_normal_form($action, $show_thesaurus, $sf_terms, $op);
  271. }
  272. // show built form
  273. echo $form;
  274. }
  275. /**
  276. * Show the search widget
  277. *
  278. * The form will post to index.php by default, you can pass a value to
  279. * $action to use a custom action.
  280. * IMPORTANT: you have to call search_widget_prepare() before calling this
  281. * function or otherwise the form will not behave correctly.
  282. *
  283. * @param string $action Just in case your action is not
  284. * index.php
  285. */
  286. function search_widget_show($action='index.php')
  287. {
  288. require_once api_get_path(LIBRARY_PATH).'search/ChamiloQuery.php';
  289. // TODO: load images dinamically when they're avalaible from specific field ui to add
  290. $groupId = api_get_group_id();
  291. $sf_terms = array();
  292. $specific_fields = get_specific_field_list();
  293. $url_params = array();
  294. if (($cid = api_get_course_id()) != -1) { // with cid
  295. // get search engine terms
  296. $course_filter = chamilo_get_boolean_query(XAPIAN_PREFIX_COURSEID . $cid);
  297. $dkterms = chamilo_query_simple_query('', 0, 1000, array($course_filter));
  298. //prepare specific fields names (and also get possible URL param names)
  299. foreach ($specific_fields as $specific_field) {
  300. $temp = array();
  301. if (is_array($dkterms) && count($dkterms)>0) {
  302. foreach($dkterms[1] as $obj) {
  303. $temp = array_merge($obj['sf-'.$specific_field['code']], $temp);
  304. }
  305. }
  306. $sf_terms[$specific_field['code']] = $temp;
  307. $url_params[] = 'sf_'.$specific_field['code'];
  308. unset($temp);
  309. }
  310. } else { // without cid
  311. // prepare specific fields names (and also get possible URL param names)
  312. foreach ($specific_fields as $specific_field) {
  313. //get Xapian terms for a specific term prefix, in ISO, apparently
  314. $sf_terms[$specific_field['code']] = xapian_get_all_terms(1000, $specific_field['code']);
  315. $url_params[] = 'sf_'.$specific_field['code'];
  316. }
  317. }
  318. echo '<h2>'.get_lang('Search').'</h2>';
  319. // Tool introduction
  320. // TODO: Settings for the online editor to be checked (insert an image for example). Probably this is a special case here.
  321. if (api_get_course_id() !== -1)
  322. if (!empty($groupId)) {
  323. Display::display_introduction_section(TOOL_SEARCH.$groupId);
  324. } else {
  325. Display::display_introduction_section(TOOL_SEARCH);
  326. }
  327. $op = 'or';
  328. if (!empty($_REQUEST['operator']) && in_array($op,array('or','and'))) {
  329. $op = $_REQUEST['operator'];
  330. }
  331. //check if URL params are defined (to see if we show the thesaurus or not)
  332. $show_thesaurus = false;
  333. foreach ($url_params as $param) {
  334. if (isset($_REQUEST[$param]) && is_array($_REQUEST[$param])) {
  335. $thesaurus_decided = FALSE;
  336. foreach ($_REQUEST[$param] as $term) {
  337. if (!empty($term)) {
  338. $show_thesaurus = true;
  339. $thesaurus_decided = TRUE;
  340. break;
  341. }
  342. }
  343. if ($thesaurus_decided) break;
  344. }
  345. }
  346. // create the form
  347. // TODO: use FormValidator
  348. display_search_form($action, $show_thesaurus, $sf_terms, $op);
  349. }