--- loncom/interface/lonsearchcat.pm	2003/10/24 21:09:24	1.191
+++ loncom/interface/lonsearchcat.pm	2003/12/22 22:40:37	1.196
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Search Catalog
 #
-# $Id: lonsearchcat.pm,v 1.191 2003/10/24 21:09:24 albertel Exp $
+# $Id: lonsearchcat.pm,v 1.196 2003/12/22 22:40:37 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -59,20 +59,8 @@ search (on a server basis) is displayed
 ###############################################################################
 ###############################################################################
 
-###############################################################################
-##                                                                           ##
-## ORGANIZATION OF THIS PERL MODULE                                          ##
-##                                                                           ##
-## 1. Modules used by this module                                            ##
-## 2. Variables used throughout the module                                   ##
-## 3. handler subroutine called via Apache and mod_perl                      ##
-## 4. Other subroutines                                                      ##
-##                                                                           ##
-###############################################################################
-
 package Apache::lonsearchcat;
 
-# ------------------------------------------------- modules used by this module
 use strict;
 use Apache::Constants qw(:common :http);
 use Apache::lonnet();
@@ -84,101 +72,46 @@ use Apache::loncommon();
 use Apache::lonmysql();
 use Apache::lonlocal;
 
-# ---------------------------------------- variables used throughout the module
-
 ######################################################################
 ######################################################################
-
-=pod 
-
-=item Global variables
-
-=over 4
-
-=item $importbutton
-
-button to take the select results and go to group sorting
-
-=item %groupsearch_db   
-
-Database hash used to save values for the groupsearch RAT interface.
-
-=item $diropendb 
-
-The full path to the (temporary) search database file.  This is set and
-used in &handler() and is also used in &output_results().
-
-=item %Views
-
-Hash which associates an output view description with the function
-that produces it.  Adding a new view type should be as easy as
-adding a line to the definition of this hash and making sure the function
-takes the proper parameters.
-
-=item $bodytag
-
-LON-CAPA standard body tag, gotten from &Apache::lonnet::bodytag.
-No title, no table, just a <body> tag.
-
-=back 
-
-=cut
-
+##
+## Global variables
+##
 ######################################################################
 ######################################################################
-
-# -- dynamically rendered interface components
-my $importbutton; # button to take the selected results and go to group sorting
-
-# -- miscellaneous variables
-my %groupsearch_db;     # database hash
-my $diropendb = "";    # db file
-#             View Description           Function Pointer
+my %groupsearch_db;  # Database hash used to save values for the 
+                     # groupsearch RAT interface.
+my %persistent_db;   # gdbm hash which holds data which is supposed to
+                     # persist across calls to lonsearchcat.pm
+## %Views:
+## Hash which associates an output view description with the function
+## that produces it.
+#             View Description/Name      Function Pointer
 my %Views = ("Detailed Citation View" => \&detailed_citation_view,
              "Summary View"           => \&summary_view,
              "Fielded Format"         => \&fielded_format_view,
              "XML/SGML"               => \&xml_sgml_view,
              "Compact View"           => \&compact_view);
-my %persistent_db;
-my $hidden_fields;
-my $bodytag;
-
-#
-# For course search
-#
-my %alreadyseen;
-my $hashtied;
-my %hash;
-my $totalfound;
- 
-######################################################################
-######################################################################
-
-=pod 
-
-=item &handler() - main handler invoked by httpd child
-
-=item Variables
 
-=over 4
-
-=item $hidden
-
-holds 'hidden' html forms
-
-=item $scrout
-
-string that holds portions of the screen output
-
-=back 
-
-=cut
 
 ######################################################################
 ######################################################################
 sub handler {
     my $r = shift;
+    &set_defaults();
+    #
+    # set form defaults
     #
+    my $hidden_fields;# Hold all the hidden fields used to keep track
+                      # of the search system state
+    my $importbutton; # button to take the selected results and go to group 
+                      # sorting
+    my $diropendb;    # The full path to the (temporary) search database file.
+                      # This is set and used in &handler() and is also used in 
+                      # &output_results().
+    my $bodytag;  # LON-CAPA standard body tag, gotten from 
+                  # &Apache::lonnet::bodytag. 
+                  # No title, no table, just a <body> tag.
 
     my $loaderror=&Apache::lonnet::overloaderror($r);
     if ($loaderror) { return $loaderror; }
@@ -334,11 +267,11 @@ END
     ## Switch on the phase
     ##
     if ($ENV{'form.phase'} eq 'disp_basic') {
-        &print_basic_search_form($r,$closebutton);
+        &print_basic_search_form($r,$closebutton,$hidden_fields);
     } elsif ($ENV{'form.phase'} eq 'disp_adv') {
-        &print_advanced_search_form($r,$closebutton);
+        &print_advanced_search_form($r,$closebutton,$hidden_fields);
     } elsif ($ENV{'form.phase'} eq 'results') {
-        &display_results($r,$importbutton,$closebutton);
+        &display_results($r,$importbutton,$closebutton,$diropendb);
     } elsif ($ENV{'form.phase'} =~ /^(sort|run_search)$/) {
         my ($query,$customquery,$customshow,$libraries,$pretty_string) =
             &get_persistent_data($persistent_db_file,
@@ -388,10 +321,10 @@ END
         my $pretty_string;
         if ($ENV{'form.phase'} eq 'basic_search') {
             ($query,$pretty_string,$libraries) = 
-                &parse_basic_search($r,$closebutton);
+                &parse_basic_search($r,$closebutton,$hidden_fields);
         } else {                      # Advanced search
             ($query,$customquery,$customshow,$libraries,$pretty_string) 
-                = &parse_advanced_search($r,$closebutton);
+                = &parse_advanced_search($r,$closebutton,$hidden_fields);
             return OK if (! defined($query));
         }
         &make_persistent({ query => $query,
@@ -410,6 +343,17 @@ END
 
 ######################################################################
 ######################################################################
+##
+##   Course Search
+##
+######################################################################
+######################################################################
+{   # Scope the course search to avoid global variables
+#
+# Variables For course search
+my %alreadyseen;
+my %hash;
+my $totalfound;
 
 sub course_search {
     my $r=shift;
@@ -432,18 +376,20 @@ sub course_search {
 	      $bodytag.$pretty_search_string);
     $r->rflush();
 # ======================================================= Go through the course
-   $hashtied=0;
-   undef %alreadyseen;
-   %alreadyseen=();
+    undef %alreadyseen;
+    %alreadyseen=();
     my $c=$r->connection;
-   &tiehash();
-   foreach (keys %hash) {
-       if ($c->aborted()) { last; }
-       if (($_=~/^src\_(.+)$/) && (!$alreadyseen{$hash{$_}})) {
-           &checkonthis($r,$hash{$_},0,$hash{'title_'.$1},$fulltext,@allwords);
-       }
-   }
-   &untiehash();
+    if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.".db",
+            &GDBM_READER(),0640)) {
+        foreach (keys %hash) {
+            if ($c->aborted()) { last; }
+            if (($_=~/^src\_(.+)$/) && (!$alreadyseen{$hash{$_}})) {
+                &checkonthis($r,$hash{$_},0,$hash{'title_'.$1},$fulltext,
+                             @allwords);
+            }
+        }
+        untie(%hash);
+    }
     unless ($totalfound) {
 	$r->print('<p>'.&mt('No resources found').'.</p>');
     }
@@ -451,23 +397,6 @@ sub course_search {
     $r->print('</body></html>');
 }
 
-# ---------------------------------------------------------------- tie the hash
-
-sub tiehash {
-    $hashtied=0;
-    if ($ENV{'request.course.fn'}) {
-        if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.".db",
-            &GDBM_READER(),0640)) {
-                $hashtied=1;
-        }
-    }    
-}
-
-sub untiehash {
-    if ($hashtied) { untie %hash; }
-    $hashtied=0;
-}
-
 # =============================== This pulls up a resource and its dependencies
 
 sub checkonthis {
@@ -513,6 +442,14 @@ sub checkonthis {
     }
 }
 
+sub untiehash {
+    if (tied(%hash)) {
+        untie(%hash);
+    }
+}
+
+} # End of course search scoping
+
 ######################################################################
 ######################################################################
 
@@ -528,7 +465,7 @@ Returns a scalar which holds html for th
 ######################################################################
 
 sub print_basic_search_form{
-    my ($r,$closebutton) = @_;
+    my ($r,$closebutton,$hidden_fields) = @_;
     my $bodytag=&Apache::loncommon::bodytag('Search');
     my $scrout=<<"ENDDOCUMENT";
 <html>
@@ -545,14 +482,15 @@ sub print_basic_search_form{
 $bodytag
 ENDDOCUMENT
 if (&Apache::lonnet::allowed('bre',$ENV{'request.role.domain'})) {
+    my $CatalogSearch=&mt('Catalog Search');
+    my $Statement=&mt('Enter terms or phrases separated by AND, OR, or NOT then press SEARCH below');
     $scrout.=(<<ENDDOCUMENT);
-<h1>Catalog Search</h1>
-<form method="post" action="/adm/searchcat">
+<h1>$CatalogSearch</h1>
+<form name="loncapa_search" method="post" action="/adm/searchcat">
 <input type="hidden" name="phase" value="basic_search" />
 $hidden_fields
 <p>
-Enter terms or phrases separated by AND, OR, or NOT 
-then press SEARCH below.
+$Statement.
 </p>
 <p>
 <table>
@@ -560,17 +498,19 @@ then press SEARCH below.
 ENDDOCUMENT
     $scrout.='&nbsp;'.&simpletextfield('basicexp',$ENV{'form.basicexp'},40).
         '&nbsp;';
-    my $relatedcheckbox = &simplecheckbox('related',$ENV{'form.related'});
+    my $relatedcheckbox = &simplecheckbox('related','related',$ENV{'form.related'});
     my $domain = $r->dir_config('lonDefDomain');
     my $domaincheckbox = &simplecheckbox('domains',$domain);
     my $srch=&mt('Search');
     my $header=&mt('Advanced Search');
+    my $userelatedwords=&mt('use related words');
+    my $onlysearchdomain=&mt('only search domain');
     $scrout.=<<END;
 </td><td><a
 href="/adm/searchcat?phase=disp_adv&catalogmode=$ENV{'form.catalogmode'}&launch=$ENV{'form.launch'}&mode=$ENV{'form.mode'}"
 >$header</a></td></tr>
-<tr><td>$relatedcheckbox use related words</td>
-    <td>$domaincheckbox only search domain <b>$domain</b></td></tr>
+<tr><td>$relatedcheckbox $userelatedwords</td>
+    <td>$domaincheckbox $onlysearchdomain <b>$domain</b></td></tr>
 </table>
 </p>
 <p>
@@ -599,7 +539,7 @@ ENDDOCUMENT
         $scrout.=(<<ENDCOURSESEARCH);
 <hr />
 <h1>$lt{'header'}</h1>    
-<form method="post" action="/adm/searchcat">
+<form name="course_search" method="post" action="/adm/searchcat">
 <input type="hidden" name="phase" value="course_search" />
 $hidden_fields
 <p>
@@ -646,7 +586,7 @@ Returns a scalar which holds html for th
 ######################################################################
 
 sub print_advanced_search_form{
-    my ($r,$closebutton) = @_;
+    my ($r,$closebutton,$hidden_fields) = @_;
     my $advanced_buttons = <<"END";
 <p>
 <input type="submit" name="advancedsubmit" value='SEARCH' />
@@ -1033,7 +973,7 @@ sub simpletextfield {
 
 =item &simplecheckbox()
 
-Inputs: $name,$value
+Inputs: $name,$value,$checked
 
 Returns a simple check box with the given $name.
 If $value eq 'on' the box is checked.
@@ -1045,7 +985,7 @@ If $value eq 'on' the box is checked.
 
 sub simplecheckbox{
     my ($name,$value,$checked)=@_;
-    $checked="checked" if ($value eq 'on');
+    $checked="checked" if ($value eq $checked || $value eq 'on');
     return '<input type="checkbox" name="'.$name.'" value="'.$value.'" '.
         $checked.' />';
 }
@@ -1298,7 +1238,7 @@ Parse advanced search form and return th
 ######################################################################
 ######################################################################
 sub parse_advanced_search {
-    my ($r,$closebutton)=@_;
+    my ($r,$closebutton,$hidden_fields)=@_;
     my $fillflag=0;
     my $pretty_search_string = "<br />\n";
     # Clean up fields for safety
@@ -1332,7 +1272,7 @@ sub parse_advanced_search {
 	}
     }
     unless ($fillflag) {
-	&output_blank_field_error($r,$closebutton,'phase=disp_adv');
+	&output_blank_field_error($r,$closebutton,'phase=disp_adv',$hidden_fields);
 	return ;
     }
     # Turn the form input into a SQL-based query
@@ -1415,7 +1355,7 @@ sub parse_advanced_search {
 			);
     # Test to see if date windows are legitimate
     if ($datequery=~/^Incorrect/) {
-	&output_date_error($r,$datequery,$closebutton);
+	&output_date_error($r,$datequery,$closebutton,$hidden_fields);
 	return ;
     } elsif ($datequery) {
         # Here is where you would set up pretty_search_string to output
@@ -1850,6 +1790,7 @@ sub ensure_db_and_table {
 ######################################################################
 sub print_sort_form {
     my ($r,$pretty_query_string) = @_;
+    my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
     ##
     my %SortableFields=&Apache::lonlocal::texthash( 
          id        => 'Default',
@@ -2095,6 +2036,7 @@ sub revise_button {
 ######################################################################
 sub run_search {
     my ($r,$query,$customquery,$customshow,$serverlist,$pretty_string) = @_;
+    my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
     my $connection = $r->connection;
     #
     # Timing variables
@@ -2345,7 +2287,7 @@ sub prev_next_buttons {
 ######################################################################
 ######################################################################
 sub display_results {
-    my ($r,$importbutton,$closebutton) = @_;
+    my ($r,$importbutton,$closebutton,$diropendb) = @_;
     my $connection = $r->connection;
     $r->print(&search_results_header($importbutton,$closebutton));
     ##
@@ -2710,6 +2652,7 @@ the name of the input field to put the t
 ######################################################################
 sub search_results_header {
     my ($importbutton,$closebutton) = @_;
+    my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
     my $result = '';
     # output beginning of search page
     # conditional output of script functions dependent on the mode in
@@ -2813,6 +2756,7 @@ END
 ######################################################################
 ######################################################################
 sub search_status_header {
+    my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
     return <<ENDSTATUS;
 <html><head><title>Search Status</title></head>
 $bodytag
@@ -2880,8 +2824,9 @@ extra custom metadata to show.
 ######################################################################
 sub detailed_citation_view {
     my ($prefix,%values) = @_;
+    my $icon=&Apache::loncommon::icon($values{'url'});
     my $result=<<END;
-<b>$prefix<a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
+<b>$prefix<img src="$icon" /><a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
     target='search_preview'>$values{'title'}</a></b>
 <p>
 <b>$values{'author'}</b>, <i>$values{'owner'}</i><br />
@@ -2914,8 +2859,9 @@ END
 ######################################################################
 sub summary_view {
     my ($prefix,%values) = @_;
+    my $icon=&Apache::loncommon::icon($values{'url'});
     my $result=<<END;
-$prefix<a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
+$prefix<img src="$icon" /><a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
    target='search_preview'>$values{'author'}</a><br />
 $values{'title'}<br />
 $values{'owner'} -- $values{'lastrevisiondate'}<br />
@@ -2940,8 +2886,9 @@ END
 ######################################################################
 sub compact_view {
     my ($prefix,%values) = @_;
+    my $icon=&Apache::loncommon::icon($values{'url'});
     my $result=<<END;
-$prefix <a href="http://$ENV{'HTTP_HOST'}$values{'url'}"  target='search_preview'>
+$prefix <img src="$icon" /> <a href="http://$ENV{'HTTP_HOST'}$values{'url'}"  target='search_preview'>
 $values{'title'}</a>
 <b>$values{'author'}</b><br />
 END
@@ -2962,8 +2909,9 @@ END
 ######################################################################
 sub fielded_format_view {
     my ($prefix,%values) = @_;
+    my $icon=&Apache::loncommon::icon($values{'url'});
     my $result=<<END;
-$prefix
+$prefix <img src="$icon" />
 <b>URL: </b> <a href="http://$ENV{'HTTP_HOST'}$values{'url'}" 
               target='search_preview'>$values{'url'}</a>
 <br />
@@ -3079,7 +3027,8 @@ $parms is extra information to include i
 ######################################################################
 ######################################################################
 sub output_blank_field_error {
-    my ($r,$closebutton,$parms)=@_;
+    my ($r,$closebutton,$parms,$hidden_fields)=@_;
+    my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
     # make query information persistent to allow for subsequent revision
     $r->print(<<BEGINNING);
 <html>
@@ -3128,8 +3077,9 @@ Inputs:
 ######################################################################
 ######################################################################
 sub output_date_error {
-    my ($r,$message,$closebutton)=@_;
+    my ($r,$message,$closebutton,$hidden_fields)=@_;
     # make query information persistent to allow for subsequent revision
+    my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
     $r->print(<<RESULTS);
 <html>
 <head>
@@ -3183,7 +3133,6 @@ sub start_fresh_session {
 
 sub cleanup {
     if (tied(%groupsearch_db)) {
-	&Apache::lonnet::logthis('Cleanup searchcat: groupsearch_db');
         unless (untie(%groupsearch_db)) {
 	  &Apache::lonnet::logthis('Failed cleanup searchcat: groupsearch_db');
         }