search_widget.php 15 KB

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