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

matthew lon-capa-cvs@mail.lon-capa.org
Mon, 22 Dec 2003 22:40:37 -0000


This is a MIME encoded message

--matthew1072132837
Content-Type: text/plain

matthew		Mon Dec 22 17:40:37 2003 EDT

  Modified files:              
    /loncom/interface	lonsearchcat.pm 
  Log:
  Removed many global variables and cleaned up POD.  Working towards bug 2537.
  
  
--matthew1072132837
Content-Type: text/plain
Content-Disposition: attachment; filename="matthew-20031222174037.txt"

Index: loncom/interface/lonsearchcat.pm
diff -u loncom/interface/lonsearchcat.pm:1.195 loncom/interface/lonsearchcat.pm:1.196
--- loncom/interface/lonsearchcat.pm:1.195	Tue Dec 16 10:00:56 2003
+++ loncom/interface/lonsearchcat.pm	Mon Dec 22 17:40:37 2003
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Search Catalog
 #
-# $Id: lonsearchcat.pm,v 1.195 2003/12/16 15:00:56 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 @@
 ###############################################################################
 ###############################################################################
 
-###############################################################################
-##                                                                           ##
-## 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();
@@ -84,101 +72,46 @@
 use Apache::lonmysql();
 use Apache::lonlocal;
 
-# ---------------------------------------- variables used throughout the module
-
 ######################################################################
 ######################################################################
-
-=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 <body> 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 <body> tag.
 
     my $loaderror=&Apache::lonnet::overloaderror($r);
     if ($loaderror) { return $loaderror; }
@@ -334,11 +267,11 @@
     ## 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,
@@ -388,10 +321,10 @@
         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,
@@ -410,6 +343,17 @@
 
 ######################################################################
 ######################################################################
+##
+##   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;
@@ -432,18 +376,20 @@
 	      $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('<p>'.&mt('No resources found').'.</p>');
     }
@@ -451,23 +397,6 @@
     $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 {
@@ -513,6 +442,14 @@
     }
 }
 
+sub untiehash {
+    if (tied(%hash)) {
+        untie(%hash);
+    }
+}
+
+} # End of course search scoping
+
 ######################################################################
 ######################################################################
 
@@ -528,7 +465,7 @@
 ######################################################################
 
 sub print_basic_search_form{
-    my ($r,$closebutton) = @_;
+    my ($r,$closebutton,$hidden_fields) = @_;
     my $bodytag=&Apache::loncommon::bodytag('Search');
     my $scrout=<<"ENDDOCUMENT";
 <html>
@@ -561,7 +498,7 @@
 ENDDOCUMENT
     $scrout.='&nbsp;'.&simpletextfield('basicexp',$ENV{'form.basicexp'},40).
         '&nbsp;';
-    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);
     my $srch=&mt('Search');
@@ -649,7 +586,7 @@
 ######################################################################
 
 sub print_advanced_search_form{
-    my ($r,$closebutton) = @_;
+    my ($r,$closebutton,$hidden_fields) = @_;
     my $advanced_buttons = <<"END";
 <p>
 <input type="submit" name="advancedsubmit" value='SEARCH' />
@@ -1036,7 +973,7 @@
 
 =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.
@@ -1048,7 +985,7 @@
 
 sub simplecheckbox{
     my ($name,$value,$checked)=@_;
-    $checked="checked" if ($value eq 'on');
+    $checked="checked" if ($value eq $checked || $value eq 'on');
     return '<input type="checkbox" name="'.$name.'" value="'.$value.'" '.
         $checked.' />';
 }
@@ -1301,7 +1238,7 @@
 ######################################################################
 ######################################################################
 sub parse_advanced_search {
-    my ($r,$closebutton)=@_;
+    my ($r,$closebutton,$hidden_fields)=@_;
     my $fillflag=0;
     my $pretty_search_string = "<br />\n";
     # Clean up fields for safety
@@ -1335,7 +1272,7 @@
 	}
     }
     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
@@ -1418,7 +1355,7 @@
 			);
     # 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
@@ -1853,6 +1790,7 @@
 ######################################################################
 sub print_sort_form {
     my ($r,$pretty_query_string) = @_;
+    my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
     ##
     my %SortableFields=&Apache::lonlocal::texthash( 
          id        => 'Default',
@@ -2098,6 +2036,7 @@
 ######################################################################
 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
@@ -2348,7 +2287,7 @@
 ######################################################################
 ######################################################################
 sub display_results {
-    my ($r,$importbutton,$closebutton) = @_;
+    my ($r,$importbutton,$closebutton,$diropendb) = @_;
     my $connection = $r->connection;
     $r->print(&search_results_header($importbutton,$closebutton));
     ##
@@ -2713,6 +2652,7 @@
 ######################################################################
 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
@@ -2816,6 +2756,7 @@
 ######################################################################
 ######################################################################
 sub search_status_header {
+    my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
     return <<ENDSTATUS;
 <html><head><title>Search Status</title></head>
 $bodytag
@@ -3086,7 +3027,8 @@
 ######################################################################
 ######################################################################
 sub output_blank_field_error {
-    my ($r,$closebutton,$parms)=@_;
+    my ($r,$closebutton,$parms,$hidden_fields)=@_;
+    my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
     # make query information persistent to allow for subsequent revision
     $r->print(<<BEGINNING);
 <html>
@@ -3135,8 +3077,9 @@
 ######################################################################
 ######################################################################
 sub output_date_error {
-    my ($r,$message,$closebutton)=@_;
+    my ($r,$message,$closebutton,$hidden_fields)=@_;
     # make query information persistent to allow for subsequent revision
+    my $bodytag=&Apache::loncommon::bodytag(undef,undef,undef,1);
     $r->print(<<RESULTS);
 <html>
 <head>

--matthew1072132837--