--- loncom/interface/lonsearchcat.pm 2002/08/01 14:11:57 1.150 +++ loncom/interface/lonsearchcat.pm 2002/08/04 18:28:01 1.151 @@ -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 @@ END } } $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 @@ END &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(< +per page.

@@ -492,6 +505,13 @@ ENDHEADER $ENV{'form.viewselect'}, undef,undef,undef, sort(keys(%Views))); + $scrout.=' '; + $scrout.=&selectbox(undef,'show', + $ENV{'form.show'}, + undef,undef,undef, + (10,20,50,100)); + $scrout.=' '. + 'Per Page'; $scrout.="Related
Words\n"; $scrout.=&searchphrasefield_with_related('title', 'title' , $ENV{'form.title'}); @@ -1123,6 +1143,7 @@ sub parse_advanced_search { $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 @@ sub parse_advanced_search { } } 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 @@ sub parse_basic_search { # 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 = ''.$ENV{'form.basicexp'}.''; @@ -1530,7 +1551,6 @@ sub build_date_queries { ###################################################################### ###################################################################### - sub copyright_check { my $Metadata = shift; # Check copyright tags and skip results the user cannot use @@ -1550,6 +1570,151 @@ sub copyright_check { 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. ". + ""); + 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. "); + &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."); + &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."); + &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.=< + + +Results + + +
+END + +#

Sort Results

+#Sort by: }; + return $result; +} + +###################################################################### +###################################################################### + +=pod + =item &run_search =cut @@ -1703,12 +1895,25 @@ sub run_search { # 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("Search Status"); - $r->print("Search: ".$pretty_string."\n"); + $r->print(< +Search Status + + + +END + # Check to see if $pretty_string has more than one carriage return. + # Assume \n s are following
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],'....
'); + } + $r->print("Search: ".$pretty_string); $r->rflush(); # # Determine the servers we need to contact. @@ -1746,15 +1951,15 @@ sub run_search { my $hitcountsum; my $server; my $status; + my $revise = &revise_button(); $r->print(< - - + +
StatusTotal MatchesTime Remaining
StatusTotal MatchesTime Remaining
$revise
@@ -1851,18 +2056,12 @@ END } } &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("Search Completed.  "); - if ($hitcountsum) { - $r->print($hitcountsum." matches were found."); - } else { - $r->print("There were no successful matches to your query."); - } $r->print(""); + $r->print(""); return; } @@ -1883,7 +2082,7 @@ sub prev_next_buttons { ## ## 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{ prev @@ -1953,22 +2152,7 @@ sub display_results { ## 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."); - &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 @@ sub display_results { &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 @@ sub display_results { ## 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 @@ sub results_link { 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 @@ sub filled { =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(< @@ -2644,16 +2835,15 @@ BEGINNING

Search Catalog

$hidden_fields -Revise search request  $closebutton
-

Helpful Message

+

Unactionable search query.

-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.

@@ -2696,7 +2886,7 @@ $hidden_fields onClick='this.form.submit();' /> $closebutton
-

Helpful Message

+

Error

$message