--- loncom/interface/lonsearchcat.pm 2003/02/10 21:25:43 1.166 +++ loncom/interface/lonsearchcat.pm 2003/03/08 01:43:12 1.167 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Search Catalog # -# $Id: lonsearchcat.pm,v 1.166 2003/02/10 21:25:43 albertel Exp $ +# $Id: lonsearchcat.pm,v 1.167 2003/03/08 01:43:12 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -74,7 +74,7 @@ package Apache::lonsearchcat; # ------------------------------------------------- modules used by this module use strict; -use Apache::Constants qw(:common); +use Apache::Constants qw(:common :http); use Apache::lonnet(); use Apache::File(); use CGI qw(:standard); @@ -142,6 +142,10 @@ my %persistent_db; my $hidden_fields; my $bodytag; +my %alreadyseen; +my $hashtied; +my %hash; + ###################################################################### ###################################################################### @@ -338,6 +342,8 @@ END &run_search($r,$query,$customquery,$customshow, $libraries,$pretty_string); } + } elsif ($ENV{'form.phase'} eq 'course_search') { + &course_search($r); } elsif(($ENV{'form.phase'} eq 'basic_search') || ($ENV{'form.phase'} eq 'adv_search')) { $ENV{'form.searchmode'} = 'basic'; @@ -396,6 +402,102 @@ END ###################################################################### ###################################################################### +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; + if ($ENV{'form.crsrelated'}) { + ($search_string,@New_Words) = &related_version($ENV{'form.courseexp'}); + if (@New_Words) { + $pretty_search_string .= " with related words: @New_Words."; + } else { + $pretty_search_string .= " with no related words."; + } + } + my $fulltext=$ENV{'form.crsfulltext'}; + my @allwords=($search_string,@New_Words); + $r->print('LON-CAPA Course Search'. + $bodytag.$pretty_search_string); + $r->rflush(); +# ======================================================= Go through the course + $hashtied=0; + undef %alreadyseen; + %alreadyseen=(); + &tiehash(); + foreach (keys %hash) { + if (($_=~/^src\_(.+)$/) && (!$alreadyseen{$hash{$_}})) { + &checkonthis($r,$hash{$_},0,$hash{'title_'.$1},@allwords,$fulltext); + } + } + &untiehash(); +# =================================================== 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 { + my ($r,$url,$level,$title,@allwords,$fulltext)=@_; + $alreadyseen{$url}=1; + $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::ssibody($url); + } + $result=~s/\s+/ /gs; + my $applies=0; + foreach (@allwords) { + if ($_=~/\w/) { + if ($result=~/$_/si) { + $applies++; + } + } + } +# Does this resource apply? + if ($applies) { + $r->print('
'); + for (my $i=0;$i<=$level*5;$i++) { + $r->print(' '); + } + $r->print(''. + ($title?$title:$url).''); + $r->rflush(); + } +# Check also the dependencies of this one + my $dependencies= + &Apache::lonnet::metadata($url,'dependencies'); + foreach (split(/\,/,$dependencies)) { + if (($_=~/^\/res\//) && (!$alreadyseen{$_})) { + &checkonthis($r,$_,$level+1,'',@allwords,$fulltext); + } + } +} + +###################################################################### +###################################################################### + =pod =item &print_basic_search_form() @@ -409,7 +511,7 @@ Returns a scalar which holds html for th sub print_basic_search_form{ my ($r,$closebutton) = @_; - my $bodytag=&Apache::loncommon::bodytag('Catalog Search'); + my $bodytag=&Apache::loncommon::bodytag('Search'); my $scrout=<<"ENDDOCUMENT"; @@ -423,6 +525,10 @@ sub print_basic_search_form{ $bodytag +ENDDOCUMENT +if (&Apache::lonnet::allowed('bre',$ENV{'request.role.domain'})) { + $scrout.=(<Catalog Search
$hidden_fields @@ -460,6 +566,35 @@ END per page.

+ENDDOCUMENT + if ($ENV{'request.course.id'}) { $scrout.='
'; } +} +if ($ENV{'request.course.id'}) { + $scrout.=(<Course Search +
+ +$hidden_fields +

+Enter terms or phrases, then press SEARCH below. +

+

+ + + + +
+ENDCOURSESEARCH + $scrout.=' '.&simpletextfield('courseexp',$ENV{'form.courseexp'},40); + my $crscheckbox = &simplecheckbox('crsfulltext',$ENV{'form.crsfulltext'}); + my $relcheckbox = &simplecheckbox('crsrelated',$ENV{'form.crsrelated'}); +$scrout.=(<
$relcheckbox use related words
$crscheckbox fulltext search (time consuming)

+

+ENDENDCOURSE +} + $scrout.=(< ENDDOCUMENT @@ -2968,6 +3103,7 @@ sub cleanup { &Apache::lonnet::logthis('Failed cleanup searchcat: groupsearch_db'); } } + &untiehash(); &Apache::lonmysql::disconnect_from_db(); }