Browse Source

[svn r17960] search widget: some improvements and prefilter type

- improving the way parameters are passed by
  - this allow creating more than one type of search widget
- prefilter type
  - allow filter by one specific field the other specific fields at search widget (it manages all "normal forms" like including course id and All/Any option by a ajax call)
Marco Villegas 16 years ago
parent
commit
ff9ee53f6d

+ 67 - 22
main/inc/lib/search/get_terms.php

@@ -5,42 +5,87 @@
  * This script retrieves a list of terms that have xapian documents
  * related with the term passed
  */
-$items = array();
+$terms_list = array();
 
 // verify parameter and return a right value to avoid problems parsing it
 if (empty($_GET['term']) || empty($_GET['prefix']) || !in_array($_GET['operator'], array('or', 'and'))) {
-    echo json_encode($array);
+    echo json_encode($terms_list);
     return;
 }
 
-$term = $_GET['term'];
-$prefix = $_GET['prefix'];
-$operator = $_GET['operator'];
-
 require_once dirname(__FILE__) . '../../../global.inc.php';
 require_once api_get_path(LIBRARY_PATH).'/search/DokeosQuery.php';
-//TODO: manage Any/All
-$term_filter = dokeos_get_boolean_query($prefix . $term);
-$dkterms = dokeos_query_simple_query('', 0, 1000, array($term_filter));
-if (is_array($dkterms) && is_array($dkterms[1])) {
-    $specific_fields = get_specific_field_list();
+
+/**
+ * search with filter and build base array avoding repeated terms
+ * @param array $filter XapianQuery array
+ * @param array $specific_fields
+ * @return array $sf_terms
+ */
+function get_usual_sf_terms($filter, $specific_fields) {
     $sf_terms = array();
-    // build base array avoding repeated terms
-    foreach ($specific_fields as $specific_field) {
-        foreach($dkterms[1] as $obj) {
-            foreach ($obj['sf-'.$specific_field['code']] as $raw_term) {
-                if (count($raw_term) > 1) {
-                    $normal_term = substr($raw_term['name'], 1);
-                    $sf_terms[$specific_field['code']][$normal_term] = $normal_term;
+    $dkterms = dokeos_query_simple_query('', 0, 1000, $filter);
+
+    if (is_array($dkterms) && is_array($dkterms[1])) {
+        // 
+        foreach ($specific_fields as $specific_field) {
+            foreach($dkterms[1] as $obj) {
+                foreach ($obj['sf-'.$specific_field['code']] as $raw_term) {
+                    if (count($raw_term) > 1) {
+                        $normal_term = substr($raw_term['name'], 1);
+                        $sf_terms[$specific_field['code']][$normal_term] = $normal_term;
+                    }
                 }
             }
         }
     }
-    // build array to return
-    foreach ($sf_terms as $prefix => $term_group) {
-        $first_term = array('__all__' => ($operator=='or'? '-- Any --': '-- All -- '));
+    return $sf_terms;
+}
+
+$term = $_GET['term'];
+$prefix = $_GET['prefix'];
+$operator = $_GET['operator'];
+
+$specific_fields = get_specific_field_list();
+$sf_terms = array();
+
+if ( ($cid=api_get_course_id()) != -1) { // with cid
+    // course filter
+    $filter[] = dokeos_get_boolean_query(XAPIAN_PREFIX_COURSEID . $cid);
+    // term filter
+    if ($term != '__all__') {
+        $filter[] = dokeos_get_boolean_query($prefix . $term);
+        // always and between term and courseid
+        $filter = dokeos_join_queries($filter, null, 'and');
+    }
+
+    $sf_terms = get_usual_sf_terms($filter, $specific_fields);
+
+} else { // without cid
+    if ($term != '__all__') {
+        $filter[] = dokeos_get_boolean_query($prefix . $term);
+
+        $sf_terms = get_usual_sf_terms($filter, $specific_fields);
+        
+    } else { // no cid and all/any terms
+        foreach ($specific_fields as $specific_field) {
+            foreach(xapian_get_all_terms(1000, $specific_field['code']) as $raw_term) {
+                $normal_term = substr($raw_term['name'], 1);
+                $sf_terms[$specific_field['code']][$normal_term] = $normal_term;
+            }
+        }
+    }
+
+}
+
+// build array to return
+foreach ($sf_terms as $sf_prefix => $term_group) {
+    //if (count($tem_group) > 0) {
+    $first_term = array('__all__' => ($operator=='or'? '-- Any --': '-- All -- '));
+    //}
+    if ($sf_prefix != $prefix) {
         $terms_list[] = array(
-            'prefix' => $prefix,
+            'prefix' => $sf_prefix,
             'terms' => array_merge($first_term, $term_group),
         );
     }

+ 20 - 0
main/inc/lib/search/search_widget.js

@@ -19,4 +19,24 @@ $(document).ready(function() {
         mustMatch: false,
         autoFill: false
       });
+      /* prefilter form */
+      $("#tags").show();
+      $('#prefilter').change(function () {
+        var str = "";
+        $("#prefilter option:selected").each(function () {
+            str += $(this).text() + " ";
+        });
+        process_terms = function(data) {
+            $(".sf-select-multiple").html("");
+            $.each(data, function(i,item) {
+                $.each(item.terms, function(index, term) {
+                    $('<option />').val(index).text(term).appendTo("#sf-" + item.prefix);
+                });
+            });
+        };
+        url = "/main/inc/lib/search/get_terms.php";
+        params = "?term=" + $(this).val() + "&prefix=" + $(this).attr("title") + "&operator=" + $("input[@name=operator]:checked").val();
+        $.getJSON(url + params, process_terms);
+      });
+
 });

+ 116 - 9
main/inc/lib/search/search_widget.php

@@ -29,8 +29,8 @@ function search_widget_prepare(&$htmlHeadXtra) {
 /**
  * Get one term html select
  */
-function format_one_specific_field_select($prefix, $sf_term_array, $op) {
-    $multiple_select .= '<select multiple="multiple" size="7" class="sf-select-multiple" name="sf_'. $prefix .'[]">';
+function format_one_specific_field_select($prefix, $sf_term_array, $op, $extra_select_attr='size="7" class="sf-select-multiple"') {
+    $multiple_select .= '<select '. $extra_select_attr .' title="'. $prefix .'" id="sf-'. $prefix .'" name="sf_'. $prefix .'[]">';
 
     $all_selected = '';
     if (!empty($_REQUEST['sf_'. $prefix]) ) {
@@ -62,12 +62,13 @@ function format_one_specific_field_select($prefix, $sf_term_array, $op) {
 /**
  * Get terms html selects
  */
-function format_specific_fields_selects($sf_terms, $op) {
+function format_specific_fields_selects($sf_terms, $op, $prefilter_prefix='') {
     // Process each prefix type term
     $i = 0;
     $max = count($sf_terms);
     $multiple_selects .='';
     foreach ($sf_terms as $prefix => $sf_term_array) {
+        if ($prefix == $prefilter_prefix) continue;
         $multiple_select = '';
         if ($i>0) {
             //print "+" image
@@ -88,7 +89,7 @@ function format_specific_fields_selects($sf_terms, $op) {
         $sf_value = get_specific_field_list(array( 'code' => "'$prefix'" ));
         $sf_value = array_shift($sf_value);
         $multiple_select .= '<td><label class="sf-select-multiple-title" for="sf_'. $prefix .'[]">'.$icons_for_search_terms[$prefix].' '.$sf_value['name'].'</label><br />';
-        $multiple_select .= format_one_specific_field_select($prefix, $sf_term_array, $op);
+        $multiple_select .= format_one_specific_field_select($prefix, $sf_term_array, $op, 'multiple="multiple" size="7" class="sf-select-multiple"');
         $multiple_select .= '</td>';
         $multiple_selects .= $multiple_select;
         $i++;
@@ -97,25 +98,28 @@ function format_specific_fields_selects($sf_terms, $op) {
 }
 
 /**
- * Show search form
+ * Build the normal form.
+ *
+ * First, natural way.
  */
-function display_search_form($action, $show_thesaurus, $sf_terms, $op) {
+function search_widget_normal_form($action, $show_thesaurus, $sf_terms, $op) {
     $thesaurus_icon = Display::return_icon('thesaurus.gif', get_lang('SearchAdvancedOptions'), array('id'=>'thesaurus-icon'));
     $advanced_options = '<a id="tags-toggle" href="#">'.  get_lang('SearchAdvancedOptions') .'</a>';
     $display_thesaurus = ($show_thesaurus==true? 'block': 'none');
     $help = '<h3>'. get_lang('SearchKeywordsHelpTitle') .'</h3>'. get_lang('SearchKeywordsHelpComment');
     $mode = (!empty($_REQUEST['mode'])? htmlentities($_REQUEST['mode']): 'gallery');
     $type = (!empty($_REQUEST['type'])? htmlentities($_REQUEST['type']): 'normal');
+
     /**
      * POST avoid long urls, but we are using GET because
      * SortableTableFromArray pagination is done with simple links, so now we
      * could not send a form in pagination
      */
     $form = '
-        <form id="dokeos_search" action="'. $action .'?mode='. $mode .'" method="get">
+        <form id="dokeos_search" action="'. $action .'" method="GET">
+            <input type="text" id="query" name="query" size="40" />
             <input type="hidden" name="mode" value="'. $mode .'"/>
             <input type="hidden" name="type" value="'. $type .'"/>
-            <input type="text" id="query" name="query" size="40" />
             <input type="hidden" name="tablename_page_nr" value="1" />
             <input type="submit" id="submit" value="'. get_lang("Search") .'" />
             <br /><br />
@@ -152,7 +156,110 @@ function display_search_form($action, $show_thesaurus, $sf_terms, $op) {
             </div>
         </form>
         <br style="clear: both;"/>
-        ';
+             ';
+    return $form;
+}
+
+/**
+ * Build the prefilter form.
+ *
+ * This type allow filter all other multiple select terms by one term in a dinamic way
+ */
+function search_widget_prefilter_form($action, $show_thesaurus, $sf_terms, $op, $prefilter_prefix=NULL) {
+    $thesaurus_icon = Display::return_icon('thesaurus.gif', get_lang('SearchAdvancedOptions'), array('id'=>'thesaurus-icon'));
+    $advanced_options = '<a id="tags-toggle" href="#">'.  get_lang('SearchAdvancedOptions') .'</a>';
+    $display_thesaurus = ($show_thesaurus==true? 'block': 'none');
+    $help = '<h3>'. get_lang('SearchKeywordsHelpTitle') .'</h3>'. get_lang('SearchKeywordsHelpComment');
+    $mode = (!empty($_REQUEST['mode'])? htmlentities($_REQUEST['mode']): 'gallery');
+    $type = (!empty($_REQUEST['type'])? htmlentities($_REQUEST['type']): 'normal');
+
+    /**
+     * POST avoid long urls, but we are using GET because
+     * SortableTableFromArray pagination is done with simple links, so now we
+     * could not send a form in pagination
+     */
+    $form = '
+        <form id="dokeos_search" action="'. $action .'" method="GET">
+            <input type="text" id="query" name="query" size="40" />
+            <input type="hidden" name="mode" value="'. $mode .'"/>
+            <input type="hidden" name="type" value="'. $type .'"/>
+            <input type="hidden" name="tablename_page_nr" value="1" />
+            <input type="submit" id="submit" value="'. get_lang("Search") .'" />
+            <br /><br />
+            <span class="search-links-box">'. $thesaurus_icon . $advanced_options .'&nbsp;</span>
+            <div id="tags" class="tags" style="display:'. $display_thesaurus .';">
+                <div class="search-help-box">'. $help .'</div>
+                <table>
+                <tr>
+            ';
+
+    if (!is_null($prefilter_prefix)) {
+        //sorting the array of terms
+        $temp = array();
+        foreach ($sf_terms[$prefilter_prefix] as $key => $value) {
+            $temp[trim(stripslashes($value['name']))] = $key;
+        }
+        $temp = array_flip($temp);
+        unset($sf_term_array);
+        natcasesort($temp);
+        $sf_term_array = $temp;
+
+        // get specific field name
+        $sf_value = get_specific_field_list(array( 'code' => "'$prefilter_prefix'" ));
+        $sf_value = array_shift($sf_value);
+        $form .= '<label class="sf-select-multiple-title" for="sf_'. $prefix .'[]">'.$icons_for_search_terms[$prefix].' '.$sf_value['name'].'</label><br />';
+
+        $form .= format_one_specific_field_select($prefilter_prefix, $sf_term_array, $op, 'id="prefilter"');
+        $form .= format_specific_fields_selects($sf_terms, $op, $prefilter_prefix);
+    } else {
+        $form .= format_specific_fields_selects($sf_terms, $op);
+    }
+    $or_checked = '';
+    $and_checked = '';
+    if ($op == 'or') {
+        $or_checked = 'checked="checked"';
+    } else if ($op == 'and') {
+        $and_checked = 'checked="checked"';
+    }
+    $form .= '
+                </tr>
+                <tr>
+                    <td id="operator-select">
+                        '. get_lang('SearchCombineSearchWith') .':<br />
+                        <input type="radio" class="search-operator" name="operator" value="or" '. $or_checked .'>'. strtoupper(get_lang('Or')) .'</input>
+                        <input type="radio" class="search-operator" name="operator" value="and" '. $and_checked .'>'. strtoupper(get_lang('And')) .'</input>
+                    </td>
+                    <td></td>
+                    <td>
+                        <br />
+                        <input class="lower-submit" type="submit" value="'. get_lang('Search') .'" />
+                        <input type="submit" id="tags-clean" value="'. get_lang('SearchResetKeywords') .'" />
+                    </td>
+                </tr>
+                </table>
+            </div>
+        </form>
+        <br style="clear: both;"/>
+             ';
+    return $form;
+}
+
+/**
+ * Show search form
+ */
+function display_search_form($action, $show_thesaurus, $sf_terms, $op) {
+    $type = (!empty($_REQUEST['type'])? htmlentities($_REQUEST['type']): 'normal');
+    switch ($type) {
+    case 'prefilter':
+        $prefilter_prefix = 'E'; //TODO: should be api_get_setting('search_prefilter_prefix')
+        $form = search_widget_prefilter_form($action, $show_thesaurus, $sf_terms, $op, $prefilter_prefix);
+        break;
+    case 'normal':
+    default:
+        $form = search_widget_normal_form($action, $show_thesaurus, $sf_terms, $op);
+    }
+
+    // show built form
     echo $form;
 }
 

+ 5 - 1
main/newscorm/lp_list_search.php

@@ -21,7 +21,11 @@ Display::display_header(null,'Path');
 if (api_get_setting('search_enabled') !== 'true') {
     Display::display_error_message(get_lang('SearchFeatureNotEnabledComment'));
 } else {
-    search_widget_show(empty($search_action)?null:'index.php');
+    if (!empty($_GET['action'])) {
+        search_widget_show($_GET['action']);
+    } else {
+        search_widget_show();
+    }
 }
 
 // initialize