--- loncom/interface/lonsearchcat.pm 2003/06/19 13:07:49 1.183 +++ 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.183 2003/06/19 13:07:49 matthew 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(); @@ -82,102 +70,48 @@ use Text::Query; use GDBM_File; use Apache::loncommon(); use Apache::lonmysql(); - -# ---------------------------------------- variables used throughout the module +use Apache::lonlocal; ###################################################################### ###################################################################### - -=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 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 tag. my $loaderror=&Apache::lonnet::overloaderror($r); if ($loaderror) { return $loaderror; } @@ -186,7 +120,7 @@ sub handler { # This button is different for the RAT compared to # normal invocation. # - $r->content_type('text/html'); + &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; return OK if $r->header_only; ## @@ -199,7 +133,7 @@ sub handler { &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['catalogmode','launch','acts','mode','form','element','pause', 'phase','persistent_db_id','table','start','show', - 'cleargroupsort']); + 'cleargroupsort','titleelement']); ## ## 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 @@ -281,6 +215,10 @@ END $hidden_fields .= ''."\n"; } + if (exists($ENV{'form.titleelement'})) { + $hidden_fields .= ''."\n"; + } if (exists($ENV{'form.mode'})) { $hidden_fields .= ''."\n"; @@ -329,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, @@ -383,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, @@ -405,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; @@ -427,42 +376,27 @@ 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('

No resources found.

'); + $r->print('

'.&mt('No resources found').'.

'); } # =================================================== Done going through course $r->print(''); } -# ---------------------------------------------------------------- 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 { @@ -508,6 +442,14 @@ sub checkonthis { } } +sub untiehash { + if (tied(%hash)) { + untie(%hash); + } +} + +} # End of course search scoping + ###################################################################### ###################################################################### @@ -523,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"; @@ -540,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.=(<Catalog Search -
+

$CatalogSearch

+ $hidden_fields

-Enter terms or phrases separated by AND, OR, or NOT -then press SEARCH below. +$Statement.

@@ -555,44 +498,52 @@ then press SEARCH below. ENDDOCUMENT $scrout.=' '.&simpletextfield('basicexp',$ENV{'form.basicexp'},40). ' '; - 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,'checked'); + 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.=< - - +href="/adm/searchcat?phase=disp_adv&catalogmode=$ENV{'form.catalogmode'}&launch=$ENV{'form.launch'}&mode=$ENV{'form.mode'}" +>$header + +
Advanced Search
$relatedcheckbox use related words$domaincheckbox only search domain $domain
$relatedcheckbox $userelatedwords$domaincheckbox $onlysearchdomain $domain

  +   $closebutton END - $scrout.=&selectbox(undef,'viewselect', + $scrout.=&selectbox('View','viewselect', $ENV{'form.viewselect'}, undef,undef,undef, sort(keys(%Views))); - $scrout.=&selectbox(undef,'show', + $scrout.=&selectbox('Per Page','show', $ENV{'form.show'}, undef,undef,undef, (10,20,50,100)); $scrout.=< ENDDOCUMENT } if ($ENV{'request.course.id'}) { + my %lt=&Apache::lonlocal::texthash( + 'srch' => 'Search', + 'header' => 'Course Search', + 'note' => 'Enter terms or phrases, then press "Search" below', + ); $scrout.=(< -

Course Search

-
+

$lt{'header'}

+ $hidden_fields

-Enter terms or phrases, then press SEARCH below. +$lt{'note'}.

@@ -609,7 +560,7 @@ ENDCOURSESEARCH
$relcheckbox use related words
$crscheckbox fulltext search (time consuming)

ENDENDCOURSE } @@ -635,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";

@@ -681,8 +632,8 @@ ENDHEADER undef,undef,undef, (10,20,50,100)); $scrout.=' '. - 'Per Page'; - $scrout.="Related
Words\n"; + ''.&mt('Per Page').''; + $scrout.="".&mt('Related').'
'.&mt('Words')."\n"; $scrout.=&searchphrasefield_with_related('title', 'title' , $ENV{'form.title'}); $scrout.=&searchphrasefield('author', 'author' ,$ENV{'form.author'}); @@ -1022,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. @@ -1034,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 ''; } @@ -1057,7 +1008,7 @@ field heading. ############################################### sub fieldtitle { - my $title = uc(shift()); + my $title = uc(&mt(shift())); return ''.$title. ': '; } @@ -1152,7 +1103,7 @@ END my $i = 1; foreach (qw/January February March April May June July August September October November December /){ - $month .="\n"; + $month .="\n"; $i++; } $month.="\n"; @@ -1229,6 +1180,7 @@ sub selectbox { my ($title,$name,$default,$anyvalue,$anytag,$functionref,@idlist)=@_; if (! defined($functionref)) { $functionref = sub { $_[0]}; } my $selout=''; + $title=&mt($title); if (defined($title)) { my $uctitle=uc($title); $selout="\n".'

'. @@ -1239,12 +1191,12 @@ sub selectbox { foreach (@idlist) { $selout.=''; } elsif ($_ eq $default and /^$anyvalue$/) { - $selout.=' selected >'.$anytag.''; + $selout.=' selected >'.&mt($anytag).''; } - else {$selout.='>'.&{$functionref}($_).'';} + else {$selout.='>'.&mt(&{$functionref}($_)).'';} } return $selout.''.(defined($title)?'

':' '); } @@ -1286,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 = "
\n"; # Clean up fields for safety @@ -1320,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 @@ -1403,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 @@ -1838,9 +1790,10 @@ 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 = - (id => 'Default', + my %SortableFields=&Apache::lonlocal::texthash( + id => 'Default', title => 'Title', author => 'Author', subject => 'Subject', @@ -1852,7 +1805,7 @@ sub print_sort_form { copyright => 'Copyright', hostname => 'Host', creationdate => 'Creation Date', - lastrevisiondate => 'Revision Date', + lastrevisiondate => 'Revision Date' ); ## my $table = $ENV{'form.table'}; @@ -2083,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 @@ -2106,7 +2060,7 @@ END if (@Lines > 2) { $pretty_string = join '
',(@Lines[0..2],'....
'); } - $r->print("Search: ".$pretty_string); + $r->print(&mt("Search").": ".$pretty_string); $r->rflush(); # # Determine the servers we need to contact. @@ -2172,7 +2126,7 @@ END if (@Servers_to_contact) { # Contact one server my $server = shift(@Servers_to_contact); - &update_status($r,'contacting '.$server); + &update_status($r,&mt('contacting').' '.$server); my $reply=&Apache::lonnet::metadata_query($query,$customquery, $customshow,[$server]); ($server) = keys(%$reply); @@ -2184,7 +2138,7 @@ END # left to contact. if (scalar (keys(%Server_status))) { &update_status($r, - 'waiting on '.(join(' ',keys(%Server_status)))); + &mt('waiting on').' '.(join(' ',keys(%Server_status)))); } sleep(1); } @@ -2201,12 +2155,12 @@ END $status=~/^([\.\w]+)$/; my $datafile=$r->dir_config('lonDaemons').'/tmp/'.$1; if (-e $datafile && ! -e "$datafile.end") { - &update_status($r,'Receiving results from '.$server); + &update_status($r,&mt('Receiving results from').' '.$server); next; } last if ($connection->aborted()); if (-e "$datafile.end") { - &update_status($r,'Reading results from '.$server); + &update_status($r,&mt('Reading results from').' '.$server); if (-z "$datafile") { delete($Server_status{$server}); next; @@ -2261,7 +2215,7 @@ END &update_seconds($r,$time_remaining); } } - &update_status($r,'Search Complete'.$server); + &update_status($r,&mt('Search Complete').$server); &update_seconds($r,0); &Apache::lonmysql::disconnect_from_db(); # We have run out of time or run out of servers to talk to and @@ -2333,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)); ## @@ -2406,7 +2360,7 @@ sub display_results { ); if ($total_results == 0) { $r->print(''. - '

There are currently no results.

'. + '

'.&mt('There are currently no results').'.

'. ""); return; } else { @@ -2437,7 +2391,7 @@ sub display_results { $r->rflush(); } if (@Results < 1) { - $r->print("There were no results matching your query"); + $r->print(&mt("There were no results matching your query")); } else { $r->print ('
'. @@ -2680,8 +2634,16 @@ Checked for existance & 'edit' mode. =item 'form.form' +Contains the name of the form that has the input fields to set + =item 'form.element' +the name of the input field to put the URL into + +=item 'form.titleelement' + +the name of the input field to put the title into + =back =cut @@ -2690,6 +2652,7 @@ Checked for existance & 'edit' mode. ###################################################################### 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 @@ -2718,14 +2681,31 @@ SCRIPT } elsif ($ENV{'form.mode'} eq 'edit') { my $form = $ENV{'form.form'}; my $element = $ENV{'form.element'}; + my $titleelement = $ENV{'form.titleelement'}; + my $changetitle; + if (!$titleelement) { + $changetitle='function changeTitle(val) {}'; + } else { + $changetitle=< function select_data(title,url) { changeURL(url); + changeTitle(title); parent.close(); } -function changeTitle(val) { -} +$changetitle function changeURL(val) { if (parent.targetwin.document) { parent.targetwin.document.forms["$form"].elements["$element"].value=val; @@ -2741,10 +2721,19 @@ SCRIPT $result.=<