--- loncom/interface/lonsearchcat.pm 2004/07/19 16:38:07 1.231 +++ loncom/interface/lonsearchcat.pm 2005/03/21 21:01:15 1.240 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Search Catalog # -# $Id: lonsearchcat.pm,v 1.231 2004/07/19 16:38:07 raeburn Exp $ +# $Id: lonsearchcat.pm,v 1.240 2005/03/21 21:01:15 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -195,9 +195,11 @@ sub handler { &Apache::lonnet::logthis('lonsearchcat:'. 'Unable to recover data from '. $persistent_db_file); - $r->print(< -LON-CAPA Search Error + my $html=&Apache::lonxml::xmlbegin(); + $r->print(< +LON-CAPA Search Error $bodytag We were unable to retrieve data describing your search. This is a serious error and has been logged. Please alert your LON-CAPA administrator. @@ -224,7 +226,8 @@ END } else { # This is a stupid error to give to the user. # It really tells them nothing. - $r->print(''.$bodytag. + my $html=&Apache::lonxml::xmlbegin(); + $r->print($html.''.$bodytag. 'Unable to tie hash to db file'); return OK; } @@ -368,8 +371,11 @@ END &Apache::lonnet::logthis('lonsearchcat.pm: Unable to create '. 'needed table. lonmysql error:'. $errorstring); + my $html=&Apache::lonxml::xmlbegin(); $r->print(<Search Error +$html + +Search Error $bodytag Unable to create table in which to store search results. The search has been aborted. @@ -380,8 +386,11 @@ END } delete($ENV{'form.launch'}); if (! &make_form_data_persistent($r,$persistent_db_file)) { - $r->print(<Search Error + my $html=&Apache::lonxml::xmlbegin(); + $r->print(< +Search Error $bodytag Unable to properly store search information. The search has been aborted. @@ -449,12 +458,22 @@ my %alreadyseen; my %hash; my $totalfound; +sub make_symb { + my ($id)=@_; + my ($mapid,$resid)=split(/\./,$id); + my $map=$hash{'map_id_'.$mapid}; + my $res=$hash{'src_'.$id}; + my $symb=&Apache::lonnet::encode_symb($map,$resid,$res); + return $symb; +} + sub course_search { my $r=shift; my $bodytag=&Apache::loncommon::bodytag('Course Search'); my $pretty_search_string = ''.$ENV{'form.courseexp'}.''; my $search_string = $ENV{'form.courseexp'}; my @New_Words; + undef(%alreadyseen); if ($ENV{'form.crsrelated'}) { ($search_string,@New_Words) = &related_version($ENV{'form.courseexp'}); if (@New_Words) { @@ -467,20 +486,23 @@ sub course_search { my $discuss=$ENV{'form.crsdiscuss'}; my @allwords=($search_string,@New_Words); $totalfound=0; - $r->print('LON-CAPA Course Search'. + my $html=&Apache::lonxml::xmlbegin(); + $r->print($html.'LON-CAPA Course Search'. $bodytag.'
'.$pretty_search_string.'

'.&mt('Course content').':
'); $r->rflush(); # ======================================================= Go through the course - undef %alreadyseen; - %alreadyseen=(); my $c=$r->connection; if (tie(%hash,'GDBM_File',$ENV{'request.course.fn'}.".db", &GDBM_READER(),0640)) { - foreach (keys %hash) { + foreach (sort(keys(%hash))) { if ($c->aborted()) { last; } - if (($_=~/^src\_(.+)$/) && (!$alreadyseen{$hash{$_}})) { - &checkonthis($r,$hash{$_},0,$hash{'title_'.$1},$fulltext, - @allwords); + if (($_=~/^src\_(.+)$/)) { + if ($hash{'randomout_'.$1} & !$ENV{'request.role.adv'}) { + next; + } + my $symb=&make_symb($1); + &checkonthis($r,$1,$hash{$_},0,&Apache::lonnet::gettitle($symb), + $fulltext,$symb,@allwords); } } untie(%hash); @@ -570,15 +592,22 @@ sub course_search { # =============================== This pulls up a resource and its dependencies sub checkonthis { - my ($r,$url,$level,$title,$fulltext,@allwords)=@_; - $alreadyseen{$url}=1; + my ($r,$id,$url,$level,$title,$fulltext,$symb,@allwords)=@_; + $alreadyseen{$id}=1; + if (&Apache::loncommon::connection_aborted($r)) { return; } $r->rflush(); - my $result=&Apache::lonnet::metadata($url,'title').' '. - &Apache::lonnet::metadata($url,'subject').' '. - &Apache::lonnet::metadata($url,'abstract').' '. - &Apache::lonnet::metadata($url,'keywords'); - if (($url) && ($fulltext)) { - $result.=&Apache::lonnet::ssi_body($url); + + 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='.&Apache::lonnet::escape($symb)); } $result=~s/\s+/ /gs; my $applies = 0; @@ -589,8 +618,15 @@ sub checkonthis { for (my $i=0;$i<=$level*5;$i++) { $r->print(' '); } - $r->print(''. - ($title?$title:$url).'
'); + my $href=$url; + if ($hash{'encrypted_'.$id} && !$ENV{'request.role.adv'}) { + $href=&Apache::lonenc::encrypted($href) + .'?symb='.&Apache::lonenc::encrypted($symb); + } else { + $href.='?symb='.&Apache::lonnet::escape($symb); + } + $r->print(''.($title?$title:$url). + '
'); $totalfound++; } elsif ($fulltext) { $r->print(' .'); @@ -600,8 +636,8 @@ sub checkonthis { my $dependencies= &Apache::lonnet::metadata($url,'dependencies'); foreach (split(/\,/,$dependencies)) { - if (($_=~/^\/res\//) && (!$alreadyseen{$_})) { - &checkonthis($r,$_,$level+1,'',$fulltext,@allwords); + if (($_=~/^\/res\//) && (!$alreadyseen{$id})) { + &checkonthis($r,$id,$_,$level+1,'',$fulltext,undef,@allwords); } } } @@ -627,8 +663,9 @@ sub untiehash { } # End of course search scoping sub search_html_header { + my $html=&Apache::lonxml::xmlbegin(); my $Str = < +$html The LearningOnline Network with CAPA @@ -659,18 +696,18 @@ sub print_basic_search_form { my $scrout = &search_html_header().$bodytag; if (&Apache::lonnet::allowed('bre',$ENV{'request.role.domain'})) { # Define interface components - my $userelatedwords= + my $userelatedwords= ''; + my $onlysearchdomain=''; my $adv_search_link = '

@@ -1498,8 +1535,9 @@ sub parse_advanced_search { ## ## Deal with restrictions to given domains ## - my ($libraries_to_query,$pretty_domains_string) = + my ($libraries_to_query,$pretty_domains_string,$domain_sql_restriction) = &parse_domain_restrictions(); + push(@queries,$domain_sql_restriction); $pretty_search_string .= $pretty_domains_string."
\n"; # if (@queries) { @@ -1507,7 +1545,7 @@ sub parse_advanced_search { } elsif ($customquery) { $query = ''; } - # &Apache::lonnet::logthis('query = '.$/.$query); + &Apache::lonnet::logthis('advanced query = '.$/.$query); return ($query,$customquery,$customshow,$libraries_to_query, $pretty_search_string); } @@ -1517,7 +1555,7 @@ sub parse_domain_restrictions { # $ENV{'form.domains'} can be either a scalar or an array reference. # We need an array. if (! exists($ENV{'form.domains'}) || $ENV{'form.domains'} eq '') { - return (undef,''); + return (undef,'',undef); } my @allowed_domains; if (ref($ENV{'form.domains'})) { @@ -1528,17 +1566,21 @@ sub parse_domain_restrictions { # my %domain_hash = (); my $pretty_domains_string; + my $domain_sql_restriction; foreach (@allowed_domains) { $domain_hash{$_}++; } if ($domain_hash{'any'}) { $pretty_domains_string = "In all LON-CAPA domains."; + $domain_sql_restriction = undef; } else { if (@allowed_domains > 1) { $pretty_domains_string = "In LON-CAPA domains:"; } else { $pretty_domains_string = "In LON-CAPA domain "; } + $domain_sql_restriction = + '(domain="'.join('" OR domain="',@allowed_domains).'")'; foreach (sort @allowed_domains) { $pretty_domains_string .= "".$_." "; } @@ -1548,7 +1590,9 @@ sub parse_domain_restrictions { } } } - return ($libraries_to_query,$pretty_domains_string); + return ($libraries_to_query, + $pretty_domains_string, + $domain_sql_restriction); } ###################################################################### @@ -1577,7 +1621,7 @@ sub parse_basic_search { $ENV{"form.$_"}=&Apache::lonnet::unescape($ENV{"form.$_"}); $ENV{"form.$_"}=~s/[^\w\/\s\(\)\=\-\"\']//g; } - my ($libraries_to_query,$pretty_domains_string) = + my ($libraries_to_query,$pretty_domains_string,$domain_sql_restriction) = &parse_domain_restrictions(); # # Check to see if enough of a query is filled in @@ -1601,6 +1645,9 @@ sub parse_basic_search { return; } push(@Queries,$SQLQuery); + if (defined($domain_sql_restriction) && $domain_sql_restriction ne '') { + push(@Queries,$domain_sql_restriction); + } #foreach my $q (@Queries) { # &Apache::lonnet::logthis(' '.$q); #} @@ -1611,7 +1658,7 @@ sub parse_basic_search { } $pretty_search_string .= "
\n"; $pretty_search_string =~ s:^
and ::; - # &Apache::lonnet::logthis($final_query); + &Apache::lonnet::logthis('simple search final query = '.$/.$final_query); return ($final_query,$pretty_search_string, $libraries_to_query); } @@ -2085,8 +2132,9 @@ sub print_sort_form { return; } my $result; + my $html=&Apache::lonxml::xmlbegin(); $result.=< +$html