--- loncom/interface/lonsearchcat.pm 2006/09/27 19:32:11 1.277 +++ loncom/interface/lonsearchcat.pm 2008/12/12 17:46:25 1.302 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Search Catalog # -# $Id: lonsearchcat.pm,v 1.277 2006/09/27 19:32:11 raeburn Exp $ +# $Id: lonsearchcat.pm,v 1.302 2008/12/12 17:46:25 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -78,7 +78,6 @@ use HTML::Entities(); use Parse::RecDescent; use Apache::lonnavmaps; use Apache::lonindexer(); -use lib '/home/httpd/lib/perl/'; use LONCAPA; ###################################################################### @@ -139,7 +138,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','titleelement','area']); + '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 @@ -191,7 +190,8 @@ sub handler { } &Apache::lonhtmlcommon::add_breadcrumb ({href=>'/adm/searchcat?'. - 'catalogmode='.$env{'form.catalogmode'}. + &Apache::loncommon::inhibit_menu_check(). + '&catalogmode='.$env{'form.catalogmode'}. '&launch='.$env{'form.launch'}. '&mode='.$env{'form.mode'}, text=>"$crumb_text", @@ -259,11 +259,14 @@ sub handler { 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 ## if ($env{'form.catalogmode'} eq 'interactive') { - $closebutton=" END } else { @@ -309,15 +313,17 @@ END $srchtype = 'Portfolio'; } &Apache::lonhtmlcommon::add_breadcrumb - ({href=>'/adm/searchcat?phase=disp_adv&'. - 'catalogmode='.$env{'form.catalogmode'}. + ({href=>'/adm/searchcat?'.&Apache::loncommon::inhibit_menu_check(). + '&phase=disp_adv'. + '&catalogmode='.$env{'form.catalogmode'}. '&launch='.$env{'form.launch'}. '&mode='.$env{'form.mode'}, text=>"Advanced $srchtype Search", bug=>'Searching',}); } elsif ($env{'form.searchmode'} eq 'course search') { &Apache::lonhtmlcommon::add_breadcrumb - ({href=>'/adm/searchcat?phase=disp_adv&'. + ({href=>'/adm/searchcat?'.&Apache::loncommon::inhibit_menu_check(). + '&phase=disp_adv'. 'catalogmode='.$env{'form.catalogmode'}. '&launch='.$env{'form.launch'}. '&mode='.$env{'form.mode'}, @@ -380,7 +386,7 @@ END $errorstring); my $msg = - 'Unable to create table in which to store search results. '. + 'Unable to create table in which to save search results. '. 'The search has been aborted.'; &Apache::loncommon::simple_error_page($r,'Search Error', $msg); @@ -389,7 +395,7 @@ END delete($env{'form.launch'}); if (! &make_form_data_persistent($r,$persistent_db_file)) { my $msg= - 'Unable to properly store search information. '. + 'Unable to properly save search information. '. 'The search has been aborted.'; &Apache::loncommon::simple_error_page($r,'Search Error', $msg); @@ -522,7 +528,7 @@ sub course_search { my $applies = 0; my $symb = $resource->symb(); my $ressymb = $symb; - if ($symb =~ m#(___adm/\w+/\w+)/(\d+)/bulletinboard$#) { + if ($symb =~ m#(___adm/$LONCAPA::domain_re/$LONCAPA::username_re)/(\d+)/bulletinboard$#) { $ressymb = 'bulletin___'.$2.$1.'/'.$2.'/bulletinboard'; unless ($ressymb =~ m#bulletin___\d+___adm/wrapper#) { $ressymb=~s#(bulletin___\d+___)#$1adm/wrapper/#; @@ -751,19 +757,19 @@ sub setup_basic_search { ''; $onlysearchdomain = ''; - if ($area eq 'res') { - $inclext= ''; - } $adv_search_link = ''.&mt('Advanced Search').''; # $scrout.='
'. ''.&searchhelp().''.''. ''. - ''.(' 'x3).$adv_search_link.''.'
'. - ''.(' 'x1).$userelatedwords.''.'
'. - ''.(' 'x1).$onlysearchdomain.''.'
'. - ''.(' 'x1).$inclext.''.'
'. + ''.(' 'x3).$adv_search_link.''.'
'. + ''.(' 'x1).$userelatedwords.''.'
'. + ''.(' 'x1).$onlysearchdomain.''.'
'. + ''.(' 'x1).$inclext.''.'
'. '
'. ''.$/; # @@ -833,10 +839,21 @@ sub print_advanced_search_form{ $closebutton END my $srchtype = 'Catalog'; + my $jscript; if ($env{'form.area'} eq 'portfolio') { $srchtype = 'Portfolio'; + $jscript = ''; + } + my $scrout= &Apache::loncommon::start_page("Advanced $srchtype Search", + $jscript); $scrout .= <<"ENDHEADER"; $bread_crumb @@ -847,7 +864,7 @@ ENDHEADER ''; my %fields=&Apache::lonmeta::fieldnames(); # - $scrout .= '

'.&mt('Standard [_1] Metadata',$srchtype).'

'; + $scrout .= '

'.&mt("Standard $srchtype Metadata").'

'; $scrout .= "\n"; $scrout .= '\n"; @@ -878,7 +895,7 @@ ENDHEADER $env{'form.'.$field.'_related'}, 50); if ($related_word_search{$field}) { - $scrout .= 'related words'; + $scrout .= &mt('related words'); } else { $scrout .= ''.$/; # # Misc metadata @@ -936,33 +948,33 @@ ENDHEADER if ($env{'form.area'} eq 'portfolio') { # Added fields + my $curnumadd = $env{'form.numaddedfields'}; + if ($curnumadd eq '') { + $curnumadd = 1; + } $scrout .= '

'.&mt('Custom Metadata fields').'

'; $scrout .= "
 '. (' 'x2).&searchhelp()."
 '; } @@ -905,11 +922,6 @@ ENDHEADER &Apache::loncommon::domain_select('domains', $env{'form.domains'},1). '
' - } $scrout .= '
\n"; $scrout .= ''.''. - ''. - ''; - for (my $j=1; $j<=$env{'form.numaddedfields'}; $j++) { + &mt('Field Value(s)').''; + + for (my $j=0; $j<$curnumadd; $j++) { my $num = $j+1; $scrout .= ''. + '" size="10" value="'.$env{'form.addedfield_'.$j}. + '" />'. ''; + 'name="addedvalues_'.$j.'" size="15" value="'. + $env{'form.addedvalues_'.$j}.'" />'; } - my $numadded = 1 + $env{'form.numaddedfields'}; - $scrout .= ''. - '
 '. &mt('Field Name').''. - &mt('Field Value(s)').'
'.&mt('1: '). - ''. - '
'.&mt('Custom metadata [_1]: ',$num). ''. '
 
'. - ''. - '
'; + $scrout .= ''. + ''; } else { # # Dynamic metadata @@ -976,7 +988,7 @@ ENDHEADER description=>'Network-wide number of accesses (hits)',}, { name=>'stdno', description=> - 'Total number of students who have worked on this problem',}, + 'Statistics calculated for number of students',}, { name => 'avetries', description=>'Average number of tries till solved',}, { name => 'difficulty', @@ -1023,6 +1035,7 @@ ENDHEADER # Creation/Modification date limits $scrout .= '

'.&mt('Creation and Modification dates').'

'; $scrout .= "\n\n"; + $scrout .= "\n"; my $cafter = &Apache::lonhtmlcommon::date_setter('advsearch', # formname 'creationdate1', # fieldname @@ -1041,10 +1054,10 @@ ENDHEADER '', # state 1, # no_hh_mm_ss ); - $scrout .= &mt(''. - ''. - ''. - '',$cafter,$cbefore); + $scrout .= '' + .'' + .'' + .''; my $lafter = &Apache::lonhtmlcommon::date_setter('advsearch', 'revisiondate1', @@ -1063,10 +1076,10 @@ ENDHEADER '', # state 1, # no_hh_mm_ss ); - $scrout .= &mt(''. - ''. - ''. - '',$lafter,$lbefore); + $scrout .= '' + .'' + .'' + .''; $scrout.="
 ".&mt('Month[_1]Day[_2]Year',' 'x14,' 'x6)."
Created between[_1]
and [_2]
'.&mt('Created between').''.$cafter.'
'.&mt('and').''.$cbefore.'
Last modified between [_1]
and[_2]
'.&mt('Last modified between').''.$lafter.'
'.&mt('and').''.$lbefore.'
\n"; $scrout.=<'.$/; + $scrout .= (' 'x2) + .&mt('[_1] Records per Page',$countselect) + .''.$/; return $scrout; } @@ -1381,7 +1395,7 @@ sub parse_advanced_search { 'lastrevisiondatestart_month','lastrevisiondatestart_day', 'lastrevisiondatestart_year','lastrevisiondateend_month', 'lastrevisiondateend_day','lastrevisiondateend_year') { - $env{'form.'.$field}=~s/[^\w\/\s\(\)\=\-\"\']//g; + $env{'form.'.$field}=~s/[^\w\/\s\(\)\=\-\"\'.\*]//g; } foreach ('mode','form','element') { # is this required? Hmmm. @@ -1414,6 +1428,17 @@ sub parse_advanced_search { $fillflag++; } } + if ($env{'form.area'} eq 'portfolio') { + # Added metadata fields + for (my $i=0; $i<$env{'form.numaddedfields'} ; $i++) { + my $field = $env{'form.addedfield_'.$i}; + $field =~ s/^\s*(\S*)\s*$/$1/; + $field =~ s/\W/_/g; + if ($field ne '') { + $fillflag++; + } + } + } if (! $fillflag) { &output_blank_field_error($r,$closebutton, 'phase=disp_adv',$hidden_fields); @@ -1504,9 +1529,25 @@ sub parse_advanced_search { # # Added metadata fields for (my $i=0; $i<$env{'form.numaddedfields'} ; $i++) { - if (($env{'form.addedfield_'.$i} ne '') && - ($env{'form.addedvalue_'.$i} ne '')) { - my $stuff = 1; #FIXME + my $field = $env{'form.addedfield_'.$i}; + $field =~ s/^\s*(\S*)\s*$/$1/; + $field =~ s/\W/_/g; + $field =~ tr/A-Z/a-z/; + if ($field ne '') { + my $value = $env{'form.addedvalues_'.$i}; + if ($value ne '') { + $value =~ s/'/''/g; #' stupid emacs + my ($error,$query) = + &process_phrase_input($value,0,'pf.value'); + if (!defined($error)) { + push(@queries,"pf.field = '$field' AND $query"); + $pretty_search_string .= + $font.$field.': '. + $env{'form.addedvalues_'.$i}.'
'; + } + } else { + push(@queries,"pf.field = '$field' AND pf.value IS NULL"); + } } } } else { @@ -1599,7 +1640,7 @@ sub parse_advanced_search { # if (@queries) { if ($env{'form.area'} eq 'portfolio') { - $query ="SELECT pm.*,pa.keynum,pa.scope FROM portfolio_metadata pm, portfolio_access pa WHERE (pm.url = pa.url AND (pa.start < NOW() AND (pa.end IS NULL OR pa.end > NOW())) AND (".join(') AND (',@queries).'))'; + $query ="SELECT pm.*,pa.keynum,pa.scope FROM portfolio_metadata pm, portfolio_access pa, portfolio_addedfields pf WHERE (pm.url = pa.url AND pf.url = pm.url AND (pa.start < UTC_TIMESTAMP() AND (pa.end IS NULL OR pa.end > UTC_TIMESTAMP())) AND (".join(') AND (',@queries).'))'; } else { $query="SELECT * FROM metadata WHERE (".join(") AND (",@queries).')'; } @@ -1636,11 +1677,9 @@ sub parse_domain_restrictions { foreach (sort @allowed_domains) { $pretty_domains_string .= "".$_." "; } - foreach (keys(%Apache::lonnet::libserv)) { - if (exists($domain_hash{$Apache::lonnet::hostdom{$_}})) { - push @$libraries_to_query,$_; - } - } + my %servers = &Apache::lonnet::get_servers(\@allowed_domains, + 'library'); + $libraries_to_query = [keys(%servers)]; } return ($libraries_to_query, $pretty_domains_string); @@ -1664,7 +1703,7 @@ sub parse_basic_search { # # Clean up fields for safety for my $field ('basicexp') { - $env{"form.$field"}=~s/[^\w\s\'\"\!\(\)\-]//g; + $env{"form.$field"}=~s/[^\w\s\'\"\!\(\)\-\*]//g; } foreach ('mode','form','element') { # is this required? Hmmm. @@ -1703,7 +1742,7 @@ sub parse_basic_search { #} my $final_query; if ($env{'form.area'} eq 'portfolio') { - $final_query = 'SELECT pm.*,pa.keynum,pa.scope FROM portfolio_metadata pm, portfolio_access pa WHERE (pm.url = pa.url AND (pa.start < NOW() AND (pa.end IS NULL OR pa.end > NOW())) AND '.join(" AND ",@Queries).')'; + $final_query = 'SELECT pm.*,pa.keynum,pa.scope FROM portfolio_metadata pm, portfolio_access pa WHERE (pm.url = pa.url AND (pa.start < UTC_TIMESTAMP() AND (pa.end IS NULL OR pa.end > UTC_TIMESTAMP())) AND '.join(" AND ",@Queries).')'; } else { $final_query = 'SELECT * FROM metadata WHERE '.join(" AND ",@Queries); } @@ -1819,7 +1858,7 @@ sub process_phrase_input { $item[1]; } term: - /[\w\Q:!@#$%^&*()+_=|{}<>,.;\\\/?\E]+/ { + /[\w\Q:!@#$%^&*()+_=|{}<>,.;\\\/?\E\-]+/ { $item[1]; } ENDGRAMMAR @@ -2079,6 +2118,10 @@ sub copyright_check { ($env{'user.domain'} ne $resdom)) { return 0; } + # Check for custom rights + if ($Metadata->{'copyright'} eq 'custom') { + return &Apache::lonnet::customaccess('bre',$Metadata->{'url'}); + } return 1; } @@ -2107,7 +2150,7 @@ sub ensure_db_and_table { ## if (! defined($table) || $table eq '' || $table =~ /\D/ ) { $r->print("Unable to retrieve search results. ". - "Unable to determine the table results were stored in. ". + "Unable to determine the table results were saved in. ". &Apache::loncommon::end_page()); return undef; } @@ -2117,7 +2160,7 @@ sub ensure_db_and_table { 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.". + " are saved.". &Apache::loncommon::end_page()); &Apache::lonnet::logthis("lonsearchcat: unable to get lonmysql to". " connect to database."); @@ -2226,14 +2269,11 @@ END # } # $result.="\n"; my $revise = &revise_button(); - $result.=< -There are $total_results matches to your query. $revise -

-Search: $pretty_query_string -

- -END + $result.='

' + .&mt('There are [_1] matches to your query.',$total_results) + .' '.$revise.'

' + .'

'.&mt('Search: ').$pretty_query_string + .'

'; $r->print($result.&Apache::loncommon::end_page()); return; } @@ -2405,13 +2445,14 @@ Returns: html string for a 'revise searc ###################################################################### ###################################################################### sub revise_button { + my $revisetext = &mt('Revise search'); 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'}. '&cleargroupsort=1'. '&phase='.$revise_phase; - my $result = qq{ }; return $result; } @@ -2449,8 +2490,6 @@ sub run_search { $r->print(< - END # Remove leading and trailing
$pretty_string =~ s:^\s*
::i; @@ -2461,7 +2500,7 @@ END pop(@Lines); } if (@Lines > 2) { - $pretty_string = join '
',(@Lines[0..2],'....
'); + $pretty_string = join '
',(@Lines[0..2],'...
'); } $r->print(&mt("Search: [_1]",$pretty_string)); $r->rflush(); @@ -2475,14 +2514,15 @@ END @Servers_to_contact = ($serverlist); } } else { - @Servers_to_contact = sort(keys(%Apache::lonnet::libserv)); + my %all_library_servers = &Apache::lonnet::all_library(); + @Servers_to_contact = sort(keys(%all_library_servers)); } my %Server_status; # # Check on the mysql table we will use to store results. my $table =$env{'form.table'}; if (! defined($table) || $table eq '' || $table =~ /\D/ ) { - $r->print("Unable to determine table id to store search results in.". + $r->print("Unable to determine table id to save search results in.". "The search has been aborted.". &Apache::loncommon::end_page()); return; @@ -2515,18 +2555,22 @@ END my $server; my $status; my $revise = &revise_button(); - $r->print(< -StatusTotal MatchesTime Remaining - - - - -$revise - - - -END + $r->print('
'."\n". + ''."\n". + '' + .'' + .'' + .&Apache::loncommon::end_data_table_header_row() + .&Apache::loncommon::start_data_table_row() + .'' + .'' + .'' + .&Apache::loncommon::end_data_table_row() + .&Apache::loncommon::end_data_table() + .'
'."\n". + &Apache::loncommon::start_data_table()); + $r->print(&Apache::loncommon::start_data_table_header_row() + .''.&mt('Status').''.&mt('Total Matches').''.&mt('Time Remaining').' '.$revise.'
'); $r->rflush(); &reset_timing(); &update_seconds($r); @@ -2598,8 +2642,6 @@ END my %Fields = &parse_raw_result($result,$server,$tabletype); $Fields{'hostname'} = $server; # - # Skip if external and we did not want that - next if ((! $env{'form.inclext'}) && ($Fields{'url'}=~/^\/ext\//)); # Skip based on copyright next if (! ©right_check(\%Fields)); @@ -2720,7 +2762,7 @@ sub display_results { if ($env{'form.catalogmode'} eq 'import') { if (! tie(%groupsearch_db,'GDBM_File',$diropendb, &GDBM_WRCREAT(),0640)) { - $r->print('Unable to store import results.'. + $r->print('Unable to save import results.'. &Apache::loncommon::end_page()); $r->rflush(); return; @@ -2853,7 +2895,7 @@ sub display_results { ## Output links (if necessary) for 'prev' and 'next' pages. $r->print ('
'. - ''.$sortform.''. + ''.$sortform.''. ''. &prev_next_buttons($min,$env{'form.show'},$total_results). ''. @@ -2957,8 +2999,8 @@ sub catalogmode_output { $title=~ s/\'/\\\'/g; if ($env{'form.catalogmode'} eq 'interactive') { $output.=< + END } @@ -2967,7 +3009,7 @@ END $groupsearch_db{"pre_${fnum}_title"}=$title; $output.=< - END @@ -3194,6 +3236,7 @@ function changeURL(val) { SCRIPT } } + my $inhibit_menu = "&".&Apache::loncommon::inhibit_menu_check(); $js.=< @@ -3337,7 +3380,7 @@ sub detailed_citation_view { $jumpurl=~s|^/ext/|http://|; $result .= ''.$prefix. ''.' '. - ''.$values{'title'}."\n"; $result .= "

\n"; $result .= ''.$values{'author'}.','. @@ -3404,11 +3447,8 @@ sub detailed_citation_view { if (exists($field->{'type'}) && $field->{'type'} eq 'list') { $result .= ''.&mt($field->{'translate'}).''; foreach my $item (split(',',$values{$field->{'name'}})){ - $result .= - &Apache::lonhtmlcommon::crumbs(&Apache::lonnet::clutter($item), - 'preview', - '', - (($env{'form.catalogmode'} eq 'import')?'parent.statusframe.document.forms.statusform':''),2,0,1); + $item = &Apache::lonnet::clutter($item); + $result .= &display_url($item,[2,0,1]); } } elsif (exists($field->{'format'}) && $field->{'format'} ne ''){ $result.= &mt($field->{'translate'}, @@ -3416,15 +3456,7 @@ sub detailed_citation_view { $values{$field->{'name'}}))."
\n"; } else { if ($field->{'special'} eq 'url link') { - if ($jumpurl=~/^http\:\/\//) { - $result.=''.$jumpurl.''; - } else { - $result .= - &Apache::lonhtmlcommon::crumbs($jumpurl, - 'preview', - '', - (($env{'form.catalogmode'} eq 'import')?'parent.statusframe.document.forms.statusform':''),3,0,1); - } + $result .= &display_url($jumpurl,[3,0,1]); } else { $result.= &mt($field->{'translate'}, $values{$field->{'name'}}); @@ -3480,19 +3512,13 @@ sub summary_view { $result .= ' '.$tmp.' '; } my $jumpurl=$values{'url'}; - my $link; - if ($jumpurl=~m|^/ext/|) { - $jumpurl=~s|^/ext/|http://|; - $link='
'.$jumpurl.''; - } else { - $link=&Apache::lonhtmlcommon::crumbs($jumpurl, - 'preview', - '', - (($env{'form.catalogmode'} eq 'import')?'parent.statusframe.document.forms.statusform':''),2,0,1); - } + $jumpurl=~s|^/ext/|http://|; + my $link = &display_url($jumpurl,[2,0,1]); + $result.=<$values{'title'}$link +$values{'title'}
+$link
$values{'author'}, $values{'owner'} -- $values{'lastrevisiondate'}
$values{'copyrighttag'}
$values{'extrashow'} @@ -3525,16 +3551,10 @@ sub summary_preview { sub compact_view { my ($prefix,%values) = @_; my $jumpurl=$values{'url'}; - my $link; - if ($jumpurl=~m|^/ext/|) { - $jumpurl=~s|^/ext/|http://|; - $link=''.$jumpurl.''; - } else { - $link=&Apache::lonhtmlcommon::crumbs($jumpurl, - 'preview', - '', - (($env{'form.catalogmode'} eq 'import')?'parent.statusframe.document.forms.statusform':''),1,1,1).' '; - } + $jumpurl=~s|^/ext/|http://|; + + my $link = &display_url($jumpurl,[1,1,1]); + my $result = $prefix.''; if (exists($env{'form.sortfield'}) && @@ -3543,12 +3563,30 @@ sub compact_view { if (! defined($tmp)) { $tmp = 'undefined'; } $result .= ' '.$tmp.' '; } - $result.=' '. - $values{'title'}.''.(' 'x2).$link. - ''.$values{'author'}.' ('.$values{'domain'}.')
'; + $jumpurl = &HTML::Entities::encode($jumpurl,'<>&"'); + $result.=' '. + ''. + &HTML::Entities::encode($values{'title'},'<>&"').' '. + $link.' '.$values{'author'}.' ('.$values{'domain'}.')
'; return $result; } +sub display_url { + my ($url,$crumb_args) = @_; + my $link; + if ($url=~m|^/ext/|) { + $url=~s|^/ext/|http://|; + $link=''.$url.''; + } elsif ($url=~m{^(http://|/uploaded/)}) { + $link=''.$url.''; + } else { + $link=&Apache::lonhtmlcommon::crumbs($url, + 'preview', + '', + (($env{'form.catalogmode'} eq 'import')?'parent.statusframe.document.forms.statusform':''),@{$crumb_args}).' '; + } + return $link; +} ###################################################################### ###################################################################### @@ -3572,7 +3610,7 @@ sub fielded_format_view { $prefix

URL:
-
$values{'url'}
END foreach my $field ('title','author','domain','subject','keywords','notes',