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

matthew lon-capa-cvs@mail.lon-capa.org
Sun, 04 Aug 2002 18:28:01 -0000


This is a MIME encoded message

--matthew1028485681
Content-Type: text/plain

matthew		Sun Aug  4 14:28:01 2002 EDT

  Modified files:              
    /loncom/interface	lonsearchcat.pm 
  Log:
  Added 'show', the number of results to show per page, as a form parameter - 
  Added 'show', the number of results to display per page, as a form parameter
     defaults to 20 but can be selected to be 10,20,50, or 100.
  Added a 'sort' phase, which calls the new &print_sort_form function.
     The sort phase is automatically called after a search is run.
  Added a 'revise search' button to run_search and the sort phase.
  Added parameter to &output_blank_field_error so it can take you to the right
     search phase (disp_basic or disp_adv) after it chides you.
  &ensure_db_and_table is a new function made up of code taken from run_search
     that was also needed for the sort form.  This function connects lonmysql
     to MySQL and makes sure our table is there.
  &print_sort_form outputs a sorting form.  Or rather, it WILL do that someday.
     Currently just tells the user about their search and prints a 
     'revise search' button.
  Changed $Datatypes{'id'}->{'type'} and {'restrictions'} because I am learning
      more about MySQL every day.
  
  
--matthew1028485681
Content-Type: text/plain
Content-Disposition: attachment; filename="matthew-20020804142801.txt"

Index: loncom/interface/lonsearchcat.pm
diff -u loncom/interface/lonsearchcat.pm:1.150 loncom/interface/lonsearchcat.pm:1.151
--- loncom/interface/lonsearchcat.pm:1.150	Thu Aug  1 10:11:57 2002
+++ loncom/interface/lonsearchcat.pm	Sun Aug  4 14:28:01 2002
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Search Catalog
 #
-# $Id: lonsearchcat.pm,v 1.150 2002/08/01 14:11:57 matthew Exp $
+# $Id: lonsearchcat.pm,v 1.151 2002/08/04 18:28:01 matthew Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -298,6 +298,7 @@
         }
     }
     $ENV{'form.phase'} = 'disp_basic' if (! exists($ENV{'form.phase'}));
+    $ENV{'form.show'} = 20 if (! exists($ENV{'form.show'}));
     ##
     ## Switch on the phase
     ##
@@ -307,15 +308,23 @@
         &print_advanced_search_form($r,$closebutton);
     } elsif ($ENV{'form.phase'} eq 'results') {
         &display_results($r,$importbutton,$closebutton);
-    } elsif($ENV{'form.phase'} eq 'run_search') {
+    } elsif ($ENV{'form.phase'} =~ /^(sort|run_search)$/) {
         my ($query,$customquery,$customshow,$libraries,$pretty_string) =
             &get_persistent_data($persistent_db_file,
                  ['query','customquery','customshow',
                   'libraries','pretty_string']);
-        &run_search($r,$query,$customquery,$customshow,
-                    $libraries,$pretty_string);
+        if ($ENV{'form.phase'} eq 'sort') {
+            &print_sort_form($r,$pretty_string);
+        } elsif ($ENV{'form.phase'} eq 'run_search') {
+            &run_search($r,$query,$customquery,$customshow,
+                        $libraries,$pretty_string);
+        }
     } elsif(($ENV{'form.phase'} eq 'basic_search') ||
             ($ENV{'form.phase'} eq 'adv_search')) {
+        $ENV{'form.searchmode'} = 'basic';
+        if ($ENV{'form.phase'} eq 'adv_search') {
+            $ENV{'form.searchmode'} = 'advanced';
+        }
         # Set up table
         if (! defined(&create_results_table())) {
             $r->print(<<END);
@@ -425,8 +434,12 @@
 			$ENV{'form.viewselect'},
 			undef,undef,undef,
 			sort(keys(%Views)));
+    $scrout.=&selectbox(undef,'show',
+			$ENV{'form.show'},
+			undef,undef,undef,
+			(10,20,50,100));
     $scrout.=<<ENDDOCUMENT;
-<input type="button" value="HELP" onClick="openhelp()" />
+per page.
 </p>
 </form>
 </body>
@@ -492,6 +505,13 @@
 			$ENV{'form.viewselect'},
 			undef,undef,undef,
 			sort(keys(%Views)));
+    $scrout.='&nbsp;';
+    $scrout.=&selectbox(undef,'show',
+			$ENV{'form.show'},
+			undef,undef,undef,
+			(10,20,50,100));
+    $scrout.='&nbsp;'.
+        '<font color="#800000" face="helvetica">Per Page</font>';
     $scrout.="</td><td>Related<br />Words</td></tr>\n";
     $scrout.=&searchphrasefield_with_related('title',   'title'   ,
                                              $ENV{'form.title'});
@@ -1123,6 +1143,7 @@
 	$ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g;
     }
     # Preprocess the category form element.
+    $ENV{'form.category'} = 'any' if (ref($ENV{'form.category'}));
     if ($ENV{'form.category'} ne 'any') {
         my @extensions = &Apache::loncommon::filecategorytypes
             ($ENV{'form.category'});
@@ -1137,7 +1158,7 @@
 	}
     }
     unless ($fillflag) {
-	&output_blank_field_error($r,$closebutton);
+	&output_blank_field_error($r,$closebutton,'phase=disp_adv');
 	return ;
     }
     # Turn the form input into a SQL-based query
@@ -1294,7 +1315,7 @@
 
     # Check to see if enough is filled in
     unless (&filled($ENV{'form.basicexp'})) {
-	&output_blank_field_error($r,$closebutton);
+	&output_blank_field_error($r,$closebutton,'phase=disp_basic');
 	return OK;
     }
     my $pretty_search_string = '<b>'.$ENV{'form.basicexp'}.'</b>';
@@ -1530,7 +1551,6 @@
 
 ######################################################################
 ######################################################################
-
 sub copyright_check {
     my $Metadata = shift;
     # Check copyright tags and skip results the user cannot use
@@ -1550,6 +1570,151 @@
     return 1;
 }
 
+
+######################################################################
+######################################################################
+
+=pod
+
+=item &ensure_db_and_table
+
+Ensure we can get lonmysql to connect to the database and the table we
+need exists.
+
+Inputs: $r, table id
+
+Returns: undef on error, 1 if the table exists.
+
+=cut
+
+######################################################################
+######################################################################
+sub ensure_db_and_table {
+    my ($r,$table) = @_;
+    ##
+    ## Sanity check the table id.
+    ##
+    if (! defined($table) || $table eq '' || $table =~ /\D/ ) {
+        $r->print("Unable to retrieve search results.  ".
+                  "Unable to determine the table results were stored in.  ".
+                  "</body></html>");
+        return undef;
+    }
+    ##
+    ## Make sure we can connect and the table exists.
+    ##
+    my $connection_result = &Apache::lonmysql::connect_to_db();
+    if (!defined($connection_result)) {
+        $r->print("Unable to connect to the MySQL database where your results".
+                  " are stored. </body></html>");
+        &Apache::lonnet::logthis("lonsearchcat: unable to get lonmysql to".
+                                 " connect to database.");
+        &Apache::lonnet::logthis(&Apache::lonmysql::get_error());
+        return undef;
+    }
+    my $table_check = &Apache::lonmysql::check_table($table);
+    if (! defined($table_check)) {
+        $r->print("A MySQL error has occurred.</form></body></html>");
+        &Apache::lonnet::logthis("lonmysql was unable to determine the status".
+                                 " of table ".$table);
+        return undef;
+    } elsif (! $table_check) {
+        $r->print("The table of results could not be found.");
+        &Apache::lonnet::logthis("The user requested a table, ".$table.
+                                 ", that could not be found.");
+        return undef;
+    }
+    return 1;
+}
+
+######################################################################
+######################################################################
+
+=pod
+
+=item &print_sort_form
+
+=cut
+
+######################################################################
+######################################################################
+sub print_sort_form {
+    my ($r,$pretty_query_string) = @_;
+    ##
+    my %SortableFields = 
+        (id        => 'Default',
+         title     => 'Title',
+         author    => 'Author',
+         subject   => 'Subject',
+         url       => 'URL',
+         version   => 'Version Number',
+         mime      => 'Mime type',
+         lang      => 'Language',
+         owner     => 'Owner/Publisher',
+         copyright => 'Copyright',
+         hostname  => 'Host',
+         creationdate     => 'Creation Date',
+         lastrevisiondate => 'Revision Date',
+     );
+    ##
+    my $table = $ENV{'form.table'};
+    return if (! &ensure_db_and_table($r,$table));
+    ##
+    ## Get the number of results 
+    ##
+    my $total_results = &Apache::lonmysql::number_of_rows($table);
+    if (! defined($total_results)) {
+        $r->print("A MySQL error has occurred.</form></body></html>");
+        &Apache::lonnet::logthis("lonmysql was unable to determine the number".
+                                 " of rows in table ".$table);
+        &Apache::lonnet::logthis(&Apache::lonmysql::get_error());
+        return;
+    }
+    my $result;
+    $result.=<<END;
+<html>
+<head>
+<script>
+    function change_sort() {
+        var newloc = "/adm/searchcat?phase=results";
+        newloc += "&persistent_db_id=$ENV{'form.persistent_db_id'}";
+        newloc += "&sortby=";
+        newloc += document.forms.statusform.elements.sortby.value;
+        parent.resultsframe.location= newloc;
+    }
+</script>
+<title>Results</title>
+</head>
+<body>
+<form name="statusform" action="" method="post">
+END
+
+#<h2>Sort Results</h2>
+#Sort by: <select size="1" name="sortby" onchange="javascript:change_sort();">
+#    $ENV{'form.sortby'} = 'id' if (! defined($ENV{'form.sortby'}));
+#    foreach (keys(%SortableFields)) {
+#        $result.="<option name=\"$_\"";
+#        if ($_ eq $ENV{'form.sortby'}) {
+#            $result.=" selected ";
+#        }
+#        $result.=" >$SortableFields{$_}</option>\n";
+#    }
+#    $result.="</select>\n";
+    my $revise = &revise_button();
+    $result.=<<END;
+<p>
+There are $total_results matches to your query. $revise
+</p><p>
+Search:$pretty_query_string
+</p>
+</form>
+</body>
+</html>
+END
+    $r->print($result);
+    return;
+}
+
 #####################################################################
 #####################################################################
 
@@ -1573,8 +1738,8 @@
     abstract mime lang owner copyright creationdate lastrevisiondate hostname/;
 
 my %Datatypes = 
-    ( id        =>{ type         => 'INT',
-                    restrictions => 'NOT NULL',
+    ( id        =>{ type         => 'MEDIUMINT',
+                    restrictions => 'UNSIGNED NOT NULL',
                     primary_key  => 'yes',
                     auto_inc     => 'yes'
                     },
@@ -1599,7 +1764,8 @@
       );
 
 my @Fullindicies = 
-    qw/title author subject abstract mime language owner copyright/;
+    qw/title/;
+#    qw/title author subject abstract mime language owner copyright/;
     
 ######################################################################
 ######################################################################
@@ -1690,6 +1856,32 @@
 
 =pod
 
+=item &revise_button
+
+Inputs: None
+
+Returns: html string for a 'revise search' button.
+
+=cut
+
+######################################################################
+######################################################################
+sub revise_button {
+    my $revise_phase = 'disp_basic';
+    $revise_phase = 'disp_adv' if ($ENV{'form.searchmode'} eq 'advanced');
+    my $newloc = '/adm/searchcat'.
+        '?persistent_db_id='.$ENV{'form.persistent_db_id'}.
+            '&phase='.$revise_phase;
+    my $result = qq{<input type="button" value="Revise search" name="revise"} .
+        qq{ onClick="parent.location='$newloc';" /> };
+    return $result;
+}
+
+######################################################################
+######################################################################
+
+=pod
+
 =item &run_search 
 
 =cut
@@ -1703,12 +1895,25 @@
     # Timing variables
     #
     my $starttime = time;
-    my $max_time  = 120;  # seconds for the search to complete
+    my $max_time  = 30;  # seconds for the search to complete
     #
     # Print run_search header
     #
-    $r->print("<html><head><title>Search Status</title></head><body>");
-    $r->print("Search: ".$pretty_string."\n");
+    $r->print(<<END);
+<html>
+<head><title>Search Status</title></head>
+<body>
+<form name="statusform" action="" method="post">
+<input type="hidden" name="Queue" value="" />
+END
+    # Check to see if $pretty_string has more than one carriage return.
+    # Assume \n s are following <br /> s and truncate the value.
+    # (there is probably a better way)...
+    my @Lines = split /\n/,$pretty_string;
+    if (@Lines > 1) {
+        $pretty_string = join /\n/,(@Lines[0..1],'....<br />');
+    }
+    $r->print("Search: ".$pretty_string);
     $r->rflush();
     #
     # Determine the servers we need to contact.
@@ -1746,15 +1951,15 @@
     my $hitcountsum;
     my $server; 
     my $status;
+    my $revise = &revise_button();
     $r->print(<<END);
-<form name="statusform" action="" method="post">
-<input type="hidden" name="Queue" value="" />
 <table>
-<tr><th>Status</th><th>Total Matches</th><th>Time Remaining</th></tr>
+<tr><th>Status</th><th>Total Matches</th><th>Time Remaining</th><th></th></tr>
 <tr>
 <td><input type="text" name="status"  value="" size="30" /></td>
 <td><input type="text" name="count"   value="" size="10" /></td>
 <td><input type="text" name="seconds" value="" size="8" /></td>
+<td>$revise</td>
 </tr>
 </table>
 </form>
@@ -1851,18 +2056,12 @@
         }
     }
     &update_status($r,'Search Complete'.$server);
+    &update_seconds($r,0);
     &Apache::lonmysql::disconnect_from_db();
-    # Let the user know
-    #
     # We have run out of time or run out of servers to talk to and
     # results to get.  
-    $r->print("<b>Search Completed.</b>&nbsp;&nbsp;");
-    if ($hitcountsum) {
-        $r->print($hitcountsum." matches were found.");
-    } else {
-        $r->print("There were no successful matches to your query.");
-    }
     $r->print("</body></html>");
+    $r->print("<script>window.location='/adm/searchcat?phase=sort&persistent_db_id=$ENV{'form.persistent_db_id'}';</script>");
     return;
 }
 
@@ -1883,7 +2082,7 @@
     ##
     ## Prev
     my $prev_min = $current_min - $show;
-    $prev_min = 0 if $prev_min < 0;
+    $prev_min = 1 if $prev_min < 1;
     if ($prev_min < $current_min) {
         $links .= qq{
 <a href="/adm/searchcat?$parms&start=$prev_min&show=$show">prev</a>
@@ -1953,22 +2152,7 @@
     ## Prepare the table for querying
     ##
     my $table = $ENV{'form.table'};
-    my $connection_result = &Apache::lonmysql::connect_to_db();
-    if (!defined($connection_result)) {
-        $r->print(&Apache::lonmysql::get_error());
-    }
-    my $table_check = &Apache::lonmysql::check_table($table);
-    if (! defined($table_check)) {
-        $r->print("A MySQL error has occurred.</form></body></html>");
-        &Apache::lonnet::logthis("lonmysql was unable to determine the status".
-                                 " of table ".$table);
-        return;
-    } elsif (! $table_check) {
-        $r->print("The table of results could not be found.");
-        &Apache::lonnet::logthis("The user requested a table, ".$table.
-                                 ", that could not be found.");
-        return;
-    }
+    return if (! &ensure_db_and_table($r,$table));
     ##
     ## Get the number of results 
     ##
@@ -1978,21 +2162,19 @@
         &Apache::lonnet::logthis("lonmysql was unable to determine the number".
                                  " of rows in table ".$table);
         &Apache::lonnet::logthis(&Apache::lonmysql::get_error());
-        &Apache::lonnet::logthis(&Apache::lonmysql::get_debug());
         return;
     }
     ##
     ## Determine how many results we need to get
     ##
-    $ENV{'form.show'} = 20;
-    $ENV{'form.start'} = 0      if (! exists($ENV{'form.start'}));
-    $ENV{'form.show'}      = 'all'  if (! exists($ENV{'form.show'}));
+    $ENV{'form.start'} = 1      if (! exists($ENV{'form.start'}));
+    $ENV{'form.show'}  = 'all'  if (! exists($ENV{'form.show'}));
     my $min = $ENV{'form.start'};
     my $max;
     if ($ENV{'form.show'} eq 'all') {
         $max = $total_results ;
     } else {
-        $max = $min + $ENV{'form.show'};
+        $max = $min + $ENV{'form.show'} - 1;
         $max = $total_results if ($max > $total_results);
     }
     ##
@@ -2018,7 +2200,7 @@
     ## Get results from MySQL table
     ##
     my @Results = &Apache::lonmysql::get_rows($table,
-                                              'id>'.$min.' AND id<='.$max);
+                                              'id>='.$min.' AND id<='.$max);
     ##
     ## Loop through the results and output them.
     ##
@@ -2386,7 +2568,7 @@
     my $basic_link   = "/adm/searchcat?"."&table=".$ENV{'form.table'}.
         "&persistent_db_id=".$ENV{'form.persistent_db_id'};
     my $results_link = $basic_link."&phase=results".
-        "&pause=10"."&start=0"."&show=20";
+        "&pause=1"."&start=1";
     return $results_link;
 }
 
@@ -2625,12 +2807,21 @@
 
 =item &output_blank_field_error()
 
+Output a complete page that indicates the user has not filled in enough
+information to do a search.
+
+Inputs: $r (Apache request handle), $closebutton, $parms.
+
+Returns: nothing
+
+$parms is extra information to include in the 'Revise search request' link.
+
 =cut
 
 ######################################################################
 ######################################################################
 sub output_blank_field_error {
-    my ($r,$closebutton)=@_;
+    my ($r,$closebutton,$parms)=@_;
     # make query information persistent to allow for subsequent revision
     $r->print(<<BEGINNING);
 <html>
@@ -2644,16 +2835,15 @@
 <h1>Search Catalog</h1>
 <form method="post" action="/adm/searchcat">
 $hidden_fields
-<a href="/adm/searchcat?persistent_db_id=$ENV{'form.persistent_db_id'}"
+<a href="/adm/searchcat?$parms&persistent_db_id=$ENV{'form.persistent_db_id'}"
 >Revise search request</a>&nbsp;
 $closebutton
 <hr />
-<h3>Helpful Message</h3>
+<h3>Unactionable search query.</h3>
 <p>
-Incorrect search query due to blank entry fields.
-You need to fill in the relevant
-fields on the search page in order for a query to be
-processed.
+You did not fill in enough information for the search to be started.
+You need to fill in relevant fields on the search page in order 
+for a query to be processed.
 </p>
 </body>
 </html>
@@ -2696,7 +2886,7 @@
 onClick='this.form.submit();' />
 $closebutton
 <hr />
-<h3>Helpful Message</h3>
+<h3>Error</h3>
 <p>
 $message
 </p>

--matthew1028485681--