[LON-CAPA-cvs] cvs: loncom /interface lonsearchcat.pm

matthew lon-capa-cvs@mail.lon-capa.org
Fri, 12 Jul 2002 21:02:27 -0000


This is a MIME encoded message

--matthew1026507747
Content-Type: text/plain

matthew		Fri Jul 12 17:02:27 2002 EDT

  Modified files:              
    /loncom/interface	lonsearchcat.pm 
  Log:
  Renamed "%hash" "%groupsearch_db" in an effort to stamp out useless names.
  Added &related_version, which does the thesaurus lookup calls.
  Added &searchphrasefield_with_related, which is close to &searchphrasefield.
  &handler:
      Output html header *before* starting to send requests, thus letting the
      user know what is slowing us down.
  &parse_basic_search and &parse_advanced_search now call &related_version.
  &advanced_search_form
      Main table of entry values now has three columns instead of two.
      Call &searchphrasefield_with_related where appropriate.
  Changes to the documentation for the HTML form helper functions.
  
  
--matthew1026507747
Content-Type: text/plain
Content-Disposition: attachment; filename="matthew-20020712170227.txt"

Index: loncom/interface/lonsearchcat.pm
diff -u loncom/interface/lonsearchcat.pm:1.141 loncom/interface/lonsearchcat.pm:1.142
--- loncom/interface/lonsearchcat.pm:1.141	Fri Jul 12 10:36:16 2002
+++ loncom/interface/lonsearchcat.pm	Fri Jul 12 17:02:27 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Search Catalog
 #
-# $Id: lonsearchcat.pm,v 1.141 2002/07/12 14:36:16 matthew Exp $
+# $Id: lonsearchcat.pm,v 1.142 2002/07/12 21:02:27 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -109,9 +109,9 @@
 
 button to take the select results and go to group sorting
 
-=item %hash   
+=item %groupsearch_db   
 
-The ubiquitous database hash
+Database hash used to save values for the groupsearch RAT interface.
 
 =item $diropendb 
 
@@ -137,7 +137,7 @@
 my $importbutton; # button to take the selected results and go to group sorting
 
 # -- miscellaneous variables
-my %hash;     # database hash
+my %groupsearch_db;     # database hash
 my $diropendb = "";    # db file
 
 #             View Description           Function Pointer
@@ -173,7 +173,7 @@
 ######################################################################
 sub handler {
     my $r = shift;
-    untie %hash;
+    untie %groupsearch_db;
 
     $r->content_type('text/html');
     $r->send_http_header;
@@ -190,9 +190,9 @@
     ## Clear out old values from database
     ##
     if ($ENV{'form.launch'} eq '1') {
-	if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
+	if (tie(%groupsearch_db,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
 	    &start_fresh_session();
-	    untie %hash;
+	    untie %groupsearch_db;
 	} else {
 	    $r->print('<html><head></head><body>Unable to tie hash to db '.
 		      'file</body></html>');
@@ -246,12 +246,16 @@
                 = &parse_advanced_search($r);
             return OK if (! defined($query));
         }
+        # Output some information to the user.
+        $r->print(&search_results_header($searchtype));
+        $r->print("Sending search request to LON-CAPA servers.<br />\n");
+        $r->rflush();
         # Send query statements over the network to be processed by 
         # either the SQL database or a recursive scheme of 'grep'-like 
         # actions (for custom metadata).
-        $r->rflush();
         my $reply=&Apache::lonnet::metadata_query($query,$customquery,
                                                $customshow,$libraries);
+        $r->rflush();
         &output_results($searchtype,$r,$reply,$hidden);
     } else {
         #
@@ -397,19 +401,24 @@
 			$ENV{'form.viewselect'},
 			undef,undef,undef,
 			sort(keys(%Views)));
-    $scrout.="</td></tr>\n";
-    $scrout.=&searchphrasefield('title',   'title'   ,$ENV{'form.title'});
+    $scrout.="</td><td>Related<br />Words</td></tr>\n";
+    $scrout.=&searchphrasefield_with_related('title',   'title'   ,
+                                             $ENV{'form.title'});
     $scrout.=&searchphrasefield('author',  'author'  ,$ENV{'form.author'});
-    $scrout.=&searchphrasefield('subject', 'subject' ,$ENV{'form.subject'});
-    $scrout.=&searchphrasefield('keywords','keywords',$ENV{'form.keywords'});
+    $scrout.=&searchphrasefield_with_related('subject', 'subject' ,
+                                             $ENV{'form.subject'});
+    $scrout.=&searchphrasefield_with_related('keywords','keywords',
+                                             $ENV{'form.keywords'});
     $scrout.=&searchphrasefield('URL',     'url'     ,$ENV{'form.url'});
-    $scrout.=&searchphrasefield('notes',   'notes'   ,$ENV{'form.notes'});
-    $scrout.=&searchphrasefield('abstract','abstract',$ENV{'form.abstract'});
+    $scrout.=&searchphrasefield_with_related('notes',   'notes'   ,
+                                             $ENV{'form.notes'});
+    $scrout.=&searchphrasefield_with_related('abstract','abstract',
+                                             $ENV{'form.abstract'});
     # Hack - an empty table row.
-    $scrout.="<tr><td>&nbsp;</td><td>&nbsp;</td></tr>\n";
+    $scrout.="<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>\n";
     $scrout.=&searchphrasefield('file<br />extension','mime',
                         $ENV{'form.mime'});
-    $scrout.="<tr><td>&nbsp;</td><td>&nbsp;</td></tr>\n";
+    $scrout.="<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>\n";
     $scrout.=&searchphrasefield('publisher<br />owner','owner',
 				$ENV{'form.owner'});
     $scrout.="</table>\n";
@@ -568,8 +577,8 @@
     return $persistent;
 }
 
-
 ######################################################################
+#                HTML form building functions                        #  
 ######################################################################
 
 =pod 
@@ -578,6 +587,13 @@
 
 =over 4
 
+=cut
+
+###############################################
+###############################################
+
+=pod
+
 =item &simpletextfield() 
 
 Inputs: $name,$value,$size
@@ -585,6 +601,23 @@
 Returns a text input field with the given name, value, and size.  
 If size is not specified, a value of 20 is used.
 
+=cut
+
+###############################################
+###############################################
+
+sub simpletextfield {
+    my ($name,$value,$size)=@_;
+    $size = 20 if (! defined($size));
+    return '<input type="text" name="'.$name.
+        '" size="'.$size.'" value="'.$value.'" />';
+}
+
+###############################################
+###############################################
+
+=pod
+
 =item &simplecheckbox()
 
 Inputs: $name,$value
@@ -592,92 +625,106 @@
 Returns a simple check box with the given $name.
 If $value eq 'on' the box is checked.
 
-=item &searchphrasefield()
+=cut
 
-Inputs: $title,$name,$value
+###############################################
+###############################################
 
-Returns html for a title line and an input field for entering search terms.
-the instructions "Enter terms or phrases separated by search operators such 
-as AND, OR, or NOT." are given following the title.  The entry field (which
-is where the $name and $value are used) is an 80 column simpletextfield.
+sub simplecheckbox {
+    my ($name,$value)=@_;
+    my $checked='';
+    $checked="checked" if $value eq 'on';
+    return '<input type="checkbox" name="'.$name.'" '. $checked . ' />';
+}
 
-=item &dateboxes()
+###############################################
+###############################################
 
-Returns html selection form elements for the specification of 
-the day, month, and year.
+=pod
 
-=item &selectbox()
+=item &fieldtitle()
 
-Returns a scalar containing an html <select> form.  
+Input: $title
 
-Inputs: 
+Returns a scalar with html which will display $title as a search
+field heading.
 
-=over 4
+=cut
 
-=item $title 
+###############################################
+###############################################
 
-Printed above the select box, in uppercase.  If undefined, only a select
-box will be returned, with no additional html.
+sub fieldtitle {
+    my $title = uc(shift());
+    return '<font color="#800000" face="helvetica"><b>'.$title.
+        ':&nbsp;</b></font>';
+}
 
-=item $name 
+###############################################
+###############################################
 
-The name element of the <select> tag.
+=pod
 
-=item $default 
+=item &searchphrasefield()
 
-The default value of the form.  Can be $anyvalue, or in @idlist.
+Inputs: $title,$name,$value
 
-=item $anyvalue 
+Returns html for a title line and an input field for entering search terms.
+The entry field (which is where the $name and $value are used) is a 50 column 
+simpletextfield.  The html returned is for a row in a three column table.
 
-The <option value="..."> used to indicate a default of 
-none of the values.  Can be undef.
+=cut
 
-=item $anytag 
+###############################################
+###############################################
+    
+sub searchphrasefield {
+    my ($title,$name,$value)=@_;
+    return '<tr><td>'.&fieldtitle($title).'</td><td>'.
+        &simpletextfield($name,$value,50)."</td><td>&nbsp;</td></tr>\n";
+}
 
-The text associate with $anyvalue above.
+###############################################
+###############################################
 
-=item $functionref 
+=pod
 
-Each element in @idlist will be passed as a parameter 
-to the function referenced here.  The return value of the function should
-be a scalar description of the items.  If this value is undefined the 
-description of each item in @idlist will be the item name.
+=item &searchphrasefield_with_related()
 
-=item @idlist 
+Inputs: $title,$name,$value
 
-The items to be selected from.  One of these or $anyvalue will be the 
-value returned by the form element, $ENV{form.$name}.
+Returns html for a title line and an input field for entering search terms
+and a check box for 'related words'.  The entry field (which is where the 
+$name and $value are used) is a 50 column simpletextfield.  The name of
+the related words checkbox is "$name_related".
 
-=back
+=cut
 
-=back 
+###############################################
+###############################################
+    
+sub searchphrasefield_with_related {
+    my ($title,$name,$value)=@_;
+    return '<tr><td>'.&fieldtitle($title).'</td><td>'.
+        &simpletextfield($name,$value,50).'</td><td align="center">&nbsp;'.
+            &simplecheckbox($name.'_related',$ENV{'form.'.$name.'_related'}).
+                "&nbsp;</td></tr>\n";
+}
 
-=cut
+###############################################
+###############################################
 
-######################################################################
-######################################################################
+=pod
 
-sub simpletextfield {
-    my ($name,$value,$size)=@_;
-    $size = 20 if (! defined($size));
-    return '<input type="text" name="'.$name.
-        '" size="'.$size.'" value="'.$value.'" />';
-}
+=item &dateboxes()
 
-sub simplecheckbox {
-    my ($name,$value)=@_;
-    my $checked='';
-    $checked="checked" if $value eq 'on';
-    return '<input type="checkbox" name="'.$name.'" '. $checked . ' />';
-}
+Returns html selection form elements for the specification of 
+the day, month, and year.
 
-sub searchphrasefield {
-    my ($title,$name,$value)=@_;
-    my $uctitle=uc($title);
-    return '<tr><td><font color="#800000" face="helvetica">'.
-        '<b>'.$uctitle.':&nbsp;</b></font></td><td>'.
-                &simpletextfield($name,$value,50)."</td></tr>\n";
-}
+=cut
+
+###############################################
+###############################################
 
 sub dateboxes {
     my ($name,$defaultmonth,$defaultday,$defaultyear,
@@ -723,6 +770,59 @@
     return "$month$day$year";
 }
 
+###############################################
+###############################################
+
+=pod
+
+=item &selectbox()
+
+Returns a scalar containing an html <select> form.  
+
+Inputs: 
+
+=over 4
+
+=item $title 
+
+Printed above the select box, in uppercase.  If undefined, only a select
+box will be returned, with no additional html.
+
+=item $name 
+
+The name element of the <select> tag.
+
+=item $default 
+
+The default value of the form.  Can be $anyvalue, or in @idlist.
+
+=item $anyvalue 
+
+The <option value="..."> used to indicate a default of 
+none of the values.  Can be undef.
+
+=item $anytag 
+
+The text associate with $anyvalue above.
+
+=item $functionref 
+
+Each element in @idlist will be passed as a parameter 
+to the function referenced here.  The return value of the function should
+be a scalar description of the items.  If this value is undefined the 
+description of each item in @idlist will be the item name.
+
+=item @idlist 
+
+The items to be selected from.  One of these or $anyvalue will be the 
+value returned by the form element, $ENV{form.$name}.
+
+=back
+
+=cut
+
+###############################################
+
 sub selectbox {
     my ($title,$name,$default,$anyvalue,$anytag,$functionref,@idlist)=@_;
     if (! defined($functionref)) { $functionref = sub { $_[0]}; }
@@ -748,6 +848,17 @@
 }
 
 ######################################################################
+#                End of HTML form building functions                 #  
+######################################################################
+
+=pod
+
+=back 
+
+=cut
+
+
+######################################################################
 ######################################################################
 
 =pod 
@@ -818,7 +929,13 @@
     foreach my $field ('title','author','subject','notes','abstract','url',
 		       'keywords','version','owner','mime') {
 	if ($ENV{'form.'.$field}) {
-	    push @queries,&build_SQL_query($field,$ENV{'form.'.$field});
+            my $searchphrase = $ENV{'form.'.$field};
+            if ($ENV{'form.'.$field.'_related'}) {
+                $searchphrase = &related_version($searchphrase);
+                $ENV{'form.'.$field} = $searchphrase;
+                $ENV{'form.'.$field.'_related'} = undef;
+            }
+	    push @queries,&build_SQL_query($field,$searchphrase);
         }
     }
     # I dislike the hack below.
@@ -930,17 +1047,9 @@
 	&output_blank_field_error($r);
 	return OK;
     }
+    my $search_string = $ENV{'form.basicexp'};
     if ($ENV{'form.related'}) {
-        my $tmp = $ENV{'form.basicexp'};
-        while ($ENV{'form.basicexp'} =~ /(\w+)/cg) {
-            my $word = $1;
-            my @Words = &Apache::loncommon::get_related_words($word);
-            my $replacement = join " OR ", ($word,
-                                            ($#Words>4? @Words[0..4] : @Words)
-                                            );
-            $tmp =~ s/\b$word\b/ $replacement /g;
-        }
-        $ENV{'form.basicexp'} = $tmp;
+        $search_string = &related_version($ENV{'form.basicexp'});
     }
     # Build SQL query string based on form page
     my $query='';
@@ -948,8 +1057,7 @@
 		       ('title', 'author', 'subject', 'notes', 'abstract',
                         'keywords'));
     $concatarg='title' if $ENV{'form.titleonly'};
-
-    $query=&build_SQL_query('concat('.$concatarg.')',$ENV{'form.'.'basicexp'});
+    $query=&build_SQL_query('concat('.$concatarg.')',$search_string);
     return 'select * from metadata where '.$query;
 }
 
@@ -959,6 +1067,38 @@
 
 =pod 
 
+=item &related_version
+
+Modifies an input string to include related words.  Words in the string
+are replaced with parenthesized lists of 'OR'd words.  For example
+"torque" is replaced with "(torque OR word1 OR word2 OR ...)".  
+
+Note: Using this twice on a string is probably silly.
+
+=cut
+
+######################################################################
+######################################################################
+sub related_version {
+    my $search_string = shift;
+    my $result = $search_string;
+    while ($search_string =~ /(\w+)/cg) {
+        my $word = $1;
+        next if (lc($word) =~ /\b(or|and|not)\b/);
+        my @Words = &Apache::loncommon::get_related_words($word);
+        my $replacement = join " OR ", ($word,
+                                        ($#Words>4? @Words[0..4] : @Words)
+                                        );
+        $result =~ s/(\b)$word(\b)/$1($replacement)$2/g;
+    }
+    return $result;
+}
+
+######################################################################
+######################################################################
+
+=pod 
+
 =item &build_SQL_query() 
 
 Builds a SQL query string from a logical expression with AND/OR keywords
@@ -1161,7 +1301,6 @@
     my $persistent=&make_persistent(\%ENV);
     #
     # Begin producing output
-    $r->print(&search_results_header($mode));
     $r->rflush();
     #
     # begin showing the cataloged results
@@ -1264,9 +1403,9 @@
                                     &handle_custom_fields(\@results);
         my @customfields = @$customfields;
         my %customhash   = %$customhash;
-	untie %hash if (keys %hash);
+	untie %groupsearch_db if (tied %groupsearch_db);
         #
-	if (! tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
+	if (! tie(%groupsearch_db,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) {
 	    $r->print('<html><head></head><body>Unable to tie hash to db '.
                       'file</body></html>');
         } else {
@@ -1314,8 +1453,8 @@
                 }
                 if ($ENV{'form.catalogmode'} eq 'groupsearch') {
 		    $fnum+=0;
-		    $hash{"pre_${fnum}_link"}=$Fields{'url'};
-		    $hash{"pre_${fnum}_title"}=$Fields{'title'};
+		    $groupsearch_db{"pre_${fnum}_link"}=$Fields{'url'};
+		    $groupsearch_db{"pre_${fnum}_title"}=$Fields{'title'};
 		    $compiledresult.=<<END;
 <font size='-1'>
 <input type="checkbox" name="returnvalues" value="SELECT"
@@ -1333,7 +1472,7 @@
                     $compiledresult.="<hr align='left' width='200' noshade />";
                 }
             }
-            untie %hash;
+            untie %groupsearch_db;
         }
 	if ($compiledresult) {
 	    $resultflag=1;
@@ -1982,7 +2121,7 @@
 
 =item &start_fresh_session()
 
-Cleans the global %hash by removing all fields which begin with
+Cleans the global %groupsearch_db by removing all fields which begin with
 'pre_' or 'store'.
 
 =cut
@@ -1990,13 +2129,13 @@
 ######################################################################
 ######################################################################
 sub start_fresh_session {
-    delete $hash{'mode_catalog'};
-    foreach (keys %hash) {
+    delete $groupsearch_db{'mode_catalog'};
+    foreach (keys %groupsearch_db) {
         if ($_ =~ /^pre_/) {
-            delete $hash{$_};
+            delete $groupsearch_db{$_};
         }
         if ($_ =~ /^store/) {
-	    delete $hash{$_};
+	    delete $groupsearch_db{$_};
 	}
     }
 }

--matthew1026507747--