--- loncom/interface/lonsearchcat.pm 2002/06/24 16:45:22 1.128 +++ loncom/interface/lonsearchcat.pm 2008/05/28 21:07:29 1.293 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Search Catalog # -# $Id: lonsearchcat.pm,v 1.128 2002/06/24 16:45:22 harris41 Exp $ +# $Id: lonsearchcat.pm,v 1.293 2008/05/28 21:07:29 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -25,14 +25,6 @@ # # http://www.lon-capa.org/ # -# YEAR=2001 -# 3/8, 3/12, 3/13, 3/14, 3/15, 3/19 Scott Harrison -# 3/20, 3/21, 3/22, 3/26, 3/27, 4/2, 8/15, 8/24, 8/25 Scott Harrison -# 10/12,10/14,10/15,10/16,11/28,11/29,12/10,12/12,12/16 Scott Harrison -# YEAR=2002 -# 1/17 Scott Harrison -# 6/17 Matthew Hall -# ############################################################################### ############################################################################### @@ -40,7 +32,7 @@ =head1 NAME -lonsearchcat +lonsearchcat - LONCAPA Search Interface =head1 SYNOPSIS @@ -56,7 +48,7 @@ described at http://www.lon-capa.org. lonsearchcat presents the user with an interface to search the LON-CAPA digital library. lonsearchcat also initiates the execution of a search by sending the search parameters to LON-CAPA servers. The progress of -search (on a server basis) is displayed to the user in a seperate window. +search (on a server basis) is displayed to the user in a separate window. =head1 Internals @@ -67,245 +59,758 @@ 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); -use Apache::lonnet(); +use Apache::Constants qw(:common :http); +use Apache::lonnet; use Apache::File(); use CGI qw(:standard); use Text::Query; use GDBM_File; use Apache::loncommon(); - -# ---------------------------------------- variables used throughout the module +use Apache::lonmysql(); +use Apache::lonmeta; +use Apache::lonhtmlcommon; +use Apache::lonlocal; +use LONCAPA::lonmetadata(); +use HTML::Entities(); +use Parse::RecDescent; +use Apache::lonnavmaps; +use Apache::lonindexer(); +use LONCAPA; ###################################################################### ###################################################################### - -=pod - -=item Global variables - -=over 4 - -=item $closebutton - -button that closes the search window - -=item $importbutton - -button to take the selecte results and go to group sorting - -=item %hash - -The ubiquitous database hash - -=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(). - -=back - -=cut - -###################################################################### -###################################################################### - -# -- dynamically rendered interface components -my $closebutton; # button that closes the search window -my $importbutton; # button to take the selected results and go to group sorting - -# -- miscellaneous variables -my %hash; # database hash -my $diropendb = ""; # db file - -###################################################################### -###################################################################### - -=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 +## +## Global variables +## +###################################################################### +###################################################################### +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 + +# The different view modes and associated functions + +my %Views = ("detailed" => \&detailed_citation_view, + "detailedpreview" => \&detailed_citation_preview, + "summary" => \&summary_view, + "summarypreview" => \&summary_preview, + "fielded" => \&fielded_format_view, + "xml" => \&xml_sgml_view, + "compact" => \&compact_view); ###################################################################### ###################################################################### sub handler { my $r = shift; - untie %hash; +# &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(). - $r->content_type('text/html'); + my $loaderror=&Apache::lonnet::overloaderror($r); + if ($loaderror) { return $loaderror; } + # + my $closebutton; # button that closes the search window + # This button is different for the RAT compared to + # normal invocation. + # + &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; return OK if $r->header_only; - + ## + ## Prevent caching of the search interface window. Hopefully this means + ## we will get the launch=1 passed in a little more. + &Apache::loncommon::no_cache($r); + ## + ## Pick up form fields passed in the links. + ## + &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, + ['catalogmode','launch','acts','mode','form','element','pause', + 'phase','persistent_db_id','table','start','show', + 'cleargroupsort','titleelement','area','inhibitmenu']); + ## + ## The following is a trick - we wait a few seconds if asked to so + ## the daemon running the search can get ahead of the daemon + ## printing the results. We only need (theoretically) to do + ## this once, so the pause indicator is deleted + ## + if (exists($env{'form.pause'})) { + 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'}_sel_res.db"; + # + # set the name of the persistent database + # $env{'form.persistent_db_id'} can only have digits in it. + if (! exists($env{'form.persistent_db_id'}) || + ($env{'form.persistent_db_id'} =~ /\D/) || + ($env{'form.launch'} eq '1')) { + $env{'form.persistent_db_id'} = time; + } - &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, - ['catalogmode','launch','acts','mode','form','element', - 'reqinterface']); + my $persistent_db_file = "/home/httpd/perl/tmp/". + &escape($domain). + '_'.&escape($env{'user.name'}). + '_'.$env{'form.persistent_db_id'}.'_persistent_search.db'; ## - ## Clear out old values from database + &Apache::lonhtmlcommon::clear_breadcrumbs(); + + my @allowed_searches = ('portfolio'); + if (&Apache::lonnet::allowed('bre',$env{'request.role.domain'})) { + push(@allowed_searches,'res'); + } + if (exists($env{'request.course.id'}) && $env{'request.course.id'} ne '') { + push(@allowed_searches,'course'); + } + my $crumb_text = 'Portfolio Search'; + if (@allowed_searches == 3) { + $crumb_text = 'Course, Portfolio and Catalog Search'; + } elsif (@allowed_searches ==2) { + if (grep(/^res$/,@allowed_searches)) { + $crumb_text = 'Portfolio and Catalog Search'; + } elsif (grep(/^course$/,@allowed_searches)) { + $crumb_text = 'Portfolio and Course Search'; + } + } + &Apache::lonhtmlcommon::add_breadcrumb + ({href=>'/adm/searchcat?'. + &Apache::loncommon::inhibit_menu_check(). + '&catalogmode='.$env{'form.catalogmode'}. + '&launch='.$env{'form.launch'}. + '&mode='.$env{'form.mode'}, + text=>"$crumb_text", + target=>'_top', + bug=>'Searching',}); + # + if ($env{'form.phase'} !~ m/(basic|adv|course)_search/) { + if (! &get_persistent_form_data($persistent_db_file)) { + if ($env{'form.phase'} =~ /(run_search|results)/) { + &Apache::lonnet::logthis('lonsearchcat:'. + 'Unable to recover data from '. + $persistent_db_file); + my $msg = + 'We were unable to retrieve data describing your search. '. + 'This is a serious error and has been logged. '. + 'Please alert your LON-CAPA administrator.'; + &Apache::loncommon::simple_error_page($r,'Search Error', + $msg); + return OK; + } + } + } else { + &clean_up_environment(); + } + ## + ## Clear out old values from groupsearch database ## - if ($ENV{'form.launch'} eq '1') { - if (tie(%hash,'GDBM_File',$diropendb,&GDBM_WRCREAT,0640)) { + untie %groupsearch_db if (tied(%groupsearch_db)); + if (($env{'form.cleargroupsort'} eq '1') || + (($env{'form.launch'} eq '1') && + ($env{'form.catalogmode'} eq 'import'))) { + if (tie(%groupsearch_db,'GDBM_File',$diropendb,&GDBM_WRCREAT(),0640)) { &start_fresh_session(); - untie %hash; + untie %groupsearch_db; + delete($env{'form.cleargroupsort'}); } else { - $r->print('
Unable to tie hash to db '. - 'file'); + # This is a stupid error to give to the user. + # It really tells them nothing. + my $msg = 'Unable to tie hash to db file.'; + &Apache::loncommon::simple_error_page($r,'Search Error', + $msg); return OK; } } ## - ## Produce some output, so people know it is working + ## Configure hidden fields ## - $r->print("\n"); - $r->rflush; + $hidden_fields = ''."\n"; + if (exists($env{'form.catalogmode'})) { + $hidden_fields .= &hidden_field('catalogmode'); + } + if (exists($env{'form.form'})) { + $hidden_fields .= &hidden_field('form'); + } + if (exists($env{'form.element'})) { + $hidden_fields .= &hidden_field('element'); + } + if (exists($env{'form.titleelement'})) { + $hidden_fields .= &hidden_field('titleelement'); + } + if (exists($env{'form.mode'})) { + $hidden_fields .= &hidden_field('mode'); + } + if (exists($env{'form.area'})) { + $hidden_fields .= &hidden_field('area'); + } + if (exists($env{'form.inhibitmenu'})) { + $hidden_fields .= &hidden_field('inhibitmenu'); + } ## ## Configure dynamic components of interface ## - my $hidden; # Holds 'hidden' html forms - if ($ENV{'form.catalogmode'} eq 'interactive') { - $hidden="". - "\n"; - $closebutton=""."\n"; - } elsif ($ENV{'form.catalogmode'} eq 'groupsearch') { - $hidden=<'.&mt('No matches found in resources').'.
'); + } + +# Check discussions if requested + if ($discuss) { + my $totaldiscussions = 0; + $r->print(''.&mt('No matches found in postings').'.
'); + } + } + +# =================================================== Done going through course + $r->print(&Apache::loncommon::end_page()); +} + +# =============================== This pulls up a resource and its dependencies + +sub checkonthis { + my ($r,$id,$url,$level,$title,$fulltext,$symb,@allwords)=@_; + $alreadyseen{$id}=1; + if (&Apache::loncommon::connection_aborted($r)) { return; } + $r->rflush(); + + my $result=$title.' '; + if ($env{'request.role.adv'} || !$hash{'encrypted_'.$id}) { + $result.=&Apache::lonnet::metadata($url,'title').' '. + &Apache::lonnet::metadata($url,'subject').' '. + &Apache::lonnet::metadata($url,'abstract').' '. + &Apache::lonnet::metadata($url,'keywords'); + } + my ($extension)=($url=~/\.(\w+)$/); + if (&Apache::loncommon::fileembstyle($extension) eq 'ssi' && + ($url) && ($fulltext)) { + $result.=&Apache::lonnet::ssi_body($url.'?symb='.&escape($symb)); + } + $result=~s/\s+/ /gs; + my $applies = 0; + $applies = &checkwords($result,$applies,@allwords); +# Does this resource apply? + if ($applies) { + $r->print('' + .&mt('There are [_1] matches to your query.',$total_results) + .' '.$revise.'
' + .''.&mt('Search: ').$pretty_query_string + .'
'; + $r->print($result.&Apache::loncommon::end_page()); + return; +} + +##################################################################### +##################################################################### + +=pod + +=item MySQL Table Description + +MySQL table creation requires a precise description of the data to be +stored. The use of the correct types to hold data is vital to efficient +storage and quick retrieval of records. The columns must be described in +the following format: + +=cut + +##################################################################### +##################################################################### +# +# These should probably be scoped but I don't have time right now... +# +my @Datatypes; +my @Fullindicies; + +###################################################################### +###################################################################### + +=pod + +=item &create_results_table() + +Creates the table of search results by calling lonmysql. Stores the +table id in $env{'form.table'} + +Inputs: search area - either res or portfolio + +Returns: the identifier of the table on success, undef on error. + +=cut + +###################################################################### +###################################################################### +sub set_up_table_structure { + my ($tabletype) = @_; + my ($datatypes,$fullindicies) = + &LONCAPA::lonmetadata::describe_metadata_storage($tabletype); + # Copy the table description before modifying it... + @Datatypes = @{$datatypes}; + unshift(@Datatypes,{name => 'id', + type => 'MEDIUMINT', + restrictions => 'UNSIGNED NOT NULL', + primary_key => 'yes', + auto_inc => 'yes' }); + @Fullindicies = @{$fullindicies}; + return; +} + +sub create_results_table { + my ($area) = @_; + if ($area eq 'portfolio') { + &set_up_table_structure('portfolio_search'); + } else { + &set_up_table_structure('metadata'); + } + my $table = &Apache::lonmysql::create_table + ( { columns => \@Datatypes, + FULLTEXT => [{'columns' => \@Fullindicies},], + } ); + if (defined($table)) { + $env{'form.table'} = $table; + return $table; + } + return undef; # Error... +} + +###################################################################### +###################################################################### + +=pod + +=item Search Status update functions + +Each of the following functions changes the values of one of the +input fields used to display the search status to the user. The names +should be explanatory. + +Inputs: Apache request handler ($r), text to display. + +Returns: Nothing. + +=over 4 + +=item &update_count_status() + +=item &update_status() + +=item &update_seconds() + +=back + +=cut + +###################################################################### +###################################################################### +sub update_count_status { + my ($r,$text) = @_; + $text =~ s/\'/\\\'/g; + $r->print + ("\n"); $r->rflush(); - # begin showing the cataloged results - $r->print(<Basic search: '.$ENV{'form.basicexp'}.'
'); - } elsif ($mode eq 'Advanced') { - $r->print('Advanced search '.$query.'
'); + my $start_page = &Apache::loncommon::start_page('Search Status',undef, + {'no_title' => 1}); + my $breadcrumbs = + &Apache::lonhtmlcommon::breadcrumbs('Searching','Searching', + $env{'form.catalogmode'} ne 'import'); + $r->print(<\n";
- if ($ENV{'form.catalogmode'} eq 'interactive') {
- my $titleesc=$Fields{'title'};
- $titleesc=~s/\'/\\'/; # '
- $compiledresult.=<
-END
+ # Read in the whole file.
+ while (my $result = <$fh>) {
+ last if ($connection->aborted());
+ #
+ # Records are stored one per line
+ chomp($result);
+ next if (! $result);
+ #
+ # Parse the result.
+ my %Fields = &parse_raw_result($result,$server,$tabletype);
+ $Fields{'hostname'} = $server;
+ #
+ # Skip based on copyright
+ next if (! ©right_check(\%Fields));
+
+ if ($area eq 'portfolio') {
+ next if (defined($matches{$Fields{'url'}}));
+ # Skip if inaccessible
+ next if (!&Apache::lonnet::portfolio_access($Fields{'url'}));
+ $matches{$Fields{'url'}} = 1;
+ }
+ #
+ # Store the result in the mysql database
+ my $result = &Apache::lonmysql::store_row($table,\%Fields);
+ if (! defined($result)) {
+ $r->print(&Apache::lonmysql::get_error());
+ }
+ #
+ $hitcountsum ++;
+ &update_seconds($r);
+ if ($hitcountsum % 50 == 0) {
+ &update_count_status($r,$hitcountsum);
+ }
}
- if ($ENV{'form.catalogmode'} eq 'groupsearch') {
- $fnum+=0;
- $hash{"pre_${fnum}_link"}=$Fields{'url'};
- $hash{"pre_${fnum}_title"}=$Fields{'title'};
- $compiledresult.=<
-END
-#
-#
- $fnum++;
- }
- my $viewselect;
- if ($mode eq 'Basic') {
- $viewselect=$ENV{'form.basicviewselect'};
- }
- elsif ($mode eq 'Advanced') {
- $viewselect=$ENV{'form.advancedviewselect'};
- }
- if ($viewselect eq 'Detailed Citation View') {
- $compiledresult.=&detailed_citation_view
- (%Fields, hostname => $rkey );
- }
- elsif ($viewselect eq 'Summary View') {
- $compiledresult.=&summary_view
- (%Fields, hostname => $rkey );
- }
- elsif ($viewselect eq 'Fielded Format') {
- $compiledresult.=&fielded_format_view
- (%Fields, hostname => $rkey );
- }
- elsif ($viewselect eq 'XML/SGML') {
- $compiledresult.=&xml_sgml_view
- (%Fields, hostname => $rkey );
- }
+ $fh->close();
+ # $server is only deleted if the results file has been
+ # found and (successfully) opened. This may be a bad idea.
+ delete($Server_status{$server});
+ }
+ last if ($connection->aborted());
+ &update_count_status($r,$hitcountsum);
+ }
+ last if ($connection->aborted());
+ &update_seconds($r);
+ }
+ &update_status($r,&mt('Search Complete [_1]',$server));
+ &update_seconds($r);
+ #
+ &Apache::lonmysql::disconnect_from_db(); # This is unneccessary
+ #
+ # We have run out of time or run out of servers to talk to and
+ # results to get, so let the client know the top frame needs to be
+ # loaded from /adm/searchcat
+ $r->print(&Apache::loncommon::end_page());
+# if ($env{'form.catalogmode'} ne 'import') {
+ $r->print("");
+# }
+ return;
+}
+
+######################################################################
+######################################################################
+
+=pod
+
+=item &prev_next_buttons()
+
+Returns html for the previous and next buttons on the search results page.
+
+=cut
+
+######################################################################
+######################################################################
+sub prev_next_buttons {
+ my ($current_min,$show,$total,$parms) = @_;
+ return '' if ($show eq 'all'); # No links if you get them all at once.
+ #
+ # Create buttons
+ my $buttons = '';
+ $buttons .= ' 'x3;
+ $buttons .= 'connection;
+ $r->print(&search_results_header($importbutton,$closebutton));
+ ##
+ ## Set viewing function
+ ##
+ my $viewfunction = $Views{$env{'form.viewselect'}};
+ if (!defined($viewfunction)) {
+ $r->print("Internal Error - Bad view selected.\n");
+ $r->rflush();
+ return;
+ }
+ ##
+ ## $checkbox_num is a count of the number of checkboxes output on the
+ ## page this is used only during catalogmode=import.
+ my $checkbox_num = 0;
+ ##
+ ## Get the catalog controls setup
+ ##
+ my $action = "/adm/searchcat?phase=results";
+ ##
+ ## Deal with import by opening the import db file.
+ if ($env{'form.catalogmode'} eq 'import') {
+ if (! tie(%groupsearch_db,'GDBM_File',$diropendb,
+ &GDBM_WRCREAT(),0640)) {
+ $r->print('Unable to save import results.'.
+ &Apache::loncommon::end_page());
+ $r->rflush();
+ return;
+ }
+ }
+ ##
+ ## Prepare the table for querying
+ 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::loncommon::end_page());
+ &Apache::lonnet::logthis("lonmysql was unable to determine the number".
+ " of rows in table ".$table);
+ &Apache::lonnet::logthis(&Apache::lonmysql::get_error());
+ return;
+ }
+ ##
+ ## Determine how many results we need to get
+ $env{'form.start'} = 1 if (! exists($env{'form.start'}));
+ $env{'form.show'} = 20 if (! exists($env{'form.show'}));
+ if (exists($env{'form.prev'})) {
+ $env{'form.start'} -= $env{'form.show'};
+ } elsif (exists($env{'form.next'})) {
+ $env{'form.start'} += $env{'form.show'};
+ }
+ $env{'form.start'} = 1 if ($env{'form.start'}<1);
+ $env{'form.start'} = $total_results if ($env{'form.start'}>$total_results);
+ my $min = $env{'form.start'};
+ my $max;
+ if ($env{'form.show'} eq 'all') {
+ $max = $total_results ;
+ } else {
+ $max = $min + $env{'form.show'} - 1;
+ $max = $total_results if ($max > $total_results);
+ }
+ ##
+ ## Output form elements
+ $r->print(&hidden_field('table').
+ &hidden_field('phase').
+ &hidden_field('persistent_db_id').
+ &hidden_field('start').
+ &hidden_field('area')
+ );
+ #
+ # Build sorting selector
+ my @fields =
+ (
+ {key=>'default' },
+ {key=>'title' },
+ {key =>'author' },
+ {key =>'subject'},
+ {key =>'url',desc=>'URL'},
+ {key =>'keywords'},
+ {key =>'language'},
+ {key =>'creationdate'},
+ {key =>'lastrevisiondate'},
+ {key =>'owner'},
+ {key =>'copyright'},
+ {key =>'authorspace'},
+ {key =>'lowestgradelevel'},
+ {key =>'highestgradelevel'},
+ {key =>'standards',desc=>'Standards'},
+ );
+ if ($area eq 'portfolio') {
+ push(@fields,
+ (
+ {key => 'scope'},
+ {key => 'keynum'},
+ ));
+ } else {
+ push(@fields,
+ (
+ {key =>'count',desc=>'Number of accesses'},
+ {key =>'stdno',desc=>'Students Attempting'},
+ {key =>'avetries',desc=>'Average Number of Tries'},
+ {key =>'difficulty',desc=>'Mean Degree of Difficulty'},
+ {key =>'disc',desc=>'Mean Degree of Discrimination'},
+ {key =>'clear',desc=>'Evaluation: Clear'},
+ {key =>'technical',desc=>'Evaluation: Technically Correct'},
+ {key =>'correct',desc=>'Evaluation: Material is Correct'},
+ {key =>'helpful',desc=>'Evaluation: Material is Helpful'},
+ {key =>'depth',desc=>'Evaluation: Material has Depth'},
+ ));
+ }
+ my %fieldnames = &Apache::lonmeta::fieldnames();
+ my @field_order;
+ foreach my $field_data (@fields) {
+ push(@field_order,$field_data->{'key'});
+ if (! exists($field_data->{'desc'})) {
+ $field_data->{'desc'}=$fieldnames{$field_data->{'key'}};
+ } else {
+ if (! defined($field_data->{'desc'})) {
+ $field_data->{'desc'} = ucfirst($field_data->{'key'});
}
- untie %hash;
+ $field_data->{'desc'} = &mt($field_data->{'desc'});
+ }
+ }
+ my %sort_fields = map {$_->{'key'},$_->{'desc'}} @fields;
+ $sort_fields{'select_form_order'} = \@field_order;
+ $env{'form.sortorder'} = 'desc' if (! exists($env{'form.sortorder'}));
+ if (! exists($env{'form.sortfield'})) {
+ if ($area eq 'portfolio') {
+ $env{'form.sortfield'} = 'owner';
+ } else {
+ $env{'form.sortfield'} = 'count';
}
- if ($compiledresult) {
- $resultflag=1;
- $r->print($compiledresult);
+ }
+ if (! exists($env{'form.sortorder'})) {
+ if ($env{'form.sortfield'}=~/^(count|stdno|disc|clear|technical|correct|helpful)$/) {
+ $env{'form.sortorder'}='desc';
+ } else {
+ $env{'form.sortorder'}='asc';
}
- my $percent=sprintf('%3.0f',($servercount/$servernum*100));
- } # End of foreach loop over servers remaining
- } # End of big loop - while($serversleft && $timeremain)
- unless ($resultflag) {
- $r->print("\nThere were no results that matched your query\n");
}
-# $r->print(''."\n"); $r->rflush();
- $r->print("\n