[LON-CAPA-cvs] cvs: loncom /interface lonsearchcat.pm

www lon-capa-cvs@mail.lon-capa.org
Sat, 08 Mar 2003 01:43:12 -0000


www		Fri Mar  7 20:43:12 2003 EDT

  Modified files:              
    /loncom/interface	lonsearchcat.pm 
  Log:
  Search course contents
  
  
Index: loncom/interface/lonsearchcat.pm
diff -u loncom/interface/lonsearchcat.pm:1.166 loncom/interface/lonsearchcat.pm:1.167
--- loncom/interface/lonsearchcat.pm:1.166	Mon Feb 10 16:25:43 2003
+++ loncom/interface/lonsearchcat.pm	Fri Mar  7 20:43:12 2003
@@ -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 @@
 
 # ------------------------------------------------- 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 $hidden_fields;
 my $bodytag;
 
+my %alreadyseen;
+my $hashtied;
+my %hash;
+ 
 ######################################################################
 ######################################################################
 
@@ -338,6 +342,8 @@
             &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 @@
 ######################################################################
 ######################################################################
 
+sub course_search {
+    my $r=shift;
+    my $bodytag=&Apache::loncommon::bodytag('Course Search');
+    my $pretty_search_string = '<b>'.$ENV{'form.courseexp'}.'</b>';
+    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: <b>@New_Words</b>.";
+        } else {
+            $pretty_search_string .= " with no related words.";
+        }
+    }
+    my $fulltext=$ENV{'form.crsfulltext'};
+    my @allwords=($search_string,@New_Words);
+    $r->print('<html><head><title>LON-CAPA Course Search</title></head>'.
+	      $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('</body></html>');
+}
+
+# ---------------------------------------------------------------- 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('<br />');
+       for (my $i=0;$i<=$level*5;$i++) {
+           $r->print('&nbsp;');
+       }
+       $r->print('<a href="'.$url.'" target="cat">'.
+		 ($title?$title:$url).'</a>');
+       $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 @@
 
 sub print_basic_search_form{
     my ($r,$closebutton) = @_;
-    my $bodytag=&Apache::loncommon::bodytag('Catalog Search');
+    my $bodytag=&Apache::loncommon::bodytag('Search');
     my $scrout=<<"ENDDOCUMENT";
 <html>
 <head>
@@ -423,6 +525,10 @@
 </script>
 </head>
 $bodytag
+ENDDOCUMENT
+if (&Apache::lonnet::allowed('bre',$ENV{'request.role.domain'})) {
+    $scrout.=(<<ENDDOCUMENT);
+<h1>Catalog Search</h1>
 <form method="post" action="/adm/searchcat">
 <input type="hidden" name="phase" value="basic_search" />
 $hidden_fields
@@ -460,6 +566,35 @@
 per page.
 </p>
 </form>
+ENDDOCUMENT
+    if ($ENV{'request.course.id'}) { $scrout.='<hr />'; }
+}
+if ($ENV{'request.course.id'}) {
+    $scrout.=(<<ENDCOURSESEARCH);
+<h1>Course Search</h1>    
+<form method="post" action="/adm/searchcat">
+<input type="hidden" name="phase" value="course_search" />
+$hidden_fields
+<p>
+Enter terms or phrases, then press SEARCH below.
+</p>
+<p>
+<table>
+<tr><td>
+ENDCOURSESEARCH
+    $scrout.='&nbsp;'.&simpletextfield('courseexp',$ENV{'form.courseexp'},40);
+    my $crscheckbox = &simplecheckbox('crsfulltext',$ENV{'form.crsfulltext'});
+    my $relcheckbox = &simplecheckbox('crsrelated',$ENV{'form.crsrelated'});
+$scrout.=(<<ENDENDCOURSE);
+</td></tr>
+<tr><td>$relcheckbox use related words</td><td></td></tr>
+<tr><td>$crscheckbox fulltext search (time consuming)</td><td></td></tr>
+</table><p>
+&nbsp;<input type="submit" name="coursesubmit" value='SEARCH' />
+</p>
+ENDENDCOURSE
+}
+    $scrout.=(<<ENDDOCUMENT);
 </body>
 </html>
 ENDDOCUMENT
@@ -2968,6 +3103,7 @@
 	  &Apache::lonnet::logthis('Failed cleanup searchcat: groupsearch_db');
         }
     }
+    &untiehash();
     &Apache::lonmysql::disconnect_from_db();
 }