--- loncom/interface/lonsearchcat.pm	2003/05/21 14:43:46	1.173
+++ loncom/interface/lonsearchcat.pm	2003/06/19 13:07:49	1.183
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Search Catalog
 #
-# $Id: lonsearchcat.pm,v 1.173 2003/05/21 14:43:46 matthew Exp $
+# $Id: lonsearchcat.pm,v 1.183 2003/06/19 13:07:49 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -207,15 +207,14 @@ sub handler {
     ##     this once, so the pause indicator is deleted
     ##
     if (exists($ENV{'form.pause'})) {
-        sleep(3);
+        sleep(1);
         delete($ENV{'form.pause'});
     }
     ##
     ## Initialize global variables
     ##
     my $domain  = $r->dir_config('lonDefDomain');
-    $diropendb= "/home/httpd/perl/tmp/".&Apache::lonnet::escape($domain).
-            "\_".&Apache::lonnet::escape($ENV{'user.name'})."_searchcat.db";
+    $diropendb= "/home/httpd/perl/tmp/$ENV{'user.domain'}_$ENV{'user.name'}_searchcat.db";
     #
     # set the name of the persistent database
     #          $ENV{'form.persistent_db_id'} can only have digits in it.
@@ -383,7 +382,8 @@ END
             (undef,undef,undef,undef);
         my $pretty_string;
         if ($ENV{'form.phase'} eq 'basic_search') {
-            ($query,$pretty_string) = &parse_basic_search($r,$closebutton);
+            ($query,$pretty_string,$libraries) = 
+                &parse_basic_search($r,$closebutton);
         } else {                      # Advanced search
             ($query,$customquery,$customshow,$libraries,$pretty_string) 
                 = &parse_advanced_search($r,$closebutton);
@@ -555,12 +555,15 @@ then press SEARCH below.
 ENDDOCUMENT
     $scrout.=' '.&simpletextfield('basicexp',$ENV{'form.basicexp'},40).
         ' ';
-    my $checkbox = &simplecheckbox('related',$ENV{'form.related'});
+    my $relatedcheckbox = &simplecheckbox('related',$ENV{'form.related'});
+    my $domain = $r->dir_config('lonDefDomain');
+    my $domaincheckbox = &simplecheckbox('domains',$domain,'checked');
     $scrout.=<<END;
 </td><td><a
 href="/adm/searchcat?phase=disp_adv&catalogmode=$ENV{'form.catalogmode'}&launch=$ENV{'form.launch'}"
 >Advanced Search</a></td></tr>
-<tr><td>$checkbox use related words</td><td></td></tr>
+<tr><td>$relatedcheckbox use related words</td>
+    <td>$domaincheckbox only search domain <b>$domain</b></td></tr>
 </table>
 </p>
 <p>
@@ -580,10 +583,10 @@ per page.
 </p>
 </form>
 ENDDOCUMENT
-    if ($ENV{'request.course.id'}) { $scrout.='<hr />'; }
-}
-if ($ENV{'request.course.id'}) {
-    $scrout.=(<<ENDCOURSESEARCH);
+    }
+    if ($ENV{'request.course.id'}) {
+        $scrout.=(<<ENDCOURSESEARCH);
+<hr />
 <h1>Course Search</h1>    
 <form method="post" action="/adm/searchcat">
 <input type="hidden" name="phase" value="course_search" />
@@ -595,10 +598,13 @@ Enter terms or phrases, then press SEARC
 <table>
 <tr><td>
 ENDCOURSESEARCH
-    $scrout.='&nbsp;'.&simpletextfield('courseexp',$ENV{'form.courseexp'},40);
-    my $crscheckbox = &simplecheckbox('crsfulltext',$ENV{'form.crsfulltext'});
-    my $relcheckbox = &simplecheckbox('crsrelated',$ENV{'form.crsrelated'});
-$scrout.=(<<ENDENDCOURSE);
+        $scrout.='&nbsp;'.
+            &simpletextfield('courseexp',$ENV{'form.courseexp'},40);
+        my $crscheckbox = 
+            &simplecheckbox('crsfulltext',$ENV{'form.crsfulltext'});
+        my $relcheckbox = 
+            &simplecheckbox('crsrelated',$ENV{'form.crsrelated'});
+        $scrout.=(<<ENDENDCOURSE);
 </td></tr>
 <tr><td>$relcheckbox use related words</td><td></td></tr>
 <tr><td>$crscheckbox fulltext search (time consuming)</td><td></td></tr>
@@ -606,7 +612,7 @@ $scrout.=(<<ENDENDCOURSE);
 &nbsp;<input type="submit" name="coursesubmit" value='SEARCH' />
 </p>
 ENDENDCOURSE
-}
+    }
     $scrout.=(<<ENDDOCUMENT);
 </body>
 </html>
@@ -720,7 +726,7 @@ ENDHEADER
     my $size = 4;
     $size = (scalar @domains < ($size - 1) ? scalar @domains + 1 : $size);
     $scrout.="\n".'<font color="#800000" face="helvetica"><b>'.
-        'DOMAINS</b></font><br />'.
+        'DOMAINS TO BE SEARCHED</b></font><br />'.
             '<select name="domains" size="'.$size.'" multiple>'."\n".
                 '<option name="any" value="any" '.
                     ($domain_hash{'any'}? 'selected ' :'').
@@ -846,6 +852,13 @@ sub get_persistent_form_data {
     # Loop through the keys, looking for 'form.'
     foreach my $name (keys(%persistent_db)) {
         next if ($name !~ /^form./);
+        # Kludgification begins!
+        if ($name eq 'form.domains' && 
+            $ENV{'form.searchmode'} eq 'basic' &&
+            $ENV{'form.phase'} ne 'disp_basic') {
+            next;
+        }
+        # End kludge (hopefully)
         next if (exists($ENV{$name}));
         my @values = map { 
             &Apache::lonnet::unescape($_);
@@ -860,6 +873,7 @@ sub get_persistent_form_data {
     untie (%persistent_db);
     return 1;
 }
+
 ######################################################################
 ######################################################################
 
@@ -1018,11 +1032,11 @@ If $value eq 'on' the box is checked.
 ###############################################
 ###############################################
 
-sub simplecheckbox {
-    my ($name,$value)=@_;
-    my $checked='';
-    $checked="checked" if $value eq 'on';
-    return '<input type="checkbox" name="'.$name.'" '. $checked . ' />';
+sub simplecheckbox{
+    my ($name,$value,$checked)=@_;
+    $checked="checked" if ($value eq 'on');
+    return '<input type="checkbox" name="'.$name.'" value="'.$value.'" '.
+        $checked.' />';
 }
 
 ###############################################
@@ -1416,11 +1430,33 @@ sub parse_advanced_search {
     ## ---------------------------------------------------------------
     ## Deal with restrictions to given domains
     ## 
+    my ($libraries_to_query,$pretty_domains_string) = 
+        &parse_domain_restrictions();
+    $pretty_search_string .= $pretty_domains_string."<br />\n";
+    #
+    if (@queries) {
+	$query=join(" AND ",@queries);
+	$query="select * from metadata where $query";
+    } elsif ($customquery) {
+        $query = '';
+    }
+    return ($query,$customquery,$customshow,$libraries_to_query,
+            $pretty_search_string);
+}
+
+sub parse_domain_restrictions {
     my $libraries_to_query = undef;
     # $ENV{'form.domains'} can be either a scalar or an array reference.
     # We need an array.
-    my @allowed_domains = (ref($ENV{'form.domains'}) ? @{$ENV{'form.domains'}} 
-                           :  ($ENV{'form.domains'}) );
+    if (! exists($ENV{'form.domains'})) {
+        return (undef,'');
+    }
+    my @allowed_domains;
+    if (ref($ENV{'form.domains'})) {
+        @allowed_domains =  @{$ENV{'form.domains'}};
+    } else {
+        @allowed_domains = ($ENV{'form.domains'});
+    }
     my %domain_hash = ();
     my $pretty_domains_string;
     foreach (@allowed_domains) {
@@ -1443,16 +1479,7 @@ sub parse_advanced_search {
             }
         }
     }
-    $pretty_search_string .= $pretty_domains_string."<br />\n";
-    #
-    if (@queries) {
-	$query=join(" AND ",@queries);
-	$query="select * from metadata where $query";
-    } elsif ($customquery) {
-        $query = '';
-    }
-    return ($query,$customquery,$customshow,$libraries_to_query,
-            $pretty_search_string);
+    return ($libraries_to_query,$pretty_domains_string);
 }
 
 ######################################################################
@@ -1480,7 +1507,8 @@ sub parse_basic_search {
 	$ENV{"form.$_"}=&Apache::lonnet::unescape($ENV{"form.$_"});
 	$ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
     }
-
+    my ($libraries_to_query,$pretty_domains_string) = 
+        &parse_domain_restrictions();
     # Check to see if enough is filled in
     unless (&filled($ENV{'form.basicexp'})) {
 	&output_blank_field_error($r,$closebutton,'phase=disp_basic');
@@ -1499,13 +1527,19 @@ sub parse_basic_search {
     }
     # Build SQL query string based on form page
     my $query='';
-    my $concatarg=join(',"    ",',
+    my $concatarg=join(',',
 		       ('title', 'author', 'subject', 'notes', 'abstract',
                         'keywords'));
     $concatarg='title' if $ENV{'form.titleonly'};
-    $query=&build_SQL_query('concat('.$concatarg.')',$search_string);
+    $query=&build_SQL_query('concat_ws(" ",'.$concatarg.')',$search_string);
+    if (defined($pretty_domains_string) && $pretty_domains_string ne '') {
+        $pretty_search_string .= ' '.$pretty_domains_string;
+    }
     $pretty_search_string .= "<br />\n";
-    return 'select * from metadata where '.$query,$pretty_search_string;
+    my $final_query = 'SELECT * FROM metadata WHERE '.$query;
+#    &Apache::lonnet::logthis($final_query);
+    return ($final_query,$pretty_search_string,
+            $libraries_to_query);
 }
 
 
@@ -1622,9 +1656,9 @@ sub recursive_SQL_query_build {
         my ($key,$value)=($1,$2);
         my $replacement='';
         if ($key eq 'literal') {
-            $replacement="($dkey like \"\%$value\%\")";
-        } elsif ($key eq 'not') {
-            $value=~s/like/not like/;
+            $replacement="($dkey LIKE \"\%$value\%\")";
+        } elsif (lc($key) eq 'not') {
+            $value=~s/LIKE/NOT LIKE/;
 #          $replacement="($dkey not like $value)";
             $replacement="$value";
         } elsif ($key eq 'and') {
@@ -2131,28 +2165,30 @@ END
     my $time_remaining = $max_time - (time - $starttime) ;
     my $last_time = $time_remaining;
     &update_seconds($r,$time_remaining);
+    &update_status($r,'contacting '.$Servers_to_contact[0]);
     while (($time_remaining > 0) &&
            ((@Servers_to_contact) || keys(%Server_status))) {
         # Send out a search request if it needs to be done.
         if (@Servers_to_contact) {
             # Contact one server
             my $server = shift(@Servers_to_contact);
+            &update_status($r,'contacting '.$server);
             my $reply=&Apache::lonnet::metadata_query($query,$customquery,
                                                       $customshow,[$server]);
             ($server) = keys(%$reply);
             $Server_status{$server} = $reply->{$server};
-            &update_status($r,'contacting '.$server);
         } else {
             # wait a sec. to give time for files to be written
             # This sleep statement is here instead of outside the else 
             # block because we do not want to pause if we have servers
             # left to contact.  
+            if (scalar (keys(%Server_status))) {
+                &update_status($r,
+                               'waiting on '.(join(' ',keys(%Server_status))));
+            }
             sleep(1); 
         }
         #
-        if (scalar (keys(%Server_status))) {
-            &update_status($r,'waiting on '.(join(' ',keys(%Server_status))));
-        }
         #
         # Loop through the servers we have contacted but do not
         # have results from yet, looking for results.
@@ -2369,7 +2405,8 @@ sub display_results {
          ."</center>\n"
          );
     if ($total_results == 0) {
-        $r->print("There are currently no results.\n".
+        $r->print('<meta HTTP-EQUIV="Refresh" CONTENT="1">'.
+                  '<h3>There are currently no results.</h3>'.
                   "</form></body></html>");
         return;
     } else {